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

Revision 13041, 8.6 kB (checked in by robert, 3 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_VEC4F
15#define OSG_VEC4F 1
16
17#include <osg/Vec3f>
18
19namespace 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*/
27class Vec4f
28{
29    public:
30
31        /** Data type of vector components.*/
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        /** 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;}
44
45        Vec4f(value_type x, value_type y, value_type z, value_type w)
46        {
47            _v[0]=x;
48            _v[1]=y;
49            _v[2]=z;
50            _v[3]=w;
51        }
52
53        Vec4f(const Vec3f& v3,value_type w)
54        {
55            _v[0]=v3[0];
56            _v[1]=v3[1];
57            _v[2]=v3[2];
58            _v[3]=w;
59        }
60
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]; }
62
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]; }
64
65        inline bool operator <  (const Vec4f& v) const
66        {
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]);
74        }
75
76        inline value_type* ptr() { return _v; }
77        inline const value_type* ptr() const { return _v; }
78
79        inline void set( value_type x, value_type y, value_type z, value_type w)
80        {
81            _v[0]=x; _v[1]=y; _v[2]=z; _v[3]=w;
82        }
83
84        inline value_type& operator [] (unsigned int i) { return _v[i]; }
85        inline value_type  operator [] (unsigned int i) const { return _v[i]; }
86
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]; }
91
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]; }
96
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]; }
101
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]; }
106
107        inline unsigned int asABGR() const
108        {
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);
113        }
114
115        inline unsigned int asRGBA() const
116        {
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);
121        }
122
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]); }
127
128        /** Dot product. */
129        inline value_type operator * (const Vec4f& rhs) const
130        {
131            return _v[0]*rhs._v[0]+
132                   _v[1]*rhs._v[1]+
133                   _v[2]*rhs._v[2]+
134                   _v[3]*rhs._v[3] ;
135        }
136
137        /** Multiply by scalar. */
138        inline Vec4f operator * (value_type rhs) const
139        {
140            return Vec4f(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs, _v[3]*rhs);
141        }
142
143        /** Unary multiply by scalar. */
144        inline Vec4f& operator *= (value_type rhs)
145        {
146            _v[0]*=rhs;
147            _v[1]*=rhs;
148            _v[2]*=rhs;
149            _v[3]*=rhs;
150            return *this;
151        }
152
153        /** Divide by scalar. */
154        inline Vec4f operator / (value_type rhs) const
155        {
156            return Vec4f(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs, _v[3]/rhs);
157        }
158
159        /** Unary divide by scalar. */
160        inline Vec4f& operator /= (value_type rhs)
161        {
162            _v[0]/=rhs;
163            _v[1]/=rhs;
164            _v[2]/=rhs;
165            _v[3]/=rhs;
166            return *this;
167        }
168
169        /** Binary vector add. */
170        inline Vec4f operator + (const Vec4f& rhs) const
171        {
172            return Vec4f(_v[0]+rhs._v[0], _v[1]+rhs._v[1],
173                         _v[2]+rhs._v[2], _v[3]+rhs._v[3]);
174        }
175
176        /** Unary vector add. Slightly more efficient because no temporary
177          * intermediate object.
178        */
179        inline Vec4f& operator += (const Vec4f& rhs)
180        {
181            _v[0] += rhs._v[0];
182            _v[1] += rhs._v[1];
183            _v[2] += rhs._v[2];
184            _v[3] += rhs._v[3];
185            return *this;
186        }
187
188        /** Binary vector subtract. */
189        inline Vec4f operator - (const Vec4f& rhs) const
190        {
191            return Vec4f(_v[0]-rhs._v[0], _v[1]-rhs._v[1],
192                         _v[2]-rhs._v[2], _v[3]-rhs._v[3] );
193        }
194
195        /** Unary vector subtract. */
196        inline Vec4f& operator -= (const Vec4f& rhs)
197        {
198            _v[0]-=rhs._v[0];
199            _v[1]-=rhs._v[1];
200            _v[2]-=rhs._v[2];
201            _v[3]-=rhs._v[3];
202            return *this;
203        }
204
205        /** Negation operator. Returns the negative of the Vec4f. */
206        inline const Vec4f operator - () const
207        {
208            return Vec4f (-_v[0], -_v[1], -_v[2], -_v[3]);
209        }
210
211        /** Length of the vector = sqrt( vec . vec ) */
212        inline value_type length() const
213        {
214            return sqrtf( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3]);
215        }
216
217        /** Length squared of the vector = vec . vec */
218        inline value_type length2() const
219        {
220            return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3];
221        }
222
223        /** Normalize the vector so that it has length unity.
224          * Returns the previous length of the vector.
225        */
226        inline value_type normalize()
227        {
228            value_type norm = Vec4f::length();
229            if (norm>0.0f)
230            {
231                value_type inv = 1.0f/norm;
232                _v[0] *= inv;
233                _v[1] *= inv;
234                _v[2] *= inv;
235                _v[3] *= inv;
236            }
237            return( norm );
238        }
239
240};    // end of class Vec4f
241
242/** Compute the dot product of a (Vec3,1.0) and a Vec4f. */
243inline Vec4f::value_type operator * (const Vec3f& lhs,const Vec4f& rhs)
244{
245    return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+rhs[3];
246}
247
248/** Compute the dot product of a Vec4f and a (Vec3,1.0). */
249inline Vec4f::value_type operator * (const Vec4f& lhs,const Vec3f& rhs)
250{
251    return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+lhs[3];
252}
253
254/** multiply by vector components. */
255inline Vec4f componentMultiply(const Vec4f& lhs, const Vec4f& rhs)
256{
257    return Vec4f(lhs[0]*rhs[0], lhs[1]*rhs[1], lhs[2]*rhs[2], lhs[3]*rhs[3]);
258}
259
260/** divide rhs components by rhs vector components. */
261inline Vec4f componentDivide(const Vec4f& lhs, const Vec4f& rhs)
262{
263    return Vec4f(lhs[0]/rhs[0], lhs[1]/rhs[1], lhs[2]/rhs[2], lhs[3]/rhs[3]);
264}
265
266}    // end of namespace osg
267
268#endif
269
Note: See TracBrowser for help on using the browser.