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

Revision 8038, 9.0 kB (checked in by robert, 7 years ago)

Introduced CMake build option for compiling double or float versions of osg::BoundingSphere? and osg::BoundingBox?.

Introduced code in BoundgingSphere?, BoundingBox?, ProxyNode? and LOD to utilise the above settings.

Added Matrix::value_type, Plane::value_type, BoundingSphere::value_type and BoundingBox::value_type command line
options that report where the types of floats or doubles.

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