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

Revision 639, 3.5 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
6RayInfo::RayInfo(): mRay(NULL), mMinT(0),
7#if USE_FIXEDPOINT_T
8#define FIXEDPOINT_ONE 0x7FFE
9        // mMaxT(0xFFFF)
10        mMaxT(FIXEDPOINT_ONE)
11#else
12        mMaxT(1.0f)
13#endif
14{}
15               
16RayInfo::RayInfo(VssRay *r):mRay(r), mMinT(0),
17#if USE_FIXEDPOINT_T
18#define FIXEDPOINT_ONE 0x7FFE
19        // mMaxT(0xFFFF)
20        mMaxT(FIXEDPOINT_ONE)
21#else
22        mMaxT(1.0f)
23#endif
24{}
25               
26RayInfo::RayInfo(VssRay *r,     const float _min, const float _max):
27mRay(r)
28{
29        SetMinT(_min);
30        SetMaxT(_max);
31}
32               
33RayInfo::RayInfo(VssRay *r, const short _min, const float _max):
34mRay(r), mMinT(_min)
35{
36        SetMaxT(_max);
37}
38               
39RayInfo::RayInfo(VssRay *r,     const float _min, const short _max):
40mRay(r), mMaxT(_max)
41{
42        SetMinT(_min);
43}
44               
45float RayInfo::ExtrapOrigin(const int axis) const
46{
47        return mRay->GetOrigin(axis) + GetMinT() * mRay->GetDir(axis);
48}
49               
50float RayInfo::ExtrapTermination(const int axis) const
51{
52        return mRay->GetOrigin(axis) + GetMaxT() * mRay->GetDir(axis);
53}
54
55Vector3 RayInfo::ExtrapOrigin() const
56{
57        return mRay->GetOrigin() + GetMinT() * mRay->GetDir();
58}
59               
60Vector3 RayInfo::ExtrapTermination() const
61{
62        return mRay->GetOrigin() + GetMaxT() * mRay->GetDir();
63}
64
65#if USE_FIXEDPOINT_T
66        float RayInfo::GetMinT () const
67        {
68                return mMinT/(float)(FIXEDPOINT_ONE);
69        }
70
71        float RayInfo::GetMaxT() const
72        {
73                return mMaxT / (float)(FIXEDPOINT_ONE);
74        }
75               
76        void RayInfo::SetMinT (const float t)
77        {
78                mMinT = (short) (t * (float)(FIXEDPOINT_ONE));
79        }
80               
81        void RayInfo::SetMaxT (const float t)
82        {
83                mMaxT = (short) (t*(float)(FIXEDPOINT_ONE));
84                ++ mMaxT;
85                //      if (mMaxT!=0xFFFF)
86                //      mMaxT++;
87        }
88#else
89        float RayInfo::GetMinT () const
90        {
91                return mMinT;
92        }
93       
94        float RayInfo::GetMaxT () const
95        {
96                return mMaxT;
97        }
98               
99        void RayInfo::SetMinT (const float t)
100        {
101                mMinT = t;
102        }
103       
104        void RayInfo::SetMaxT (const float t)
105        {
106                mMaxT = t;
107        }
108#endif
109
110
111int RayInfo::ComputeRayIntersection(const int axis, const float position, float &t) const
112{               
113        // intersect the ray with the plane
114        const float denom = mRay->GetDir(axis);
115
116        if (fabs(denom) < 1e-20)
117                //if (denom == 0.0f)
118                return (mRay->GetOrigin(axis) > position) ? 1 : -1;
119
120        t = (position - mRay->GetOrigin(axis)) / denom;
121
122        if (t < GetMinT())
123                return (denom > 0) ? 1 : -1;
124
125        if (t > GetMaxT())
126                return (denom > 0) ? -1 : 1;
127
128        return 0;
129}
130
131
132int RayInfo::ComputeRayIntersection(const Plane3 &splitPlane, float &t) const
133{       
134        t = splitPlane.FindT(mRay->GetOrigin(), mRay->GetTermination());
135
136        if (0)
137                Debug << "t: " << t << " mint " << GetMinT() << " maxt " << GetMaxT()
138                      << " orig: " << mRay->GetOrigin() << " term " << mRay->GetTermination() << endl;
139
140        // NOTE: if plane equals end point of ray, the ray should be classified
141        // non-intersecting, otherwise polygon-plane intersections of bsp tree
142        // approaches are not eliminating any rays intersecting the polygon!
143        const float thresh = 1 ? 1e-6f : 0.0f;
144
145        // segment is not intersecting plane: fond out if on front or back side
146        if (t >= GetMaxT() - thresh)
147        {
148                return splitPlane.Side(ExtrapOrigin());
149        }
150               
151        if (t <= GetMinT() + thresh)
152        {
153                return splitPlane.Side(ExtrapTermination());
154        }
155       
156        return 0;
157}
158
159
160float RayInfo::SegmentLength() const
161{
162        return Distance(ExtrapOrigin(), ExtrapTermination());
163}
164
165float RayInfo::SqrSegmentLength() const
166{
167        return SqrDistance(ExtrapOrigin(), ExtrapTermination());
168}
Note: See TracBrowser for help on using the repository browser.