OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Vec3d.h
Go to the documentation of this file.
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2  *
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.
7  *
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.
12 */
13 
14 #ifndef OSG_VEC3D
15 #define OSG_VEC3D 1
16 
17 #include <osg/Vec2d>
18 #include <osg/Vec3f>
19 
20 namespace osg {
21 
29 class Vec3d
30 {
31  public:
32 
34  typedef double value_type;
35 
37  enum { num_components = 3 };
38 
39  value_type _v[3];
40 
42  Vec3d() { _v[0]=0.0; _v[1]=0.0; _v[2]=0.0;}
43 
44  inline Vec3d(const Vec3f& vec) { _v[0]=vec._v[0]; _v[1]=vec._v[1]; _v[2]=vec._v[2];}
45 
46  inline operator Vec3f() const { return Vec3f(static_cast<float>(_v[0]),static_cast<float>(_v[1]),static_cast<float>(_v[2]));}
47 
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)
50  {
51  _v[0] = v2[0];
52  _v[1] = v2[1];
53  _v[2] = zz;
54  }
55 
56  inline bool operator == (const Vec3d& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1] && _v[2]==v._v[2]; }
57 
58  inline bool operator != (const Vec3d& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1] || _v[2]!=v._v[2]; }
59 
60  inline bool operator < (const Vec3d& v) const
61  {
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]);
67  }
68 
69  inline value_type* ptr() { return _v; }
70  inline const value_type* ptr() const { return _v; }
71 
72  inline void set( value_type x, value_type y, value_type z)
73  {
74  _v[0]=x; _v[1]=y; _v[2]=z;
75  }
76 
77  inline void set( const Vec3d& rhs)
78  {
79  _v[0]=rhs._v[0]; _v[1]=rhs._v[1]; _v[2]=rhs._v[2];
80  }
81 
82  inline value_type& operator [] (int i) { return _v[i]; }
83  inline value_type operator [] (int i) const { return _v[i]; }
84 
85  inline value_type& x() { return _v[0]; }
86  inline value_type& y() { return _v[1]; }
87  inline value_type& z() { return _v[2]; }
88 
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]; }
92 
94  inline bool valid() const { return !isNaN(); }
96  inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]) || osg::isNaN(_v[2]); }
97 
99  inline value_type operator * (const Vec3d& rhs) const
100  {
101  return _v[0]*rhs._v[0]+_v[1]*rhs._v[1]+_v[2]*rhs._v[2];
102  }
103 
105  inline const Vec3d operator ^ (const Vec3d& rhs) const
106  {
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]);
110  }
111 
113  inline const Vec3d operator * (value_type rhs) const
114  {
115  return Vec3d(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs);
116  }
117 
119  inline Vec3d& operator *= (value_type rhs)
120  {
121  _v[0]*=rhs;
122  _v[1]*=rhs;
123  _v[2]*=rhs;
124  return *this;
125  }
126 
128  inline const Vec3d operator / (value_type rhs) const
129  {
130  return Vec3d(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs);
131  }
132 
134  inline Vec3d& operator /= (value_type rhs)
135  {
136  _v[0]/=rhs;
137  _v[1]/=rhs;
138  _v[2]/=rhs;
139  return *this;
140  }
141 
143  inline const Vec3d operator + (const Vec3d& rhs) const
144  {
145  return Vec3d(_v[0]+rhs._v[0], _v[1]+rhs._v[1], _v[2]+rhs._v[2]);
146  }
147 
151  inline Vec3d& operator += (const Vec3d& rhs)
152  {
153  _v[0] += rhs._v[0];
154  _v[1] += rhs._v[1];
155  _v[2] += rhs._v[2];
156  return *this;
157  }
158 
160  inline const Vec3d operator - (const Vec3d& rhs) const
161  {
162  return Vec3d(_v[0]-rhs._v[0], _v[1]-rhs._v[1], _v[2]-rhs._v[2]);
163  }
164 
166  inline Vec3d& operator -= (const Vec3d& rhs)
167  {
168  _v[0]-=rhs._v[0];
169  _v[1]-=rhs._v[1];
170  _v[2]-=rhs._v[2];
171  return *this;
172  }
173 
175  inline const Vec3d operator - () const
176  {
177  return Vec3d (-_v[0], -_v[1], -_v[2]);
178  }
179 
181  inline value_type length() const
182  {
183  return sqrt( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] );
184  }
185 
187  inline value_type length2() const
188  {
189  return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2];
190  }
191 
196  inline value_type normalize()
197  {
198  value_type norm = Vec3d::length();
199  if (norm>0.0)
200  {
201  value_type inv = 1.0/norm;
202  _v[0] *= inv;
203  _v[1] *= inv;
204  _v[2] *= inv;
205  }
206  return( norm );
207  }
208 
209 }; // end of class Vec3d
210 
212 inline Vec3d componentMultiply(const Vec3d& lhs, const Vec3d& rhs)
213 {
214  return Vec3d(lhs[0]*rhs[0], lhs[1]*rhs[1], lhs[2]*rhs[2]);
215 }
216 
218 inline Vec3d componentDivide(const Vec3d& lhs, const Vec3d& rhs)
219 {
220  return Vec3d(lhs[0]/rhs[0], lhs[1]/rhs[1], lhs[2]/rhs[2]);
221 }
222 
223 } // end of namespace osg
224 
225 #endif
value_type & x()
Definition: Vec3d.h:85
value_type x() const
Definition: Vec3d.h:89
value_type z() const
Definition: Vec3d.h:91
Vec3d(const Vec3f &vec)
Definition: Vec3d.h:44
value_type * ptr()
Definition: Vec3d.h:69
bool isNaN() const
Definition: Vec3d.h:96
const Vec3d operator-() const
Definition: Vec3d.h:175
value_type & z()
Definition: Vec3d.h:87
Vec3d(value_type x, value_type y, value_type z)
Definition: Vec3d.h:48
Vec3d(const Vec2d &v2, value_type zz)
Definition: Vec3d.h:49
bool operator!=(const Vec3d &v) const
Definition: Vec3d.h:58
double value_type
Definition: Vec3d.h:34
const Vec3d operator^(const Vec3d &rhs) const
Definition: Vec3d.h:105
value_type _v[3]
Definition: Vec3f.h:38
bool valid() const
Definition: Vec3d.h:94
Vec3d & operator/=(value_type rhs)
Definition: Vec3d.h:134
bool operator==(const Vec3d &v) const
Definition: Vec3d.h:56
Vec2d componentDivide(const Vec2d &lhs, const Vec2d &rhs)
Definition: Vec2d.h:187
value_type y() const
Definition: Vec3d.h:90
value_type & operator[](int i)
Definition: Vec3d.h:82
value_type length2() const
Definition: Vec3d.h:187
value_type _v[3]
Definition: Vec3d.h:39
Vec3d & operator-=(const Vec3d &rhs)
Definition: Vec3d.h:166
bool isNaN(float v)
Definition: Math.h:114
void set(value_type x, value_type y, value_type z)
Definition: Vec3d.h:72
const value_type * ptr() const
Definition: Vec3d.h:70
value_type operator*(const Vec3d &rhs) const
Definition: Vec3d.h:99
Vec3d & operator+=(const Vec3d &rhs)
Definition: Vec3d.h:151
const Vec3d operator/(value_type rhs) const
Definition: Vec3d.h:128
Vec3d()
Definition: Vec3d.h:42
value_type normalize()
Definition: Vec3d.h:196
bool operator<(const Vec3d &v) const
Definition: Vec3d.h:60
const Vec3d operator+(const Vec3d &rhs) const
Definition: Vec3d.h:143
Definition: AlphaFunc.h:19
value_type & y()
Definition: Vec3d.h:86
Vec3d & operator*=(value_type rhs)
Definition: Vec3d.h:119
value_type length() const
Definition: Vec3d.h:181
Vec2d componentMultiply(const Vec2d &lhs, const Vec2d &rhs)
Definition: Vec2d.h:181
void set(const Vec3d &rhs)
Definition: Vec3d.h:77