• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kstars

  • sources
  • kde-4.12
  • kdeedu
  • kstars
  • kstars
  • ekos
  • guide
matr.cpp
Go to the documentation of this file.
1 /* Ekos guide tool
2  Copyright (C) 2012 Andrew Stepanenko
3 
4  Modified by Jasem Mutlaq <mutlaqja@ikarustech.com> for KStars.
5 
6  This application is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10  */
11 
12 //---------------------------------------------------------------------------
13 #include "matr.h"
14 
15 #include <math.h>
16 #include "vect.h"
17 
18 //---------------------------------------------------------------------------
19 
20 
21 Matrix :: Matrix(double v)
22 {
23  for( int i = 0;i < 4;i++ )
24  for( int j = 0;j < 4;j++ )
25  x[i][j] = (i==j) ? v : 0.0;
26  x[3][3] = 1;
27 }
28 
29 
30 Matrix :: Matrix()
31 {
32  for( int i = 0;i < 4;i++ )
33  for( int j = 0;j < 4;j++ )
34  x[i][j] = 0.0;
35  x[3][3] = 1;
36 }
37 
38 
39 void Matrix :: Invert()
40 {
41  Matrix Out(1);
42  for(int i = 0;i < 4;i++ )
43  {
44  double d = x[i][i];
45  if( d != 1.0 )
46  {
47  for( int j = 0;j < 4;j++ )
48  {
49  Out.x[i][j] /= d;
50  x[i][j] /= d;
51  }
52  }
53 
54  for(int j = 0;j < 4;j++ )
55  {
56  if( j != i )
57  {
58  if( x[j][i] != 0.0 )
59  {
60  double mulby = x[j][i];
61  for( int k = 0;k < 4;k++)
62  {
63  x[j][k] -= mulby*x[i][k];
64  Out.x[j][k] -= mulby*Out.x[i][k];
65  }
66  }
67  }
68  }
69  }
70  *this = Out;
71 }
72 
73 void Matrix :: Transpose ()
74 {
75  double t;
76  for( int i = 0;i < 4;i++ )
77  for( int j = i;j < 4;j++ )
78  if( i != j )
79  {
80  t = x[i][j];
81  x[i][j] = x[j][i];
82  x[j][i] = t;
83  }
84 }
85 
86 Matrix& Matrix :: operator = ( const Matrix& A)
87 {
88  for( int i = 0;i < 4;i++ )
89  for( int j = 0; j < 4;j++ )
90  x[i][j] = A.x[i][j];
91 
92  return *this;
93 }
94 
95 Matrix& Matrix :: operator += ( const Matrix& A)
96 {
97  for( int i = 0;i < 4;i++ )
98  for( int j = 0; j < 4;j++ )
99  x[i][j] += A.x[i][j];
100 
101  return *this;
102 }
103 
104 Matrix& Matrix :: operator -= ( const Matrix& A)
105 {
106  for( int i = 0;i < 4;i++ )
107  for( int j = 0; j < 4;j++ )
108  x[i][j] -= A.x[i][j];
109 
110  return *this;
111 }
112 
113 Matrix& Matrix :: operator *= ( double v )
114 {
115  for( int i = 0;i < 4;i++ )
116  for( int j = 0; j < 4;j++ )
117  x[i][j] *= v;
118 
119  return *this;
120 }
121 
122 Matrix& Matrix :: operator *= ( const Matrix& A)
123 {
124  Matrix res = *this;
125  for( int i = 0;i < 4;i++ )
126  for( int j = 0; j < 4;j++ )
127  {
128  double sum = 0;
129  for( int k = 0;k < 4;k++ )
130  sum += res.x[i][k] * A.x[k][j];
131 
132  x[i][j] = sum;
133  }
134  return *this;
135 }
136 
137 Matrix operator + ( const Matrix& A, const Matrix& B )
138 {
139  Matrix res;
140  for( int i = 0;i < 4;i++ )
141  for( int j = 0; j < 4;j++ )
142  res.x[i][j] = A.x[i][j] + B.x[i][j];
143 
144  return res;
145 }
146 
147 Matrix operator - ( const Matrix& A, const Matrix& B )
148 {
149  Matrix res;
150  for( int i = 0;i < 4;i++ )
151  for( int j = 0; j < 4;j++ )
152  res.x[i][j] = A.x[i][j] - B.x[i][j];
153 
154  return res;
155 }
156 
157 Matrix operator * ( const Matrix& A, const Matrix& B )
158 {
159  Matrix res;
160  for( int i = 0;i < 4;i++ )
161  for( int j = 0; j < 4;j++ )
162  {
163  double sum = 0;
164  for( int k = 0;k < 4;k++ )
165  sum += A.x[i][k] * B.x[k][j];
166 
167  res.x[i][j] = sum;
168  }
169 
170  return res;
171 }
172 
173 Matrix operator * ( const Matrix& A, double v )
174 {
175  Matrix res;
176  for( int i = 0;i < 4;i++ )
177  for( int j = 0; j < 4;j++ )
178  res.x[i][j] = A.x[i][j] * v;
179 
180  return res;
181 }
182 
183 Vector operator * ( const Vector& v, const Matrix& M )
184 {
185  Vector res;
186 
187  res.x = v.x*M.x[0][0] + v.y*M.x[0][1] + v.z*M.x[0][2] + M.x[0][3];
188  res.y = v.x*M.x[1][0] + v.y*M.x[1][1] + v.z*M.x[1][2] + M.x[1][3];
189  res.z = v.x*M.x[2][0] + v.y*M.x[2][1] + v.z*M.x[2][2] + M.x[2][3];
190  /*
191  res.x = v.x*M.x[0][0] + v.y*M.x[1][0] + v.z*M.x[2][0] + M.x[3][0];
192  res.y = v.x*M.x[0][1] + v.y*M.x[1][1] + v.z*M.x[2][1] + M.x[3][1];
193  res.z = v.x*M.x[0][2] + v.y*M.x[1][2] + v.z*M.x[2][2] + M.x[3][2];
194 
195  double denom = v.x*M.x[0][3] + v.y*M.x[1][3] + v.z*M.x[2][3] + M.x[3][3];
196  if( denom != 1.0 )
197  {
198  res /= denom;
199  ShowMessage("denom="+FloatToStr(denom));
200  }
201 */
202 // ShowMessage( FloatToStr(v.x)+"\n" + FloatToStr(v.x)+"\n" + FloatToStr(v.x) );
203 // ShowMessage("res.x="+FloatToStr(res.x)+"\nres.y="+FloatToStr(res.y));
204 /*
205  ShowMessage( FloatToStr(M.x[0][0])+", " + FloatToStr(M.x[1][0])+", " + FloatToStr(M.x[2][0])+", " + FloatToStr(M.x[3][0])+"\n" +
206  FloatToStr(M.x[0][1])+", " + FloatToStr(M.x[1][1])+", " + FloatToStr(M.x[2][1])+", " + FloatToStr(M.x[3][1])+"\n" +
207  FloatToStr(M.x[0][2])+", " + FloatToStr(M.x[1][2])+", " + FloatToStr(M.x[2][2])+", " + FloatToStr(M.x[3][2])+"\n" +
208  FloatToStr(M.x[0][3])+", " + FloatToStr(M.x[1][3])+", " + FloatToStr(M.x[2][3])+", " + FloatToStr(M.x[3][3])+"\n"
209  );
210 */
211  return res;
212 }
213 
214 Matrix Translate ( const Vector& Loc )
215 {
216  Matrix res(1);
217  res.x[0][3] = Loc.x;
218  res.x[1][3] = Loc.y;
219  res.x[2][3] = Loc.z;
220 /*
221  res.x[3][0] = Loc.x;
222  res.x[3][1] = Loc.y;
223  res.x[3][2] = Loc.z;
224 */
225  return res;
226 }
227 
228 Matrix Scale ( const Vector& v )
229 {
230  Matrix res(1);
231  res.x[0][0] = v.x;
232  res.x[1][1] = v.y;
233  res.x[2][2] = v.z;
234 
235  return res;
236 }
237 
238 Matrix RotateX ( double Angle )
239 {
240  Matrix res(1);
241  double Cosine = cos( Angle );
242  double Sine = sin( Angle );
243 
244  res.x[1][1] = Cosine;
245  res.x[2][1] = Sine;
246  res.x[1][2] = -Sine;
247  res.x[2][2] = Cosine;
248 /*
249  res.x[1][1] = Cosine;
250  res.x[2][1] = -Sine;
251  res.x[1][2] = Sine;
252  res.x[2][2] = Cosine;
253 */
254  return res;
255 }
256 
257 Matrix RotateY ( double Angle )
258 {
259  Matrix res(1);
260  double Cosine = cos( Angle );
261  double Sine = sin( Angle );
262 
263  res.x[0][0] = Cosine;
264  res.x[2][0] = Sine; //-
265  res.x[0][2] = -Sine; //+
266  res.x[2][2] = Cosine;
267 
268  return res;
269 }
270 
271 Matrix RotateZ ( double Angle )
272 {
273  Matrix res(1);
274  double Cosine = cos( Angle );
275  double Sine = sin( Angle );
276 
277 // ShowMessage( "sin="+FloatToStr(Sine)+"\ncos="+FloatToStr(Cosine) );
278 
279  res.x[0][0] = Cosine;
280  res.x[1][0] = Sine; //-
281  res.x[0][1] = -Sine; //+
282  res.x[1][1] = Cosine;
283 
284  return res;
285 }
286 
287 Matrix Rotate ( const Vector& axis, double angle )
288 {
289  Matrix res(1);
290  double Cosine = cos( angle );
291  double Sine = sin( angle );
292 
293 
294  res.x[0][0] = axis.x*axis.x + (1 - axis.x * axis.x) * Cosine;
295  res.x[0][1] = axis.x*axis.y * (1 - Cosine) + axis.z * Sine;
296  res.x[0][2] = axis.x*axis.z * (1 - Cosine) - axis.y * Sine;
297  res.x[0][3] = 0;
298 
299  res.x[1][0] = axis.x*axis.y * (1 - Cosine) - axis.z * Sine;
300  res.x[1][1] = axis.y*axis.y + (1 - axis.y * axis.y) * Cosine;
301  res.x[1][2] = axis.y*axis.z * (1 - Cosine) + axis.x * Sine;
302  res.x[1][3] = 0;
303 
304  res.x[2][0] = axis.x*axis.z * (1 - Cosine) + axis.y * Sine;
305  res.x[2][1] = axis.y*axis.z * (1 - Cosine) - axis.x * Sine;
306  res.x[2][2] = axis.z*axis.z + (1 - axis.z * axis.z) * Cosine;
307  res.x[2][3] = 0;
308 
309  res.x[3][0] = 0;
310  res.x[3][1] = 0;
311  res.x[3][2] = 0;
312  res.x[3][3] = 1;
313 
314 
315  return res;
316 }
317 // Transforms V into coord sys. v1, v2, v3
318 Matrix Transform(const Vector& v1, const Vector& v2, const Vector& v3)
319 {
320  Matrix res(1);
321 
322  // Flipped columns & rows vs prev version
323  res.x[0][0] = v1.x;
324  res.x[0][1] = v1.y;
325  res.x[0][2] = v1.z;
326  res.x[0][3] = 0;
327 
328  res.x[1][0] = v2.x;
329  res.x[1][1] = v2.y;
330  res.x[1][2] = v2.z;
331  res.x[1][3] = 0;
332 
333  res.x[2][0] = v3.x;
334  res.x[2][1] = v3.y;
335  res.x[2][2] = v3.z;
336  res.x[2][3] = 0;
337 
338  res.x[3][0] = 0;
339  res.x[3][1] = 0;
340  res.x[3][2] = 0;
341  res.x[3][3] = 1;
342 
343  return res;
344 }
345 
346 Matrix MirrorX ()
347 {
348  Matrix res(1);
349  res.x[0][0] = -1;
350  return res;
351 }
352 
353 Matrix MirrorY ()
354 {
355  Matrix res(1);
356  res.x[1][1] = -1;
357  return res;
358 }
359 
360 Matrix MirrorZ ()
361 {
362  Matrix res(1);
363  res.x[2][2] = -1;
364  return res;
365 }
RotateY
Matrix RotateY(double Angle)
Definition: matr.cpp:257
Matrix::Invert
void Invert()
Definition: matr.cpp:39
Vector::x
double x
Definition: vect.h:21
Translate
Matrix Translate(const Vector &Loc)
Definition: matr.cpp:214
Scale
Matrix Scale(const Vector &v)
Definition: matr.cpp:228
Matrix::Matrix
Matrix()
Definition: matr.cpp:30
Vector
Definition: vect.h:18
Matrix::operator=
Matrix & operator=(const Matrix &)
Definition: matr.cpp:86
Rotate
Matrix Rotate(const Vector &axis, double angle)
Definition: matr.cpp:287
Matrix::operator*=
Matrix & operator*=(const Matrix &)
Definition: matr.cpp:122
vect.h
MirrorY
Matrix MirrorY()
Definition: matr.cpp:353
MirrorZ
Matrix MirrorZ()
Definition: matr.cpp:360
operator*
Matrix operator*(const Matrix &A, const Matrix &B)
Definition: matr.cpp:157
Vector::z
double z
Definition: vect.h:21
Transform
Matrix Transform(const Vector &v1, const Vector &v2, const Vector &v3)
Definition: matr.cpp:318
matr.h
operator-
Matrix operator-(const Matrix &A, const Matrix &B)
Definition: matr.cpp:147
Matrix
Definition: matr.h:18
Matrix::x
double x[4][4]
Definition: matr.h:21
NaN::d
const double d
Definition: nan.h:35
Matrix::operator-=
Matrix & operator-=(const Matrix &)
Definition: matr.cpp:104
Matrix::Transpose
void Transpose()
Definition: matr.cpp:73
MirrorX
Matrix MirrorX()
Definition: matr.cpp:346
operator+
Matrix operator+(const Matrix &A, const Matrix &B)
Definition: matr.cpp:137
RotateX
Matrix RotateX(double Angle)
Definition: matr.cpp:238
RotateZ
Matrix RotateZ(double Angle)
Definition: matr.cpp:271
Matrix::operator+=
Matrix & operator+=(const Matrix &)
Definition: matr.cpp:95
Vector::y
double y
Definition: vect.h:21
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:36:20 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kstars

Skip menu "kstars"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal