[1315] | 1 | #include "IntersectableWrapper.h"
|
---|
[1328] | 2 | #include "Mesh.h"
|
---|
| 3 | #include "Triangle3.h"
|
---|
[1694] | 4 | #include "KdTree.h"
|
---|
[1737] | 5 | #include "BvHierarchy.h"
|
---|
[2575] | 6 | #include "SimpleRay.h"
|
---|
[1199] | 7 |
|
---|
| 8 |
|
---|
| 9 | namespace GtpVisibilityPreprocessor {
|
---|
| 10 |
|
---|
| 11 |
|
---|
[1328] | 12 | AxisAlignedBox3 TriangleIntersectable::GetBox() const
|
---|
[1327] | 13 | {
|
---|
[2575] | 14 | return mItem.GetBoundingBox();
|
---|
[1327] | 15 | }
|
---|
[1199] | 16 |
|
---|
[1327] | 17 |
|
---|
[2575] | 18 | int
|
---|
| 19 | TriangleIntersectable::CastSimpleRay(const SimpleRay &ray)
|
---|
| 20 | {
|
---|
| 21 | static Vector3 nearestNormal;
|
---|
| 22 | // std::cout << "To be done - ray triangle intersection\n" << std::endl;
|
---|
| 23 | const int hitCode = mItem.CastSimpleRay(ray,
|
---|
| 24 | SimpleRay::IntersectionRes[0].tdist,
|
---|
| 25 | SimpleRay::IntersectionRes[0].maxt);
|
---|
| 26 | if (hitCode == Ray::INTERSECTION) {
|
---|
| 27 | SimpleRay::IntersectionRes[0].intersectable = this;
|
---|
| 28 | SimpleRay::IntersectionRes[0].maxt = SimpleRay::IntersectionRes[0].tdist;
|
---|
| 29 | return 1; // intersection occured
|
---|
| 30 | }
|
---|
| 31 | return 0; // no intersection
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | int
|
---|
| 35 | TriangleIntersectable::CastSimpleRay(const SimpleRay &ray, int rayIndex)
|
---|
| 36 | {
|
---|
| 37 | const int hitCode =
|
---|
| 38 | mItem.CastSimpleRay(ray,
|
---|
| 39 | SimpleRay::IntersectionRes[rayIndex].tdist,
|
---|
| 40 | SimpleRay::IntersectionRes[rayIndex].maxt);
|
---|
| 41 | if (hitCode == Ray::INTERSECTION) {
|
---|
| 42 | SimpleRay::IntersectionRes[rayIndex].intersectable = this;
|
---|
| 43 | // we copy the closest distance so far - it prunes the search
|
---|
| 44 | // when more triangles in the same leaf and assures the
|
---|
| 45 | // correctness.
|
---|
| 46 | SimpleRay::IntersectionRes[rayIndex].maxt =
|
---|
| 47 | SimpleRay::IntersectionRes[rayIndex].tdist;
|
---|
| 48 | return 1; // intersection occured
|
---|
| 49 | }
|
---|
| 50 | return 0; // no intersection
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 |
|
---|
[1328] | 54 | int TriangleIntersectable::CastRay(Ray &ray)
|
---|
[1344] | 55 | {
|
---|
[2575] | 56 | float nearestT = MAX_FLOAT;
|
---|
| 57 | float t;
|
---|
| 58 | Vector3 nearestNormal;
|
---|
| 59 |
|
---|
| 60 | if (ray.GetType() == Ray::LOCAL_RAY && !ray.intersections.empty())
|
---|
| 61 | nearestT = ray.intersections[0].mT;
|
---|
| 62 |
|
---|
| 63 | const int hitCode = mItem.CastRay(ray, t, nearestT, nearestNormal);
|
---|
| 64 |
|
---|
| 65 | nearestT = t;
|
---|
| 66 |
|
---|
| 67 | if (hitCode == Ray::INTERSECTION) {
|
---|
| 68 | if (ray.GetType() == Ray::LOCAL_RAY && !ray.intersections.empty()) {
|
---|
| 69 | ray.intersections[0] = Ray::Intersection(nearestT,
|
---|
| 70 | nearestNormal,
|
---|
| 71 | this,
|
---|
| 72 | 0);
|
---|
| 73 | }
|
---|
| 74 | else {
|
---|
| 75 | ray.intersections.push_back(Ray::Intersection(nearestT,
|
---|
| 76 | nearestNormal,
|
---|
| 77 | this,
|
---|
| 78 | 0));
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | return 1;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | return 0;
|
---|
[1327] | 85 | }
|
---|
[1199] | 86 |
|
---|
[1327] | 87 |
|
---|
[1328] | 88 | int TriangleIntersectable::NumberOfFaces() const
|
---|
[1327] | 89 | {
|
---|
[1328] | 90 | return 1;
|
---|
[1327] | 91 | }
|
---|
| 92 |
|
---|
[1344] | 93 |
|
---|
[2575] | 94 | Vector3
|
---|
| 95 | TriangleIntersectable::GetNormal(const int idx) const
|
---|
[1344] | 96 | {
|
---|
[2575] | 97 | return mItem.GetNormal();
|
---|
[1344] | 98 | }
|
---|
| 99 |
|
---|
[1786] | 100 |
|
---|
[1586] | 101 | int
|
---|
| 102 | TriangleIntersectable::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal)
|
---|
| 103 | {
|
---|
| 104 | // random barycentric coordinates
|
---|
[1765] | 105 | float a = RandomValue(0.0f, 1.0f);
|
---|
| 106 | float b = RandomValue(0.0f, 1.0f);
|
---|
| 107 | float c = RandomValue(0.0f, 1.0f);
|
---|
[1586] | 108 |
|
---|
[1587] | 109 | const float sum = a + b + c;
|
---|
| 110 |
|
---|
[1692] | 111 | // scale so we get vaccumated value of 1
|
---|
[1587] | 112 | if (sum)
|
---|
| 113 | {
|
---|
| 114 | a /= sum;
|
---|
| 115 | b /= sum;
|
---|
| 116 | c /= sum;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[1692] | 119 | //cout << "a: " << a << " b: " << b << " c: " << c << " sum: " << sum << endl;
|
---|
[1587] | 120 | point = mItem.mVertices[0] * a + mItem.mVertices[1] * b + mItem.mVertices[2] * c;
|
---|
[1586] | 121 | normal = mItem.GetNormal();
|
---|
| 122 |
|
---|
| 123 | return 0;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[1587] | 126 |
|
---|
[1877] | 127 | int
|
---|
| 128 | TriangleIntersectable::GetRandomSurfacePoint(const float u,
|
---|
| 129 | const float v,
|
---|
| 130 | Vector3 &point,
|
---|
| 131 | Vector3 &normal)
|
---|
| 132 | {
|
---|
| 133 | // random barycentric coordinates
|
---|
| 134 | float a = u;
|
---|
| 135 | float b = v;
|
---|
| 136 | float c = 1.0f - u - v;
|
---|
| 137 |
|
---|
| 138 | point = mItem.mVertices[0] * a + mItem.mVertices[1] * b + mItem.mVertices[2] * c;
|
---|
| 139 | normal = mItem.GetNormal();
|
---|
| 140 |
|
---|
| 141 | return 0;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 |
|
---|
| 145 |
|
---|
[1587] | 146 | int TriangleIntersectable::GetRandomVisibleSurfacePoint(Vector3 &point,
|
---|
| 147 | Vector3 &normal,
|
---|
| 148 | const Vector3 &viewpoint,
|
---|
| 149 | const int maxTries)
|
---|
| 150 | {
|
---|
| 151 | return GetRandomSurfacePoint(point, normal);
|
---|
| 152 | }
|
---|
[1694] | 153 |
|
---|
| 154 |
|
---|
[2570] | 155 | int TriangleIntersectable::GetRandomEdgePoint(Vector3 &point, Vector3 &normal)
|
---|
[1763] | 156 | {
|
---|
[1765] | 157 | const int edge = Random(3);
|
---|
[1763] | 158 |
|
---|
[1765] | 159 | const Vector3 a = mItem.mVertices[edge];
|
---|
| 160 | const Vector3 b = mItem.mVertices[(edge + 1) % 3];
|
---|
[1763] | 161 |
|
---|
| 162 | const float factor = RandomValue(0.0f, 1.0f);
|
---|
| 163 |
|
---|
| 164 | point = a * factor + b * factor;
|
---|
| 165 | normal = mItem.GetNormal();
|
---|
| 166 |
|
---|
| 167 | return edge;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 |
|
---|
[2569] | 171 | KdIntersectable::KdIntersectable(KdNode *item, const AxisAlignedBox3 &box) :
|
---|
[2604] | 172 | IntersectableWrapper<KdNode *>(item), mNumTriangles(-1), mBox(box), mGenericIdx(-1)
|
---|
[1694] | 173 | {
|
---|
| 174 | }
|
---|
[2569] | 175 |
|
---|
[2570] | 176 |
|
---|
[2569] | 177 | int KdIntersectable::ComputeNumTriangles()
|
---|
| 178 | {
|
---|
[2570] | 179 | Intersectable::NewMail();
|
---|
| 180 |
|
---|
[2569] | 181 | std::stack<KdNode *> tStack;
|
---|
| 182 | tStack.push(mItem);
|
---|
| 183 |
|
---|
| 184 | if (mNumTriangles != -1)
|
---|
| 185 | return mNumTriangles;
|
---|
| 186 |
|
---|
| 187 | mNumTriangles = 0;
|
---|
| 188 |
|
---|
| 189 | while (!tStack.empty())
|
---|
| 190 | {
|
---|
| 191 | KdNode *node = tStack.top();
|
---|
| 192 | tStack.pop();
|
---|
| 193 |
|
---|
| 194 | if (!node->IsLeaf())
|
---|
| 195 | {
|
---|
| 196 | KdInterior *kdInterior = static_cast<KdInterior *>(node);
|
---|
| 197 |
|
---|
| 198 | tStack.push(kdInterior->mBack);
|
---|
| 199 | tStack.push(kdInterior->mFront);
|
---|
| 200 | }
|
---|
| 201 | else
|
---|
| 202 | {
|
---|
| 203 | KdLeaf *leaf = static_cast<KdLeaf *>(node);
|
---|
| 204 |
|
---|
| 205 | int tri = 0;
|
---|
| 206 |
|
---|
| 207 | for (size_t i = 0; i < leaf->mObjects.size(); ++ i)
|
---|
| 208 | {
|
---|
| 209 | TriangleIntersectable *obj =
|
---|
| 210 | static_cast<TriangleIntersectable *>(leaf->mObjects[i]);
|
---|
| 211 |
|
---|
| 212 | // check if already rendered
|
---|
| 213 | if (!obj->Mailed())
|
---|
| 214 | {
|
---|
| 215 | obj->Mail();
|
---|
| 216 | ++ tri;
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | mNumTriangles += tri;
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | return mNumTriangles;
|
---|
[1694] | 225 | }
|
---|
[2569] | 226 |
|
---|
[2615] | 227 |
|
---|
| 228 | SceneGraphLeafIntersectable::SceneGraphLeafIntersectable(SceneGraphLeaf *item, const AxisAlignedBox3 &box):
|
---|
| 229 | IntersectableWrapper<SceneGraphLeaf *>(item), mBox(box)
|
---|
| 230 | {
|
---|
[2569] | 231 | }
|
---|
[2615] | 232 |
|
---|
| 233 | }
|
---|