source: GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.cpp @ 2076

Revision 2076, 6.2 KB checked in by bittner, 17 years ago (diff)

merge

RevLine 
[1520]1#include "IntelRayCaster.h"
2#include "VssRay.h"
3#include "Preprocessor.h"
[1942]4#include "SceneGraph.h"
[1520]5
6#ifdef GTP_INTERNAL
7#include "ArchModeler2MLRT.hxx"
8
[1952]9#define DEBUG_RAYCAST 0
[1520]10
11
12namespace GtpVisibilityPreprocessor {
13
14
[1960]15IntelRayCaster::IntelRayCaster(const Preprocessor &preprocessor,
16                                                           const string externKdTree):
[1520]17RayCaster(preprocessor)
18{
[2003]19        if (!InitRayCast(externKdTree))
20                cout << "warning: intel ray tracer could not be initialized!" << endl;
[1520]21}
22
23
24IntelRayCaster::~IntelRayCaster()
25{
26}
27
28
29bool IntelRayCaster::InitRayCast(const string externKdTree)
30{
[2003]31        cout<<"Intel ray cast file: " << externKdTree << endl;
32       
33        return mlrtaLoadAS(externKdTree.c_str());
[1520]34}
35
36
37int IntelRayCaster::CastRay(
[1528]38                                                        const SimpleRay &simpleRay,
[1520]39                                                        VssRayContainer &vssRays,
40                                                        const AxisAlignedBox3 &box,
[1528]41                                                        const bool castDoubleRay,
[1996]42                                                        const bool pruneInvalidRays
[1520]43                                                        )
44{
[2004]45        //cout << "intel ray" << endl;
[1520]46        VssRay *vssRay  = NULL;
47        int hits = 0;
48        int hittriangle;
[1533]49        Intersection hitA(simpleRay.mOrigin), hitB(simpleRay.mOrigin);
[1528]50
[1520]51        float dist;
52        double normal[3];
53
54        hittriangle = mlrtaIntersectAS(
[1984]55                                                                   &simpleRay.mOrigin.x,
56                                                                   &simpleRay.mDirection.x,
57                                                                   normal,
58                                                                   dist);
59       
[1520]60        if (hittriangle != -1 ) {
[1786]61                Intersectable *intersect = mPreprocessor.GetParentObject(hittriangle);
62               
63                if (intersect)
64                {
65                        hitA.mObject = intersect;
[1528]66                        hitA.mNormal = Vector3(normal[0], normal[1], normal[2]);
[1520]67                        // Get the normal of that face
68                        //              Mesh *mesh = ((MeshInstance *)objectA)->GetMesh();
69                        //              normalA = mesh->GetFacePlane(mFaceParents[forward_hit_triangles[i]].mFaceIndex).mNormal;
70                        //-rays[index+i].mDirection; // $$ temporary
[1528]71                        hitA.mPoint = simpleRay.Extrap(dist);
[1520]72                }
73        }
74
75        if (castDoubleRay)
76        {
[2003]77                Vector3 dir = -simpleRay.mDirection;
[1520]78                hittriangle = mlrtaIntersectAS(
[1960]79                                                                                &simpleRay.mOrigin.x,
80                                                                                &dir.x,
81                                                                                normal,
82                                                                                dist);
[1520]83
[1786]84                Intersectable *intersect = mPreprocessor.GetParentObject(hittriangle);
85               
86                if (intersect)
87                {
88                        hitB.mObject = intersect;
89                        hitB.mNormal = Vector3(normal[0], normal[1], normal[2]);
90                        // Get the normal of that face
91                        //              Mesh *mesh = ((MeshInstance *)objectB)->GetMesh();
92                        //              normalA = mesh->GetFacePlane(mFaceParents[forward_hit_triangles[i]].mFaceIndex).mNormal;
93                        //-rays[index+i].mDirection; // $$ temporary
94                        hitB.mPoint = simpleRay.Extrap(dist);
[1520]95                }
96        }
97
98        return ProcessRay(
[1867]99                                          simpleRay,
100                                          hitA,
101                                          hitB,
102                                          vssRays,
103                                          box,
104                                          castDoubleRay,
[1996]105                                          pruneInvalidRays
[1867]106                                          );
[1520]107}
108
109
110void IntelRayCaster::CastRays16(
[2076]111                                                                SimpleRayContainer &rays,
[1520]112                                                                VssRayContainer &vssRays,
113                                                                const AxisAlignedBox3 &sbox,
[1528]114                                                                const bool castDoubleRay,
[1996]115                                                                const bool pruneInvalidRays)
[1520]116{
[2076]117  CastRays16(rays, 0, vssRays, sbox, castDoubleRay, pruneInvalidRays);
118}
119
120void IntelRayCaster::CastRays16(
121                                                                SimpleRayContainer &rays,
122                                                                const int offset,
123                                                                VssRayContainer &vssRays,
124                                                                const AxisAlignedBox3 &sbox,
125                                                                const bool castDoubleRay,
126                                                                const bool pruneInvalidRays)
127{
128  int i, k;
129  const int num = 16;
130 
[1520]131#if DEBUG_RAYCAST
[2076]132  Debug<<"C16 "<<flush;
133  static int counter=0;
134  Debug<<counter++<<endl;
[1520]135#endif
[2076]136 
137  static int forward_hit_triangles[16];
138  static float forward_dist[16];
139 
140  static int backward_hit_triangles[16];
141  static float backward_dist[16];
142 
143 
144  Vector3 min = mPreprocessor.mSceneGraph->GetBox().Min();
145  Vector3 max = mPreprocessor.mSceneGraph->GetBox().Max();
[1520]146
[2076]147  for (k=offset, i=0; i < num; i++, k++) {
[1983]148#if DEBUG_RAYCAST
[2076]149        if (counter == 43964) {
150          Debug<<rays[k].mOrigin<<" "<<rays[k].mDirection<<endl;
151        }
[1983]152#endif
[2076]153        mlrtaStoreRayAS16(&rays[k].mOrigin.x,
154                                          &rays[k].mDirection.x,
155                                          i);
156  }
157 
[1757]158#if DEBUG_RAYCAST
[2076]159  Debug<<"TA\n"<<flush;
[1757]160#endif
[2076]161 
162  mlrtaTraverseGroupAS16(&min.x,
163                                                 &max.x,
164                                                 forward_hit_triangles,
165                                                 forward_dist);
166 
[1757]167#if DEBUG_RAYCAST
[2076]168  Debug<<"TAB\n"<<flush;
[1757]169#endif
170
[2076]171  if (castDoubleRay) {
172        for (k=offset, i=0; i < num; i++, k++)  {
173          Vector3 dir = -rays[k].mDirection;
174          mlrtaStoreRayAS16(&rays[k].mOrigin.x,
175                                                &dir.x,
176                                                i);
177        }
178       
[1757]179#if DEBUG_RAYCAST
[1983]180          Debug<<"TB\n"<<flush;
181#endif
182         
183          mlrtaTraverseGroupAS16(&min.x,
184                                                         &max.x,
185                                                         backward_hit_triangles,
186                                                         backward_dist);
187        }
188       
189#if DEBUG_RAYCAST
[1757]190        Debug<<"BBB\n"<<flush;
191#endif
[2076]192       
193        for (i=0, k=offset; i < num; i++, k++)
[1786]194        {
[2076]195                Intersection hitA(rays[k].mOrigin), hitB(rays[k].mOrigin);
[1757]196
197#if DEBUG_RAYCAST
[1786]198                Debug<<"FH\n"<<flush;
[1757]199#endif
[1984]200
[1786]201                Intersectable *intersect = mPreprocessor.GetParentObject(forward_hit_triangles[i]);
202
203                if (intersect)
204                {
205                        hitA.mObject = intersect;
[1757]206                        // Get the normal of that face
[1786]207                        hitA.mNormal = mPreprocessor.GetParentNormal(forward_hit_triangles[i]);
208
[1757]209                        //-rays[index+i].mDirection; // $$ temporary
[2076]210                        hitA.mPoint = rays[k].Extrap(forward_dist[i]);
[1786]211                }
212       
[1757]213#if DEBUG_RAYCAST
[1786]214                Debug<<"BH\n"<<flush;
[1757]215#endif
[1786]216
217                if (castDoubleRay)
218                {
219                        Intersectable *intersect = mPreprocessor.GetParentObject(backward_hit_triangles[i]);
220
221                        if (intersect)
222                        {
223                                hitB.mObject = intersect;
224                                hitB.mNormal = mPreprocessor.GetParentNormal(backward_hit_triangles[i]);
225
[1972]226                                // normalB = rays[i].mDirection; // $$ temporary
[2076]227                                hitB.mPoint = rays[k].Extrap(-backward_dist[i]);
[1786]228                        }
[1520]229                }
[1786]230
[1757]231#if DEBUG_RAYCAST
[1786]232                Debug<<"PR\n"<<flush;
[1757]233#endif
[1786]234
[2076]235#if 1
236                ProcessRay(rays[k],
[1786]237                                   hitA,
238                                   hitB,
239                                   vssRays,
240                                   sbox,
241                                   castDoubleRay,
[1996]242                                   pruneInvalidRays
[1786]243                                   );
[2076]244#endif
[1520]245        }
[1786]246
[1520]247#if DEBUG_RAYCAST
248        Debug<<"C16F\n"<<flush;
249#endif
250}
251
[2076]252
253
254void
255IntelRayCaster::CastRays(
256                                                 SimpleRayContainer &rays,
257                                                 VssRayContainer &vssRays,
258                                                 const AxisAlignedBox3 &sbox,
259                                                 const bool castDoubleRay,
260                                                 const bool pruneInvalidRays )
261{
262  cout<<"INTEL"<<endl;
263
264  int buckets = rays.size()/16;
265  int offset = 0;
266 
267  for (int i=0; i < buckets; i++, offset+=16) {
268        CastRays16(rays, offset, vssRays, sbox, castDoubleRay, pruneInvalidRays);
269
270        if ((int)rays.size() > 100000 && i % (100000/16) == 0)
271          cout<<"\r"<<offset<<"/"<<(int)rays.size()<<"\r";
272  }
273
274  for (; offset < rays.size(); offset++)
275        CastRay(rays[offset], vssRays, sbox, castDoubleRay, pruneInvalidRays);
276
[1520]277}
278
[2076]279}
280
[1520]281#endif
Note: See TracBrowser for help on using the repository browser.