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