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):
|
---|
13 | RayCaster(preprocessor)
|
---|
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 | if (simpleRay.mType == Ray::GLOBAL_RAY)
|
---|
30 | return CastGlobalRay(simpleRay, vssRays, box, pruneInvalidRays);
|
---|
31 |
|
---|
32 | //cout << "internal ray" << endl;
|
---|
33 | static Ray ray;
|
---|
34 | int hits = 0;
|
---|
35 | Intersection hitA(simpleRay.mOrigin), hitB(simpleRay.mOrigin);
|
---|
36 |
|
---|
37 | // inside test for bounding box
|
---|
38 | // enlarge box slightly so the view point fits for sure
|
---|
39 | // AxisAlignedBox3 sbox = box;
|
---|
40 | // sbox.Enlarge(Vector3(-Limits::Small));
|
---|
41 | // $$ JB moved here from Validate routine
|
---|
42 |
|
---|
43 | // if (!box.IsInside(simpleRay.mOrigin)) {
|
---|
44 | // cout<<"out of box "<<simpleRay.mOrigin<<" "<<box<<endl;
|
---|
45 | // return 0;
|
---|
46 | // }
|
---|
47 |
|
---|
48 | mPreprocessor.SetupRay(ray, simpleRay.mOrigin, simpleRay.mDirection);
|
---|
49 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
50 |
|
---|
51 |
|
---|
52 | if (mPreprocessor.mKdTree->CastRay(ray)) {
|
---|
53 | hitA.mObject = ray.intersections[0].mObject;
|
---|
54 | hitA.mPoint = ray.Extrap(ray.intersections[0].mT);
|
---|
55 | hitA.mNormal = ray.intersections[0].mNormal;
|
---|
56 | // cout << "hita: " << hitA.mPoint << " !obj: " << hitA.mObject << endl;
|
---|
57 | }
|
---|
58 |
|
---|
59 | mPreprocessor.SetupRay(ray, simpleRay.mOrigin, -simpleRay.mDirection);
|
---|
60 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
61 |
|
---|
62 | if (castDoubleRay && mPreprocessor.mKdTree->CastRay(ray)) {
|
---|
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 |
|
---|
80 | int
|
---|
81 | InternalRayCaster::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 (mPreprocessor.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 | 1.0f //simpleRay.mPdf
|
---|
122 | );
|
---|
123 |
|
---|
124 | vssRay->mFlags |= VssRay::Valid;
|
---|
125 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
126 | vssRays.push_back(vssRay);
|
---|
127 | ++hits;
|
---|
128 | }
|
---|
129 |
|
---|
130 | hit = ray.intersections[ray.intersections.size()-1];
|
---|
131 | if (DotProd(hit.mNormal, ray.GetDir()) > 0) {
|
---|
132 | // cout<<"L:"<<tmax<<" "<<hit.mT<<endl;
|
---|
133 |
|
---|
134 | // insert termination segment
|
---|
135 | vssRay = new VssRay(
|
---|
136 | ray.Extrap(tmax),
|
---|
137 | ray.Extrap(hit.mT),
|
---|
138 | NULL,
|
---|
139 | hit.mObject,
|
---|
140 | mPreprocessor.mPass,
|
---|
141 | 1.0f //simpleRay.mPdf
|
---|
142 | );
|
---|
143 | vssRay->mFlags |= VssRay::Valid;
|
---|
144 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
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 | 1.0f //simpleRay.mPdf
|
---|
165 | );
|
---|
166 | vssRay->mFlags |= VssRay::Valid;
|
---|
167 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
168 | vssRays.push_back(vssRay);
|
---|
169 | ++hits;
|
---|
170 |
|
---|
171 | vssRay = new VssRay(
|
---|
172 | ray.Extrap(hitB.mT),
|
---|
173 | ray.Extrap(hitA.mT),
|
---|
174 | hitB.mObject,
|
---|
175 | hitA.mObject,
|
---|
176 | mPreprocessor.mPass,
|
---|
177 | 1.0f //simpleRay.mPdf
|
---|
178 | );
|
---|
179 |
|
---|
180 | vssRay->mFlags |= VssRay::Valid;
|
---|
181 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
182 | vssRays.push_back(vssRay);
|
---|
183 | ++hits;
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | return hits;
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | void InternalRayCaster::CastRays16(SimpleRayContainer &rays,
|
---|
194 | VssRayContainer &vssRays,
|
---|
195 | const AxisAlignedBox3 &sbox,
|
---|
196 | const bool castDoubleRays,
|
---|
197 | const bool pruneInvalidRays)
|
---|
198 | {
|
---|
199 | #if DEBUG_RAYCAST
|
---|
200 | Debug << "C16 " << flush;
|
---|
201 | #endif
|
---|
202 |
|
---|
203 | SimpleRayContainer::const_iterator sit, sit_end = rays.end();
|
---|
204 |
|
---|
205 | // no acceleration for ray bundles implemented right now
|
---|
206 | for (sit = rays.begin(); sit != sit_end; ++ sit)
|
---|
207 | {
|
---|
208 | CastRay(*sit, vssRays, sbox, castDoubleRays, pruneInvalidRays);
|
---|
209 | }
|
---|
210 |
|
---|
211 | #if DEBUG_RAYCAST
|
---|
212 | Debug<<"C16F\n"<<flush;
|
---|
213 | #endif
|
---|
214 | }
|
---|
215 |
|
---|
216 | }
|
---|