1 | #include "IntersectableWrapper.h"
|
---|
2 | #include "Mesh.h"
|
---|
3 | #include "Triangle3.h"
|
---|
4 | #include "KdTree.h"
|
---|
5 |
|
---|
6 |
|
---|
7 | namespace GtpVisibilityPreprocessor {
|
---|
8 |
|
---|
9 |
|
---|
10 | AxisAlignedBox3 TriangleIntersectable::GetBox() const
|
---|
11 | {
|
---|
12 | return mItem.GetBoundingBox();
|
---|
13 | }
|
---|
14 |
|
---|
15 |
|
---|
16 | int TriangleIntersectable::CastRay(Ray &ray)
|
---|
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 |
|
---|
43 | return 0;
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | int TriangleIntersectable::NumberOfFaces() const
|
---|
48 | {
|
---|
49 | return 1;
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | Vector3 TriangleIntersectable::GetNormal(const int idx) const
|
---|
54 | {
|
---|
55 | return mItem.GetNormal();
|
---|
56 | }
|
---|
57 |
|
---|
58 | int
|
---|
59 | TriangleIntersectable::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal)
|
---|
60 | {
|
---|
61 | // random barycentric coordinates
|
---|
62 | float a = RandomValue(0,1);
|
---|
63 | float b = RandomValue(0,1);
|
---|
64 | float c = RandomValue(0,1);
|
---|
65 |
|
---|
66 | const float sum = a + b + c;
|
---|
67 |
|
---|
68 | // scale so we get vaccumated value of 1
|
---|
69 | if (sum)
|
---|
70 | {
|
---|
71 | a /= sum;
|
---|
72 | b /= sum;
|
---|
73 | c /= sum;
|
---|
74 | }
|
---|
75 |
|
---|
76 | //cout << "a: " << a << " b: " << b << " c: " << c << " sum: " << sum << endl;
|
---|
77 | point = mItem.mVertices[0] * a + mItem.mVertices[1] * b + mItem.mVertices[2] * c;
|
---|
78 | normal = mItem.GetNormal();
|
---|
79 |
|
---|
80 | return 0;
|
---|
81 | }
|
---|
82 |
|
---|
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 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | KdIntersectable::KdIntersectable(KdNode *item, const AxisAlignedBox3 &box) :
|
---|
94 | IntersectableWrapper<KdNode *>(item)
|
---|
95 | {
|
---|
96 | mBox = box;
|
---|
97 | }
|
---|
98 |
|
---|
99 | }
|
---|