OpenSceneGraph 3.6.5
Vec3d
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_VEC3D
15#define OSG_VEC3D 1
16
17#include <osg/Vec2d>
18#include <osg/Vec3f>
19
20namespace osg {
21
28
29class Vec3d
30{
31 public:
32
34 typedef double value_type;
35
37 enum { num_components = 3 };
38
40
42 Vec3d() { _v[0]=0.0; _v[1]=0.0; _v[2]=0.0;}
43
44 inline Vec3d(const Vec3f& vec) { _v[0]=vec._v[0]; _v[1]=vec._v[1]; _v[2]=vec._v[2];}
45
46 inline operator Vec3f() const { return Vec3f(static_cast<float>(_v[0]),static_cast<float>(_v[1]),static_cast<float>(_v[2]));}
47
49 Vec3d(const Vec2d& v2,value_type zz)
50 {
51 _v[0] = v2[0];
52 _v[1] = v2[1];
53 _v[2] = zz;
54 }
55
56 inline bool operator == (const Vec3d& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1] && _v[2]==v._v[2]; }
57
58 inline bool operator != (const Vec3d& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1] || _v[2]!=v._v[2]; }
59
60 inline bool operator < (const Vec3d& v) const
61 {
62 if (_v[0]<v._v[0]) return true;
63 else if (_v[0]>v._v[0]) return false;
64 else if (_v[1]<v._v[1]) return true;
65 else if (_v[1]>v._v[1]) return false;
66 else return (_v[2]<v._v[2]);
67 }
68
69 inline value_type* ptr() { return _v; }
70 inline const value_type* ptr() const { return _v; }
71
73 {
74 _v[0]=x; _v[1]=y; _v[2]=z;
75 }
76
77 inline void set( const Vec3d& rhs)
78 {
79 _v[0]=rhs._v[0]; _v[1]=rhs._v[1]; _v[2]=rhs._v[2];
80 }
81
82 inline value_type& operator [] (int i) { return _v[i]; }
83 inline value_type operator [] (int i) const { return _v[i]; }
84
85 inline value_type& x() { return _v[0]; }
86 inline value_type& y() { return _v[1]; }
87 inline value_type& z() { return _v[2]; }
88
89 inline value_type x() const { return _v[0]; }
90 inline value_type y() const { return _v[1]; }
91 inline value_type z() const { return _v[2]; }
92
94 inline bool valid() const { return !isNaN(); }
96 inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]) || osg::isNaN(_v[2]); }
97
99 inline value_type operator * (const Vec3d& rhs) const
100 {
101 return _v[0]*rhs._v[0]+_v[1]*rhs._v[1]+_v[2]*rhs._v[2];
102 }
103
105 inline const Vec3d operator ^ (const Vec3d& rhs) const
106 {
107 return Vec3d(_v[1]*rhs._v[2]-_v[2]*rhs._v[1],
108 _v[2]*rhs._v[0]-_v[0]*rhs._v[2] ,
109 _v[0]*rhs._v[1]-_v[1]*rhs._v[0]);
110 }
111
113 inline const Vec3d operator * (value_type rhs) const
114 {
115 return Vec3d(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs);
116 }
117
120 {
121 _v[0]*=rhs;
122 _v[1]*=rhs;
123 _v[2]*=rhs;
124 return *this;
125 }
126
128 inline const Vec3d operator / (value_type rhs) const
129 {
130 return Vec3d(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs);
131 }
132
135 {
136 _v[0]/=rhs;
137 _v[1]/=rhs;
138 _v[2]/=rhs;
139 return *this;
140 }
141
143 inline const Vec3d operator + (const Vec3d& rhs) const
144 {
145 return Vec3d(_v[0]+rhs._v[0], _v[1]+rhs._v[1], _v[2]+rhs._v[2]);
146 }
147
151 inline Vec3d& operator += (const Vec3d& rhs)
152 {
153 _v[0] += rhs._v[0];
154 _v[1] += rhs._v[1];
155 _v[2] += rhs._v[2];
156 return *this;
157 }
158
160 inline const Vec3d operator - (const Vec3d& rhs) const
161 {
162 return Vec3d(_v[0]-rhs._v[0], _v[1]-rhs._v[1], _v[2]-rhs._v[2]);
163 }
164
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
175 inline const Vec3d operator - () const
176 {
177 return Vec3d (-_v[0], -_v[1], -_v[2]);
178 }
179
181 inline value_type length() const
182 {
183 return sqrt( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] );
184 }
185
187 inline value_type length2() const
188 {
189 return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2];
190 }
191
197 {
198 value_type norm = Vec3d::length();
199 if (norm>0.0)
200 {
201 value_type inv = 1.0/norm;
202 _v[0] *= inv;
203 _v[1] *= inv;
204 _v[2] *= inv;
205 }
206 return( norm );
207 }
208
209}; // end of class Vec3d
210
212inline Vec3d componentMultiply(const Vec3d& lhs, const Vec3d& rhs)
213{
214 return Vec3d(lhs[0]*rhs[0], lhs[1]*rhs[1], lhs[2]*rhs[2]);
215}
216
218inline Vec3d componentDivide(const Vec3d& lhs, const Vec3d& rhs)
219{
220 return Vec3d(lhs[0]/rhs[0], lhs[1]/rhs[1], lhs[2]/rhs[2]);
221}
222
223} // end of namespace osg
224
225#endif
The core osg library provides the basic scene graph classes such as Nodes, State and Drawables,...
Definition AlphaFunc:19
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
General purpose double pair, uses include representation of texture coordinates.
Definition Vec2d:29
General purpose double triple for use as vertices, vectors and normals.
Definition Vec3d:30
@ num_components
Definition Vec3d:37
double value_type
Data type of vector components.
Definition Vec3d:34
value_type z() const
Definition Vec3d:91
value_type & z()
Definition Vec3d:87
bool operator==(const Vec3d &v) const
Definition Vec3d:56
Vec3d(const Vec3f &vec)
Definition Vec3d:44
bool isNaN() const
Returns true if at least one component has value NaN.
Definition Vec3d:96
bool valid() const
Returns true if all components have values that are not NaN.
Definition Vec3d:94
value_type _v[3]
Definition Vec3d:39
Vec3d & operator-=(const Vec3d &rhs)
Unary vector subtract.
Definition Vec3d:166
Vec3d(value_type x, value_type y, value_type z)
Definition Vec3d:48
value_type operator*(const Vec3d &rhs) const
Dot product.
Definition Vec3d:99
const Vec3d operator+(const Vec3d &rhs) const
Binary vector add.
Definition Vec3d:143
value_type normalize()
Normalize the vector so that it has length unity.
Definition Vec3d:196
value_type length2() const
Length squared of the vector = vec .
Definition Vec3d:187
const value_type * ptr() const
Definition Vec3d:70
Vec3d()
Constructor that sets all components of the vector to zero.
Definition Vec3d:42
const Vec3d operator-() const
Negation operator.
Definition Vec3d:175
void set(value_type x, value_type y, value_type z)
Definition Vec3d:72
const Vec3d operator^(const Vec3d &rhs) const
Cross product.
Definition Vec3d:105
value_type & operator[](int i)
Definition Vec3d:82
value_type * ptr()
Definition Vec3d:69
void set(const Vec3d &rhs)
Definition Vec3d:77
value_type & x()
Definition Vec3d:85
value_type & y()
Definition Vec3d:86
bool operator!=(const Vec3d &v) const
Definition Vec3d:58
Vec3d & operator/=(value_type rhs)
Unary divide by scalar.
Definition Vec3d:134
value_type y() const
Definition Vec3d:90
bool operator<(const Vec3d &v) const
Definition Vec3d:60
value_type x() const
Definition Vec3d:89
Vec3d & operator+=(const Vec3d &rhs)
Unary vector add.
Definition Vec3d:151
Vec3d & operator*=(value_type rhs)
Unary multiply by scalar.
Definition Vec3d:119
const Vec3d operator/(value_type rhs) const
Divide by scalar.
Definition Vec3d:128
Vec3d(const Vec2d &v2, value_type zz)
Definition Vec3d:49
value_type length() const
Length of the vector = sqrt( vec .
Definition Vec3d:181
General purpose float triple for use as vertices, vectors and normals.
Definition Vec3f:29
value_type _v[3]
Definition Vec3f:38

osg logo
Generated at Sun Jul 20 2025 00:00:00 for the OpenSceneGraph by doxygen 1.14.0.