Subversion Repositories gelsvn

Rev

Rev 89 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
39 bj 1
#ifndef __QEM_H
2
#define __QEM_H
3
 
4
#include <cfloat>
5
#include "CGLA/Vec3f.h"
6
#include "CGLA/Vec3d.h"
7
#include "CGLA/Mat3x3d.h"
8
 
9
namespace
10
{
11
	inline const CGLA::Mat3x3d direct_product(const CGLA::Vec3d& v0, const CGLA::Vec3d& v1)
12
		{
13
			CGLA::Mat3x3d m;
14
			for(int i=0;i<3;++i)
15
				for(int j=0;j<3;++j)
16
					m[i][j] = v0[i]*v1[j];
17
			return m;
18
		}
19
}
20
 
21
namespace Geometry
22
{
23
	class QEM
24
		{
25
			CGLA::Mat3x3d A;
26
			CGLA::Vec3d   b;
27
			double   c;
28
		public:
29
 
30
			QEM(): A(0), c(0), b(0) {}
31
 
32
			QEM(const CGLA::Vec3d& p0, const CGLA::Vec3d& n0, double w=1.0f):
33
				A(direct_product(n0,n0) * w), 
34
				b(-n0*dot(n0,p0) * w), 
35
				c(dot(p0,n0)*dot(p0,n0) * w) {}
36
 
37
 
38
			void operator+=(const QEM& q)
39
				{
40
					A += q.A;
41
					b += q.b;
42
					c += q.c;
43
				}
44
 
45
			float error(const CGLA::Vec3f& _p) const
46
				{
47
					CGLA::Vec3d p(_p);
48
					return dot(p,A*p) + 2*dot(b,p)+ c;
49
				}
50
 
51
			double determinant() const
52
				{
53
					return CGLA::determinant(A);
54
				}
55
 
56
			const CGLA::Vec3f grad(const CGLA::Vec3f& _p) const
57
				{
58
					CGLA::Vec3d p(_p);
59
					CGLA::Vec3f g(2*A*p+2*b);
60
					return g;
61
				}
62
 
63
			CGLA::Vec3f opt_pos(double QEM_thresh = 0.005) const;
64
 
65
		};
66
}
67
 
68
namespace GEO = Geometry;
69
 
70
#endif