Coin: SbVec2s.h Source File

| Coin3D is Free Software, published under the BSD 3-clause license. | https://coin3d.github.io https://www.kongsberg.com/en/kogt/

SbVec2s.h
1#ifndef COIN_SBVEC2S_H
2#define COIN_SBVEC2S_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/system/inttypes.h>
40#include <Inventor/SbString.h>
41#ifndef NDEBUG
42#include <Inventor/errors/SoDebugError.h>
43#endif // !NDEBUG
44
45class SbVec2us;
46class SbVec2b;
47class SbVec2i32;
48class SbVec2f;
49class SbVec2d;
50
51 class COIN_DLL_API SbVec2s {
52public:
53 SbVec2s(void) { }
54 SbVec2s(const short v[2]) { vec[0] = v[0]; vec[1] = v[1]; }
55 SbVec2s(short x, short y) { vec[0] = x; vec[1] = y; }
56 explicit SbVec2s(const SbVec2us & v) { setValue(v); }
57 explicit SbVec2s(const SbVec2b & v) { setValue(v); }
58 explicit SbVec2s(const SbVec2i32 & v) { setValue(v); }
59 explicit SbVec2s(const SbVec2f & v) { setValue(v); }
60 explicit SbVec2s(const SbVec2d & v) { setValue(v); }
61
62 SbVec2s & setValue(const short v[2]) { vec[0] = v[0]; vec[1] = v[1]; return *this; }
63 SbVec2s & setValue(short x, short y) { vec[0] = x; vec[1] = y; return *this; }
64 SbVec2s & setValue(const SbVec2us & v);
65 SbVec2s & setValue(const SbVec2b & v);
66 SbVec2s & setValue(const SbVec2i32 & v);
67 SbVec2s & setValue(const SbVec2f & v);
68 SbVec2s & setValue(const SbVec2d & v);
69
70 const short * getValue(void) const { return vec; }
71 void getValue(short & x, short & y) const { x = vec[0]; y = vec[1]; }
72
73 short & operator [] (int i) { return vec[i]; }
74 const short & operator [] (int i) const { return vec[i]; }
75
76 int32_t dot(SbVec2s v) const { return vec[0] * v[0] + vec[1] * v[1]; }
77 void negate(void) { vec[0] = -vec[0]; vec[1] = -vec[1]; }
78
79 SbVec2s & operator *= (int d) { vec[0] = short(vec[0] * d); vec[1] = short(vec[1] * d); return *this; }
80 SbVec2s & operator *= (double d);
81 SbVec2s & operator /= (int d) { SbDividerChk("SbVec2s::operator/=(int)", d); vec[0] = short(vec[0] / d); vec[1] = short(vec[1] / d); return *this; }
82 SbVec2s & operator /= (double d) { SbDividerChk("SbVec2s::operator/=(double)", d); return operator *= (1.0 / d); }
83 SbVec2s & operator += (SbVec2s v) { vec[0] += v[0]; vec[1] += v[1]; return *this; }
84 SbVec2s & operator -= (SbVec2s v) { vec[0] -= v[0]; vec[1] -= v[1]; return *this; }
85 SbVec2s operator - (void) const { return SbVec2s(-vec[0], -vec[1]); }
86
87 SbString toString() const;
88 SbBool fromString(const SbString & str);
89
90 void print(FILE * fp) const;
91
92protected:
93 short vec[2];
94
95}; // SbVec2s
96
97COIN_DLL_API inline SbVec2s operator * (SbVec2s v, int d) {
98 SbVec2s val(v); val *= d; return val;
99}
100
101COIN_DLL_API inline SbVec2s operator * (SbVec2s v, double d) {
102 SbVec2s val(v); val *= d; return val;
103}
104
105COIN_DLL_API inline SbVec2s operator * (int d, SbVec2s v) {
106 SbVec2s val(v); val *= d; return val;
107}
108
109COIN_DLL_API inline SbVec2s operator * (double d, SbVec2s v) {
110 SbVec2s val(v); val *= d; return val;
111}
112
113COIN_DLL_API inline SbVec2s operator / (SbVec2s v, int d) {
114 SbDividerChk("operator/(SbVec2s,int)", d);
115 SbVec2s val(v); val /= d; return val;
116}
117
118COIN_DLL_API inline SbVec2s operator / (SbVec2s v, double d) {
119 SbDividerChk("operator/(SbVec2s,double)", d);
120 SbVec2s val(v); val /= d; return val;
121}
122
123COIN_DLL_API inline SbVec2s operator + (SbVec2s v1, SbVec2s v2) {
124 SbVec2s v(v1); v += v2; return v;
125}
126
127COIN_DLL_API inline SbVec2s operator - (SbVec2s v1, SbVec2s v2) {
128 SbVec2s v(v1); v -= v2; return v;
129}
130
131COIN_DLL_API inline int operator == (SbVec2s v1, SbVec2s v2) {
132 return ((v1[0] == v2[0]) && (v1[1] == v2[1]));
133}
134
135COIN_DLL_API inline int operator != (SbVec2s v1, SbVec2s v2) {
136 return !(v1 == v2);
137}
138
139#endif // !COIN_SBVEC2S_H
SbString
The SbString class is a string class with convenience functions for string operations.
Definition SbString.h:52
SbVec2b
a vector class for containing two byte integers.
Definition SbVec2b.h:48
SbVec2d
The SbVec2d class is a 2 dimensional vector with double precision floating point coordinates.
Definition SbVec2d.h:48
SbVec2f
The SbVec2f class is a 2 dimensional vector with floating point coordinates.
Definition SbVec2f.h:49
SbVec2i32
The SbVec2i32 class is a 2 dimensional vector with 32-bit signed integer coordinates.
Definition SbVec2i32.h:50
SbVec2s
The SbVec2s class is a 2 dimensional vector with short integer coordinates.
Definition SbVec2s.h:51
SbVec2s::SbVec2s
SbVec2s(short x, short y)
Definition SbVec2s.h:55
SbVec2s::SbVec2s
SbVec2s(const SbVec2b &v)
Definition SbVec2s.h:57
SbVec2s::getValue
const short * getValue(void) const
Definition SbVec2s.h:70
SbVec2s::getValue
void getValue(short &x, short &y) const
Definition SbVec2s.h:71
SbVec2s::SbVec2s
SbVec2s(const SbVec2i32 &v)
Definition SbVec2s.h:58
SbVec2s::setValue
SbVec2s & setValue(const short v[2])
Definition SbVec2s.h:62
SbVec2s::setValue
SbVec2s & setValue(short x, short y)
Definition SbVec2s.h:63
SbVec2s::SbVec2s
SbVec2s(const short v[2])
Definition SbVec2s.h:54
SbVec2s::SbVec2s
SbVec2s(const SbVec2f &v)
Definition SbVec2s.h:59
SbVec2s::SbVec2s
SbVec2s(void)
Definition SbVec2s.h:53
SbVec2s::SbVec2s
SbVec2s(const SbVec2d &v)
Definition SbVec2s.h:60
SbVec2s::negate
void negate(void)
Definition SbVec2s.h:77
SbVec2s::dot
int32_t dot(SbVec2s v) const
Definition SbVec2s.h:76
SbVec2s::SbVec2s
SbVec2s(const SbVec2us &v)
Definition SbVec2s.h:56
SbVec2us
Definition SbVec2us.h:46