summaryrefslogtreecommitdiff
path: root/cad/plugins/NanoVision-1/src/Plugins/RenderingEngines/OpenGL/GLT/glt_matrix4.h
blob: 992f891c27c27c9fd8ec6c7665696d183d184edf (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#ifndef MATH_MATRIX4_H
#define MATH_MATRIX4_H

/*

  GLT OpenGL C++ Toolkit (LGPL)
  Copyright (C) 2000-2002 Nigel Stewart  

  Email: nigels@nigels.com   
  WWW:   http://www.nigels.com/glt/

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

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

/*! \file 
    \brief   4x4 Matrix 
	\ingroup Math
*/

#include <iosfwd>

#if defined(__APPLE__)
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#endif

#include "glt_real.h"
#include "glt_vector3.h"

////////////////////////// GltMatrix /////////////////////////////////

/*! \class   GltMatrix 
    \brief   4x4 Matrix
	\ingroup Math
	\author  Nigel Stewart, RMIT (nigels@nigels.com)
	\todo    Nice pictures and explanation for 4x4 transformation matrices.
*/

class GltUnMatrix;

class GltMatrix
{
	friend GltMatrix matrixScale(const double sf);
	friend GltMatrix matrixScale(const Vector sf);
	friend GltMatrix matrixTranslate(const Vector trans);
	friend GltMatrix matrixTranslate(const real x,const real y,const real z);
	friend GltMatrix matrixRotate(const Vector axis,const double angle);
	friend GltMatrix matrixRotate(const double azimuth,const double elevation);
	friend GltMatrix matrixOrient(const Vector &x,const Vector &y,const Vector &z);
	friend GltMatrix matrixOrient(const Vector &direction,const Vector &up);

	friend std::ostream &operator<<(std::ostream &os,const GltMatrix &m);
	friend std::istream &operator>>(std::istream &is,      GltMatrix &m);

public:

	/// Default constructor
	GltMatrix();
	/// Copy constructor
	GltMatrix(const GltMatrix &matrix);
	/// Construct from array 
	GltMatrix(const GLfloat *matrix);
	/// Construct from array 
	GltMatrix(const real *matrix);
	/// Construct from OpenGL GL_MODELVIEW_MATRIX or GL_PROJECTION_MATRIX
	GltMatrix(const unsigned int glMatrix);
	/// Construct from string
	GltMatrix(const std::string &str);
#if defined(__APPLE__)
	GltMatrix(GLenum glMatrixMode);
#endif

	/// Assignment operator
	GltMatrix &operator=(const GltMatrix &);

	/// GltMatrix multiplication
	GltMatrix  operator*(const GltMatrix &) const;
	/// In-place matrix multiplication
	GltMatrix &operator*=(const GltMatrix &);

	/// GltMatrix transformation of 3D vector
	Vector operator*(const Vector &) const;

	/// Reset to identity matrix
	void reset();
	/// Reset to identity matrix
	void identity();

	/// Is this matrix identity?
	bool isIdentity() const;

    /// Access i'th row, j'th column element
    real& operator () (int i, int j) { return element(j, i); }
    real const& operator () (int i, int j) const { return get(j, i); }
    
	/// Access i'th element of matrix
	real &operator[](const int i);
	/// Access i'th element of matrix
	const real &operator[](const int i) const;

	/// Access as array
	operator real * ();
	/// Access as array
	operator const double * () const;

	/// Equality operator
	bool operator==(const GltMatrix &) const;
	/// Not-equal operator
	bool operator!=(const GltMatrix &) const;

	/// Calculate matrix inverse
	GltMatrix inverse() const;
	/// Calculate matrix transpose
	GltMatrix transpose() const;
	/// Calculate unmatrix
	GltUnMatrix unmatrix() const;
	/// Calculate matrix determinant
	double det() const;

	/// Mult current OpenGL matrix 
	void glMultMatrix() const;
	/// Load current OpenGL matrix
	void glLoadMatrix() const;

    /// set to current model-view or projection matrix
    void glGet(GLenum glMatrixMode);
    
    /// set current modelview or projection matrix
    void glSet(GLenum glMatrixMode);
    
    /// Is this an orthographic projection matrix?
    bool isOrtho() const;
    
    /// Is this a perspective projection matrix?
    bool isPerspective() const;
    
    /// Write matrix in Povray format
	std::ostream &writePov(std::ostream &os) const;

private:

	real _matrix[16];
	static real _identity[16];

	inline void set(const int col,const int row,const real& val) 
	{ 
		_matrix[col*4+row] = val;
	}

	inline real const& get(const int col,const int row) const
	{
		return _matrix[col*4+row];
	}

	inline real& element(const int col,const int row) 
	{
		return _matrix[col*4+row];
	}

	// From Mesa-2.2\src\glu\project.c

	static void invertMatrixGeneral( const double *m, double *out );
	static void invertMatrix( const double *m, double *out );

	// From Graphics Gems GEMSI\MATINV.C

	double 
	det3x3
	( 
		const double a1, 
		const double a2, 
		const double a3, 
		const double b1, 
		const double b2, 
		const double b3, 
		const double c1, 
		const double c2, 
		const double c3 
	) const;

	double
	det2x2
	( 
		const double a, 
		const double b, 
		const double c, 
		const double d
	) const;
};

#endif