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