[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;
|
---|
| 25 |
|
---|
| 26 | const int hitCode = mItem.CastRay(ray, t, nearestT, nearestNormal);
|
---|
| 27 |
|
---|
| 28 | nearestT = t;
|
---|
| 29 |
|
---|
| 30 | if ((hitCode == Ray::INTERSECTION) && (ray.GetType() == Ray::LOCAL_RAY))
|
---|
| 31 | {
|
---|
| 32 | if (!ray.intersections.empty())
|
---|
| 33 | {
|
---|
| 34 | ray.intersections[0] = Ray::Intersection(nearestT, nearestNormal, this, 0);
|
---|
| 35 | }
|
---|
| 36 | else
|
---|
| 37 | {
|
---|
| 38 | ray.intersections.push_back(Ray::Intersection(nearestT, nearestNormal, this, 0));
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | return 1;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[1327] | 44 | return 0;
|
---|
| 45 | }
|
---|
[1199] | 46 |
|
---|
[1327] | 47 |
|
---|
[1328] | 48 | int TriangleIntersectable::NumberOfFaces() const
|
---|
[1327] | 49 | {
|
---|
[1328] | 50 | return 1;
|
---|
[1327] | 51 | }
|
---|
| 52 |
|
---|
[1344] | 53 |
|
---|
| 54 | Vector3 TriangleIntersectable::GetNormal(const int idx) const
|
---|
| 55 | {
|
---|
| 56 | return mItem.GetNormal();
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[1586] | 59 | int
|
---|
| 60 | TriangleIntersectable::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal)
|
---|
| 61 | {
|
---|
| 62 | // random barycentric coordinates
|
---|
[1587] | 63 | float a = RandomValue(0,1);
|
---|
| 64 | float b = RandomValue(0,1);
|
---|
| 65 | float c = RandomValue(0,1);
|
---|
[1586] | 66 |
|
---|
[1587] | 67 | const float sum = a + b + c;
|
---|
| 68 |
|
---|
[1692] | 69 | // scale so we get vaccumated value of 1
|
---|
[1587] | 70 | if (sum)
|
---|
| 71 | {
|
---|
| 72 | a /= sum;
|
---|
| 73 | b /= sum;
|
---|
| 74 | c /= sum;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[1692] | 77 | //cout << "a: " << a << " b: " << b << " c: " << c << " sum: " << sum << endl;
|
---|
[1587] | 78 | point = mItem.mVertices[0] * a + mItem.mVertices[1] * b + mItem.mVertices[2] * c;
|
---|
[1586] | 79 | normal = mItem.GetNormal();
|
---|
| 80 |
|
---|
| 81 | return 0;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[1587] | 84 |
|
---|
| 85 | int TriangleIntersectable::GetRandomVisibleSurfacePoint(Vector3 &point,
|
---|
| 86 | Vector3 &normal,
|
---|
| 87 | const Vector3 &viewpoint,
|
---|
| 88 | const int maxTries)
|
---|
| 89 | {
|
---|
| 90 | return GetRandomSurfacePoint(point, normal);
|
---|
| 91 | }
|
---|
[1694] | 92 |
|
---|
| 93 |
|
---|
[1763] | 94 | int TriangleIntersectable::GetRandomEdgePoint(Vector3 &point,
|
---|
| 95 | Vector3 &normal)
|
---|
| 96 | {
|
---|
| 97 | int edge = Random(3);
|
---|
| 98 |
|
---|
| 99 | Vector3 a = mItem.mVertices[edge];
|
---|
| 100 | Vector3 b = mItem.mVertices[(edge + 1) % 3];
|
---|
| 101 |
|
---|
| 102 | const float factor = RandomValue(0.0f, 1.0f);
|
---|
| 103 |
|
---|
| 104 | point = a * factor + b * factor;
|
---|
| 105 | normal = mItem.GetNormal();
|
---|
| 106 |
|
---|
| 107 | return edge;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 |
|
---|
[1694] | 111 | KdIntersectable::KdIntersectable(KdNode *item, const AxisAlignedBox3 &box) :
|
---|
| 112 | IntersectableWrapper<KdNode *>(item)
|
---|
| 113 | {
|
---|
| 114 | mBox = box;
|
---|
| 115 | }
|
---|
| 116 | }
|
---|