[1520] | 1 | #ifndef _RayCaster_H__
|
---|
| 2 | #define _RayCaster_H__
|
---|
| 3 |
|
---|
[2583] | 4 |
|
---|
[1520] | 5 | #include "Containers.h"
|
---|
| 6 | #include <string>
|
---|
[2583] | 7 | #include <iostream>
|
---|
[1528] | 8 | #include "Vector3.h"
|
---|
[2014] | 9 | #include "VssRay.h"
|
---|
[2729] | 10 | #include "Timer/PerfTimer.h"
|
---|
[1528] | 11 |
|
---|
[2176] | 12 | //
|
---|
[1520] | 13 |
|
---|
| 14 |
|
---|
| 15 | namespace GtpVisibilityPreprocessor {
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 | class Intersectable;
|
---|
| 19 | class VssRay;
|
---|
| 20 | class SimpleRayContainer;
|
---|
| 21 | class AxisAlignedBox3;
|
---|
| 22 | struct VssRayContainer;
|
---|
| 23 | class Preprocessor;
|
---|
[1528] | 24 | struct SimpleRay;
|
---|
[2575] | 25 | class RayPacket2x2;
|
---|
[1520] | 26 |
|
---|
| 27 | /** This class provides an interface for ray casting.
|
---|
| 28 | */
|
---|
| 29 | class RayCaster
|
---|
| 30 | {
|
---|
[1528] | 31 |
|
---|
[1520] | 32 | public:
|
---|
| 33 |
|
---|
[2575] | 34 | enum {
|
---|
| 35 | INTERNAL_RAYCASTER = 0,
|
---|
| 36 | INTEL_RAYCASTER = 1,
|
---|
[2629] | 37 | HAVRAN_RAYCASTER = 2,
|
---|
| 38 | HAVRAN_DYN_RAYCASTER = 3
|
---|
[2575] | 39 | };
|
---|
[1520] | 40 |
|
---|
[2575] | 41 | RayCaster(const Preprocessor &preprocessor);
|
---|
[1520] | 42 |
|
---|
[2575] | 43 | virtual ~RayCaster();
|
---|
| 44 |
|
---|
| 45 | virtual int Type() const = 0;
|
---|
[1520] | 46 |
|
---|
[2575] | 47 | /** Wrapper for casting single ray.
|
---|
| 48 | @returns ray or NULL if invalid
|
---|
| 49 | */
|
---|
| 50 | VssRay *CastRay(const SimpleRay &simpleRay,
|
---|
| 51 | const AxisAlignedBox3 &box,
|
---|
| 52 | const bool castDoubleRay);
|
---|
[1520] | 53 |
|
---|
[2575] | 54 | virtual int CastRay(const SimpleRay &simpleRay,
|
---|
| 55 | VssRayContainer &vssRays,
|
---|
| 56 | const AxisAlignedBox3 &box,
|
---|
| 57 | const bool castDoubleRay,
|
---|
| 58 | const bool pruneInvalidRays = true) = 0;
|
---|
[1521] | 59 |
|
---|
[2575] | 60 | virtual void CastRays16(SimpleRayContainer &rays,
|
---|
| 61 | VssRayContainer &vssRays,
|
---|
| 62 | const AxisAlignedBox3 &sbox,
|
---|
| 63 | const bool castDoubleRay,
|
---|
| 64 | const bool pruneInvalidRays = true) = 0;
|
---|
| 65 |
|
---|
[2076] | 66 | virtual void CastRays(
|
---|
[2575] | 67 | SimpleRayContainer &rays,
|
---|
| 68 | VssRayContainer &vssRays,
|
---|
| 69 | const AxisAlignedBox3 &sbox,
|
---|
| 70 | const bool castDoubleRay,
|
---|
| 71 | const bool pruneInvalidRays = true);
|
---|
[2077] | 72 |
|
---|
[2629] | 73 | virtual void
|
---|
| 74 | CastSimpleForwardRays(SimpleRayContainer &rays,
|
---|
| 75 | const AxisAlignedBox3 &sbox
|
---|
| 76 | ) { return;}
|
---|
| 77 |
|
---|
[2582] | 78 | // Using packet of 4 rays supposing that these are coherent
|
---|
| 79 | virtual void CastRaysPacket4(Vector3 origin4[],
|
---|
| 80 | Vector3 direction4[],
|
---|
| 81 | int result4[],
|
---|
| 82 | float dist4[]) { }
|
---|
| 83 |
|
---|
| 84 | // Using packet of 4 rays supposing that these are coherent
|
---|
| 85 | // We give a box to which each ray is clipped to before the
|
---|
| 86 | // ray shooting is computed !
|
---|
| 87 | virtual void CastRaysPacket4(const Vector3 &minBox,
|
---|
[2629] | 88 | const Vector3 &maxBox,
|
---|
| 89 | const Vector3 origin4[],
|
---|
| 90 | const Vector3 direction4[],
|
---|
| 91 | int result4[],
|
---|
| 92 | float dist4[]) { }
|
---|
[2582] | 93 |
|
---|
[2575] | 94 | // Just for testing concept
|
---|
| 95 | virtual void CastRaysPacket2x2(RayPacket2x2 &raysPack,
|
---|
| 96 | bool castDoubleRay,
|
---|
| 97 | const bool pruneInvalidRays = true)
|
---|
| 98 | { }
|
---|
[2629] | 99 |
|
---|
| 100 | virtual void AddDynamicObjecs(const ObjectContainer &objects, const Matrix4x4 &m)
|
---|
| 101 | { }
|
---|
| 102 |
|
---|
| 103 | virtual void UpdateDynamicObjects(const Matrix4x4 &m)
|
---|
| 104 | { }
|
---|
| 105 |
|
---|
| 106 | virtual void DeleteDynamicObjects()
|
---|
| 107 | { }
|
---|
| 108 |
|
---|
[2742] | 109 | void SheduleRayForDeletion(VssRay *ray);
|
---|
| 110 |
|
---|
[2077] | 111 | /*virtual void CastRaysEye4(SimpleRayContainer &rays,
|
---|
[2575] | 112 | VssRayContainer &vssRays,
|
---|
| 113 | const AxisAlignedBox3 &sbox,
|
---|
| 114 | const bool castDoubleRay,
|
---|
| 115 | const bool pruneInvalidRays = true) = 0;
|
---|
| 116 | */
|
---|
[1974] | 117 |
|
---|
[2629] | 118 | // This sorts only rays by origin
|
---|
[2575] | 119 | virtual void
|
---|
| 120 | SortRays(SimpleRayContainer &rays);
|
---|
[1974] | 121 |
|
---|
[2629] | 122 | // This sorts the ray by origin and direction
|
---|
| 123 | virtual void
|
---|
| 124 | SortRays2(SimpleRayContainer &rays);
|
---|
| 125 |
|
---|
[2729] | 126 | VssRay *RequestRay(const Vector3 &origin,
|
---|
| 127 | const Vector3 &termination,
|
---|
| 128 | Intersectable *originObject,
|
---|
| 129 | Intersectable *terminationObject,
|
---|
| 130 | const int pass,
|
---|
| 131 | const float pdf);
|
---|
| 132 |
|
---|
[2575] | 133 | // pool of vss rays to be used in one pass of the sampling
|
---|
| 134 | struct VssRayPool
|
---|
| 135 | {
|
---|
| 136 | VssRayPool(): mRays(NULL), mIndex(0), mNumber(0)
|
---|
| 137 | {}
|
---|
| 138 |
|
---|
| 139 | ~VssRayPool()
|
---|
| 140 | {
|
---|
| 141 | delete []mRays;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | void Reserve(const int number)
|
---|
| 145 | {
|
---|
| 146 | DEL_PTR(mRays);
|
---|
| 147 | mRays = new VssRay[number];
|
---|
| 148 | mNumber = number;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | void Clear()
|
---|
| 152 | {
|
---|
| 153 | mIndex = 0;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | VssRay *Alloc()
|
---|
| 157 | {
|
---|
[2729] | 158 | #if 1
|
---|
| 159 | // reset pool
|
---|
| 160 | if (mIndex == mNumber)
|
---|
| 161 | {
|
---|
| 162 | std::cerr << "warning: ray pool too small! " << std::endl;
|
---|
| 163 | mIndex = 0;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | // raypool larger index => enlarge ray pool
|
---|
| 167 | #else
|
---|
| 168 | if (mNumber == mIndex)
|
---|
| 169 | {
|
---|
| 170 | cerr << "warning: ray pool too small! " << "reserving " << mNumber * 2 << " rays" << std::endl;
|
---|
| 171 | Reserve(mNumber * 2);
|
---|
| 172 | }
|
---|
| 173 | #endif
|
---|
| 174 | return mRays + mIndex ++;
|
---|
[2575] | 175 | }
|
---|
| 176 | protected:
|
---|
[2729] | 177 | VssRay *mRays;
|
---|
| 178 | int mIndex;
|
---|
| 179 | int mNumber;
|
---|
[2575] | 180 | };
|
---|
[2543] | 181 |
|
---|
| 182 |
|
---|
[2575] | 183 | VssRayPool mVssRayPool;
|
---|
| 184 |
|
---|
| 185 | void ReserveVssRayPool(const int n)
|
---|
| 186 | {
|
---|
| 187 | mVssRayPool.Reserve(n);
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | void InitPass()
|
---|
| 191 | {
|
---|
| 192 | mVssRayPool.Clear();
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[2729] | 195 | PerfTimer rawCastTimer;
|
---|
| 196 |
|
---|
[1520] | 197 | protected:
|
---|
[2575] | 198 |
|
---|
| 199 |
|
---|
| 200 | void _SortRays(SimpleRayContainer &rays,
|
---|
| 201 | const int l,
|
---|
| 202 | const int r,
|
---|
| 203 | const int depth,
|
---|
[2660] | 204 | float *box);
|
---|
[2629] | 205 |
|
---|
| 206 | void _SortRays2(SimpleRayContainer &rays,
|
---|
| 207 | const int l,
|
---|
| 208 | const int r,
|
---|
| 209 | const int depth,
|
---|
| 210 | float box[12]);
|
---|
[2575] | 211 |
|
---|
| 212 | struct Intersection
|
---|
| 213 | {
|
---|
| 214 | Intersection(): mObject(NULL), mFaceId(0)
|
---|
| 215 | {}
|
---|
| 216 |
|
---|
| 217 | Intersection(const Vector3 &p, const Vector3 &n, Intersectable *o, const int f):
|
---|
| 218 | mPoint(p), mNormal(n), mObject(o), mFaceId(f)
|
---|
| 219 | {}
|
---|
| 220 |
|
---|
| 221 | Intersection(const Vector3 &p): mPoint(p), mObject(NULL), mFaceId(0)
|
---|
| 222 | {}
|
---|
| 223 |
|
---|
| 224 |
|
---|
| 225 | ////////////
|
---|
| 226 |
|
---|
| 227 | Vector3 mPoint;
|
---|
| 228 | Vector3 mNormal;
|
---|
| 229 | Intersectable *mObject;
|
---|
| 230 | int mFaceId;
|
---|
| 231 | };
|
---|
[2187] | 232 |
|
---|
| 233 |
|
---|
[2575] | 234 | int ProcessRay(const SimpleRay &ray,
|
---|
| 235 | Intersection &hitA,
|
---|
| 236 | Intersection &hitB,
|
---|
| 237 | VssRayContainer &vssRays,
|
---|
| 238 | const AxisAlignedBox3 &box,
|
---|
| 239 | const bool castDoubleRay,
|
---|
| 240 | const bool pruneInvalidRays = true);
|
---|
| 241 |
|
---|
| 242 | /** Checks if ray is valid.
|
---|
| 243 | I.e., the ray is in valid view space.
|
---|
| 244 | @note: clamps the ray to valid view space.
|
---|
| 245 | */
|
---|
| 246 | bool ValidateRay(const Vector3 &origin,
|
---|
| 247 | const Vector3 &direction,
|
---|
| 248 | const AxisAlignedBox3 &box,
|
---|
| 249 | Intersection &hit);
|
---|
| 250 |
|
---|
| 251 | bool ClipToViewSpaceBox(const Vector3 &origin,
|
---|
| 252 | const Vector3 &termination,
|
---|
| 253 | Vector3 &clippedOrigin,
|
---|
| 254 | Vector3 &clippedTermination);
|
---|
| 255 |
|
---|
| 256 | const Preprocessor &mPreprocessor;
|
---|
| 257 | #if 1
|
---|
| 258 | public:
|
---|
| 259 | // Added by VH for debugging
|
---|
| 260 | Intersection intersect;
|
---|
| 261 | #endif
|
---|
[1520] | 262 | };
|
---|
| 263 |
|
---|
| 264 |
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | #endif
|
---|