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_VEC2D |
---|
15 | #define OSG_VEC2D 1 |
---|
16 | |
---|
17 | #include <osg/Vec2f> |
---|
18 | |
---|
19 | namespace osg { |
---|
20 | |
---|
21 | /** General purpose double pair, uses include representation of |
---|
22 | * texture coordinates. |
---|
23 | * No support yet added for double * Vec2d - is it necessary? |
---|
24 | * Need to define a non-member non-friend operator* etc. |
---|
25 | * BTW: Vec2d * double is okay |
---|
26 | */ |
---|
27 | |
---|
28 | class Vec2d |
---|
29 | { |
---|
30 | public: |
---|
31 | |
---|
32 | /** Type of Vec class.*/ |
---|
33 | typedef double value_type; |
---|
34 | |
---|
35 | /** Number of vector components. */ |
---|
36 | enum { num_components = 2 }; |
---|
37 | |
---|
38 | value_type _v[2]; |
---|
39 | |
---|
40 | Vec2d() {_v[0]=0.0; _v[1]=0.0;} |
---|
41 | |
---|
42 | Vec2d(value_type x,value_type y) { _v[0]=x; _v[1]=y; } |
---|
43 | |
---|
44 | inline Vec2d(const Vec2f& vec) { _v[0]=vec._v[0]; _v[1]=vec._v[1]; } |
---|
45 | |
---|
46 | inline operator Vec2f() const { return Vec2f(static_cast<float>(_v[0]),static_cast<float>(_v[1]));} |
---|
47 | |
---|
48 | |
---|
49 | inline bool operator == (const Vec2d& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1]; } |
---|
50 | |
---|
51 | inline bool operator != (const Vec2d& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1]; } |
---|
52 | |
---|
53 | inline bool operator < (const Vec2d& v) const |
---|
54 | { |
---|
55 | if (_v[0]<v._v[0]) return true; |
---|
56 | else if (_v[0]>v._v[0]) return false; |
---|
57 | else return (_v[1]<v._v[1]); |
---|
58 | } |
---|
59 | |
---|
60 | inline value_type* ptr() { return _v; } |
---|
61 | inline const value_type* ptr() const { return _v; } |
---|
62 | |
---|
63 | inline void set( value_type x, value_type y ) { _v[0]=x; _v[1]=y; } |
---|
64 | |
---|
65 | inline value_type& operator [] (int i) { return _v[i]; } |
---|
66 | inline value_type operator [] (int i) const { return _v[i]; } |
---|
67 | |
---|
68 | inline value_type& x() { return _v[0]; } |
---|
69 | inline value_type& y() { return _v[1]; } |
---|
70 | |
---|
71 | inline value_type x() const { return _v[0]; } |
---|
72 | inline value_type y() const { return _v[1]; } |
---|
73 | |
---|
74 | inline bool valid() const { return !isNaN(); } |
---|
75 | inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]); } |
---|
76 | |
---|
77 | /** Dot product. */ |
---|
78 | inline value_type operator * (const Vec2d& rhs) const |
---|
79 | { |
---|
80 | return _v[0]*rhs._v[0]+_v[1]*rhs._v[1]; |
---|
81 | } |
---|
82 | |
---|
83 | /** Multiply by scalar. */ |
---|
84 | inline const Vec2d operator * (value_type rhs) const |
---|
85 | { |
---|
86 | return Vec2d(_v[0]*rhs, _v[1]*rhs); |
---|
87 | } |
---|
88 | |
---|
89 | /** Unary multiply by scalar. */ |
---|
90 | inline Vec2d& operator *= (value_type rhs) |
---|
91 | { |
---|
92 | _v[0]*=rhs; |
---|
93 | _v[1]*=rhs; |
---|
94 | return *this; |
---|
95 | } |
---|
96 | |
---|
97 | /** Unary multiply by vector. */ |
---|
98 | inline Vec2d& operator *= (const Vec2d& rhs) |
---|
99 | { |
---|
100 | _v[0]*=rhs[0]; |
---|
101 | _v[1]*=rhs[1]; |
---|
102 | return *this; |
---|
103 | } |
---|
104 | |
---|
105 | /** Divide by scalar. */ |
---|
106 | inline const Vec2d operator / (value_type rhs) const |
---|
107 | { |
---|
108 | return Vec2d(_v[0]/rhs, _v[1]/rhs); |
---|
109 | } |
---|
110 | |
---|
111 | /** Unary divide by scalar. */ |
---|
112 | inline Vec2d& operator /= (value_type rhs) |
---|
113 | { |
---|
114 | _v[0]/=rhs; |
---|
115 | _v[1]/=rhs; |
---|
116 | return *this; |
---|
117 | } |
---|
118 | |
---|
119 | /** Unary divide by vector. */ |
---|
120 | inline Vec2d& operator /= (const Vec2d& rhs) |
---|
121 | { |
---|
122 | _v[0]/=rhs[0]; |
---|
123 | _v[1]/=rhs[1]; |
---|
124 | return *this; |
---|
125 | } |
---|
126 | |
---|
127 | /** Binary vector add. */ |
---|
128 | inline const Vec2d operator + (const Vec2d& rhs) const |
---|
129 | { |
---|
130 | return Vec2d(_v[0]+rhs._v[0], _v[1]+rhs._v[1]); |
---|
131 | } |
---|
132 | |
---|
133 | /** Unary vector add. Slightly more efficient because no temporary |
---|
134 | * intermediate object. |
---|
135 | */ |
---|
136 | inline Vec2d& operator += (const Vec2d& rhs) |
---|
137 | { |
---|
138 | _v[0] += rhs._v[0]; |
---|
139 | _v[1] += rhs._v[1]; |
---|
140 | return *this; |
---|
141 | } |
---|
142 | |
---|
143 | /** Binary vector subtract. */ |
---|
144 | inline const Vec2d operator - (const Vec2d& rhs) const |
---|
145 | { |
---|
146 | return Vec2d(_v[0]-rhs._v[0], _v[1]-rhs._v[1]); |
---|
147 | } |
---|
148 | |
---|
149 | /** Unary vector subtract. */ |
---|
150 | inline Vec2d& operator -= (const Vec2d& rhs) |
---|
151 | { |
---|
152 | _v[0]-=rhs._v[0]; |
---|
153 | _v[1]-=rhs._v[1]; |
---|
154 | return *this; |
---|
155 | } |
---|
156 | |
---|
157 | /** Negation operator. Returns the negative of the Vec2d. */ |
---|
158 | inline const Vec2d operator - () const |
---|
159 | { |
---|
160 | return Vec2d (-_v[0], -_v[1]); |
---|
161 | } |
---|
162 | |
---|
163 | /** Length of the vector = sqrt( vec . vec ) */ |
---|
164 | inline value_type length() const |
---|
165 | { |
---|
166 | return sqrt( _v[0]*_v[0] + _v[1]*_v[1] ); |
---|
167 | } |
---|
168 | |
---|
169 | /** Length squared of the vector = vec . vec */ |
---|
170 | inline value_type length2( void ) const |
---|
171 | { |
---|
172 | return _v[0]*_v[0] + _v[1]*_v[1]; |
---|
173 | } |
---|
174 | |
---|
175 | /** Normalize the vector so that it has length unity. |
---|
176 | * Returns the previous length of the vector. |
---|
177 | */ |
---|
178 | inline value_type normalize() |
---|
179 | { |
---|
180 | value_type norm = Vec2d::length(); |
---|
181 | if (norm>0.0) |
---|
182 | { |
---|
183 | value_type inv = 1.0/norm; |
---|
184 | _v[0] *= inv; |
---|
185 | _v[1] *= inv; |
---|
186 | } |
---|
187 | return( norm ); |
---|
188 | } |
---|
189 | |
---|
190 | }; // end of class Vec2d |
---|
191 | |
---|
192 | } // end of namespace osg |
---|
193 | #endif |
---|