1 /* 2 * Copyright 2011-2019 Branimir Karadzic. All rights reserved. 3 * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause 4 */ 5 6 #ifndef BOUNDS_H_HEADER_GUARD 7 #define BOUNDS_H_HEADER_GUARD 8 9 #include <bx/math.h> 10 11 /// 12 struct Aabb 13 { 14 bx::Vec3 min; 15 bx::Vec3 max; 16 }; 17 18 /// 19 struct Capsule 20 { 21 bx::Vec3 pos; 22 bx::Vec3 end; 23 float radius; 24 }; 25 26 /// 27 struct Cone 28 { 29 bx::Vec3 pos; 30 bx::Vec3 end; 31 float radius; 32 }; 33 34 /// 35 struct Cylinder 36 { 37 bx::Vec3 pos; 38 bx::Vec3 end; 39 float radius; 40 }; 41 42 /// 43 struct Disk 44 { 45 bx::Vec3 center; 46 bx::Vec3 normal; 47 float radius; 48 }; 49 50 /// 51 struct Obb 52 { 53 float mtx[16]; 54 }; 55 56 /// 57 struct Sphere 58 { 59 bx::Vec3 center; 60 float radius; 61 }; 62 63 /// 64 struct Triangle 65 { 66 bx::Vec3 v0; 67 bx::Vec3 v1; 68 bx::Vec3 v2; 69 }; 70 71 /// 72 struct Ray 73 { 74 bx::Vec3 pos; 75 bx::Vec3 dir; 76 }; 77 78 /// 79 struct Hit 80 { 81 bx::Vec3 pos; 82 bx::Plane plane; 83 }; 84 85 /// 86 bx::Vec3 getCenter(const Aabb& _aabb); 87 88 /// 89 bx::Vec3 getExtents(const Aabb& _aabb); 90 91 /// 92 bx::Vec3 getCenter(const Triangle& _triangle); 93 94 /// 95 void toAabb(Aabb& _outAabb, const bx::Vec3& _extents); 96 97 /// 98 void toAabb(Aabb& _outAabb, const bx::Vec3& _center, const bx::Vec3& _extents); 99 100 /// Convert cylinder to axis aligned bounding box. 101 void toAabb(Aabb& _outAabb, const Cylinder& _cylinder); 102 103 /// Convert disk to axis aligned bounding box. 104 void toAabb(Aabb& _outAabb, const Disk& _disk); 105 106 /// Convert oriented bounding box to axis aligned bounding box. 107 void toAabb(Aabb& _outAabb, const Obb& _obb); 108 109 /// Convert sphere to axis aligned bounding box. 110 void toAabb(Aabb& _outAabb, const Sphere& _sphere); 111 112 /// Convert triangle to axis aligned bounding box. 113 void toAabb(Aabb& _outAabb, const Triangle& _triangle); 114 115 /// Calculate axis aligned bounding box. 116 void toAabb(Aabb& _outAabb, const void* _vertices, uint32_t _numVertices, uint32_t _stride); 117 118 /// Transform vertices and calculate axis aligned bounding box. 119 void toAabb(Aabb& _outAabb, const float* _mtx, const void* _vertices, uint32_t _numVertices, uint32_t _stride); 120 121 /// Expand AABB. 122 void aabbExpand(Aabb& _outAabb, float _factor); 123 124 /// Expand AABB with xyz. 125 void aabbExpand(Aabb& _outAabb, const bx::Vec3& _pos); 126 127 /// Calculate surface area of axis aligned bounding box. 128 float calcAreaAabb(const Aabb& _aabb); 129 130 /// Convert axis aligned bounding box to oriented bounding box. 131 void toObb(Obb& _outObb, const Aabb& _aabb); 132 133 /// Calculate oriented bounding box. 134 void calcObb(Obb& _outObb, const void* _vertices, uint32_t _numVertices, uint32_t _stride, uint32_t _steps = 17); 135 136 /// Calculate maximum bounding sphere. 137 void calcMaxBoundingSphere(Sphere& _outSphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride); 138 139 /// Calculate minimum bounding sphere. 140 void calcMinBoundingSphere(Sphere& _outSphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride, float _step = 0.01f); 141 142 /// Returns 6 (near, far, left, right, top, bottom) planes representing frustum planes. 143 void buildFrustumPlanes(bx::Plane* _outPlanes, const float* _viewProj); 144 145 /// Returns point from 3 intersecting planes. 146 bx::Vec3 intersectPlanes(const bx::Plane& _pa, const bx::Plane& _pb, const bx::Plane& _pc); 147 148 /// Make screen space ray from x, y coordinate and inverse view-projection matrix. 149 Ray makeRay(float _x, float _y, const float* _invVp); 150 151 /// Intersect ray / AABB. 152 bool intersect(const Ray& _ray, const Aabb& _aabb, Hit* _hit = NULL); 153 154 /// Intersect ray / OBB. 155 bool intersect(const Ray& _ray, const Obb& _obb, Hit* _hit = NULL); 156 157 /// Intersect ray / cylinder. 158 bool intersect(const Ray& _ray, const Cylinder& _cylinder, Hit* _hit = NULL); 159 160 /// Intersect ray / capsule. 161 bool intersect(const Ray& _ray, const Capsule& _capsule, Hit* _hit = NULL); 162 163 /// Intersect ray / cone. 164 bool intersect(const Ray& _ray, const Cone& _cone, Hit* _hit = NULL); 165 166 /// Intersect ray / disk. 167 bool intersect(const Ray& _ray, const Disk& _disk, Hit* _hit = NULL); 168 169 /// Intersect ray / plane. 170 bool intersect(const Ray& _ray, const bx::Plane& _plane, Hit* _hit = NULL); 171 172 /// Intersect ray / sphere. 173 bool intersect(const Ray& _ray, const Sphere& _sphere, Hit* _hit = NULL); 174 175 /// Intersect ray / triangle. 176 bool intersect(const Ray& _ray, const Triangle& _triangle, Hit* _hit = NULL); 177 178 /// 179 bool overlap(const Aabb& _aabb, const bx::Vec3& _pos); 180 181 /// 182 bool overlap(const Aabb& _aabb, const Sphere& _sphere); 183 184 /// 185 bool overlap(const Aabb& _aabbA, const Aabb& _aabbB); 186 187 /// 188 bool overlap(const Aabb& _aabb, const bx::Plane& _plane); 189 190 /// 191 bool overlap(const Aabb& _aabb, const Triangle& _triangle); 192 193 /// 194 bool overlap(const Aabb& _aabb, const Cylinder& _cylinder); 195 196 /// 197 bool overlap(const Aabb& _aabb, const Capsule& _capsule); 198 199 /// 200 bool overlap(const Aabb& _aabb, const Cone& _cone); 201 202 /// 203 bool overlap(const Aabb& _aabb, const Disk& _disk); 204 205 /// 206 bool overlap(const Aabb& _aabb, const Obb& _obb); 207 208 /// 209 bool overlap(const Capsule& _capsule, const bx::Vec3& _pos); 210 211 /// 212 bool overlap(const Capsule& _capsule, const Sphere& _sphere); 213 214 /// 215 bool overlap(const Capsule& _capsule, const Aabb& _aabb); 216 217 /// 218 bool overlap(const Capsule& _capsule, const bx::Plane& _plane); 219 220 /// 221 bool overlap(const Capsule& _capsule, const Triangle& _triangle); 222 223 /// 224 bool overlap(const Capsule& _capsule, const Cylinder& _cylinder); 225 226 /// 227 bool overlap(const Capsule& _capsuleA, const Capsule& _capsuleB); 228 229 /// 230 bool overlap(const Capsule& _capsule, const Cone& _cone); 231 232 /// 233 bool overlap(const Capsule& _capsule, const Disk& _disk); 234 235 /// 236 bool overlap(const Capsule& _capsule, const Obb& _obb); 237 238 /// 239 bool overlap(const Cone& _cone, const bx::Vec3& _pos); 240 241 /// 242 bool overlap(const Cone& _cone, const Sphere& _sphere); 243 244 /// 245 bool overlap(const Cone& _cone, const Aabb& _aabb); 246 247 /// 248 bool overlap(const Cone& _cone, const bx::Plane& _plane); 249 250 /// 251 bool overlap(const Cone& _cone, const Triangle& _triangle); 252 253 /// 254 bool overlap(const Cone& _cone, const Cylinder& _cylinder); 255 256 /// 257 bool overlap(const Cone& _cone, const Capsule& _capsule); 258 259 /// 260 bool overlap(const Cone& _coneA, const Cone& _coneB); 261 262 /// 263 bool overlap(const Cone& _cone, const Disk& _disk); 264 265 /// 266 bool overlap(const Cone& _cone, const Obb& _obb); 267 268 /// 269 bool overlap(const Cylinder& _cylinder, const bx::Vec3& _pos); 270 271 /// 272 bool overlap(const Cylinder& _cylinder, const Sphere& _sphere); 273 274 /// 275 bool overlap(const Cylinder& _cylinder, const Aabb& _aabb); 276 277 /// 278 bool overlap(const Cylinder& _cylinder, const bx::Plane& _plane); 279 280 /// 281 bool overlap(const Cylinder& _cylinder, const Triangle& _triangle); 282 283 /// 284 bool overlap(const Cylinder& _cylinderA, const Cylinder& _cylinderB); 285 286 /// 287 bool overlap(const Cylinder& _cylinder, const Capsule& _capsule); 288 289 /// 290 bool overlap(const Cylinder& _cylinder, const Cone& _cone); 291 292 /// 293 bool overlap(const Cylinder& _cylinder, const Disk& _disk); 294 295 /// 296 bool overlap(const Cylinder& _cylinder, const Obb& _obb); 297 298 /// 299 bool overlap(const Disk& _disk, const bx::Vec3& _pos); 300 301 /// 302 bool overlap(const Disk& _disk, const Sphere& _sphere); 303 304 /// 305 bool overlap(const Disk& _disk, const Aabb& _aabb); 306 307 /// 308 bool overlap(const Disk& _disk, const bx::Plane& _plane); 309 310 /// 311 bool overlap(const Disk& _disk, const Triangle& _triangle); 312 313 /// 314 bool overlap(const Disk& _disk, const Cylinder& _cylinder); 315 316 /// 317 bool overlap(const Disk& _disk, const Capsule& _capsule); 318 319 /// 320 bool overlap(const Disk& _disk, const Cone& _cone); 321 322 /// 323 bool overlap(const Disk& _diskA, const Disk& _diskB); 324 325 /// 326 bool overlap(const Disk& _disk, const Obb& _obb); 327 328 /// 329 bool overlap(const Obb& _obb, const bx::Vec3& _pos); 330 331 /// 332 bool overlap(const Obb& _obb, const Sphere& _sphere); 333 334 /// 335 bool overlap(const Obb& _obb, const Aabb& _aabb); 336 337 /// 338 bool overlap(const Obb& _obb, const bx::Plane& _plane); 339 340 /// 341 bool overlap(const Obb& _obb, const Triangle& _triangle); 342 343 /// 344 bool overlap(const Obb& _obb, const Cylinder& _cylinder); 345 346 /// 347 bool overlap(const Obb& _obb, const Capsule& _capsule); 348 349 /// 350 bool overlap(const Obb& _obb, const Cone& _cone); 351 352 /// 353 bool overlap(const Obb& _obb, const Disk& _disk); 354 355 /// 356 bool overlap(const Obb& _obbA, const Obb& _obbB); 357 358 /// 359 bool overlap(const bx::Plane& _plane, const bx::Vec3& _pos); 360 361 /// 362 bool overlap(const bx::Plane& _plane, const Sphere& _sphere); 363 364 /// 365 bool overlap(const bx::Plane& _plane, const Aabb& _aabb); 366 367 /// 368 bool overlap(const bx::Plane& _planeA, const bx::Plane& _planeB); 369 370 /// 371 bool overlap(const bx::Plane& _plane, const Triangle& _triangle); 372 373 /// 374 bool overlap(const bx::Plane& _plane, const Cylinder& _cylinder); 375 376 /// 377 bool overlap(const bx::Plane& _plane, const Capsule& _capsule); 378 379 /// 380 bool overlap(const bx::Plane& _plane, const Cone& _cone); 381 382 /// 383 bool overlap(const bx::Plane& _plane, const Disk& _disk); 384 385 /// 386 bool overlap(const bx::Plane& _plane, const Obb& _obb); 387 388 /// 389 bool overlap(const Sphere& _sphere, const bx::Vec3& _pos); 390 391 /// 392 bool overlap(const Sphere& _sphereA, const Sphere& _sphereB); 393 394 /// 395 bool overlap(const Sphere& _sphere, const Aabb& _aabb); 396 397 /// 398 bool overlap(const Sphere& _sphere, const bx::Plane& _plane); 399 400 /// 401 bool overlap(const Sphere& _sphere, const Triangle& _triangle); 402 403 /// 404 bool overlap(const Sphere& _sphere, const Cylinder& _cylinder); 405 406 /// 407 bool overlap(const Sphere& _sphere, const Capsule& _capsule); 408 409 /// 410 bool overlap(const Sphere& _sphere, const Cone& _cone); 411 412 /// 413 bool overlap(const Sphere& _sphere, const Disk& _disk); 414 415 /// 416 bool overlap(const Sphere& _sphere, const Obb& _obb); 417 418 /// 419 bool overlap(const Triangle& _triangle, const bx::Vec3& _pos); 420 421 /// 422 bool overlap(const Triangle& _triangle, const Sphere& _sphere); 423 424 /// 425 bool overlap(const Triangle& _triangle, const Aabb& _aabb); 426 427 /// 428 bool overlap(const Triangle& _triangle, const bx::Plane& _plane); 429 430 /// 431 bool overlap(const Triangle& _triangleA, const Triangle& _triangleB); 432 433 /// 434 bool overlap(const Triangle& _triangle, const Cylinder& _cylinder); 435 436 /// 437 bool overlap(const Triangle& _triangle, const Capsule& _capsule); 438 439 /// 440 bool overlap(const Triangle& _triangle, const Cone& _cone); 441 442 /// 443 bool overlap(const Triangle& _triangle, const Disk& _disk); 444 445 /// 446 bool overlap(const Triangle& _triangle, const Obb& _obb); 447 448 #endif // BOUNDS_H_HEADER_GUARD 449