Subversion Repositories gelsvn

Rev

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

Rev 309 Rev 373
1
#include "CGLA/Vec4f.h"
1
#include "CGLA/Vec4f.h"
2
#include "Triangle.h"
2
#include "Triangle.h"
3
 
3
 
4
 
4
 
5
using namespace std;
5
using namespace std;
6
using namespace CGLA;
6
using namespace CGLA;
7
 
7
 
8
namespace Geometry
8
namespace Geometry
9
{
9
{
10
 
10
 
11
const float EPSILON = 1e-10;
11
const float EPSILON = 1e-10f;
12
 
12
 
13
Triangle::Triangle(const CGLA::Vec3f& _v0, 
13
Triangle::Triangle(const CGLA::Vec3f& _v0, 
14
									 const CGLA::Vec3f& _v1, 
14
									 const CGLA::Vec3f& _v1, 
15
									 const CGLA::Vec3f& _v2,
15
									 const CGLA::Vec3f& _v2,
16
									 
16
									 
17
									 const CGLA::Vec3f& _vn0,
17
									 const CGLA::Vec3f& _vn0,
18
									 const CGLA::Vec3f& _vn1,
18
									 const CGLA::Vec3f& _vn1,
19
									 const CGLA::Vec3f& _vn2,
19
									 const CGLA::Vec3f& _vn2,
20
									 
20
									 
21
									 const CGLA::Vec3f& _en0,
21
									 const CGLA::Vec3f& _en0,
22
									 const CGLA::Vec3f& _en1,
22
									 const CGLA::Vec3f& _en1,
23
									 const CGLA::Vec3f& _en2)
23
									 const CGLA::Vec3f& _en2)
24
{
24
{
25
	vert[0]  =_v0;
25
	vert[0]  =_v0;
26
	vert[1]  =_v1;
26
	vert[1]  =_v1;
27
	vert[2]  =_v2;
27
	vert[2]  =_v2;
28
 
28
 
29
#ifdef COMPUTE_SIGN
29
#ifdef COMPUTE_SIGN
30
	vert_norm[0] = _vn0;
30
	vert_norm[0] = _vn0;
31
	vert_norm[1] = _vn1;
31
	vert_norm[1] = _vn1;
32
	vert_norm[2] = _vn2;
32
	vert_norm[2] = _vn2;
33
 
33
 
34
	edge_norm[0] = _en0;
34
	edge_norm[0] = _en0;
35
	edge_norm[1] = _en1;
35
	edge_norm[1] = _en1;
36
	edge_norm[2] = _en2;
36
	edge_norm[2] = _en2;
37
#endif
37
#endif
38
 
38
 
39
	face_norm = normalize(cross(vert[1]-vert[0], vert[2]-vert[0]));
39
	face_norm = normalize(cross(vert[1]-vert[0], vert[2]-vert[0]));
40
	for(int i=0;i<3;++i)
40
	for(int i=0;i<3;++i)
41
		{
41
		{
42
			int j= (i+1)%3;
42
			int j= (i+1)%3;
43
			edge[i] = vert[j]-vert[i];
43
			edge[i] = vert[j]-vert[i];
44
			tri_plane_edge_norm[i] = cross(face_norm, edge[i]);
44
			tri_plane_edge_norm[i] = cross(face_norm, edge[i]);
45
			edge_len[i] = edge[i].length();
45
			edge_len[i] = edge[i].length();
46
		}
46
		}
47
}
47
}
48
 
48
 
49
 
49
 
50
// Moellers method
50
// Moellers method
51
bool Triangle::intersect(const CGLA::Vec3f& orig,
51
bool Triangle::intersect(const CGLA::Vec3f& orig,
52
												 const CGLA::Vec3f& dir, float&t) const
52
												 const CGLA::Vec3f& dir, float&t) const
53
{
53
{
54
	Vec3f tvec, pvec, qvec;
54
	Vec3f tvec, pvec, qvec;
55
	float det,inv_det;
55
	float det,inv_det;
56
 
56
 
57
   /* begin calculating determinant - also used to calculate U parameter */
57
   /* begin calculating determinant - also used to calculate U parameter */
58
   pvec = cross(dir, -edge[2]);
58
   pvec = cross(dir, -edge[2]);
59
 
59
 
60
   /* if determinant is near zero, ray lies in plane of triangle */
60
   /* if determinant is near zero, ray lies in plane of triangle */
61
   det = dot(edge[0], pvec);
61
   det = dot(edge[0], pvec);
62
 
62
 
63
   if (det > -EPSILON && det < EPSILON)
63
   if (det > -EPSILON && det < EPSILON)
64
     return 0;
64
     return 0;
65
   inv_det = 1.0 / det;
65
   inv_det = 1.0 / det;
66
 
66
 
67
   /* calculate distance from v0 to ray origin */
67
   /* calculate distance from v0 to ray origin */
68
   tvec =  orig - vert[0];
68
   tvec =  orig - vert[0];
69
 
69
 
70
   /* calculate U parameter and test bounds */
70
   /* calculate U parameter and test bounds */
71
   float u = dot(tvec, pvec) * inv_det;
71
   float u = dot(tvec, pvec) * inv_det;
72
   if (u < 0.0 || u > 1.0)
72
   if (u < 0.0 || u > 1.0)
73
     return false;
73
     return false;
74
 
74
 
75
   /* prepare to test V parameter */
75
   /* prepare to test V parameter */
76
   qvec = cross(tvec, edge[0]);
76
   qvec = cross(tvec, edge[0]);
77
 
77
 
78
   /* calculate V parameter and test bounds */
78
   /* calculate V parameter and test bounds */
79
   float v = dot(dir, qvec) * inv_det;
79
   float v = dot(dir, qvec) * inv_det;
80
   if (v < 0.0 || u + v > 1.0)
80
   if (v < 0.0 || u + v > 1.0)
81
     return false;
81
     return false;
82
 
82
 
83
   /* calculate t, ray intersects triangle */
83
   /* calculate t, ray intersects triangle */
84
   t = dot(-edge[2], qvec) * inv_det;
84
   t = dot(-edge[2], qvec) * inv_det;
85
 
85
 
86
   return true;
86
   return true;
87
}
87
}
88
 
88
 
89
 
89
 
90
 
90
 
91
 
91
 
92
bool Triangle::signed_distance(const Vec3f& p, 
92
bool Triangle::signed_distance(const Vec3f& p, 
93
															 float& sq_dist, float& sgn) const
93
															 float& sq_dist, float& sgn) const
94
{
94
{
95
	int vertex_scores[3] = {0,0,0};
95
	int vertex_scores[3] = {0,0,0};
96
	Vec3f closest_pnt, normal;
96
	Vec3f closest_pnt, normal;
97
	int idx_0;
97
	int idx_0;
98
 
98
 
99
	// Loop over all three edges.
99
	// Loop over all three edges.
100
	for(idx_0=0; idx_0<3; ++idx_0)
100
	for(idx_0=0; idx_0<3; ++idx_0)
101
		{
101
		{
102
			const int idx_1 = (idx_0+1) % 3;
102
			const int idx_1 = (idx_0+1) % 3;
103
			const Vec3f dir_3d = edge[idx_0]/edge_len[idx_0];
103
			const Vec3f dir_3d = edge[idx_0]/edge_len[idx_0];
104
			const float t = dot(p - vert[idx_0], dir_3d);
104
			const float t = dot(p - vert[idx_0], dir_3d);
105
			if(t <= 0)
105
			if(t <= 0)
106
				{
106
				{
107
					++vertex_scores[idx_0];
107
					++vertex_scores[idx_0];
108
					if(vertex_scores[idx_0] == 2)
108
					if(vertex_scores[idx_0] == 2)
109
						{
109
						{
110
							closest_pnt = vert[idx_0];
110
							closest_pnt = vert[idx_0];
111
#ifdef COMPUTE_SIGN
111
#ifdef COMPUTE_SIGN
112
							normal = vert_norm[idx_0];
112
							normal = vert_norm[idx_0];
113
#endif
113
#endif
114
							break;
114
							break;
115
						}
115
						}
116
				}
116
				}
117
			else if(t >= edge_len[idx_0])
117
			else if(t >= edge_len[idx_0])
118
				{
118
				{
119
					++vertex_scores[idx_1];
119
					++vertex_scores[idx_1];
120
					if(vertex_scores[idx_1] == 2)
120
					if(vertex_scores[idx_1] == 2)
121
						{
121
						{
122
							closest_pnt = vert[idx_1];
122
							closest_pnt = vert[idx_1];
123
#ifdef COMPUTE_SIGN
123
#ifdef COMPUTE_SIGN
124
							normal = vert_norm[idx_1];
124
							normal = vert_norm[idx_1];
125
#endif
125
#endif
126
							break;
126
							break;
127
						}
127
						}
128
				}
128
				}
129
			else if(dot(tri_plane_edge_norm[idx_0], p-vert[idx_0]) <=0)
129
			else if(dot(tri_plane_edge_norm[idx_0], p-vert[idx_0]) <=0)
130
				{
130
				{
131
					closest_pnt=vert[idx_0]+t*dir_3d;
131
					closest_pnt=vert[idx_0]+t*dir_3d;
132
#ifdef COMPUTE_SIGN
132
#ifdef COMPUTE_SIGN
133
					normal = edge_norm[idx_0];
133
					normal = edge_norm[idx_0];
134
#endif
134
#endif
135
					break;
135
					break;
136
				}
136
				}
137
		}
137
		}
138
	if(idx_0 == 3)
138
	if(idx_0 == 3)
139
		{
139
		{
140
			closest_pnt = p - face_norm*(dot(p-vert[0],face_norm));
140
			closest_pnt = p - face_norm*(dot(p-vert[0],face_norm));
141
#ifdef COMPUTE_SIGN
141
#ifdef COMPUTE_SIGN
142
			normal = face_norm;
142
			normal = face_norm;
143
#endif
143
#endif
144
		}
144
		}
145
	sq_dist = sqr_length(p-closest_pnt);
145
	sq_dist = sqr_length(p-closest_pnt);
146
	
146
	
147
#ifdef COMPUTE_SIGN
147
#ifdef COMPUTE_SIGN
148
	// Compute dot product with angle weighted normal, and
148
	// Compute dot product with angle weighted normal, and
149
	// assign the sign based on the result.
149
	// assign the sign based on the result.
150
	if(dot(normal, p-closest_pnt) >=0)
150
	if(dot(normal, p-closest_pnt) >=0)
151
		sgn = 1.0f;
151
		sgn = 1.0f;
152
	else
152
	else
153
		sgn = -1.0f;
153
		sgn = -1.0f;
154
#else
154
#else
155
	sgn = 1.0f;
155
	sgn = 1.0f;
156
#endif
156
#endif
157
 
157
 
158
	return true;
158
	return true;
159
}
159
}
160
 
160
 
161
}
161
}
162
 
162