Subversion Repositories gelsvn

Rev

Rev 89 | Rev 207 | Go to most recent revision | Details | Compare with Previous | 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
 
89 jab 9
 
39 bj 10
namespace
11
{
12
	inline const CGLA::Mat3x3d direct_product(const CGLA::Vec3d& v0, const CGLA::Vec3d& v1)
13
		{
14
			CGLA::Mat3x3d m;
15
			for(int i=0;i<3;++i)
16
				for(int j=0;j<3;++j)
17
					m[i][j] = v0[i]*v1[j];
18
			return m;
19
		}
20
}
21
 
22
namespace Geometry
23
{
24
	class QEM
25
		{
26
			CGLA::Mat3x3d A;
27
			CGLA::Vec3d   b;
28
			double   c;
29
		public:
30
 
136 jab 31
			QEM(): A(0), b(0), c(0) {}
39 bj 32
 
33
			QEM(const CGLA::Vec3d& p0, const CGLA::Vec3d& n0, double w=1.0f):
34
				A(direct_product(n0,n0) * w), 
35
				b(-n0*dot(n0,p0) * w), 
36
				c(dot(p0,n0)*dot(p0,n0) * w) {}
37
 
38
 
39
			void operator+=(const QEM& q)
40
				{
41
					A += q.A;
42
					b += q.b;
43
					c += q.c;
44
				}
45
 
46
			float error(const CGLA::Vec3f& _p) const
47
				{
48
					CGLA::Vec3d p(_p);
49
					return dot(p,A*p) + 2*dot(b,p)+ c;
50
				}
51
 
52
			double determinant() const
53
				{
54
					return CGLA::determinant(A);
55
				}
56
 
57
			const CGLA::Vec3f grad(const CGLA::Vec3f& _p) const
58
				{
59
					CGLA::Vec3d p(_p);
60
					CGLA::Vec3f g(2*A*p+2*b);
61
					return g;
62
				}
63
 
64
			CGLA::Vec3f opt_pos(double QEM_thresh = 0.005) const;
65
 
66
		};
67
}
68
 
69
namespace GEO = Geometry;
70
 
71
#endif