1 #ifndef COIN_SBVEC2F_H
2 #define COIN_SBVEC2F_H
3 
4 /**************************************************************************\
5  * Copyright (c) Kongsberg Oil & Gas Technologies AS
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * Neither the name of the copyright holder nor the names of its
20  * contributors may be used to endorse or promote products derived from
21  * this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 \**************************************************************************/
35 
36 #include <cstdio>
37 
38 #include <Inventor/SbBasic.h>
39 #include <Inventor/SbString.h>
40 #ifndef NDEBUG
41 #include <Inventor/errors/SoDebugError.h>
42 #endif // !NDEBUG
43 
44 class SbVec2d;
45 class SbVec2b;
46 class SbVec2s;
47 class SbVec2i32;
48 
49 class COIN_DLL_API SbVec2f {
50 public:
SbVec2f(void)51   SbVec2f(void) { }
SbVec2f(const float v[2])52   SbVec2f(const float v[2]) { vec[0] = v[0]; vec[1] = v[1]; }
SbVec2f(float x,float y)53   SbVec2f(float x, float y) { vec[0] = x; vec[1] = y; }
SbVec2f(const SbVec2d & v)54   explicit SbVec2f(const SbVec2d & v) { setValue(v); }
SbVec2f(const SbVec2b & v)55   explicit SbVec2f(const SbVec2b & v) { setValue(v); }
SbVec2f(const SbVec2s & v)56   explicit SbVec2f(const SbVec2s & v) { setValue(v); }
SbVec2f(const SbVec2i32 & v)57   explicit SbVec2f(const SbVec2i32 & v) { setValue(v); }
58 
setValue(const float v[2])59   SbVec2f & setValue(const float v[2]) { vec[0] = v[0]; vec[1] = v[1]; return *this; }
setValue(float x,float y)60   SbVec2f & setValue(float x, float y) { vec[0] = x; vec[1] = y; return *this; }
61   SbVec2f & setValue(const SbVec2d & v);
62   SbVec2f & setValue(const SbVec2b & v);
63   SbVec2f & setValue(const SbVec2s & v);
64   SbVec2f & setValue(const SbVec2i32 & v);
65 
getValue(void)66   const float * getValue(void) const { return vec; }
getValue(float & x,float & y)67   void getValue(float & x, float & y) const { x = vec[0]; y = vec[1]; }
68 
69   float & operator [] (int i) { return vec[i]; }
70   const float & operator [] (int i) const { return vec[i]; }
71 
dot(const SbVec2f & v)72   float dot(const SbVec2f & v) const { return vec[0] * v[0] + vec[1] * v[1]; }
73   SbBool equals(const SbVec2f & v, float tolerance) const;
74   float length(void) const;
sqrLength(void)75   float sqrLength(void) const { return vec[0] * vec[0] + vec[1] * vec[1]; }
negate(void)76   void negate(void) { vec[0] = -vec[0]; vec[1] = -vec[1]; }
77   float normalize(void);
78 
79   SbVec2f & operator *= (float d) { vec[0] *= d; vec[1] *= d; return *this; }
80   SbVec2f & operator /= (float d) { SbDividerChk("SbVec2f::operator/=(float)", d); return operator *= (1.0f / d); }
81   SbVec2f & operator += (const SbVec2f & v) { vec[0] += v[0]; vec[1] += v[1]; return *this; }
82   SbVec2f & operator -= (const SbVec2f & v) { vec[0] -= v[0]; vec[1] -= v[1]; return *this; }
83   SbVec2f operator - (void) const { return SbVec2f(-vec[0], -vec[1]); }
84 
85   SbString toString() const;
86   SbBool fromString(const SbString & str);
87 
88   void print(FILE * fp) const;
89 
90 protected:
91   float vec[2];
92 
93 }; // SbVec2f
94 
95 COIN_DLL_API inline SbVec2f operator * (const SbVec2f & v, float d) {
96   SbVec2f val(v); val *= d; return val;
97 }
98 
99 COIN_DLL_API inline SbVec2f operator * (float d, const SbVec2f & v) {
100   SbVec2f val(v); val *= d; return val;
101 }
102 
103 COIN_DLL_API inline SbVec2f operator / (const SbVec2f & v, float d) {
104   SbDividerChk("operator/(SbVec2f,float)", d);
105   SbVec2f val(v); val /= d; return val;
106 }
107 
108 COIN_DLL_API inline SbVec2f operator + (const SbVec2f & v1, const SbVec2f & v2) {
109   SbVec2f v(v1); v += v2; return v;
110 }
111 
112 COIN_DLL_API inline SbVec2f operator - (const SbVec2f & v1, const SbVec2f & v2) {
113   SbVec2f v(v1); v -= v2; return v;
114 }
115 
116 COIN_DLL_API inline int operator == (const SbVec2f & v1, const SbVec2f & v2) {
117   return ((v1[0] == v2[0]) && (v1[1] == v2[1]));
118 }
119 
120 COIN_DLL_API inline int operator != (const SbVec2f & v1, const SbVec2f & v2) {
121   return !(v1 == v2);
122 }
123 
124 // *************************************************************************
125 
126 #endif // !COIN_SBVEC2F_H
127