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

Revision 2615, 5.2 KB checked in by mattausch, 16 years ago (diff)

did some stuff for the visualization

Line 
1#include "IntersectableWrapper.h"
2#include "Mesh.h"
3#include "Triangle3.h"
4#include "KdTree.h"
5#include "BvHierarchy.h"
6#include "SimpleRay.h"
7
8
9namespace GtpVisibilityPreprocessor {
10
11
12AxisAlignedBox3 TriangleIntersectable::GetBox() const
13{       
14  return mItem.GetBoundingBox();
15}
16
17
18int
19TriangleIntersectable::CastSimpleRay(const SimpleRay &ray)
20{
21  static Vector3 nearestNormal;
22  // std::cout << "To be done - ray triangle intersection\n" << std::endl;
23  const int hitCode = mItem.CastSimpleRay(ray,
24                                          SimpleRay::IntersectionRes[0].tdist,
25                                          SimpleRay::IntersectionRes[0].maxt);
26  if (hitCode == Ray::INTERSECTION) {
27    SimpleRay::IntersectionRes[0].intersectable = this;
28    SimpleRay::IntersectionRes[0].maxt = SimpleRay::IntersectionRes[0].tdist;
29    return 1; // intersection occured
30  }
31  return 0; // no intersection
32}
33
34int
35TriangleIntersectable::CastSimpleRay(const SimpleRay &ray, int rayIndex)
36{
37  const int hitCode =
38    mItem.CastSimpleRay(ray,
39                        SimpleRay::IntersectionRes[rayIndex].tdist,
40                        SimpleRay::IntersectionRes[rayIndex].maxt);
41  if (hitCode == Ray::INTERSECTION) {
42    SimpleRay::IntersectionRes[rayIndex].intersectable = this;
43    // we copy the closest distance so far - it prunes the search
44    // when more triangles in the same leaf and assures the
45    // correctness.
46    SimpleRay::IntersectionRes[rayIndex].maxt =
47      SimpleRay::IntersectionRes[rayIndex].tdist;
48    return 1; // intersection occured
49  }
50  return 0; // no intersection
51}
52
53 
54int TriangleIntersectable::CastRay(Ray &ray)
55{       
56  float nearestT = MAX_FLOAT;
57  float t;
58  Vector3 nearestNormal;
59 
60  if (ray.GetType() == Ray::LOCAL_RAY && !ray.intersections.empty())
61    nearestT = ray.intersections[0].mT;
62 
63  const int hitCode = mItem.CastRay(ray, t, nearestT, nearestNormal);
64 
65  nearestT = t;
66 
67  if (hitCode == Ray::INTERSECTION) {
68    if (ray.GetType() == Ray::LOCAL_RAY && !ray.intersections.empty()) {
69      ray.intersections[0] = Ray::Intersection(nearestT,
70                                               nearestNormal,
71                                               this,
72                                               0);
73    }
74    else {
75      ray.intersections.push_back(Ray::Intersection(nearestT,
76                                                    nearestNormal,
77                                                    this,
78                                                    0));
79    }
80   
81    return 1;
82  }
83 
84  return 0;
85}
86       
87
88int TriangleIntersectable::NumberOfFaces() const
89{
90        return 1;
91}
92
93
94Vector3
95TriangleIntersectable::GetNormal(const int idx) const
96{
97  return mItem.GetNormal();
98}
99
100
101int
102TriangleIntersectable::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal)
103{
104        // random barycentric coordinates
105        float a = RandomValue(0.0f, 1.0f);
106        float b = RandomValue(0.0f, 1.0f);
107        float c = RandomValue(0.0f, 1.0f);
108       
109        const float sum = a + b + c;
110
111        // scale so we get vaccumated value of 1
112        if (sum)
113        {
114                a /= sum;
115                b /= sum;
116                c /= sum;
117        }
118
119        //cout << "a: " << a << " b: " << b << " c: " << c << " sum: " << sum << endl;
120        point = mItem.mVertices[0] * a + mItem.mVertices[1] * b + mItem.mVertices[2] * c;
121        normal = mItem.GetNormal();
122
123        return 0;
124}
125
126
127int
128TriangleIntersectable::GetRandomSurfacePoint(const float u,
129                                                                                         const float v,
130                                                                                         Vector3 &point,
131                                                                                         Vector3 &normal)
132{
133  // random barycentric coordinates
134  float a = u;
135  float b = v;
136  float c = 1.0f - u - v;
137 
138  point = mItem.mVertices[0] * a + mItem.mVertices[1] * b + mItem.mVertices[2] * c;
139  normal = mItem.GetNormal();
140 
141  return 0;
142}
143
144
145
146int TriangleIntersectable::GetRandomVisibleSurfacePoint(Vector3 &point,
147                                                                                                                Vector3 &normal,
148                                                                                                                const Vector3 &viewpoint,
149                                                                                                                const int maxTries)
150{
151        return GetRandomSurfacePoint(point, normal);
152}
153
154
155int TriangleIntersectable::GetRandomEdgePoint(Vector3 &point, Vector3 &normal)
156{
157        const int edge = Random(3);
158
159        const Vector3 a = mItem.mVertices[edge];
160        const Vector3 b = mItem.mVertices[(edge + 1) % 3];
161
162        const float factor = RandomValue(0.0f, 1.0f);
163
164        point = a * factor + b * factor;
165        normal = mItem.GetNormal();
166
167        return edge;
168}
169
170
171KdIntersectable::KdIntersectable(KdNode *item, const AxisAlignedBox3 &box) :
172  IntersectableWrapper<KdNode *>(item), mNumTriangles(-1), mBox(box), mGenericIdx(-1)
173{
174}
175
176
177int KdIntersectable::ComputeNumTriangles()
178{
179        Intersectable::NewMail();
180
181        std::stack<KdNode *> tStack;
182        tStack.push(mItem);
183
184        if (mNumTriangles != -1)
185                return mNumTriangles;
186
187        mNumTriangles = 0;
188
189        while (!tStack.empty())
190        {
191                KdNode *node = tStack.top();
192                tStack.pop();
193
194                if (!node->IsLeaf())
195                {
196                        KdInterior *kdInterior = static_cast<KdInterior *>(node);
197
198                        tStack.push(kdInterior->mBack);
199                        tStack.push(kdInterior->mFront);
200                }
201                else
202                {
203                        KdLeaf *leaf = static_cast<KdLeaf *>(node);
204
205                        int tri = 0;
206
207                        for (size_t i = 0; i < leaf->mObjects.size(); ++ i)
208                        {
209                                TriangleIntersectable *obj =
210                                        static_cast<TriangleIntersectable *>(leaf->mObjects[i]);
211                               
212                                // check if already rendered
213                                if (!obj->Mailed())
214                                {
215                                        obj->Mail();
216                                        ++ tri;
217                                }
218                        }
219
220            mNumTriangles += tri;
221                }
222        }
223
224        return mNumTriangles;
225}
226
227
228SceneGraphLeafIntersectable::SceneGraphLeafIntersectable(SceneGraphLeaf *item, const AxisAlignedBox3 &box):
229  IntersectableWrapper<SceneGraphLeaf *>(item), mBox(box)
230{
231}
232
233}
Note: See TracBrowser for help on using the repository browser.