source: GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.cpp @ 1876

Revision 1876, 5.3 KB checked in by bittner, 18 years ago (diff)

halton generator updates

Line 
1#include "InternalRayCaster.h"
2#include "VssRay.h"
3#include "KdTree.h"
4#include "Preprocessor.h"
5
6#define DEBUG_RAYCAST 0
7
8
9namespace GtpVisibilityPreprocessor {
10
11
12InternalRayCaster::InternalRayCaster(const Preprocessor &preprocessor, KdTree *kdTree):
13RayCaster(preprocessor), mKdTree(kdTree)
14{
15}
16
17
18InternalRayCaster::~InternalRayCaster()
19{
20}
21
22
23int InternalRayCaster::CastRay(const SimpleRay &simpleRay,
24                                                           VssRayContainer &vssRays,
25                                                           const AxisAlignedBox3 &box,
26                                                           const bool castDoubleRay,
27                                                           const bool pruneInvalidRays
28                                                           )
29{
30  if (simpleRay.mType == Ray::GLOBAL_RAY)
31        return CastGlobalRay(simpleRay, vssRays, box, pruneInvalidRays);
32 
33  //cout << "internal ray" << endl;
34  static Ray ray;
35  int hits = 0;
36  Intersection hitA(simpleRay.mOrigin), hitB(simpleRay.mOrigin);
37 
38  // inside test for bounding box
39  // enlarge box slightly so the view point fits for sure
40  //  AxisAlignedBox3 sbox = box;
41  //  sbox.Enlarge(Vector3(-Limits::Small));
42  // $$ JB moved here from Validate routine
43
44//   if (!box.IsInside(simpleRay.mOrigin)) {
45//      cout<<"out of box "<<simpleRay.mOrigin<<" "<<box<<endl;
46//      return 0;
47//   }
48 
49  mPreprocessor.SetupRay(ray, simpleRay.mOrigin, simpleRay.mDirection);
50  ray.mFlags &= ~Ray::CULL_BACKFACES;
51 
52  if (mKdTree->CastRay(ray))
53        {
54          hitA.mObject = ray.intersections[0].mObject;
55          hitA.mPoint = ray.Extrap(ray.intersections[0].mT);
56          hitA.mNormal = ray.intersections[0].mNormal;
57          //    cout << "hita: " << hitA.mPoint << " !obj: " << hitA.mObject << endl;
58        }
59 
60        mPreprocessor.SetupRay(ray, simpleRay.mOrigin, -simpleRay.mDirection);
61        ray.mFlags &= ~Ray::CULL_BACKFACES;
62 
63  if (castDoubleRay && mKdTree->CastRay(ray))
64                {
65                        hitB.mObject = ray.intersections[0].mObject;
66                        hitB.mPoint = ray.Extrap(ray.intersections[0].mT);
67                        hitB.mNormal = ray.intersections[0].mNormal;
68                }
69 
70  return ProcessRay(
71                                                                                simpleRay,
72                                                                                hitA,
73                                                                                hitB,
74                                                                                vssRays,
75                                                                                box,
76                                                                                castDoubleRay,
77                                                                                pruneInvalidRays
78                                                                                );
79}
80
81
82int
83InternalRayCaster::CastGlobalRay(const SimpleRay &simpleRay,
84                                                                                                                                 VssRayContainer &vssRays,
85                                                                                                                                 const AxisAlignedBox3 &box,
86                                                                                                                                 const bool pruneInvalidRays
87                                                                                                                                 )
88{
89  static Ray ray;
90  int hits = 0;
91  mPreprocessor.SetupRay(ray, simpleRay.mOrigin, simpleRay.mDirection);
92 
93  float tmin, tmax;
94  if (!(box.ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax)))
95        return 0;
96 
97  // shift the ray origin to tmin
98  //    Vector3 origin = ray.Extrap(tmin);
99  //    mPreprocessor.SetupRay(ray, origin, simpleRay.mDirection);
100  ray.SetType(Ray::GLOBAL_RAY);
101  ray.mFlags &= ~Ray::CULL_BACKFACES;
102 
103
104  VssRay *vssRay;
105 
106  if (mKdTree->CastRay(ray)) {
107        // sort intersections
108        ray.SortIntersections();
109        //      cout<<"I="<<ray.intersections.size()<<endl;
110       
111       
112        Ray::Intersection &hit = ray.intersections[0];
113       
114        if (DotProd(hit.mNormal, ray.GetDir()) < 0) {
115          //      cout<<"F:"<<tmin<<" "<<hit.mT<<endl;
116          // insert intial segment
117          vssRay = new VssRay(
118                                                  ray.Extrap(tmin),
119                                                  ray.Extrap(hit.mT),
120                                                  NULL,
121                                                  hit.mObject,
122                                                  mPreprocessor.mPass,
123                                                  simpleRay.mPdf
124                                                  );
125
126          vssRay->mFlags |= VssRay::Valid;
127          vssRays.push_back(vssRay);
128          ++hits;
129        }
130                               
131        hit = ray.intersections[ray.intersections.size()-1];
132        if (DotProd(hit.mNormal, ray.GetDir()) > 0) {
133          //      cout<<"L:"<<tmax<<" "<<hit.mT<<endl;
134         
135          // insert termination segment
136          vssRay = new VssRay(
137                                                  ray.Extrap(tmax),
138                                                  ray.Extrap(hit.mT),
139                                                  NULL,
140                                                  hit.mObject,
141                                                  mPreprocessor.mPass,
142                                                  simpleRay.mPdf
143                                                  );
144          vssRay->mFlags |= VssRay::Valid;
145          vssRays.push_back(vssRay);
146          ++hits;
147        }
148       
149        // insert the rest of segments
150        for (int i=0; i < ray.intersections.size() - 1; i++) {
151          Ray::Intersection &hitA = ray.intersections[i];
152          Ray::Intersection &hitB = ray.intersections[i + 1];
153          if (hitB.mT - hitA.mT > Limits::Small) {
154                if (DotProd(hitA.mNormal, ray.GetDir()) > 0 &&
155                        DotProd(hitB.mNormal, ray.GetDir()) < 0
156                        ) {
157                 
158                  vssRay = new VssRay(
159                                                          ray.Extrap(hitA.mT),
160                                                          ray.Extrap(hitB.mT),
161                                                          hitA.mObject,
162                                                        hitB.mObject,
163                                                          mPreprocessor.mPass,
164                                                          simpleRay.mPdf
165                                                          );
166                  vssRay->mFlags |= VssRay::Valid;
167                  vssRays.push_back(vssRay);
168                  ++hits;
169                 
170                  vssRay = new VssRay(
171                                                          ray.Extrap(hitB.mT),
172                                                          ray.Extrap(hitA.mT),
173                                                          hitB.mObject,
174                                                          hitA.mObject,
175                                                          mPreprocessor.mPass,
176                                                          simpleRay.mPdf
177                                                          );
178                 
179                  vssRay->mFlags |= VssRay::Valid;
180                  vssRays.push_back(vssRay);
181                  ++hits;
182                }
183          }
184        }
185  }
186 
187  return hits;
188}
189
190void InternalRayCaster::CastRays16(const int index,
191                                                                   SimpleRayContainer &rays,
192                                                                   VssRayContainer &vssRays,
193                                                                   const AxisAlignedBox3 &sbox,
194                                                                   const bool castDoubleRays,
195                                                                   const bool pruneInvalidRays)
196{
197        const int num = 16;
198
199#if DEBUG_RAYCAST
200        Debug << "C16 " << flush;
201#endif
202
203        // no acceleration for ray bundles implemented right now
204        for (int i = index; i < index + num; i++)
205        {
206                CastRay(rays[i], vssRays, sbox, castDoubleRays, pruneInvalidRays);
207        }
208
209#if DEBUG_RAYCAST
210        Debug<<"C16F\n"<<flush;
211#endif
212}
213
214}
Note: See TracBrowser for help on using the repository browser.