[162] | 1 | #include "Ray.h"
|
---|
[327] | 2 | #include "Plane3.h"
|
---|
[162] | 3 |
|
---|
| 4 | // =========================================================
|
---|
| 5 | // Ray .. static item used for generation of unique ID for
|
---|
| 6 | // each instantiated ray
|
---|
| 7 |
|
---|
| 8 | // it has to start from 1, since 0 is default and for the first
|
---|
| 9 | // ray initial value 0 will not work .. V.H.
|
---|
| 10 | // The value 0 is reserved for particular purpose - the rays
|
---|
| 11 | // that are converted to canonical space and thus the mailbox
|
---|
| 12 | // rayID identification does not work for them!
|
---|
| 13 | int
|
---|
| 14 | Ray::genID = 1;
|
---|
| 15 |
|
---|
| 16 | // Precompute some Ray parameters. Most of them is used for
|
---|
| 17 | // ropes traversal.
|
---|
| 18 |
|
---|
| 19 | void
|
---|
| 20 | Ray::Init()
|
---|
| 21 | {
|
---|
| 22 | // if (mType == LOCAL_RAY)
|
---|
| 23 | // intersections.reserve(1);
|
---|
| 24 | // else
|
---|
| 25 | // intersections.reserve(10);
|
---|
| 26 |
|
---|
| 27 | // apply the standard precomputation
|
---|
| 28 | Precompute();
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | void
|
---|
| 32 | Ray::Precompute()
|
---|
| 33 | {
|
---|
| 34 | // initialize inverted dir
|
---|
| 35 | invDir.SetValue(0.0);
|
---|
| 36 |
|
---|
[328] | 37 | SetId();
|
---|
[162] | 38 | }
|
---|
| 39 |
|
---|
| 40 | void
|
---|
| 41 | Ray::SetLoc(const Vector3 &l)
|
---|
| 42 | {
|
---|
| 43 | loc = l;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | // make such operation to slightly change the ray direction
|
---|
| 47 | // in case any component of ray direction is zero.
|
---|
| 48 | void
|
---|
| 49 | Ray::CorrectZeroComponents()
|
---|
| 50 | {
|
---|
| 51 | const float eps = 1e-6;
|
---|
| 52 |
|
---|
| 53 | // it does change the ray direction very slightly,
|
---|
| 54 | // but the size direction vector is not practically changed
|
---|
| 55 |
|
---|
| 56 | if (fabs(dir.x) < eps) {
|
---|
| 57 | if (dir.x < 0.0)
|
---|
| 58 | dir.x = -eps;
|
---|
| 59 | else
|
---|
| 60 | dir.x = eps;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | if (fabs(dir.y) < eps) {
|
---|
| 64 | if (dir.y < 0.0)
|
---|
| 65 | dir.y = -eps;
|
---|
| 66 | else
|
---|
| 67 | dir.y = eps;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | if (fabs(dir.z) < eps) {
|
---|
| 71 | if (dir.z < 0.0)
|
---|
| 72 | dir.z = -eps;
|
---|
| 73 | else
|
---|
| 74 | dir.z = eps;
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 |
|
---|
| 79 | void
|
---|
| 80 | Ray::ComputeInvertedDir() const
|
---|
| 81 | {
|
---|
| 82 | if ( (invDir.x != 0.0) ||
|
---|
| 83 | (invDir.y != 0.0) ||
|
---|
| 84 | (invDir.z != 0.0) )
|
---|
| 85 | return; // has been already precomputed
|
---|
| 86 |
|
---|
| 87 | const float eps = 1e-6;
|
---|
| 88 | const float invEps = 1e6;
|
---|
| 89 |
|
---|
| 90 | // it does change the ray direction very slightly,
|
---|
| 91 | // but the size direction vector is not practically changed
|
---|
| 92 |
|
---|
| 93 | if (fabs(dir.x) < eps) {
|
---|
| 94 | if (dir.x < 0.0)
|
---|
| 95 | invDir.x = -invEps;
|
---|
| 96 | else
|
---|
| 97 | invDir.x = invEps;
|
---|
| 98 | }
|
---|
| 99 | else
|
---|
| 100 | invDir.x = 1.0 / dir.x;
|
---|
| 101 |
|
---|
| 102 | if (fabs(dir.y) < eps) {
|
---|
| 103 | if (dir.y < 0.0)
|
---|
| 104 | invDir.y = -invEps;
|
---|
| 105 | else
|
---|
| 106 | invDir.y = invEps;
|
---|
| 107 | }
|
---|
| 108 | else
|
---|
| 109 | invDir.y = 1.0 / dir.y;
|
---|
| 110 |
|
---|
| 111 | if (fabs(dir.z) < eps) {
|
---|
| 112 | if (dir.z < 0.0)
|
---|
| 113 | invDir.z = -invEps;
|
---|
| 114 | else
|
---|
| 115 | invDir.z = invEps;
|
---|
| 116 | }
|
---|
| 117 | else
|
---|
| 118 | invDir.z = 1.0 / dir.z;
|
---|
| 119 |
|
---|
| 120 | return;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[191] | 123 | void
|
---|
| 124 | PassingRaySet::Reset()
|
---|
| 125 | {
|
---|
| 126 | for (int i=0; i < 3*Resolution*Resolution; i++)
|
---|
| 127 | mDirectionalContributions[i] = 0;
|
---|
| 128 | mRays = 0;
|
---|
| 129 | mContributions = 0;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | void
|
---|
| 133 | PassingRaySet::AddRay(const Ray &ray, const int contributions)
|
---|
| 134 | {
|
---|
| 135 | int i = GetEntryIndex(ray.GetDir());
|
---|
| 136 | mRays++;
|
---|
| 137 | mContributions += contributions;
|
---|
| 138 | mDirectionalContributions[i] += contributions;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | int
|
---|
| 142 | PassingRaySet::GetEntryIndex(const Vector3 &direction) const
|
---|
| 143 | {
|
---|
| 144 | // get face
|
---|
| 145 | int axis = direction.DrivingAxis();
|
---|
| 146 | Vector3 dir;
|
---|
| 147 | float k = direction[axis];
|
---|
| 148 | if ( k < 0.0f)
|
---|
| 149 | k = -k;
|
---|
| 150 |
|
---|
| 151 | dir = direction/k;
|
---|
| 152 | float x, y;
|
---|
| 153 | dir.ExtractVerts(&x, &y, axis);
|
---|
| 154 | int ix = (x + 1.0f)*0.5f*Resolution;
|
---|
| 155 | int iy = (y + 1.0f)*0.5f*Resolution;
|
---|
| 156 |
|
---|
| 157 | return Resolution*(Resolution*axis + iy) + ix;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[349] | 160 | int Ray::ClassifyPlane(const Plane3 &plane,
|
---|
| 161 | const float minT,
|
---|
| 162 | const float maxT) const
|
---|
[327] | 163 | {
|
---|
[349] | 164 | const Vector3 entp = Extrap(minT);
|
---|
| 165 | const Vector3 extp = Extrap(maxT);
|
---|
[328] | 166 |
|
---|
[349] | 167 | const int entSide = plane.Side(entp);
|
---|
| 168 | const int extSide = plane.Side(extp);
|
---|
[328] | 169 |
|
---|
[362] | 170 | if (entSide < 0)
|
---|
[349] | 171 | {
|
---|
[362] | 172 | if (extSide >= 0)
|
---|
| 173 | {
|
---|
| 174 | return BACK_FRONT;
|
---|
| 175 | }
|
---|
[349] | 176 | return BACK;
|
---|
[328] | 177 | }
|
---|
[362] | 178 | else if (entSide > 0)
|
---|
[328] | 179 | {
|
---|
[362] | 180 | if (extSide <= 0)
|
---|
| 181 | return FRONT_BACK;
|
---|
| 182 |
|
---|
[349] | 183 | return FRONT;
|
---|
[328] | 184 | }
|
---|
[362] | 185 | else if (entSide == 0)
|
---|
| 186 | {
|
---|
| 187 | if (extSide > 0)
|
---|
| 188 | return BACK_FRONT;
|
---|
| 189 | else if (extSide < 0)
|
---|
| 190 | return FRONT_BACK;
|
---|
| 191 | }
|
---|
[327] | 192 |
|
---|
[362] | 193 | return COINCIDENT;
|
---|
[327] | 194 | }
|
---|
| 195 |
|
---|
[366] | 196 |
|
---|
[191] | 197 | ostream &
|
---|
| 198 | operator<<(ostream &s, const PassingRaySet &set)
|
---|
| 199 | {
|
---|
| 200 | s<<"Ray Set #rays="<<set.mRays<<" #contributions="<<set.mContributions<<endl;
|
---|
| 201 | for (int i=0; i < 3*sqr(PassingRaySet::Resolution); i++)
|
---|
| 202 | s<<set.mDirectionalContributions[i]<<" ";
|
---|
| 203 | s<<endl;
|
---|
| 204 | return s;
|
---|
[366] | 205 | } |
---|