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

Revision 1867, 5.2 KB checked in by bittner, 18 years ago (diff)

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