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