1 | #include "RayCaster.h"
|
---|
2 | #include "VssRay.h"
|
---|
3 | #include "Ray.h"
|
---|
4 | #include "Preprocessor.h"
|
---|
5 |
|
---|
6 |
|
---|
7 | namespace GtpVisibilityPreprocessor {
|
---|
8 |
|
---|
9 |
|
---|
10 | #define DEBUG_RAYCAST 0
|
---|
11 |
|
---|
12 | #define EXACT_BOX_CLIPPING 1
|
---|
13 |
|
---|
14 | RayCaster::RayCaster(const Preprocessor &preprocessor):
|
---|
15 | mPreprocessor(preprocessor)
|
---|
16 | {
|
---|
17 | }
|
---|
18 |
|
---|
19 |
|
---|
20 | RayCaster::~RayCaster()
|
---|
21 | {
|
---|
22 | }
|
---|
23 |
|
---|
24 |
|
---|
25 | VssRay *RayCaster::CastRay(const SimpleRay &simpleRay,
|
---|
26 | const AxisAlignedBox3 &box,
|
---|
27 | const bool castDoubleRay)
|
---|
28 | {
|
---|
29 | VssRayContainer rays;
|
---|
30 | CastRay(simpleRay, rays, box, castDoubleRay);
|
---|
31 |
|
---|
32 | if (!rays.empty())
|
---|
33 | return rays.back();
|
---|
34 | else
|
---|
35 | return NULL;
|
---|
36 | }
|
---|
37 |
|
---|
38 |
|
---|
39 | /** Checks if ray is valid, (e.g., not in empty view space or outside the view space)
|
---|
40 | */
|
---|
41 | bool
|
---|
42 | RayCaster::ValidateRay(const Vector3 &origin,
|
---|
43 | const Vector3 &direction,
|
---|
44 | const AxisAlignedBox3 &box,
|
---|
45 | Intersection &hit)
|
---|
46 | {
|
---|
47 | if (!hit.mObject) {
|
---|
48 | // compute intersection with the scene bounding box
|
---|
49 | #if EXACT_BOX_CLIPPING
|
---|
50 | static Ray ray;
|
---|
51 | mPreprocessor.SetupRay(ray, origin, direction);
|
---|
52 |
|
---|
53 | float tmin, tmax;
|
---|
54 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax)) {
|
---|
55 | hit.mPoint = ray.Extrap(tmax);
|
---|
56 | }
|
---|
57 | else {
|
---|
58 | // cout<<" invalid hp "<<tmin<<" "<<tmax<<endl;
|
---|
59 | // cout<<" box "<<box<<endl;
|
---|
60 | // cout<<" origin "<<origin<<endl;
|
---|
61 | // cout<<" direction "<<direction<<endl;
|
---|
62 | // cout<< "inv dir"<<ray.GetInvDir()<<endl;
|
---|
63 | return false;
|
---|
64 | }
|
---|
65 | #else
|
---|
66 | hit.mPoint = origin + direction*Magnitude(box.Diagonal());
|
---|
67 | #endif
|
---|
68 | }
|
---|
69 | else
|
---|
70 | if (mPreprocessor.mDetectEmptyViewSpace) {
|
---|
71 | if (DotProd(hit.mNormal, direction) >= -Limits::Small) {
|
---|
72 | hit.mObject = NULL;
|
---|
73 | return false;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | return true;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | int
|
---|
82 | RayCaster::ProcessRay(const SimpleRay &simpleRay,
|
---|
83 | Intersection &hitA,
|
---|
84 | Intersection &hitB,
|
---|
85 | VssRayContainer &vssRays,
|
---|
86 | const AxisAlignedBox3 &box,
|
---|
87 | const bool castDoubleRay,
|
---|
88 | const bool pruneInvalidRays)
|
---|
89 | {
|
---|
90 | int hits = 0;
|
---|
91 |
|
---|
92 | #if DEBUG_RAYCAST
|
---|
93 | Debug<<"PRA"<<flush;
|
---|
94 | #endif
|
---|
95 |
|
---|
96 | // regardless of the pruneInvalidRays setting reject rays whic degenerate to a point
|
---|
97 | if (EpsilonEqualV3(hitA.mPoint, hitB.mPoint, Limits::Small))
|
---|
98 | return 0;
|
---|
99 |
|
---|
100 | if (pruneInvalidRays) {
|
---|
101 | if (!hitA.mObject && !hitB.mObject) {
|
---|
102 | return 0;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | const bool validA =
|
---|
107 | ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
|
---|
108 |
|
---|
109 | const bool validB = castDoubleRay &&
|
---|
110 | ValidateRay(simpleRay.mOrigin, -simpleRay.mDirection, box, hitB);
|
---|
111 |
|
---|
112 | #if DEBUG_RAYCAST
|
---|
113 | Debug<<"PR1"<<flush;
|
---|
114 | #endif
|
---|
115 |
|
---|
116 | // reset both contributions
|
---|
117 | if (!validA || !validB) {
|
---|
118 | if (pruneInvalidRays)
|
---|
119 | return 0;
|
---|
120 | // reset both contributions of this ray
|
---|
121 | hitA.mObject = NULL;
|
---|
122 | hitB.mObject = NULL;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | const bool validSample = true;
|
---|
127 | // 8.11. 2007 JB
|
---|
128 | // degenerate rays checked by geometrical constraint...
|
---|
129 | // !pruneInvalidRays || (hitA.mObject != hitB.mObject);
|
---|
130 |
|
---|
131 | #if DEBUG_RAYCAST
|
---|
132 | Debug<<"PR2"<<flush;
|
---|
133 | #endif
|
---|
134 |
|
---|
135 | if (validSample) {
|
---|
136 | if (!pruneInvalidRays || hitA.mObject) {
|
---|
137 | VssRay *vssRay = new VssRay(
|
---|
138 | hitB.mPoint,
|
---|
139 | hitA.mPoint,
|
---|
140 | hitB.mObject,
|
---|
141 | hitA.mObject,
|
---|
142 | mPreprocessor.mPass,
|
---|
143 | simpleRay.mPdf
|
---|
144 | );
|
---|
145 |
|
---|
146 | if (validA)
|
---|
147 | vssRay->mFlags |= VssRay::Valid;
|
---|
148 | vssRay->mGeneratingRayId = simpleRay.mId;
|
---|
149 | vssRays.push_back(vssRay);
|
---|
150 | ++ hits;
|
---|
151 | //cout << "vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
|
---|
152 | }
|
---|
153 |
|
---|
154 | #if DEBUG_RAYCAST
|
---|
155 | Debug<<"PR3"<<flush;
|
---|
156 | #endif
|
---|
157 |
|
---|
158 | if (castDoubleRay && (!pruneInvalidRays || hitB.mObject))
|
---|
159 | {
|
---|
160 | VssRay *vssRay = new VssRay(
|
---|
161 | hitA.mPoint,
|
---|
162 | hitB.mPoint,
|
---|
163 | hitA.mObject,
|
---|
164 | hitB.mObject,
|
---|
165 | mPreprocessor.mPass,
|
---|
166 | simpleRay.mPdf
|
---|
167 | );
|
---|
168 | if (validB)
|
---|
169 | vssRay->mFlags |= VssRay::Valid;
|
---|
170 |
|
---|
171 | vssRay->mGeneratingRayId = simpleRay.mId;
|
---|
172 | vssRays.push_back(vssRay);
|
---|
173 | ++ hits;
|
---|
174 | //cout << "vssray 2: " << *vssRay << endl;
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | #if DEBUG_RAYCAST
|
---|
179 | Debug<<"PR4"<<flush;
|
---|
180 | #endif
|
---|
181 |
|
---|
182 | return hits;
|
---|
183 | }
|
---|
184 |
|
---|
185 | }
|
---|