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

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