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_VEC4F |
---|
15 | #define OSG_VEC4F 1 |
---|
16 | |
---|
17 | #include <osg/Vec3f> |
---|
18 | |
---|
19 | namespace osg { |
---|
20 | |
---|
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 |
---|
26 | */ |
---|
27 | class Vec4f |
---|
28 | { |
---|
29 | public: |
---|
30 | |
---|
31 | /** Type of Vec class.*/ |
---|
32 | typedef float value_type; |
---|
33 | |
---|
34 | /** Number of vector components. */ |
---|
35 | enum { num_components = 4 }; |
---|
36 | |
---|
37 | /** Vec member variable. */ |
---|
38 | value_type _v[4]; |
---|
39 | |
---|
40 | // Methods are defined here so that they are implicitly inlined |
---|
41 | |
---|
42 | Vec4f() { _v[0]=0.0f; _v[1]=0.0f; _v[2]=0.0f; _v[3]=0.0f;} |
---|
43 | |
---|
44 | Vec4f(value_type x, value_type y, value_type z, value_type w) |
---|
45 | { |
---|
46 | _v[0]=x; |
---|
47 | _v[1]=y; |
---|
48 | _v[2]=z; |
---|
49 | _v[3]=w; |
---|
50 | } |
---|
51 | |
---|
52 | Vec4f(const Vec3f& v3,value_type w) |
---|
53 | { |
---|
54 | _v[0]=v3[0]; |
---|
55 | _v[1]=v3[1]; |
---|
56 | _v[2]=v3[2]; |
---|
57 | _v[3]=w; |
---|
58 | } |
---|
59 | |
---|
60 | 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]; } |
---|
61 | |
---|
62 | 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 | |
---|
64 | inline bool operator < (const Vec4f& v) const |
---|
65 | { |
---|
66 | if (_v[0]<v._v[0]) return true; |
---|
67 | else if (_v[0]>v._v[0]) return false; |
---|
68 | else if (_v[1]<v._v[1]) return true; |
---|
69 | else if (_v[1]>v._v[1]) return false; |
---|
70 | else if (_v[2]<v._v[2]) return true; |
---|
71 | else if (_v[2]>v._v[2]) return false; |
---|
72 | else return (_v[3]<v._v[3]); |
---|
73 | } |
---|
74 | |
---|
75 | inline value_type* ptr() { return _v; } |
---|
76 | inline const value_type* ptr() const { return _v; } |
---|
77 | |
---|
78 | inline void set( value_type x, value_type y, value_type z, value_type w) |
---|
79 | { |
---|
80 | _v[0]=x; _v[1]=y; _v[2]=z; _v[3]=w; |
---|
81 | } |
---|
82 | |
---|
83 | inline value_type& operator [] (unsigned int i) { return _v[i]; } |
---|
84 | inline value_type operator [] (unsigned int i) const { return _v[i]; } |
---|
85 | |
---|
86 | inline value_type& x() { return _v[0]; } |
---|
87 | inline value_type& y() { return _v[1]; } |
---|
88 | inline value_type& z() { return _v[2]; } |
---|
89 | inline value_type& w() { return _v[3]; } |
---|
90 | |
---|
91 | inline value_type x() const { return _v[0]; } |
---|
92 | inline value_type y() const { return _v[1]; } |
---|
93 | inline value_type z() const { return _v[2]; } |
---|
94 | inline value_type w() const { return _v[3]; } |
---|
95 | |
---|
96 | inline value_type& r() { return _v[0]; } |
---|
97 | inline value_type& g() { return _v[1]; } |
---|
98 | inline value_type& b() { return _v[2]; } |
---|
99 | inline value_type& a() { return _v[3]; } |
---|
100 | |
---|
101 | inline value_type r() const { return _v[0]; } |
---|
102 | inline value_type g() const { return _v[1]; } |
---|
103 | inline value_type b() const { return _v[2]; } |
---|
104 | inline value_type a() const { return _v[3]; } |
---|
105 | |
---|
106 | inline unsigned int asABGR() const |
---|
107 | { |
---|
108 | return (unsigned int)clampTo((_v[0]*255.0f),0.0f,255.0f)<<24 | |
---|
109 | (unsigned int)clampTo((_v[1]*255.0f),0.0f,255.0f)<<16 | |
---|
110 | (unsigned int)clampTo((_v[2]*255.0f),0.0f,255.0f)<<8 | |
---|
111 | (unsigned int)clampTo((_v[3]*255.0f),0.0f,255.0f); |
---|
112 | } |
---|
113 | |
---|
114 | inline unsigned int asRGBA() const |
---|
115 | { |
---|
116 | return (unsigned int)clampTo((_v[3]*255.0f),0.0f,255.0f)<<24 | |
---|
117 | (unsigned int)clampTo((_v[2]*255.0f),0.0f,255.0f)<<16 | |
---|
118 | (unsigned int)clampTo((_v[1]*255.0f),0.0f,255.0f)<<8 | |
---|
119 | (unsigned int)clampTo((_v[0]*255.0f),0.0f,255.0f); |
---|
120 | } |
---|
121 | |
---|
122 | inline bool valid() const { return !isNaN(); } |
---|
123 | inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]) || osg::isNaN(_v[2]) || osg::isNaN(_v[3]); } |
---|
124 | |
---|
125 | /** Dot product. */ |
---|
126 | inline value_type operator * (const Vec4f& rhs) const |
---|
127 | { |
---|
128 | return _v[0]*rhs._v[0]+ |
---|
129 | _v[1]*rhs._v[1]+ |
---|
130 | _v[2]*rhs._v[2]+ |
---|
131 | _v[3]*rhs._v[3] ; |
---|
132 | } |
---|
133 | |
---|
134 | /** Multiply by scalar. */ |
---|
135 | inline Vec4f operator * (value_type rhs) const |
---|
136 | { |
---|
137 | return Vec4f(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs, _v[3]*rhs); |
---|
138 | } |
---|
139 | |
---|
140 | /** Unary multiply by scalar. */ |
---|
141 | inline Vec4f& operator *= (value_type rhs) |
---|
142 | { |
---|
143 | _v[0]*=rhs; |
---|
144 | _v[1]*=rhs; |
---|
145 | _v[2]*=rhs; |
---|
146 | _v[3]*=rhs; |
---|
147 | return *this; |
---|
148 | } |
---|
149 | |
---|
150 | /** Divide by scalar. */ |
---|
151 | inline Vec4f operator / (value_type rhs) const |
---|
152 | { |
---|
153 | return Vec4f(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs, _v[3]/rhs); |
---|
154 | } |
---|
155 | |
---|
156 | /** Unary divide by scalar. */ |
---|
157 | inline Vec4f& operator /= (value_type rhs) |
---|
158 | { |
---|
159 | _v[0]/=rhs; |
---|
160 | _v[1]/=rhs; |
---|
161 | _v[2]/=rhs; |
---|
162 | _v[3]/=rhs; |
---|
163 | return *this; |
---|
164 | } |
---|
165 | |
---|
166 | /** Binary vector add. */ |
---|
167 | inline Vec4f operator + (const Vec4f& rhs) const |
---|
168 | { |
---|
169 | return Vec4f(_v[0]+rhs._v[0], _v[1]+rhs._v[1], |
---|
170 | _v[2]+rhs._v[2], _v[3]+rhs._v[3]); |
---|
171 | } |
---|
172 | |
---|
173 | /** Unary vector add. Slightly more efficient because no temporary |
---|
174 | * intermediate object. |
---|
175 | */ |
---|
176 | inline Vec4f& operator += (const Vec4f& rhs) |
---|
177 | { |
---|
178 | _v[0] += rhs._v[0]; |
---|
179 | _v[1] += rhs._v[1]; |
---|
180 | _v[2] += rhs._v[2]; |
---|
181 | _v[3] += rhs._v[3]; |
---|
182 | return *this; |
---|
183 | } |
---|
184 | |
---|
185 | /** Binary vector subtract. */ |
---|
186 | inline Vec4f operator - (const Vec4f& rhs) const |
---|
187 | { |
---|
188 | return Vec4f(_v[0]-rhs._v[0], _v[1]-rhs._v[1], |
---|
189 | _v[2]-rhs._v[2], _v[3]-rhs._v[3] ); |
---|
190 | } |
---|
191 | |
---|
192 | /** Unary vector subtract. */ |
---|
193 | inline Vec4f& operator -= (const Vec4f& rhs) |
---|
194 | { |
---|
195 | _v[0]-=rhs._v[0]; |
---|
196 | _v[1]-=rhs._v[1]; |
---|
197 | _v[2]-=rhs._v[2]; |
---|
198 | _v[3]-=rhs._v[3]; |
---|
199 | return *this; |
---|
200 | } |
---|
201 | |
---|
202 | /** Negation operator. Returns the negative of the Vec4f. */ |
---|
203 | inline const Vec4f operator - () const |
---|
204 | { |
---|
205 | return Vec4f (-_v[0], -_v[1], -_v[2], -_v[3]); |
---|
206 | } |
---|
207 | |
---|
208 | /** Length of the vector = sqrt( vec . vec ) */ |
---|
209 | inline value_type length() const |
---|
210 | { |
---|
211 | return sqrtf( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3]); |
---|
212 | } |
---|
213 | |
---|
214 | /** Length squared of the vector = vec . vec */ |
---|
215 | inline value_type length2() const |
---|
216 | { |
---|
217 | return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3]; |
---|
218 | } |
---|
219 | |
---|
220 | /** Normalize the vector so that it has length unity. |
---|
221 | * Returns the previous length of the vector. |
---|
222 | */ |
---|
223 | inline value_type normalize() |
---|
224 | { |
---|
225 | value_type norm = Vec4f::length(); |
---|
226 | if (norm>0.0f) |
---|
227 | { |
---|
228 | value_type inv = 1.0f/norm; |
---|
229 | _v[0] *= inv; |
---|
230 | _v[1] *= inv; |
---|
231 | _v[2] *= inv; |
---|
232 | _v[3] *= inv; |
---|
233 | } |
---|
234 | return( norm ); |
---|
235 | } |
---|
236 | |
---|
237 | }; // end of class Vec4f |
---|
238 | |
---|
239 | |
---|
240 | |
---|
241 | /** Compute the dot product of a (Vec3,1.0) and a Vec4f. */ |
---|
242 | inline Vec4f::value_type operator * (const Vec3f& lhs,const Vec4f& rhs) |
---|
243 | { |
---|
244 | return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+rhs[3]; |
---|
245 | } |
---|
246 | |
---|
247 | /** Compute the dot product of a Vec4f and a (Vec3,1.0). */ |
---|
248 | inline Vec4f::value_type operator * (const Vec4f& lhs,const Vec3f& rhs) |
---|
249 | { |
---|
250 | return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+lhs[3]; |
---|
251 | } |
---|
252 | |
---|
253 | } // end of namespace osg |
---|
254 | |
---|
255 | #endif |
---|
256 | |
---|