Coin Logo Coin3D is Free Software,
published under the BSD 3-clause license.
https://bitbucket.org/Coin3D/
http://www.kongsberg.com/kogt/
Linear.h
1 /**************************************************************************\
2  * Copyright (c) Kongsberg Oil & Gas Technologies AS
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 \**************************************************************************/
32 
33 #ifndef DIME_LINEAR_H
34 #define DIME_LINEAR_H
35 
36 #include <dime/Basic.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <math.h>
40 
41 class DIME_DLL_API dimeVec2f
42 {
43 public:
44  dimeVec2f() {}
45  dimeVec2f(const dimeVec2f &vec) {x = vec.x; y = vec.y;}
46  dimeVec2f(dxfdouble _x, dxfdouble _y) {x = _x; y = _y;}
47  void setValue(const dxfdouble _x, const dxfdouble _y) {x = _x; y = _y;}
48 
49  //dxfdouble operator [] (const int i) const
50  //{ return (i==0?x:y); }
51  // dxfdouble & operator [] (const int i)
52  //{ return(i==0?x:y);}
53  void print() const { printf("Coord2: (%.3f, %.3f)\n", x,y);}
54  void print(const char *s) {printf("%s: (%.3f, %.3f)\n", s,x,y);}
55  dxfdouble x,y;
56 
57 }; // class dimeVec2f
58 
59 typedef dimeVec2f dimeVec2d;
60 
61 class DIME_DLL_API dimeVec3f
62 {
63 public:
64  dxfdouble x, y, z;
65 
66  dimeVec3f(void) {};
67  dimeVec3f(const dxfdouble X, const dxfdouble Y, const dxfdouble Z)
68  { x=X; y=Y; z=Z; };
69  dimeVec3f(const dxfdouble *xyz)
70  { x = xyz[0]; y = xyz[1]; z = xyz[2]; }
71  dimeVec3f (const dimeVec3f& v)
72  { x=v.x; y=v.y; z=v.z; };
73  dimeVec3f cross(const dimeVec3f &v) const
74  { return dimeVec3f(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x); }
75  dxfdouble dot(const dimeVec3f &v) const
76  { return x*v.x+y*v.y+z*v.z; }
77 
78  bool equals(const dimeVec3f &v)
79  { return (x == v.x && y == v.y && z == v.z); }
80  bool equals(const dimeVec3f &v, dxfdouble tol)
81  { return (fabs(x-v.x) <= tol && fabs(y-v.y) <= tol && fabs(z-v.z) <= tol); }
82 
83  operator dxfdouble *() { return &x; }
84  const dxfdouble *getValue() const { return &x; }
85  void getValue(dxfdouble &_x, dxfdouble &_y, dxfdouble &_z) const
86  { _x = x; _y = y; _z = z;}
87  dxfdouble length() const
88  { return (dxfdouble) sqrt(x*x+y*y+z*z); }
89  dxfdouble sqrLength(void) const
90  { return x*x+y*y+z*z; }
91  void negate(void)
92  { x = -x; y = -y; z = -z; }
93  void setValue(const dxfdouble *v)
94  { x = v[0]; y = v[1]; z = v[2]; }
95  void setValue(const dxfdouble X, const dxfdouble Y, const dxfdouble Z)
96  { x=X; y=Y; z=Z; }
97 
98  dxfdouble operator [] (const int i) const
99  { return( (i==0)?x:((i==1)?y:z) ); };
100  dxfdouble & operator [] (const int i)
101  { return( (i==0)?x:((i==1)?y:z) ); };
102 
103  dimeVec3f &operator *= (const dxfdouble s)
104  { x*=s; y*=s; z*=s; return *this; }
105  dimeVec3f &operator /= (const dxfdouble s)
106  { x/=s; y/=s; z/=s; return *this; }
107  dimeVec3f &operator += (const dimeVec3f &v)
108  { x+=v.x; y+=v.y; z+=v.z; return *this; }
109  dimeVec3f &operator -= (const dimeVec3f &v)
110  { x-=v.x; y-=v.y; z-=v.z; return *this;}
111  dimeVec3f operator -() const
112  { return dimeVec3f(-x, -y, -z); }
113  friend dimeVec3f operator *(const dimeVec3f &v, dxfdouble s)
114  { return dimeVec3f(v.x*s, v.y*s, v.z*s); }
115  friend dimeVec3f operator *(dxfdouble s, const dimeVec3f &v)
116  { return dimeVec3f(v.x*s, v.y*s, v.z*s); }
117  friend dimeVec3f operator / (const dimeVec3f &v, dxfdouble s)
118  { return dimeVec3f(v.x/s, v.y/s, v.z/s); }
119  friend dimeVec3f operator + (const dimeVec3f &v1, const dimeVec3f &v2)
120  { return dimeVec3f(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
121  friend dimeVec3f operator - (const dimeVec3f &v1, const dimeVec3f &v2)
122  { return dimeVec3f(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
123 
124  friend bool operator ==(const dimeVec3f &v1, const dimeVec3f &v2)
125  { return (v1.x == v2.x && v1.y == v2.y && v1.z == v2.z); }
126  friend bool operator !=(const dimeVec3f &v1, const dimeVec3f &v2)
127  { return (v1.x != v2.x || v1.y != v2.y || v1.z != v2.z); }
128 
129  dimeVec3f& operator = (const dimeVec3f &v) // extra
130  { x=v.x; y=v.y; z=v.z; return *this; }
131 
132  void multMatrix(dxfdouble *matrix) // extra
133  {
134  dxfdouble tx, ty, tz;
135  tx = ty = tz = 0.0f;
136  tx = matrix[0]*x+matrix[1]*y+matrix[2]*z;
137  ty = matrix[4]*x+matrix[5]*y+matrix[6]*z;
138  tz = matrix[8]*x+matrix[9]*y+matrix[10]*z;
139  x = tx, y = ty, z = tz;
140  }
141 
142  void print() const // extra
143  { printf("dimeVec3f: (%.3f, %.3f, %.3f)\n",x, y, z); }
144  void print(const char *s) const
145  { printf("%s: (%.3f, %.3f, %.3f)\n",s, x, y, z); }
146 
147 
148  dimeVec3f multComponents(const dimeVec3f &v) const
149  { return dimeVec3f(x*v.x, y*v.y, z*v.z); }
150 
151  dxfdouble angle(const dimeVec3f &v2);
152  void normalize();
153 
154 }; // class dimeVec3f
155 
156 typedef dimeVec3f dimeVec3d;
157 
158 class DIME_DLL_API dimeMatrix
159 {
160 public:
161  dimeMatrix() {}
162  dimeMatrix(const dimeMatrix &matrix);
163  // Constructor given all 16 elements in row-major order
164  dimeMatrix(dxfdouble a11, dxfdouble a12, dxfdouble a13, dxfdouble a14,
165  dxfdouble a21, dxfdouble a22, dxfdouble a23, dxfdouble a24,
166  dxfdouble a31, dxfdouble a32, dxfdouble a33, dxfdouble a34,
167  dxfdouble a41, dxfdouble a42, dxfdouble a43, dxfdouble a44);
168  void transpose();
169  void makeIdentity();
170  bool isIdentity() const;
171  void setTransform(const dimeVec3f &translation,
172  const dimeVec3f &scalefactor,
173  const dimeVec3f &rotAngles);
174  dimeMatrix &multRight(const dimeMatrix &m); // this = this * m
175  dimeMatrix &multLeft(const dimeMatrix &m); // this = m * this
176 
177  // Sets matrix to rotate by given rotation
178  void setRotate(const dimeVec3f &rot);
179 
180  void setRotate(const dimeVec3f &x, const dimeVec3f &y, const dimeVec3f &z);
181 
182  // sets matrix to rotate around given vector
183  void setRotation(const dimeVec3f &u, const dxfdouble angle);
184 
185  // Sets matrix to scale by given uniform factor
186  void setScale(const dxfdouble s);
187 
188  // Sets matrix to scale by given vector
189  void setScale(const dimeVec3f &s);
190 
191  // Sets matrix to translate by given vector
192  void setTranslate(const dimeVec3f &t);
193 
194  // Multiplies matrix by given column vector, giving vector result
195  void multMatrixVec(const dimeVec3f &src, dimeVec3f &dst) const;
196 
197  // transforms vector
198  void multMatrixVec(dimeVec3f &vec) const;
199 
200  // Multiplies given row vector by matrix, giving vector result
201  //void multVecMatrix(const dimeVec3f &src, dimeVec3f &dst) const;
202 
203  // Cast: returns pointer to storage of first element
204  operator dxfdouble *() { return &matrix[0][0]; }
205 
206  // Make it look like a usual matrix (so you can do m[3][2])
207  dxfdouble *operator [](int i) { return &matrix[i][0]; }
208  const dxfdouble * operator [](int i) const { return &matrix[i][0];}
209 
210  dimeMatrix &operator =(const dimeMatrix &m);
211 
212  // Performs right multiplication with another matrix
213  dimeMatrix &operator *=(const dimeMatrix &m) { return multRight(m); }
214 
215 
216  static dimeMatrix identity();
217  bool inverse();
218  bool inverse2();
219  dxfdouble determinant(const int i=-2, const int j=-1);
220 
221  void operator *=(const dxfdouble val);
222 
223 private:
224  dxfdouble matrix[4][4];
225 
226 }; // class dimeMatrix
227 
228 #endif // ! DIME_LINEAR_H
229 
The dimeVec2f class is for containing and operating on a 2D vector / coordinate.
Definition: Linear.h:41
The dimeVec3f class is for containing and operating on a 3D vector / coordinate.
Definition: Linear.h:61
The dimeMatrix class is for containing and operating on a four-by-four matrix.
Definition: Linear.h:158