root/OpenSceneGraph/trunk/include/osg/Vec4d @ 13041

Revision 13041, 9.5 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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
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
27*/
28class Vec4d
29{
30    public:
31
32        /** Data type of vector components.*/
33        typedef double value_type;
34
35        /** Number of vector components. */
36        enum { num_components = 4 };
37
38        value_type _v[4];
39
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; }
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
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]); }
131
132        /** Dot product. */
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
141        /** Multiply by scalar. */
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
147        /** Unary multiply by scalar. */
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
157        /** Divide by scalar. */
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
163        /** Unary divide by scalar. */
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
173        /** Binary vector add. */
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
180        /** Unary vector add. Slightly more efficient because no temporary
181          * intermediate object.
182        */
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
192        /** Binary vector subtract. */
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
199        /** Unary vector subtract. */
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
209        /** Negation operator. Returns the negative of the Vec4d. */
210        inline const Vec4d operator - () const
211        {
212            return Vec4d (-_v[0], -_v[1], -_v[2], -_v[3]);
213        }
214
215        /** Length of the vector = sqrt( vec . vec ) */
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
221        /** Length squared of the vector = vec . vec */
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
227        /** Normalize the vector so that it has length unity.
228          * Returns the previous length of the vector.
229        */
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
248/** Compute the dot product of a (Vec3,1.0) and a Vec4d. */
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
254/** Compute the dot product of a (Vec3,1.0) and a Vec4d. */
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
260/** Compute the dot product of a (Vec3,1.0) and a Vec4d. */
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
267/** Compute the dot product of a Vec4d and a (Vec3,1.0). */
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
273/** Compute the dot product of a Vec4d and a (Vec3,1.0). */
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
279/** Compute the dot product of a Vec4d and a (Vec3,1.0). */
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
285/** multiply by vector components. */
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
291/** divide rhs components by rhs vector components. */
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
Note: See TracBrowser for help on using the browser.