1 /**************************************************************************\
2 * Copyright (c) Kongsberg Oil & Gas Technologies AS
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 \**************************************************************************/
32
33 /*!
34 \class SbVec2f SbVec2f.h Inventor/SbVec2f.h
35 \brief The SbVec2f class is a 2 dimensional vector with floating point coordinates.
36
37 \ingroup base
38
39 This vector class is used by many other classes in
40 Coin. It provides storage for a vector in 2 dimensions as well
41 as simple floating point arithmetic operations on this vector.
42
43 \sa SbVec2s, SbVec2d, SbVec3s, SbVec3f, SbVec3d, SbVec4f, SbVec4d.
44 */
45
46 #include <Inventor/SbVec2f.h>
47
48 #include <limits>
49 #include <cassert>
50
51 #include <Inventor/SbVec2d.h>
52 #include <Inventor/SbVec2b.h>
53 #include <Inventor/SbVec2s.h>
54 #include <Inventor/SbVec2i32.h>
55 #include <Inventor/fields/SoSFVec2f.h>
56
57 #include "coinString.h"
58
59 #if COIN_DEBUG
60 #include <Inventor/errors/SoDebugError.h>
61 #endif // COIN_DEBUG
62
63 #include "tidbitsp.h" // coin_debug_normalize()
64
65 // *************************************************************************
66
67 /*!
68 \fn SbVec2f::SbVec2f(void)
69
70 The default constructor does nothing. The vector coordinates will be
71 uninitialized until you do a setValue().
72 */
73
74 /*!
75 \fn SbVec2f::SbVec2f(const float v[2])
76
77 Constructs an SbVec2f instance with initial values from \a v.
78 */
79
80 /*!
81 \fn SbVec2f::SbVec2f(float x, float y)
82
83 Constructs an SbVec2f instance with the initial vector endpoints from
84 \a x and \a y.
85 */
86
87 /*!
88 \fn SbVec2f::SbVec2f(const SbVec2d & v)
89
90 Constructs an instance from an SbVec2d instance.
91
92 \since Coin 2.5
93 */
94
95 /*!
96 \fn SbVec2f::SbVec2f(const SbVec2b & v)
97
98 Constructs an instance from an SbVec2d instance.
99
100 \since Coin 2.5
101 */
102
103 /*!
104 \fn SbVec2f::SbVec2f(const SbVec2s & v)
105
106 Constructs an instance from an SbVec2d instance.
107
108 \since Coin 2.5
109 */
110
111 /*!
112 \fn SbVec2f::SbVec2f(const SbVec2i32 & v)
113
114 Constructs an instance from an SbVec2d instance.
115
116 \since Coin 2.5
117 */
118
119 /*!
120 \fn float SbVec2f::dot(const SbVec2f & v) const
121
122 Calculates and returns the result of taking the dot product of this
123 vector and \a v.
124 */
125
126 /*!
127 Compares the vector with \a v and returns \c TRUE if the distance
128 between the vectors is smaller or equal to the square root of
129 \a tolerance.
130 */
131
132 SbBool
equals(const SbVec2f & v,float tolerance) const133 SbVec2f::equals(const SbVec2f& v, float tolerance) const
134 {
135 #if COIN_DEBUG
136 if(!(tolerance >= 0.0f))
137 SoDebugError::postWarning("SbVec2f::equals",
138 "Tolerance should be >= 0.0f");
139 #endif // COIN_DEBUG
140
141 float xdist = this->vec[0] - v[0];
142 float ydist = this->vec[1] - v[1];
143
144 if((xdist*xdist + ydist*ydist) <= tolerance) return TRUE;
145 return FALSE;
146 }
147
148 /*!
149 \fn const float * SbVec2f::getValue(void) const
150
151 Returns a pointer to an array of two floats containing the x and y
152 coordinates of the vector.
153
154 \sa setValue().
155 */
156
157 /*!
158 \fn void SbVec2f::getValue(float & x, float & y) const
159
160 Returns the x and y coordinates of the vector.
161
162 \sa setValue().
163 */
164
165 /*!
166 Return length of vector.
167 */
168
169 float
length(void) const170 SbVec2f::length(void) const
171 {
172 return static_cast<float>(sqrt(this->sqrLength()));
173 }
174
175 /*!
176 \fn float SbVec2f::sqrLength(void) const
177
178 Returns the square of the length of the vector.
179
180 \since Coin 2.5
181 */
182
183 /*!
184 \fn void SbVec2f::negate(void)
185
186 Negate the vector (i.e. point it in the opposite direction).
187 */
188
189 /*!
190 Normalize the vector to unit length. Return value is the original
191 length of the vector before normalization.
192 */
193
194 float
normalize(void)195 SbVec2f::normalize(void)
196 {
197 float len = this->length();
198
199 if (len > 0.0f) {
200 operator/=(len);
201 }
202 #if COIN_DEBUG
203 else if (coin_debug_normalize()) {
204 SoDebugError::postWarning("SbVec2f::normalize",
205 "The length of the vector should be > 0.0f "
206 "to be able to normalize.");
207 }
208 #endif // COIN_DEBUG
209 return len;
210 }
211
212 /*!
213 \fn SbVec2f & SbVec2f::setValue(const float v[2])
214
215 Set new x and y coordinates for the vector from \a v. Returns reference to
216 self.
217
218 \sa getValue().
219 */
220
221 /*!
222 \fn SbVec2f & SbVec2f::setValue(float x, float y)
223
224 Set new x and y coordinates for the vector. Returns reference to self.
225
226 \sa getValue().
227 */
228
229 /*!
230 Sets the value from an SbVec2d instance.
231
232 \since Coin 2.5
233 */
234
235 SbVec2f &
setValue(const SbVec2d & v)236 SbVec2f::setValue(const SbVec2d & v)
237 {
238 vec[0] = static_cast<float>(v[0]);
239 vec[1] = static_cast<float>(v[1]);
240 return *this;
241 }
242
243 /*!
244 Sets the value from an SbVec2b instance.
245
246 \since Coin 2.5
247 */
248
249 SbVec2f &
setValue(const SbVec2b & v)250 SbVec2f::setValue(const SbVec2b & v)
251 {
252 vec[0] = static_cast<float>(v[0]);
253 vec[1] = static_cast<float>(v[1]);
254 return *this;
255 }
256
257 /*!
258 Sets the value from an SbVec2s instance.
259
260 \since Coin 2.5
261 */
262
263 SbVec2f &
setValue(const SbVec2s & v)264 SbVec2f::setValue(const SbVec2s & v)
265 {
266 vec[0] = static_cast<float>(v[0]);
267 vec[1] = static_cast<float>(v[1]);
268 return *this;
269 }
270
271 /*!
272 Sets the value from an SbVec2i32 instance.
273
274 \since Coin 2.5
275 */
276
277 SbVec2f &
setValue(const SbVec2i32 & v)278 SbVec2f::setValue(const SbVec2i32 & v)
279 {
280 vec[0] = static_cast<float>(v[0]);
281 vec[1] = static_cast<float>(v[1]);
282 return *this;
283 }
284
285 /*!
286 \fn float & SbVec2f::operator [] (int i)
287
288 Index operator. Returns modifiable x or y coordinate.
289
290 \sa getValue() and setValue().
291 */
292
293
294 /*!
295 \fn const float & SbVec2f::operator [] (int i) const
296
297 Index operator. Returns x or y coordinate.
298
299 \sa getValue().
300 */
301
302 /*!
303 \fn SbVec2f & SbVec2f::operator *= (float d)
304
305 Multiply components of vector with value \a d. Returns reference to self.
306 */
307
308
309 /*!
310 \fn SbVec2f & SbVec2f::operator /= (float d)
311
312 Divides components of vector with value \a d. Returns reference to self.
313 */
314
315 /*!
316 \fn SbVec2f & SbVec2f::operator += (const SbVec2f & v)
317
318 Adds this vector and vector \a u. Returns reference to self.
319 */
320
321 /*!
322 \fn SbVec2f & SbVec2f::operator -= (const SbVec2f & v)
323
324 Subtracts vector \a u from this vector. Returns reference to self.
325 */
326
327 /*!
328 \fn SbVec2f SbVec2f::operator - (void) const
329
330 Non-destructive negation operator. Returns a new SbVec2f instance which
331 points in the opposite direction of this vector.
332
333 \sa negate().
334 */
335
336 /*!
337 \fn SbVec2f operator * (const SbVec2f& v, float d)
338 \relates SbVec2f
339
340 Returns an SbVec2f instance which is the components of vector \a v
341 multiplied with \a d.
342 */
343
344 /*!
345 \fn SbVec2f operator * (float d, const SbVec2f & v)
346 \relates SbVec2f
347
348 Returns an SbVec2f instance which is the components of vector \a v
349 multiplied with \a d.
350 */
351
352 /*!
353 \fn SbVec2f operator / (const SbVec2f& v, float d)
354 \relates SbVec2f
355
356 Returns an SbVec2f instance which is the components of vector \a v
357 divided on the scalar factor \a d.
358 */
359
360 /*!
361 \fn SbVec2f operator + (const SbVec2f & v1, const SbVec2f & v2)
362 \relates SbVec2f
363
364 Returns an SbVec2f instance which is the sum of vectors \a v1 and \a v2.
365 */
366
367 /*!
368 \fn SbVec2f operator -(const SbVec2f & v1, const SbVec2f & v2)
369 \relates SbVec2f
370
371 Returns an SbVec2f instance which is vector \a v2 subtracted from
372 vector \a v1.
373 */
374
375 /*!
376 \fn int operator == (const SbVec2f & v1, const SbVec2f & v2)
377 \relates SbVec2f
378
379 Returns \a 1 if \a v1 and \a v2 are equal, \a 0 otherwise.
380
381 \sa equals().
382 */
383
384 /*!
385 \fn int operator != (const SbVec2f & v1, const SbVec2f & v2)
386 \relates SbVec2f
387
388 Returns \a 1 if \a v1 and \a v2 are not equal, \a 0 if they are equal.
389
390 \sa equals().
391 */
392
393 /*!
394 Return a string representation of this object
395 */
396 SbString
toString() const397 SbVec2f::toString() const
398 {
399 return CoinInternal::ToString(*this);
400 }
401
402 /*!
403 Convert from a string representation, return wether this is a valid conversion
404 */
405 SbBool
fromString(const SbString & str)406 SbVec2f::fromString(const SbString & str)
407 {
408 SbBool conversionOk;
409 *this = CoinInternal::FromString<SbVec2f>(str,&conversionOk);
410 return conversionOk;
411 }
412
413 /*!
414 Dump the state of this object to the \a file stream. Only works in
415 debug version of library, method does nothing in an optimized
416 compile.
417 */
418 void
print(FILE * fp) const419 SbVec2f::print(FILE * fp) const
420 {
421 #if COIN_DEBUG
422 fputs(this->toString().getString(),fp);
423 #endif // COIN_DEBUG
424 }
425