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