source: GTP/trunk/Lib/Vis/Preprocessing/src/RayInfo.cpp @ 2239

Revision 2239, 2.9 KB checked in by mattausch, 18 years ago (diff)
Line 
1#include "RayInfo.h"
2#include "Ray.h"
3#include "VssRay.h"
4#include "Plane3.h"
5
6using namespace std;
7
8namespace GtpVisibilityPreprocessor {
9
10RayInfo::RayInfo(): mRay(NULL), mMinT(0),
11#if USE_FIXEDPOINT_T
12#define FIXEDPOINT_ONE 0x7FFE
13        // mMaxT(0xFFFF)
14        mMaxT(FIXEDPOINT_ONE)
15#else
16        mMaxT(1.0f)
17#endif
18{}
19               
20RayInfo::RayInfo(VssRay *r):mRay(r), mMinT(0),
21#if USE_FIXEDPOINT_T
22#define FIXEDPOINT_ONE 0x7FFE
23        // mMaxT(0xFFFF)
24        mMaxT(FIXEDPOINT_ONE)
25#else
26        mMaxT(1.0f)
27#endif
28{}
29               
30RayInfo::RayInfo(VssRay *r,     const float _min, const float _max):
31mRay(r)
32{
33        SetMinT(_min);
34        SetMaxT(_max);
35}
36               
37RayInfo::RayInfo(VssRay *r, const short _min, const float _max):
38mRay(r), mMinT(_min)
39{
40        SetMaxT(_max);
41}
42               
43RayInfo::RayInfo(VssRay *r,     const float _min, const short _max):
44mRay(r), mMaxT(_max)
45{
46        SetMinT(_min);
47}
48
49
50int RayInfo::ComputeRayIntersection(const int axis, const float position, float &t) const
51{               
52        // intersect the ray with the plane
53        const float denom = mRay->GetDir(axis);
54
55        if (fabs(denom) < 1e-20)
56                //if (denom == 0.0f)
57                return (mRay->GetOrigin(axis) > position) ? 1 : -1;
58
59        t = (position - mRay->GetOrigin(axis)) / denom;
60
61        if (t < GetMinT())
62                return (denom > 0) ? 1 : -1;
63
64        if (t > GetMaxT())
65                return (denom > 0) ? -1 : 1;
66
67        return 0;
68}
69
70
71int RayInfo::ComputeRayIntersection(const Plane3 &splitPlane, float &t) const
72{       
73        t = splitPlane.FindT(mRay->GetOrigin(), mRay->GetTermination());
74
75        if (0)
76                Debug << "t: " << t << " mint " << GetMinT() << " maxt " << GetMaxT()
77                      << " orig: " << mRay->GetOrigin() << " term " << mRay->GetTermination() << endl;
78
79        // NOTE: if plane equals end point of ray, the ray should be classified
80        // non-intersecting, otherwise polygon-plane intersections of bsp tree
81        // approaches are not eliminating any rays intersecting the polygon!
82        const float thresh = 1e-6f;
83
84        // segment is not intersecting plane: fond out if on front or back side
85        if (t >= (GetMaxT() - thresh))
86        {
87                return splitPlane.Side(ExtrapOrigin());
88        }
89               
90        if (t <= (GetMinT() + thresh))
91        {
92                return splitPlane.Side(ExtrapTermination());
93        }
94       
95        return 0;
96}
97
98
99float RayInfo::SegmentLength() const
100{
101        return Distance(ExtrapOrigin(), ExtrapTermination());
102}
103
104
105float RayInfo::SqrSegmentLength() const
106{
107        return SqrDistance(ExtrapOrigin(), ExtrapTermination());
108}
109
110
111void GetRayInfoSets(const RayInfoContainer &sourceRays,
112                    const int maxSize,
113                                        RayInfoContainer &usedRays,
114                                        RayInfoContainer *savedRays)
115{
116        const int limit = min(maxSize, (int)sourceRays.size());
117        const float prop = (float)limit / ((float)sourceRays.size() + Limits::Small);
118
119        RayInfoContainer::const_iterator it, it_end = sourceRays.end();
120
121        for (it = sourceRays.begin(); it != it_end; ++ it)
122        {
123                if (Random(1.0f) < prop)
124                        usedRays.push_back(*it);
125                else if (savedRays)
126                        savedRays->push_back(*it);
127        }
128}
129
130}
Note: See TracBrowser for help on using the repository browser.