OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Vec4d.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_VEC4D
15 #define OSG_VEC4D 1
16 
17 #include <osg/Vec3d>
18 #include <osg/Vec4f>
19 
20 namespace osg {
21 
28 class Vec4d
29 {
30  public:
31 
33  typedef double value_type;
34 
36  enum { num_components = 4 };
37 
38  value_type _v[4];
39 
41  Vec4d() { _v[0]=0.0; _v[1]=0.0; _v[2]=0.0; _v[3]=0.0; }
42 
43  Vec4d(value_type x, value_type y, value_type z, value_type w)
44  {
45  _v[0]=x;
46  _v[1]=y;
47  _v[2]=z;
48  _v[3]=w;
49  }
50 
51  Vec4d(const Vec3d& v3,value_type w)
52  {
53  _v[0]=v3[0];
54  _v[1]=v3[1];
55  _v[2]=v3[2];
56  _v[3]=w;
57  }
58 
59  inline Vec4d(const Vec4f& vec) { _v[0]=vec._v[0]; _v[1]=vec._v[1]; _v[2]=vec._v[2]; _v[3]=vec._v[3];}
60 
61  inline operator Vec4f() const { return Vec4f(static_cast<float>(_v[0]),static_cast<float>(_v[1]),static_cast<float>(_v[2]),static_cast<float>(_v[3]));}
62 
63 
64  inline bool operator == (const Vec4d& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1] && _v[2]==v._v[2] && _v[3]==v._v[3]; }
65 
66  inline bool operator != (const Vec4d& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1] || _v[2]!=v._v[2] || _v[3]!=v._v[3]; }
67 
68  inline bool operator < (const Vec4d& v) const
69  {
70  if (_v[0]<v._v[0]) return true;
71  else if (_v[0]>v._v[0]) return false;
72  else if (_v[1]<v._v[1]) return true;
73  else if (_v[1]>v._v[1]) return false;
74  else if (_v[2]<v._v[2]) return true;
75  else if (_v[2]>v._v[2]) return false;
76  else return (_v[3]<v._v[3]);
77  }
78 
79  inline value_type* ptr() { return _v; }
80  inline const value_type* ptr() const { return _v; }
81 
82  inline void set( value_type x, value_type y, value_type z, value_type w)
83  {
84  _v[0]=x; _v[1]=y; _v[2]=z; _v[3]=w;
85  }
86 
87  inline value_type& operator [] (unsigned int i) { return _v[i]; }
88  inline value_type operator [] (unsigned int i) const { return _v[i]; }
89 
90  inline value_type& x() { return _v[0]; }
91  inline value_type& y() { return _v[1]; }
92  inline value_type& z() { return _v[2]; }
93  inline value_type& w() { return _v[3]; }
94 
95  inline value_type x() const { return _v[0]; }
96  inline value_type y() const { return _v[1]; }
97  inline value_type z() const { return _v[2]; }
98  inline value_type w() const { return _v[3]; }
99 
100  inline value_type& r() { return _v[0]; }
101  inline value_type& g() { return _v[1]; }
102  inline value_type& b() { return _v[2]; }
103  inline value_type& a() { return _v[3]; }
104 
105  inline value_type r() const { return _v[0]; }
106  inline value_type g() const { return _v[1]; }
107  inline value_type b() const { return _v[2]; }
108  inline value_type a() const { return _v[3]; }
109 
110 
111  inline unsigned int asABGR() const
112  {
113  return (unsigned int)clampTo((_v[0]*255.0),0.0,255.0)<<24 |
114  (unsigned int)clampTo((_v[1]*255.0),0.0,255.0)<<16 |
115  (unsigned int)clampTo((_v[2]*255.0),0.0,255.0)<<8 |
116  (unsigned int)clampTo((_v[3]*255.0),0.0,255.0);
117  }
118 
119  inline unsigned int asRGBA() const
120  {
121  return (unsigned int)clampTo((_v[3]*255.0),0.0,255.0)<<24 |
122  (unsigned int)clampTo((_v[2]*255.0),0.0,255.0)<<16 |
123  (unsigned int)clampTo((_v[1]*255.0),0.0,255.0)<<8 |
124  (unsigned int)clampTo((_v[0]*255.0),0.0,255.0);
125  }
126 
128  inline bool valid() const { return !isNaN(); }
130  inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]) || osg::isNaN(_v[2]) || osg::isNaN(_v[3]); }
131 
133  inline value_type operator * (const Vec4d& rhs) const
134  {
135  return _v[0]*rhs._v[0]+
136  _v[1]*rhs._v[1]+
137  _v[2]*rhs._v[2]+
138  _v[3]*rhs._v[3] ;
139  }
140 
142  inline Vec4d operator * (value_type rhs) const
143  {
144  return Vec4d(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs, _v[3]*rhs);
145  }
146 
148  inline Vec4d& operator *= (value_type rhs)
149  {
150  _v[0]*=rhs;
151  _v[1]*=rhs;
152  _v[2]*=rhs;
153  _v[3]*=rhs;
154  return *this;
155  }
156 
158  inline Vec4d operator / (value_type rhs) const
159  {
160  return Vec4d(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs, _v[3]/rhs);
161  }
162 
164  inline Vec4d& operator /= (value_type rhs)
165  {
166  _v[0]/=rhs;
167  _v[1]/=rhs;
168  _v[2]/=rhs;
169  _v[3]/=rhs;
170  return *this;
171  }
172 
174  inline Vec4d operator + (const Vec4d& rhs) const
175  {
176  return Vec4d(_v[0]+rhs._v[0], _v[1]+rhs._v[1],
177  _v[2]+rhs._v[2], _v[3]+rhs._v[3]);
178  }
179 
183  inline Vec4d& operator += (const Vec4d& rhs)
184  {
185  _v[0] += rhs._v[0];
186  _v[1] += rhs._v[1];
187  _v[2] += rhs._v[2];
188  _v[3] += rhs._v[3];
189  return *this;
190  }
191 
193  inline Vec4d operator - (const Vec4d& rhs) const
194  {
195  return Vec4d(_v[0]-rhs._v[0], _v[1]-rhs._v[1],
196  _v[2]-rhs._v[2], _v[3]-rhs._v[3] );
197  }
198 
200  inline Vec4d& operator -= (const Vec4d& rhs)
201  {
202  _v[0]-=rhs._v[0];
203  _v[1]-=rhs._v[1];
204  _v[2]-=rhs._v[2];
205  _v[3]-=rhs._v[3];
206  return *this;
207  }
208 
210  inline const Vec4d operator - () const
211  {
212  return Vec4d (-_v[0], -_v[1], -_v[2], -_v[3]);
213  }
214 
216  inline value_type length() const
217  {
218  return sqrt( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3]);
219  }
220 
222  inline value_type length2() const
223  {
224  return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3];
225  }
226 
230  inline value_type normalize()
231  {
232  value_type norm = Vec4d::length();
233  if (norm>0.0f)
234  {
235  value_type inv = 1.0/norm;
236  _v[0] *= inv;
237  _v[1] *= inv;
238  _v[2] *= inv;
239  _v[3] *= inv;
240  }
241  return( norm );
242  }
243 
244 }; // end of class Vec4d
245 
246 
247 
249 inline Vec4d::value_type operator * (const Vec3d& lhs,const Vec4d& rhs)
250 {
251  return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+rhs[3];
252 }
253 
255 inline Vec4d::value_type operator * (const Vec3f& lhs,const Vec4d& rhs)
256 {
257  return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+rhs[3];
258 }
259 
261 inline Vec4d::value_type operator * (const Vec3d& lhs,const Vec4f& rhs)
262 {
263  return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+rhs[3];
264 }
265 
266 
268 inline Vec4d::value_type operator * (const Vec4d& lhs,const Vec3d& rhs)
269 {
270  return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+lhs[3];
271 }
272 
274 inline Vec4d::value_type operator * (const Vec4d& lhs,const Vec3f& rhs)
275 {
276  return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+lhs[3];
277 }
278 
280 inline Vec4d::value_type operator * (const Vec4f& lhs,const Vec3d& rhs)
281 {
282  return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+lhs[3];
283 }
284 
286 inline Vec4d componentMultiply(const Vec4d& lhs, const Vec4d& rhs)
287 {
288  return Vec4d(lhs[0]*rhs[0], lhs[1]*rhs[1], lhs[2]*rhs[2], lhs[3]*rhs[3]);
289 }
290 
292 inline Vec4d componentDivide(const Vec4d& lhs, const Vec4d& rhs)
293 {
294  return Vec4d(lhs[0]/rhs[0], lhs[1]/rhs[1], lhs[2]/rhs[2], lhs[3]/rhs[3]);
295 }
296 
297 } // end of namespace osg
298 
299 #endif
value_type & r()
Definition: Vec4d.h:100
value_type & y()
Definition: Vec4d.h:91
Vec4d & operator-=(const Vec4d &rhs)
Definition: Vec4d.h:200
unsigned int asRGBA() const
Definition: Vec4d.h:119
value_type y() const
Definition: Vec4d.h:96
bool operator==(const Vec4d &v) const
Definition: Vec4d.h:64
value_type & a()
Definition: Vec4d.h:103
value_type length() const
Definition: Vec4d.h:216
value_type _v[4]
Definition: Vec4d.h:38
value_type g() const
Definition: Vec4d.h:106
value_type z() const
Definition: Vec4d.h:97
value_type _v[4]
Definition: Vec4f.h:38
unsigned int asABGR() const
Definition: Vec4d.h:111
value_type & operator[](unsigned int i)
Definition: Vec4d.h:87
value_type r() const
Definition: Vec4d.h:105
value_type a() const
Definition: Vec4d.h:108
Vec4d & operator*=(value_type rhs)
Definition: Vec4d.h:148
value_type & x()
Definition: Vec4d.h:90
Vec2d componentDivide(const Vec2d &lhs, const Vec2d &rhs)
Definition: Vec2d.h:187
Vec4d(value_type x, value_type y, value_type z, value_type w)
Definition: Vec4d.h:43
Vec4d(const Vec3d &v3, value_type w)
Definition: Vec4d.h:51
double value_type
Definition: Vec4d.h:33
bool isNaN(float v)
Definition: Math.h:114
Vec4d operator/(value_type rhs) const
Definition: Vec4d.h:158
const Vec4d operator-() const
Definition: Vec4d.h:210
bool isNaN() const
Definition: Vec4d.h:130
value_type operator*(const Vec4d &rhs) const
Definition: Vec4d.h:133
value_type * ptr()
Definition: Vec4d.h:79
Vec4d & operator/=(value_type rhs)
Definition: Vec4d.h:164
value_type b() const
Definition: Vec4d.h:107
value_type length2() const
Definition: Vec4d.h:222
value_type & z()
Definition: Vec4d.h:92
bool operator!=(const Vec4d &v) const
Definition: Vec4d.h:66
void set(value_type x, value_type y, value_type z, value_type w)
Definition: Vec4d.h:82
const value_type * ptr() const
Definition: Vec4d.h:80
value_type & g()
Definition: Vec4d.h:101
T clampTo(T v, T minimum, T maximum)
Definition: Math.h:69
Vec4d operator+(const Vec4d &rhs) const
Definition: Vec4d.h:174
value_type x() const
Definition: Vec4d.h:95
Vec3f operator*(const Vec3f &v, const Matrixd &m)
Definition: Matrixd.h:787
Definition: AlphaFunc.h:19
Vec4d & operator+=(const Vec4d &rhs)
Definition: Vec4d.h:183
value_type & w()
Definition: Vec4d.h:93
value_type & b()
Definition: Vec4d.h:102
value_type w() const
Definition: Vec4d.h:98
Vec4d()
Definition: Vec4d.h:41
value_type normalize()
Definition: Vec4d.h:230
Vec2d componentMultiply(const Vec2d &lhs, const Vec2d &rhs)
Definition: Vec2d.h:181
bool valid() const
Definition: Vec4d.h:128
bool operator<(const Vec4d &v) const
Definition: Vec4d.h:68
Vec4d(const Vec4f &vec)
Definition: Vec4d.h:59