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