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