1 #ifndef COIN_SBVEC3F_H
2 #define COIN_SBVEC3F_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/SbByteBuffer.h>
40 #include <Inventor/SbString.h>
41 #ifndef NDEBUG
42 #include <Inventor/errors/SoDebugError.h>
43 #endif // !NDEBUG
44 
45 class SbPlane;
46 class SbVec3d;
47 class SbVec3b;
48 class SbVec3s;
49 class SbVec3i32;
50 
51 class COIN_DLL_API SbVec3f {
52 public:
SbVec3f(void)53   SbVec3f(void) { }
SbVec3f(const float v[3])54   SbVec3f(const float v[3]) { vec[0] = v[0]; vec[1] = v[1]; vec[2] = v[2]; }
SbVec3f(float x,float y,float z)55   SbVec3f(float x, float y, float z) { vec[0] = x; vec[1] = y; vec[2] = z; }
SbVec3f(const SbVec3d & v)56   explicit SbVec3f(const SbVec3d & v) { setValue(v); }
SbVec3f(const SbVec3b & v)57   explicit SbVec3f(const SbVec3b & v) { setValue(v); }
SbVec3f(const SbVec3s & v)58   explicit SbVec3f(const SbVec3s & v) { setValue(v); }
SbVec3f(const SbVec3i32 & v)59   explicit SbVec3f(const SbVec3i32 & v) { setValue(v); }
60   SbVec3f(const SbPlane & p0, const SbPlane & p1, const SbPlane & p2);
61 
setValue(const float v[3])62   SbVec3f & setValue(const float v[3]) { vec[0] = v[0]; vec[1] = v[1]; vec[2] = v[2]; return *this; }
setValue(float x,float y,float z)63   SbVec3f & setValue(float x, float y, float z) { vec[0] = x; vec[1] = y; vec[2] = z; return *this; }
64   SbVec3f & setValue(const SbVec3f & barycentric,
65                      const SbVec3f & v0,
66                      const SbVec3f & v1,
67                      const SbVec3f & v2);
68   SbVec3f & setValue(const SbVec3d & v);
69   SbVec3f & setValue(const SbVec3b & v);
70   SbVec3f & setValue(const SbVec3s & v);
71   SbVec3f & setValue(const SbVec3i32 & v);
72 
getValue(void)73   const float * getValue(void) const { return vec; }
getValue(float & x,float & y,float & z)74   void getValue(float & x, float & y, float & z) const { x = vec[0]; y = vec[1]; z = vec[2]; }
75 
76   float & operator [] (int i) { return vec[i]; }
77   const float & operator [] (int i) const { return vec[i]; }
78 
79   SbBool equals(const SbVec3f & v, float tolerance) const;
80   SbVec3f cross(const SbVec3f & v) const;
dot(const SbVec3f & v)81   float dot(const SbVec3f & v) const { return vec[0] * v[0] + vec[1] * v[1] + vec[2] * v[2]; }
82   SbVec3f getClosestAxis(void) const;
83   float length(void) const;
sqrLength(void)84   float sqrLength(void) const { return vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]; }
85   float normalize(void);
negate(void)86   void negate(void) { vec[0] = -vec[0]; vec[1] = -vec[1]; vec[2] = -vec[2]; }
87 
88   SbVec3f & operator *= (float d) { vec[0] *= d; vec[1] *= d; vec[2] *= d; return *this; }
89   SbVec3f & operator /= (float d) { SbDividerChk("SbVec3f::operator/=(float)", d); return operator *= (1.0f / d); }
90   SbVec3f & operator += (const SbVec3f & v) { vec[0] += v[0]; vec[1] += v[1]; vec[2] += v[2]; return *this; }
91   SbVec3f & operator -= (const SbVec3f & v) { vec[0] -= v[0]; vec[1] -= v[1]; vec[2] -= v[2]; return *this; }
92   SbVec3f operator - (void) const { return SbVec3f(-vec[0], -vec[1], -vec[2]); }
93 
94   SbString toString() const;
95   SbBool fromString(const SbString & str);
96 
97   void print(FILE * fp) const;
98 
99 protected:
100   float vec[3];
101 
102 }; // SbVec3f
103 
104 COIN_DLL_API inline SbVec3f operator * (const SbVec3f & v, float d) {
105   SbVec3f val(v); val *= d; return val;
106 }
107 
108 COIN_DLL_API inline SbVec3f operator * (float d, const SbVec3f & v) {
109   SbVec3f val(v); val *= d; return val;
110 }
111 
112 COIN_DLL_API inline SbVec3f operator / (const SbVec3f & v, float d) {
113   SbDividerChk("operator/(SbVec3f,float)", d);
114   SbVec3f val(v); val /= d; return val;
115 }
116 
117 COIN_DLL_API inline SbVec3f operator + (const SbVec3f & v1, const SbVec3f & v2) {
118   SbVec3f v(v1); v += v2; return v;
119 }
120 
121 COIN_DLL_API inline SbVec3f operator - (const SbVec3f & v1, const SbVec3f & v2) {
122   SbVec3f v(v1); v -= v2; return v;
123 }
124 
125 COIN_DLL_API inline int operator == (const SbVec3f & v1, const SbVec3f & v2) {
126   return ((v1[0] == v2[0]) && (v1[1] == v2[1]) && (v1[2] == v2[2]));
127 }
128 
129 COIN_DLL_API inline int operator != (const SbVec3f & v1, const SbVec3f & v2) {
130   return !(v1 == v2);
131 }
132 
133 #endif // !COIN_SBVEC3F_H
134