D3vector.h 3.99 KB
 Francois Gygi committed Dec 01, 2010 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 //////////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2008 The Regents of the University of California // // This file is part of Qbox // // Qbox is distributed under the terms of the GNU General Public License // as published by the Free Software Foundation, either version 2 of // the License, or (at your option) any later version. // See the file COPYING in the root directory of this distribution // or . // //////////////////////////////////////////////////////////////////////////////// // // D3vector.h // // double 3-vectors // //////////////////////////////////////////////////////////////////////////////// // $Id: D3vector.h,v 1.8 2008-09-08 15:56:18 fgygi Exp$ #ifndef D3VECTOR_H #define D3VECTOR_H #include #include #include class D3vector { public: double x, y, z; // explicit constructor to avoid implicit conversion from double to D3vector explicit D3vector(const double& xv, const double& yv, const double& zv) : x(xv), y(yv), z(zv) {} explicit D3vector(void) : x(0.0), y(0.0), z(0.0) {} explicit D3vector(const double* r) : x(r[0]), y(r[1]), z(r[2]) {} double& operator[](const int &i) { assert(i>=0 && i <3); if ( i == 0 ) return x; else if ( i == 1 ) return y; else return z; } bool operator==(const D3vector &rhs) const { return x == rhs.x && y == rhs.y && z == rhs.z; } bool operator!=(const D3vector &rhs) const { return x != rhs.x || y != rhs.y || z != rhs.z; } D3vector& operator += ( const D3vector& rhs ) { x += rhs.x; y += rhs.y; z += rhs.z; return *this; } D3vector& operator -= ( const D3vector& rhs ) { x -= rhs.x; y -= rhs.y; z -= rhs.z; return *this; } D3vector& operator *= ( const double& rhs ) { x *= rhs; y *= rhs; z *= rhs; return *this; } D3vector& operator /= ( const double& rhs ) { x /= rhs; y /= rhs; z /= rhs; return *this; } friend const D3vector operator + (const D3vector& lhs, const D3vector& rhs ) { return D3vector(lhs) += rhs; } friend const D3vector operator - ( const D3vector& a, const D3vector& b ) { return D3vector(a) -= b; } friend D3vector operator - ( const D3vector& a ) // unary minus { return D3vector( -a.x, -a.y, -a.z ); } friend D3vector operator * ( const double& a, const D3vector& b ) { return D3vector(b) *= a; } friend D3vector operator * ( const D3vector& a, const double& b ) { return D3vector(a) *= b; } friend D3vector operator / ( const D3vector& a, const double& b ) { return D3vector(a) /= b; } // scalar product friend double operator * ( const D3vector& a, const D3vector& b ) { return a.x * b.x + a.y * b.y + a.z * b.z ; } friend D3vector operator ^ ( const D3vector& a, const D3vector& b ) { return D3vector( a.y * b.z - a.z * b.y , a.z * b.x - a.x * b.z , a.x * b.y - a.y * b.x ); } friend D3vector rotate ( const D3vector& x, const D3vector& w ) { if ( length(x) == 0.0 ) return x; // x has zero length double theta = length( w ); // rotate by zero if ( theta == 0.0 ) return x; D3vector ew = normalized ( w ); D3vector v = w ^ x; if ( length( v ) == 0.0 ) return x; // x is parallel to the rotation axis v = normalized( v ); D3vector u = v ^ ew; double p = x * u; return (x*ew)*ew + p*cos(theta)*u + p*sin(theta)*v ; } friend double length( const D3vector& a ) { return sqrt( a.x * a.x + a.y * a.y + a.z * a.z ); } friend double norm( const D3vector& a ) { return a.x * a.x + a.y * a.y + a.z * a.z; } friend D3vector normalized( const D3vector a ) { return a / length( a ); } friend std::ostream& operator << ( std::ostream& os, const D3vector& v ) { os << v.x << " " << v.y << " " << v.z; return os; } friend std::istream& operator >> ( std::istream& is, D3vector& v ) { is >> v.x >> v.y >> v.z ; return is; } }; #endif