SmallChange  1.0.0
A collection of extensions to Coin3D
Loading...
Searching...
No Matches
SbVec3.h
1#ifndef COIN_SBVEC3_H
2#define COIN_SBVEC3_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 <cassert>
37#include <cstddef>
38
39template <class Type>
40class SbVec3 {
41public:
42 SbVec3(void) { }
43
44 SbVec3(const Type v[3]) {
45 this->vec[0] = v[0]; this->vec[1] = v[1]; this->vec[2] = v[2];
46 }
47
48 SbVec3(const Type x, const Type y, const Type z) {
49 this->vec[0] = x; this->vec[1] = y; this->vec[2] = z;
50 }
51
52 SbVec3<Type> & setValue(const Type v[3]) {
53 this->vec[0] = v[0]; this->vec[1] = v[1]; this->vec[2] = v[2];
54 return *this;
55 }
56
57 SbVec3<Type> & setValue(const Type x, const Type y, const Type z) {
58 this->vec[0] = x; this->vec[1] = y; this->vec[2] = z;
59 return *this;
60 }
61
62 const Type * getValue(void) const {
63 return this->vec;
64 }
65
66 void getValue(Type & x, Type & y, Type & z) const {
67 x = this->vec[0]; y = this->vec[1]; z = this->vec[2];
68 }
69
70 SbVec3<Type> cross(const SbVec3<Type> & v) const {
71 return SbVec3<Type>(this->vec[1]*v.vec[2] - this->vec[2]*v.vec[1],
72 this->vec[2]*v.vec[0] - this->vec[0]*v.vec[2],
73 this->vec[0]*v.vec[1] - this->vec[1]*v.vec[0]);
74 }
75
76 Type dot(const SbVec3<Type> & v) const {
77 return this->vec[0]*v.vec[0] + this->vec[1]*v.vec[1] + this->vec[2]*v.vec[2];
78 }
79
80 Type sqrLength(void) const {
81 return this->vec[0]*this->vec[0] + this->vec[1]*this->vec[1] + this->vec[2]*this->vec[2];
82 }
83
84 double length(void) const {
85 return sqrt(this->sqrLength());
86 }
87
88 double normalize(void) {
89 const double len = this->length();
90 if ( len != 0.0 ) {
91 operator /= ((Type) len);
92 }
93 return len;
94 }
95
96 void negate(void) {
97 this->vec[0] = -this->vec[0];
98 this->vec[1] = -this->vec[1];
99 this->vec[2] = -this->vec[2];
100 }
101
102 SbVec3<Type> & operator *= (const Type d) {
103 this->vec[0] *= d;
104 this->vec[1] *= d;
105 this->vec[2] *= d;
106 return *this;
107 }
108
109 SbVec3<Type> & operator /= (const Type d) {
110 return operator *= (((Type) 1.0) / d);
111 }
112
113 SbVec3<Type> & operator += (const SbVec3<Type> & u) {
114 this->vec[0] += u.vec[0];
115 this->vec[1] += u.vec[1];
116 this->vec[2] += u.vec[2];
117 return *this;
118 }
119 SbVec3<Type> & operator -= (const SbVec3<Type> & u) {
120 this->vec[0] -= u.vec[0];
121 this->vec[1] -= u.vec[1];
122 this->vec[2] -= u.vec[2];
123 return *this;
124 }
125
126 SbVec3<Type> operator - (void) const {
127 return SbVec3<Type>(-this->vec[0], -this->vec[1], -this->vec[2]);
128 }
129
130 SbVec3<Type> operator + (const SbVec3<Type> & v) const {
131 return SbVec3<Type>(this->vec[0] + v.vec[0], this->vec[1] + v.vec[1], this->vec[2] + v.vec[2]);
132 }
133
134 SbVec3<Type> operator - (const SbVec3<Type> & v) const {
135 return SbVec3<Type>(this->vec[0] - v.vec[0], this->vec[1] - v.vec[1], this->vec[2] - v.vec[2]);
136 }
137
138 SbVec3<Type> operator * (Type f) const {
139 return SbVec3<Type>(this->vec[0] * f, this->vec[1] * f, this->vec[2] * f);
140 }
141
142 SbVec3<Type> operator / (Type f) const {
143 return SbVec3<Type>(this->vec[0] / f, this->vec[1] / f, this->vec[2] / f);
144 }
145
146 Type & operator [] (int idx) {
147 return this->vec[idx];
148 }
149
150 const Type & operator [] (int idx) const {
151 return this->vec[idx];
152 }
153
154private:
155 Type vec[3];
156
157};
158
159#endif // !COIN_SBVEC3_H
Definition SbVec3.h:40