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 quad. Uses include representation
23 * of color coordinates.
24 * No support yet added for double * Vec4d - is it necessary?
25 * Need to define a non-member non-friend operator* etc.
26 * Vec4d * double is okay
32 /** Data type of vector components.*/
33 typedef double value_type;
35 /** Number of vector components. */
36 enum { num_components = 4 };
40 /** Constructor that sets all components of the vector to zero */
41 Vec4d() { _v[0]=0.0; _v[1]=0.0; _v[2]=0.0; _v[3]=0.0; }
43 Vec4d(value_type x, value_type y, value_type z, value_type w)
51 Vec4d(const Vec3d& v3,value_type w)
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];}
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]));}
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]; }
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]; }
68 inline bool operator < (const Vec4d& v) const
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]);
79 inline value_type* ptr() { return _v; }
80 inline const value_type* ptr() const { return _v; }
82 inline void set( value_type x, value_type y, value_type z, value_type w)
84 _v[0]=x; _v[1]=y; _v[2]=z; _v[3]=w;
87 inline value_type& operator [] (unsigned int i) { return _v[i]; }
88 inline value_type operator [] (unsigned int i) const { return _v[i]; }
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]; }
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]; }
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]; }
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]; }
111 inline unsigned int asABGR() const
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);
119 inline unsigned int asRGBA() const
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);
127 /** Returns true if all components have values that are not NaN. */
128 inline bool valid() const { return !isNaN(); }
129 /** Returns true if at least one component has value NaN. */
130 inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]) || osg::isNaN(_v[2]) || osg::isNaN(_v[3]); }
133 inline value_type operator * (const Vec4d& rhs) const
135 return _v[0]*rhs._v[0]+
141 /** Multiply by scalar. */
142 inline Vec4d operator * (value_type rhs) const
144 return Vec4d(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs, _v[3]*rhs);
147 /** Unary multiply by scalar. */
148 inline Vec4d& operator *= (value_type rhs)
157 /** Divide by scalar. */
158 inline Vec4d operator / (value_type rhs) const
160 return Vec4d(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs, _v[3]/rhs);
163 /** Unary divide by scalar. */
164 inline Vec4d& operator /= (value_type rhs)
173 /** Binary vector add. */
174 inline Vec4d operator + (const Vec4d& rhs) const
176 return Vec4d(_v[0]+rhs._v[0], _v[1]+rhs._v[1],
177 _v[2]+rhs._v[2], _v[3]+rhs._v[3]);
180 /** Unary vector add. Slightly more efficient because no temporary
181 * intermediate object.
183 inline Vec4d& operator += (const Vec4d& rhs)
192 /** Binary vector subtract. */
193 inline Vec4d operator - (const Vec4d& rhs) const
195 return Vec4d(_v[0]-rhs._v[0], _v[1]-rhs._v[1],
196 _v[2]-rhs._v[2], _v[3]-rhs._v[3] );
199 /** Unary vector subtract. */
200 inline Vec4d& operator -= (const Vec4d& rhs)
209 /** Negation operator. Returns the negative of the Vec4d. */
210 inline const Vec4d operator - () const
212 return Vec4d (-_v[0], -_v[1], -_v[2], -_v[3]);
215 /** Length of the vector = sqrt( vec . vec ) */
216 inline value_type length() const
218 return sqrt( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3]);
221 /** Length squared of the vector = vec . vec */
222 inline value_type length2() const
224 return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3];
227 /** Normalize the vector so that it has length unity.
228 * Returns the previous length of the vector.
230 inline value_type normalize()
232 value_type norm = Vec4d::length();
235 value_type inv = 1.0/norm;
244}; // end of class Vec4d
248/** Compute the dot product of a (Vec3,1.0) and a Vec4d. */
249inline Vec4d::value_type operator * (const Vec3d& lhs,const Vec4d& rhs)
251 return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+rhs[3];
254/** Compute the dot product of a (Vec3,1.0) and a Vec4d. */
255inline Vec4d::value_type operator * (const Vec3f& lhs,const Vec4d& rhs)
257 return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+rhs[3];
260/** Compute the dot product of a (Vec3,1.0) and a Vec4d. */
261inline Vec4d::value_type operator * (const Vec3d& lhs,const Vec4f& rhs)
263 return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+rhs[3];
267/** Compute the dot product of a Vec4d and a (Vec3,1.0). */
268inline Vec4d::value_type operator * (const Vec4d& lhs,const Vec3d& rhs)
270 return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+lhs[3];
273/** Compute the dot product of a Vec4d and a (Vec3,1.0). */
274inline Vec4d::value_type operator * (const Vec4d& lhs,const Vec3f& rhs)
276 return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+lhs[3];
279/** Compute the dot product of a Vec4d and a (Vec3,1.0). */
280inline Vec4d::value_type operator * (const Vec4f& lhs,const Vec3d& rhs)
282 return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+lhs[3];
285/** multiply by vector components. */
286inline Vec4d componentMultiply(const Vec4d& lhs, const Vec4d& rhs)
288 return Vec4d(lhs[0]*rhs[0], lhs[1]*rhs[1], lhs[2]*rhs[2], lhs[3]*rhs[3]);
291/** divide rhs components by rhs vector components. */
292inline Vec4d componentDivide(const Vec4d& lhs, const Vec4d& rhs)
294 return Vec4d(lhs[0]/rhs[0], lhs[1]/rhs[1], lhs[2]/rhs[2], lhs[3]/rhs[3]);
297} // end of namespace osg