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