[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,
|
---|
[1990] | 27 | const AxisAlignedBox3 &box,
|
---|
[1996] | 28 | const bool castDoubleRay)
|
---|
[1521] | 29 | {
|
---|
[1932] | 30 | VssRayContainer rays;
|
---|
[1996] | 31 | CastRay(simpleRay, rays, box, castDoubleRay, true);
|
---|
[1932] | 32 |
|
---|
| 33 | if (!rays.empty())
|
---|
| 34 | return rays.back();
|
---|
| 35 | else
|
---|
| 36 | return NULL;
|
---|
[1521] | 37 | }
|
---|
| 38 |
|
---|
[1990] | 39 |
|
---|
[1942] | 40 | bool
|
---|
| 41 | RayCaster::ClipToViewSpaceBox(const Vector3 &origin,
|
---|
| 42 | const Vector3 &termination,
|
---|
| 43 | Vector3 &clippedOrigin,
|
---|
| 44 | Vector3 &clippedTermination)
|
---|
| 45 | {
|
---|
[1952] | 46 |
|
---|
[1942] | 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 |
|
---|
[1974] | 121 | void
|
---|
| 122 | RayCaster::SortRays(SimpleRayContainer &rays)
|
---|
| 123 | {
|
---|
[1984] | 124 | AxisAlignedBox3 box =
|
---|
| 125 | mPreprocessor.mViewCellsManager->GetViewSpaceBox();
|
---|
| 126 |
|
---|
| 127 | float b[12]={
|
---|
| 128 | box.Min().x,
|
---|
| 129 | box.Min().y,
|
---|
| 130 | box.Min().z,
|
---|
| 131 | -1,
|
---|
| 132 | -1,
|
---|
| 133 | -1,
|
---|
| 134 | box.Max().x,
|
---|
| 135 | box.Max().y,
|
---|
| 136 | box.Max().z,
|
---|
| 137 | 1,
|
---|
| 138 | 1,
|
---|
| 139 | 1
|
---|
| 140 | };
|
---|
| 141 |
|
---|
| 142 | _SortRays(rays,
|
---|
| 143 | 0,
|
---|
[1990] | 144 | (int)rays.size()-1,
|
---|
[1984] | 145 | 0,
|
---|
| 146 | b
|
---|
| 147 | );
|
---|
[1974] | 148 | }
|
---|
| 149 |
|
---|
| 150 | void
|
---|
| 151 | RayCaster::_SortRays(SimpleRayContainer &rays,
|
---|
| 152 | const int l,
|
---|
| 153 | const int r,
|
---|
[1984] | 154 | const int depth,
|
---|
| 155 | float box[12])
|
---|
[1974] | 156 | {
|
---|
| 157 | // pick-up a pivot
|
---|
[1984] | 158 | int axis;
|
---|
| 159 |
|
---|
| 160 | float maxDiff = -1.0f;
|
---|
| 161 | // get the largest axis
|
---|
| 162 | int offset = 0;
|
---|
[1990] | 163 | int i;
|
---|
[1984] | 164 |
|
---|
[2008] | 165 | const int batchsize = 8192;
|
---|
| 166 |
|
---|
| 167 | // if (r - l < 32*batchsize)
|
---|
[2002] | 168 | // offset = 3;
|
---|
[2008] | 169 |
|
---|
| 170 | // if (depth%5==0)
|
---|
| 171 | // offset = 3;
|
---|
[1984] | 172 |
|
---|
[2002] | 173 | for (i=offset; i < offset + 3; i++) {
|
---|
| 174 | float diff = box[i + 6] - box[i];
|
---|
[1984] | 175 | if (diff > maxDiff) {
|
---|
| 176 | maxDiff = diff;
|
---|
| 177 | axis = i;
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[2008] | 181 | // cout<<depth<<" "<<axis<<" "<<l<<" "<<r<<endl;
|
---|
[1984] | 182 |
|
---|
[1990] | 183 | i=l;
|
---|
| 184 | int j=r;
|
---|
| 185 |
|
---|
[1984] | 186 | float x = (box[axis] + box[axis+6])*0.5f;
|
---|
| 187 | // float x = rays[(l+r)/2].GetParam(axis);
|
---|
[1974] | 188 | do {
|
---|
[2008] | 189 | while(i<j && rays[i].GetParam(axis) < x)
|
---|
[1974] | 190 | i++;
|
---|
[2008] | 191 | while(i<j && x < rays[j].GetParam(axis))
|
---|
[1974] | 192 | j--;
|
---|
| 193 |
|
---|
| 194 | if (i <= j) {
|
---|
| 195 | swap(rays[i], rays[j]);
|
---|
| 196 | i++;
|
---|
| 197 | j--;
|
---|
| 198 | }
|
---|
| 199 | } while (i<=j);
|
---|
[2008] | 200 |
|
---|
[1974] | 201 |
|
---|
[2008] | 202 | if (l + batchsize < j ) {
|
---|
[1984] | 203 | // set new max
|
---|
| 204 | float save = box[axis+6];
|
---|
| 205 | box[axis+6] = x;
|
---|
| 206 | _SortRays(rays, l, j, depth+1, box);
|
---|
| 207 | box[axis+6] = save;
|
---|
[2008] | 208 | } else {
|
---|
| 209 | // for (int k=0; k < 6; k++)
|
---|
| 210 | // cout<<k<<" "<<box[k]<<" - "<<box[k+6]<<endl;
|
---|
[1984] | 211 | }
|
---|
[1974] | 212 |
|
---|
[2008] | 213 | if (i + batchsize < r) {
|
---|
[1984] | 214 | // set new min
|
---|
| 215 | box[axis] = x;
|
---|
| 216 | _SortRays(rays, i, r, depth+1, box);
|
---|
[2008] | 217 | } else {
|
---|
| 218 | // for (int k=0; k < 6; k++)
|
---|
| 219 | // cout<<k<<" "<<box[k]<<" - "<<box[k+6]<<endl;
|
---|
[1984] | 220 | }
|
---|
[2008] | 221 |
|
---|
[1974] | 222 | }
|
---|
| 223 |
|
---|
| 224 |
|
---|
[1528] | 225 |
|
---|
[1867] | 226 | int
|
---|
| 227 | RayCaster::ProcessRay(const SimpleRay &simpleRay,
|
---|
| 228 | Intersection &hitA,
|
---|
| 229 | Intersection &hitB,
|
---|
| 230 | VssRayContainer &vssRays,
|
---|
| 231 | const AxisAlignedBox3 &box,
|
---|
| 232 | const bool castDoubleRay,
|
---|
[1996] | 233 | const bool pruneInvalidRays)
|
---|
[1520] | 234 | {
|
---|
[1984] | 235 | int hits = 0;
|
---|
[1528] | 236 |
|
---|
[1520] | 237 | #if DEBUG_RAYCAST
|
---|
[1952] | 238 | static int id=0;
|
---|
| 239 | Debug<<"PRA "<<id++<<endl<<flush;
|
---|
[1520] | 240 | #endif
|
---|
[1528] | 241 |
|
---|
[1932] | 242 | if (pruneInvalidRays)
|
---|
| 243 | {
|
---|
[1952] | 244 | if (!hitA.mObject && !hitB.mObject) {
|
---|
| 245 | return 0;
|
---|
| 246 | }
|
---|
[1520] | 247 | }
|
---|
[1966] | 248 |
|
---|
[1584] | 249 |
|
---|
[1952] | 250 | // regardless of the pruneInvalidRays setting reject rays whic degenerate to a point
|
---|
[1966] | 251 | if (EpsilonEqualV3(hitA.mPoint, hitB.mPoint, Limits::Small)) {
|
---|
[1990] | 252 | return 0;
|
---|
[1966] | 253 | }
|
---|
| 254 |
|
---|
| 255 |
|
---|
[1952] | 256 | const bool validA = ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
|
---|
[1932] | 257 | const bool validB = //castDoubleRay &&
|
---|
[1952] | 258 | ValidateRay(simpleRay.mOrigin, -simpleRay.mDirection, box, hitB);
|
---|
[1867] | 259 |
|
---|
[1952] | 260 |
|
---|
[1757] | 261 | #if DEBUG_RAYCAST
|
---|
| 262 | Debug<<"PR1"<<flush;
|
---|
| 263 | #endif
|
---|
[1584] | 264 |
|
---|
[1867] | 265 | // reset both contributions
|
---|
[1952] | 266 | if (!validA || !validB) {
|
---|
| 267 | if (pruneInvalidRays)
|
---|
| 268 | return 0;
|
---|
[1867] | 269 |
|
---|
[1952] | 270 | // reset both contributions of this ray
|
---|
| 271 | hitA.mObject = NULL;
|
---|
| 272 | hitB.mObject = NULL;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
[1867] | 275 | // 8.11. 2007 JB
|
---|
| 276 | // degenerate rays checked by geometrical constraint...
|
---|
| 277 | // !pruneInvalidRays || (hitA.mObject != hitB.mObject);
|
---|
[1942] | 278 |
|
---|
[1584] | 279 |
|
---|
[1757] | 280 | #if DEBUG_RAYCAST
|
---|
| 281 | Debug<<"PR2"<<flush;
|
---|
| 282 | #endif
|
---|
[1942] | 283 | const bool validSample = true;
|
---|
| 284 | if (validSample) {
|
---|
| 285 | Vector3 clipA, clipB;
|
---|
| 286 | if (!ClipToViewSpaceBox(hitA.mPoint,
|
---|
| 287 | hitB.mPoint,
|
---|
| 288 | clipA,
|
---|
| 289 | clipB))
|
---|
| 290 | return 0;
|
---|
[1952] | 291 |
|
---|
[1942] | 292 | if (!pruneInvalidRays || hitA.mObject) {
|
---|
[2012] | 293 | VssRay *vssRay = mVssRayPool.Alloc();
|
---|
[1932] | 294 |
|
---|
[2012] | 295 | *vssRay = VssRay(
|
---|
| 296 | !castDoubleRay ? simpleRay.mOrigin : clipB,
|
---|
| 297 | hitA.mPoint,
|
---|
| 298 | hitB.mObject,
|
---|
| 299 | hitA.mObject,
|
---|
| 300 | mPreprocessor.mPass,
|
---|
| 301 | simpleRay.mPdf
|
---|
| 302 | );
|
---|
| 303 |
|
---|
[1867] | 304 | if (validA)
|
---|
[1932] | 305 | vssRay->mFlags |= VssRay::Valid;
|
---|
| 306 |
|
---|
[1883] | 307 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
[1989] | 308 | vssRay->mGeneratorId = simpleRay.mGeneratorId;
|
---|
| 309 |
|
---|
[1867] | 310 | vssRays.push_back(vssRay);
|
---|
| 311 | ++ hits;
|
---|
| 312 | //cout << "vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
|
---|
[1932] | 313 | }
|
---|
| 314 |
|
---|
[1867] | 315 | #if DEBUG_RAYCAST
|
---|
[1932] | 316 | Debug<<"PR3"<<flush;
|
---|
[1867] | 317 | #endif
|
---|
| 318 |
|
---|
[1942] | 319 | if (castDoubleRay && (!pruneInvalidRays || hitB.mObject))
|
---|
| 320 | {
|
---|
[2012] | 321 | VssRay *vssRay = mVssRayPool.Alloc();
|
---|
| 322 | *vssRay = VssRay(
|
---|
| 323 | clipA,
|
---|
| 324 | hitB.mPoint,
|
---|
| 325 | hitA.mObject,
|
---|
| 326 | hitB.mObject,
|
---|
| 327 | mPreprocessor.mPass,
|
---|
| 328 | simpleRay.mPdf
|
---|
| 329 | );
|
---|
[1942] | 330 | if (validB)
|
---|
[1771] | 331 | vssRay->mFlags |= VssRay::Valid;
|
---|
[1932] | 332 |
|
---|
| 333 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
[1989] | 334 | vssRay->mGeneratorId = simpleRay.mGeneratorId;
|
---|
[1932] | 335 | vssRays.push_back(vssRay);
|
---|
| 336 | ++ hits;
|
---|
| 337 | //cout << "vssray 2: " << *vssRay << endl;
|
---|
[1942] | 338 | }
|
---|
| 339 | #if DEBUG_RAYCAST
|
---|
| 340 | Debug<<"PR4"<<flush;
|
---|
| 341 | #endif
|
---|
[1520] | 342 | }
|
---|
[1867] | 343 |
|
---|
[1520] | 344 | return hits;
|
---|
| 345 | }
|
---|
| 346 |
|
---|
[1583] | 347 | }
|
---|