[372] | 1 | #include "VssRay.h"
|
---|
| 2 | #include "AxisAlignedBox3.h"
|
---|
| 3 |
|
---|
[863] | 4 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 5 |
|
---|
[372] | 6 | // Static variables
|
---|
| 7 | int
|
---|
| 8 | VssRay::mailID = 0;
|
---|
| 9 |
|
---|
| 10 |
|
---|
| 11 | bool
|
---|
| 12 | VssRay::ComputeMinMaxT(const AxisAlignedBox3 &box,
|
---|
[438] | 13 | float &tmin,
|
---|
| 14 | float &tmax) const
|
---|
[372] | 15 | {
|
---|
| 16 |
|
---|
| 17 | float minx, maxx, miny, maxy, minz, maxz;
|
---|
| 18 | Vector3 dir = GetDir();
|
---|
| 19 |
|
---|
| 20 | if (fabs(dir.x) < 0.001) {
|
---|
| 21 | if (box.Min().x < GetOrigin().x && box.Max().x > GetOrigin().x) {
|
---|
| 22 | minx = -MAXFLOAT;
|
---|
| 23 | maxx = MAXFLOAT;
|
---|
| 24 | }
|
---|
| 25 | else
|
---|
| 26 | return false;
|
---|
| 27 | }
|
---|
| 28 | else {
|
---|
| 29 | float t1 = (box.Min().x - GetOrigin().x) / dir.x;
|
---|
| 30 | float t2 = (box.Max().x - GetOrigin().x) / dir.x;
|
---|
| 31 | if (t1 < t2) {
|
---|
| 32 | minx = t1;
|
---|
| 33 | maxx = t2;
|
---|
| 34 | }
|
---|
| 35 | else {
|
---|
| 36 | minx = t2;
|
---|
| 37 | maxx = t1;
|
---|
| 38 | }
|
---|
| 39 | if (maxx < 0)
|
---|
| 40 | return false;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | if (fabs(dir.y) < 0.001) {
|
---|
| 44 | if (box.Min().y < GetOrigin().y && box.Max().y > GetOrigin().y) {
|
---|
| 45 | miny = -MAXFLOAT;
|
---|
| 46 | maxy = MAXFLOAT;
|
---|
| 47 | }
|
---|
| 48 | else
|
---|
| 49 | return false;
|
---|
| 50 | }
|
---|
| 51 | else {
|
---|
| 52 | float t1 = (box.Min().y - GetOrigin().y) / dir.y;
|
---|
| 53 | float t2 = (box.Max().y - GetOrigin().y) / dir.y;
|
---|
| 54 | if (t1 < t2) {
|
---|
| 55 | miny = t1;
|
---|
| 56 | maxy = t2;
|
---|
| 57 | }
|
---|
| 58 | else {
|
---|
| 59 | miny = t2;
|
---|
| 60 | maxy = t1;
|
---|
| 61 | }
|
---|
| 62 | if (maxy < 0.0)
|
---|
| 63 | return false;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | if (fabs(dir.z) < 0.001) {
|
---|
| 67 | if (box.Min().z < GetOrigin().z && box.Max().z > GetOrigin().z) {
|
---|
| 68 | minz = -MAXFLOAT;
|
---|
| 69 | maxz = MAXFLOAT;
|
---|
| 70 | }
|
---|
| 71 | else
|
---|
| 72 | return false;
|
---|
| 73 | }
|
---|
| 74 | else {
|
---|
| 75 | float t1 = (box.Min().z - GetOrigin().z) / dir.z;
|
---|
| 76 | float t2 = (box.Max().z - GetOrigin().z) / dir.z;
|
---|
| 77 | if (t1 < t2) {
|
---|
| 78 | minz = t1;
|
---|
| 79 | maxz = t2;
|
---|
| 80 | }
|
---|
| 81 | else {
|
---|
| 82 | minz = t2;
|
---|
| 83 | maxz = t1;
|
---|
| 84 | }
|
---|
| 85 | if (maxz < 0.0)
|
---|
| 86 | return false;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | tmin = minx;
|
---|
| 90 | if (miny > tmin)
|
---|
| 91 | tmin = miny;
|
---|
| 92 | if (minz > tmin)
|
---|
| 93 | tmin = minz;
|
---|
| 94 |
|
---|
| 95 | tmax = maxx;
|
---|
| 96 | if (maxy < tmax)
|
---|
| 97 | tmax = maxy;
|
---|
| 98 | if (maxz < tmax)
|
---|
| 99 | tmax = maxz;
|
---|
| 100 |
|
---|
| 101 | return 1; // yes, intersection was found
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 |
|
---|
| 105 |
|
---|
| 106 | bool
|
---|
| 107 | VssRay::Intersects(const AxisAlignedBox3 &box,
|
---|
[863] | 108 | float &tmin,
|
---|
| 109 | float &tmax) const
|
---|
[372] | 110 | {
|
---|
| 111 | if (!ComputeMinMaxT(box, tmin, tmax))
|
---|
| 112 | return false;
|
---|
| 113 |
|
---|
| 114 | if ( tmax < tmin)
|
---|
| 115 | return false; // the ray passes outside the box
|
---|
| 116 |
|
---|
| 117 | if ( tmax < 0.0f)
|
---|
| 118 | return false; // the intersection is not on the positive halfline
|
---|
| 119 |
|
---|
| 120 | return true; // ray hits the box .. origin can be outside or inside the box
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 |
|
---|
| 124 | bool
|
---|
| 125 | VssRay::IntersectsSphere(const Vector3 ¢er,
|
---|
[863] | 126 | const float sqrRadius,
|
---|
| 127 | Vector3 &point,
|
---|
| 128 | float &t) const
|
---|
[372] | 129 | {
|
---|
| 130 | // compute ray/plane intersection
|
---|
| 131 | Vector3 dir = GetDir();
|
---|
| 132 |
|
---|
| 133 | t = -DotProd(GetOrigin() - center, dir)*sqr(GetInvSize());
|
---|
| 134 |
|
---|
| 135 | if (t < 0.0f)
|
---|
| 136 | t = 0.0f;
|
---|
| 137 | else
|
---|
| 138 | if (t > 1.0f)
|
---|
| 139 | t = 1.0f;
|
---|
| 140 |
|
---|
| 141 | point = GetOrigin() + t*dir;
|
---|
| 142 |
|
---|
[863] | 143 | return GtpVisibilityPreprocessor::SqrDistance(point, center) < sqrRadius;
|
---|
[372] | 144 | }
|
---|
[382] | 145 |
|
---|
| 146 |
|
---|
| 147 | float
|
---|
[438] | 148 | VssRay::GetDirParam(const int axis, const Vector3 dir)
|
---|
| 149 | {
|
---|
| 150 | return (axis == 0) ? atan2(dir.x, dir.z) : asin(dir.y);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | float
|
---|
[382] | 154 | VssRay::GetDirParametrization(const int axis) const
|
---|
| 155 | {
|
---|
[438] | 156 | Vector3 dir = GetNormalizedDir();
|
---|
| 157 | return GetDirParam(axis, dir);
|
---|
[382] | 158 | }
|
---|
[446] | 159 |
|
---|
[492] | 160 | float
|
---|
| 161 | VssRay::GetOpositeDirParametrization(const int axis) const
|
---|
| 162 | {
|
---|
| 163 | Vector3 dir = -GetNormalizedDir();
|
---|
| 164 | return GetDirParam(axis, dir);
|
---|
| 165 | }
|
---|
[446] | 166 |
|
---|
[492] | 167 |
|
---|
[446] | 168 | void
|
---|
[464] | 169 | GenerateExtendedConvexCombinationWeights2(float &w1,
|
---|
| 170 | float &w2,
|
---|
| 171 | const float overlap
|
---|
| 172 | )
|
---|
| 173 | {
|
---|
| 174 | w1 = RandomValue(-overlap, 1.0f + overlap);
|
---|
| 175 | w2 = 1.0f - w1;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | void
|
---|
[446] | 179 | GenerateExtendedConvexCombinationWeights(float &w1,
|
---|
| 180 | float &w2,
|
---|
| 181 | float &w3,
|
---|
| 182 | const float overlap
|
---|
| 183 | )
|
---|
| 184 | {
|
---|
[464] | 185 |
|
---|
| 186 | while (1) {
|
---|
| 187 | w1 = RandomValue(-overlap, 1.0f + overlap);
|
---|
| 188 | w2 = RandomValue(-overlap, 1.0f + overlap);
|
---|
| 189 | w3 = 1.0f + overlap - w1 - w2;
|
---|
| 190 | if (w3 >= -overlap && w3 <= 1.0f + overlap)
|
---|
| 191 | break;
|
---|
| 192 | /*
|
---|
| 193 | w3 = RandomValue(0.0f, 1.0f + overlap);
|
---|
| 194 |
|
---|
| 195 | float sum = w1 + w2 + w3;
|
---|
| 196 | if (sum>0.0f) {
|
---|
| 197 | w1/=sum;
|
---|
| 198 | w2/=sum;
|
---|
| 199 | w3/=sum;
|
---|
| 200 | break;
|
---|
| 201 | }
|
---|
| 202 | */
|
---|
| 203 | }
|
---|
[446] | 204 | }
|
---|
[467] | 205 |
|
---|
| 206 |
|
---|
| 207 | void
|
---|
| 208 | VssRayContainer::PrintStatistics(ostream &s)
|
---|
| 209 | {
|
---|
| 210 |
|
---|
| 211 | VssRayContainer::const_iterator it = begin(), it_end = end();
|
---|
| 212 | int sumContributions = 0;
|
---|
| 213 | float sumRelContributions = 0.0f;
|
---|
| 214 |
|
---|
| 215 | for (; it != it_end; ++it) {
|
---|
| 216 | VssRay *ray = *it;
|
---|
| 217 | sumContributions += ray->mPvsContribution;
|
---|
| 218 | sumRelContributions += ray->mRelativePvsContribution;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | s<<"##### VSS RAY STAT ##########\n";
|
---|
[487] | 222 | s<<"#RAYS\n"<<(int)size()<<endl;
|
---|
[467] | 223 | s<<"#AVG_RAY_PVS_CONTRIBUTION\n"<<sumContributions/(float)size()<<endl;
|
---|
| 224 | s<<"#AVG_RAY_RELATIVE_PVS_CONTRIBUTION\n"<<sumRelContributions/
|
---|
| 225 | (float)size()<<endl;
|
---|
| 226 |
|
---|
| 227 | }
|
---|
[492] | 228 |
|
---|
| 229 |
|
---|
| 230 | int
|
---|
| 231 | VssRayContainer::SelectRays(const int number,
|
---|
| 232 | VssRayContainer &selected) const
|
---|
| 233 | {
|
---|
| 234 | float p = number/(float)size();
|
---|
| 235 | VssRayContainer::const_iterator it = begin();
|
---|
| 236 |
|
---|
| 237 | for (; it != end(); it++)
|
---|
| 238 | if (Random(1.0f) < p)
|
---|
| 239 | selected.push_back(*it);
|
---|
| 240 |
|
---|
[863] | 241 | return (int)selected.size();
|
---|
[492] | 242 | }
|
---|
| 243 |
|
---|
| 244 |
|
---|
| 245 | int
|
---|
| 246 | VssRayContainer::GetContributingRays(VssRayContainer &selected,
|
---|
| 247 | const int minPass
|
---|
| 248 | ) const
|
---|
| 249 | {
|
---|
| 250 | VssRayContainer::const_iterator it = begin();
|
---|
| 251 |
|
---|
| 252 | for (; it != end(); it++) {
|
---|
| 253 | VssRay *ray = *it;
|
---|
| 254 | if (ray->mPass >= minPass && ray->mPvsContribution > 0)
|
---|
| 255 | selected.push_back(ray);
|
---|
| 256 | }
|
---|
[863] | 257 | return (int)selected.size();
|
---|
[492] | 258 | }
|
---|
| 259 |
|
---|
[860] | 260 | } |
---|