OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Vec3f.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_VEC3F
15 #define OSG_VEC3F 1
16 
17 #include <osg/Vec2f>
18 #include <osg/Math>
19 
20 namespace osg {
21 
28 class Vec3f
29 {
30  public:
31 
33  typedef float value_type;
34 
36  enum { num_components = 3 };
37 
38  value_type _v[3];
39 
41  Vec3f() { _v[0]=0.0f; _v[1]=0.0f; _v[2]=0.0f;}
42  Vec3f(value_type x,value_type y,value_type z) { _v[0]=x; _v[1]=y; _v[2]=z; }
43  Vec3f(const Vec2f& v2,value_type zz)
44  {
45  _v[0] = v2[0];
46  _v[1] = v2[1];
47  _v[2] = zz;
48  }
49 
50 
51  inline bool operator == (const Vec3f& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1] && _v[2]==v._v[2]; }
52 
53  inline bool operator != (const Vec3f& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1] || _v[2]!=v._v[2]; }
54 
55  inline bool operator < (const Vec3f& v) const
56  {
57  if (_v[0]<v._v[0]) return true;
58  else if (_v[0]>v._v[0]) return false;
59  else if (_v[1]<v._v[1]) return true;
60  else if (_v[1]>v._v[1]) return false;
61  else return (_v[2]<v._v[2]);
62  }
63 
64  inline value_type* ptr() { return _v; }
65  inline const value_type* ptr() const { return _v; }
66 
67  inline void set( value_type x, value_type y, value_type z)
68  {
69  _v[0]=x; _v[1]=y; _v[2]=z;
70  }
71 
72  inline void set( const Vec3f& rhs)
73  {
74  _v[0]=rhs._v[0]; _v[1]=rhs._v[1]; _v[2]=rhs._v[2];
75  }
76 
77  inline value_type& operator [] (int i) { return _v[i]; }
78  inline value_type operator [] (int i) const { return _v[i]; }
79 
80  inline value_type& x() { return _v[0]; }
81  inline value_type& y() { return _v[1]; }
82  inline value_type& z() { return _v[2]; }
83 
84  inline value_type x() const { return _v[0]; }
85  inline value_type y() const { return _v[1]; }
86  inline value_type z() const { return _v[2]; }
87 
89  inline bool valid() const { return !isNaN(); }
91  inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]) || osg::isNaN(_v[2]); }
92 
94  inline value_type operator * (const Vec3f& rhs) const
95  {
96  return _v[0]*rhs._v[0]+_v[1]*rhs._v[1]+_v[2]*rhs._v[2];
97  }
98 
100  inline const Vec3f operator ^ (const Vec3f& rhs) const
101  {
102  return Vec3f(_v[1]*rhs._v[2]-_v[2]*rhs._v[1],
103  _v[2]*rhs._v[0]-_v[0]*rhs._v[2] ,
104  _v[0]*rhs._v[1]-_v[1]*rhs._v[0]);
105  }
106 
108  inline const Vec3f operator * (value_type rhs) const
109  {
110  return Vec3f(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs);
111  }
112 
114  inline Vec3f& operator *= (value_type rhs)
115  {
116  _v[0]*=rhs;
117  _v[1]*=rhs;
118  _v[2]*=rhs;
119  return *this;
120  }
121 
123  inline const Vec3f operator / (value_type rhs) const
124  {
125  return Vec3f(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs);
126  }
127 
129  inline Vec3f& operator /= (value_type rhs)
130  {
131  _v[0]/=rhs;
132  _v[1]/=rhs;
133  _v[2]/=rhs;
134  return *this;
135  }
136 
138  inline const Vec3f operator + (const Vec3f& rhs) const
139  {
140  return Vec3f(_v[0]+rhs._v[0], _v[1]+rhs._v[1], _v[2]+rhs._v[2]);
141  }
142 
146  inline Vec3f& operator += (const Vec3f& rhs)
147  {
148  _v[0] += rhs._v[0];
149  _v[1] += rhs._v[1];
150  _v[2] += rhs._v[2];
151  return *this;
152  }
153 
155  inline const Vec3f operator - (const Vec3f& rhs) const
156  {
157  return Vec3f(_v[0]-rhs._v[0], _v[1]-rhs._v[1], _v[2]-rhs._v[2]);
158  }
159 
161  inline Vec3f& operator -= (const Vec3f& rhs)
162  {
163  _v[0]-=rhs._v[0];
164  _v[1]-=rhs._v[1];
165  _v[2]-=rhs._v[2];
166  return *this;
167  }
168 
170  inline const Vec3f operator - () const
171  {
172  return Vec3f (-_v[0], -_v[1], -_v[2]);
173  }
174 
176  inline value_type length() const
177  {
178  return sqrtf( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] );
179  }
180 
182  inline value_type length2() const
183  {
184  return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2];
185  }
186 
190  inline value_type normalize()
191  {
192  value_type norm = Vec3f::length();
193  if (norm>0.0)
194  {
195  value_type inv = 1.0f/norm;
196  _v[0] *= inv;
197  _v[1] *= inv;
198  _v[2] *= inv;
199  }
200  return( norm );
201  }
202 
203 }; // end of class Vec3f
204 
206 inline Vec3f componentMultiply(const Vec3f& lhs, const Vec3f& rhs)
207 {
208  return Vec3f(lhs[0]*rhs[0], lhs[1]*rhs[1], lhs[2]*rhs[2]);
209 }
210 
212 inline Vec3f componentDivide(const Vec3f& lhs, const Vec3f& rhs)
213 {
214  return Vec3f(lhs[0]/rhs[0], lhs[1]/rhs[1], lhs[2]/rhs[2]);
215 }
216 
217 const Vec3f X_AXIS(1.0,0.0,0.0);
218 const Vec3f Y_AXIS(0.0,1.0,0.0);
219 const Vec3f Z_AXIS(0.0,0.0,1.0);
220 
221 } // end of namespace osg
222 
223 #endif
224 
const Vec3f operator^(const Vec3f &rhs) const
Definition: Vec3f.h:100
value_type * ptr()
Definition: Vec3f.h:64
Vec3f(const Vec2f &v2, value_type zz)
Definition: Vec3f.h:43
const Vec3f X_AXIS(1.0, 0.0, 0.0)
value_type normalize()
Definition: Vec3f.h:190
value_type length2() const
Definition: Vec3f.h:182
value_type & x()
Definition: Vec3f.h:80
Vec3f & operator*=(value_type rhs)
Definition: Vec3f.h:114
value_type & z()
Definition: Vec3f.h:82
value_type x() const
Definition: Vec3f.h:84
const Vec3f operator/(value_type rhs) const
Definition: Vec3f.h:123
value_type _v[3]
Definition: Vec3f.h:38
Vec3f & operator-=(const Vec3f &rhs)
Definition: Vec3f.h:161
bool operator==(const Vec3f &v) const
Definition: Vec3f.h:51
value_type z() const
Definition: Vec3f.h:86
Vec2d componentDivide(const Vec2d &lhs, const Vec2d &rhs)
Definition: Vec2d.h:187
float value_type
Definition: Vec3f.h:33
const value_type * ptr() const
Definition: Vec3f.h:65
bool isNaN(float v)
Definition: Math.h:114
void set(const Vec3f &rhs)
Definition: Vec3f.h:72
Vec3f & operator+=(const Vec3f &rhs)
Definition: Vec3f.h:146
const Vec3f Y_AXIS(0.0, 1.0, 0.0)
void set(value_type x, value_type y, value_type z)
Definition: Vec3f.h:67
value_type & operator[](int i)
Definition: Vec3f.h:77
bool operator!=(const Vec3f &v) const
Definition: Vec3f.h:53
Vec3f & operator/=(value_type rhs)
Definition: Vec3f.h:129
const Vec3f Z_AXIS(0.0, 0.0, 1.0)
value_type operator*(const Vec3f &rhs) const
Definition: Vec3f.h:94
value_type & y()
Definition: Vec3f.h:81
bool isNaN() const
Definition: Vec3f.h:91
value_type y() const
Definition: Vec3f.h:85
Vec3f(value_type x, value_type y, value_type z)
Definition: Vec3f.h:42
const Vec3f operator-() const
Definition: Vec3f.h:170
Definition: AlphaFunc.h:19
bool valid() const
Definition: Vec3f.h:89
value_type length() const
Definition: Vec3f.h:176
bool operator<(const Vec3f &v) const
Definition: Vec3f.h:55
const Vec3f operator+(const Vec3f &rhs) const
Definition: Vec3f.h:138
Vec2d componentMultiply(const Vec2d &lhs, const Vec2d &rhs)
Definition: Vec2d.h:181
Vec3f()
Definition: Vec3f.h:41