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 SbVec2s SbVec2s.h Inventor/SbVec2s.h
35   \brief The SbVec2s class is a 2 dimensional vector with short integer
36   coordinates.
37 
38   \ingroup base
39 
40   This vector class is used by many other classes in
41   Coin. It provides storage for a vector in 2 dimensions
42   as well as simple integer arithmetic operations.
43 
44   \sa SbVec2f, SbVec2d, SbVec3s, SbVec3f, SbVec3d, SbVec4f, SbVec4d.
45 */
46 
47 #include <Inventor/SbVec2s.h>
48 
49 #include <limits>
50 #include <cassert>
51 
52 #include <Inventor/SbVec2us.h>
53 #include <Inventor/SbVec2b.h>
54 #include <Inventor/SbVec2i32.h>
55 #include <Inventor/SbVec2f.h>
56 #include <Inventor/SbVec2d.h>
57 #include <Inventor/fields/SoSFVec2s.h>
58 
59 #include "coinString.h"
60 #if COIN_DEBUG
61 #include <Inventor/errors/SoDebugError.h>
62 #endif // COIN_DEBUG
63 
64 /*!
65   \fn SbVec2s::SbVec2s(void)
66 
67   The default constructor does nothing. The vector coordinates will be
68   uninitialized until you do a setValue().
69 */
70 
71 /*!
72   \fn SbVec2s::SbVec2s(const short v[2])
73 
74   Constructs an SbVec2s instance with initial values from \a v.
75 */
76 
77 /*!
78   \fn SbVec2s::SbVec2s(short x, short y)
79 
80   Constructs an SbVec2s instance with the initial vector endpoints from
81   \a x and \a y.
82 */
83 
84 /*!
85   \fn SbVec2s::SbVec2s(const SbVec2us & v)
86 
87   Constructs an SbVec2s instance from the value in an SbVec2us instance.
88 
89   \since Coin 2.5
90 */
91 
92 /*!
93   \fn SbVec2s::SbVec2s(const SbVec2b & v)
94 
95   Constructs an SbVec2s instance from the value in an SbVec2b instance.
96 
97   \since Coin 2.5
98 */
99 
100 /*!
101   \fn SbVec2s::SbVec2s(const SbVec2i32 & v)
102 
103   Constructs an SbVec2s instance from the value in an SbVec2i32 instance.
104 
105   \since Coin 2.5
106 */
107 
108 /*!
109   \fn SbVec2s::SbVec2s(const SbVec2f & v)
110 
111   Constructs an SbVec2s instance from the value in an SbVec2f instance.
112 
113   \since Coin 2.5
114 */
115 
116 /*!
117   \fn SbVec2s::SbVec2s(const SbVec2d & v)
118 
119   Constructs an SbVec2s instance from the value in an SbVec2d instance.
120 
121   \since Coin 2.5
122 */
123 
124 /*!
125   \fn int32_t SbVec2s::dot(const SbVec2s & v) const
126 
127   Calculates and returns the result of taking the dot product of this
128   vector and \a v.
129 */
130 
131 /*!
132   \fn const short * SbVec2s::getValue(void) const
133 
134   Returns a pointer to an array of two floats containing the x and y
135   coordinates of the vector.
136 
137   \sa setValue().
138 */
139 
140 /*!
141   \fn void SbVec2s::getValue(short & x, short & y) const
142 
143   Returns the x and y coordinates of the vector.
144 
145   \sa setValue().
146 */
147 
148 /*!
149   \fn void SbVec2s::negate(void)
150 
151   Negate the vector (i.e. point it in the opposite direction).
152 */
153 
154 /*!
155   \fn SbVec2s & SbVec2s::setValue(const short v[2])
156 
157   Set new x and y coordinates for the vector from \a v. Returns reference to
158   self.
159 
160   \sa getValue().
161 */
162 
163 /*!
164   \fn SbVec2s & SbVec2s::setValue(short x, short y)
165 
166   Set new x and y coordinates for the vector. Returns reference to self.
167 
168   \sa getValue().
169 */
170 
171 /*!
172   \since Coin 2.5
173 */
174 
175 SbVec2s &
setValue(const SbVec2us & v)176 SbVec2s::setValue(const SbVec2us & v)
177 {
178   vec[0] = static_cast<short>(v[0]);
179   vec[1] = static_cast<short>(v[1]);
180   return *this;
181 }
182 
183 /*!
184   \since Coin 2.5
185 */
186 
187 SbVec2s &
setValue(const SbVec2b & v)188 SbVec2s::setValue(const SbVec2b & v)
189 {
190   vec[0] = static_cast<short>(v[0]);
191   vec[1] = static_cast<short>(v[1]);
192   return *this;
193 }
194 
195 /*!
196   \since Coin 2.5
197 */
198 
199 SbVec2s &
setValue(const SbVec2i32 & v)200 SbVec2s::setValue(const SbVec2i32 & v)
201 {
202 #if COIN_DEBUG
203   if (v[0] > std::numeric_limits<short>::max() || v[0] < -std::numeric_limits<short>::max() ||
204       v[1] > std::numeric_limits<short>::max() || v[1] < -std::numeric_limits<short>::max()) {
205     SoDebugError::post("SbVec2s::setValue", "SbVec2i32 argument out of range for SbVec2s");
206   }
207 #endif // COIN_DEBUG
208   vec[0] = static_cast<short>(v[0]);
209   vec[1] = static_cast<short>(v[1]);
210   return *this;
211 }
212 
213 /*!
214   \since Coin 2.5
215 */
216 
217 SbVec2s &
setValue(const SbVec2f & v)218 SbVec2s::setValue(const SbVec2f & v)
219 {
220 #if COIN_DEBUG
221   if (v[0] > std::numeric_limits<short>::max() || v[0] < -std::numeric_limits<short>::max() ||
222       v[1] > std::numeric_limits<short>::max() || v[1] < -std::numeric_limits<short>::max()) {
223     SoDebugError::post("SbVec2s::setValue", "SbVec2f argument out of range for SbVec2s");
224   }
225 #endif // COIN_DEBUG
226   vec[0] = static_cast<short>(v[0]);
227   vec[1] = static_cast<short>(v[1]);
228   return *this;
229 }
230 
231 /*!
232   \since Coin 2.5
233 */
234 
235 SbVec2s &
setValue(const SbVec2d & v)236 SbVec2s::setValue(const SbVec2d & v)
237 {
238 #if COIN_DEBUG
239   if (v[0] > std::numeric_limits<short>::max() || v[0] < -std::numeric_limits<short>::max() ||
240       v[1] > std::numeric_limits<short>::max() || v[1] < -std::numeric_limits<short>::max()) {
241     SoDebugError::post("SbVec2s::setValue", "SbVec2d argument out of range for SbVec2s");
242   }
243 #endif // COIN_DEBUG
244   vec[0] = static_cast<short>(v[0]);
245   vec[1] = static_cast<short>(v[1]);
246   return *this;
247 }
248 
249 /*!
250   \fn short & SbVec2s::operator [] (int i)
251 
252   Index operator. Returns modifiable x or y coordinate.
253 
254   \sa getValue() and setValue().
255 */
256 
257 /*!
258   \fn const short & SbVec2s::operator [](int i) const
259 
260   Index operator. Returns x or y coordinate.
261 
262   \sa getValue().
263 */
264 
265 /*!
266   \fn SbVec2s & SbVec2s::operator *= (int d)
267 
268   Multiply components of vector with value \a d. Returns reference to self.
269 */
270 
271 /*!
272   Multiply components of vector with value \a d. Returns reference to self.
273 */
274 
275 SbVec2s &
operator *=(double d)276 SbVec2s::operator *= (double d)
277 {
278   vec[0] = static_cast<short>(vec[0] * d);
279   vec[1] = static_cast<short>(vec[1] * d);
280   return *this;
281 }
282 
283 /*!
284   \fn SbVec2s & SbVec2s::operator /= (int d)
285 
286   Divides components of vector with value \a d. Returns reference to self.
287 */
288 
289 
290 /*!
291   \fn SbVec2s & SbVec2s::operator /= (double d)
292 
293   Divides components of vector with value \a d. Returns reference to self.
294 */
295 
296 /*!
297   \fn SbVec2s & SbVec2s::operator += (const SbVec2s & v)
298 
299   Adds this vector and vector \a v. Returns reference to self.
300 */
301 
302 /*!
303   \fn SbVec2s & SbVec2s::operator -= (const SbVec2s & v)
304 
305   Subtracts vector \a v from this vector. Returns reference to self.
306 */
307 
308 /*!
309   \fn SbVec2s SbVec2s::operator - (void) const
310 
311   Non-destructive negation operator. Returns a new SbVec2s instance which
312   points in the opposite direction of this vector.
313 
314   \sa negate().
315 */
316 
317 /*!
318   \fn SbVec2s operator * (const SbVec2s & v, int d)
319   \relates SbVec2s
320 
321   Returns an SbVec2s instance which is the components of vector \a v
322   multiplied with \a d.
323 */
324 
325 /*!
326   \fn SbVec2s operator * (const SbVec2s & v, double d)
327   \relates SbVec2s
328 
329   Returns an SbVec2s instance which is the components of vector \a v
330   multiplied with \a d.
331 */
332 
333 /*!
334   \fn SbVec2s operator * (int d, const SbVec2s & v)
335   \relates SbVec2s
336 
337   Returns an SbVec2s instance which is the components of vector \a v
338   multiplied with \a d.
339 */
340 
341 /*!
342   \fn SbVec2s operator * (double d, const SbVec2s & v)
343   \relates SbVec2s
344 
345   Returns an SbVec2s instance which is the components of vector \a v
346   multiplied with \a d.
347 */
348 
349 /*!
350   \fn SbVec2s operator / (const SbVec2s & v, int d)
351   \relates SbVec2s
352 
353   Returns an SbVec2s instance which is the components of vector \a v
354   divided on \a d.
355 */
356 
357 /*!
358   \fn SbVec2s operator / (const SbVec2s & v, double d)
359   \relates SbVec2s
360 
361   Returns an SbVec2s instance which is the components of vector \a v
362   divided on \a d.
363 */
364 
365 /*!
366   \fn SbVec2s operator + (const SbVec2s & v1, const SbVec2s & v2)
367   \relates SbVec2s
368 
369   Returns an SbVec2s instance which is the sum of vectors \a v1 and \a v2.
370 */
371 
372 /*!
373   \fn SbVec2s operator - (const SbVec2s & v1, const SbVec2s & v2)
374   \relates SbVec2s
375 
376   Returns an SbVec2s instance which is vector \a v2 subtracted from
377   vector \a v1.
378 */
379 
380 /*!
381   \fn int operator == (const SbVec2s & v1, const SbVec2s & v2)
382   \relates SbVec2s
383 
384   Returns \a 1 if \a v1 and \a v2 are equal, \a 0 otherwise.
385 */
386 
387 /*!
388   \fn int operator != (const SbVec2s & v1, const SbVec2s & v2)
389   \relates SbVec2s
390 
391   Returns \a 1 if \a v1 and \a v2 are not equal, \a 0 if they are equal.
392 */
393 
394 /*!
395   Return a string representation of this object
396 */
397 SbString
toString() const398 SbVec2s::toString() const
399 {
400   return CoinInternal::ToString(*this);
401 }
402 
403 /*!
404   Convert from a string representation, return wether this is a valid conversion
405 */
406 SbBool
fromString(const SbString & str)407 SbVec2s::fromString(const SbString & str)
408 {
409   SbBool conversionOk;
410   *this = CoinInternal::FromString<SbVec2s>(str,&conversionOk);
411   return conversionOk;
412 }
413 
414 /*!
415   Dump the state of this object to the \a file stream. Only works in
416   debug version of library, method does nothing in an optimized
417   compile.
418 */
419 void
print(FILE * fp) const420 SbVec2s::print(FILE * fp) const
421 {
422 #if COIN_DEBUG
423   fputs(this->toString().getString(),fp);
424 #endif // COIN_DEBUG
425 }
426