1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
22/** General purpose double triple for use as vertices, vectors and normals.
23 * Provides general math operations from addition through to cross products.
24 * No support yet added for double * Vec3d - is it necessary?
25 * Need to define a non-member non-friend operator* etc.
26 * Vec3d * double is okay
33 /** Data type of vector components.*/
34 typedef double value_type;
36 /** Number of vector components. */
37 enum { num_components = 3 };
41 /** Constructor that sets all components of the vector to zero */
42 Vec3d() { _v[0]=0.0; _v[1]=0.0; _v[2]=0.0;}
44 inline Vec3d(const Vec3f& vec) { _v[0]=vec._v[0]; _v[1]=vec._v[1]; _v[2]=vec._v[2];}
46 inline operator Vec3f() const { return Vec3f(static_cast<float>(_v[0]),static_cast<float>(_v[1]),static_cast<float>(_v[2]));}
48 Vec3d(value_type x,value_type y,value_type z) { _v[0]=x; _v[1]=y; _v[2]=z; }
49 Vec3d(const Vec2d& v2,value_type zz)
56 inline bool operator == (const Vec3d& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1] && _v[2]==v._v[2]; }
58 inline bool operator != (const Vec3d& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1] || _v[2]!=v._v[2]; }
60 inline bool operator < (const Vec3d& v) const
62 if (_v[0]<v._v[0]) return true;
63 else if (_v[0]>v._v[0]) return false;
64 else if (_v[1]<v._v[1]) return true;
65 else if (_v[1]>v._v[1]) return false;
66 else return (_v[2]<v._v[2]);
69 inline value_type* ptr() { return _v; }
70 inline const value_type* ptr() const { return _v; }
72 inline void set( value_type x, value_type y, value_type z)
74 _v[0]=x; _v[1]=y; _v[2]=z;
77 inline void set( const Vec3d& rhs)
79 _v[0]=rhs._v[0]; _v[1]=rhs._v[1]; _v[2]=rhs._v[2];
82 inline value_type& operator [] (int i) { return _v[i]; }
83 inline value_type operator [] (int i) const { return _v[i]; }
85 inline value_type& x() { return _v[0]; }
86 inline value_type& y() { return _v[1]; }
87 inline value_type& z() { return _v[2]; }
89 inline value_type x() const { return _v[0]; }
90 inline value_type y() const { return _v[1]; }
91 inline value_type z() const { return _v[2]; }
93 /** Returns true if all components have values that are not NaN. */
94 inline bool valid() const { return !isNaN(); }
95 /** Returns true if at least one component has value NaN. */
96 inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]) || osg::isNaN(_v[2]); }
99 inline value_type operator * (const Vec3d& rhs) const
101 return _v[0]*rhs._v[0]+_v[1]*rhs._v[1]+_v[2]*rhs._v[2];
104 /** Cross product. */
105 inline const Vec3d operator ^ (const Vec3d& rhs) const
107 return Vec3d(_v[1]*rhs._v[2]-_v[2]*rhs._v[1],
108 _v[2]*rhs._v[0]-_v[0]*rhs._v[2] ,
109 _v[0]*rhs._v[1]-_v[1]*rhs._v[0]);
112 /** Multiply by scalar. */
113 inline const Vec3d operator * (value_type rhs) const
115 return Vec3d(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs);
118 /** Unary multiply by scalar. */
119 inline Vec3d& operator *= (value_type rhs)
127 /** Divide by scalar. */
128 inline const Vec3d operator / (value_type rhs) const
130 return Vec3d(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs);
133 /** Unary divide by scalar. */
134 inline Vec3d& operator /= (value_type rhs)
142 /** Binary vector add. */
143 inline const Vec3d operator + (const Vec3d& rhs) const
145 return Vec3d(_v[0]+rhs._v[0], _v[1]+rhs._v[1], _v[2]+rhs._v[2]);
148 /** Unary vector add. Slightly more efficient because no temporary
149 * intermediate object.
151 inline Vec3d& operator += (const Vec3d& rhs)
159 /** Binary vector subtract. */
160 inline const Vec3d operator - (const Vec3d& rhs) const
162 return Vec3d(_v[0]-rhs._v[0], _v[1]-rhs._v[1], _v[2]-rhs._v[2]);
165 /** Unary vector subtract. */
166 inline Vec3d& operator -= (const Vec3d& rhs)
174 /** Negation operator. Returns the negative of the Vec3d. */
175 inline const Vec3d operator - () const
177 return Vec3d (-_v[0], -_v[1], -_v[2]);
180 /** Length of the vector = sqrt( vec . vec ) */
181 inline value_type length() const
183 return sqrt( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] );
186 /** Length squared of the vector = vec . vec */
187 inline value_type length2() const
189 return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2];
192 /** Normalize the vector so that it has length unity.
193 * Returns the previous length of the vector.
194 * If the vector is zero length, it is left unchanged and zero is returned.
196 inline value_type normalize()
198 value_type norm = Vec3d::length();
201 value_type inv = 1.0/norm;
209}; // end of class Vec3d
211/** multiply by vector components. */
212inline Vec3d componentMultiply(const Vec3d& lhs, const Vec3d& rhs)
214 return Vec3d(lhs[0]*rhs[0], lhs[1]*rhs[1], lhs[2]*rhs[2]);
217/** divide rhs components by rhs vector components. */
218inline Vec3d componentDivide(const Vec3d& lhs, const Vec3d& rhs)
220 return Vec3d(lhs[0]/rhs[0], lhs[1]/rhs[1], lhs[2]/rhs[2]);
223} // end of namespace osg