[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 | */
|
---|
[1867] | 41 | bool
|
---|
| 42 | RayCaster::ValidateRay(const Vector3 &origin,
|
---|
| 43 | const Vector3 &direction,
|
---|
| 44 | const AxisAlignedBox3 &box,
|
---|
| 45 | Intersection &hit)
|
---|
[1528] | 46 | {
|
---|
[1867] | 47 | if (!hit.mObject) {
|
---|
| 48 | // compute intersection with the scene bounding box
|
---|
[1584] | 49 | #if EXACT_BOX_CLIPPING
|
---|
[1867] | 50 | static Ray ray;
|
---|
| 51 | mPreprocessor.SetupRay(ray, origin, direction);
|
---|
| 52 |
|
---|
| 53 | float tmin, tmax;
|
---|
| 54 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax)) {
|
---|
| 55 | hit.mPoint = ray.Extrap(tmax);
|
---|
| 56 | }
|
---|
| 57 | else {
|
---|
| 58 | // cout<<" invalid hp "<<tmin<<" "<<tmax<<endl;
|
---|
| 59 | // cout<<" box "<<box<<endl;
|
---|
| 60 | // cout<<" origin "<<origin<<endl;
|
---|
| 61 | // cout<<" direction "<<direction<<endl;
|
---|
| 62 | // cout<< "inv dir"<<ray.GetInvDir()<<endl;
|
---|
| 63 | return false;
|
---|
| 64 | }
|
---|
[1584] | 65 | #else
|
---|
[1867] | 66 | hit.mPoint = origin + direction*Magnitude(box.Diagonal());
|
---|
[1584] | 67 | #endif
|
---|
[1867] | 68 | }
|
---|
| 69 | else
|
---|
| 70 | if (mPreprocessor.mDetectEmptyViewSpace) {
|
---|
| 71 | if (DotProd(hit.mNormal, direction) >= -Limits::Small) {
|
---|
[1584] | 72 | hit.mObject = NULL;
|
---|
| 73 | return false;
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
[1867] | 76 |
|
---|
| 77 | return true;
|
---|
[1528] | 78 | }
|
---|
| 79 |
|
---|
| 80 |
|
---|
[1867] | 81 | int
|
---|
| 82 | RayCaster::ProcessRay(const SimpleRay &simpleRay,
|
---|
| 83 | Intersection &hitA,
|
---|
| 84 | Intersection &hitB,
|
---|
| 85 | VssRayContainer &vssRays,
|
---|
| 86 | const AxisAlignedBox3 &box,
|
---|
| 87 | const bool castDoubleRay,
|
---|
| 88 | const bool pruneInvalidRays)
|
---|
[1520] | 89 | {
|
---|
| 90 | int hits = 0;
|
---|
[1528] | 91 |
|
---|
[1520] | 92 | #if DEBUG_RAYCAST
|
---|
[1757] | 93 | Debug<<"PRA"<<flush;
|
---|
[1520] | 94 | #endif
|
---|
[1867] | 95 |
|
---|
| 96 | // regardless of the pruneInvalidRays setting reject rays whic degenerate to a point
|
---|
[1900] | 97 | // if (EpsilonEqualV3(hitA.mPoint, hitB.mPoint, Limits::Small))
|
---|
| 98 | // return 0;
|
---|
[1528] | 99 |
|
---|
[1867] | 100 | if (pruneInvalidRays) {
|
---|
[1584] | 101 | if (!hitA.mObject && !hitB.mObject) {
|
---|
| 102 | return 0;
|
---|
| 103 | }
|
---|
[1520] | 104 | }
|
---|
[1584] | 105 |
|
---|
[1528] | 106 | const bool validA =
|
---|
[1584] | 107 | ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
|
---|
[1867] | 108 |
|
---|
| 109 | const bool validB = castDoubleRay &&
|
---|
| 110 | ValidateRay(simpleRay.mOrigin, -simpleRay.mDirection, box, hitB);
|
---|
| 111 |
|
---|
[1757] | 112 | #if DEBUG_RAYCAST
|
---|
| 113 | Debug<<"PR1"<<flush;
|
---|
| 114 | #endif
|
---|
[1584] | 115 |
|
---|
[1867] | 116 | // reset both contributions
|
---|
| 117 | if (!validA || !validB) {
|
---|
| 118 | if (pruneInvalidRays)
|
---|
[1528] | 119 | return 0;
|
---|
[1867] | 120 | // reset both contributions of this ray
|
---|
| 121 | hitA.mObject = NULL;
|
---|
| 122 | hitB.mObject = NULL;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[1584] | 125 |
|
---|
[1867] | 126 | const bool validSample = true;
|
---|
| 127 | // 8.11. 2007 JB
|
---|
| 128 | // degenerate rays checked by geometrical constraint...
|
---|
| 129 | // !pruneInvalidRays || (hitA.mObject != hitB.mObject);
|
---|
[1584] | 130 |
|
---|
[1757] | 131 | #if DEBUG_RAYCAST
|
---|
| 132 | Debug<<"PR2"<<flush;
|
---|
| 133 | #endif
|
---|
| 134 |
|
---|
[1771] | 135 | if (validSample) {
|
---|
[1867] | 136 | if (!pruneInvalidRays || hitA.mObject) {
|
---|
| 137 | VssRay *vssRay = new VssRay(
|
---|
| 138 | hitB.mPoint,
|
---|
| 139 | hitA.mPoint,
|
---|
| 140 | hitB.mObject,
|
---|
| 141 | hitA.mObject,
|
---|
| 142 | mPreprocessor.mPass,
|
---|
| 143 | simpleRay.mPdf
|
---|
| 144 | );
|
---|
| 145 |
|
---|
| 146 | if (validA)
|
---|
| 147 | vssRay->mFlags |= VssRay::Valid;
|
---|
[1883] | 148 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
[1867] | 149 | vssRays.push_back(vssRay);
|
---|
| 150 | ++ hits;
|
---|
| 151 | //cout << "vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | #if DEBUG_RAYCAST
|
---|
| 155 | Debug<<"PR3"<<flush;
|
---|
| 156 | #endif
|
---|
| 157 |
|
---|
| 158 | if (castDoubleRay && (!pruneInvalidRays || hitB.mObject))
|
---|
[1528] | 159 | {
|
---|
[1583] | 160 | VssRay *vssRay = new VssRay(
|
---|
[1867] | 161 | hitA.mPoint,
|
---|
[1583] | 162 | hitB.mPoint,
|
---|
[1867] | 163 | hitA.mObject,
|
---|
[1583] | 164 | hitB.mObject,
|
---|
| 165 | mPreprocessor.mPass,
|
---|
| 166 | simpleRay.mPdf
|
---|
| 167 | );
|
---|
[1867] | 168 | if (validB)
|
---|
[1771] | 169 | vssRay->mFlags |= VssRay::Valid;
|
---|
[1583] | 170 |
|
---|
[1883] | 171 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
[1583] | 172 | vssRays.push_back(vssRay);
|
---|
| 173 | ++ hits;
|
---|
| 174 | //cout << "vssray 2: " << *vssRay << endl;
|
---|
[1520] | 175 | }
|
---|
| 176 | }
|
---|
[1867] | 177 |
|
---|
[1757] | 178 | #if DEBUG_RAYCAST
|
---|
| 179 | Debug<<"PR4"<<flush;
|
---|
| 180 | #endif
|
---|
| 181 |
|
---|
[1520] | 182 | return hits;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[1583] | 185 | }
|
---|