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

Revision 13041, 6.8 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_VEC3F
15#define OSG_VEC3F 1
16
17#include <osg/Vec2f>
18#include <osg/Math>
19
20namespace osg {
21
22/** General purpose float triple for use as vertices, vectors and normals.
23  * Provides general math operations from addition through to cross products.
24  * No support yet added for float * Vec3f - is it necessary?
25  * Need to define a non-member non-friend operator*  etc.
26  * Vec3f * float is okay
27*/
28class Vec3f
29{
30    public:
31
32        /** Data type of vector components.*/
33        typedef float value_type;
34
35        /** Number of vector components. */
36        enum { num_components = 3 };
37
38        value_type _v[3];
39
40        /** Constructor that sets all components of the vector to zero */
41        Vec3f() { _v[0]=0.0f; _v[1]=0.0f; _v[2]=0.0f;}
42        Vec3f(value_type x,value_type y,value_type z) { _v[0]=x; _v[1]=y; _v[2]=z; }
43        Vec3f(const Vec2f& v2,value_type zz)
44        {
45            _v[0] = v2[0];
46            _v[1] = v2[1];
47            _v[2] = zz;
48        }
49
50
51        inline bool operator == (const Vec3f& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1] && _v[2]==v._v[2]; }
52
53        inline bool operator != (const Vec3f& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1] || _v[2]!=v._v[2]; }
54
55        inline bool operator <  (const Vec3f& v) const
56        {
57            if (_v[0]<v._v[0]) return true;
58            else if (_v[0]>v._v[0]) return false;
59            else if (_v[1]<v._v[1]) return true;
60            else if (_v[1]>v._v[1]) return false;
61            else return (_v[2]<v._v[2]);
62        }
63
64        inline value_type* ptr() { return _v; }
65        inline const value_type* ptr() const { return _v; }
66
67        inline void set( value_type x, value_type y, value_type z)
68        {
69            _v[0]=x; _v[1]=y; _v[2]=z;
70        }
71
72        inline void set( const Vec3f& rhs)
73        {
74            _v[0]=rhs._v[0]; _v[1]=rhs._v[1]; _v[2]=rhs._v[2];
75        }
76
77        inline value_type& operator [] (int i) { return _v[i]; }
78        inline value_type operator [] (int i) const { return _v[i]; }
79
80        inline value_type& x() { return _v[0]; }
81        inline value_type& y() { return _v[1]; }
82        inline value_type& z() { return _v[2]; }
83
84        inline value_type x() const { return _v[0]; }
85        inline value_type y() const { return _v[1]; }
86        inline value_type z() const { return _v[2]; }
87
88        /** Returns true if all components have values that are not NaN. */
89        inline bool valid() const { return !isNaN(); }
90        /** Returns true if at least one component has value NaN. */
91        inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]) || osg::isNaN(_v[2]); }
92
93        /** Dot product. */
94        inline value_type operator * (const Vec3f& rhs) const
95        {
96            return _v[0]*rhs._v[0]+_v[1]*rhs._v[1]+_v[2]*rhs._v[2];
97        }
98
99        /** Cross product. */
100        inline const Vec3f operator ^ (const Vec3f& rhs) const
101        {
102            return Vec3f(_v[1]*rhs._v[2]-_v[2]*rhs._v[1],
103                         _v[2]*rhs._v[0]-_v[0]*rhs._v[2] ,
104                         _v[0]*rhs._v[1]-_v[1]*rhs._v[0]);
105        }
106
107        /** Multiply by scalar. */
108        inline const Vec3f operator * (value_type rhs) const
109        {
110            return Vec3f(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs);
111        }
112
113        /** Unary multiply by scalar. */
114        inline Vec3f& operator *= (value_type rhs)
115        {
116            _v[0]*=rhs;
117            _v[1]*=rhs;
118            _v[2]*=rhs;
119            return *this;
120        }
121
122        /** Divide by scalar. */
123        inline const Vec3f operator / (value_type rhs) const
124        {
125            return Vec3f(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs);
126        }
127
128        /** Unary divide by scalar. */
129        inline Vec3f& operator /= (value_type rhs)
130        {
131            _v[0]/=rhs;
132            _v[1]/=rhs;
133            _v[2]/=rhs;
134            return *this;
135        }
136
137        /** Binary vector add. */
138        inline const Vec3f operator + (const Vec3f& rhs) const
139        {
140            return Vec3f(_v[0]+rhs._v[0], _v[1]+rhs._v[1], _v[2]+rhs._v[2]);
141        }
142
143        /** Unary vector add. Slightly more efficient because no temporary
144          * intermediate object.
145        */
146        inline Vec3f& operator += (const Vec3f& rhs)
147        {
148            _v[0] += rhs._v[0];
149            _v[1] += rhs._v[1];
150            _v[2] += rhs._v[2];
151            return *this;
152        }
153
154        /** Binary vector subtract. */
155        inline const Vec3f operator - (const Vec3f& rhs) const
156        {
157            return Vec3f(_v[0]-rhs._v[0], _v[1]-rhs._v[1], _v[2]-rhs._v[2]);
158        }
159
160        /** Unary vector subtract. */
161        inline Vec3f& operator -= (const Vec3f& rhs)
162        {
163            _v[0]-=rhs._v[0];
164            _v[1]-=rhs._v[1];
165            _v[2]-=rhs._v[2];
166            return *this;
167        }
168
169        /** Negation operator. Returns the negative of the Vec3f. */
170        inline const Vec3f operator - () const
171        {
172            return Vec3f (-_v[0], -_v[1], -_v[2]);
173        }
174
175        /** Length of the vector = sqrt( vec . vec ) */
176        inline value_type length() const
177        {
178            return sqrtf( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] );
179        }
180
181        /** Length squared of the vector = vec . vec */
182        inline value_type length2() const
183        {
184            return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2];
185        }
186
187        /** Normalize the vector so that it has length unity.
188          * Returns the previous length of the vector.
189        */
190        inline value_type normalize()
191        {
192            value_type norm = Vec3f::length();
193            if (norm>0.0)
194            {
195                value_type inv = 1.0f/norm;
196                _v[0] *= inv;
197                _v[1] *= inv;
198                _v[2] *= inv;
199            }
200            return( norm );
201        }
202
203};    // end of class Vec3f
204
205/** multiply by vector components. */
206inline Vec3f componentMultiply(const Vec3f& lhs, const Vec3f& rhs)
207{
208    return Vec3f(lhs[0]*rhs[0], lhs[1]*rhs[1], lhs[2]*rhs[2]);
209}
210
211/** divide rhs components by rhs vector components. */
212inline Vec3f componentDivide(const Vec3f& lhs, const Vec3f& rhs)
213{
214    return Vec3f(lhs[0]/rhs[0], lhs[1]/rhs[1], lhs[2]/rhs[2]);
215}
216
217const Vec3f X_AXIS(1.0,0.0,0.0);
218const Vec3f Y_AXIS(0.0,1.0,0.0);
219const Vec3f Z_AXIS(0.0,0.0,1.0);
220
221}    // end of namespace osg
222
223#endif
224
Note: See TracBrowser for help on using the browser.