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 | /** Divide by scalar. */ |
---|
98 | inline const Vec2d operator / (value_type rhs) const |
---|
99 | { |
---|
100 | return Vec2d(_v[0]/rhs, _v[1]/rhs); |
---|
101 | } |
---|
102 | |
---|
103 | /** Unary divide by scalar. */ |
---|
104 | inline Vec2d& operator /= (value_type rhs) |
---|
105 | { |
---|
106 | _v[0]/=rhs; |
---|
107 | _v[1]/=rhs; |
---|
108 | return *this; |
---|
109 | } |
---|
110 | |
---|
111 | /** Binary vector add. */ |
---|
112 | inline const Vec2d operator + (const Vec2d& rhs) const |
---|
113 | { |
---|
114 | return Vec2d(_v[0]+rhs._v[0], _v[1]+rhs._v[1]); |
---|
115 | } |
---|
116 | |
---|
117 | /** Unary vector add. Slightly more efficient because no temporary |
---|
118 | * intermediate object. |
---|
119 | */ |
---|
120 | inline Vec2d& operator += (const Vec2d& rhs) |
---|
121 | { |
---|
122 | _v[0] += rhs._v[0]; |
---|
123 | _v[1] += rhs._v[1]; |
---|
124 | return *this; |
---|
125 | } |
---|
126 | |
---|
127 | /** Binary vector subtract. */ |
---|
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 subtract. */ |
---|
134 | inline Vec2d& operator -= (const Vec2d& rhs) |
---|
135 | { |
---|
136 | _v[0]-=rhs._v[0]; |
---|
137 | _v[1]-=rhs._v[1]; |
---|
138 | return *this; |
---|
139 | } |
---|
140 | |
---|
141 | /** Negation operator. Returns the negative of the Vec2d. */ |
---|
142 | inline const Vec2d operator - () const |
---|
143 | { |
---|
144 | return Vec2d (-_v[0], -_v[1]); |
---|
145 | } |
---|
146 | |
---|
147 | /** Length of the vector = sqrt( vec . vec ) */ |
---|
148 | inline value_type length() const |
---|
149 | { |
---|
150 | return sqrt( _v[0]*_v[0] + _v[1]*_v[1] ); |
---|
151 | } |
---|
152 | |
---|
153 | /** Length squared of the vector = vec . vec */ |
---|
154 | inline value_type length2( void ) const |
---|
155 | { |
---|
156 | return _v[0]*_v[0] + _v[1]*_v[1]; |
---|
157 | } |
---|
158 | |
---|
159 | /** Normalize the vector so that it has length unity. |
---|
160 | * Returns the previous length of the vector. |
---|
161 | */ |
---|
162 | inline value_type normalize() |
---|
163 | { |
---|
164 | value_type norm = Vec2d::length(); |
---|
165 | if (norm>0.0) |
---|
166 | { |
---|
167 | value_type inv = 1.0/norm; |
---|
168 | _v[0] *= inv; |
---|
169 | _v[1] *= inv; |
---|
170 | } |
---|
171 | return( norm ); |
---|
172 | } |
---|
173 | |
---|
174 | }; // end of class Vec2d |
---|
175 | |
---|
176 | } // end of namespace osg |
---|
177 | #endif |
---|