Subversion Repositories gelsvn

Rev

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

Rev 311 Rev 313
1
#include "CGLA/Vec3d.h"
1
#include "CGLA/Vec3d.h"
2
#include "Geometry/TriMesh.h"
2
#include "Geometry/TriMesh.h"
3
 
3
 
4
#include "BSPTree.h"
4
#include "BSPTree.h"
5
 
5
 
6
using namespace std;
6
using namespace std;
7
using namespace CGLA;
7
using namespace CGLA;
8
 
8
 
9
namespace Geometry
9
namespace Geometry
10
{
10
{
11
  int BSPTree::node_calls;
11
  int BSPTree::node_calls;
12
  int BSPTree::tri_calls;
12
  int BSPTree::tri_calls;
13
 
13
 
14
  void create_tri_accel(Vec3f A, Vec3f B, Vec3f C, TriAccel &tri_accel) 
14
  void create_tri_accel(Vec3f A, Vec3f B, Vec3f C, TriAccel &tri_accel) 
15
  {
15
  {
16
    Vec3f N = normalize(cross(B-A, C-A));
16
    Vec3f N = normalize(cross(B-A, C-A));
17
    // Find projection dir
17
    // Find projection dir
18
    int k,u,v;
18
    int k,u,v;
19
    if (fabs(N[0]) > fabs(N[1]))
19
    if (fabs(N[0]) > fabs(N[1]))
20
      if (fabs(N[0]) > fabs(N[2])) 
20
      if (fabs(N[0]) > fabs(N[2])) 
21
	k = 0; /* X */ 
21
	k = 0; /* X */ 
22
      else 
22
      else 
23
	k=2;   /* Z */
23
	k=2;   /* Z */
24
    else
24
    else
25
      if (fabs(N[1]) > fabs(N[2])) 
25
      if (fabs(N[1]) > fabs(N[2])) 
26
	k = 1; /* Y */ 
26
	k = 1; /* Y */ 
27
      else 
27
      else 
28
	k=2;   /* Z */
28
	k=2;   /* Z */
29
    
29
    
30
    u = (k+1)% 3; 
30
    u = (k+1)% 3; 
31
    v = (k+2)% 3;
31
    v = (k+2)% 3;
32
 
32
 
33
    Vec3f b = C - A;
33
    Vec3f b = C - A;
34
    Vec3f c = B - A;
34
    Vec3f c = B - A;
35
	
35
	
36
    double div = (b[u]*c[v]-b[v]*c[u]);
36
    double div = (b[u]*c[v]-b[v]*c[u]);
37
 
37
 
38
    tri_accel.n_u = N[u]/N[k];
38
    tri_accel.n_u = N[u]/N[k];
39
    tri_accel.n_v = N[v]/N[k];
39
    tri_accel.n_v = N[v]/N[k];
40
    tri_accel.n_d = dot(A, N/N[k]);
40
    tri_accel.n_d = dot(A, N/N[k]);
41
    tri_accel.k = k;
41
    tri_accel.k = k;
42
    
42
    
43
    tri_accel.b_nu = -b[v]/div;
43
    tri_accel.b_nu = -b[v]/div;
44
    tri_accel.b_nv = b[u]/div;
44
    tri_accel.b_nv = b[u]/div;
45
    tri_accel.b_d = (b[v]*A[u]-b[u]*A[v])/div;
45
    tri_accel.b_d = (b[v]*A[u]-b[u]*A[v])/div;
46
    
46
    
47
    tri_accel.c_nu = c[v]/div;
47
    tri_accel.c_nu = c[v]/div;
48
    tri_accel.c_nv = -c[u]/div;
48
    tri_accel.c_nv = -c[u]/div;
49
    tri_accel.c_d = (c[u]*A[v]-c[v]*A[u])/div;
49
    tri_accel.c_d = (c[u]*A[v]-c[v]*A[u])/div;
50
  }
50
  }
51
  
51
  
52
  // Most of this file is a direct copy from Henrik's notes
52
  // Most of this file is a direct copy from Henrik's notes
53
  BSPTree::BSPTree() 
53
  BSPTree::BSPTree() 
54
  {
54
  {
55
    root=0;
55
    root=0;
56
    b_is_build = false;
56
    b_is_build = false;
57
  }
57
  }
58
 
58
 
59
  BSPTree::~BSPTree() 
59
  BSPTree::~BSPTree() 
60
  {
60
  {
61
    clear();		
61
    clear();		
62
  }
62
  }
63
 
63
 
64
  void BSPTree::clear() 
64
  void BSPTree::clear() 
65
  {
65
  {
66
    if (root!=0) 
66
    if (root!=0) 
67
    {
67
    {
68
      delete_node(root);
68
      delete_node(root);
69
      root=0;
69
      root=0;
70
      b_is_build=false;
70
      b_is_build=false;
71
    }
71
    }
72
    isecttris.clear();
72
    isecttris.clear();
73
    all_objects.clear();
73
    all_objects.clear();
74
    all_triaccel.clear();
74
    all_triaccel.clear();
75
  }
75
  }
76
 
76
 
77
  void BSPTree::delete_node(BSPNode *node) 
77
  void BSPTree::delete_node(BSPNode *node) 
78
  {
78
  {
79
    if (node->left!=0)
79
    if (node->left!=0)
80
      delete_node(node->left);
80
      delete_node(node->left);
81
    if (node->right!=0)
81
    if (node->right!=0)
82
      delete_node(node->right);
82
      delete_node(node->right);
83
    delete node;
83
    delete node;
84
  }
84
  }
85
 
85
 
86
  void BSPTree::subdivide_node(BSPNode &node, BBox &bbox, 
86
  void BSPTree::subdivide_node(BSPNode &node, BBox &bbox, 
87
			       unsigned int level, 
87
			       unsigned int level, 
88
			       vector<ISectTri*>& objects, 
88
			       vector<ISectTri*>& objects, 
89
			       vector<TriAccel*>& tri_objects) 
89
			       vector<TriAccel*>& tri_objects) 
90
  {
90
  {
91
    const int TESTS = 2;
91
    const int TESTS = 2;
92
    
92
    
93
    if (objects.size()<=max_objects || level==max_level) 
93
    if (objects.size()<=max_objects || level==max_level) 
94
    {
94
    {
95
      node.axis_leaf = 4; // Means that this is a leaf
95
      node.axis_leaf = 4; // Means that this is a leaf
96
      node.plane = 0;
96
      node.plane = 0;
97
      node.left = 0;
97
      node.left = 0;
98
      node.right = 0;
98
      node.right = 0;
99
      node.id = all_objects.size();
99
      node.id = all_objects.size();
100
      node.count = objects.size();
100
      node.count = objects.size();
101
		
101
		
102
      for(unsigned int i = 0; i < objects.size(); ++i) 
102
      for(unsigned int i = 0; i < objects.size(); ++i) 
103
      {
103
      {
104
	all_objects.push_back(objects[i]);
104
	all_objects.push_back(objects[i]);
105
	all_triaccel.push_back(tri_objects[i]);
105
	all_triaccel.push_back(tri_objects[i]);
106
      }  
106
      }  
107
    } 
107
    } 
108
    else 
108
    else 
109
    {
109
    {
110
      bool right_zero=false;
110
      bool right_zero=false;
111
      bool left_zero=false;
111
      bool left_zero=false;
112
      unsigned int i;
112
      unsigned int i;
113
      BSPNode* left_node  = new BSPNode();
113
      BSPNode* left_node  = new BSPNode();
114
      BSPNode* right_node = new BSPNode();
114
      BSPNode* right_node = new BSPNode();
115
      vector<ISectTri*> left_objects;
115
      vector<ISectTri*> left_objects;
116
      vector<ISectTri*> right_objects;
116
      vector<ISectTri*> right_objects;
117
      vector<TriAccel*> tri_left_objects;
117
      vector<TriAccel*> tri_left_objects;
118
      vector<TriAccel*> tri_right_objects;
118
      vector<TriAccel*> tri_right_objects;
119
      
119
      
120
      node.left = left_node;
120
      node.left = left_node;
121
      node.right = right_node;
121
      node.right = right_node;
122
      //node.is_leaf = false;
-
 
123
 
122
 
124
      int new_axis=-1;
123
      int new_axis=-1;
125
      double min_cost=CGLA::BIG;
124
      double min_cost=CGLA::BIG;
126
      int new_pos = -1;      
125
      int new_pos = -1;      
127
/*
126
 
128
       float max_size=0;
-
 
129
       for(i=0;i<3;i++) {
-
 
130
         if (bbox.max_corner[i]-bbox.min_corner[i]>max_size) {
-
 
131
	   max_size = bbox.max_corner[i]-bbox.min_corner[i];
-
 
132
	   new_axis=i;
-
 
133
	   new_pos = 1;
-
 
134
	 }
-
 
135
       }
-
 
136
*/
-
 
137
      for(i=0;i<3;i++) 
127
	  for(i=0;i<3;i++) 
138
      {
128
      {
139
	for(int k=1;k<TESTS;k++) 
129
	for(int k=1;k<TESTS;k++) 
140
	{
130
	{
141
	  BBox left_bbox = bbox;
131
	  BBox left_bbox = bbox;
142
	  BBox right_bbox = bbox;
132
	  BBox right_bbox = bbox;
143
				
133
				
144
	  double center = (bbox.max_corner[i]- bbox.min_corner[i])*(double)k/(double)TESTS + bbox.min_corner[i];
134
	  double center = (bbox.max_corner[i]- bbox.min_corner[i])*(double)k/(double)TESTS + bbox.min_corner[i];
145
	  node.plane = center;
135
	  node.plane = center;
146
	  
136
	  
147
	  left_bbox.max_corner[i] = center; 
137
	  left_bbox.max_corner[i] = center; 
148
	  right_bbox.min_corner[i] = center; 
138
	  right_bbox.min_corner[i] = center; 
149
 
139
 
150
	  // Try putting the triangles in the left and right boxes
140
	  // Try putting the triangles in the left and right boxes
151
	  int left_count = 0;
141
	  int left_count = 0;
152
	  int right_count = 0;
142
	  int right_count = 0;
153
	  for(unsigned int j=0;j<objects.size();j++) 
143
	  for(unsigned int j=0;j<objects.size();j++) 
154
	  {
144
	  {
155
	    ISectTri* tri = objects[j];
145
	    ISectTri* tri = objects[j];
156
	    left_count += left_bbox.intersect_triangle(*tri);
146
	    left_count += left_bbox.intersect_triangle(*tri);
157
	    right_count += right_bbox.intersect_triangle(*tri);
147
	    right_count += right_bbox.intersect_triangle(*tri);
158
	  }
148
	  }
159
	  
149
 
-
 
150
	  //double len = bbox.max_corner[i] - bbox.min_corner[i];
160
	  if (left_count*left_bbox.area() + right_count*right_bbox.area() < min_cost) 
151
	  double cost = left_count*left_bbox.area() + right_count*right_bbox.area(); // - len*len;
-
 
152
	  if(cost < min_cost) 
161
	  {
153
	  {
162
	    min_cost = left_count*left_bbox.area() + right_count*right_bbox.area();
154
	    min_cost = cost;
163
	    new_axis = i;
155
	    new_axis = i;
164
	    new_pos = k;
156
	    new_pos = k;
165
	    right_zero = (right_count==0);
157
	    right_zero = (right_count==0);
166
	    left_zero = (left_count==0);
158
	    left_zero = (left_count==0);
167
	  }
159
	  }
168
	}
160
	}
169
      }
161
      }
170
      node.axis_leaf = new_axis;
162
      node.axis_leaf = new_axis;
171
      //	node.axis_leaf = level %3;
-
 
172
      left_node->axis_leaf = static_cast<unsigned char>(-1); //(node.axis + 1)%3;
163
      left_node->axis_leaf = static_cast<unsigned char>(-1); 
173
      right_node->axis_leaf = static_cast<unsigned char>(-1); //(node.axis + 1)%3;
164
      right_node->axis_leaf = static_cast<unsigned char>(-1); 
-
 
165
 
174
      // Now chose the right splitting plane
166
      // Now chose the right splitting plane
175
      BBox left_bbox = bbox;
167
      BBox left_bbox = bbox;
176
      BBox right_bbox = bbox;
168
      BBox right_bbox = bbox;
177
 
169
 
178
      double size = bbox.max_corner[node.axis_leaf]- bbox.min_corner[node.axis_leaf];
170
      double size = bbox.max_corner[node.axis_leaf]- bbox.min_corner[node.axis_leaf];
179
      double center = size*(double)new_pos/(double)TESTS + bbox.min_corner[node.axis_leaf];
171
      double center = size*(double)new_pos/(double)TESTS + bbox.min_corner[node.axis_leaf];
180
      double diff = f_eps < size/8.0 ? f_eps : size/8.0;
172
      double diff = f_eps < size/8.0 ? f_eps : size/8.0;
181
      
173
      
182
      // This doesn't help that much
-
 
183
      if (left_zero) 
174
      if (left_zero) 
184
      {
175
      {
185
//			cout << "Old left center: " << center;
-
 
186
	// Find min position af alle trekanter og placer center der
176
	// Find min position of all triangle vertices and place the center there
187
	center = bbox.max_corner[node.axis_leaf];
177
	center = bbox.max_corner[node.axis_leaf];
188
	for(unsigned int j=0;j<objects.size();j++) 
178
	for(unsigned int j=0;j<objects.size();j++) 
189
	{
179
	{
190
	  ISectTri* tri = objects[j];
180
	  ISectTri* tri = objects[j];
191
	  if (tri->point0[node.axis_leaf]<center)
181
	  if (tri->point0[node.axis_leaf]<center)
192
	    center=tri->point0[node.axis_leaf];
182
	    center=tri->point0[node.axis_leaf];
193
	  if (tri->point1[node.axis_leaf]<center)
183
	  if (tri->point1[node.axis_leaf]<center)
194
	    center=tri->point1[node.axis_leaf];
184
	    center=tri->point1[node.axis_leaf];
195
	  if (tri->point2[node.axis_leaf]<center)
185
	  if (tri->point2[node.axis_leaf]<center)
196
	    center=tri->point2[node.axis_leaf];
186
	    center=tri->point2[node.axis_leaf];
197
	}
187
	}
198
	center -= diff;
188
	center -= diff;
199
//			cout << "new left center: " << center << endl;
-
 
200
      }
189
      }
201
      if (right_zero) 
190
      if (right_zero) 
202
      {
191
      {
203
//			cout << "Old right center: " << center;
-
 
204
	// Find max position af alle trekanter og placer center der
192
	// Find max position of all triangle vertices and place the center there
205
	center = bbox.min_corner[node.axis_leaf];
193
	center = bbox.min_corner[node.axis_leaf];
206
	for(unsigned int j=0;j<objects.size();j++) 
194
	for(unsigned int j=0;j<objects.size();j++) 
207
	{
195
	{
208
	  ISectTri* tri = objects[j];
196
	  ISectTri* tri = objects[j];
209
	  if (tri->point0[node.axis_leaf]>center)
197
	  if (tri->point0[node.axis_leaf]>center)
210
	    center=tri->point0[node.axis_leaf];
198
	    center=tri->point0[node.axis_leaf];
211
	  if (tri->point1[node.axis_leaf]>center)
199
	  if (tri->point1[node.axis_leaf]>center)
212
	    center=tri->point1[node.axis_leaf];
200
	    center=tri->point1[node.axis_leaf];
213
	  if (tri->point2[node.axis_leaf]>center)
201
	  if (tri->point2[node.axis_leaf]>center)
214
	    center=tri->point2[node.axis_leaf];
202
	    center=tri->point2[node.axis_leaf];
215
	}
203
	}
216
	center += diff;
204
	center += diff;
217
//			cout << "new right center: " << center << endl;
-
 
218
      }
205
      }
219
 
206
 
220
      node.plane = center;
207
      node.plane = center;
221
      left_bbox.max_corner[node.axis_leaf] = center; // + f_eps; 
208
      left_bbox.max_corner[node.axis_leaf] = center; 
222
      right_bbox.min_corner[node.axis_leaf] = center; // - f_eps; 
209
      right_bbox.min_corner[node.axis_leaf] = center;  
223
            
210
            
224
      // Now put the triangles in the right and left node
211
      // Now put the triangles in the right and left node
225
      for(i=0;i<objects.size();i++) 
212
      for(i=0;i<objects.size();i++) 
226
      {
213
      {
227
	ISectTri* tri = objects[i];
214
	ISectTri* tri = objects[i];
228
	TriAccel *tri_accel = tri_objects[i];
215
	TriAccel *tri_accel = tri_objects[i];
229
	if (left_bbox.intersect_triangle(*tri)) 
216
	if (left_bbox.intersect_triangle(*tri)) 
230
	{
217
	{
231
	  left_objects.push_back(tri);
218
	  left_objects.push_back(tri);
232
	  tri_left_objects.push_back(tri_accel);
219
	  tri_left_objects.push_back(tri_accel);
233
	}
220
	}
234
	if (right_bbox.intersect_triangle(*tri)) 
221
	if (right_bbox.intersect_triangle(*tri)) 
235
	{
222
	{
236
	  right_objects.push_back(tri);
223
	  right_objects.push_back(tri);
237
	  tri_right_objects.push_back(tri_accel);
224
	  tri_right_objects.push_back(tri_accel);
238
	}
225
	}
239
      }
226
      }
240
    //if (left_zero||right_zero)
227
    //if (left_zero||right_zero)
241
	//  cout << left_objects.size() << "," << right_objects.size() << "," << level << endl;
228
	//  cout << left_objects.size() << "," << right_objects.size() << "," << level << endl;
242
 
229
 
243
      objects.clear();
230
      objects.clear();
244
      subdivide_node(*left_node , left_bbox , level+1, left_objects, tri_left_objects);
231
      subdivide_node(*left_node , left_bbox , level+1, left_objects, tri_left_objects);
245
      subdivide_node(*right_node, right_bbox, level+1, right_objects, tri_right_objects);
232
      subdivide_node(*right_node, right_bbox, level+1, right_objects, tri_right_objects);
246
    }
233
    }
247
  }
234
  }
248
 
235
 
249
  void BSPTree::init() 
236
  void BSPTree::init() 
250
  {
237
  {
251
    root = new BSPNode();
238
    root = new BSPNode();
252
    bbox.compute_bbox(isecttris);
239
    bbox.compute_bbox(isecttris);
253
    bbox.min_corner-=Vec3f(1.0);
240
    bbox.min_corner-=Vec3f(1.0);
254
    bbox.max_corner+=Vec3f(1.0);
241
    bbox.max_corner+=Vec3f(1.0);
255
  }
242
  }
256
 
243
 
257
  void BSPTree::init(vector<TriMesh*>& _trimesh, 
244
  void BSPTree::init(vector<TriMesh*>& _trimesh, 
258
		     vector<Mat4x4f>& _transforms, 
245
		     vector<Mat4x4f>& _transforms, 
259
		     int _max_objects, int _max_level) 
246
		     int _max_objects, int _max_level) 
260
  {
247
  {
261
    trimesh = _trimesh;
248
    trimesh = _trimesh;
262
    transforms = _transforms;
249
    transforms = _transforms;
263
    for(unsigned int i=0;i<trimesh.size();i++) 
250
    for(unsigned int i=0;i<trimesh.size();i++) 
264
    {
251
    {
265
      TriMesh *mesh = trimesh[i];
252
      TriMesh *mesh = trimesh[i];
266
      // Loop through all triangles and add them to intersection structure
253
      // Loop through all triangles and add them to intersection structure
267
      for(int j=0;j<mesh->geometry.no_faces();j++) 
254
      for(int j=0;j<mesh->geometry.no_faces();j++) 
268
      {
255
      {
269
	Vec3i face = mesh->geometry.face(j);
256
	Vec3i face = mesh->geometry.face(j);
270
	ISectTri new_tri;
257
	ISectTri new_tri;
271
	new_tri.point0 = transforms[i].mul_3D_point(mesh->geometry.vertex(face[0]));
258
	new_tri.point0 = transforms[i].mul_3D_point(mesh->geometry.vertex(face[0]));
272
	new_tri.point1 = transforms[i].mul_3D_point(mesh->geometry.vertex(face[1]));
259
	new_tri.point1 = transforms[i].mul_3D_point(mesh->geometry.vertex(face[1]));
273
	new_tri.point2 = transforms[i].mul_3D_point(mesh->geometry.vertex(face[2]));
260
	new_tri.point2 = transforms[i].mul_3D_point(mesh->geometry.vertex(face[2]));
274
	new_tri.edge0 = new_tri.point1 - new_tri.point0;
261
	new_tri.edge0 = new_tri.point1 - new_tri.point0;
275
	new_tri.edge1 = new_tri.point2 - new_tri.point0;
262
	new_tri.edge1 = new_tri.point2 - new_tri.point0;
276
	new_tri.mesh_id = i;
263
	new_tri.mesh_id = i;
277
	new_tri.tri_id = j;
264
	new_tri.tri_id = j;
278
	isecttris.push_back(new_tri);
265
	isecttris.push_back(new_tri);
279
	TriAccel ta;
266
	TriAccel ta;
280
	create_tri_accel(new_tri.point0, new_tri.point1, new_tri.point2, ta);
267
	create_tri_accel(new_tri.point0, new_tri.point1, new_tri.point2, ta);
281
	ta.mesh_id = i;
268
	ta.mesh_id = i;
282
	ta.tri_id = j;
269
	ta.tri_id = j;
283
	triaccel.push_back(ta);
270
	triaccel.push_back(ta);
284
      }
271
      }
285
    }
272
    }
286
 
273
 
287
    max_objects = _max_objects;
274
    max_objects = _max_objects;
288
    max_level = _max_level;
275
    max_level = _max_level;
289
    init();
276
    init();
290
  }
277
  }
291
 
278
 
292
  void BSPTree::init(TriMesh* mesh, Mat4x4f transform, 
279
  void BSPTree::init(TriMesh* mesh, Mat4x4f transform, 
293
		     vector<int> &trilist, 
280
		     vector<int> &trilist, 
294
		     int _max_objects, int _max_level) 
281
		     int _max_objects, int _max_level) 
295
  {
282
  {
296
    trimesh.push_back(mesh);
283
    trimesh.push_back(mesh);
297
    transforms.push_back(transform);
284
    transforms.push_back(transform);
298
    // Loop through all triangles and add them to intersection structure
285
    // Loop through all triangles and add them to intersection structure
299
    for(unsigned int j=0;j<trilist.size();j++) 
286
    for(unsigned int j=0;j<trilist.size();j++) 
300
    {
287
    {
301
      Vec3i face = mesh->geometry.face(trilist[j]);
288
      Vec3i face = mesh->geometry.face(trilist[j]);
302
      ISectTri new_tri;
289
      ISectTri new_tri;
303
      new_tri.point0 = transform.mul_3D_point(mesh->geometry.vertex(face[0]));
290
      new_tri.point0 = transform.mul_3D_point(mesh->geometry.vertex(face[0]));
304
      new_tri.point1 = transform.mul_3D_point(mesh->geometry.vertex(face[1]));
291
      new_tri.point1 = transform.mul_3D_point(mesh->geometry.vertex(face[1]));
305
      new_tri.point2 = transform.mul_3D_point(mesh->geometry.vertex(face[2]));
292
      new_tri.point2 = transform.mul_3D_point(mesh->geometry.vertex(face[2]));
306
      new_tri.edge0 = new_tri.point1 - new_tri.point0;
293
      new_tri.edge0 = new_tri.point1 - new_tri.point0;
307
      new_tri.edge1 = new_tri.point2 - new_tri.point0;
294
      new_tri.edge1 = new_tri.point2 - new_tri.point0;
308
      new_tri.mesh_id = 0;
295
      new_tri.mesh_id = 0;
309
      new_tri.tri_id = trilist[j];
296
      new_tri.tri_id = trilist[j];
310
      isecttris.push_back(new_tri);
297
      isecttris.push_back(new_tri);
311
      TriAccel ta;
298
      TriAccel ta;
312
      create_tri_accel(new_tri.point0, new_tri.point1, new_tri.point2, ta);
299
      create_tri_accel(new_tri.point0, new_tri.point1, new_tri.point2, ta);
313
      ta.mesh_id = 0;
300
      ta.mesh_id = 0;
314
      ta.tri_id = trilist[j];
301
      ta.tri_id = trilist[j];
315
      triaccel.push_back(ta);
302
      triaccel.push_back(ta);
316
    }
303
    }
317
 
304
 
318
    max_objects = _max_objects;
305
    max_objects = _max_objects;
319
    max_level = _max_level;
306
    max_level = _max_level;
320
    init();
307
    init();
321
  }
308
  }
322
 
309
 
323
  void BSPTree::build() 
310
  void BSPTree::build() 
324
  {
311
  {
325
    if (!b_is_build) 
312
    if (!b_is_build) 
326
    {
313
    {
327
      vector<ISectTri*> objects;
314
      vector<ISectTri*> objects;
328
      vector<TriAccel*> tri_objects;
315
      vector<TriAccel*> tri_objects;
329
      for(unsigned int i=0;i<isecttris.size();i++) 
316
      for(unsigned int i=0;i<isecttris.size();i++) 
330
      {
317
      {
331
	ISectTri* tri = &isecttris[i];
318
	ISectTri* tri = &isecttris[i];
332
	TriAccel* tri_accel = &triaccel[i];
319
	TriAccel* tri_accel = &triaccel[i];
333
	objects.push_back(tri);
320
	objects.push_back(tri);
334
	tri_objects.push_back(tri_accel);
321
	tri_objects.push_back(tri_accel);
335
      }
322
      }
336
      subdivide_node(*root, bbox, 0, objects, tri_objects);
323
      subdivide_node(*root, bbox, 0, objects, tri_objects);
337
      b_is_build = true;
324
      b_is_build = true;
338
    }
325
    }
339
    isecttris.clear();
326
    isecttris.clear();
340
    all_objects.clear();
327
    all_objects.clear();
341
    make_fast_tree(root);
328
    make_fast_tree(root);
342
  }
329
  }
343
 
330
 
344
  bool BSPTree::is_build() 
331
  bool BSPTree::is_build() 
345
  {
332
  {
346
    return b_is_build;
333
    return b_is_build;
347
  }
334
  }
348
 
335
 
349
  void BSPTree::print(BSPNode *node, int depth) 
336
  void BSPTree::print(BSPNode *node, int depth) 
350
  {
337
  {
351
    if (node==0)
338
    if (node==0)
352
      return;
339
      return;
353
    for(int i=0;i<depth;i++)
340
    for(int i=0;i<depth;i++)
354
      cout << " ";
341
      cout << " ";
355
//	cout << "axis:" << node->axis_leaf << ", count:" << node->objects.size() << ", plane:" << node->plane << ", " << endl;
342
//	cout << "axis:" << node->axis_leaf << ", count:" << node->objects.size() << ", plane:" << node->plane << ", " << endl;
356
    print(node->left, depth+1);
343
    print(node->left, depth+1);
357
    print(node->right, depth+1);
344
    print(node->right, depth+1);
358
  }
345
  }
359
 
346
 
360
  int BSPTree::size(BSPNode *node) 
347
  int BSPTree::size(BSPNode *node) 
361
  {
348
  {
362
    if (node==0)
349
    if (node==0)
363
      return 0;
350
      return 0;
364
    int s = sizeof(BSPNode);
351
    int s = sizeof(BSPNode);
365
    s+= node->count * sizeof(ISectTri);
352
    s+= node->count * sizeof(ISectTri);
366
    s+=size(node->left);
353
    s+=size(node->left);
367
    s+=size(node->right);
354
    s+=size(node->right);
368
    return s;
355
    return s;
369
  }
356
  }
370
 
357
 
371
  int BSPTree::size() 
358
  int BSPTree::size() 
372
  {
359
  {
373
    return size(root);
360
    return size(root);
374
  }
361
  }
375
 
362
 
376
/*__declspec(align(16))*/ static const unsigned int modulo[] = {0,1,2,0,1};
363
/*__declspec(align(16))*/ static const unsigned int modulo[] = {0,1,2,0,1};
377
 
364
 
378
  inline bool intersect2(Ray &ray, const TriAccel &acc, double t_max) 
365
  inline bool intersect2(Ray &ray, const TriAccel &acc, double t_max) 
379
  {
366
  {
380
//	cout << "Acc: " << (int)&acc << " ";
367
//	cout << "Acc: " << (int)&acc << " ";
381
//inline bool Intersect(TriAccel &acc,Ray &ray)
368
//inline bool Intersect(TriAccel &acc,Ray &ray)
382
#define ku modulo[acc.k+1]
369
#define ku modulo[acc.k+1]
383
#define kv modulo[acc.k+2]
370
#define kv modulo[acc.k+2]
384
    // don’t prefetch here, assume data has already been prefetched
371
    // don’t prefetch here, assume data has already been prefetched
385
    // start high-latency division as early as possible
372
    // start high-latency division as early as possible
386
    const double nd = 1.0/((double)ray.direction[acc.k]	+ (double)acc.n_u * (double)ray.direction[ku] + (double)acc.n_v * (double)ray.direction[kv]);
373
    const double nd = 1.0/((double)ray.direction[acc.k]	+ (double)acc.n_u * (double)ray.direction[ku] + (double)acc.n_v * (double)ray.direction[kv]);
387
    const double f = ((double)acc.n_d - (double)ray.origin[acc.k]	- (double)acc.n_u * (double)ray.origin[ku] - (double)acc.n_v * (double)ray.origin[kv]) * nd;
374
    const double f = ((double)acc.n_d - (double)ray.origin[acc.k]	- (double)acc.n_u * (double)ray.origin[ku] - (double)acc.n_v * (double)ray.origin[kv]) * nd;
388
    // check for valid distance.
375
    // check for valid distance.
389
    if (!(t_max > f && f > f_eps)||ray.dist<f) return false;
376
    if (!(t_max > f && f > f_eps)||ray.dist<f) return false;
390
    // compute hitpoint positions on uv plane
377
    // compute hitpoint positions on uv plane
391
    const double hu = (ray.origin[ku] + f * ray.direction[ku]);
378
    const double hu = (ray.origin[ku] + f * ray.direction[ku]);
392
    const double hv = (ray.origin[kv] + f * ray.direction[kv]);
379
    const double hv = (ray.origin[kv] + f * ray.direction[kv]);
393
    // check first barycentric coordinate
380
    // check first barycentric coordinate
394
    const double lambda = (hu * (double)acc.b_nu + hv * (double)acc.b_nv + (double)acc.b_d);
381
    const double lambda = (hu * (double)acc.b_nu + hv * (double)acc.b_nv + (double)acc.b_d);
395
    if (lambda < 0.0) return false;
382
    if (lambda < 0.0) return false;
396
    // check second barycentric coordinate
383
    // check second barycentric coordinate
397
    const double mue = (hu * (double)acc.c_nu + hv * (double)acc.c_nv + (double)acc.c_d);
384
    const double mue = (hu * (double)acc.c_nu + hv * (double)acc.c_nv + (double)acc.c_d);
398
    if (mue < 0.0) return false;
385
    if (mue < 0.0) return false;
399
    // check third barycentric coordinate
386
    // check third barycentric coordinate
400
    if (lambda+mue > 1.0) return false;
387
    if (lambda+mue > 1.0) return false;
401
    // have a valid hitpoint here. store it.
388
    // have a valid hitpoint here. store it.
402
    ray.dist = f;
389
    ray.dist = f;
403
    ray.u = lambda;
390
    ray.u = lambda;
404
    ray.v = mue;
391
    ray.v = mue;
405
    ray.hit_object = (TriMesh*)acc.mesh_id;
392
    ray.hit_object = (TriMesh*)acc.mesh_id;
406
    ray.hit_face_id = acc.tri_id;
393
    ray.hit_face_id = acc.tri_id;
407
    ray.has_hit=true;
394
    ray.has_hit=true;
408
    return true;
395
    return true;
409
  }
396
  }
410
 
397
 
411
  bool BSPTree::intersect_node(Ray &ray, const BSPNode &node, double t_min, double t_max) const 
398
  bool BSPTree::intersect_node(Ray &ray, const BSPNode &node, double t_min, double t_max) const 
412
  {
399
  {
413
//    cout << node.plane << endl;
400
//    cout << node.plane << endl;
414
    node_calls++;
401
    node_calls++;
415
    static bool found;
402
    static bool found;
416
    static int i;
403
    static int i;
417
    
404
    
418
    if (node.axis_leaf==4) 
405
    if (node.axis_leaf==4) 
419
    {
406
    {
420
      found = false; 
407
      found = false; 
421
      for(i=0;i<node.count;i++) 
408
      for(i=0;i<node.count;i++) 
422
      {
409
      {
423
//	const TriAccel* tri2 = all_triaccel[node.id+i];
410
//	const TriAccel* tri2 = all_triaccel[node.id+i];
424
	const ISectTri* tri = all_objects[node.id+i];
411
	const ISectTri* tri = all_objects[node.id+i];
425
//			if (intersect2(ray, *tri2, t_max))  
412
//			if (intersect2(ray, *tri2, t_max))  
426
//				found=true;
413
//				found=true;
427
	if (intersect(ray, *tri, t_max))  
414
	if (intersect(ray, *tri, t_max))  
428
	  found=true;
415
	  found=true;
429
      }
416
      }
430
      if (found)
417
      if (found)
431
	return true;
418
	return true;
432
      else 
419
      else 
433
	return false;
420
	return false;
434
    } 
421
    } 
435
    else 
422
    else 
436
    {
423
    {
437
      BSPNode *near_node;
424
      BSPNode *near_node;
438
      BSPNode *far_node;
425
      BSPNode *far_node;
439
      if (ray.direction[node.axis_leaf]>=0) 
426
      if (ray.direction[node.axis_leaf]>=0) 
440
      {
427
      {
441
	near_node = node.left;
428
	near_node = node.left;
442
	far_node = node.right;
429
	far_node = node.right;
443
      } 
430
      } 
444
      else 
431
      else 
445
      {
432
      {
446
	near_node = node.right;
433
	near_node = node.right;
447
	far_node = node.left;
434
	far_node = node.left;
448
      }
435
      }
449
 
436
 
450
      // In order to avoid instability
437
      // In order to avoid instability
451
      double t;
438
      double t;
452
      if (fabs(ray.direction[node.axis_leaf])<d_eps)
439
      if (fabs(ray.direction[node.axis_leaf])<d_eps)
453
	t = (node.plane - ray.origin[node.axis_leaf])/d_eps;// intersect node plane;
440
	t = (node.plane - ray.origin[node.axis_leaf])/d_eps;// intersect node plane;
454
      else
441
      else
455
	t = (node.plane - ray.origin[node.axis_leaf])/ray.direction[node.axis_leaf];// intersect node plane;
442
	t = (node.plane - ray.origin[node.axis_leaf])/ray.direction[node.axis_leaf];// intersect node plane;
456
      
443
      
457
      if (t>t_max) 
444
      if (t>t_max) 
458
      	return intersect_node(ray, *near_node, t_min, t_max);
445
      	return intersect_node(ray, *near_node, t_min, t_max);
459
      
446
      
460
      else if (t<t_min) 
447
      else if (t<t_min) 
461
	return intersect_node(ray, *far_node, t_min, t_max);
448
	return intersect_node(ray, *far_node, t_min, t_max);
462
      else 
449
      else 
463
      {
450
      {
464
	if (intersect_node(ray, *near_node, t_min, t))
451
	if (intersect_node(ray, *near_node, t_min, t))
465
	  return true;
452
	  return true;
466
	else 
453
	else 
467
	  return intersect_node(ray, *far_node, t, t_max);
454
	  return intersect_node(ray, *far_node, t, t_max);
468
      }
455
      }
469
    }
456
    }
470
  }
457
  }
471
 
458
 
472
  bool BSPTree::intersect(Ray &ray) const 
459
  bool BSPTree::intersect(Ray &ray) const 
473
  {
460
  {
474
    double t_min, t_max;
461
    double t_min, t_max;
475
    bbox.intersect_min_max(ray, t_min, t_max);
462
    bbox.intersect_min_max(ray, t_min, t_max);
476
    if (t_min>t_max)
463
    if (t_min>t_max)
477
      return false;
464
      return false;
478
//    if (!intersect_node(ray, *root, t_min, t_max))
465
//    if (!intersect_node(ray, *root, t_min, t_max))
479
//      return false;
466
//      return false;
480
//	cout << "____" << endl;
467
//	cout << "____" << endl;
481
//	ray.reset();
468
//	ray.reset();
482
//	cout << "Here " << endl;
469
//	cout << "Here " << endl;
483
    intersect_fast_node(ray, &fast_tree[0], t_min, t_max);
470
    intersect_fast_node(ray, &fast_tree[0], t_min, t_max);
484
    if (!ray.has_hit)
471
    if (!ray.has_hit)
485
      return false;
472
      return false;
486
    else 
473
    else 
487
    {
474
    {
488
      // Calculate the normal at the intersection
475
      // Calculate the normal at the intersection
489
      ray.hit_object = trimesh[(int)ray.hit_object];
476
      ray.hit_object = trimesh[(int)ray.hit_object];
490
/*
477
/*
491
      Vec3i face = ray.hit_object->normals.face(ray.hit_face_id);
478
      Vec3i face = ray.hit_object->normals.face(ray.hit_face_id);
492
      Vec3f normal0 = ray.hit_object->normals.vertex(face[0]);
479
      Vec3f normal0 = ray.hit_object->normals.vertex(face[0]);
493
      Vec3f normal1 = ray.hit_object->normals.vertex(face[1]);
480
      Vec3f normal1 = ray.hit_object->normals.vertex(face[1]);
494
      Vec3f normal2 = ray.hit_object->normals.vertex(face[2]);
481
      Vec3f normal2 = ray.hit_object->normals.vertex(face[2]);
495
      ray.hit_normal = normalize(normal0*(1 - ray.u - ray.v)
482
      ray.hit_normal = normalize(normal0*(1 - ray.u - ray.v)
496
				 +normal1*ray.u
483
				 +normal1*ray.u
497
				 +normal2*ray.v);
484
				 +normal2*ray.v);
498
      ray.hit_pos = ray.origin + ray.direction*ray.dist;
485
      ray.hit_pos = ray.origin + ray.direction*ray.dist;
499
*/
486
*/
500
      return true;
487
      return true;
501
    }
488
    }
502
  }
489
  }
503
 
490
 
504
  const int MAX_DEPTH=25;
491
  const int MAX_DEPTH=25;
505
 
492
 
506
  void BSPTree::make_fast_tree(BSPNode *node) 
493
  void BSPTree::make_fast_tree(BSPNode *node) 
507
  {
494
  {
508
    fast_tree.resize(1000000); // 
495
    fast_tree.resize(1000000); // 
509
    FastBSPNode f_node;
496
    FastBSPNode f_node;
510
    fast_tree.push_back(f_node);
497
    fast_tree.push_back(f_node);
511
    push_fast_bsp_node(node,0);
498
    push_fast_bsp_node(node,0);
512
  }
499
  }
513
 
500
 
514
  void BSPTree::push_fast_bsp_node(BSPNode *node, int id) 
501
  void BSPTree::push_fast_bsp_node(BSPNode *node, int id) 
515
  {
502
  {
516
    if (node->axis_leaf==4)  // It is a leaf
503
    if (node->axis_leaf==4)  // It is a leaf
517
    {
504
    {
518
      fast_tree[id].leaf.flagAndOffset = (unsigned int)1<<31 | (unsigned int)(&all_triaccel[node->id]);
505
      fast_tree[id].leaf.flagAndOffset = (unsigned int)1<<31 | (unsigned int)(&all_triaccel[node->id]);
519
      fast_tree[id].leaf.count = node->count;
506
      fast_tree[id].leaf.count = node->count;
520
    } 
507
    } 
521
    else // It is an inner node
508
    else // It is an inner node
522
    { 
509
    { 
523
      FastBSPNode fnode;
510
      FastBSPNode fnode;
524
      int p_l = fast_tree.size();
511
      int p_l = fast_tree.size();
525
      fast_tree.push_back(fnode); // left
512
      fast_tree.push_back(fnode); // left
526
      fast_tree.push_back(fnode); // right
513
      fast_tree.push_back(fnode); // right
527
      push_fast_bsp_node(node->left, p_l);
514
      push_fast_bsp_node(node->left, p_l);
528
      push_fast_bsp_node(node->right, p_l+1);
515
      push_fast_bsp_node(node->right, p_l+1);
529
      fast_tree[id].inner.flagAndOffset = (unsigned int) &fast_tree[p_l] | node->axis_leaf;
516
      fast_tree[id].inner.flagAndOffset = (unsigned int) &fast_tree[p_l] | node->axis_leaf;
530
      fast_tree[id].inner.splitCoordinate = node->plane;
517
      fast_tree[id].inner.splitCoordinate = node->plane;
531
      node->ref = fast_tree[id].inner.flagAndOffset;
518
      node->ref = fast_tree[id].inner.flagAndOffset;
532
    }
519
    }
533
  }
520
  }
534
 
521
 
535
#define ABSP_ISLEAF(n)       (n->inner.flagAndOffset & (unsigned int)1<<31)
522
#define ABSP_ISLEAF(n)       (n->inner.flagAndOffset & (unsigned int)1<<31)
536
#define ABSP_DIMENSION(n)    (n->inner.flagAndOffset & 0x3)
523
#define ABSP_DIMENSION(n)    (n->inner.flagAndOffset & 0x3)
537
#define ABSP_OFFSET(n)       (n->inner.flagAndOffset & (0x7FFFFFFC))
524
#define ABSP_OFFSET(n)       (n->inner.flagAndOffset & (0x7FFFFFFC))
538
#define ABSP_NEARNODE(n)     (FastBSPNode*)(ray.direction[dimension]>=0?ABSP_OFFSET(node):ABSP_OFFSET(node)+sizeof(*node))
525
#define ABSP_NEARNODE(n)     (FastBSPNode*)(ray.direction[dimension]>=0?ABSP_OFFSET(node):ABSP_OFFSET(node)+sizeof(*node))
539
#define ABSP_FARNODE(n)      (FastBSPNode*)(ray.direction[dimension]>=0?ABSP_OFFSET(node)+sizeof(*node):ABSP_OFFSET(node))
526
#define ABSP_FARNODE(n)      (FastBSPNode*)(ray.direction[dimension]>=0?ABSP_OFFSET(node)+sizeof(*node):ABSP_OFFSET(node))
540
#define ABSP_TRIANGLENODE(n) (vector<TriAccel*>::iterator)(n->flagAndOffset & (0x7FFFFFFF))
527
#define ABSP_TRIANGLENODE(n) (vector<TriAccel*>::iterator)(n->flagAndOffset & (0x7FFFFFFF))
541
  
528
  
542
  struct Stack 
529
  struct Stack 
543
  {
530
  {
544
    FastBSPNode *node;
531
    FastBSPNode *node;
545
    double t_min;
532
    double t_min;
546
    double t_max;
533
    double t_max;
547
  };
534
  };
548
 
535
 
549
  inline void IntersectAlltrianglesInLeaf(const BSPLeaf* leaf, Ray &ray, double t_max) {
536
  inline void IntersectAlltrianglesInLeaf(const BSPLeaf* leaf, Ray &ray, double t_max) {
550
    TriAccel** tri_acc_ptr = reinterpret_cast<TriAccel**>(leaf->flagAndOffset & (0x7FFFFFFF));
537
    TriAccel** tri_acc_ptr = reinterpret_cast<TriAccel**>(leaf->flagAndOffset & (0x7FFFFFFF));
551
    vector<TriAccel*>::iterator acc = vector<TriAccel*>::iterator(tri_acc_ptr);
538
    vector<TriAccel*>::iterator acc = vector<TriAccel*>::iterator(tri_acc_ptr);
552
//	vector<TriAccel*>::iterator acc = ABSP_TRIANGLENODE(leaf);
539
//	vector<TriAccel*>::iterator acc = ABSP_TRIANGLENODE(leaf);
553
    for(unsigned int i=0;i<leaf->count;++i)
540
    for(unsigned int i=0;i<leaf->count;++i)
554
      intersect2(ray, *(*acc++), t_max);
541
      intersect2(ray, *(*acc++), t_max);
555
  }
542
  }
556
 
543
 
557
  void BSPTree::intersect_fast_node(Ray &ray, const FastBSPNode *node, double t_min, double t_max) const 
544
  void BSPTree::intersect_fast_node(Ray &ray, const FastBSPNode *node, double t_min, double t_max) const 
558
  {
545
  {
559
    Stack stack[MAX_DEPTH];
546
    Stack stack[MAX_DEPTH];
560
    int stack_id=0;
547
    int stack_id=0;
561
    double t;
548
    double t;
562
    // Precalculate one over dir
549
    // Precalculate one over dir
563
    double one_over_dir[3];
550
    double one_over_dir[3];
564
    for(int i=0;i<3;i++) 
551
    for(int i=0;i<3;i++) 
565
    {
552
    {
566
      if (ray.direction[i]!=0)
553
      if (ray.direction[i]!=0)
567
	one_over_dir[i]=1.0/ray.direction[i];
554
	one_over_dir[i]=1.0/ray.direction[i];
568
      else
555
      else
569
	one_over_dir[i]=1.0/d_eps;
556
	one_over_dir[i]=1.0/d_eps;
570
    }
557
    }
571
 
558
 
572
    int dimension;
559
    int dimension;
573
    while(1) 
560
    while(1) 
574
    {
561
    {
575
      while(!ABSP_ISLEAF(node)) 
562
      while(!ABSP_ISLEAF(node)) 
576
      {
563
      {
577
	dimension = ABSP_DIMENSION(node);
564
	dimension = ABSP_DIMENSION(node);
578
	t = (node->inner.splitCoordinate - ray.origin[dimension])*one_over_dir[dimension];
565
	t = (node->inner.splitCoordinate - ray.origin[dimension])*one_over_dir[dimension];
579
	if (t>=t_max) 
566
	if (t>=t_max) 
580
	  node = ABSP_NEARNODE(node);
567
	  node = ABSP_NEARNODE(node);
581
	else if (t<=t_min)
568
	else if (t<=t_min)
582
	  node = ABSP_FARNODE(node);
569
	  node = ABSP_FARNODE(node);
583
	else 
570
	else 
584
	{
571
	{
585
	  // Stack push
572
	  // Stack push
586
	  stack[stack_id].node = ABSP_FARNODE(node);
573
	  stack[stack_id].node = ABSP_FARNODE(node);
587
	  stack[stack_id].t_min = t;
574
	  stack[stack_id].t_min = t;
588
	  stack[stack_id++].t_max = t_max;
575
	  stack[stack_id++].t_max = t_max;
589
	  // Set current node to near side
576
	  // Set current node to near side
590
	  node = ABSP_NEARNODE(node);
577
	  node = ABSP_NEARNODE(node);
591
	  t_max = t;
578
	  t_max = t;
592
	}
579
	}
593
      }
580
      }
594
      
581
      
595
      IntersectAlltrianglesInLeaf(&node->leaf, ray, t_max);
582
      IntersectAlltrianglesInLeaf(&node->leaf, ray, t_max);
596
      if (ray.dist<t_max)
583
      if (ray.dist<t_max)
597
	return;
584
	return;
598
      if (stack_id==0)
585
      if (stack_id==0)
599
	return;
586
	return;
600
      // Stack pop
587
      // Stack pop
601
      
588
      
602
      node = stack[--stack_id].node;
589
      node = stack[--stack_id].node;
603
      t_min = stack[stack_id].t_min;
590
      t_min = stack[stack_id].t_min;
604
      t_max = stack[stack_id].t_max;
591
      t_max = stack[stack_id].t_max;
605
    }
592
    }
606
  }
593
  }
607
 
594
 
608
  bool BSPTree::intersect(Ray &ray, const ISectTri &isecttri, double t_max) const 
595
  bool BSPTree::intersect(Ray &ray, const ISectTri &isecttri, double t_max) const 
609
  {
596
  {
610
    tri_calls++;
597
    tri_calls++;
611
    // This is the Möller-Trumbore method
598
    // This is the Möller-Trumbore method
612
    static Vec3d direction; // = Vec3d(ray.direction);
599
    static Vec3d direction; // = Vec3d(ray.direction);
613
    static Vec3d origin; // = Vec3d(ray.direction);
600
    static Vec3d origin; // = Vec3d(ray.direction);
614
    static Vec3d edge1; // = Vec3d(ray.direction);
601
    static Vec3d edge1; // = Vec3d(ray.direction);
615
    static Vec3d edge0; // = Vec3d(ray.direction);
602
    static Vec3d edge0; // = Vec3d(ray.direction);
616
    static Vec3d point0; // = Vec3d(ray.direction);
603
    static Vec3d point0; // = Vec3d(ray.direction);
617
    static Vec3d p;
604
    static Vec3d p;
618
    static Vec3d q;
605
    static Vec3d q;
619
    static Vec3d s;
606
    static Vec3d s;
620
    static Vec3f point;
607
    static Vec3f point;
621
    static double a, f, u, v, t;
608
    static double a, f, u, v, t;
622
    static double ray_dist_sq;
609
    static double ray_dist_sq;
623
    
610
    
624
    static double dist_sq;
611
    static double dist_sq;
625
    
612
    
626
    static Vec3f found_point;
613
    static Vec3f found_point;
627
    
614
    
628
    direction.set((double)ray.direction[0], (double)ray.direction[1], (double)ray.direction[2]);
615
    direction.set((double)ray.direction[0], (double)ray.direction[1], (double)ray.direction[2]);
629
    edge0.set((double)isecttri.edge0[0], (double)isecttri.edge0[1], (double)isecttri.edge0[2]);
616
    edge0.set((double)isecttri.edge0[0], (double)isecttri.edge0[1], (double)isecttri.edge0[2]);
630
    edge1.set((double)isecttri.edge1[0], (double)isecttri.edge1[1], (double)isecttri.edge1[2]);
617
    edge1.set((double)isecttri.edge1[0], (double)isecttri.edge1[1], (double)isecttri.edge1[2]);
631
 
618
 
632
// Ray-tri intersection
619
// Ray-tri intersection
633
//		const double eps = 0.001f;
620
//		const double eps = 0.001f;
634
/********* Complain!!!!!!!!!!!!!!!! *****************/		
621
/********* Complain!!!!!!!!!!!!!!!! *****************/		
635
	//p = cross(direction, edge1);
622
	//p = cross(direction, edge1);
636
	// Why the &%¤/ is this so much faster????? - Because of MS Compiler - the intel compiler does it right!!
623
	// Why the &%¤/ is this so much faster????? - Because of MS Compiler - the intel compiler does it right!!
637
    p.set(direction[1] * edge1[2] - direction[2] * edge1[1], 
624
    p.set(direction[1] * edge1[2] - direction[2] * edge1[1], 
638
	  direction[2] * edge1[0] - direction[0] * edge1[2], 
625
	  direction[2] * edge1[0] - direction[0] * edge1[2], 
639
	  direction[0] * edge1[1] - direction[1] * edge1[0]);
626
	  direction[0] * edge1[1] - direction[1] * edge1[0]);
640
/****************************************************/
627
/****************************************************/
641
    a = dot(edge0,p);
628
    a = dot(edge0,p);
642
    if (a>-d_eps && a<d_eps)
629
    if (a>-d_eps && a<d_eps)
643
      return false;
630
      return false;
644
  // Just delay these 
631
  // Just delay these 
645
    origin.set((double)ray.origin[0], (double)ray.origin[1], (double)ray.origin[2]);
632
    origin.set((double)ray.origin[0], (double)ray.origin[1], (double)ray.origin[2]);
646
    point0.set((double)isecttri.point0[0], (double)isecttri.point0[1], (double)isecttri.point0[2]);
633
    point0.set((double)isecttri.point0[0], (double)isecttri.point0[1], (double)isecttri.point0[2]);
647
    
634
    
648
    f = 1.0/a;
635
    f = 1.0/a;
649
    s = origin - point0;
636
    s = origin - point0;
650
    u = f*dot(s,p);
637
    u = f*dot(s,p);
651
    if (u<0.0 || u>1.0)
638
    if (u<0.0 || u>1.0)
652
      return false;
639
      return false;
653
/********* Complain!!!!!!!!!!!!!!!! *****************/		
640
/********* Complain!!!!!!!!!!!!!!!! *****************/		
654
  //q = cross(s, edge0);
641
  //q = cross(s, edge0);
655
  // Why the &%¤/ is this so much faster?????
642
  // Why the &%¤/ is this so much faster?????
656
    q.set(s[1] * edge0[2] - s[2] * edge0[1], 
643
    q.set(s[1] * edge0[2] - s[2] * edge0[1], 
657
	  s[2] * edge0[0] - s[0] * edge0[2], 
644
	  s[2] * edge0[0] - s[0] * edge0[2], 
658
	  s[0] * edge0[1] - s[1] * edge0[0]);
645
	  s[0] * edge0[1] - s[1] * edge0[0]);
659
/****************************************************/
646
/****************************************************/
660
	
647
	
661
    v = f * dot(direction, q);  
648
    v = f * dot(direction, q);  
662
    if (v<0.0 || u+v>1.0)
649
    if (v<0.0 || u+v>1.0)
663
      return false;
650
      return false;
664
    t = f*dot(edge1, q);
651
    t = f*dot(edge1, q);
665
    if (t<0)
652
    if (t<0)
666
      return false;
653
      return false;
667
    if (fabs(t)<d_eps)
654
    if (fabs(t)<d_eps)
668
      return false;
655
      return false;
669
    if (t_max<t)
656
    if (t_max<t)
670
      return false;
657
      return false;
671
    point = ray.origin + ray.direction*t;
658
    point = ray.origin + ray.direction*t;
672
    
659
    
673
    dist_sq = dot(point-ray.origin, point-ray.origin);
660
    dist_sq = dot(point-ray.origin, point-ray.origin);
674
    ray_dist_sq = ray.dist * ray.dist;
661
    ray_dist_sq = ray.dist * ray.dist;
675
    
662
    
676
    if (dist_sq<f_eps)
663
    if (dist_sq<f_eps)
677
      return false;
664
      return false;
678
    if (dist_sq>ray_dist_sq)
665
    if (dist_sq>ray_dist_sq)
679
      return false;
666
      return false;
680
  
667
  
681
    ray.dist = sqrt(dist_sq);
668
    ray.dist = sqrt(dist_sq);
682
    ray.u = u;
669
    ray.u = u;
683
    ray.v = v;
670
    ray.v = v;
684
    ray.hit_object = (TriMesh*)isecttri.mesh_id;
671
    ray.hit_object = (TriMesh*)isecttri.mesh_id;
685
    ray.hit_face_id = isecttri.tri_id;
672
    ray.hit_face_id = isecttri.tri_id;
686
    ray.has_hit=true;
673
    ray.has_hit=true;
687
    return true; 
674
    return true; 
688
  }
675
  }
689
}
676
}
690
 
677