summaryrefslogtreecommitdiffstats
path: root/kpovmodeler/pmmatrix.h
blob: a36916024f880c0e5dd34fb65027ec6564081682 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//-*-C++-*-
/*
**************************************************************************
                                 description
                             --------------------
    copyright            : (C) 2000-2001 by Andreas Zehender
    email                : [email protected]
**************************************************************************

**************************************************************************
*                                                                        *
*  This program is free software; you can redistribute it and/or modify  *
*  it under the terms of the GNU General Public License as published by  *
*  the Free Software Foundation; either version 2 of the License, or     *
*  (at your option) any later version.                                   *
*                                                                        *
**************************************************************************/


#ifndef PMMATRIX_H
#define PMMATRIX_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "pmmath.h"
#include "math.h"

#include <tqstring.h>

#include <GL/gl.h>

class PMVector;

/**
 * 4x4 matrix for transformations with homogenous coordinates
 *
 * Optimized for OpenGL.
 */
class PMMatrix
{
public:
   /**
    * Creates a zero matrix
    */
   PMMatrix( );
   /**
    * Deletes the matrix
    */
   ~PMMatrix( );
   /**
    * Assigns m to the matrix
    */
   PMMatrix& operator= ( const PMMatrix& m );
   /**
    * Creates an identity matrix
    */
   static PMMatrix identity( );
   /**
    * Creates a matrix for translation.
    */
   static PMMatrix translation( double x, double y, double z );
   /**
    * Creates a matrix for rotations. Rotation first around the x, then y
    * and then around the z axis.
    *
    * x, y and z in rad
    */
   static PMMatrix rotation( double x, double y, double z );
   /**
    * Creates a matrix for rotation around the direction n with angle a.
    *
    * a in rad.
    */
   static PMMatrix rotation( const PMVector& n, double a );
   /**
    * Creates a viewing matrix.
    *
    * The viewpoint is specified by the parameter eye. The parameter
    * lookAt specifies any point along the line of sight. The up vector
    * indicate which direction is up.
    */
   static PMMatrix viewTransformation( const PMVector& eye,
                                       const PMVector& lookAt,
                                       const PMVector& up );
   /**
    * Backwards calculate the rotations from the matrix
    */
   void toRotation( double* x, double* y, double* z );
   
   /**
    * Creates a matrix for scale.
    */
   static PMMatrix scale( double x, double y, double z );
   /**
    * Returns the current gl modelview matrix
    */
   static PMMatrix modelviewMatrix( );
   
   /**
    * Returns a pointer to the column (!) at position index.
    * That means matrix[i][j] is the element at column i and row j*/
   GLdouble* operator[] ( int index ) { return &( m_elements[index*4] ); }
   /**
    * Returns a pointer to the column (!) at position index.
    * That means matrix[i][j] is the element at column i and row j*/
   const GLdouble* operator[] ( int index ) const
      { return &(m_elements[index*4]); }
   /**
    * Returns true if an inverse matrix can be calculated
    */
   bool canBuildInverse( ) const { return ! approxZero( det( ) ); }
   /**
    * Returns the inverse matrix if possible, otherwise a identity matrix
    */
   PMMatrix inverse( ) const;
   /**
    * Returns the determinant of the matrix
    */
   double det( ) const;
   /**
    * Multiplies m to the matrix from the right side
    */
   PMMatrix& operator*= ( const PMMatrix& m );
   /**
    * Multiplies each element with d
    */
   PMMatrix& operator*= ( const double d );
   /**
    * Divides each element by d
    */
   PMMatrix& operator/= ( const double d );

   /**
    * Returns a matrix with negated elements
    */
   friend PMMatrix operator- ( const PMMatrix& m );
   /**
    * Multiplies m2 with m1 from the right side (m1*m2)*/
   friend PMMatrix operator* ( const PMMatrix& m1, const PMMatrix& m2 );
   /**
    * Multiplies d to all elements
    */
   friend PMMatrix operator* ( const PMMatrix& m, const double d );
   /**
    * Multiplies d to all elements
    */
   friend PMMatrix operator* ( const GLdouble d, const PMMatrix& m );
   /**
    * Divides all elements by d
    */
   friend PMMatrix operator/ ( const PMMatrix& m, const double d );

   /**
    * Only for tests
    */
   void testOutput( );
   /**
    * Returns a pointer to the data. Can be used in glMultMatrixd
    */
   const GLdouble* data( ) const { return m_elements; }
   
   /**
    * Returns a string for xml output
    */
   TQString serializeXML( ) const;
   /**
    * loads the vector data from the xml string
    */
   bool loadXML( const TQString& str );
private:
   /**
    * Exchanges two rows
    */
   void exchangeRows( int r1, int r2 );
   /**
    * Finds a row with not zero element in column index. Begins to
    * search in row at position index. Used for det() and inverse()
    *
    * Returns -1 if all rows are zero in that column
    */
   int notNullElementRow( const int index ) const;
   
   GLdouble m_elements[16];
};

#endif