source: GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.cpp @ 1786

Revision 1786, 2.4 KB checked in by mattausch, 18 years ago (diff)
Line 
1#include "IntersectableWrapper.h"
2#include "Mesh.h"
3#include "Triangle3.h"
4#include "KdTree.h"
5#include "BvHierarchy.h"
6
7
8namespace GtpVisibilityPreprocessor {
9
10
11AxisAlignedBox3 TriangleIntersectable::GetBox() const
12{       
13        return mItem.GetBoundingBox();
14}
15
16
17int 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
48int TriangleIntersectable::NumberOfFaces() const
49{
50        return 1;
51}
52
53
54Vector3 TriangleIntersectable::GetNormal(const int idx) const
55{
56        return mItem.GetNormal();
57}
58
59
60int
61TriangleIntersectable::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal)
62{
63        // random barycentric coordinates
64        float a = RandomValue(0.0f, 1.0f);
65        float b = RandomValue(0.0f, 1.0f);
66        float c = RandomValue(0.0f, 1.0f);
67       
68        const float sum = a + b + c;
69
70        // scale so we get vaccumated value of 1
71        if (sum)
72        {
73                a /= sum;
74                b /= sum;
75                c /= sum;
76        }
77
78        //cout << "a: " << a << " b: " << b << " c: " << c << " sum: " << sum << endl;
79        point = mItem.mVertices[0] * a + mItem.mVertices[1] * b + mItem.mVertices[2] * c;
80        normal = mItem.GetNormal();
81
82        return 0;
83}
84
85
86int TriangleIntersectable::GetRandomVisibleSurfacePoint(Vector3 &point,
87                                                                                                                Vector3 &normal,
88                                                                                                                const Vector3 &viewpoint,
89                                                                                                                const int maxTries)
90{
91        return GetRandomSurfacePoint(point, normal);
92}
93
94
95int TriangleIntersectable::GetRandomEdgePoint(Vector3 &point,
96                                                                                          Vector3 &normal)
97{
98        const int edge = Random(3);
99
100        const Vector3 a = mItem.mVertices[edge];
101        const Vector3 b = mItem.mVertices[(edge + 1) % 3];
102
103        const float factor = RandomValue(0.0f, 1.0f);
104
105        point = a * factor + b * factor;
106        normal = mItem.GetNormal();
107
108        return edge;
109}
110
111
112KdIntersectable::KdIntersectable(KdNode *item, const AxisAlignedBox3 &box)  :
113  IntersectableWrapper<KdNode *>(item)
114{
115        mBox = box;
116}
117}
Note: See TracBrowser for help on using the repository browser.