1 | #include "InternalRayCaster.h"
|
---|
2 | #include "VssRay.h"
|
---|
3 | #include "KdTree.h"
|
---|
4 | #include "Preprocessor.h"
|
---|
5 |
|
---|
6 | #define DEBUG_RAYCAST 0
|
---|
7 |
|
---|
8 |
|
---|
9 | namespace GtpVisibilityPreprocessor {
|
---|
10 |
|
---|
11 |
|
---|
12 | InternalRayCaster::InternalRayCaster(const Preprocessor &preprocessor, KdTree *kdTree):
|
---|
13 | RayCaster(preprocessor), mKdTree(kdTree)
|
---|
14 | {
|
---|
15 | }
|
---|
16 |
|
---|
17 |
|
---|
18 | InternalRayCaster::~InternalRayCaster()
|
---|
19 | {
|
---|
20 | }
|
---|
21 |
|
---|
22 |
|
---|
23 | int 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 |
|
---|
82 | int
|
---|
83 | InternalRayCaster::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 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
128 | vssRays.push_back(vssRay);
|
---|
129 | ++hits;
|
---|
130 | }
|
---|
131 |
|
---|
132 | hit = ray.intersections[ray.intersections.size()-1];
|
---|
133 | if (DotProd(hit.mNormal, ray.GetDir()) > 0) {
|
---|
134 | // cout<<"L:"<<tmax<<" "<<hit.mT<<endl;
|
---|
135 |
|
---|
136 | // insert termination segment
|
---|
137 | vssRay = new VssRay(
|
---|
138 | ray.Extrap(tmax),
|
---|
139 | ray.Extrap(hit.mT),
|
---|
140 | NULL,
|
---|
141 | hit.mObject,
|
---|
142 | mPreprocessor.mPass,
|
---|
143 | simpleRay.mPdf
|
---|
144 | );
|
---|
145 | vssRay->mFlags |= VssRay::Valid;
|
---|
146 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
147 | vssRays.push_back(vssRay);
|
---|
148 | ++hits;
|
---|
149 | }
|
---|
150 |
|
---|
151 | // insert the rest of segments
|
---|
152 | for (int i=0; i < ray.intersections.size() - 1; i++) {
|
---|
153 | Ray::Intersection &hitA = ray.intersections[i];
|
---|
154 | Ray::Intersection &hitB = ray.intersections[i + 1];
|
---|
155 | if (hitB.mT - hitA.mT > Limits::Small) {
|
---|
156 | if (DotProd(hitA.mNormal, ray.GetDir()) > 0 &&
|
---|
157 | DotProd(hitB.mNormal, ray.GetDir()) < 0
|
---|
158 | ) {
|
---|
159 |
|
---|
160 | vssRay = new VssRay(
|
---|
161 | ray.Extrap(hitA.mT),
|
---|
162 | ray.Extrap(hitB.mT),
|
---|
163 | hitA.mObject,
|
---|
164 | hitB.mObject,
|
---|
165 | mPreprocessor.mPass,
|
---|
166 | simpleRay.mPdf
|
---|
167 | );
|
---|
168 | vssRay->mFlags |= VssRay::Valid;
|
---|
169 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
170 | vssRays.push_back(vssRay);
|
---|
171 | ++hits;
|
---|
172 |
|
---|
173 | vssRay = new VssRay(
|
---|
174 | ray.Extrap(hitB.mT),
|
---|
175 | ray.Extrap(hitA.mT),
|
---|
176 | hitB.mObject,
|
---|
177 | hitA.mObject,
|
---|
178 | mPreprocessor.mPass,
|
---|
179 | simpleRay.mPdf
|
---|
180 | );
|
---|
181 |
|
---|
182 | vssRay->mFlags |= VssRay::Valid;
|
---|
183 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
184 | vssRays.push_back(vssRay);
|
---|
185 | ++hits;
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | return hits;
|
---|
192 | }
|
---|
193 |
|
---|
194 | void InternalRayCaster::CastRays16(const int index,
|
---|
195 | SimpleRayContainer &rays,
|
---|
196 | VssRayContainer &vssRays,
|
---|
197 | const AxisAlignedBox3 &sbox,
|
---|
198 | const bool castDoubleRays,
|
---|
199 | const bool pruneInvalidRays)
|
---|
200 | {
|
---|
201 | const int num = 16;
|
---|
202 |
|
---|
203 | #if DEBUG_RAYCAST
|
---|
204 | Debug << "C16 " << flush;
|
---|
205 | #endif
|
---|
206 |
|
---|
207 | // no acceleration for ray bundles implemented right now
|
---|
208 | for (int i = index; i < index + num; i++)
|
---|
209 | {
|
---|
210 | CastRay(rays[i], vssRays, sbox, castDoubleRays, pruneInvalidRays);
|
---|
211 | }
|
---|
212 |
|
---|
213 | #if DEBUG_RAYCAST
|
---|
214 | Debug<<"C16F\n"<<flush;
|
---|
215 | #endif
|
---|
216 | }
|
---|
217 |
|
---|
218 | }
|
---|