Subversion Repositories gelsvn

Rev

Rev 324 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
595 jab 1
/* ----------------------------------------------------------------------- *
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
5
 * ----------------------------------------------------------------------- */
6
 
304 jab 7
// Created by Bent Dalgaard Larsen, Nov, 2003
8
 
9
#include "Ray.h"
10
#include "BBox.h"
11
 
12
using namespace std;
13
using namespace CGLA;
14
 
15
#define my_min(x,y) (x<y?x:y)
16
#define my_max(x,y) (x>y?x:y)
17
 
18
namespace Geometry
19
{
20
  void BBox::intersect_min_max(Ray &ray, double &t_min, double &t_max) const 
21
  {
22
    double tx1 = (min_corner[0]-ray.origin[0])/ray.direction[0];
23
    double ty1 = (min_corner[1]-ray.origin[1])/ray.direction[1];
24
    double tz1 = (min_corner[2]-ray.origin[2])/ray.direction[2];
25
 
26
    double tx2 = (max_corner[0]-ray.origin[0])/ray.direction[0];
27
    double ty2 = (max_corner[1]-ray.origin[1])/ray.direction[1];
28
    double tz2 = (max_corner[2]-ray.origin[2])/ray.direction[2];
29
 
30
    t_min = my_max(my_min(tx1, tx2), my_max(my_min(ty1, ty2), my_min(tz1,tz2)));
31
    t_max = my_min(my_max(tx1, tx2), my_min(my_max(ty1, ty2), my_max(tz1,tz2)));
32
  }
33
 
34
  bool BBox::intersect(Ray &ray) 
35
  {
36
    double t_min, t_max;
37
    intersect_min_max(ray, t_min, t_max);
38
 
39
    if(t_min <= t_max && t_min < ray.dist && t_min > f_eps) 
40
      return true;
41
    else
42
      return false;
43
  }
44
 
45
  bool BBox::ray_triangle(Vec3f &ray_start, Vec3f &ray_end, ISectTri &tri) 
46
  {
47
    Vec3f origin = ray_start;
48
    Vec3f direction = ray_end - ray_start;
49
    double dist = direction.length();
50
    direction.normalize();
51
 
52
    Vec3f p;
53
    Vec3f q;
54
    Vec3f s;
55
    double a, f, u, v, t;
56
 
57
    p = cross(direction, Vec3f(tri.edge1));
58
    a = dot(Vec3f(tri.edge0),p);
59
    if (a>-f_eps && a<f_eps)
60
      return false;
61
    f = 1/a;
62
    s = origin - Vec3f(tri.point0);
63
    u = f*dot(s,p);
64
    if (u<0.0 || u>1.0)
65
      return false;
66
    q = cross(s, Vec3f(tri.edge0));
67
    v = f * dot(direction, q);  
68
    if (v<0.0 || u+v>1.0)
69
      return false;
70
    t = f*dot(Vec3f(tri.edge1), q);
71
    if (t<0)
72
      return false;
73
//	if (t<eps)
74
//		return false;
75
    if (t>dist)
76
      return false;
77
 
78
    return true;
79
  }
80
 
81
 
82
  bool BBox::intersect_edge_box(Vec3f &ray_start, Vec3f &ray_end) 
83
  {
84
    Ray test_ray;
85
    test_ray.origin = ray_start;
86
    test_ray.direction = ray_end - ray_start;
87
    test_ray.dist = test_ray.direction.length();
88
    test_ray.direction.normalize();
89
    return intersect(test_ray);
90
  }
91
 
92
  bool BBox::in_interval(double min_limit, double test_value, double max_limit) 
93
  {
94
    if (min_limit<=test_value && test_value<=max_limit)
95
      return true;
96
    return false;
97
  }
98
/*
99
 
100
bool BBox::intersect_triangle_left(ISectTri &tri, double plane, int axis) {
101
	if (tri.point0[axis]<=plane)
102
		return true;
103
	if (tri.point1[axis]<=plane)
104
		return true;
105
	if (tri.point2[axis]<=plane)
106
		return true;
107
	return false;
108
}
109
 
110
bool BBox::intersect_triangle_right(ISectTri &tri, double plane, int axis) {
111
	if (tri.point0[axis]>=plane)
112
		return true;
113
	if (tri.point1[axis]>=plane)
114
		return true;
115
	if (tri.point2[axis]>=plane)
116
		return true;
117
	return false;
118
}
119
*/
120
  bool BBox::intersect_triangle(ISectTri &tri) 
121
  {
313 jrf 122
    Vec3f tmin_corner = min_corner - Vec3f(f_eps);
123
    Vec3f tmax_corner = max_corner + Vec3f(f_eps);
304 jab 124
 
125
    // Vertex in box test:
126
    // If any of the triangle vertices are inside the box then 
127
    // the triangle intersects the box
128
    if (in_interval(tmin_corner[0],tri.point0[0],tmax_corner[0]) && in_interval(tmin_corner[1],tri.point0[1],tmax_corner[1]) && in_interval(tmin_corner[2],tri.point0[2],tmax_corner[2]))
129
      return true;
130
    if (in_interval(tmin_corner[0],tri.point1[0],tmax_corner[0]) && in_interval(tmin_corner[1],tri.point1[1],tmax_corner[1]) && in_interval(tmin_corner[2],tri.point1[2],tmax_corner[2]))
131
      return true;
132
    if (in_interval(tmin_corner[0],tri.point2[0],tmax_corner[0]) && in_interval(tmin_corner[1],tri.point2[1],tmax_corner[1]) && in_interval(tmin_corner[2],tri.point2[2],tmax_corner[2]))
133
      return true;
134
 
135
    // Triangle outside box test:
136
    // If all of the triangle vertices are outside one of the planes 
137
    // defining the sides of the box then the triangle can be trivially
138
    // rejected as outside
139
    int i;
140
    for(i=0;i<3;i++)
141
      if (tri.point0[i]<tmin_corner[i] && tri.point1[i]<tmin_corner[i] && tri.point2[i]<tmin_corner[i])
142
	return false;
143
 
144
    for(i=0;i<3;i++)
145
      if (tri.point0[i]>tmax_corner[i] && tri.point1[i]>tmax_corner[i] && tri.point2[i]>tmax_corner[i])
146
	return false;
147
 
148
    // Triangle edges - box intersection test
149
    if (intersect_edge_box(tri.point0, tri.point1))
150
      return true;
151
 
152
    if (intersect_edge_box(tri.point1, tri.point2))
153
      return true;
154
 
155
    if (intersect_edge_box(tri.point2, tri.point0))
156
      return true;
157
 
158
    // Box diagonal - triangle intersection test, 4 tests in total
159
    Vec3f corner0;
160
    Vec3f corner1;
161
 
162
    Vec3f tmin_corner_e = tmin_corner;
163
    Vec3f tmax_corner_e = tmax_corner;
164
 
165
    corner0.set(tmin_corner_e[0],tmin_corner_e[1],tmin_corner_e[2]);
166
    corner1.set(tmax_corner_e[0],tmax_corner_e[1],tmax_corner_e[2]);
167
    if (ray_triangle(corner0, corner1, tri))
168
      return true;
169
 
170
    corner0.set(tmax_corner_e[0],tmin_corner_e[1],tmin_corner_e[2]);
171
    corner1.set(tmin_corner_e[0],tmax_corner_e[1],tmax_corner_e[2]);
172
    if (ray_triangle(corner0, corner1, tri))
173
      return true;
174
 
175
    corner0.set(tmin_corner_e[0],tmax_corner_e[1],tmin_corner_e[2]);
176
    corner1.set(tmax_corner_e[0],tmin_corner_e[1],tmax_corner_e[2]);
177
    if (ray_triangle(corner0, corner1, tri))
178
      return true;
179
 
180
    corner0.set(tmin_corner_e[0],tmin_corner_e[1],tmax_corner_e[2]);
181
    corner1.set(tmax_corner_e[0],tmax_corner_e[1],tmin_corner_e[2]);
182
    if (ray_triangle(corner0, corner1, tri))
183
      return true;
184
 
185
    // None succeded 
186
    return false;
187
  }
188
 
189
  void BBox::compute_bbox(vector<ISectTri> &isectmesh) 
190
  {
191
    min_corner.set(CGLA::BIG,CGLA::BIG,CGLA::BIG);
192
    max_corner.set(-CGLA::BIG,-CGLA::BIG,-CGLA::BIG);
193
 
194
    for(unsigned int i=0;i<isectmesh.size(); ++i) 
195
    {
196
      const ISectTri& tri = isectmesh[i];
197
      for(int j=0;j<3;j++) {
198
					if (min_corner[j]>tri.point0[j])
199
							min_corner[j]=tri.point0[j];
200
					if (min_corner[j]>tri.point1[j])
201
							min_corner[j]=tri.point1[j];
202
					if (min_corner[j]>tri.point2[j])
203
							min_corner[j]=tri.point2[j];
204
					if (max_corner[j]<tri.point0[j])
205
							max_corner[j]=tri.point0[j];
206
					if (max_corner[j]<tri.point1[j])
207
							max_corner[j]=tri.point1[j];
208
					if (max_corner[j]<tri.point2[j])
209
							max_corner[j]=tri.point2[j];
210
      }
211
    }
212
  }
213
 
214
  double BBox::area() 
215
  {
216
    Vec3f size = max_corner - min_corner;
217
    return size[0]*size[1]*2 + 
218
      size[1]*size[2]*2 + 
219
      size[0]*size[2]*2; 
220
  }
221
}
222