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

Revision 1984, 5.4 KB checked in by bittner, 17 years ago (diff)

ray sort 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 
53  if (mKdTree->CastRay(ray)) {
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        hitB.mObject = ray.intersections[0].mObject;
65        hitB.mPoint = ray.Extrap(ray.intersections[0].mT);
66        hitB.mNormal = ray.intersections[0].mNormal;
67  }
68 
69  return ProcessRay(
70                                        simpleRay,
71                                        hitA,
72                                        hitB,
73                                        vssRays,
74                                        box,
75                                        castDoubleRay,
76                                        pruneInvalidRays
77                                        );
78}
79
80
81int
82InternalRayCaster::CastGlobalRay(const SimpleRay &simpleRay,
83                                                                 VssRayContainer &vssRays,
84                                                                 const AxisAlignedBox3 &box,
85                                                                 const bool pruneInvalidRays
86                                                                )
87{
88  static Ray ray;
89  int hits = 0;
90  mPreprocessor.SetupRay(ray, simpleRay.mOrigin, simpleRay.mDirection);
91 
92  float tmin, tmax;
93  if (!(box.ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax)))
94        return 0;
95 
96  // shift the ray origin to tmin
97  //    Vector3 origin = ray.Extrap(tmin);
98  //    mPreprocessor.SetupRay(ray, origin, simpleRay.mDirection);
99  ray.SetType(Ray::GLOBAL_RAY);
100  ray.mFlags &= ~Ray::CULL_BACKFACES;
101 
102
103  VssRay *vssRay;
104 
105  if (mKdTree->CastRay(ray)) {
106        // sort intersections
107        ray.SortIntersections();
108        //      cout<<"I="<<ray.intersections.size()<<endl;
109       
110       
111        Ray::Intersection &hit = ray.intersections[0];
112       
113        if (DotProd(hit.mNormal, ray.GetDir()) < 0) {
114          //      cout<<"F:"<<tmin<<" "<<hit.mT<<endl;
115          // insert intial segment
116          vssRay = new VssRay(
117                                                  ray.Extrap(tmin),
118                                                  ray.Extrap(hit.mT),
119                                                  NULL,
120                                                  hit.mObject,
121                                                  mPreprocessor.mPass,
122                                                  simpleRay.mPdf
123                                                  );
124
125          vssRay->mFlags |= VssRay::Valid;
126          vssRay->mDistribution = simpleRay.mDistribution;
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          vssRay->mDistribution = simpleRay.mDistribution;
146          vssRays.push_back(vssRay);
147          ++hits;
148        }
149       
150        // insert the rest of segments
151        for (int i=0; i < ray.intersections.size() - 1; i++) {
152          Ray::Intersection &hitA = ray.intersections[i];
153          Ray::Intersection &hitB = ray.intersections[i + 1];
154          if (hitB.mT - hitA.mT > Limits::Small) {
155                if (DotProd(hitA.mNormal, ray.GetDir()) > 0 &&
156                        DotProd(hitB.mNormal, ray.GetDir()) < 0
157                        ) {
158                 
159                  vssRay = new VssRay(
160                                                          ray.Extrap(hitA.mT),
161                                                          ray.Extrap(hitB.mT),
162                                                          hitA.mObject,
163                                                        hitB.mObject,
164                                                          mPreprocessor.mPass,
165                                                          simpleRay.mPdf
166                                                          );
167                  vssRay->mFlags |= VssRay::Valid;
168                  vssRay->mDistribution = simpleRay.mDistribution;
169                  vssRays.push_back(vssRay);
170                  ++hits;
171                 
172                  vssRay = new VssRay(
173                                                          ray.Extrap(hitB.mT),
174                                                          ray.Extrap(hitA.mT),
175                                                          hitB.mObject,
176                                                          hitA.mObject,
177                                                          mPreprocessor.mPass,
178                                                          simpleRay.mPdf
179                                                          );
180                 
181                  vssRay->mFlags |= VssRay::Valid;
182                  vssRay->mDistribution = simpleRay.mDistribution;
183                  vssRays.push_back(vssRay);
184                  ++hits;
185                }
186          }
187        }
188  }
189 
190  return hits;
191}
192
193
194void InternalRayCaster::CastRays16(SimpleRayContainer &rays,
195                                                                   VssRayContainer &vssRays,
196                                                                   const AxisAlignedBox3 &sbox,
197                                                                   const bool castDoubleRays,
198                                                                   const bool pruneInvalidRays)
199{
200#if DEBUG_RAYCAST
201        Debug << "C16 " << flush;
202#endif
203
204        SimpleRayContainer::const_iterator sit, sit_end = rays.end();
205
206        // no acceleration for ray bundles implemented right now
207        for (sit = rays.begin(); sit != sit_end; ++ sit)
208        {
209                CastRay(*sit, vssRays, sbox, castDoubleRays, pruneInvalidRays);
210        }
211
212#if DEBUG_RAYCAST
213        Debug<<"C16F\n"<<flush;
214#endif
215}
216
217}
Note: See TracBrowser for help on using the repository browser.