File: matrix.h

package info (click to toggle)
megaglest 3.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 12,904 kB
  • ctags: 18,215
  • sloc: cpp: 144,232; ansic: 11,860; sh: 2,949; perl: 1,899; python: 1,751; objc: 142; asm: 42; makefile: 24
file content (161 lines) | stat: -rw-r--r-- 2,981 bytes parent folder | download | duplicates (7)
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
// ==============================================================
//	This file is part of Glest Shared Library (www.glest.org)
//
//	Copyright (C) 2001-2008 MartiƱo Figueroa
//
//	You can redistribute this code 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 _SHARED_GRAPHICS_MATRIX_H_
#define _SHARED_GRAPHICS_MATRIX_H_

#include "vec.h"
#include "leak_dumper.h"

namespace Shared{ namespace Graphics{

// =====================================================
//	class Matrix3
// =====================================================

template<typename T>
class Matrix3{
private:
	T data[9];
public:
	Matrix3(){};

	Matrix3(T *p){
		for(int i=0; i<9; ++i){
			data[i]= p[i];
		}
	}

	T *ptr(){
		return data;
	}

	const T *ptr() const{
		return data;
	}

	T &operator[](int i){
		return data[i];
	}

	T &operator()(int i, int j){
		return data[i*3+j];
	}

	Vec3<T> operator * (const Vec3<T> &v) const{
		Vec3<T> rv;
		
		return Vec3f(
			data[0]*v.x + data[1]*v.y + data[2]*v.z,
			data[3]*v.x + data[4]*v.y + data[5]*v.z,
			data[6]*v.x + data[7]*v.y + data[8]*v.z);
	}

	Matrix3<T> operator * (const Matrix3<T> &m) const{
		Matrix3<T> rm;

		for(int i=0; i<3; ++i){
			for(int j=0; j<3; ++j){
				T acum= 0.0f;
				for(int k=0; k<3; ++k){  
					acum+= data[i*3+k]*m[k*3+j];	
				}
				rm[i*3+j]= acum;
			}
		}
		return rm;
	}

	void traspose(){
		for(int i=0; i<3; ++i){
			for(int j=0; j<3; ++j){
				T tmp= data[j*3+i];
				data[j*3+i]= data[i*3+j];
				data[i*3+j]= tmp;
			}
		}
	}
};

typedef Matrix3<float> Matrix3f;
typedef Matrix3<double> Matrix3d;

// =====================================================
//	class Matrix4
// =====================================================

template<typename T>
class Matrix4{
private:
	T data[16];
public:
	Matrix4(){};

	Matrix4(T *p){
		for(int i=0; i<16; ++i){
			data[i]= p[i];
		}
	}

	T *ptr(){
		return data;
	}

	const T *ptr() const{
		return data;
	}

	T &operator[](int i){
		return data[i];
	}

	const T &operator[](int i) const{
		return data[i];
	}

	T &operator()(int i, int j){
		return data[i*4+j];
	}

	Vec4<T> operator * (const Vec4<T> &v) const{
		Vec4<T> rv;
		
		return Vec4f(
			data[0]*v.x + data[1]*v.y + data[2]*v.z + data[3]*v.w,
			data[4]*v.x + data[5]*v.y + data[6]*v.z + data[7]*v.w,
			data[8]*v.x + data[9]*v.y + data[10]*v.z + data[11]*v.w,
			data[12]*v.x + data[13]*v.y + data[14]*v.z + data[15]*v.w);
	}

	Matrix4<T> operator * (const Matrix4<T> &m) const{
		Matrix4<T> rm;

		for(int i=0; i<4; ++i){
			for(int j=0; j<4; ++j){
				T acum= 0.0f;
				for(int k=0; k<4; ++k){  
					acum+= data[i*4+k]*m[k*4+j];	
				}
				rm[i*4+j]= acum;
			}
		}
		return rm;
	}

};

typedef Matrix4<float> Matrix4f;
typedef Matrix4<double> Matrix4d;


}} //enmd namespace

#endif