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

Revision 1972, 4.8 KB checked in by mattausch, 18 years ago (diff)

implemented improved ray casting

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