[1520] | 1 | #include "RayCaster.h"
|
---|
| 2 | #include "VssRay.h"
|
---|
| 3 | #include "Ray.h"
|
---|
| 4 | #include "Preprocessor.h"
|
---|
[1942] | 5 | #include "ViewCellsManager.h"
|
---|
[1520] | 6 |
|
---|
| 7 |
|
---|
| 8 | namespace GtpVisibilityPreprocessor {
|
---|
| 9 |
|
---|
| 10 |
|
---|
| 11 | #define DEBUG_RAYCAST 0
|
---|
| 12 |
|
---|
[1942] | 13 | #define EXACT_BOX_CLIPPING 0
|
---|
[1520] | 14 |
|
---|
| 15 | RayCaster::RayCaster(const Preprocessor &preprocessor):
|
---|
| 16 | mPreprocessor(preprocessor)
|
---|
| 17 | {
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 |
|
---|
[1521] | 21 | RayCaster::~RayCaster()
|
---|
| 22 | {
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 |
|
---|
[1545] | 26 | VssRay *RayCaster::CastRay(const SimpleRay &simpleRay,
|
---|
[1932] | 27 | const AxisAlignedBox3 &box)
|
---|
| 28 | //const bool castDoubleRay)
|
---|
[1521] | 29 | {
|
---|
[1932] | 30 | // note: make no sense otherwise
|
---|
| 31 | const bool castDoubleRay = false;
|
---|
| 32 | VssRayContainer rays;
|
---|
| 33 | CastRay(simpleRay, rays, box, castDoubleRay);
|
---|
| 34 |
|
---|
| 35 | if (!rays.empty())
|
---|
| 36 | return rays.back();
|
---|
| 37 | else
|
---|
| 38 | return NULL;
|
---|
[1521] | 39 | }
|
---|
| 40 |
|
---|
[1942] | 41 | bool
|
---|
| 42 | RayCaster::ClipToViewSpaceBox(const Vector3 &origin,
|
---|
| 43 | const Vector3 &termination,
|
---|
| 44 | Vector3 &clippedOrigin,
|
---|
| 45 | Vector3 &clippedTermination)
|
---|
| 46 | {
|
---|
| 47 | Ray ray(origin, termination - origin, Ray::LINE_SEGMENT);
|
---|
| 48 | ray.Precompute();
|
---|
| 49 |
|
---|
| 50 | float tmin, tmax;
|
---|
| 51 | if ((!mPreprocessor.mViewCellsManager->
|
---|
| 52 | GetViewSpaceBox().ComputeMinMaxT(ray, &tmin, &tmax)) ||
|
---|
| 53 | tmin>=tmax
|
---|
| 54 | )
|
---|
| 55 | return false;
|
---|
[1521] | 56 |
|
---|
[1942] | 57 | if (tmin >= 1.0f || tmax <=0.0f)
|
---|
| 58 | return false;
|
---|
| 59 |
|
---|
| 60 | if (tmin > 0.0f)
|
---|
| 61 | clippedOrigin = ray.Extrap(tmin);
|
---|
| 62 | else
|
---|
| 63 | clippedOrigin = origin;
|
---|
| 64 |
|
---|
| 65 | if (tmax < 1.0f)
|
---|
| 66 | clippedTermination = ray.Extrap(tmax);
|
---|
| 67 | else
|
---|
| 68 | clippedTermination = termination;
|
---|
| 69 |
|
---|
| 70 | return true;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[1528] | 73 | /** Checks if ray is valid, (e.g., not in empty view space or outside the view space)
|
---|
| 74 | */
|
---|
[1867] | 75 | bool
|
---|
| 76 | RayCaster::ValidateRay(const Vector3 &origin,
|
---|
| 77 | const Vector3 &direction,
|
---|
| 78 | const AxisAlignedBox3 &box,
|
---|
| 79 | Intersection &hit)
|
---|
[1528] | 80 | {
|
---|
[1932] | 81 | if (!hit.mObject)
|
---|
| 82 | {
|
---|
| 83 | // compute intersection with the scene bounding box
|
---|
[1584] | 84 | #if EXACT_BOX_CLIPPING
|
---|
[1932] | 85 | static Ray ray;
|
---|
| 86 | mPreprocessor.SetupRay(ray, origin, direction);
|
---|
| 87 |
|
---|
| 88 | float tmin, tmax;
|
---|
| 89 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax))
|
---|
| 90 | {
|
---|
| 91 | hit.mPoint = ray.Extrap(tmax);
|
---|
| 92 | }
|
---|
| 93 | else
|
---|
| 94 | {
|
---|
| 95 | // cout<<" invalid hp "<<tmin<<" "<<tmax<<endl;
|
---|
| 96 | // cout<<" box "<<box<<endl;
|
---|
| 97 | // cout<<" origin "<<origin<<endl;
|
---|
| 98 | // cout<<" direction "<<direction<<endl;
|
---|
| 99 | // cout<< "inv dir"<<ray.GetInvDir()<<endl;
|
---|
| 100 | return false;
|
---|
| 101 | }
|
---|
[1584] | 102 | #else
|
---|
[1932] | 103 | hit.mPoint = origin + direction * Magnitude(box.Diagonal());
|
---|
[1584] | 104 | #endif
|
---|
| 105 | }
|
---|
[1932] | 106 | else
|
---|
| 107 | {
|
---|
| 108 | if (mPreprocessor.mDetectEmptyViewSpace)
|
---|
| 109 | {
|
---|
| 110 | if (DotProd(hit.mNormal, direction) >= -Limits::Small)
|
---|
| 111 | {
|
---|
| 112 | hit.mObject = NULL;
|
---|
| 113 | return false;
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | return true;
|
---|
[1528] | 119 | }
|
---|
| 120 |
|
---|
| 121 |
|
---|
[1867] | 122 | int
|
---|
| 123 | RayCaster::ProcessRay(const SimpleRay &simpleRay,
|
---|
| 124 | Intersection &hitA,
|
---|
| 125 | Intersection &hitB,
|
---|
| 126 | VssRayContainer &vssRays,
|
---|
| 127 | const AxisAlignedBox3 &box,
|
---|
| 128 | const bool castDoubleRay,
|
---|
| 129 | const bool pruneInvalidRays)
|
---|
[1520] | 130 | {
|
---|
| 131 | int hits = 0;
|
---|
[1528] | 132 |
|
---|
[1520] | 133 | #if DEBUG_RAYCAST
|
---|
[1757] | 134 | Debug<<"PRA"<<flush;
|
---|
[1520] | 135 | #endif
|
---|
[1867] | 136 |
|
---|
| 137 | // regardless of the pruneInvalidRays setting reject rays whic degenerate to a point
|
---|
[1900] | 138 | // if (EpsilonEqualV3(hitA.mPoint, hitB.mPoint, Limits::Small))
|
---|
| 139 | // return 0;
|
---|
[1528] | 140 |
|
---|
[1932] | 141 | if (pruneInvalidRays)
|
---|
| 142 | {
|
---|
| 143 | if (!hitA.mObject && !hitB.mObject)
|
---|
| 144 | {
|
---|
| 145 | return 0;
|
---|
| 146 | }
|
---|
[1520] | 147 | }
|
---|
[1584] | 148 |
|
---|
[1528] | 149 | const bool validA =
|
---|
[1584] | 150 | ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
|
---|
[1867] | 151 |
|
---|
[1932] | 152 | // note: should we check for backward valitidy also for single rays?
|
---|
| 153 | const bool validB = //castDoubleRay &&
|
---|
| 154 | ValidateRay(simpleRay.mOrigin, -simpleRay.mDirection, box, hitB);
|
---|
[1867] | 155 |
|
---|
[1757] | 156 | #if DEBUG_RAYCAST
|
---|
| 157 | Debug<<"PR1"<<flush;
|
---|
| 158 | #endif
|
---|
[1584] | 159 |
|
---|
[1867] | 160 | // reset both contributions
|
---|
[1932] | 161 | if (!validA || !validB)
|
---|
| 162 | {
|
---|
| 163 | if (pruneInvalidRays)
|
---|
| 164 | return 0;
|
---|
| 165 |
|
---|
| 166 | // reset both contributions of this ray
|
---|
| 167 | hitA.mObject = NULL;
|
---|
| 168 | hitB.mObject = NULL;
|
---|
[1867] | 169 | }
|
---|
| 170 |
|
---|
| 171 | // 8.11. 2007 JB
|
---|
| 172 | // degenerate rays checked by geometrical constraint...
|
---|
| 173 | // !pruneInvalidRays || (hitA.mObject != hitB.mObject);
|
---|
[1942] | 174 |
|
---|
[1584] | 175 |
|
---|
[1757] | 176 | #if DEBUG_RAYCAST
|
---|
| 177 | Debug<<"PR2"<<flush;
|
---|
| 178 | #endif
|
---|
[1942] | 179 | const bool validSample = true;
|
---|
| 180 | if (validSample) {
|
---|
| 181 | Vector3 clipA, clipB;
|
---|
| 182 | if (!ClipToViewSpaceBox(hitA.mPoint,
|
---|
| 183 | hitB.mPoint,
|
---|
| 184 | clipA,
|
---|
| 185 | clipB))
|
---|
| 186 | return 0;
|
---|
| 187 | if (!pruneInvalidRays || hitA.mObject) {
|
---|
| 188 | VssRay *vssRay = new VssRay(
|
---|
| 189 | clipB,
|
---|
[1867] | 190 | hitA.mPoint,
|
---|
| 191 | hitB.mObject,
|
---|
| 192 | hitA.mObject,
|
---|
| 193 | mPreprocessor.mPass,
|
---|
[1932] | 194 | simpleRay.mPdf);
|
---|
| 195 |
|
---|
[1867] | 196 | if (validA)
|
---|
[1932] | 197 | vssRay->mFlags |= VssRay::Valid;
|
---|
| 198 |
|
---|
[1883] | 199 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
[1867] | 200 | vssRays.push_back(vssRay);
|
---|
| 201 | ++ hits;
|
---|
| 202 | //cout << "vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
|
---|
[1932] | 203 | }
|
---|
| 204 |
|
---|
[1867] | 205 | #if DEBUG_RAYCAST
|
---|
[1932] | 206 | Debug<<"PR3"<<flush;
|
---|
[1867] | 207 | #endif
|
---|
| 208 |
|
---|
[1942] | 209 | if (castDoubleRay && (!pruneInvalidRays || hitB.mObject))
|
---|
| 210 | {
|
---|
| 211 | VssRay *vssRay = new VssRay(
|
---|
| 212 | clipA,
|
---|
| 213 | hitB.mPoint,
|
---|
| 214 | hitA.mObject,
|
---|
| 215 | hitB.mObject,
|
---|
| 216 | mPreprocessor.mPass,
|
---|
| 217 | simpleRay.mPdf
|
---|
| 218 | );
|
---|
| 219 | if (validB)
|
---|
[1771] | 220 | vssRay->mFlags |= VssRay::Valid;
|
---|
[1932] | 221 |
|
---|
| 222 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
| 223 | vssRays.push_back(vssRay);
|
---|
| 224 | ++ hits;
|
---|
| 225 | //cout << "vssray 2: " << *vssRay << endl;
|
---|
[1942] | 226 | }
|
---|
| 227 | #if DEBUG_RAYCAST
|
---|
| 228 | Debug<<"PR4"<<flush;
|
---|
| 229 | #endif
|
---|
[1520] | 230 | }
|
---|
[1867] | 231 |
|
---|
[1520] | 232 | return hits;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
[1583] | 235 | }
|
---|