Linear.h
1 /**************************************************************************\
2  *
3  * FILE: Linear.h
4  *
5  * This source file is part of DIME.
6  * Copyright (C) 1998-1999 by Systems In Motion. All rights reserved.
7  *
8  * This library is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License, version 2, as
10  * published by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License (the accompanying file named COPYING) for more
16  * details.
17  *
18  **************************************************************************
19  *
20  * If you need DIME for a non-GPL project, contact Systems In Motion
21  * to acquire a Professional Edition License:
22  *
23  * Systems In Motion http://www.sim.no/
24  * Prof. Brochs gate 6 sales@sim.no
25  * N-7030 Trondheim Voice: +47 22114160
26  * NORWAY Fax: +47 67172912
27  *
28 \**************************************************************************/
29 
30 #ifndef DIME_LINEAR_H
31 #define DIME_LINEAR_H
32 
33 #include <dime/Basic.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <math.h>
37 
38 class DIME_DLL_API dimeVec2f
39 {
40 public:
41  dimeVec2f() {}
42  dimeVec2f(const dimeVec2f &vec) {x = vec.x; y = vec.y;}
43  dimeVec2f(dxfdouble _x, dxfdouble _y) {x = _x; y = _y;}
44  void setValue(const dxfdouble _x, const dxfdouble _y) {x = _x; y = _y;}
45 
46  //dxfdouble operator [] (const int i) const
47  //{ return (i==0?x:y); }
48  // dxfdouble & operator [] (const int i)
49  //{ return(i==0?x:y);}
50  void print() const { printf("Coord2: (%.3f, %.3f)\n", x,y);}
51  void print(const char *s) {printf("%s: (%.3f, %.3f)\n", s,x,y);}
52  dxfdouble x,y;
53 
54 }; // class dimeVec2f
55 
56 class DIME_DLL_API dimeVec3f
57 {
58 public:
59  dxfdouble x, y, z;
60 
61  dimeVec3f(void) {};
62  dimeVec3f(const dxfdouble X, const dxfdouble Y, const dxfdouble Z)
63  { x=X; y=Y; z=Z; };
64  dimeVec3f(const dxfdouble *xyz)
65  { x = xyz[0]; y = xyz[1]; z = xyz[2]; }
66  dimeVec3f (const dimeVec3f& v)
67  { x=v.x; y=v.y; z=v.z; };
68  dimeVec3f cross(const dimeVec3f &v) const
69  { return dimeVec3f(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x); }
70  dxfdouble dot(const dimeVec3f &v) const
71  { return x*v.x+y*v.y+z*v.z; }
72 
73  bool equals(const dimeVec3f &v)
74  { return (x == v.x && y == v.y && z == v.z); }
75  bool equals(const dimeVec3f &v, dxfdouble tol)
76  { return (fabs(x-v.x) <= tol && fabs(y-v.y) <= tol && fabs(z-v.z) <= tol); }
77 
78  operator dxfdouble *() { return &x; }
79  const dxfdouble *getValue() const { return &x; }
80  void getValue(dxfdouble &_x, dxfdouble &_y, dxfdouble &_z) const
81  { _x = x; _y = y; _z = z;}
82  dxfdouble length() const
83  { return (dxfdouble) sqrt(x*x+y*y+z*z); }
84  dxfdouble sqrLength(void) const
85  { return x*x+y*y+z*z; }
86  void negate(void)
87  { x = -x; y = -y; z = -z; }
88  void setValue(const dxfdouble *v)
89  { x = v[0]; y = v[1]; z = v[2]; }
90  void setValue(const dxfdouble X, const dxfdouble Y, const dxfdouble Z)
91  { x=X; y=Y; z=Z; }
92 
93  dxfdouble operator [] (const int i) const
94  { return( (i==0)?x:((i==1)?y:z) ); };
95  dxfdouble & operator [] (const int i)
96  { return( (i==0)?x:((i==1)?y:z) ); };
97 
98  dimeVec3f &operator *= (const dxfdouble s)
99  { x*=s; y*=s; z*=s; return *this; }
100  dimeVec3f &operator /= (const dxfdouble s)
101  { x/=s; y/=s; z/=s; return *this; }
102  dimeVec3f &operator += (const dimeVec3f &v)
103  { x+=v.x; y+=v.y; z+=v.z; return *this; }
104  dimeVec3f &operator -= (const dimeVec3f &v)
105  { x-=v.x; y-=v.y; z-=v.z; return *this;}
106  dimeVec3f operator -() const
107  { return dimeVec3f(-x, -y, -z); }
108  friend dimeVec3f operator *(const dimeVec3f &v, dxfdouble s)
109  { return dimeVec3f(v.x*s, v.y*s, v.z*s); }
110  friend dimeVec3f operator *(dxfdouble s, const dimeVec3f &v)
111  { return dimeVec3f(v.x*s, v.y*s, v.z*s); }
112  friend dimeVec3f operator / (const dimeVec3f &v, dxfdouble s)
113  { return dimeVec3f(v.x/s, v.y/s, v.z/s); }
114  friend dimeVec3f operator + (const dimeVec3f &v1, const dimeVec3f &v2)
115  { return dimeVec3f(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
116  friend dimeVec3f operator - (const dimeVec3f &v1, const dimeVec3f &v2)
117  { return dimeVec3f(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
118 
119  friend bool operator ==(const dimeVec3f &v1, const dimeVec3f &v2)
120  { return (v1.x == v2.x && v1.y == v2.y && v1.z == v2.z); }
121  friend bool operator !=(const dimeVec3f &v1, const dimeVec3f &v2)
122  { return (v1.x != v2.x || v1.y != v2.y || v1.z != v2.z); }
123 
124  dimeVec3f& operator = (const dimeVec3f &v) // extra
125  { x=v.x; y=v.y; z=v.z; return *this; }
126 
127  void multMatrix(dxfdouble *matrix) // extra
128  {
129  dxfdouble tx, ty, tz;
130  tx = ty = tz = 0.0f;
131  tx = matrix[0]*x+matrix[1]*y+matrix[2]*z;
132  ty = matrix[4]*x+matrix[5]*y+matrix[6]*z;
133  tz = matrix[8]*x+matrix[9]*y+matrix[10]*z;
134  x = tx, y = ty, z = tz;
135  }
136 
137  void print() const // extra
138  { printf("dimeVec3f: (%.3f, %.3f, %.3f)\n",x, y, z); }
139  void print(const char *s) const
140  { printf("%s: (%.3f, %.3f, %.3f)\n",s, x, y, z); }
141 
142 
143  dimeVec3f multComponents(const dimeVec3f &v) const
144  { return dimeVec3f(x*v.x, y*v.y, z*v.z); }
145 
146  dxfdouble angle(const dimeVec3f &v2);
147  void normalize();
148 
149 }; // class dimeVec3f
150 
151 class DIME_DLL_API dimeMatrix
152 {
153 public:
154  dimeMatrix() {}
155  dimeMatrix(const dimeMatrix &matrix);
156  // Constructor given all 16 elements in row-major order
157  dimeMatrix(dxfdouble a11, dxfdouble a12, dxfdouble a13, dxfdouble a14,
158  dxfdouble a21, dxfdouble a22, dxfdouble a23, dxfdouble a24,
159  dxfdouble a31, dxfdouble a32, dxfdouble a33, dxfdouble a34,
160  dxfdouble a41, dxfdouble a42, dxfdouble a43, dxfdouble a44);
161  void transpose();
162  void makeIdentity();
163  bool isIdentity() const;
164  void setTransform(const dimeVec3f &translation,
165  const dimeVec3f &scalefactor,
166  const dimeVec3f &rotAngles);
167  dimeMatrix &multRight(const dimeMatrix &m); // this = this * m
168  dimeMatrix &multLeft(const dimeMatrix &m); // this = m * this
169 
170  // Sets matrix to rotate by given rotation
171  void setRotate(const dimeVec3f &rot);
172 
173  void setRotate(const dimeVec3f &x, const dimeVec3f &y, const dimeVec3f &z);
174 
175  // sets matrix to rotate around given vector
176  void setRotation(const dimeVec3f &u, const dxfdouble angle);
177 
178  // Sets matrix to scale by given uniform factor
179  void setScale(const dxfdouble s);
180 
181  // Sets matrix to scale by given vector
182  void setScale(const dimeVec3f &s);
183 
184  // Sets matrix to translate by given vector
185  void setTranslate(const dimeVec3f &t);
186 
187  // Multiplies matrix by given column vector, giving vector result
188  void multMatrixVec(const dimeVec3f &src, dimeVec3f &dst) const;
189 
190  // transforms vector
191  void multMatrixVec(dimeVec3f &vec) const;
192 
193  // Multiplies given row vector by matrix, giving vector result
194  //void multVecMatrix(const dimeVec3f &src, dimeVec3f &dst) const;
195 
196  // Cast: returns pointer to storage of first element
197  operator dxfdouble *() { return &matrix[0][0]; }
198 
199  // Make it look like a usual matrix (so you can do m[3][2])
200  dxfdouble *operator [](int i) { return &matrix[i][0]; }
201  const dxfdouble * operator [](int i) const { return &matrix[i][0];}
202 
203  dimeMatrix &operator =(const dimeMatrix &m);
204 
205  // Performs right multiplication with another matrix
206  dimeMatrix &operator *=(const dimeMatrix &m) { return multRight(m); }
207 
208 
209  static dimeMatrix identity();
210  bool inverse();
211  bool inverse2();
212  dxfdouble determinant(const int i=-2, const int j=-1);
213 
214  void operator *=(const dxfdouble val);
215 
216 private:
217  dxfdouble matrix[4][4];
218 
219 }; // class dimeMatrix
220 
221 #endif // ! DIME_LINEAR_H
222 

Copyright © 1998-1999, Systems In Motion <sales@sim.no>. All rights reserved.
System documentation was generated using doxygen.