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

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