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

Revision 1824, 2.5 KB checked in by bittner, 18 years ago (diff)

global lines support

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) {
31                if (ray.GetType() == Ray::LOCAL_RAY && !ray.intersections.empty()) {
32                        ray.intersections[0] = Ray::Intersection(nearestT,
33                                                                                                                                                                                         nearestNormal,
34                                                                                                                                                                                         this,
35                                                                                                                                                                                         0);
36                }
37                else {
38                        ray.intersections.push_back(Ray::Intersection(nearestT,
39                                                                                                                                                                                                                nearestNormal,
40                                                                                                                                                                                                                this,
41                                                                                                                                                                                                                0));
42                }
43               
44                return 1;
45        }
46       
47        return 0;
48}
49       
50
51int TriangleIntersectable::NumberOfFaces() const
52{
53        return 1;
54}
55
56
57Vector3 TriangleIntersectable::GetNormal(const int idx) const
58{
59        return mItem.GetNormal();
60}
61
62
63int
64TriangleIntersectable::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal)
65{
66        // random barycentric coordinates
67        float a = RandomValue(0.0f, 1.0f);
68        float b = RandomValue(0.0f, 1.0f);
69        float c = RandomValue(0.0f, 1.0f);
70       
71        const float sum = a + b + c;
72
73        // scale so we get vaccumated value of 1
74        if (sum)
75        {
76                a /= sum;
77                b /= sum;
78                c /= sum;
79        }
80
81        //cout << "a: " << a << " b: " << b << " c: " << c << " sum: " << sum << endl;
82        point = mItem.mVertices[0] * a + mItem.mVertices[1] * b + mItem.mVertices[2] * c;
83        normal = mItem.GetNormal();
84
85        return 0;
86}
87
88
89int TriangleIntersectable::GetRandomVisibleSurfacePoint(Vector3 &point,
90                                                                                                                Vector3 &normal,
91                                                                                                                const Vector3 &viewpoint,
92                                                                                                                const int maxTries)
93{
94        return GetRandomSurfacePoint(point, normal);
95}
96
97
98int TriangleIntersectable::GetRandomEdgePoint(Vector3 &point,
99                                                                                          Vector3 &normal)
100{
101        const int edge = Random(3);
102
103        const Vector3 a = mItem.mVertices[edge];
104        const Vector3 b = mItem.mVertices[(edge + 1) % 3];
105
106        const float factor = RandomValue(0.0f, 1.0f);
107
108        point = a * factor + b * factor;
109        normal = mItem.GetNormal();
110
111        return edge;
112}
113
114
115KdIntersectable::KdIntersectable(KdNode *item, const AxisAlignedBox3 &box)  :
116  IntersectableWrapper<KdNode *>(item)
117{
118        mBox = box;
119}
120}
Note: See TracBrowser for help on using the repository browser.