Coin Logo Coin3D is Free Software,
published under the BSD 3-clause license.
https://coin3d.github.io
https://www.kongsberg.com/en/kogt/
SbVec2f.h
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:
51  SbVec2f(void) { }
52  SbVec2f(const float v[2]) { vec[0] = v[0]; vec[1] = v[1]; }
53  SbVec2f(float x, float y) { vec[0] = x; vec[1] = y; }
54  explicit SbVec2f(const SbVec2d & v) { setValue(v); }
55  explicit SbVec2f(const SbVec2b & v) { setValue(v); }
56  explicit SbVec2f(const SbVec2s & v) { setValue(v); }
57  explicit SbVec2f(const SbVec2i32 & v) { setValue(v); }
58 
59  SbVec2f & setValue(const float v[2]) { vec[0] = v[0]; vec[1] = v[1]; return *this; }
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 
66  const float * getValue(void) const { return vec; }
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 
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;
75  float sqrLength(void) const { return vec[0] * vec[0] + vec[1] * vec[1]; }
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
The SbVec2f class is a 2 dimensional vector with floating point coordinates.
Definition: SbVec2f.h:49
void getValue(float &x, float &y) const
Definition: SbVec2f.h:67
SbVec2f(void)
Definition: SbVec2f.h:51
float dot(const SbVec2f &v) const
Definition: SbVec2f.h:72
a vector class for containing two byte integers.
Definition: SbVec2b.h:48
SbVec2f(const SbVec2i32 &v)
Definition: SbVec2f.h:57
int operator==(const SbBox2s &b1, const SbBox2s &b2)
Definition: SbBox2s.h:102
void negate(void)
Definition: SbVec2f.h:76
SbVec2f(const SbVec2s &v)
Definition: SbVec2f.h:56
SbVec2s operator-(const SbVec2s &v1, const SbVec2s &v2)
int operator!=(const SbBox2s &b1, const SbBox2s &b2)
Definition: SbBox2s.h:106
SbVec2f(const SbVec2d &v)
Definition: SbVec2f.h:54
float sqrLength(void) const
Definition: SbVec2f.h:75
The SbVec2d class is a 2 dimensional vector with double precision floating point coordinates.
Definition: SbVec2d.h:48
The SbVec2i32 class is a 2 dimensional vector with 32-bit signed integer coordinates.
Definition: SbVec2i32.h:50
SbVec2f & setValue(float x, float y)
Definition: SbVec2f.h:60
SbVec2f & setValue(const float v[2])
Definition: SbVec2f.h:59
SbVec2f(float x, float y)
Definition: SbVec2f.h:53
SbVec2s operator/(const SbVec2s &v, int d)
SbVec2s operator*(const SbVec2s &v, int d)
The SbString class is a string class with convenience functions for string operations.
Definition: SbString.h:52
SbVec2f(const float v[2])
Definition: SbVec2f.h:52
The SbVec2s class is a 2 dimensional vector with short integer coordinates.
Definition: SbVec2s.h:51
SbVec2f(const SbVec2b &v)
Definition: SbVec2f.h:55
const float * getValue(void) const
Definition: SbVec2f.h:66
SbVec2s operator+(const SbVec2s &v1, const SbVec2s &v2)