OpenSceneGraph 3.6.5
Vec3f
Go to the documentation of this file.
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
20namespace osg {
21
28class Vec3f
29{
30 public:
31
33 typedef float value_type;
34
36 enum { num_components = 3 };
37
39
41 Vec3f() { _v[0]=0.0f; _v[1]=0.0f; _v[2]=0.0f;}
43 Vec3f(const Vec2f& v2,value_type zz)
44 {
45 _v[0] = v2[0];
46 _v[1] = v2[1];
47 _v[2] = zz;
48 }
49
50
51 inline bool operator == (const Vec3f& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1] && _v[2]==v._v[2]; }
52
53 inline bool operator != (const Vec3f& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1] || _v[2]!=v._v[2]; }
54
55 inline bool operator < (const Vec3f& v) const
56 {
57 if (_v[0]<v._v[0]) return true;
58 else if (_v[0]>v._v[0]) return false;
59 else if (_v[1]<v._v[1]) return true;
60 else if (_v[1]>v._v[1]) return false;
61 else return (_v[2]<v._v[2]);
62 }
63
64 inline value_type* ptr() { return _v; }
65 inline const value_type* ptr() const { return _v; }
66
68 {
69 _v[0]=x; _v[1]=y; _v[2]=z;
70 }
71
72 inline void set( const Vec3f& rhs)
73 {
74 _v[0]=rhs._v[0]; _v[1]=rhs._v[1]; _v[2]=rhs._v[2];
75 }
76
77 inline value_type& operator [] (int i) { return _v[i]; }
78 inline value_type operator [] (int i) const { return _v[i]; }
79
80 inline value_type& x() { return _v[0]; }
81 inline value_type& y() { return _v[1]; }
82 inline value_type& z() { return _v[2]; }
83
84 inline value_type x() const { return _v[0]; }
85 inline value_type y() const { return _v[1]; }
86 inline value_type z() const { return _v[2]; }
87
89 inline bool valid() const { return !isNaN(); }
91 inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]) || osg::isNaN(_v[2]); }
92
94 inline value_type operator * (const Vec3f& rhs) const
95 {
96 return _v[0]*rhs._v[0]+_v[1]*rhs._v[1]+_v[2]*rhs._v[2];
97 }
98
100 inline const Vec3f operator ^ (const Vec3f& rhs) const
101 {
102 return Vec3f(_v[1]*rhs._v[2]-_v[2]*rhs._v[1],
103 _v[2]*rhs._v[0]-_v[0]*rhs._v[2] ,
104 _v[0]*rhs._v[1]-_v[1]*rhs._v[0]);
105 }
106
108 inline const Vec3f operator * (value_type rhs) const
109 {
110 return Vec3f(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs);
111 }
112
115 {
116 _v[0]*=rhs;
117 _v[1]*=rhs;
118 _v[2]*=rhs;
119 return *this;
120 }
121
123 inline const Vec3f operator / (value_type rhs) const
124 {
125 return Vec3f(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs);
126 }
127
130 {
131 _v[0]/=rhs;
132 _v[1]/=rhs;
133 _v[2]/=rhs;
134 return *this;
135 }
136
138 inline const Vec3f operator + (const Vec3f& rhs) const
139 {
140 return Vec3f(_v[0]+rhs._v[0], _v[1]+rhs._v[1], _v[2]+rhs._v[2]);
141 }
142
146 inline Vec3f& operator += (const Vec3f& rhs)
147 {
148 _v[0] += rhs._v[0];
149 _v[1] += rhs._v[1];
150 _v[2] += rhs._v[2];
151 return *this;
152 }
153
155 inline const Vec3f operator - (const Vec3f& rhs) const
156 {
157 return Vec3f(_v[0]-rhs._v[0], _v[1]-rhs._v[1], _v[2]-rhs._v[2]);
158 }
159
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
170 inline const Vec3f operator - () const
171 {
172 return Vec3f (-_v[0], -_v[1], -_v[2]);
173 }
174
176 inline value_type length() const
177 {
178 return sqrtf( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] );
179 }
180
182 inline value_type length2() const
183 {
184 return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2];
185 }
186
191 {
192 value_type norm = Vec3f::length();
193 if (norm>0.0)
194 {
195 value_type inv = 1.0f/norm;
196 _v[0] *= inv;
197 _v[1] *= inv;
198 _v[2] *= inv;
199 }
200 return( norm );
201 }
202
203}; // end of class Vec3f
204
206inline Vec3f componentMultiply(const Vec3f& lhs, const Vec3f& rhs)
207{
208 return Vec3f(lhs[0]*rhs[0], lhs[1]*rhs[1], lhs[2]*rhs[2]);
209}
210
212inline Vec3f componentDivide(const Vec3f& lhs, const Vec3f& rhs)
213{
214 return Vec3f(lhs[0]/rhs[0], lhs[1]/rhs[1], lhs[2]/rhs[2]);
215}
216
217const Vec3f X_AXIS(1.0,0.0,0.0);
218const Vec3f Y_AXIS(0.0,1.0,0.0);
219const Vec3f Z_AXIS(0.0,0.0,1.0);
220
221} // end of namespace osg
222
223#endif
224
The core osg library provides the basic scene graph classes such as Nodes, State and Drawables,...
Definition AlphaFunc:19
const Vec3f X_AXIS(1.0, 0.0, 0.0)
const Vec3f Z_AXIS(0.0, 0.0, 1.0)
Vec2d componentDivide(const Vec2d &lhs, const Vec2d &rhs)
divide rhs components by rhs vector components.
Definition Vec2d:187
bool isNaN(float v)
Definition Math:133
Vec2d componentMultiply(const Vec2d &lhs, const Vec2d &rhs)
multiply by vector components.
Definition Vec2d:181
const Vec3f Y_AXIS(0.0, 1.0, 0.0)
General purpose float pair.
Definition Vec2f:29
General purpose float triple for use as vertices, vectors and normals.
Definition Vec3f:29
Vec3f & operator-=(const Vec3f &rhs)
Unary vector subtract.
Definition Vec3f:161
Vec3f(value_type x, value_type y, value_type z)
Definition Vec3f:42
const Vec3f operator+(const Vec3f &rhs) const
Binary vector add.
Definition Vec3f:138
value_type & z()
Definition Vec3f:82
const Vec3f operator^(const Vec3f &rhs) const
Cross product.
Definition Vec3f:100
const Vec3f operator-() const
Negation operator.
Definition Vec3f:170
void set(value_type x, value_type y, value_type z)
Definition Vec3f:67
value_type operator*(const Vec3f &rhs) const
Dot product.
Definition Vec3f:94
value_type * ptr()
Definition Vec3f:64
value_type & y()
Definition Vec3f:81
Vec3f & operator/=(value_type rhs)
Unary divide by scalar.
Definition Vec3f:129
float value_type
Data type of vector components.
Definition Vec3f:33
const Vec3f operator/(value_type rhs) const
Divide by scalar.
Definition Vec3f:123
value_type z() const
Definition Vec3f:86
value_type y() const
Definition Vec3f:85
Vec3f & operator+=(const Vec3f &rhs)
Unary vector add.
Definition Vec3f:146
Vec3f()
Constructor that sets all components of the vector to zero.
Definition Vec3f:41
value_type & operator[](int i)
Definition Vec3f:77
bool isNaN() const
Returns true if at least one component has value NaN.
Definition Vec3f:91
value_type normalize()
Normalize the vector so that it has length unity.
Definition Vec3f:190
@ num_components
Definition Vec3f:36
value_type _v[3]
Definition Vec3f:38
const value_type * ptr() const
Definition Vec3f:65
value_type & x()
Definition Vec3f:80
value_type length2() const
Length squared of the vector = vec .
Definition Vec3f:182
bool operator!=(const Vec3f &v) const
Definition Vec3f:53
value_type length() const
Length of the vector = sqrt( vec .
Definition Vec3f:176
bool operator==(const Vec3f &v) const
Definition Vec3f:51
bool operator<(const Vec3f &v) const
Definition Vec3f:55
value_type x() const
Definition Vec3f:84
Vec3f(const Vec2f &v2, value_type zz)
Definition Vec3f:43
void set(const Vec3f &rhs)
Definition Vec3f:72
bool valid() const
Returns true if all components have values that are not NaN.
Definition Vec3f:89
Vec3f & operator*=(value_type rhs)
Unary multiply by scalar.
Definition Vec3f:114

osg logo
Generated at Wed Jul 23 2025 00:00:00 for the OpenSceneGraph by doxygen 1.14.0.