[1315] | 1 | #include "IntersectableWrapper.h"
|
---|
[1328] | 2 | #include "Mesh.h"
|
---|
| 3 | #include "Triangle3.h"
|
---|
[1694] | 4 | #include "KdTree.h"
|
---|
[1737] | 5 | #include "BvHierarchy.h"
|
---|
[1199] | 6 |
|
---|
| 7 |
|
---|
| 8 | namespace GtpVisibilityPreprocessor {
|
---|
| 9 |
|
---|
| 10 |
|
---|
[1328] | 11 | AxisAlignedBox3 TriangleIntersectable::GetBox() const
|
---|
[1327] | 12 | {
|
---|
[1344] | 13 | return mItem.GetBoundingBox();
|
---|
[1327] | 14 | }
|
---|
[1199] | 15 |
|
---|
[1327] | 16 |
|
---|
[1328] | 17 | int TriangleIntersectable::CastRay(Ray &ray)
|
---|
[1344] | 18 | {
|
---|
| 19 | float nearestT = MAX_FLOAT;
|
---|
| 20 | float t;
|
---|
| 21 | Vector3 nearestNormal;
|
---|
| 22 |
|
---|
| 23 | if (ray.GetType() == Ray::LOCAL_RAY && !ray.intersections.empty())
|
---|
| 24 | nearestT = ray.intersections[0].mT;
|
---|
[1824] | 25 |
|
---|
[1344] | 26 | const int hitCode = mItem.CastRay(ray, t, nearestT, nearestNormal);
|
---|
| 27 |
|
---|
| 28 | nearestT = t;
|
---|
| 29 |
|
---|
[1824] | 30 | if (hitCode == Ray::INTERSECTION) {
|
---|
| 31 | if (ray.GetType() == Ray::LOCAL_RAY && !ray.intersections.empty()) {
|
---|
| 32 | ray.intersections[0] = Ray::Intersection(nearestT,
|
---|
[1867] | 33 | nearestNormal,
|
---|
| 34 | this,
|
---|
| 35 | 0);
|
---|
[1344] | 36 | }
|
---|
[1824] | 37 | else {
|
---|
| 38 | ray.intersections.push_back(Ray::Intersection(nearestT,
|
---|
[1867] | 39 | nearestNormal,
|
---|
| 40 | this,
|
---|
| 41 | 0));
|
---|
[1344] | 42 | }
|
---|
| 43 |
|
---|
| 44 | return 1;
|
---|
| 45 | }
|
---|
[1824] | 46 |
|
---|
[1327] | 47 | return 0;
|
---|
| 48 | }
|
---|
[1199] | 49 |
|
---|
[1327] | 50 |
|
---|
[1328] | 51 | int TriangleIntersectable::NumberOfFaces() const
|
---|
[1327] | 52 | {
|
---|
[1328] | 53 | return 1;
|
---|
[1327] | 54 | }
|
---|
| 55 |
|
---|
[1344] | 56 |
|
---|
| 57 | Vector3 TriangleIntersectable::GetNormal(const int idx) const
|
---|
| 58 | {
|
---|
| 59 | return mItem.GetNormal();
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[1786] | 62 |
|
---|
[1586] | 63 | int
|
---|
| 64 | TriangleIntersectable::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal)
|
---|
| 65 | {
|
---|
| 66 | // random barycentric coordinates
|
---|
[1765] | 67 | float a = RandomValue(0.0f, 1.0f);
|
---|
| 68 | float b = RandomValue(0.0f, 1.0f);
|
---|
| 69 | float c = RandomValue(0.0f, 1.0f);
|
---|
[1586] | 70 |
|
---|
[1587] | 71 | const float sum = a + b + c;
|
---|
| 72 |
|
---|
[1692] | 73 | // scale so we get vaccumated value of 1
|
---|
[1587] | 74 | if (sum)
|
---|
| 75 | {
|
---|
| 76 | a /= sum;
|
---|
| 77 | b /= sum;
|
---|
| 78 | c /= sum;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[1692] | 81 | //cout << "a: " << a << " b: " << b << " c: " << c << " sum: " << sum << endl;
|
---|
[1587] | 82 | point = mItem.mVertices[0] * a + mItem.mVertices[1] * b + mItem.mVertices[2] * c;
|
---|
[1586] | 83 | normal = mItem.GetNormal();
|
---|
| 84 |
|
---|
| 85 | return 0;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[1587] | 88 |
|
---|
[1877] | 89 | int
|
---|
| 90 | TriangleIntersectable::GetRandomSurfacePoint(const float u,
|
---|
| 91 | const float v,
|
---|
| 92 | Vector3 &point,
|
---|
| 93 | Vector3 &normal)
|
---|
| 94 | {
|
---|
| 95 | // random barycentric coordinates
|
---|
| 96 | float a = u;
|
---|
| 97 | float b = v;
|
---|
| 98 | float c = 1.0f - u - v;
|
---|
| 99 |
|
---|
| 100 | point = mItem.mVertices[0] * a + mItem.mVertices[1] * b + mItem.mVertices[2] * c;
|
---|
| 101 | normal = mItem.GetNormal();
|
---|
| 102 |
|
---|
| 103 | return 0;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 |
|
---|
| 107 |
|
---|
[1587] | 108 | int TriangleIntersectable::GetRandomVisibleSurfacePoint(Vector3 &point,
|
---|
| 109 | Vector3 &normal,
|
---|
| 110 | const Vector3 &viewpoint,
|
---|
| 111 | const int maxTries)
|
---|
| 112 | {
|
---|
| 113 | return GetRandomSurfacePoint(point, normal);
|
---|
| 114 | }
|
---|
[1694] | 115 |
|
---|
| 116 |
|
---|
[2570] | 117 | int TriangleIntersectable::GetRandomEdgePoint(Vector3 &point, Vector3 &normal)
|
---|
[1763] | 118 | {
|
---|
[1765] | 119 | const int edge = Random(3);
|
---|
[1763] | 120 |
|
---|
[1765] | 121 | const Vector3 a = mItem.mVertices[edge];
|
---|
| 122 | const Vector3 b = mItem.mVertices[(edge + 1) % 3];
|
---|
[1763] | 123 |
|
---|
| 124 | const float factor = RandomValue(0.0f, 1.0f);
|
---|
| 125 |
|
---|
| 126 | point = a * factor + b * factor;
|
---|
| 127 | normal = mItem.GetNormal();
|
---|
| 128 |
|
---|
| 129 | return edge;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 |
|
---|
[2569] | 133 | KdIntersectable::KdIntersectable(KdNode *item, const AxisAlignedBox3 &box) :
|
---|
[2571] | 134 | IntersectableWrapper<KdNode *>(item), mNumTriangles(-1), mBox(box)
|
---|
[1694] | 135 | {
|
---|
| 136 | }
|
---|
[2569] | 137 |
|
---|
[2570] | 138 |
|
---|
[2569] | 139 | int KdIntersectable::ComputeNumTriangles()
|
---|
| 140 | {
|
---|
[2570] | 141 | Intersectable::NewMail();
|
---|
| 142 |
|
---|
[2569] | 143 | std::stack<KdNode *> tStack;
|
---|
| 144 | tStack.push(mItem);
|
---|
| 145 |
|
---|
| 146 | if (mNumTriangles != -1)
|
---|
| 147 | return mNumTriangles;
|
---|
| 148 |
|
---|
| 149 | mNumTriangles = 0;
|
---|
| 150 |
|
---|
| 151 | while (!tStack.empty())
|
---|
| 152 | {
|
---|
| 153 | KdNode *node = tStack.top();
|
---|
| 154 | tStack.pop();
|
---|
| 155 |
|
---|
| 156 | if (!node->IsLeaf())
|
---|
| 157 | {
|
---|
| 158 | KdInterior *kdInterior = static_cast<KdInterior *>(node);
|
---|
| 159 |
|
---|
| 160 | tStack.push(kdInterior->mBack);
|
---|
| 161 | tStack.push(kdInterior->mFront);
|
---|
| 162 | }
|
---|
| 163 | else
|
---|
| 164 | {
|
---|
| 165 | KdLeaf *leaf = static_cast<KdLeaf *>(node);
|
---|
| 166 |
|
---|
| 167 | int tri = 0;
|
---|
| 168 |
|
---|
| 169 | for (size_t i = 0; i < leaf->mObjects.size(); ++ i)
|
---|
| 170 | {
|
---|
| 171 | TriangleIntersectable *obj =
|
---|
| 172 | static_cast<TriangleIntersectable *>(leaf->mObjects[i]);
|
---|
| 173 |
|
---|
| 174 | // check if already rendered
|
---|
| 175 | if (!obj->Mailed())
|
---|
| 176 | {
|
---|
| 177 | obj->Mail();
|
---|
| 178 | ++ tri;
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | mNumTriangles += tri;
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | return mNumTriangles;
|
---|
[1694] | 187 | }
|
---|
[2569] | 188 |
|
---|
| 189 | }
|
---|