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