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_VEC2F |
---|
15 | #define OSG_VEC2F 1 |
---|
16 | |
---|
17 | #include <osg/Math> |
---|
18 | |
---|
19 | namespace osg { |
---|
20 | |
---|
21 | /** General purpose float pair. Uses include representation of |
---|
22 | * texture coordinates. |
---|
23 | * No support yet added for float * Vec2f - is it necessary? |
---|
24 | * Need to define a non-member non-friend operator* etc. |
---|
25 | * BTW: Vec2f * float is okay |
---|
26 | */ |
---|
27 | |
---|
28 | class Vec2f |
---|
29 | { |
---|
30 | public: |
---|
31 | |
---|
32 | /** Data type of vector components.*/ |
---|
33 | typedef float value_type; |
---|
34 | |
---|
35 | /** Number of vector components. */ |
---|
36 | enum { num_components = 2 }; |
---|
37 | |
---|
38 | /** Vec member variable. */ |
---|
39 | value_type _v[2]; |
---|
40 | |
---|
41 | |
---|
42 | /** Constructor that sets all components of the vector to zero */ |
---|
43 | Vec2f() {_v[0]=0.0; _v[1]=0.0;} |
---|
44 | Vec2f(value_type x,value_type y) { _v[0]=x; _v[1]=y; } |
---|
45 | |
---|
46 | |
---|
47 | inline bool operator == (const Vec2f& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1]; } |
---|
48 | |
---|
49 | inline bool operator != (const Vec2f& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1]; } |
---|
50 | |
---|
51 | inline bool operator < (const Vec2f& v) const |
---|
52 | { |
---|
53 | if (_v[0]<v._v[0]) return true; |
---|
54 | else if (_v[0]>v._v[0]) return false; |
---|
55 | else return (_v[1]<v._v[1]); |
---|
56 | } |
---|
57 | |
---|
58 | inline value_type * ptr() { return _v; } |
---|
59 | inline const value_type * ptr() const { return _v; } |
---|
60 | |
---|
61 | inline void set( value_type x, value_type y ) { _v[0]=x; _v[1]=y; } |
---|
62 | |
---|
63 | inline value_type & operator [] (int i) { return _v[i]; } |
---|
64 | inline value_type operator [] (int i) const { return _v[i]; } |
---|
65 | |
---|
66 | inline value_type & x() { return _v[0]; } |
---|
67 | inline value_type & y() { return _v[1]; } |
---|
68 | |
---|
69 | inline value_type x() const { return _v[0]; } |
---|
70 | inline value_type y() const { return _v[1]; } |
---|
71 | |
---|
72 | /** Returns true if all components have values that are not NaN. */ |
---|
73 | inline bool valid() const { return !isNaN(); } |
---|
74 | /** Returns true if at least one component has value NaN. */ |
---|
75 | inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]); } |
---|
76 | |
---|
77 | /** Dot product. */ |
---|
78 | inline value_type operator * (const Vec2f& rhs) const |
---|
79 | { |
---|
80 | return _v[0]*rhs._v[0]+_v[1]*rhs._v[1]; |
---|
81 | } |
---|
82 | |
---|
83 | /** Multiply by scalar. */ |
---|
84 | inline const Vec2f operator * (value_type rhs) const |
---|
85 | { |
---|
86 | return Vec2f(_v[0]*rhs, _v[1]*rhs); |
---|
87 | } |
---|
88 | |
---|
89 | /** Unary multiply by scalar. */ |
---|
90 | inline Vec2f& 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 Vec2f operator / (value_type rhs) const |
---|
99 | { |
---|
100 | return Vec2f(_v[0]/rhs, _v[1]/rhs); |
---|
101 | } |
---|
102 | |
---|
103 | /** Unary divide by scalar. */ |
---|
104 | inline Vec2f& 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 Vec2f operator + (const Vec2f& rhs) const |
---|
113 | { |
---|
114 | return Vec2f(_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 Vec2f& operator += (const Vec2f& 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 Vec2f operator - (const Vec2f& rhs) const |
---|
129 | { |
---|
130 | return Vec2f(_v[0]-rhs._v[0], _v[1]-rhs._v[1]); |
---|
131 | } |
---|
132 | |
---|
133 | /** Unary vector subtract. */ |
---|
134 | inline Vec2f& operator -= (const Vec2f& 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 Vec2f. */ |
---|
142 | inline const Vec2f operator - () const |
---|
143 | { |
---|
144 | return Vec2f (-_v[0], -_v[1]); |
---|
145 | } |
---|
146 | |
---|
147 | /** Length of the vector = sqrt( vec . vec ) */ |
---|
148 | inline value_type length() const |
---|
149 | { |
---|
150 | return sqrtf( _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 = Vec2f::length(); |
---|
165 | if (norm>0.0) |
---|
166 | { |
---|
167 | value_type inv = 1.0f/norm; |
---|
168 | _v[0] *= inv; |
---|
169 | _v[1] *= inv; |
---|
170 | } |
---|
171 | return( norm ); |
---|
172 | } |
---|
173 | |
---|
174 | }; // end of class Vec2f |
---|
175 | |
---|
176 | /** multiply by vector components. */ |
---|
177 | inline Vec2f componentMultiply(const Vec2f& lhs, const Vec2f& rhs) |
---|
178 | { |
---|
179 | return Vec2f(lhs[0]*rhs[0], lhs[1]*rhs[1]); |
---|
180 | } |
---|
181 | |
---|
182 | /** divide rhs components by rhs vector components. */ |
---|
183 | inline Vec2f componentDivide(const Vec2f& lhs, const Vec2f& rhs) |
---|
184 | { |
---|
185 | return Vec2f(lhs[0]/rhs[0], lhs[1]/rhs[1]); |
---|
186 | } |
---|
187 | |
---|
188 | } // end of namespace osg |
---|
189 | #endif |
---|
190 | |
---|