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.
21/** General purpose float quad. Uses include representation
22 * of color coordinates.
23 * No support yet added for float * Vec4f - is it necessary?
24 * Need to define a non-member non-friend operator* etc.
25 * Vec4f * float is okay
31 /** Data type of vector components.*/
32 typedef float value_type;
34 /** Number of vector components. */
35 enum { num_components = 4 };
37 /** Vec member variable. */
40 // Methods are defined here so that they are implicitly inlined
42 /** Constructor that sets all components of the vector to zero */
43 Vec4f() { _v[0]=0.0f; _v[1]=0.0f; _v[2]=0.0f; _v[3]=0.0f;}
45 Vec4f(value_type x, value_type y, value_type z, value_type w)
53 Vec4f(const Vec3f& v3,value_type w)
61 inline bool operator == (const Vec4f& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1] && _v[2]==v._v[2] && _v[3]==v._v[3]; }
63 inline bool operator != (const Vec4f& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1] || _v[2]!=v._v[2] || _v[3]!=v._v[3]; }
65 inline bool operator < (const Vec4f& v) const
67 if (_v[0]<v._v[0]) return true;
68 else if (_v[0]>v._v[0]) return false;
69 else if (_v[1]<v._v[1]) return true;
70 else if (_v[1]>v._v[1]) return false;
71 else if (_v[2]<v._v[2]) return true;
72 else if (_v[2]>v._v[2]) return false;
73 else return (_v[3]<v._v[3]);
76 inline value_type* ptr() { return _v; }
77 inline const value_type* ptr() const { return _v; }
79 inline void set( value_type x, value_type y, value_type z, value_type w)
81 _v[0]=x; _v[1]=y; _v[2]=z; _v[3]=w;
84 inline value_type& operator [] (unsigned int i) { return _v[i]; }
85 inline value_type operator [] (unsigned int i) const { return _v[i]; }
87 inline value_type& x() { return _v[0]; }
88 inline value_type& y() { return _v[1]; }
89 inline value_type& z() { return _v[2]; }
90 inline value_type& w() { return _v[3]; }
92 inline value_type x() const { return _v[0]; }
93 inline value_type y() const { return _v[1]; }
94 inline value_type z() const { return _v[2]; }
95 inline value_type w() const { return _v[3]; }
97 inline value_type& r() { return _v[0]; }
98 inline value_type& g() { return _v[1]; }
99 inline value_type& b() { return _v[2]; }
100 inline value_type& a() { return _v[3]; }
102 inline value_type r() const { return _v[0]; }
103 inline value_type g() const { return _v[1]; }
104 inline value_type b() const { return _v[2]; }
105 inline value_type a() const { return _v[3]; }
107 inline unsigned int asABGR() const
109 return (unsigned int)clampTo((_v[0]*255.0f),0.0f,255.0f)<<24 |
110 (unsigned int)clampTo((_v[1]*255.0f),0.0f,255.0f)<<16 |
111 (unsigned int)clampTo((_v[2]*255.0f),0.0f,255.0f)<<8 |
112 (unsigned int)clampTo((_v[3]*255.0f),0.0f,255.0f);
115 inline unsigned int asRGBA() const
117 return (unsigned int)clampTo((_v[3]*255.0f),0.0f,255.0f)<<24 |
118 (unsigned int)clampTo((_v[2]*255.0f),0.0f,255.0f)<<16 |
119 (unsigned int)clampTo((_v[1]*255.0f),0.0f,255.0f)<<8 |
120 (unsigned int)clampTo((_v[0]*255.0f),0.0f,255.0f);
123 /** Returns true if all components have values that are not NaN. */
124 inline bool valid() const { return !isNaN(); }
125 /** Returns true if at least one component has value NaN. */
126 inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]) || osg::isNaN(_v[2]) || osg::isNaN(_v[3]); }
129 inline value_type operator * (const Vec4f& rhs) const
131 return _v[0]*rhs._v[0]+
137 /** Multiply by scalar. */
138 inline Vec4f operator * (value_type rhs) const
140 return Vec4f(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs, _v[3]*rhs);
143 /** Unary multiply by scalar. */
144 inline Vec4f& operator *= (value_type rhs)
153 /** Divide by scalar. */
154 inline Vec4f operator / (value_type rhs) const
156 return Vec4f(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs, _v[3]/rhs);
159 /** Unary divide by scalar. */
160 inline Vec4f& operator /= (value_type rhs)
169 /** Binary vector add. */
170 inline Vec4f operator + (const Vec4f& rhs) const
172 return Vec4f(_v[0]+rhs._v[0], _v[1]+rhs._v[1],
173 _v[2]+rhs._v[2], _v[3]+rhs._v[3]);
176 /** Unary vector add. Slightly more efficient because no temporary
177 * intermediate object.
179 inline Vec4f& operator += (const Vec4f& rhs)
188 /** Binary vector subtract. */
189 inline Vec4f operator - (const Vec4f& rhs) const
191 return Vec4f(_v[0]-rhs._v[0], _v[1]-rhs._v[1],
192 _v[2]-rhs._v[2], _v[3]-rhs._v[3] );
195 /** Unary vector subtract. */
196 inline Vec4f& operator -= (const Vec4f& rhs)
205 /** Negation operator. Returns the negative of the Vec4f. */
206 inline const Vec4f operator - () const
208 return Vec4f (-_v[0], -_v[1], -_v[2], -_v[3]);
211 /** Length of the vector = sqrt( vec . vec ) */
212 inline value_type length() const
214 return sqrtf( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3]);
217 /** Length squared of the vector = vec . vec */
218 inline value_type length2() const
220 return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3];
223 /** Normalize the vector so that it has length unity.
224 * Returns the previous length of the vector.
226 inline value_type normalize()
228 value_type norm = Vec4f::length();
231 value_type inv = 1.0f/norm;
240}; // end of class Vec4f
242/** Compute the dot product of a (Vec3,1.0) and a Vec4f. */
243inline Vec4f::value_type operator * (const Vec3f& lhs,const Vec4f& rhs)
245 return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+rhs[3];
248/** Compute the dot product of a Vec4f and a (Vec3,1.0). */
249inline Vec4f::value_type operator * (const Vec4f& lhs,const Vec3f& rhs)
251 return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+lhs[3];
254/** multiply by vector components. */
255inline Vec4f componentMultiply(const Vec4f& lhs, const Vec4f& rhs)
257 return Vec4f(lhs[0]*rhs[0], lhs[1]*rhs[1], lhs[2]*rhs[2], lhs[3]*rhs[3]);
260/** divide rhs components by rhs vector components. */
261inline Vec4f componentDivide(const Vec4f& lhs, const Vec4f& rhs)
263 return Vec4f(lhs[0]/rhs[0], lhs[1]/rhs[1], lhs[2]/rhs[2], lhs[3]/rhs[3]);
266} // end of namespace osg