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 | /** Divide by scalar. */ |
---|
125 | inline const Vec3d operator / (value_type rhs) const |
---|
126 | { |
---|
127 | return Vec3d(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs); |
---|
128 | } |
---|
129 | |
---|
130 | /** Unary divide by scalar. */ |
---|
131 | inline Vec3d& operator /= (value_type rhs) |
---|
132 | { |
---|
133 | _v[0]/=rhs; |
---|
134 | _v[1]/=rhs; |
---|
135 | _v[2]/=rhs; |
---|
136 | return *this; |
---|
137 | } |
---|
138 | |
---|
139 | /** Binary vector add. */ |
---|
140 | inline const Vec3d operator + (const Vec3d& rhs) const |
---|
141 | { |
---|
142 | return Vec3d(_v[0]+rhs._v[0], _v[1]+rhs._v[1], _v[2]+rhs._v[2]); |
---|
143 | } |
---|
144 | |
---|
145 | /** Unary vector add. Slightly more efficient because no temporary |
---|
146 | * intermediate object. |
---|
147 | */ |
---|
148 | inline Vec3d& operator += (const Vec3d& rhs) |
---|
149 | { |
---|
150 | _v[0] += rhs._v[0]; |
---|
151 | _v[1] += rhs._v[1]; |
---|
152 | _v[2] += rhs._v[2]; |
---|
153 | return *this; |
---|
154 | } |
---|
155 | |
---|
156 | /** Binary vector subtract. */ |
---|
157 | inline const Vec3d operator - (const Vec3d& rhs) const |
---|
158 | { |
---|
159 | return Vec3d(_v[0]-rhs._v[0], _v[1]-rhs._v[1], _v[2]-rhs._v[2]); |
---|
160 | } |
---|
161 | |
---|
162 | /** Unary vector subtract. */ |
---|
163 | inline Vec3d& operator -= (const Vec3d& rhs) |
---|
164 | { |
---|
165 | _v[0]-=rhs._v[0]; |
---|
166 | _v[1]-=rhs._v[1]; |
---|
167 | _v[2]-=rhs._v[2]; |
---|
168 | return *this; |
---|
169 | } |
---|
170 | |
---|
171 | /** Negation operator. Returns the negative of the Vec3d. */ |
---|
172 | inline const Vec3d operator - () const |
---|
173 | { |
---|
174 | return Vec3d (-_v[0], -_v[1], -_v[2]); |
---|
175 | } |
---|
176 | |
---|
177 | /** Length of the vector = sqrt( vec . vec ) */ |
---|
178 | inline value_type length() const |
---|
179 | { |
---|
180 | return sqrt( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] ); |
---|
181 | } |
---|
182 | |
---|
183 | /** Length squared of the vector = vec . vec */ |
---|
184 | inline value_type length2() const |
---|
185 | { |
---|
186 | return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2]; |
---|
187 | } |
---|
188 | |
---|
189 | /** Normalize the vector so that it has length unity. |
---|
190 | * Returns the previous length of the vector. |
---|
191 | */ |
---|
192 | inline value_type normalize() |
---|
193 | { |
---|
194 | value_type norm = Vec3d::length(); |
---|
195 | if (norm>0.0) |
---|
196 | { |
---|
197 | value_type inv = 1.0/norm; |
---|
198 | _v[0] *= inv; |
---|
199 | _v[1] *= inv; |
---|
200 | _v[2] *= inv; |
---|
201 | } |
---|
202 | return( norm ); |
---|
203 | } |
---|
204 | |
---|
205 | }; // end of class Vec3d |
---|
206 | |
---|
207 | } // end of namespace osg |
---|
208 | |
---|
209 | #endif |
---|