Subversion Repositories gelsvn

Rev

Rev 89 | Rev 214 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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