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 RayCaster::ValidateRay(const Vector3 &origin,
|
---|
42 | const Vector3 &direction,
|
---|
43 | const AxisAlignedBox3 &box,
|
---|
44 | Intersection &hit)
|
---|
45 | {
|
---|
46 | if (!hit.mObject) {
|
---|
47 | // compute intersection with the scene bounding box
|
---|
48 | #if EXACT_BOX_CLIPPING
|
---|
49 | static Ray ray;
|
---|
50 | mPreprocessor.SetupRay(ray, origin, direction);
|
---|
51 |
|
---|
52 | float tmin, tmax;
|
---|
53 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax)) {
|
---|
54 | hit.mPoint = ray.Extrap(tmax);
|
---|
55 | }
|
---|
56 | else {
|
---|
57 | // cout<<" invalid hp "<<tmin<<" "<<tmax<<endl;
|
---|
58 | // cout<<" box "<<box<<endl;
|
---|
59 | // cout<<" origin "<<origin<<endl;
|
---|
60 | // cout<<" direction "<<direction<<endl;
|
---|
61 | // cout<< "inv dir"<<ray.GetInvDir()<<endl;
|
---|
62 | return false;
|
---|
63 | }
|
---|
64 | #else
|
---|
65 | hit.mPoint = origin + direction*Magnitude(box.Diagonal());
|
---|
66 | #endif
|
---|
67 | }
|
---|
68 | else if (mPreprocessor.mDetectEmptyViewSpace) {
|
---|
69 | if (DotProd(hit.mNormal, direction) >= 0) {
|
---|
70 | hit.mObject = NULL;
|
---|
71 | return false;
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | return true;
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | int RayCaster::ProcessRay(const SimpleRay &simpleRay,
|
---|
80 | Intersection &hitA,
|
---|
81 | Intersection &hitB,
|
---|
82 | VssRayContainer &vssRays,
|
---|
83 | const AxisAlignedBox3 &box,
|
---|
84 | const bool castDoubleRay,
|
---|
85 | const bool pruneInvalidRays)
|
---|
86 | {
|
---|
87 | int hits = 0;
|
---|
88 |
|
---|
89 | #if DEBUG_RAYCAST
|
---|
90 | Debug<<"PR ";
|
---|
91 | #endif
|
---|
92 |
|
---|
93 | if (pruneInvalidRays)
|
---|
94 | {
|
---|
95 | if (!hitA.mObject && !hitB.mObject) {
|
---|
96 | return 0;
|
---|
97 | }
|
---|
98 |
|
---|
99 | }
|
---|
100 |
|
---|
101 | const bool validA =
|
---|
102 | ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
|
---|
103 |
|
---|
104 | if (!validA && pruneInvalidRays)
|
---|
105 | {
|
---|
106 | return 0;
|
---|
107 | }
|
---|
108 |
|
---|
109 | const bool validB = castDoubleRay &&
|
---|
110 | ValidateRay(simpleRay.mOrigin, -simpleRay.mDirection, box, hitB);
|
---|
111 |
|
---|
112 | if (!validB && pruneInvalidRays)
|
---|
113 | {
|
---|
114 | return 0;
|
---|
115 | }
|
---|
116 |
|
---|
117 | const bool validSample = !pruneInvalidRays || (hitA.mObject != hitB.mObject);
|
---|
118 |
|
---|
119 | if (validSample)
|
---|
120 | {
|
---|
121 | if (!pruneInvalidRays || hitA.mObject)
|
---|
122 | {
|
---|
123 | VssRay *vssRay = new VssRay(
|
---|
124 | hitB.mPoint,
|
---|
125 | hitA.mPoint,
|
---|
126 | hitB.mObject,
|
---|
127 | hitA.mObject,
|
---|
128 | mPreprocessor.mPass,
|
---|
129 | simpleRay.mPdf
|
---|
130 | );
|
---|
131 |
|
---|
132 | vssRays.push_back(vssRay);
|
---|
133 | ++ hits;
|
---|
134 | //cout << "vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
|
---|
135 | }
|
---|
136 |
|
---|
137 | if (castDoubleRay && (!pruneInvalidRays || hitB.mObject))
|
---|
138 | {
|
---|
139 | VssRay *vssRay = new VssRay(
|
---|
140 | hitA.mPoint,
|
---|
141 | hitB.mPoint,
|
---|
142 | hitA.mObject,
|
---|
143 | hitB.mObject,
|
---|
144 | mPreprocessor.mPass,
|
---|
145 | simpleRay.mPdf
|
---|
146 | );
|
---|
147 |
|
---|
148 | vssRays.push_back(vssRay);
|
---|
149 | ++ hits;
|
---|
150 | //cout << "vssray 2: " << *vssRay << endl;
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | return hits;
|
---|
155 | }
|
---|
156 |
|
---|
157 | }
|
---|