File: matrix.cc

package info (click to toggle)
gnome-chemistry-utils 0.14.9-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,836 kB
  • ctags: 7,337
  • sloc: cpp: 72,977; sh: 11,381; xml: 6,304; makefile: 1,663; ansic: 1,061
file content (245 lines) | stat: -rw-r--r-- 6,186 bytes parent folder | download | duplicates (4)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// -*- C++ -*-

/*
 * Gnome Chemistry Utils
 * gcu/matrix.cc
 *
 * Copyright (C) 2000-2009 Jean Bréfort <jean.brefort@normalesup.org>
 *
 * 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 3 of the
 * License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#include "config.h"
#include "matrix.h"
#include "vector.h"
#include <cmath>

namespace gcu
{

Matrix::Matrix ()
{
	x[0][0] = x[1][1] = x[2][2] = 1.;
	x[0][1] = x[0][2] = x[1][0] = x[1][2] = x[2][0] = x[2][1] = 0.;
}

Matrix::Matrix (double d)
{
	x[0][0] = x[1][1] = x[2][2] = d;
	x[0][1] = x[0][2] = x[1][0] = x[1][2] = x[2][0] = x[2][1] = 0.;
}

Matrix::Matrix (double d[3][3])
{
	x[0][0] = d[0][0];
	x[0][1] = d[0][1];
	x[0][2] = d[0][2];
	x[1][0] = d[1][0];
	x[1][1] = d[1][1];
	x[1][2] = d[1][2];
	x[2][0] = d[2][0];
	x[2][1] = d[2][1];
	x[2][2] = d[2][2];
}

Matrix::Matrix (Matrix const &m)
{
	x[0][0] = m.x[0][0];
	x[0][1] = m.x[0][1];
	x[0][2] = m.x[0][2];
	x[1][0] = m.x[1][0];
	x[1][1] = m.x[1][1];
	x[1][2] = m.x[1][2];
	x[2][0] = m.x[2][0];
	x[2][1] = m.x[2][1];
	x[2][2] = m.x[2][2];
}

Matrix::Matrix (Vector const &v1, Vector const &v2, Vector const &v3, bool as_rows)
{
	if (as_rows) {
		x[0][0] = v1.GetX ();
		x[0][1] = v1.GetY ();
		x[0][2] = v1.GetZ ();
		x[1][0] = v2.GetX ();
		x[1][1] = v2.GetY ();
		x[1][2] = v2.GetZ ();
		x[2][0] = v3.GetX ();
		x[2][1] = v3.GetY ();
		x[2][2] = v3.GetZ ();
	} else {
		x[0][0] = v1.GetX ();
		x[0][1] = v2.GetX ();
		x[0][2] = v3.GetX ();
		x[1][0] = v1.GetY ();
		x[1][1] = v2.GetY ();
		x[1][2] = v3.GetY ();
		x[2][0] = v1.GetZ ();
		x[2][1] = v2.GetZ ();
		x[2][2] = v3.GetZ ();
	}
}

Matrix::~Matrix ()
{
}

Matrix::Matrix (double Psi, double Theta, double Phi, MatrixType Type)
{
	double sp = sin (Psi);
	double cp = cos (Psi);
	double st = sin (Theta);
	double ct = cos (Theta);
	double sf = sin (Phi);
	double cf = cos (Phi);
	switch(Type) {
	case euler :
		x[0][0] = cf * cp - sf * sp * ct;
		x[0][1] = - cp * sf - sp * cf * ct;
		x[0][2] = st * sp;
		x[1][0] =  sp * cf + cp * sf * ct;
		x[1][1] = cf * cp * ct - sf * sp;
		x[1][2] = - st * cp;
		x[2][0] = st * sf;
		x[2][1] = st * cf;
		x[2][2] = ct;
		break;
	case antieuler :
		x[0][0] = cf * cp - sf * sp * ct;
		x[0][1] = cf * sp + cp * sf * ct;
		x[0][2] = st * sf;
		x[1][0] = - sf * cp - cf * sp * ct;
		x[1][1] = cf * cp * ct - sf * sp;
		x[1][2] = st * cf;
		x[2][0] = st * sp;
		x[2][1] = - st * cp;
		x[2][2] = ct;
		break;
	case rotation :
		Matrix m1(Psi,Theta,Phi,euler);
		Matrix m2(Psi,Theta,0,antieuler);
		*this = m1 * m2;
		break;
	}
}

Matrix::Matrix (double x11, double x12, double x13, double x21, double x22, double x23, double x31, double x32, double x33)
{
	x[0][0] = x11;
	x[0][1] = x12;
	x[0][2] = x13;
	x[1][0] = x21;
	x[1][1] = x22;
	x[1][2] = x23;
	x[2][0] = x31;
	x[2][1] = x32;
	x[2][2] = x33;
}

Matrix& Matrix::operator* (Matrix const &cMat) const
{
	static Matrix cMat0;
	Matrix m(
			x[0][0] * cMat.x[0][0] + x[0][1] * cMat.x[1][0] + x[0][2] * cMat.x[2][0],
			x[0][0] * cMat.x[0][1] + x[0][1] * cMat.x[1][1] + x[0][2] * cMat.x[2][1],
			x[0][0] * cMat.x[0][2] + x[0][1] * cMat.x[1][2] + x[0][2] * cMat.x[2][2],
			x[1][0] * cMat.x[0][0] + x[1][1] * cMat.x[1][0] + x[1][2] * cMat.x[2][0],
			x[1][0] * cMat.x[0][1] + x[1][1] * cMat.x[1][1] + x[1][2] * cMat.x[2][1],
			x[1][0] * cMat.x[0][2] + x[1][1] * cMat.x[1][2] + x[1][2] * cMat.x[2][2],
			x[2][0] * cMat.x[0][0] + x[2][1] * cMat.x[1][0] + x[2][2] * cMat.x[2][0],
			x[2][0] * cMat.x[0][1] + x[2][1] * cMat.x[1][1] + x[2][2] * cMat.x[2][1],
			x[2][0] * cMat.x[0][2] + x[2][1] * cMat.x[1][2] + x[2][2] * cMat.x[2][2]
		);
	return cMat0 = m;
}

Matrix& Matrix::operator= (Matrix const &cMat)
{
	x[0][0] = cMat.x[0][0];
	x[0][1] = cMat.x[0][1];
	x[0][2] = cMat.x[0][2];
	x[1][0] = cMat.x[1][0];
	x[1][1] = cMat.x[1][1];
	x[1][2] = cMat.x[1][2];
	x[2][0] = cMat.x[2][0];
	x[2][1] = cMat.x[2][1];
	x[2][2] = cMat.x[2][2];
	return *this;
}

void Matrix::Euler (double& Psi, double& Theta, double& Phi)
{
	if (fabs(x[2][2]) > .999999999) {
		Theta = (x[2][2] > 0) ? 0 : 3.1415926535897931;
		Psi = 0;
		if (fabs(x[0][0]) > .999999999)
		Phi = (x[0][0] > 0) ? 0 : 3.1415926535897931;
		else Phi = (x[1][0] > 0) ? acos(x[0][0]) : - acos(x[0][0]);
	} else {
		Theta = acos(x[2][2]);
		double st = sin(Theta);
		double si = x[0][2] / st;
		double co = - x[1][2] / st;
		if (fabs(co) > .999999999)
			Psi = (co > 0) ? 0 : 3.1415926535897931;
		else
			Psi = (si > 0) ? acos(co) : - acos(co);
		si = x[2][0] / st;
		co = x[2][1] / st;
		if (fabs(co) > .999999999)
			Phi = (co > 0) ? 0 : 3.1415926535897931;
		else
			Phi = (si > 0) ? acos(co) : - acos(co);
	}
}

void Matrix::Transform (double &dx, double &dy , double &dz) const
{
	double x1 = dx * x[0][0] + dy * x[0][1] + dz * x[0][2];
	double y1 = dx * x[1][0] + dy * x[1][1] + dz * x[1][2];
	dz = dx * x[2][0] + dy * x[2][1] + dz * x[2][2];
	dx = x1;
	dy = y1;
}

Vector Matrix::operator* (Vector const &v) const
{
	Vector r;
	r.SetX (v.GetX () * x[0][0] + v.GetY () * x[0][1] + v.GetZ () * x[0][2]);
	r.SetY (v.GetX () * x[1][0] + v.GetY () * x[1][1] + v.GetZ () * x[1][2]);
	r.SetZ (v.GetX () * x[2][0] + v.GetY () * x[2][1] + v.GetZ () * x[2][2]);
	return r;
}

Vector Matrix::glmult (Vector const &v) const
{
	Vector r;
	r.SetZ (v.GetZ () * x[0][0] + v.GetX () * x[0][1] + v.GetY () * x[0][2]);
	r.SetX (v.GetZ () * x[1][0] + v.GetX () * x[1][1] + v.GetY () * x[1][2]);
	r.SetY (v.GetZ () * x[2][0] + v.GetX () * x[2][1] + v.GetY () * x[2][2]);
	return r;
}

// Implementation of GLMatrix
GLMatrix::GLMatrix ()
{
}

GLMatrix::~GLMatrix ()
{
}

}	//	namespace gcu