[1315] | 1 | #include "IntersectableWrapper.h"
|
---|
[1328] | 2 | #include "Mesh.h"
|
---|
| 3 | #include "Triangle3.h"
|
---|
[1694] | 4 | #include "KdTree.h"
|
---|
[1199] | 5 |
|
---|
| 6 |
|
---|
| 7 | namespace GtpVisibilityPreprocessor {
|
---|
| 8 |
|
---|
| 9 |
|
---|
[1328] | 10 | AxisAlignedBox3 TriangleIntersectable::GetBox() const
|
---|
[1327] | 11 | {
|
---|
[1344] | 12 | return mItem.GetBoundingBox();
|
---|
[1327] | 13 | }
|
---|
[1199] | 14 |
|
---|
[1327] | 15 |
|
---|
[1328] | 16 | int TriangleIntersectable::CastRay(Ray &ray)
|
---|
[1344] | 17 | {
|
---|
| 18 | float nearestT = MAX_FLOAT;
|
---|
| 19 | float t;
|
---|
| 20 | Vector3 nearestNormal;
|
---|
| 21 |
|
---|
| 22 | if (ray.GetType() == Ray::LOCAL_RAY && !ray.intersections.empty())
|
---|
| 23 | nearestT = ray.intersections[0].mT;
|
---|
| 24 |
|
---|
| 25 | const int hitCode = mItem.CastRay(ray, t, nearestT, nearestNormal);
|
---|
| 26 |
|
---|
| 27 | nearestT = t;
|
---|
| 28 |
|
---|
| 29 | if ((hitCode == Ray::INTERSECTION) && (ray.GetType() == Ray::LOCAL_RAY))
|
---|
| 30 | {
|
---|
| 31 | if (!ray.intersections.empty())
|
---|
| 32 | {
|
---|
| 33 | ray.intersections[0] = Ray::Intersection(nearestT, nearestNormal, this, 0);
|
---|
| 34 | }
|
---|
| 35 | else
|
---|
| 36 | {
|
---|
| 37 | ray.intersections.push_back(Ray::Intersection(nearestT, nearestNormal, this, 0));
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | return 1;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[1327] | 43 | return 0;
|
---|
| 44 | }
|
---|
[1199] | 45 |
|
---|
[1327] | 46 |
|
---|
[1328] | 47 | int TriangleIntersectable::NumberOfFaces() const
|
---|
[1327] | 48 | {
|
---|
[1328] | 49 | return 1;
|
---|
[1327] | 50 | }
|
---|
| 51 |
|
---|
[1344] | 52 |
|
---|
| 53 | Vector3 TriangleIntersectable::GetNormal(const int idx) const
|
---|
| 54 | {
|
---|
| 55 | return mItem.GetNormal();
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[1586] | 58 | int
|
---|
| 59 | TriangleIntersectable::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal)
|
---|
| 60 | {
|
---|
| 61 | // random barycentric coordinates
|
---|
[1587] | 62 | float a = RandomValue(0,1);
|
---|
| 63 | float b = RandomValue(0,1);
|
---|
| 64 | float c = RandomValue(0,1);
|
---|
[1586] | 65 |
|
---|
[1587] | 66 | const float sum = a + b + c;
|
---|
| 67 |
|
---|
[1692] | 68 | // scale so we get vaccumated value of 1
|
---|
[1587] | 69 | if (sum)
|
---|
| 70 | {
|
---|
| 71 | a /= sum;
|
---|
| 72 | b /= sum;
|
---|
| 73 | c /= sum;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[1692] | 76 | //cout << "a: " << a << " b: " << b << " c: " << c << " sum: " << sum << endl;
|
---|
[1587] | 77 | point = mItem.mVertices[0] * a + mItem.mVertices[1] * b + mItem.mVertices[2] * c;
|
---|
[1586] | 78 | normal = mItem.GetNormal();
|
---|
| 79 |
|
---|
| 80 | return 0;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[1587] | 83 |
|
---|
| 84 | int TriangleIntersectable::GetRandomVisibleSurfacePoint(Vector3 &point,
|
---|
| 85 | Vector3 &normal,
|
---|
| 86 | const Vector3 &viewpoint,
|
---|
| 87 | const int maxTries)
|
---|
| 88 | {
|
---|
| 89 | return GetRandomSurfacePoint(point, normal);
|
---|
| 90 | }
|
---|
[1694] | 91 |
|
---|
| 92 |
|
---|
| 93 | KdIntersectable::KdIntersectable(KdNode *item, const AxisAlignedBox3 &box) :
|
---|
| 94 | IntersectableWrapper<KdNode *>(item)
|
---|
| 95 | {
|
---|
| 96 | mBox = box;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | }
|
---|