[1520] | 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 |
|
---|
[1584] | 12 | #define EXACT_BOX_CLIPPING 1
|
---|
[1520] | 13 |
|
---|
| 14 | RayCaster::RayCaster(const Preprocessor &preprocessor):
|
---|
| 15 | mPreprocessor(preprocessor)
|
---|
| 16 | {
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 |
|
---|
[1521] | 20 | RayCaster::~RayCaster()
|
---|
| 21 | {
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 |
|
---|
[1545] | 25 | VssRay *RayCaster::CastRay(const SimpleRay &simpleRay,
|
---|
| 26 | const AxisAlignedBox3 &box,
|
---|
| 27 | const bool castDoubleRay)
|
---|
[1521] | 28 | {
|
---|
[1583] | 29 | VssRayContainer rays;
|
---|
| 30 | CastRay(simpleRay, rays, box, castDoubleRay);
|
---|
| 31 |
|
---|
| 32 | if (!rays.empty())
|
---|
| 33 | return rays.back();
|
---|
| 34 | else
|
---|
| 35 | return NULL;
|
---|
[1521] | 36 | }
|
---|
| 37 |
|
---|
| 38 |
|
---|
[1528] | 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 | {
|
---|
[1584] | 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
|
---|
[1528] | 67 | }
|
---|
[1584] | 68 | else if (mPreprocessor.mDetectEmptyViewSpace) {
|
---|
| 69 | if (DotProd(hit.mNormal, direction) >= 0) {
|
---|
| 70 | hit.mObject = NULL;
|
---|
| 71 | return false;
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[1528] | 75 | return true;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 |
|
---|
| 79 | int RayCaster::ProcessRay(const SimpleRay &simpleRay,
|
---|
| 80 | Intersection &hitA,
|
---|
| 81 | Intersection &hitB,
|
---|
[1520] | 82 | VssRayContainer &vssRays,
|
---|
[1528] | 83 | const AxisAlignedBox3 &box,
|
---|
| 84 | const bool castDoubleRay,
|
---|
| 85 | const bool pruneInvalidRays)
|
---|
[1520] | 86 | {
|
---|
| 87 | int hits = 0;
|
---|
[1528] | 88 |
|
---|
[1520] | 89 | #if DEBUG_RAYCAST
|
---|
[1757] | 90 | Debug<<"PRA"<<flush;
|
---|
[1520] | 91 | #endif
|
---|
[1528] | 92 |
|
---|
| 93 | if (pruneInvalidRays)
|
---|
| 94 | {
|
---|
[1584] | 95 | if (!hitA.mObject && !hitB.mObject) {
|
---|
| 96 | return 0;
|
---|
| 97 | }
|
---|
[1520] | 98 | }
|
---|
[1584] | 99 |
|
---|
[1528] | 100 | const bool validA =
|
---|
[1584] | 101 | ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
|
---|
[1757] | 102 |
|
---|
| 103 | #if DEBUG_RAYCAST
|
---|
| 104 | Debug<<"PR1"<<flush;
|
---|
| 105 | #endif
|
---|
[1584] | 106 |
|
---|
[1528] | 107 | if (!validA && pruneInvalidRays)
|
---|
[1584] | 108 | {
|
---|
[1528] | 109 | return 0;
|
---|
[1584] | 110 | }
|
---|
| 111 |
|
---|
[1528] | 112 | const bool validB = castDoubleRay &&
|
---|
[1584] | 113 | ValidateRay(simpleRay.mOrigin, -simpleRay.mDirection, box, hitB);
|
---|
| 114 |
|
---|
[1528] | 115 | if (!validB && pruneInvalidRays)
|
---|
[1584] | 116 | {
|
---|
[1528] | 117 | return 0;
|
---|
[1520] | 118 | }
|
---|
| 119 |
|
---|
[1528] | 120 | const bool validSample = !pruneInvalidRays || (hitA.mObject != hitB.mObject);
|
---|
[1757] | 121 |
|
---|
[1771] | 122 |
|
---|
[1757] | 123 | #if DEBUG_RAYCAST
|
---|
| 124 | Debug<<"PR2"<<flush;
|
---|
| 125 | #endif
|
---|
| 126 |
|
---|
[1771] | 127 | if (validSample) {
|
---|
[1583] | 128 | if (!pruneInvalidRays || hitA.mObject)
|
---|
[1528] | 129 | {
|
---|
[1583] | 130 | VssRay *vssRay = new VssRay(
|
---|
| 131 | hitB.mPoint,
|
---|
| 132 | hitA.mPoint,
|
---|
| 133 | hitB.mObject,
|
---|
| 134 | hitA.mObject,
|
---|
| 135 | mPreprocessor.mPass,
|
---|
| 136 | simpleRay.mPdf
|
---|
| 137 | );
|
---|
[1771] | 138 | if (validA)
|
---|
| 139 | vssRay->mFlags |= VssRay::Valid;
|
---|
[1583] | 140 | vssRays.push_back(vssRay);
|
---|
| 141 | ++ hits;
|
---|
| 142 | //cout << "vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
|
---|
[1520] | 143 | }
|
---|
[1757] | 144 |
|
---|
| 145 | #if DEBUG_RAYCAST
|
---|
| 146 | Debug<<"PR3"<<flush;
|
---|
| 147 | #endif
|
---|
| 148 |
|
---|
[1583] | 149 | if (castDoubleRay && (!pruneInvalidRays || hitB.mObject))
|
---|
[1528] | 150 | {
|
---|
[1583] | 151 | VssRay *vssRay = new VssRay(
|
---|
| 152 | hitA.mPoint,
|
---|
| 153 | hitB.mPoint,
|
---|
| 154 | hitA.mObject,
|
---|
| 155 | hitB.mObject,
|
---|
| 156 | mPreprocessor.mPass,
|
---|
| 157 | simpleRay.mPdf
|
---|
| 158 | );
|
---|
[1771] | 159 | if (validB)
|
---|
| 160 | vssRay->mFlags |= VssRay::Valid;
|
---|
[1583] | 161 |
|
---|
| 162 | vssRays.push_back(vssRay);
|
---|
| 163 | ++ hits;
|
---|
| 164 | //cout << "vssray 2: " << *vssRay << endl;
|
---|
[1520] | 165 | }
|
---|
| 166 | }
|
---|
[1757] | 167 |
|
---|
| 168 | #if DEBUG_RAYCAST
|
---|
| 169 | Debug<<"PR4"<<flush;
|
---|
| 170 | #endif
|
---|
| 171 |
|
---|
[1520] | 172 | return hits;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[1583] | 175 | }
|
---|