1 | #include "RayInfo.h"
|
---|
2 | #include "Ray.h"
|
---|
3 | #include "VssRay.h"
|
---|
4 | #include "Plane3.h"
|
---|
5 |
|
---|
6 | using namespace std;
|
---|
7 |
|
---|
8 | namespace GtpVisibilityPreprocessor {
|
---|
9 |
|
---|
10 | RayInfo::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 |
|
---|
20 | RayInfo::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 |
|
---|
30 | RayInfo::RayInfo(VssRay *r, const float _min, const float _max):
|
---|
31 | mRay(r)
|
---|
32 | {
|
---|
33 | SetMinT(_min);
|
---|
34 | SetMaxT(_max);
|
---|
35 | }
|
---|
36 |
|
---|
37 | RayInfo::RayInfo(VssRay *r, const short _min, const float _max):
|
---|
38 | mRay(r), mMinT(_min)
|
---|
39 | {
|
---|
40 | SetMaxT(_max);
|
---|
41 | }
|
---|
42 |
|
---|
43 | RayInfo::RayInfo(VssRay *r, const float _min, const short _max):
|
---|
44 | mRay(r), mMaxT(_max)
|
---|
45 | {
|
---|
46 | SetMinT(_min);
|
---|
47 | }
|
---|
48 |
|
---|
49 | float RayInfo::ExtrapOrigin(const int axis) const
|
---|
50 | {
|
---|
51 | return mRay->GetOrigin(axis) + GetMinT() * mRay->GetDir(axis);
|
---|
52 | }
|
---|
53 |
|
---|
54 | float RayInfo::ExtrapTermination(const int axis) const
|
---|
55 | {
|
---|
56 | return mRay->GetOrigin(axis) + GetMaxT() * mRay->GetDir(axis);
|
---|
57 | }
|
---|
58 |
|
---|
59 | Vector3 RayInfo::ExtrapOrigin() const
|
---|
60 | {
|
---|
61 | return mRay->GetOrigin() + GetMinT() * mRay->GetDir();
|
---|
62 | }
|
---|
63 |
|
---|
64 | Vector3 RayInfo::ExtrapTermination() const
|
---|
65 | {
|
---|
66 | return mRay->GetOrigin() + GetMaxT() * mRay->GetDir();
|
---|
67 | }
|
---|
68 |
|
---|
69 | #if USE_FIXEDPOINT_T
|
---|
70 | float RayInfo::GetMinT () const
|
---|
71 | {
|
---|
72 | return mMinT/(float)(FIXEDPOINT_ONE);
|
---|
73 | }
|
---|
74 |
|
---|
75 | float RayInfo::GetMaxT() const
|
---|
76 | {
|
---|
77 | return mMaxT / (float)(FIXEDPOINT_ONE);
|
---|
78 | }
|
---|
79 |
|
---|
80 | void RayInfo::SetMinT (const float t)
|
---|
81 | {
|
---|
82 | mMinT = (short) (t * (float)(FIXEDPOINT_ONE));
|
---|
83 | }
|
---|
84 |
|
---|
85 | void RayInfo::SetMaxT (const float t)
|
---|
86 | {
|
---|
87 | mMaxT = (short) (t*(float)(FIXEDPOINT_ONE));
|
---|
88 | ++ mMaxT;
|
---|
89 | // if (mMaxT!=0xFFFF)
|
---|
90 | // mMaxT++;
|
---|
91 | }
|
---|
92 | #else
|
---|
93 | float RayInfo::GetMinT () const
|
---|
94 | {
|
---|
95 | return mMinT;
|
---|
96 | }
|
---|
97 |
|
---|
98 | float RayInfo::GetMaxT () const
|
---|
99 | {
|
---|
100 | return mMaxT;
|
---|
101 | }
|
---|
102 |
|
---|
103 | void RayInfo::SetMinT (const float t)
|
---|
104 | {
|
---|
105 | mMinT = t;
|
---|
106 | }
|
---|
107 |
|
---|
108 | void RayInfo::SetMaxT (const float t)
|
---|
109 | {
|
---|
110 | mMaxT = t;
|
---|
111 | }
|
---|
112 | #endif
|
---|
113 |
|
---|
114 |
|
---|
115 | int RayInfo::ComputeRayIntersection(const int axis, const float position, float &t) const
|
---|
116 | {
|
---|
117 | // intersect the ray with the plane
|
---|
118 | const float denom = mRay->GetDir(axis);
|
---|
119 |
|
---|
120 | if (fabs(denom) < 1e-20)
|
---|
121 | //if (denom == 0.0f)
|
---|
122 | return (mRay->GetOrigin(axis) > position) ? 1 : -1;
|
---|
123 |
|
---|
124 | t = (position - mRay->GetOrigin(axis)) / denom;
|
---|
125 |
|
---|
126 | if (t < GetMinT())
|
---|
127 | return (denom > 0) ? 1 : -1;
|
---|
128 |
|
---|
129 | if (t > GetMaxT())
|
---|
130 | return (denom > 0) ? -1 : 1;
|
---|
131 |
|
---|
132 | return 0;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | int RayInfo::ComputeRayIntersection(const Plane3 &splitPlane, float &t) const
|
---|
137 | {
|
---|
138 | t = splitPlane.FindT(mRay->GetOrigin(), mRay->GetTermination());
|
---|
139 |
|
---|
140 | if (0)
|
---|
141 | Debug << "t: " << t << " mint " << GetMinT() << " maxt " << GetMaxT()
|
---|
142 | << " orig: " << mRay->GetOrigin() << " term " << mRay->GetTermination() << endl;
|
---|
143 |
|
---|
144 | // NOTE: if plane equals end point of ray, the ray should be classified
|
---|
145 | // non-intersecting, otherwise polygon-plane intersections of bsp tree
|
---|
146 | // approaches are not eliminating any rays intersecting the polygon!
|
---|
147 | const float thresh = 1e-6f;
|
---|
148 |
|
---|
149 | // segment is not intersecting plane: fond out if on front or back side
|
---|
150 | if (t >= (GetMaxT() - thresh))
|
---|
151 | {
|
---|
152 | return splitPlane.Side(ExtrapOrigin());
|
---|
153 | }
|
---|
154 |
|
---|
155 | if (t <= (GetMinT() + thresh))
|
---|
156 | {
|
---|
157 | return splitPlane.Side(ExtrapTermination());
|
---|
158 | }
|
---|
159 |
|
---|
160 | return 0;
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | float RayInfo::SegmentLength() const
|
---|
165 | {
|
---|
166 | return Distance(ExtrapOrigin(), ExtrapTermination());
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | float RayInfo::SqrSegmentLength() const
|
---|
171 | {
|
---|
172 | return SqrDistance(ExtrapOrigin(), ExtrapTermination());
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | void GetRayInfoSets(const RayInfoContainer &sourceRays,
|
---|
177 | const int maxSize,
|
---|
178 | RayInfoContainer &usedRays,
|
---|
179 | RayInfoContainer *savedRays)
|
---|
180 | {
|
---|
181 | const int limit = min(maxSize, (int)sourceRays.size());
|
---|
182 | const float prop = (float)limit / ((float)sourceRays.size() + Limits::Small);
|
---|
183 |
|
---|
184 | RayInfoContainer::const_iterator it, it_end = sourceRays.end();
|
---|
185 |
|
---|
186 | for (it = sourceRays.begin(); it != it_end; ++ it)
|
---|
187 | {
|
---|
188 | if (Random(1.0f) < prop)
|
---|
189 | usedRays.push_back(*it);
|
---|
190 | else if (savedRays)
|
---|
191 | savedRays->push_back(*it);
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | } |
---|