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 |
|
---|
13 | RayCaster::RayCaster(const Preprocessor &preprocessor):
|
---|
14 | mPreprocessor(preprocessor)
|
---|
15 | {
|
---|
16 | }
|
---|
17 |
|
---|
18 |
|
---|
19 | RayCaster::~RayCaster()
|
---|
20 | {
|
---|
21 | }
|
---|
22 |
|
---|
23 |
|
---|
24 | VssRay *RayCaster::CastSingleRay(const SimpleRay &simpleRay,
|
---|
25 | const AxisAlignedBox3 &box)
|
---|
26 | {
|
---|
27 | VssRayContainer rays;
|
---|
28 | CastRay(simpleRay, rays, box, false);
|
---|
29 |
|
---|
30 | if (!rays.empty())
|
---|
31 | return rays.back();
|
---|
32 | else
|
---|
33 | return NULL;
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 | /** Checks if ray is valid, (e.g., not in empty view space or outside the view space)
|
---|
38 | */
|
---|
39 | bool RayCaster::ValidateRay(const Vector3 &origin,
|
---|
40 | const Vector3 &direction,
|
---|
41 | const AxisAlignedBox3 &box,
|
---|
42 | Intersection &hit)
|
---|
43 | {
|
---|
44 | if (!hit.mObject)
|
---|
45 | {
|
---|
46 | // compute intersection with the scene bounding box
|
---|
47 | static Ray ray;
|
---|
48 | mPreprocessor.SetupRay(ray, origin, direction);
|
---|
49 |
|
---|
50 | float tmin, tmax;
|
---|
51 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax))
|
---|
52 | {
|
---|
53 | hit.mPoint = ray.Extrap(tmax);
|
---|
54 | }
|
---|
55 | else
|
---|
56 | {
|
---|
57 | return false;
|
---|
58 | }
|
---|
59 | }
|
---|
60 | else if (mPreprocessor.mDetectEmptyViewSpace)
|
---|
61 | {
|
---|
62 | if (DotProd(hit.mNormal, direction) >= 0)
|
---|
63 | {
|
---|
64 | hit.mObject = NULL;
|
---|
65 | return false;
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | return true;
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | int RayCaster::ProcessRay(const SimpleRay &simpleRay,
|
---|
74 | Intersection &hitA,
|
---|
75 | Intersection &hitB,
|
---|
76 | VssRayContainer &vssRays,
|
---|
77 | const AxisAlignedBox3 &box,
|
---|
78 | const bool castDoubleRay,
|
---|
79 | const bool pruneInvalidRays)
|
---|
80 | {
|
---|
81 | int hits = 0;
|
---|
82 |
|
---|
83 | #if DEBUG_RAYCAST
|
---|
84 | Debug<<"PR ";
|
---|
85 | #endif
|
---|
86 |
|
---|
87 | if (pruneInvalidRays)
|
---|
88 | {
|
---|
89 | if (!hitA.mObject && !hitB.mObject)
|
---|
90 | {
|
---|
91 | return 0;
|
---|
92 | }
|
---|
93 |
|
---|
94 | // inside test for bounding box
|
---|
95 | // enlarge box slightly so the view point fits for sure
|
---|
96 | AxisAlignedBox3 sbox = box;
|
---|
97 | sbox.Enlarge(Vector3(-Limits::Small));
|
---|
98 |
|
---|
99 | if (!sbox.IsInside(simpleRay.mOrigin))
|
---|
100 | {
|
---|
101 | return 0;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | // cout << "here81 " << hitA.mObject << endl;
|
---|
105 | const bool validA =
|
---|
106 | ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
|
---|
107 | if (!validA && pruneInvalidRays)
|
---|
108 | {
|
---|
109 | return 0;
|
---|
110 | }
|
---|
111 |
|
---|
112 | const bool validB = castDoubleRay &&
|
---|
113 | ValidateRay(simpleRay.mOrigin, -simpleRay.mDirection, box, hitB);
|
---|
114 |
|
---|
115 | if (!validB && pruneInvalidRays)
|
---|
116 | {
|
---|
117 | return 0;
|
---|
118 | }
|
---|
119 |
|
---|
120 | const bool validSample = !pruneInvalidRays || (hitA.mObject != hitB.mObject);
|
---|
121 |
|
---|
122 | if (validSample)
|
---|
123 | {
|
---|
124 | if (!pruneInvalidRays || hitA.mObject)
|
---|
125 | {
|
---|
126 | VssRay *vssRay = new VssRay(
|
---|
127 | hitB.mPoint,
|
---|
128 | hitA.mPoint,
|
---|
129 | hitB.mObject,
|
---|
130 | hitA.mObject,
|
---|
131 | mPreprocessor.mPass,
|
---|
132 | simpleRay.mPdf
|
---|
133 | );
|
---|
134 |
|
---|
135 | vssRays.push_back(vssRay);
|
---|
136 | ++ hits;
|
---|
137 | //cout << "here70 vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
|
---|
138 | }
|
---|
139 |
|
---|
140 | if (castDoubleRay && (!pruneInvalidRays || hitB.mObject))
|
---|
141 | {
|
---|
142 | VssRay *vssRay = new VssRay(
|
---|
143 | hitA.mPoint,
|
---|
144 | hitB.mPoint,
|
---|
145 | hitA.mObject,
|
---|
146 | hitB.mObject,
|
---|
147 | mPreprocessor.mPass,
|
---|
148 | simpleRay.mPdf
|
---|
149 | );
|
---|
150 |
|
---|
151 | vssRays.push_back(vssRay);
|
---|
152 | ++ hits;
|
---|
153 | //cout << "here71 vssray 2: " << *vssRay << endl;
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | return hits;
|
---|
158 | }
|
---|
159 |
|
---|
160 | } |
---|