COVISE Core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
coMatrix.h
Go to the documentation of this file.
1 /* This file is part of COVISE.
2 
3  You can use it under the terms of the GNU Lesser General Public License
4  version 2.1 or later, see lgpl-2.1.txt.
5 
6  * License: LGPL 2+ */
7 
8 #ifndef COVISE_MATRIX_H
9 #define COVISE_MATRIX_H
10 
11 /**************************************************************************\
12  ** **
13  ** **
14  ** Description: Interface classes for application modules to the COVISE **
15  ** software environment **
16  ** **
17  ** **
18  ** **
19  ** **
20  ** **
21  ** (C)1997 RUS **
22  ** Computing Center University of Stuttgart **
23  ** Allmandring 30 **
24  ** 70550 Stuttgart **
25  ** Author: D. Rantzau **
26  ** Date: 15.08.97 V1.0 **
27 \**************************************************************************/
28 
29 #include "coExport.h"
30 #include "coVector.h"
31 
32 namespace covise
33 {
34 
36 {
37 
38  friend class coVector;
39 
40  double val[4][4];
41 
42 public:
43  int changed;
44 
45  // this is a flag for optimization
46  // if 0 ->matrix is unity matrix
47  //coMatrix() { unity(); };
49  {
50  for (int i = 0; i < 4; i++)
51  for (int j = 0; j < 4; j++)
52  val[i][j] = 0.0;
53  };
54 
55  coMatrix(double *m)
56  {
57  int i, j;
58  for (i = 0; i < 4; i++)
59  for (j = 0; j < 4; j++)
60  val[i][j] = m[i * 4 + j];
61  };
62 
64  {
65  int i, j;
66  for (i = 0; i < 4; i++)
67  for (j = 0; j < 4; j++)
68  val[i][j] = m.val[i][j];
69  };
70 
71  ~coMatrix(){};
72 
73  //
74  // Matrix operators:
75  //
77  {
78  int i, j;
79  for (i = 0; i < 4; i++)
80  for (j = 0; j < 4; j++)
81  val[i][j] = m.val[i][j];
82  return *this;
83  };
84 
85  int operator==(const coMatrix &) const;
86  double &operator()(int i, int j)
87  {
88  return val[i][j];
89  };
90  coMatrix operator+(const coMatrix &) const;
91  coMatrix operator-(const coMatrix &) const;
92  coMatrix operator*(const coMatrix &) const;
93  coVector operator*(const coVector &) const;
94  coMatrix operator*(const double &) const;
95 
96  void set(int i, int j, double d)
97  {
98  val[i][j] = d;
99  changed = 1;
100  };
101 
102  double get(int i, int j) const
103  {
104  return val[i][j];
105  };
106 
107  coMatrix invers() const;
108  coMatrix transpose() const;
109  void fromQuat(const float, const float, const float, const float);
110 
111  //
112  // Matrix transformations
113  //
114 
115  coMatrix scaleS(const double &d) const
116  {
117  coMatrix m = *this * d;
118  m.changed = 1;
119  return m;
120  };
121 
122  coMatrix invScaleS(const double &d)
123  {
124  coMatrix m = *this * -d;
125  m.changed = 1;
126  return m;
127  };
128 
129  coMatrix scale(const coVector &v) const
130  {
131  coMatrix m1, m2;
132  m1.set(0, 0, v.vec[0]);
133  m1.set(1, 1, v.vec[1]);
134  m1.set(2, 2, v.vec[2]);
135  m2 = *this * m1;
136  m2.changed = 1;
137  return m2;
138  };
139 
140  coMatrix invScale(const coVector &v) const
141  {
142  coMatrix m1, m2;
143  m1.set(0, 0, 1 / v.vec[0]);
144  m1.set(1, 1, 1 / v.vec[1]);
145  m1.set(2, 2, 1 / v.vec[2]);
146  m2 = m1 * *this;
147  m2.changed = 1;
148  return m2;
149  };
150 
151  coMatrix translation(const coVector &) const;
152  coMatrix invTranslation(const coVector &) const;
153 
154  coMatrix rotationX(const double) const;
155  coMatrix invRotationX(const double) const;
156 
157  coMatrix rotationY(const double) const;
158  coMatrix invRotationY(const double) const;
159 
160  coMatrix rotationZ(const double) const;
161  coMatrix invRotationZ(const double) const;
162 
163  coMatrix rotation(const coVector &v) const
164  {
165  coMatrix m1, m2;
166  double sinx = sin(v.vec[0]);
167  double siny = sin(v.vec[1]);
168  double sinz = sin(v.vec[2]);
169 
170  double cosx = cos(v.vec[0]);
171  double cosy = cos(v.vec[1]);
172  double cosz = cos(v.vec[2]);
173 
174  m1.val[0][0] = cosy * cosz;
175  m1.val[0][1] = cosy * sinz;
176  m1.val[0][2] = -siny;
177 
178  m1.val[1][0] = cosz * sinx * siny - cosx * sinz;
179  m1.val[1][1] = cosx * cosz + sinx * siny * sinz;
180  m1.val[1][2] = cosy * sinx;
181 
182  m1.val[2][0] = cosx * cosz * siny + sinx * sinz;
183  m1.val[2][1] = -(cosz * sinx) + cosx * siny * sinz;
184  m1.val[2][2] = cosx * cosy;
185 
186  m2 = *this * m1;
187  m2.changed = 1;
188  return m2;
189  };
190 
192  {
193  coMatrix m1, m2;
194  double sinx = sin(-v.vec[0]);
195  double siny = sin(-v.vec[1]);
196  double sinz = sin(-v.vec[2]);
197 
198  double cosx = cos(-v.vec[0]);
199  double cosy = cos(-v.vec[1]);
200  double cosz = cos(-v.vec[2]);
201 
202  m1.val[0][0] = cosy * cosz;
203  m1.val[0][1] = cosz * sinx * siny + cosx * sinz;
204  m1.val[0][2] = -(cosx * cosz * siny) + sinx * sinz;
205 
206  m1.val[1][0] = -(cosy * sinz);
207  m1.val[1][1] = cosx * cosz - sinx * siny * sinz;
208  m1.val[1][2] = cosz * sinx + cosx * siny * sinz;
209 
210  m1.val[2][0] = siny;
211  m1.val[2][1] = -(cosy * sinx);
212  m1.val[2][2] = cosx * cosy;
213 
214  m2 = m1 * *this;
215  m2.changed = 1;
216  return m2;
217  };
218 
219  coMatrix &setRotation(const coVector &axis, double angle);
220 
221  void get(float f[4][4])
222  {
223  for (int i = 0; i < 4; i++)
224  for (int j = 0; j < 4; j++)
225  f[i][j] = (float)val[i][j];
226  };
227 
228  // print the matrix:
229  void print(FILE *f, char *n = 0)
230  {
231  fprintf(f, "%s{ %f %f %f %f ", n ? n : "", val[0][0], val[0][1], val[0][2], val[0][3]);
232  fprintf(f, "%f %f %f %f ", val[1][0], val[1][1], val[1][2], val[1][3]);
233  fprintf(f, "%f %f %f %f ", val[2][0], val[2][1], val[2][2], val[2][3]);
234  fprintf(f, "%f %f %f %f } ", val[3][0], val[3][1], val[3][2], val[3][3]);
235  };
236 
237  void unity()
238  {
239  changed = 0;
240  val[0][1] = val[0][2] = val[0][3] = val[1][0] = val[1][2] = val[1][3] = val[2][0] = val[2][1] = val[2][3] = val[3][0] = val[3][1] = val[3][2] = 0;
241  val[0][0] = val[1][1] = val[2][2] = val[3][3] = 1;
242  };
243 
244  friend ostream &operator<<(ostream &O, const coMatrix &m)
245  {
246  O << m.val[0][0] << " " << m.val[0][1] << " " << m.val[0][2] << " " << m.val[0][3] << ","
247  << m.val[1][0] << " " << m.val[1][1] << " " << m.val[1][2] << " " << m.val[1][3] << ","
248  << m.val[2][0] << " " << m.val[2][1] << " " << m.val[2][2] << " " << m.val[2][3] << ","
249  << m.val[3][0] << " " << m.val[3][1] << " " << m.val[3][2] << " " << m.val[3][3];
250  return O;
251  };
252 };
253 }
254 #endif // COVISE_MATRIX_H
coMatrix(const coMatrix &m)
Definition: coMatrix.h:63
coMatrix invScaleS(const double &d)
Definition: coMatrix.h:122
friend ostream & operator<<(ostream &O, const coMatrix &m)
Definition: coMatrix.h:244
int changed
Definition: coMatrix.h:43
__host__ __device__ float2 operator*(float2 a, float2 b)
Definition: cutil_math.h:736
coMatrix(double *m)
Definition: coMatrix.h:55
GLdouble n
Definition: khronos-glext.h:8447
double & operator()(int i, int j)
Definition: coMatrix.h:86
__host__ __device__ float2 operator+(float2 a, float2 b)
Definition: cutil_math.h:281
coMatrix invScale(const coVector &v) const
Definition: coMatrix.h:140
double * vec
Definition: coVector.h:49
coMatrix invRotation(const coVector &v) const
Definition: coMatrix.h:191
Definition: coMatrix.h:35
~coMatrix()
Definition: coMatrix.h:71
GLuint GLfloat * val
Definition: khronos-glext.h:7898
coMatrix & operator=(const coMatrix &m)
Definition: coMatrix.h:76
GLsizei GLboolean transpose
Definition: khronos-glext.h:6768
const GLdouble * v
Definition: khronos-glext.h:6442
coMatrix scaleS(const double &d) const
Definition: coMatrix.h:115
Definition: coVector.h:41
coMatrix scale(const coVector &v) const
Definition: coMatrix.h:129
INLINE bool operator==(const coObjID &a, const coObjID &b)
Definition: coObjID.h:166
void print(FILE *f, char *n=0)
Definition: coMatrix.h:229
double val[4][4]
Definition: coMatrix.h:40
void set(int i, int j, double d)
Definition: coMatrix.h:96
const GLfloat * m
Definition: khronos-glext.h:12117
coMatrix()
Definition: coMatrix.h:48
__host__ __device__ float2 operator-(float2 &a)
Definition: cutil_math.h:252
GLfloat angle
Definition: khronos-glext.h:12122
coMatrix rotation(const coVector &v) const
Definition: coMatrix.h:163
GLfloat f
Definition: khronos-glext.h:8258
#define UTILEXPORT
Definition: coExport.h:182
void unity()
Definition: coMatrix.h:237