[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)
|
---|
| 31 | return CastGlobalRay(simpleRay, vssRays, box, pruneInvalidRays);
|
---|
| 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;
|
---|
| 51 |
|
---|
| 52 | if (mKdTree->CastRay(ray))
|
---|
[1520] | 53 | {
|
---|
[1583] | 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;
|
---|
[1520] | 58 | }
|
---|
[1583] | 59 |
|
---|
[1829] | 60 | mPreprocessor.SetupRay(ray, simpleRay.mOrigin, -simpleRay.mDirection);
|
---|
| 61 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
[1583] | 62 |
|
---|
| 63 | if (castDoubleRay && mKdTree->CastRay(ray))
|
---|
[1824] | 64 | {
|
---|
| 65 | hitB.mObject = ray.intersections[0].mObject;
|
---|
| 66 | hitB.mPoint = ray.Extrap(ray.intersections[0].mT);
|
---|
| 67 | hitB.mNormal = ray.intersections[0].mNormal;
|
---|
| 68 | }
|
---|
[1583] | 69 |
|
---|
| 70 | return ProcessRay(
|
---|
[1824] | 71 | simpleRay,
|
---|
| 72 | hitA,
|
---|
| 73 | hitB,
|
---|
| 74 | vssRays,
|
---|
| 75 | box,
|
---|
| 76 | castDoubleRay,
|
---|
| 77 | pruneInvalidRays
|
---|
| 78 | );
|
---|
[1520] | 79 | }
|
---|
| 80 |
|
---|
| 81 |
|
---|
[1824] | 82 | int
|
---|
| 83 | InternalRayCaster::CastGlobalRay(const SimpleRay &simpleRay,
|
---|
| 84 | VssRayContainer &vssRays,
|
---|
| 85 | const AxisAlignedBox3 &box,
|
---|
| 86 | const bool pruneInvalidRays
|
---|
| 87 | )
|
---|
| 88 | {
|
---|
| 89 | static Ray ray;
|
---|
| 90 | int hits = 0;
|
---|
[1867] | 91 | mPreprocessor.SetupRay(ray, simpleRay.mOrigin, simpleRay.mDirection);
|
---|
| 92 |
|
---|
| 93 | float tmin, tmax;
|
---|
| 94 | if (!(box.ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax)))
|
---|
| 95 | return 0;
|
---|
| 96 |
|
---|
| 97 | // shift the ray origin to tmin
|
---|
| 98 | // Vector3 origin = ray.Extrap(tmin);
|
---|
| 99 | // mPreprocessor.SetupRay(ray, origin, simpleRay.mDirection);
|
---|
| 100 | ray.SetType(Ray::GLOBAL_RAY);
|
---|
| 101 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
| 102 |
|
---|
[1824] | 103 |
|
---|
[1867] | 104 | VssRay *vssRay;
|
---|
| 105 |
|
---|
| 106 | if (mKdTree->CastRay(ray)) {
|
---|
| 107 | // sort intersections
|
---|
| 108 | ray.SortIntersections();
|
---|
| 109 | // cout<<"I="<<ray.intersections.size()<<endl;
|
---|
[1824] | 110 |
|
---|
[1867] | 111 |
|
---|
| 112 | Ray::Intersection &hit = ray.intersections[0];
|
---|
| 113 |
|
---|
| 114 | if (DotProd(hit.mNormal, ray.GetDir()) < 0) {
|
---|
| 115 | // cout<<"F:"<<tmin<<" "<<hit.mT<<endl;
|
---|
| 116 | // insert intial segment
|
---|
| 117 | vssRay = new VssRay(
|
---|
| 118 | ray.Extrap(tmin),
|
---|
| 119 | ray.Extrap(hit.mT),
|
---|
| 120 | NULL,
|
---|
| 121 | hit.mObject,
|
---|
| 122 | mPreprocessor.mPass,
|
---|
| 123 | simpleRay.mPdf
|
---|
| 124 | );
|
---|
[1824] | 125 |
|
---|
[1867] | 126 | vssRay->mFlags |= VssRay::Valid;
|
---|
[1883] | 127 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
[1867] | 128 | vssRays.push_back(vssRay);
|
---|
| 129 | ++hits;
|
---|
| 130 | }
|
---|
[1824] | 131 |
|
---|
[1867] | 132 | hit = ray.intersections[ray.intersections.size()-1];
|
---|
| 133 | if (DotProd(hit.mNormal, ray.GetDir()) > 0) {
|
---|
| 134 | // cout<<"L:"<<tmax<<" "<<hit.mT<<endl;
|
---|
| 135 |
|
---|
| 136 | // insert termination segment
|
---|
| 137 | vssRay = new VssRay(
|
---|
| 138 | ray.Extrap(tmax),
|
---|
| 139 | ray.Extrap(hit.mT),
|
---|
| 140 | NULL,
|
---|
| 141 | hit.mObject,
|
---|
| 142 | mPreprocessor.mPass,
|
---|
| 143 | simpleRay.mPdf
|
---|
| 144 | );
|
---|
| 145 | vssRay->mFlags |= VssRay::Valid;
|
---|
[1883] | 146 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
[1867] | 147 | vssRays.push_back(vssRay);
|
---|
| 148 | ++hits;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | // insert the rest of segments
|
---|
| 152 | for (int i=0; i < ray.intersections.size() - 1; i++) {
|
---|
| 153 | Ray::Intersection &hitA = ray.intersections[i];
|
---|
| 154 | Ray::Intersection &hitB = ray.intersections[i + 1];
|
---|
| 155 | if (hitB.mT - hitA.mT > Limits::Small) {
|
---|
| 156 | if (DotProd(hitA.mNormal, ray.GetDir()) > 0 &&
|
---|
| 157 | DotProd(hitB.mNormal, ray.GetDir()) < 0
|
---|
| 158 | ) {
|
---|
| 159 |
|
---|
| 160 | vssRay = new VssRay(
|
---|
| 161 | ray.Extrap(hitA.mT),
|
---|
| 162 | ray.Extrap(hitB.mT),
|
---|
| 163 | hitA.mObject,
|
---|
| 164 | hitB.mObject,
|
---|
| 165 | mPreprocessor.mPass,
|
---|
| 166 | simpleRay.mPdf
|
---|
| 167 | );
|
---|
| 168 | vssRay->mFlags |= VssRay::Valid;
|
---|
[1883] | 169 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
[1867] | 170 | vssRays.push_back(vssRay);
|
---|
| 171 | ++hits;
|
---|
| 172 |
|
---|
| 173 | vssRay = new VssRay(
|
---|
| 174 | ray.Extrap(hitB.mT),
|
---|
| 175 | ray.Extrap(hitA.mT),
|
---|
| 176 | hitB.mObject,
|
---|
| 177 | hitA.mObject,
|
---|
| 178 | mPreprocessor.mPass,
|
---|
| 179 | simpleRay.mPdf
|
---|
| 180 | );
|
---|
| 181 |
|
---|
| 182 | vssRay->mFlags |= VssRay::Valid;
|
---|
[1883] | 183 | vssRay->mDistribution = simpleRay.mDistribution;
|
---|
[1867] | 184 | vssRays.push_back(vssRay);
|
---|
| 185 | ++hits;
|
---|
[1824] | 186 | }
|
---|
[1867] | 187 | }
|
---|
[1824] | 188 | }
|
---|
[1867] | 189 | }
|
---|
| 190 |
|
---|
| 191 | return hits;
|
---|
[1824] | 192 | }
|
---|
| 193 |
|
---|
[1521] | 194 | void InternalRayCaster::CastRays16(const int index,
|
---|
[1828] | 195 | SimpleRayContainer &rays,
|
---|
| 196 | VssRayContainer &vssRays,
|
---|
| 197 | const AxisAlignedBox3 &sbox,
|
---|
| 198 | const bool castDoubleRays,
|
---|
| 199 | const bool pruneInvalidRays)
|
---|
[1520] | 200 | {
|
---|
| 201 | const int num = 16;
|
---|
| 202 |
|
---|
| 203 | #if DEBUG_RAYCAST
|
---|
[1828] | 204 | Debug << "C16 " << flush;
|
---|
[1520] | 205 | #endif
|
---|
| 206 |
|
---|
| 207 | // no acceleration for ray bundles implemented right now
|
---|
[1828] | 208 | for (int i = index; i < index + num; i++)
|
---|
[1520] | 209 | {
|
---|
[1528] | 210 | CastRay(rays[i], vssRays, sbox, castDoubleRays, pruneInvalidRays);
|
---|
[1520] | 211 | }
|
---|
| 212 |
|
---|
| 213 | #if DEBUG_RAYCAST
|
---|
| 214 | Debug<<"C16F\n"<<flush;
|
---|
| 215 | #endif
|
---|
| 216 | }
|
---|
| 217 |
|
---|
[1583] | 218 | }
|
---|