📄 globals.hpp
字号:
/* * =========================================================================== * PRODUCTION $Log: globals.hpp,v $ * PRODUCTION Revision 1000.2 2004/06/01 19:48:46 gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.8 * PRODUCTION * =========================================================================== */#ifndef GUI_MATH___GLOBALS__HPP#define GUI_MATH___GLOBALS__HPP/* $Id: globals.hpp,v 1000.2 2004/06/01 19:48:46 gouriano Exp $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software / database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software / database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Authors: Mike DiCuccio * * File Description: * */#include <corelib/ncbistd.hpp>#include <vector>/** @addtogroup GUI_MATH * * @{ */BEGIN_NCBI_SCOPE// predeclarations of the types we usetemplate <class T> class CVect2;template <class T> class CVect3;template <class T> class CVect4;template <class T> class CMatrix3;template <class T> class CMatrix4;END_NCBI_SCOPE// Promotion rules// we keep these in a separate file because they're long#include <gui/math/promote.hpp>#include <gui/math/vect2.hpp>#include <gui/math/vect3.hpp>#include <gui/math/vect4.hpp>#include <gui/math/matrix3.hpp>#include <gui/math/matrix4.hpp>// general utilities#include <math.h>#include <gui/math/math.hpp>BEGIN_NCBI_SCOPE////// Global operators//////// // CVect<> templates// ////// global addition: CVect*<> + CVect*<>//template <class T, class U>inline CVect2< NCBI_PROMOTE(T,U) >operator+ (const CVect2<T>& v1, const CVect2<U>& v2){ return CVect2< NCBI_PROMOTE(T,U) > (v1[0]+v2[0], v1[1]+v2[1]);}template <class T, class U>inline CVect3< NCBI_PROMOTE(T,U) >operator+ (const CVect3<T>& v1, const CVect3<U>& v2){ return CVect3< NCBI_PROMOTE(T,U) > (v1[0]+v2[0], v1[1]+v2[1], v1[2]+v2[2]);}template <class T, class U>inline CVect4< NCBI_PROMOTE(T,U) >operator+ (const CVect4<T>& v1, const CVect4<U>& v2){ return CVect4< NCBI_PROMOTE(T,U) > (v1[0]+v2[0], v1[1]+v2[1], v1[2]+v2[2], v1[3]+v2[3]);}//// global addition: const + CVect*<>// we do this with macros because not all compilers support partial template// specialization//#define NCBI_ADD_VECT2(type) \template <class U> \inline CVect2< NCBI_PROMOTE(type,U) > \operator+ (type v1, const CVect2<U>& v2) \{ \ return \ CVect2< NCBI_PROMOTE(type,U) > \ (v1 + v2[0], v1 + v2[1]); \} \template <class T> \inline CVect2< NCBI_PROMOTE(T,type) > \operator+ (const CVect2<T>& v1, type v2) \{ \ return \ CVect2< NCBI_PROMOTE(T,type) > \ (v1[0] + v2, v1[1] + v2); \}NCBI_ADD_VECT2(int)NCBI_ADD_VECT2(float)NCBI_ADD_VECT2(double)#undef NCBI_ADD_VECT2#define NCBI_ADD_VECT3(type) \template <class U> \inline CVect3< NCBI_PROMOTE(type,U) > \operator+ (type v1, const CVect3<U>& v2) \{ \ return \ CVect3< NCBI_PROMOTE(type,U) > \ (v1 + v2[0], v1 + v2[1], v1 + v2[2]); \} \template <class T> \inline CVect3< NCBI_PROMOTE(T,type) > \operator+ (const CVect3<T>& v1, type v2) \{ \ return \ CVect3< NCBI_PROMOTE(T,type) > \ (v1[0] + v2, v1[1] + v2, v1[2] + v2); \}NCBI_ADD_VECT3(int)NCBI_ADD_VECT3(float)NCBI_ADD_VECT3(double)#undef NCBI_ADD_VECT3#define NCBI_ADD_VECT4(type) \template <class U> \inline CVect4< NCBI_PROMOTE(type,U) > \operator+ (type v1, const CVect4<U>& v2) \{ \ return \ CVect4< NCBI_PROMOTE(type,U) > \ (v1 + v2[0], v1 + v2[1], v1 + v2[2], v1 + v2[3]); \} \template <class T> \inline CVect4< NCBI_PROMOTE(T,type) > \operator+ (const CVect4<T>& v1, type v2) \{ \ return \ CVect4< NCBI_PROMOTE(T,type) > \ (v1[0] + v2, v1[1] + v2, v1[2] + v2, v1[3] + v2); \}NCBI_ADD_VECT4(int)NCBI_ADD_VECT4(float)NCBI_ADD_VECT4(double)#undef NCBI_ADD_VECT4//// global unary negation//template <class T>inline CVect2<T>operator- (const CVect2<T>& v){ return CVect2<T> (-v[0], -v[1]);}template <class T>inline CVect3<T>operator- (const CVect3<T>& v){ return CVect3<T> (-v[0], -v[1], -v[2]);}template <class T>inline CVect4<T>operator- (const CVect4<T>& v){ return CVect4<T> (-v[0], -v[1], -v[2], -v[3]);}//// global subtraction: CVect*<> - CVect*<>//template <class T, class U>inline CVect2< NCBI_PROMOTE(T,U) >operator- (const CVect2<T>& v1, const CVect2<U>& v2){ return CVect2< NCBI_PROMOTE(T,U) > (v1[0]-v2[0], v1[1]-v2[1]);}template <class T, class U>inline CVect3< NCBI_PROMOTE(T,U) >operator- (const CVect3<T>& v1, const CVect3<U>& v2){ return CVect3< NCBI_PROMOTE(T,U) > (v1[0]-v2[0], v1[1]-v2[1], v1[2]-v2[2]);}template <class T, class U>inline CVect4< NCBI_PROMOTE(T,U) >operator- (const CVect4<T>& v1, const CVect4<U>& v2){ return CVect4< NCBI_PROMOTE(T,U) > (v1[0]-v2[0], v1[1]-v2[1], v1[2]-v2[2], v1[3]-v2[3]);}//// global subtraction: const + CVect*<>// we do this with macros because not all compilers support partial template// specialization//#define NCBI_SUBTRACT_VECT2(type) \template <class U> \inline CVect2< NCBI_PROMOTE(type,U) > \operator- (type v1, const CVect2<U>& v2) \{ \ return \ CVect2< NCBI_PROMOTE(type,U) > \ (v1 - v2[0], v1 - v2[1]); \} \template <class T> \inline CVect2< NCBI_PROMOTE(T,type) > \operator- (const CVect2<T>& v1, type v2) \{ \ return \ CVect2< NCBI_PROMOTE(T,type) > \ (v1[0] - v2, v1[1] - v2); \}NCBI_SUBTRACT_VECT2(int)NCBI_SUBTRACT_VECT2(float)NCBI_SUBTRACT_VECT2(double)#undef NCBI_SUBTRACT_VECT2#define NCBI_SUBTRACT_VECT3(type) \template <class U> \inline CVect3< NCBI_PROMOTE(type,U) > \operator- (type v1, const CVect3<U>& v2) \{ \ return \ CVect3< NCBI_PROMOTE(type,U) > \ (v1 - v2[0], v1 - v2[1], v1 - v2[2]); \} \template <class T> \inline CVect3< NCBI_PROMOTE(T,type) > \operator- (const CVect3<T>& v1, type v2) \{ \ return \ CVect3< NCBI_PROMOTE(T,type) > \ (v1[0] - v2, v1[1] - v2, v1[2] - v2); \}NCBI_SUBTRACT_VECT3(int)NCBI_SUBTRACT_VECT3(float)NCBI_SUBTRACT_VECT3(double)#undef NCBI_SUBTRACT_VECT3#define NCBI_SUBTRACT_VECT4(type) \template <class U> \inline CVect4< NCBI_PROMOTE(type,U) > \operator- (type v1, const CVect4<U>& v2) \{ \ return \ CVect4< NCBI_PROMOTE(type,U) > \ (v1 - v2[0], v1 - v2[1], v1 - v2[2], v1 - v2[3]); \} \template <class T> \inline CVect4< NCBI_PROMOTE(T,type) > \operator- (const CVect4<T>& v1, type v2) \{ \ return \ CVect4< NCBI_PROMOTE(T,type) > \ (v1[0] - v2, v1[1] - v2, v1[2] - v2, v1[3] - v2); \}NCBI_SUBTRACT_VECT4(int)NCBI_SUBTRACT_VECT4(float)NCBI_SUBTRACT_VECT4(double)#undef NCBI_SUBTRACT_VECT4//// global subtraction: const + CVect*<>// we do this with macros because not all compilers support partial template// specialization//#define NCBI_MULTIPLY_VECT2(type) \template <class U> \inline CVect2< NCBI_PROMOTE(type,U) > \operator* (type v1, const CVect2<U>& v2) \{ \ return \ CVect2< NCBI_PROMOTE(type,U) > \ (v1 * v2[0], v1 * v2[1]); \} \template <class T> \inline CVect2< NCBI_PROMOTE(T,type) > \operator* (const CVect2<T>& v1, type v2) \{ \ return \ CVect2< NCBI_PROMOTE(T,type) > \ (v1[0] * v2, v1[1] * v2); \}NCBI_MULTIPLY_VECT2(int)NCBI_MULTIPLY_VECT2(float)NCBI_MULTIPLY_VECT2(double)#undef NCBI_MULTIPLY_VECT2#define NCBI_MULTIPLY_VECT3(type) \template <class U> \inline CVect3< NCBI_PROMOTE(type,U) > \operator* (type v1, const CVect3<U>& v2) \{ \ return \ CVect3< NCBI_PROMOTE(type,U) > \ (v1 * v2[0], v1 * v2[1], v1 * v2[2]); \} \template <class T> \inline CVect3< NCBI_PROMOTE(T,type) > \operator* (const CVect3<T>& v1, type v2) \{ \ return \ CVect3< NCBI_PROMOTE(T,type) > \ (v1[0] * v2, v1[1] * v2, v1[2] * v2); \}NCBI_MULTIPLY_VECT3(int)NCBI_MULTIPLY_VECT3(float)NCBI_MULTIPLY_VECT3(double)#undef NCBI_MULTIPLY_VECT3#define NCBI_MULTIPLY_VECT4(type) \template <class U> \inline CVect4< NCBI_PROMOTE(type,U) > \operator* (type v1, const CVect4<U>& v2) \{ \ return \ CVect4< NCBI_PROMOTE(type,U) > \ (v1 * v2[0], v1 * v2[1], v1 * v2[2], v1 * v2[3]); \} \template <class T> \inline CVect4< NCBI_PROMOTE(T,type) > \operator* (const CVect4<T>& v1, type v2) \{ \ return \ CVect4< NCBI_PROMOTE(T,type) > \ (v1[0] * v2, v1[1] * v2, v1[2] * v2, v1[3] * v2); \}NCBI_MULTIPLY_VECT4(int)NCBI_MULTIPLY_VECT4(float)NCBI_MULTIPLY_VECT4(double)#undef NCBI_MULTIPLY_VECT4//// global multiplication: CVect*<> * CVect*<>// we define this as dot!//template <class T, class U>inline NCBI_PROMOTE(T,U)operator* (const CVect2<T>& v1, const CVect2<U>& v2){ return (v1[0] * v2[0] + v1[1] * v2[1]);}template <class T, class U>inline NCBI_PROMOTE(T,U)operator* (const CVect3<T>& v1, const CVect3<U>& v2){ return (v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2]);}template <class T, class U>inline NCBI_PROMOTE(T,U)operator* (const CVect4<T>& v1, const CVect4<U>& v2){ return (v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2] + v1[3] * v2[3]);}//// global division: CVect*<> / const//template <class T, class U>inline CVect2< NCBI_PROMOTE(T,U) >operator/ (const CVect2<T>& v1, U v2){ v2 = T(1) / v2; return CVect2< NCBI_PROMOTE(T,U) > (v1[0]*v2, v1[1]*v2);}template <class T, class U>inline CVect3< NCBI_PROMOTE(T,U) >operator/ (const CVect3<T>& v1, U v2){ v2 = T(1) / v2; return CVect3< NCBI_PROMOTE(T,U) > (v1[0]*v2, v1[1]*v2, v1[2]*v2);}template <class T, class U>inline CVect4< NCBI_PROMOTE(T,U) >operator/ (const CVect4<T>& v1, U v2){ v2 = T(1) / v2; return CVect4< NCBI_PROMOTE(T,U) > (v1[0]*v2, v1[1]*v2, v1[2]*v2, v1[3]*v2);}//// global comparison: equals// template <class T, class U>inline booloperator== (const CVect2<T>& v1, const CVect2<U>& v2){ return (v1[0] == v2[0] && v1[1] == v2[1]);}template <class T, class U>inline booloperator== (const CVect3<T>& v1, const CVect3<U>& v2){ return (v1[0] == v2[0] && v1[1] == v2[1] &&
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -