File: XnSymmetricMatrix3x3.h

package info (click to toggle)
openni2 2.2.0.33%2Bdfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 22,216 kB
  • sloc: cpp: 111,197; ansic: 35,511; sh: 10,542; python: 1,313; java: 952; makefile: 575; xml: 12
file content (278 lines) | stat: -rw-r--r-- 9,177 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*****************************************************************************
*                                                                            *
*  PrimeSense PSCommon Library                                               *
*  Copyright (C) 2012 PrimeSense Ltd.                                        *
*                                                                            *
*  This file is part of PSCommon.                                            *
*                                                                            *
*  Licensed under the Apache License, Version 2.0 (the "License");           *
*  you may not use this file except in compliance with the License.          *
*  You may obtain a copy of the License at                                   *
*                                                                            *
*      http://www.apache.org/licenses/LICENSE-2.0                            *
*                                                                            *
*  Unless required by applicable law or agreed to in writing, software       *
*  distributed under the License is distributed on an "AS IS" BASIS,         *
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
*  See the License for the specific language governing permissions and       *
*  limitations under the License.                                            *
*                                                                            *
*****************************************************************************/
#ifndef _XN_SYMMETRIC_MATRIX_3X3_H_
#define _XN_SYMMETRIC_MATRIX_3X3_H_

#include <XnPlatform.h>
#include "XnLib.h"
#include "XnVector3D.h"

namespace xnl
{

class Matrix3x3;

class SymmetricMatrix3x3
{
public:
	static SymmetricMatrix3x3 Zero() {return SymmetricMatrix3x3::Diagonal(Vector3D::Zero());}
	static SymmetricMatrix3x3 Identity() {return SymmetricMatrix3x3::Diagonal(Vector3D::All(1));}
	static SymmetricMatrix3x3 Diagonal(float x00, float x11, float x22) {return SymmetricMatrix3x3(x00, 0, 0, x11, 0, x22);}
	static SymmetricMatrix3x3 Diagonal(const Vector3D& vec) {return SymmetricMatrix3x3::Diagonal(vec.x, vec.y, vec.z);}

	SymmetricMatrix3x3()
	{
		SetZero();
	}
	SymmetricMatrix3x3(const SymmetricMatrix3x3& other)
	{
		*this = other;
	}
	SymmetricMatrix3x3(float x00, float x01, float x02, float x11, float x12, float x22)
	{
		Set(x00, x01, x02, x11, x12, x22);
	}

	void Set(float x00, float x01, float x02, float x11, float x12, float x22)
	{
		m_x00 = x00; m_x01=x01; m_x02=x02; m_x11=x11; m_x12=x12; m_x22=x22;
	}
	void SetZero()
	{
		*this = SymmetricMatrix3x3::Zero();
	}
	SymmetricMatrix3x3& operator=(const SymmetricMatrix3x3& other)
	{
		Set(other.m_x00, other.m_x01, other.m_x02, other.m_x11, other.m_x12, other.m_x22);

		return *this;
	}

	SymmetricMatrix3x3 operator+(const SymmetricMatrix3x3& other) const
	{
		SymmetricMatrix3x3 result(*this);
		result += other;
		return result;
	}
	SymmetricMatrix3x3 operator-(const SymmetricMatrix3x3& other) const
	{
		SymmetricMatrix3x3 result(*this);
		result -= other;
		return result;
	}

	SymmetricMatrix3x3 operator*(float a) const
	{
		SymmetricMatrix3x3 result(*this);
		result *= a;
		return result;
	}
	friend SymmetricMatrix3x3 operator*(float a, SymmetricMatrix3x3& mat)
	{
		return mat*a;
	}
	Vector3D operator*(const Vector3D& vec) const
	{
		return Vector3D(m_x00*vec.x+m_x01*vec.y+m_x02*vec.z,
						m_x01*vec.x+m_x11*vec.y+m_x12*vec.z,
						m_x02*vec.x+m_x12*vec.y+m_x22*vec.z);
	}

	SymmetricMatrix3x3 operator-() const
	{
		SymmetricMatrix3x3 mat = *this;
		mat*= -1;
		return mat;
	}

	SymmetricMatrix3x3 operator-(float a) const
	{
		return SymmetricMatrix3x3(m_x00-a,m_x01,m_x02,m_x11-a,m_x12,m_x22-a);
	}

	SymmetricMatrix3x3& operator*=(float a)
	{
		m_x00 *= a; m_x01 *= a; m_x02 *= a;
		m_x11 *= a; m_x12 *= a;
		m_x22 *= a;

		return *this;
	}
	SymmetricMatrix3x3& operator+=(const SymmetricMatrix3x3& other)
	{
		m_x00 += other.m_x00; m_x01 += other.m_x01; m_x02 += other.m_x02;
		m_x11 += other.m_x11; m_x12 += other.m_x12;
		m_x22 += other.m_x22;

		return *this;
	}
	SymmetricMatrix3x3& operator-=(const SymmetricMatrix3x3& other)
	{
		m_x00 -= other.m_x00; m_x01 -= other.m_x01; m_x02 -= other.m_x02;
		m_x11 -= other.m_x11; m_x12 -= other.m_x12;
		m_x22 -= other.m_x22;

		return *this;
	}

	void AddOuterProduct(const Vector3D& vec)
	{
		m_x00 += vec.x*vec.x;
		m_x01 += vec.x*vec.y;
		m_x02 += vec.x*vec.z;
		m_x11 += vec.y*vec.y;
		m_x12 += vec.y*vec.z;
		m_x22 += vec.z*vec.z;
	}
	void AddWeightedOuterProduct(float weight, const Vector3D& vec)
	{
		m_x00 += weight*vec.x*vec.x;
		m_x01 += weight*vec.x*vec.y;
		m_x02 += weight*vec.x*vec.z;
		m_x11 += weight*vec.y*vec.y;
		m_x12 += weight*vec.y*vec.z;
		m_x22 += weight*vec.z*vec.z;

	}
	void SubtractOuterProduct(const Vector3D& vec)
	{
		m_x00 -= vec.x*vec.x;
		m_x01 -= vec.x*vec.y;
		m_x02 -= vec.x*vec.z;
		m_x11 -= vec.y*vec.y;
		m_x12 -= vec.y*vec.z;
		m_x22 -= vec.z*vec.z;
	}

	float Trace() const
	{
		return m_x00+m_x11+m_x22;
	}
	float MaxAbs() const;

	Vector3D LargestColumnNormalized() const
	{
		float scales[3] = {Math::Sqr(m_x00) + Math::Sqr(m_x01) + Math::Sqr(m_x02), Math::Sqr(m_x01) + Math::Sqr(m_x11) + Math::Sqr(m_x12), Math::Sqr(m_x02) + Math::Sqr(m_x12) + Math::Sqr(m_x22)};
		int i = Math::ArgMax(scales[0], scales[1], scales[2]);
		if(scales[i] == 0) return Vector3D(1,0,0);
		else {
			switch(i) {
			case 0: return Vector3D(m_x00,m_x01,m_x02)/Math::Sqrt(scales[i]); break;
			case 1: return Vector3D(m_x01,m_x11,m_x12)/Math::Sqrt(scales[i]); break;
			case 2: return Vector3D(m_x02,m_x12,m_x22)/Math::Sqrt(scales[i]); break;
			default: XN_ASSERT(false); return Vector3D(1,0,0); break;
			}
		}
	}

	SymmetricMatrix3x3 CofactorMatrix() const
	{
		return SymmetricMatrix3x3(m_x11*m_x22 - m_x12*m_x12, m_x12*m_x02 - m_x01*m_x22, m_x01*m_x12 - m_x11*m_x02, m_x00*m_x22 - m_x02*m_x02, m_x01*m_x02 - m_x00*m_x12, m_x00*m_x11 - m_x01*m_x01);
	}

	float Determinant() const
	{
		float cofactor00 = m_x11*m_x22 - m_x12*m_x12;
		float cofactor01 = m_x12*m_x02 - m_x01*m_x22;
		float cofactor02 = m_x01*m_x12 - m_x11*m_x02;
		return m_x00 * cofactor00 + m_x01*cofactor01 + m_x02*cofactor02;
	}

	bool GetInverse(SymmetricMatrix3x3& inv, float tolerance=1e-8) const
	{
		float cofactor00 = m_x11*m_x22 - m_x12*m_x12;
		float cofactor01 = m_x12*m_x02 - m_x01*m_x22;
		float cofactor02 = m_x01*m_x12 - m_x11*m_x02;
		float det = m_x00 * cofactor00 + m_x01*cofactor01 + m_x02*cofactor02;
		if(Math::Abs(det)<=tolerance) return false;
		float oneOverDet = 1/det;
		inv =  SymmetricMatrix3x3(cofactor00*oneOverDet, cofactor01*oneOverDet, cofactor02*oneOverDet,
									(m_x00*m_x22 - m_x02*m_x02)*oneOverDet, (m_x01*m_x02 - m_x00*m_x12)*oneOverDet,
									(m_x00*m_x11 - m_x01*m_x01)*oneOverDet);
		return true;
	}
	SymmetricMatrix3x3 Inverse(float tolerance=1e-8) const
	{
		SymmetricMatrix3x3 inv;
		if(GetInverse(inv,tolerance)) return inv;
		else return SymmetricMatrix3x3();
	}
	void Invert(float tolerance=1e-8)
	{
		*this = Inverse(tolerance);
	}

	// vec^T * mat * vec
	float MatrixInnerProduct(const Vector3D& vec) const
	{
		return Math::Sqr(vec.x)*m_x00 + Math::Sqr(vec.y)*m_x11 + Math::Sqr(vec.z)*m_x22 + 2*vec.x*vec.y*m_x01 + 2*vec.x*vec.z*m_x02 + 2*vec.y*vec.z*m_x12;
	}

	// Eigenvalues
	Vector3D GetEigenValues() const;
	void GetEigenVector(float lambda, Vector3D& vec) const;
	void GetEigenVectors(const Vector3D& lambdas, Matrix3x3& eigenVectors, float tolerance=1e-8) const;
	void SolveEigenProblem(Vector3D& eigenValues, Matrix3x3& eigenVectors, float tolerance=1e-8) const;
	void SolveSingleEigenProblem(int i, Vector3D& eigenValues, Vector3D& eigenVector, float tolerance=1e-8) const;

	friend SymmetricMatrix3x3 OuterProduct(const Vector3D& vec)
	{
		return SymmetricMatrix3x3(vec.x*vec.x, vec.x*vec.y, vec.x*vec.z, vec.y*vec.y, vec.y*vec.z, vec.z*vec.z);
	}

	float operator()(int x, int y) const
	{
		XN_ASSERT(0 <= x && x < 3 && 0 <= y && y < 3);

		if (x == 0 && y == 0) return m_x00;
		if ((x == 0 && y == 1) || (x == 1 && y == 0)) return m_x01;
		if ((x == 0 && y == 2) || (x == 2 && y == 0)) return m_x02;
		if (x == 1 && y == 1) return m_x11;
		if ((x == 1 && y == 2) || (x == 2 && y == 1)) return m_x12;
		if (x == 2 && y == 2) return m_x22;

		return m_x00;
	}
	float& operator()(int x, int y)
	{
		XN_ASSERT(0 <= x && x < 3 && 0 <= y && y < 3);

		if (x == 0 && y == 0) return m_x00;
		if ((x == 0 && y == 1) || (x == 1 && y == 0)) return m_x01;
		if ((x == 0 && y == 2) || (x == 2 && y == 0)) return m_x02;
		if (x == 1 && y == 1) return m_x11;
		if ((x == 1 && y == 2) || (x == 2 && y == 1)) return m_x12;
		if (x == 2 && y == 2) return m_x22;

		return m_x00;
	}
private:
	Vector3D GetEigenValuesNoScaling() const;
	void SolveEigenProblemNoScaling(Vector3D& eigenValues, Matrix3x3& eigenVectors, float tolerance=1e-8) const;
	Vector3D GetEigenValuesWithScaling() const;
	void SolveEigenProblemWithScaling(Vector3D& eigenValues, Matrix3x3& eigenVectors, float tolerance=1e-8) const;

	float m_x00, m_x01, m_x02, m_x11, m_x12, m_x22;
};

} // xnl

#endif // _XN_SYMMETRIC_MATRIX_3X3_H_