[376] | 1 | #ifndef __VSS_RAY_H
|
---|
| 2 | #define __VSS_RAY_H
|
---|
[372] | 3 |
|
---|
| 4 | #include <vector>
|
---|
[2116] | 5 |
|
---|
[372] | 6 | #include "Vector3.h"
|
---|
[492] | 7 | #include "Containers.h"
|
---|
[372] | 8 |
|
---|
[2342] | 9 | #ifdef USE_SSE
|
---|
| 10 | #include <xmmintrin.h>
|
---|
| 11 | #endif
|
---|
[2116] | 12 |
|
---|
| 13 |
|
---|
[860] | 14 | namespace GtpVisibilityPreprocessor {
|
---|
| 15 |
|
---|
[372] | 16 | class AxisAlignedBox3;
|
---|
| 17 | class Intersectable;
|
---|
[1133] | 18 | class KdNode;
|
---|
[2116] | 19 | class Ray;
|
---|
[372] | 20 |
|
---|
[1900] | 21 | #define ABS_CONTRIBUTION_WEIGHT 0.0f
|
---|
[1966] | 22 | #define VSS_STORE_VIEWCELLS 1
|
---|
[2342] | 23 |
|
---|
| 24 |
|
---|
| 25 |
|
---|
[372] | 26 | class VssRay {
|
---|
| 27 | public:
|
---|
[1528] | 28 |
|
---|
| 29 | // various flags
|
---|
[372] | 30 | enum {
|
---|
[1566] | 31 | FPosDirX = 1, // the direction of ray in X-axis is positive
|
---|
| 32 | FPosDirY = 2, // the direction of ray in Y-axis is positive
|
---|
| 33 | FPosDirZ = 4, // the direction of ray in Z-axis is positive
|
---|
| 34 | BorderSample = 8,// if this ray is an adaptive border ray
|
---|
[1771] | 35 | ReverseSample = 16, // if this ray is a reverse sample
|
---|
| 36 | Valid = 32 // this ray is a valid ray
|
---|
| 37 | //(with respect to detect empty viewspace)
|
---|
[372] | 38 | };
|
---|
| 39 |
|
---|
[1867] | 40 | // Id of the generating SimpleRay
|
---|
[1989] | 41 | int mGeneratorId;
|
---|
[1867] | 42 |
|
---|
[438] | 43 | static int mailID;
|
---|
[372] | 44 | int mMailbox;
|
---|
| 45 |
|
---|
[438] | 46 | // side of the ray - used for the ray classification
|
---|
| 47 | // char mSide;
|
---|
[372] | 48 |
|
---|
[438] | 49 | // computed t
|
---|
[463] | 50 | // float mT;
|
---|
[372] | 51 |
|
---|
[438] | 52 | // inverse of the ray size
|
---|
| 53 | float mInvSize;
|
---|
| 54 |
|
---|
| 55 | // counter of references to this ray
|
---|
[372] | 56 | short mRefCount;
|
---|
[438] | 57 |
|
---|
| 58 | // various flags
|
---|
[372] | 59 | char mFlags;
|
---|
| 60 |
|
---|
| 61 | Vector3 mOrigin;
|
---|
| 62 | Vector3 mTermination;
|
---|
| 63 |
|
---|
[438] | 64 | /// Termination object for the ray
|
---|
| 65 | /// only the termination object is actually used
|
---|
| 66 | Intersectable *mOriginObject;
|
---|
| 67 | Intersectable *mTerminationObject;
|
---|
[464] | 68 |
|
---|
[1966] | 69 | #if VSS_STORE_VIEWCELLS
|
---|
[492] | 70 | ViewCellContainer mViewCells;
|
---|
[1966] | 71 | #endif
|
---|
[1133] | 72 |
|
---|
[464] | 73 | ////////////////////////
|
---|
| 74 | // members related to importance sampling
|
---|
| 75 | // sampling pass in which this ray was generated
|
---|
| 76 | short mPass;
|
---|
| 77 |
|
---|
[1883] | 78 | // Distribution used to generate this ray
|
---|
| 79 | short mDistribution;
|
---|
| 80 |
|
---|
[464] | 81 | // number of cells where this ray made a contribution to the PVS
|
---|
[1883] | 82 | int mPvsContribution;
|
---|
[464] | 83 |
|
---|
| 84 | // sum of relative ray contributions per object
|
---|
| 85 | float mRelativePvsContribution;
|
---|
[492] | 86 |
|
---|
| 87 | // weighted contribution to the pvs (based on the pass the ray was casted at)
|
---|
| 88 | // computed by the prperocessor
|
---|
| 89 | float mWeightedPvsContribution;
|
---|
[534] | 90 |
|
---|
| 91 | // probability of this ray
|
---|
[556] | 92 | float mPdf;
|
---|
[464] | 93 |
|
---|
[438] | 94 | //////////////////////////////
|
---|
[1133] | 95 |
|
---|
| 96 |
|
---|
| 97 | /// the kd node holding the termination point
|
---|
| 98 | KdNode *mTerminationNode;
|
---|
| 99 | /// the kd node holding the origin point
|
---|
| 100 | KdNode *mOriginNode;
|
---|
| 101 |
|
---|
[2014] | 102 | VssRay() {}
|
---|
| 103 |
|
---|
[464] | 104 | VssRay(
|
---|
| 105 | const Vector3 &origin,
|
---|
[438] | 106 | const Vector3 &termination,
|
---|
| 107 | Intersectable *originObject,
|
---|
[464] | 108 | Intersectable *terminationObject,
|
---|
[537] | 109 | const int pass = 0,
|
---|
[556] | 110 | const float pdf = 1.0f
|
---|
[1528] | 111 | );
|
---|
[372] | 112 |
|
---|
[1528] | 113 | VssRay(const Ray &ray);
|
---|
[492] | 114 |
|
---|
| 115 |
|
---|
[1528] | 116 | void Precompute();
|
---|
[376] | 117 |
|
---|
[372] | 118 | void Mail() { mMailbox = mailID; }
|
---|
| 119 | static void NewMail() { mailID++; }
|
---|
| 120 | bool Mailed() const { return mMailbox == mailID; }
|
---|
| 121 |
|
---|
| 122 | bool Mailed(const int mail) {
|
---|
| 123 | return mMailbox >= mailID + mail;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[438] | 126 | int HitCount() const {
|
---|
[386] | 127 | #if BIDIRECTIONAL_RAY
|
---|
[438] | 128 | if (mOriginObject && mTerminationObject)
|
---|
| 129 | return 2;
|
---|
| 130 | if (mOriginObject || mTerminationObject)
|
---|
| 131 | return 1;
|
---|
| 132 | return 0;
|
---|
[386] | 133 | #else
|
---|
[438] | 134 | return (mTerminationObject) ? 1 : 0;
|
---|
[386] | 135 | #endif
|
---|
[438] | 136 | }
|
---|
[372] | 137 |
|
---|
[438] | 138 | Vector3 GetOrigin() const { return mOrigin; }
|
---|
[372] | 139 | Vector3 GetTermination() const { return mTermination; }
|
---|
| 140 | Vector3 GetDir() const { return mTermination - mOrigin; }
|
---|
| 141 | // Vector3 GetNormalizedDir() const { return Normalize(termination - mOrigin); }
|
---|
| 142 | Vector3 GetNormalizedDir() const { return (mTermination - mOrigin)*mInvSize; }
|
---|
| 143 |
|
---|
[448] | 144 | float Length() const { return Distance(mOrigin, mTermination); }
|
---|
| 145 |
|
---|
[438] | 146 | Vector3 Extrap(const float t) const {
|
---|
| 147 | return GetOrigin() + t * GetDir();
|
---|
| 148 | }
|
---|
[434] | 149 |
|
---|
[438] | 150 | float GetDirParametrization(const int axis) const;
|
---|
[492] | 151 | float GetOpositeDirParametrization(const int axis) const;
|
---|
[438] | 152 |
|
---|
| 153 | static float VssRay::GetDirParam(const int axis, const Vector3 dir);
|
---|
[1824] | 154 | static Vector3 VssRay::GetInvDirParam(const float alpha, const float beta);
|
---|
[438] | 155 |
|
---|
| 156 | float GetSize() const { return 1.0f/mInvSize; }
|
---|
| 157 | float GetInvSize() const { return mInvSize; }
|
---|
[372] | 158 | float GetOrigin(const int axis) const { return mOrigin[axis]; }
|
---|
| 159 | float GetTermination(const int axis) const { return mTermination[axis]; }
|
---|
| 160 | float GetDir(const int axis) const { return mTermination[axis] - mOrigin[axis]; }
|
---|
| 161 | float GetNormalizedDir(const int axis) const {
|
---|
| 162 | return (mTermination[axis] - mOrigin[axis])*mInvSize;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | bool
|
---|
| 166 | ComputeMinMaxT(const AxisAlignedBox3 &box,
|
---|
[438] | 167 | float &tmin,
|
---|
| 168 | float &tmax) const;
|
---|
[372] | 169 |
|
---|
| 170 | bool
|
---|
| 171 | Intersects(const AxisAlignedBox3 &box,
|
---|
[438] | 172 | float &tmin,
|
---|
| 173 | float &tmax) const;
|
---|
[372] | 174 |
|
---|
| 175 | bool
|
---|
| 176 | IntersectsSphere(const Vector3 ¢er,
|
---|
[438] | 177 | const float sqrRadius,
|
---|
| 178 | Vector3 &point,
|
---|
| 179 | float &t) const;
|
---|
[372] | 180 |
|
---|
| 181 | void
|
---|
| 182 | Translate(const Vector3 &translation) {
|
---|
| 183 | mOrigin += translation;
|
---|
| 184 | mTermination += translation;
|
---|
| 185 | }
|
---|
[427] | 186 |
|
---|
[438] | 187 | void SetupEndPoints(const Vector3 &origin,
|
---|
| 188 | const Vector3 &termination)
|
---|
| 189 | {
|
---|
| 190 | mOrigin = origin;
|
---|
| 191 | mTermination = termination;
|
---|
| 192 | Precompute();
|
---|
| 193 | }
|
---|
[427] | 194 |
|
---|
[2234] | 195 | inline bool HasPosDir(const int axis) const { return mFlags & (1<<axis); }
|
---|
[372] | 196 |
|
---|
[1112] | 197 | char Flags() const { return mFlags;} void SetFlags(char orFlag) { mFlags |= orFlag;}
|
---|
[372] | 198 |
|
---|
| 199 | bool IsActive() const { return mRefCount>0; }
|
---|
| 200 |
|
---|
| 201 | // reference counting for leaf nodes
|
---|
| 202 | int RefCount() const { return mRefCount; }
|
---|
| 203 | int Ref() { return mRefCount++; }
|
---|
| 204 |
|
---|
| 205 | void ScheduleForRemoval() { if (mRefCount>0) mRefCount = -mRefCount; }
|
---|
| 206 | bool ScheduledForRemoval() const { return mRefCount<0; }
|
---|
| 207 | void Unref() {
|
---|
| 208 | if (mRefCount > 0)
|
---|
| 209 | mRefCount--;
|
---|
| 210 | else
|
---|
| 211 | if (mRefCount < 0)
|
---|
[438] | 212 | mRefCount++;
|
---|
[372] | 213 | else {
|
---|
[2176] | 214 | std::cerr<<"Trying to unref already deleted ray!"<<std::endl;
|
---|
[438] | 215 | exit(1);
|
---|
[372] | 216 | }
|
---|
| 217 | }
|
---|
[434] | 218 |
|
---|
[438] | 219 | static Vector3
|
---|
| 220 | GetDirection(const float a, const float b) {
|
---|
[1824] | 221 | return GetInvDirParam(a, b);
|
---|
| 222 | //return Vector3(sin(a), sin(b), cos(a));
|
---|
[438] | 223 | }
|
---|
[434] | 224 |
|
---|
[492] | 225 | friend bool GreaterWeightedPvsContribution(const VssRay * a,
|
---|
| 226 | const VssRay *b) {
|
---|
| 227 | return a->mWeightedPvsContribution > b->mWeightedPvsContribution;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | float SqrDistance(const Vector3 &point) const {
|
---|
| 231 | Vector3 diff = point - mOrigin;
|
---|
| 232 | float t = DotProd(diff, GetDir());
|
---|
| 233 |
|
---|
| 234 | if ( t <= 0.0f ) {
|
---|
| 235 | t = 0.0f;
|
---|
| 236 | } else {
|
---|
| 237 | if (t >= 1.0f) {
|
---|
| 238 | t = 1.0f;
|
---|
| 239 | } else {
|
---|
| 240 | t /= SqrMagnitude(GetDir());
|
---|
| 241 | }
|
---|
| 242 | diff -= t*GetDir();
|
---|
| 243 | }
|
---|
| 244 | return SqrMagnitude(diff);
|
---|
| 245 | }
|
---|
[1259] | 246 |
|
---|
[1528] | 247 | /** Returns the data sampled on either the ray origin or termination.
|
---|
| 248 | */
|
---|
[1259] | 249 | void GetSampleData(
|
---|
| 250 | const bool isTerminaton,
|
---|
| 251 | Vector3 &pt,
|
---|
| 252 | Intersectable **obj,
|
---|
| 253 | KdNode **node) const;
|
---|
| 254 |
|
---|
[2176] | 255 | friend std::ostream& operator<< (std::ostream &s, const VssRay &vssRay);
|
---|
[1221] | 256 |
|
---|
[372] | 257 | };
|
---|
| 258 |
|
---|
[2233] | 259 |
|
---|
[1528] | 260 | inline void VssRay::GetSampleData(const bool isTermination,
|
---|
| 261 | Vector3 &pt,
|
---|
| 262 | Intersectable **obj,
|
---|
| 263 | KdNode **node) const
|
---|
| 264 | {
|
---|
| 265 | if (isTermination)
|
---|
| 266 | {
|
---|
| 267 | pt = mTermination;
|
---|
| 268 | *obj = mTerminationObject;
|
---|
| 269 | *node = mTerminationNode;
|
---|
| 270 | }
|
---|
| 271 | else
|
---|
| 272 | {
|
---|
| 273 | pt = mOrigin;
|
---|
| 274 | *obj = mOriginObject;
|
---|
| 275 | *node = mOriginNode;
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[2233] | 279 |
|
---|
[446] | 280 | void
|
---|
| 281 | GenerateExtendedConvexCombinationWeights(float &w1,
|
---|
| 282 | float &w2,
|
---|
| 283 | float &w3,
|
---|
| 284 | const float overlap
|
---|
| 285 | );
|
---|
[372] | 286 |
|
---|
[464] | 287 | void
|
---|
| 288 | GenerateExtendedConvexCombinationWeights2(float &w1,
|
---|
| 289 | float &w2,
|
---|
| 290 | const float overlap
|
---|
| 291 | );
|
---|
| 292 |
|
---|
[1221] | 293 | // Overload << operator for C++-style output
|
---|
[2176] | 294 | inline std::ostream&
|
---|
| 295 | operator<< (std::ostream &s, const VssRay &vssRay)
|
---|
[1221] | 296 | {
|
---|
| 297 | return s
|
---|
| 298 | << "(" << vssRay.mPass << ", " << vssRay.mOrigin << ", " << vssRay.mTermination
|
---|
| 299 | << ", " << vssRay.mOriginObject << ", " << vssRay.mTerminationObject << ", " << vssRay.mPdf << ")";
|
---|
| 300 | }
|
---|
| 301 |
|
---|
[446] | 302 | // --------------------------------------------------------------
|
---|
| 303 | // For sorting rays
|
---|
| 304 | // --------------------------------------------------------------
|
---|
[1528] | 305 | struct SortableEntry
|
---|
[446] | 306 | {
|
---|
| 307 | enum EType {
|
---|
| 308 | ERayMin,
|
---|
| 309 | ERayMax
|
---|
| 310 | };
|
---|
[372] | 311 |
|
---|
[446] | 312 | int type;
|
---|
| 313 | float value;
|
---|
| 314 | void *data;
|
---|
| 315 |
|
---|
| 316 | SortableEntry() {}
|
---|
| 317 | SortableEntry(const int t, const float v, void *d):type(t),
|
---|
| 318 | value(v),
|
---|
| 319 | data(d) {}
|
---|
| 320 |
|
---|
| 321 | friend bool operator<(const SortableEntry &a, const SortableEntry &b) {
|
---|
| 322 | return a.value < b.value;
|
---|
| 323 | }
|
---|
| 324 | };
|
---|
| 325 |
|
---|
[467] | 326 | struct VssRayContainer : public vector<VssRay *>
|
---|
| 327 | {
|
---|
[2176] | 328 | void PrintStatistics(std::ostream &s);
|
---|
[1112] | 329 | int SelectRays(const int number, VssRayContainer &selected, const bool copy=false) const;
|
---|
[2353] | 330 | int GetContributingRays(VssRayContainer &selected, const int minPass) const;
|
---|
[492] | 331 |
|
---|
[467] | 332 | };
|
---|
[372] | 333 |
|
---|
| 334 |
|
---|
[2353] | 335 | struct RayPacket
|
---|
| 336 | {
|
---|
| 337 | RayPacket(const VssRayContainer &rays)
|
---|
| 338 | {
|
---|
| 339 | for (int i = 0; i < 4; ++ i)
|
---|
| 340 | mOriginX[i] = rays[i]->mOrigin[0];
|
---|
| 341 |
|
---|
| 342 | for (int i = 0; i < 4; ++ i)
|
---|
| 343 | mOriginY[i] = rays[i]->mOrigin[1];
|
---|
| 344 |
|
---|
| 345 | for (int i = 0; i < 4; ++ i)
|
---|
| 346 | mOriginZ[i] = rays[i]->mOrigin[2];
|
---|
| 347 | }
|
---|
| 348 | #ifdef USE_SSE
|
---|
| 349 | union { float mOriginX[4]; __m128 mOriginX4; };
|
---|
| 350 | union { float mOriginY[4]; __m128 mOriginY4; };
|
---|
| 351 | union { float mOriginZ[4]; __m128 mOriginZ4; };
|
---|
| 352 |
|
---|
| 353 | union { float mTerminationX[4]; __m128 mTerminationX4; };
|
---|
| 354 | union { float mTerminationY[4]; __m128 mTerminationY4; };
|
---|
| 355 | union { float mTerminationZ[4]; __m128 mTerminationZ4; };
|
---|
| 356 | #else
|
---|
| 357 | float mOriginX[4];
|
---|
| 358 | float mOriginY[4];
|
---|
| 359 | float mOriginZ[4];
|
---|
| 360 |
|
---|
| 361 | float mTerminationX[4];
|
---|
| 362 | float mTerminationY[4];
|
---|
| 363 | float mTerminationZ[4];
|
---|
| 364 | #endif
|
---|
| 365 | ViewCellContainer mViewCells[4];
|
---|
[1715] | 366 | };
|
---|
| 367 |
|
---|
[2353] | 368 |
|
---|
[1715] | 369 | };
|
---|
| 370 |
|
---|
[372] | 371 | #endif
|
---|