[420] | 1 | #include "RayInfo.h"
|
---|
| 2 | #include "Ray.h"
|
---|
| 3 | #include "VssRay.h"
|
---|
[437] | 4 | #include "Plane3.h"
|
---|
[420] | 5 |
|
---|
[432] | 6 | RayInfo::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
|
---|
[420] | 14 | {}
|
---|
| 15 |
|
---|
| 16 | RayInfo::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 |
|
---|
| 26 | RayInfo::RayInfo(VssRay *r, const float _min, const float _max):
|
---|
| 27 | mRay(r)
|
---|
| 28 | {
|
---|
| 29 | SetMinT(_min);
|
---|
| 30 | SetMaxT(_max);
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | RayInfo::RayInfo(VssRay *r, const short _min, const float _max):
|
---|
| 34 | mRay(r), mMinT(_min)
|
---|
| 35 | {
|
---|
| 36 | SetMaxT(_max);
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | RayInfo::RayInfo(VssRay *r, const float _min, const short _max):
|
---|
| 40 | mRay(r), mMaxT(_max)
|
---|
| 41 | {
|
---|
| 42 | SetMinT(_min);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | float RayInfo::ExtrapOrigin(const int axis) const
|
---|
| 46 | {
|
---|
[448] | 47 | return mRay->GetOrigin(axis) + GetMinT() * mRay->GetDir(axis);
|
---|
[420] | 48 | }
|
---|
| 49 |
|
---|
| 50 | float RayInfo::ExtrapTermination(const int axis) const
|
---|
| 51 | {
|
---|
[448] | 52 | return mRay->GetOrigin(axis) + GetMaxT() * mRay->GetDir(axis);
|
---|
[420] | 53 | }
|
---|
[437] | 54 |
|
---|
| 55 | Vector3 RayInfo::ExtrapOrigin() const
|
---|
| 56 | {
|
---|
[448] | 57 | return mRay->GetOrigin() + GetMinT() * mRay->GetDir();
|
---|
[437] | 58 | }
|
---|
[420] | 59 |
|
---|
[437] | 60 | Vector3 RayInfo::ExtrapTermination() const
|
---|
| 61 | {
|
---|
| 62 | return mRay->GetOrigin() + GetMaxT() * mRay->GetDir();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[420] | 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 |
|
---|
[639] | 110 |
|
---|
[420] | 111 | int 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;
|
---|
[437] | 129 | }
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 | int RayInfo::ComputeRayIntersection(const Plane3 &splitPlane, float &t) const
|
---|
| 133 | {
|
---|
| 134 | t = splitPlane.FindT(mRay->GetOrigin(), mRay->GetTermination());
|
---|
| 135 |
|
---|
[639] | 136 | if (0)
|
---|
| 137 | Debug << "t: " << t << " mint " << GetMinT() << " maxt " << GetMaxT()
|
---|
| 138 | << " orig: " << mRay->GetOrigin() << " term " << mRay->GetTermination() << endl;
|
---|
| 139 |
|
---|
[485] | 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!
|
---|
[694] | 143 | const float thresh = 1e-6f;
|
---|
[639] | 144 |
|
---|
| 145 | // segment is not intersecting plane: fond out if on front or back side
|
---|
[801] | 146 | if (t >= (GetMaxT() - thresh))
|
---|
[639] | 147 | {
|
---|
[437] | 148 | return splitPlane.Side(ExtrapOrigin());
|
---|
[639] | 149 | }
|
---|
| 150 |
|
---|
[801] | 151 | if (t <= (GetMinT() + thresh))
|
---|
[639] | 152 | {
|
---|
[485] | 153 | return splitPlane.Side(ExtrapTermination());
|
---|
[639] | 154 | }
|
---|
| 155 |
|
---|
[437] | 156 | return 0;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[639] | 159 |
|
---|
[437] | 160 | float RayInfo::SegmentLength() const
|
---|
| 161 | {
|
---|
| 162 | return Distance(ExtrapOrigin(), ExtrapTermination());
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | float RayInfo::SqrSegmentLength() const
|
---|
| 166 | {
|
---|
| 167 | return SqrDistance(ExtrapOrigin(), ExtrapTermination());
|
---|
| 168 | }
|
---|