experiences with memory organization (store the vertexs coords on RayFace)
[blender.git] / source / blender / render / intern / source / rayobject.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2009 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): AndrĂ© Pinto.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29 #include <assert.h>
30 #include <stdio.h>
31
32 #include "BKE_utildefines.h"
33 #include "BLI_arithb.h"
34
35 #include "RE_raytrace.h"
36 #include "render_types.h"
37 #include "rayobject.h"
38
39
40 /*
41  * Determines the distance that the ray must travel to hit the bounding volume of the given node
42  * Based on Tactical Optimization of Ray/Box Intersection, by Graham Fyffe
43  *  [http://tog.acm.org/resources/RTNews/html/rtnv21n1.html#art9]
44  */
45 float RE_rayobject_bb_intersect(const Isect *isec, const float *_bb)
46 {
47         const float *bb = _bb;
48         float dist;
49         
50         float t1x = (bb[isec->bv_index[0]] - isec->start[0]) * isec->idot_axis[0];
51         float t2x = (bb[isec->bv_index[1]] - isec->start[0]) * isec->idot_axis[0];
52         float t1y = (bb[isec->bv_index[2]] - isec->start[1]) * isec->idot_axis[1];
53         float t2y = (bb[isec->bv_index[3]] - isec->start[1]) * isec->idot_axis[1];
54         float t1z = (bb[isec->bv_index[4]] - isec->start[2]) * isec->idot_axis[2];
55         float t2z = (bb[isec->bv_index[5]] - isec->start[2]) * isec->idot_axis[2];
56
57         RE_RC_COUNT(isec->raycounter->bb.test);
58
59         if(t1x > t2y || t2x < t1y || t1x > t2z || t2x < t1z || t1y > t2z || t2y < t1z) return FLT_MAX;
60         if(t2x < 0.0 || t2y < 0.0 || t2z < 0.0) return FLT_MAX;
61         if(t1x > isec->labda || t1y > isec->labda || t1z > isec->labda) return FLT_MAX;
62
63         RE_RC_COUNT(isec->raycounter->bb.hit);
64
65         dist = t1x;
66         if (t1y > dist) dist = t1y;
67     if (t1z > dist) dist = t1z;
68         return dist;
69 }
70
71
72 /* only for self-intersecting test with current render face (where ray left) */
73 static int intersection2(VlakRen *face, float r0, float r1, float r2, float rx1, float ry1, float rz1)
74 {
75         float co1[3], co2[3], co3[3], co4[3];
76         float x0,x1,x2,t00,t01,t02,t10,t11,t12,t20,t21,t22;
77         float m0, m1, m2, divdet, det, det1;
78         float u1, v, u2;
79
80         VECCOPY(co1, face->v1->co);
81         VECCOPY(co2, face->v2->co);
82         if(face->v4)
83         {
84                 VECCOPY(co3, face->v4->co);
85                 VECCOPY(co4, face->v3->co);
86         }
87         else
88         {
89                 VECCOPY(co3, face->v3->co);
90         }
91
92         t00= co3[0]-co1[0];
93         t01= co3[1]-co1[1];
94         t02= co3[2]-co1[2];
95         t10= co3[0]-co2[0];
96         t11= co3[1]-co2[1];
97         t12= co3[2]-co2[2];
98         
99         x0= t11*r2-t12*r1;
100         x1= t12*r0-t10*r2;
101         x2= t10*r1-t11*r0;
102
103         divdet= t00*x0+t01*x1+t02*x2;
104
105         m0= rx1-co3[0];
106         m1= ry1-co3[1];
107         m2= rz1-co3[2];
108         det1= m0*x0+m1*x1+m2*x2;
109         
110         if(divdet!=0.0f) {
111                 u1= det1/divdet;
112
113                 if(u1<ISECT_EPSILON) {
114                         det= t00*(m1*r2-m2*r1);
115                         det+= t01*(m2*r0-m0*r2);
116                         det+= t02*(m0*r1-m1*r0);
117                         v= det/divdet;
118
119                         if(v<ISECT_EPSILON && (u1 + v) > -(1.0f+ISECT_EPSILON)) {
120                                 return 1;
121                         }
122                 }
123         }
124
125         if(face->v4) {
126
127                 t20= co3[0]-co4[0];
128                 t21= co3[1]-co4[1];
129                 t22= co3[2]-co4[2];
130
131                 divdet= t20*x0+t21*x1+t22*x2;
132                 if(divdet!=0.0f) {
133                         u2= det1/divdet;
134                 
135                         if(u2<ISECT_EPSILON) {
136                                 det= t20*(m1*r2-m2*r1);
137                                 det+= t21*(m2*r0-m0*r2);
138                                 det+= t22*(m0*r1-m1*r0);
139                                 v= det/divdet;
140         
141                                 if(v<ISECT_EPSILON && (u2 + v) >= -(1.0f+ISECT_EPSILON)) {
142                                         return 2;
143                                 }
144                         }
145                 }
146         }
147         return 0;
148 }
149
150
151 /* ray - triangle or quad intersection */
152 /* this function shall only modify Isect if it detects an hit */
153 static int intersect_rayface(RayFace *face, Isect *is)
154 {
155         float co1[3],co2[3],co3[3],co4[3];
156         float x0,x1,x2,t00,t01,t02,t10,t11,t12,t20,t21,t22,r0,r1,r2;
157         float m0, m1, m2, divdet, det1;
158         float labda, u, v;
159         short ok=0;
160         
161         if(is->orig.ob == face->ob && is->orig.face == face->face)
162                 return 0;
163
164         RE_RC_COUNT(is->raycounter->faces.test);
165
166         VECCOPY(co1, face->v1);
167         VECCOPY(co2, face->v2);
168         if(RE_rayface_isQuad(face))
169         {
170                 VECCOPY(co3, face->v4);
171                 VECCOPY(co4, face->v3);
172         }
173         else
174         {
175                 VECCOPY(co3, face->v3);
176         }
177
178         t00= co3[0]-co1[0];
179         t01= co3[1]-co1[1];
180         t02= co3[2]-co1[2];
181         t10= co3[0]-co2[0];
182         t11= co3[1]-co2[1];
183         t12= co3[2]-co2[2];
184         
185         r0= is->vec[0];
186         r1= is->vec[1];
187         r2= is->vec[2];
188         
189         x0= t12*r1-t11*r2;
190         x1= t10*r2-t12*r0;
191         x2= t11*r0-t10*r1;
192
193         divdet= t00*x0+t01*x1+t02*x2;
194
195         m0= is->start[0]-co3[0];
196         m1= is->start[1]-co3[1];
197         m2= is->start[2]-co3[2];
198         det1= m0*x0+m1*x1+m2*x2;
199         
200         if(divdet!=0.0f) {
201
202                 divdet= 1.0f/divdet;
203                 u= det1*divdet;
204                 if(u<ISECT_EPSILON && u>-(1.0f+ISECT_EPSILON)) {
205                         float cros0, cros1, cros2;
206                         
207                         cros0= m1*t02-m2*t01;
208                         cros1= m2*t00-m0*t02;
209                         cros2= m0*t01-m1*t00;
210                         v= divdet*(cros0*r0 + cros1*r1 + cros2*r2);
211
212                         if(v<ISECT_EPSILON && (u + v) > -(1.0f+ISECT_EPSILON)) {
213                                 labda= divdet*(cros0*t10 + cros1*t11 + cros2*t12);
214
215                                 if(labda>-ISECT_EPSILON && labda<is->labda) {
216                                         ok= 1;
217                                 }
218                         }
219                 }
220         }
221
222         if(ok==0 && RE_rayface_isQuad(face)) {
223
224                 t20= co3[0]-co4[0];
225                 t21= co3[1]-co4[1];
226                 t22= co3[2]-co4[2];
227
228                 divdet= t20*x0+t21*x1+t22*x2;
229                 if(divdet!=0.0f) {
230                         divdet= 1.0f/divdet;
231                         u = det1*divdet;
232                         
233                         if(u<ISECT_EPSILON && u>-(1.0f+ISECT_EPSILON)) {
234                                 float cros0, cros1, cros2;
235                                 cros0= m1*t22-m2*t21;
236                                 cros1= m2*t20-m0*t22;
237                                 cros2= m0*t21-m1*t20;
238                                 v= divdet*(cros0*r0 + cros1*r1 + cros2*r2);
239         
240                                 if(v<ISECT_EPSILON && (u + v) >-(1.0f+ISECT_EPSILON)) {
241                                         labda= divdet*(cros0*t10 + cros1*t11 + cros2*t12);
242                                         
243                                         if(labda>-ISECT_EPSILON && labda<is->labda) {
244                                                 ok= 2;
245                                         }
246                                 }
247                         }
248                 }
249         }
250
251         if(ok) {
252         
253                 /* when a shadow ray leaves a face, it can be little outside the edges of it, causing
254                 intersection to be detected in its neighbour face */
255                 if(is->skip & RE_SKIP_VLR_NEIGHBOUR)
256                 {
257                         if(labda < 0.1f && is->orig.ob == face->ob)
258                         {
259                                 VlakRen * a = is->orig.face;
260                                 VlakRen * b = face->face;
261
262                                 /* so there's a shared edge or vertex, let's intersect ray with face
263                                 itself, if that's true we can safely return 1, otherwise we assume
264                                 the intersection is invalid, 0 */
265                                 if(a->v1==b->v1 || a->v2==b->v1 || a->v3==b->v1 || a->v4==b->v1
266                                 || a->v1==b->v2 || a->v2==b->v2 || a->v3==b->v2 || a->v4==b->v2
267                                 || a->v1==b->v3 || a->v2==b->v3 || a->v3==b->v3 || a->v4==b->v3
268                                 || (b->v4 && (a->v1==b->v4 || a->v2==b->v4 || a->v3==b->v4 || a->v4==b->v4)))
269                                 if(intersection2((VlakRen*)b, -r0, -r1, -r2, is->start[0], is->start[1], is->start[2]))
270                                 {
271                                         return 0;
272                                 }
273                         }
274                 }
275 #if 0
276                 else if(labda < ISECT_EPSILON)
277                 {
278                         /* too close to origin */
279                         return 0;
280                 }
281 #endif
282
283                 RE_RC_COUNT(is->raycounter->faces.hit);
284
285                 is->isect= ok;  // wich half of the quad
286                 is->labda= labda;
287                 is->u= u; is->v= v;
288
289                 is->hit.ob   = face->ob;
290                 is->hit.face = face->face;
291 #ifdef RT_USE_LAST_HIT
292                 is->last_hit = (RayObject*) RayObject_unalignRayFace(face);
293 #endif
294                 return 1;
295         }
296
297         return 0;
298 }
299
300 void RE_rayface_from_vlak(RayFace *face, ObjectInstanceRen *obi, VlakRen *vlr)
301 {
302 #ifdef RE_RAYFACE_COORDS_LOCAL
303         VECCOPY(face->v1, vlr->v1->co);
304         VECCOPY(face->v2, vlr->v2->co);
305         VECCOPY(face->v3, vlr->v3->co);
306         if(vlr->v4)
307         {
308                 VECCOPY(face->v4, vlr->v4->co);
309                 face->quad = 1;
310         }
311         else
312         {
313                 face->quad = 0;
314         }
315 #elif defined(RE_RAYFACE_COORDS_POINTER)
316         face->v1 = vlr->v1->co;
317         face->v2 = vlr->v2->co;
318         face->v3 = vlr->v3->co;
319         face->v4 = vlr->v4 ? vlr->v4->co : NULL;
320 #elif defined(RE_RAYFACE_COORDS_VLAKREN)
321 #endif
322         face->ob   = obi;
323         face->face = vlr;
324 }
325
326
327 int RE_rayobject_raycast(RayObject *r, Isect *isec)
328 {
329         int i;
330         RE_RC_COUNT(isec->raycounter->raycast.test);
331
332         /* Setup vars used on raycast */
333         isec->labda *= Normalize(isec->vec);
334         isec->dist = VecLength(isec->vec);
335         
336         for(i=0; i<3; i++)
337         {
338                 isec->idot_axis[i]              = 1.0f / isec->vec[i];
339                 
340                 isec->bv_index[2*i]             = isec->idot_axis[i] < 0.0 ? 1 : 0;
341                 isec->bv_index[2*i+1]   = 1 - isec->bv_index[2*i];
342                 
343                 isec->bv_index[2*i]             = i+3*isec->bv_index[2*i];
344                 isec->bv_index[2*i+1]   = i+3*isec->bv_index[2*i+1];
345         }
346
347 #ifdef RT_USE_LAST_HIT  
348         /* Last hit heuristic */
349         if(isec->mode==RE_RAY_SHADOW && isec->last_hit)
350         {
351                 RE_RC_COUNT(isec->raycounter->rayshadow_last_hit.test);
352                 
353                 if(RE_rayobject_intersect(isec->last_hit, isec))
354                 {
355                         RE_RC_COUNT(isec->raycounter->raycast.hit);
356                         RE_RC_COUNT(isec->raycounter->rayshadow_last_hit.hit);
357                         return 1;
358                 }
359         }
360 #endif
361
362 #ifdef RT_USE_HINT
363         isec->hit_hint = 0;
364 #endif
365
366         if(RE_rayobject_intersect(r, isec))
367         {
368 #ifdef RE_RAYCOUNTER
369                 RE_RC_COUNT(isec->raycounter->raycast.hit);
370 #endif
371
372 #ifdef RT_USE_HINT
373                 isec->hint = isec->hit_hint;
374 #endif
375                 return 1;
376         }
377         return 0;
378 }
379
380 int RE_rayobject_intersect(RayObject *r, Isect *i)
381 {
382         if(RayObject_isRayFace(r))
383         {
384                 return intersect_rayface( (RayFace*) RayObject_align(r), i);
385         }
386         else if(RayObject_isRayAPI(r))
387         {
388                 r = RayObject_align( r );
389                 return r->api->raycast( r, i );
390         }
391         else assert(0);
392 }
393
394 void RE_rayobject_add(RayObject *r, RayObject *o)
395 {
396         r = RayObject_align( r );
397         return r->api->add( r, o );
398 }
399
400 void RE_rayobject_done(RayObject *r)
401 {
402         r = RayObject_align( r );
403         r->api->done( r );
404 }
405
406 void RE_rayobject_free(RayObject *r)
407 {
408         r = RayObject_align( r );
409         r->api->free( r );
410 }
411
412 void RE_rayobject_merge_bb(RayObject *r, float *min, float *max)
413 {
414         if(RayObject_isRayFace(r))
415         {
416                 RayFace *face = (RayFace*) RayObject_align(r);
417                 DO_MINMAX( face->v1, min, max );
418                 DO_MINMAX( face->v2, min, max );
419                 DO_MINMAX( face->v3, min, max );
420                 if(RE_rayface_isQuad(face)) DO_MINMAX( face->v4, min, max );
421         }
422         else if(RayObject_isRayAPI(r))
423         {
424                 r = RayObject_align( r );
425                 r->api->bb( r, min, max );
426         }
427         else assert(0);
428 }
429
430 float RE_rayobject_cost(RayObject *r)
431 {
432         if(RayObject_isRayFace(r))
433         {
434                 return 1.0;
435         }
436         else if(RayObject_isRayAPI(r))
437         {
438                 r = RayObject_align( r );
439                 return r->api->cost( r );
440         }
441         else assert(0);
442 }
443
444 void RE_rayobject_hint_bb(RayObject *r, RayHint *hint, float *min, float *max)
445 {
446         if(RayObject_isRayFace(r))
447         {
448                 return;
449         }
450         else if(RayObject_isRayAPI(r))
451         {
452                 r = RayObject_align( r );
453                 return r->api->hint_bb( r, hint, min, max );
454         }
455         else assert(0);
456 }
457
458 #ifdef RE_RAYCOUNTER
459 void RE_RC_INFO(RayCounter *info)
460 {
461         printf("----------- Raycast counter --------\n");
462         printf("Rays total: %llu\n", info->raycast.test );
463         printf("Rays hit: %llu\n",   info->raycast.hit  );
464         printf("\n");
465         printf("BB tests: %llu\n", info->bb.test );
466         printf("BB hits: %llu\n", info->bb.hit );
467         printf("\n");   
468         printf("Primitives tests: %llu\n", info->faces.test );
469         printf("Primitives hits: %llu\n", info->faces.hit );
470         printf("------------------------------------\n");
471         printf("Shadow last-hit tests per ray: %f\n", info->rayshadow_last_hit.test / ((float)info->raycast.test) );
472         printf("Shadow last-hit hits per ray: %f\n",  info->rayshadow_last_hit.hit  / ((float)info->raycast.test) );
473         printf("\n");
474         printf("Hint tests per ray: %f\n", info->raytrace_hint.test / ((float)info->raycast.test) );
475         printf("Hint hits per ray: %f\n",  info->raytrace_hint.hit  / ((float)info->raycast.test) );
476         printf("\n");
477         printf("BB tests per ray: %f\n", info->bb.test / ((float)info->raycast.test) );
478         printf("BB hits per ray: %f\n", info->bb.hit / ((float)info->raycast.test) );
479         printf("\n");
480         printf("Primitives tests per ray: %f\n", info->faces.test / ((float)info->raycast.test) );
481         printf("Primitives hits per ray: %f\n", info->faces.hit / ((float)info->raycast.test) );
482         printf("------------------------------------\n");
483 }
484
485 void RE_RC_MERGE(RayCounter *dest, RayCounter *tmp)
486 {
487         dest->faces.test += tmp->faces.test;
488         dest->faces.hit  += tmp->faces.hit;
489
490         dest->bb.test += tmp->bb.test;
491         dest->bb.hit  += tmp->bb.hit;
492
493         dest->raycast.test += tmp->raycast.test;
494         dest->raycast.hit  += tmp->raycast.hit;
495         
496         dest->rayshadow_last_hit.test += tmp->rayshadow_last_hit.test;
497         dest->rayshadow_last_hit.hit  += tmp->rayshadow_last_hit.hit;
498
499         dest->raytrace_hint.test += tmp->raytrace_hint.test;
500         dest->raytrace_hint.hit  += tmp->raytrace_hint.hit;
501 }
502
503 #endif