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

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