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