[1520] | 1 | #include "InternalRayCaster.h"
|
---|
| 2 | #include "VssRay.h"
|
---|
| 3 | #include "KdTree.h"
|
---|
| 4 | #include "Preprocessor.h"
|
---|
| 5 |
|
---|
| 6 | #define DEBUG_RAYCAST 0
|
---|
| 7 |
|
---|
| 8 |
|
---|
| 9 | namespace GtpVisibilityPreprocessor {
|
---|
| 10 |
|
---|
| 11 |
|
---|
| 12 | InternalRayCaster::InternalRayCaster(const Preprocessor &preprocessor, KdTree *kdTree):
|
---|
| 13 | RayCaster(preprocessor), mKdTree(kdTree)
|
---|
| 14 | {
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 | InternalRayCaster::~InternalRayCaster()
|
---|
| 19 | {
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 |
|
---|
[1528] | 23 | int InternalRayCaster::CastRay(const SimpleRay &simpleRay,
|
---|
[1876] | 24 | VssRayContainer &vssRays,
|
---|
| 25 | const AxisAlignedBox3 &box,
|
---|
| 26 | const bool castDoubleRay,
|
---|
| 27 | const bool pruneInvalidRays
|
---|
| 28 | )
|
---|
[1520] | 29 | {
|
---|
[1876] | 30 | if (simpleRay.mType == Ray::GLOBAL_RAY)
|
---|
[1964] | 31 | return CastGlobalRay(simpleRay, vssRays, box, pruneInvalidRays);
|
---|
[1876] | 32 |
|
---|
| 33 | //cout << "internal ray" << endl;
|
---|
[1584] | 34 | static Ray ray;
|
---|
[1583] | 35 | int hits = 0;
|
---|
| 36 | Intersection hitA(simpleRay.mOrigin), hitB(simpleRay.mOrigin);
|
---|
| 37 |
|
---|
[1584] | 38 | // inside test for bounding box
|
---|
| 39 | // enlarge box slightly so the view point fits for sure
|
---|
| 40 | // AxisAlignedBox3 sbox = box;
|
---|
| 41 | // sbox.Enlarge(Vector3(-Limits::Small));
|
---|
| 42 | // $$ JB moved here from Validate routine
|
---|
[1876] | 43 |
|
---|
| 44 | // if (!box.IsInside(simpleRay.mOrigin)) {
|
---|
| 45 | // cout<<"out of box "<<simpleRay.mOrigin<<" "<<box<<endl;
|
---|
| 46 | // return 0;
|
---|
| 47 | // }
|
---|
[1584] | 48 |
|
---|
[1583] | 49 | mPreprocessor.SetupRay(ray, simpleRay.mOrigin, simpleRay.mDirection);
|
---|
| 50 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
[1984] | 51 |
|
---|
[1583] | 52 |
|
---|
[1984] | 53 | if (mKdTree->CastRay(ray)) {
|
---|
| 54 | hitA.mObject = ray.intersections[0].mObject;
|
---|
| 55 | hitA.mPoint = ray.Extrap(ray.intersections[0].mT);
|
---|
| 56 | hitA.mNormal = ray.intersections[0].mNormal;
|
---|
| 57 | // cout << "hita: " << hitA.mPoint << " !obj: " << hitA.mObject << endl;
|
---|
| 58 | }
|
---|
[1583] | 59 |
|
---|
[1984] | 60 | mPreprocessor.SetupRay(ray, simpleRay.mOrigin, -simpleRay.mDirection);
|
---|
| 61 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
[1583] | 62 |
|
---|
[1984] | 63 | if (castDoubleRay && mKdTree->CastRay(ray)) {
|
---|
| 64 | hitB.mObject = ray.intersections[0].mObject;
|
---|
| 65 | hitB.mPoint = ray.Extrap(ray.intersections[0].mT);
|
---|
| 66 | hitB.mNormal = ray.intersections[0].mNormal;
|
---|
| 67 | }
|
---|
[1583] | 68 |
|
---|
| 69 | return ProcessRay(
|
---|
[1984] | 70 | simpleRay,
|
---|
| 71 | hitA,
|
---|
| 72 | hitB,
|
---|
| 73 | vssRays,
|
---|
| 74 | box,
|
---|
| 75 | castDoubleRay,
|
---|
| 76 | pruneInvalidRays
|
---|
| 77 | );
|
---|
[1520] | 78 | }
|
---|
| 79 |
|
---|
| 80 |
|
---|
[1824] | 81 | int
|
---|
| 82 | InternalRayCaster::CastGlobalRay(const SimpleRay &simpleRay,
|
---|
[1964] | 83 | VssRayContainer &vssRays,
|
---|
| 84 | const AxisAlignedBox3 &box,
|
---|
| 85 | const bool pruneInvalidRays
|
---|
| 86 | )
|
---|
[1824] | 87 | {
|
---|
| 88 | static Ray ray;
|
---|
| 89 | int hits = 0;
|
---|
[1867] | 90 | mPreprocessor.SetupRay(ray, simpleRay.mOrigin, simpleRay.mDirection);
|
---|
| 91 |
|
---|
| 92 | float tmin, tmax;
|
---|
| 93 | if (!(box.ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax)))
|
---|
| 94 | return 0;
|
---|
| 95 |
|
---|
| 96 | // shift the ray origin to tmin
|
---|
| 97 | // Vector3 origin = ray.Extrap(tmin);
|
---|
| 98 | // mPreprocessor.SetupRay(ray, origin, simpleRay.mDirection);
|
---|
| 99 | ray.SetType(Ray::GLOBAL_RAY);
|
---|
| 100 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
| 101 |
|
---|
[1824] | 102 |
|
---|
[1867] | 103 | VssRay *vssRay;
|
---|
| 104 |
|
---|
| 105 | if (mKdTree->CastRay(ray)) {
|
---|
| 106 | // sort intersections
|
---|
| 107 | ray.SortIntersections();
|
---|
| 108 | // cout<<"I="<<ray.intersections.size()<<endl;
|
---|
[1824] | 109 |
|
---|
[1867] | 110 |
|
---|
| 111 | Ray::Intersection &hit = ray.intersections[0];
|
---|
| 112 |
|
---|
| 113 | if (DotProd(hit.mNormal, ray.GetDir()) < 0) {
|
---|
| 114 | // cout<<"F:"<<tmin<<" "<<hit.mT<<endl;
|
---|
| 115 | // insert intial segment
|
---|
| 116 | vssRay = new VssRay(
|
---|
| 117 | ray.Extrap(tmin),
|
---|
| 118 | ray.Extrap(hit.mT),
|
---|
| 119 | NULL,
|
---|
| 120 | hit.mObject,
|
---|
| 121 | mPreprocessor.mPass,
|
---|
| 122 | simpleRay.mPdf
|
---|
| 123 | );
|
---|
[1824] | 124 |
|
---|
[1867] | 125 | vssRay->mFlags |= VssRay::Valid;
|
---|
[1883] | 126 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
[1867] | 127 | vssRays.push_back(vssRay);
|
---|
| 128 | ++hits;
|
---|
| 129 | }
|
---|
[1824] | 130 |
|
---|
[1867] | 131 | hit = ray.intersections[ray.intersections.size()-1];
|
---|
| 132 | if (DotProd(hit.mNormal, ray.GetDir()) > 0) {
|
---|
| 133 | // cout<<"L:"<<tmax<<" "<<hit.mT<<endl;
|
---|
| 134 |
|
---|
| 135 | // insert termination segment
|
---|
| 136 | vssRay = new VssRay(
|
---|
| 137 | ray.Extrap(tmax),
|
---|
| 138 | ray.Extrap(hit.mT),
|
---|
| 139 | NULL,
|
---|
| 140 | hit.mObject,
|
---|
| 141 | mPreprocessor.mPass,
|
---|
| 142 | simpleRay.mPdf
|
---|
| 143 | );
|
---|
| 144 | vssRay->mFlags |= VssRay::Valid;
|
---|
[1883] | 145 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
[1867] | 146 | vssRays.push_back(vssRay);
|
---|
| 147 | ++hits;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | // insert the rest of segments
|
---|
| 151 | for (int i=0; i < ray.intersections.size() - 1; i++) {
|
---|
| 152 | Ray::Intersection &hitA = ray.intersections[i];
|
---|
| 153 | Ray::Intersection &hitB = ray.intersections[i + 1];
|
---|
| 154 | if (hitB.mT - hitA.mT > Limits::Small) {
|
---|
| 155 | if (DotProd(hitA.mNormal, ray.GetDir()) > 0 &&
|
---|
| 156 | DotProd(hitB.mNormal, ray.GetDir()) < 0
|
---|
| 157 | ) {
|
---|
| 158 |
|
---|
| 159 | vssRay = new VssRay(
|
---|
| 160 | ray.Extrap(hitA.mT),
|
---|
| 161 | ray.Extrap(hitB.mT),
|
---|
| 162 | hitA.mObject,
|
---|
| 163 | hitB.mObject,
|
---|
| 164 | mPreprocessor.mPass,
|
---|
| 165 | simpleRay.mPdf
|
---|
| 166 | );
|
---|
| 167 | vssRay->mFlags |= VssRay::Valid;
|
---|
[1883] | 168 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
[1867] | 169 | vssRays.push_back(vssRay);
|
---|
| 170 | ++hits;
|
---|
| 171 |
|
---|
| 172 | vssRay = new VssRay(
|
---|
| 173 | ray.Extrap(hitB.mT),
|
---|
| 174 | ray.Extrap(hitA.mT),
|
---|
| 175 | hitB.mObject,
|
---|
| 176 | hitA.mObject,
|
---|
| 177 | mPreprocessor.mPass,
|
---|
| 178 | simpleRay.mPdf
|
---|
| 179 | );
|
---|
| 180 |
|
---|
| 181 | vssRay->mFlags |= VssRay::Valid;
|
---|
[1883] | 182 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
[1867] | 183 | vssRays.push_back(vssRay);
|
---|
| 184 | ++hits;
|
---|
[1824] | 185 | }
|
---|
[1867] | 186 | }
|
---|
[1824] | 187 | }
|
---|
[1867] | 188 | }
|
---|
| 189 |
|
---|
| 190 | return hits;
|
---|
[1824] | 191 | }
|
---|
| 192 |
|
---|
[1972] | 193 |
|
---|
| 194 | void InternalRayCaster::CastRays16(SimpleRayContainer &rays,
|
---|
[1828] | 195 | VssRayContainer &vssRays,
|
---|
| 196 | const AxisAlignedBox3 &sbox,
|
---|
| 197 | const bool castDoubleRays,
|
---|
| 198 | const bool pruneInvalidRays)
|
---|
[1520] | 199 | {
|
---|
| 200 | #if DEBUG_RAYCAST
|
---|
[1828] | 201 | Debug << "C16 " << flush;
|
---|
[1520] | 202 | #endif
|
---|
| 203 |
|
---|
[1972] | 204 | SimpleRayContainer::const_iterator sit, sit_end = rays.end();
|
---|
| 205 |
|
---|
[1520] | 206 | // no acceleration for ray bundles implemented right now
|
---|
[1972] | 207 | for (sit = rays.begin(); sit != sit_end; ++ sit)
|
---|
[1520] | 208 | {
|
---|
[1972] | 209 | CastRay(*sit, vssRays, sbox, castDoubleRays, pruneInvalidRays);
|
---|
[1520] | 210 | }
|
---|
| 211 |
|
---|
| 212 | #if DEBUG_RAYCAST
|
---|
| 213 | Debug<<"C16F\n"<<flush;
|
---|
| 214 | #endif
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[1583] | 217 | }
|
---|