OpenSceneGraph 3.6.5
Vec4d
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
20namespace osg {
21
28class Vec4d
29{
30 public:
31
33 typedef double value_type;
34
36 enum { num_components = 4 };
37
39
41 Vec4d() { _v[0]=0.0; _v[1]=0.0; _v[2]=0.0; _v[3]=0.0; }
42
44 {
45 _v[0]=x;
46 _v[1]=y;
47 _v[2]=z;
48 _v[3]=w;
49 }
50
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
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
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
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
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
249inline 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
255inline 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
261inline 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
268inline 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
274inline 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
280inline 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
286inline 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
292inline 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
The core osg library provides the basic scene graph classes such as Nodes, State and Drawables,...
Definition AlphaFunc:19
Vec3f operator*(const Vec3f &v, const Matrixd &m)
Definition Matrixd:793
Vec2d componentDivide(const Vec2d &lhs, const Vec2d &rhs)
divide rhs components by rhs vector components.
Definition Vec2d:187
bool isNaN(float v)
Definition Math:133
T clampTo(T v, T minimum, T maximum)
Definition Math:88
Vec2d componentMultiply(const Vec2d &lhs, const Vec2d &rhs)
multiply by vector components.
Definition Vec2d:181
General purpose double triple for use as vertices, vectors and normals.
Definition Vec3d:30
General purpose float triple for use as vertices, vectors and normals.
Definition Vec3f:29
General purpose double quad.
Definition Vec4d:29
bool operator==(const Vec4d &v) const
Definition Vec4d:64
Vec4d operator/(value_type rhs) const
Divide by scalar.
Definition Vec4d:158
value_type & x()
Definition Vec4d:90
bool valid() const
Returns true if all components have values that are not NaN.
Definition Vec4d:128
bool isNaN() const
Returns true if at least one component has value NaN.
Definition Vec4d:130
value_type & b()
Definition Vec4d:102
Vec4d & operator/=(value_type rhs)
Unary divide by scalar.
Definition Vec4d:164
value_type normalize()
Normalize the vector so that it has length unity.
Definition Vec4d:230
Vec4d & operator+=(const Vec4d &rhs)
Unary vector add.
Definition Vec4d:183
value_type & y()
Definition Vec4d:91
value_type _v[4]
Definition Vec4d:38
unsigned int asRGBA() const
Definition Vec4d:119
value_type g() const
Definition Vec4d:106
Vec4d(value_type x, value_type y, value_type z, value_type w)
Definition Vec4d:43
Vec4d(const Vec3d &v3, value_type w)
Definition Vec4d:51
value_type & z()
Definition Vec4d:92
const Vec4d operator-() const
Negation operator.
Definition Vec4d:210
const value_type * ptr() const
Definition Vec4d:80
unsigned int asABGR() const
Definition Vec4d:111
value_type r() const
Definition Vec4d:105
value_type a() const
Definition Vec4d:108
value_type & r()
Definition Vec4d:100
double value_type
Data type of vector components.
Definition Vec4d:33
value_type length() const
Length of the vector = sqrt( vec .
Definition Vec4d:216
value_type & operator[](unsigned int i)
Definition Vec4d:87
Vec4d()
Constructor that sets all components of the vector to zero.
Definition Vec4d:41
bool operator<(const Vec4d &v) const
Definition Vec4d:68
value_type length2() const
Length squared of the vector = vec .
Definition Vec4d:222
bool operator!=(const Vec4d &v) const
Definition Vec4d:66
value_type operator*(const Vec4d &rhs) const
Dot product.
Definition Vec4d:133
Vec4d(const Vec4f &vec)
Definition Vec4d:59
value_type & g()
Definition Vec4d:101
Vec4d & operator-=(const Vec4d &rhs)
Unary vector subtract.
Definition Vec4d:200
value_type b() const
Definition Vec4d:107
value_type & a()
Definition Vec4d:103
Vec4d & operator*=(value_type rhs)
Unary multiply by scalar.
Definition Vec4d:148
void set(value_type x, value_type y, value_type z, value_type w)
Definition Vec4d:82
value_type x() const
Definition Vec4d:95
value_type * ptr()
Definition Vec4d:79
@ num_components
Definition Vec4d:36
value_type z() const
Definition Vec4d:97
Vec4d operator+(const Vec4d &rhs) const
Binary vector add.
Definition Vec4d:174
value_type w() const
Definition Vec4d:98
value_type y() const
Definition Vec4d:96
value_type & w()
Definition Vec4d:93
General purpose float quad.
Definition Vec4f:28
value_type _v[4]
Vec member variable.
Definition Vec4f:38

osg logo
Generated at Sun Jul 20 2025 00:00:00 for the OpenSceneGraph by doxygen 1.14.0.