bgfx/examples/common/bounds.h

299 lines
6.6 KiB
C
Raw Normal View History

2013-02-22 09:07:31 +04:00
/*
2019-01-14 04:13:25 +03:00
* Copyright 2011-2019 Branimir Karadzic. All rights reserved.
2016-01-01 11:11:04 +03:00
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
2013-02-22 09:07:31 +04:00
*/
#ifndef BOUNDS_H_HEADER_GUARD
#define BOUNDS_H_HEADER_GUARD
2013-02-22 09:07:31 +04:00
2018-11-14 09:38:41 +03:00
#include <bx/math.h>
2019-02-06 05:52:00 +03:00
///
2013-02-22 09:07:31 +04:00
struct Aabb
{
2019-02-04 06:47:33 +03:00
bx::Vec3 min;
bx::Vec3 max;
2013-02-22 09:07:31 +04:00
};
2019-02-06 05:52:00 +03:00
///
struct Capsule
2015-11-28 10:45:42 +03:00
{
2019-02-04 06:47:33 +03:00
bx::Vec3 pos;
bx::Vec3 end;
float radius;
2015-11-28 10:45:42 +03:00
};
2019-02-06 05:52:00 +03:00
///
struct Cone
2017-09-26 06:18:19 +03:00
{
2019-02-04 06:47:33 +03:00
bx::Vec3 pos;
bx::Vec3 end;
float radius;
2017-09-26 06:18:19 +03:00
};
2019-02-06 05:52:00 +03:00
///
struct Cylinder
2017-09-25 05:12:18 +03:00
{
2019-02-04 06:47:33 +03:00
bx::Vec3 pos;
bx::Vec3 end;
float radius;
2017-09-25 05:12:18 +03:00
};
2019-02-06 05:52:00 +03:00
///
2015-11-28 10:45:42 +03:00
struct Disk
{
2019-02-04 06:47:33 +03:00
bx::Vec3 center;
bx::Vec3 normal;
float radius;
2015-11-28 10:45:42 +03:00
};
2019-02-06 05:52:00 +03:00
///
2013-02-22 09:07:31 +04:00
struct Obb
{
2019-02-04 06:47:33 +03:00
float mtx[16];
2013-02-22 09:07:31 +04:00
};
2019-02-06 05:52:00 +03:00
///
2019-02-04 06:47:33 +03:00
struct Sphere
2015-11-28 10:45:42 +03:00
{
2019-02-04 06:47:33 +03:00
bx::Vec3 center;
float radius;
2015-11-28 10:45:42 +03:00
};
2019-02-06 05:52:00 +03:00
///
2019-02-04 06:47:33 +03:00
struct Triangle
2013-02-22 09:07:31 +04:00
{
2019-02-04 06:47:33 +03:00
bx::Vec3 v0;
bx::Vec3 v1;
bx::Vec3 v2;
2013-02-22 09:07:31 +04:00
};
2019-02-06 05:52:00 +03:00
///
2019-02-04 06:47:33 +03:00
struct Ray
2015-11-28 10:45:42 +03:00
{
2019-02-04 06:47:33 +03:00
bx::Vec3 pos;
bx::Vec3 dir;
2015-11-28 10:45:42 +03:00
};
2019-02-06 05:52:00 +03:00
///
2017-09-28 07:52:25 +03:00
struct Hit
2015-11-28 10:45:42 +03:00
{
2019-02-04 06:47:33 +03:00
bx::Vec3 pos;
bx::Plane plane;
2015-11-28 10:45:42 +03:00
};
2019-02-04 09:07:51 +03:00
///
bx::Vec3 getCenter(const Aabb& _aabb);
///
bx::Vec3 getExtents(const Aabb& _aabb);
///
bx::Vec3 getCenter(const Triangle& _triangle);
2019-02-04 06:47:33 +03:00
///
2019-02-05 08:08:08 +03:00
void toAabb(Aabb& _outAabb, const bx::Vec3& _extents);
///
void toAabb(Aabb& _outAabb, const bx::Vec3& _center, const bx::Vec3& _extents);
/// Convert cylinder to axis aligned bounding box.
void toAabb(Aabb& _outAabb, const Cylinder& _cylinder);
/// Convert disk to axis aligned bounding box.
void toAabb(Aabb& _outAabb, const Disk& _disk);
2013-02-22 09:07:31 +04:00
/// Convert oriented bounding box to axis aligned bounding box.
2019-02-04 06:47:33 +03:00
void toAabb(Aabb& _outAabb, const Obb& _obb);
2014-05-20 09:08:35 +04:00
/// Convert sphere to axis aligned bounding box.
2019-02-04 06:47:33 +03:00
void toAabb(Aabb& _outAabb, const Sphere& _sphere);
2019-02-05 08:08:08 +03:00
/// Convert triangle to axis aligned bounding box.
void toAabb(Aabb& _outAabb, const Triangle& _triangle);
2014-05-20 09:08:35 +04:00
2013-02-22 09:07:31 +04:00
/// Calculate axis aligned bounding box.
2019-02-04 06:47:33 +03:00
void toAabb(Aabb& _outAabb, const void* _vertices, uint32_t _numVertices, uint32_t _stride);
2013-02-22 09:07:31 +04:00
/// Transform vertices and calculate axis aligned bounding box.
2019-02-04 06:47:33 +03:00
void toAabb(Aabb& _outAabb, const float* _mtx, const void* _vertices, uint32_t _numVertices, uint32_t _stride);
2013-02-22 09:07:31 +04:00
2014-05-27 03:55:46 +04:00
/// Expand AABB.
2019-02-04 06:47:33 +03:00
void aabbExpand(Aabb& _outAabb, float _factor);
2014-05-27 03:55:46 +04:00
/// Expand AABB with xyz.
2019-02-04 06:47:33 +03:00
void aabbExpand(Aabb& _outAabb, const bx::Vec3& _pos);
2016-12-06 06:01:11 +03:00
/// Calculate surface area of axis aligned bounding box.
float calcAreaAabb(const Aabb& _aabb);
2019-02-04 06:47:33 +03:00
/// Convert axis aligned bounding box to oriented bounding box.
void toObb(Obb& _outObb, const Aabb& _aabb);
2014-05-27 03:55:46 +04:00
2013-02-22 09:07:31 +04:00
/// Calculate oriented bounding box.
2019-02-04 06:47:33 +03:00
void calcObb(Obb& _outObb, const void* _vertices, uint32_t _numVertices, uint32_t _stride, uint32_t _steps = 17);
2013-02-22 09:07:31 +04:00
/// Calculate maximum bounding sphere.
2019-02-04 06:47:33 +03:00
void calcMaxBoundingSphere(Sphere& _outSphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride);
2013-02-22 09:07:31 +04:00
/// Calculate minimum bounding sphere.
2019-02-04 06:47:33 +03:00
void calcMinBoundingSphere(Sphere& _outSphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride, float _step = 0.01f);
2013-02-22 09:07:31 +04:00
/// Returns 6 (near, far, left, right, top, bottom) planes representing frustum planes.
2019-02-04 06:47:33 +03:00
void buildFrustumPlanes(bx::Plane* _outPlanes, const float* _viewProj);
/// Returns point from 3 intersecting planes.
2019-01-04 09:03:40 +03:00
bx::Vec3 intersectPlanes(const bx::Plane& _pa, const bx::Plane& _pb, const bx::Plane& _pc);
2015-11-28 10:45:42 +03:00
/// Make screen space ray from x, y coordinate and inverse view-projection matrix.
Ray makeRay(float _x, float _y, const float* _invVp);
/// Intersect ray / AABB.
2017-09-28 07:52:25 +03:00
bool intersect(const Ray& _ray, const Aabb& _aabb, Hit* _hit = NULL);
2015-11-28 10:45:42 +03:00
/// Intersect ray / OBB.
2017-09-28 07:52:25 +03:00
bool intersect(const Ray& _ray, const Obb& _obb, Hit* _hit = NULL);
2015-11-28 10:45:42 +03:00
/// Intersect ray / cylinder.
2017-09-28 07:52:25 +03:00
bool intersect(const Ray& _ray, const Cylinder& _cylinder, Hit* _hit = NULL);
2017-09-26 06:18:19 +03:00
/// Intersect ray / capsule.
2017-09-28 07:52:25 +03:00
bool intersect(const Ray& _ray, const Capsule& _capsule, Hit* _hit = NULL);
2015-11-28 10:45:42 +03:00
2017-09-25 05:12:18 +03:00
/// Intersect ray / cone.
2017-09-28 07:52:25 +03:00
bool intersect(const Ray& _ray, const Cone& _cone, Hit* _hit = NULL);
2017-09-25 05:12:18 +03:00
2015-11-28 10:45:42 +03:00
/// Intersect ray / disk.
2017-09-28 07:52:25 +03:00
bool intersect(const Ray& _ray, const Disk& _disk, Hit* _hit = NULL);
2015-11-28 10:45:42 +03:00
/// Intersect ray / plane.
2019-01-04 09:03:40 +03:00
bool intersect(const Ray& _ray, const bx::Plane& _plane, Hit* _hit = NULL);
2015-11-28 10:45:42 +03:00
/// Intersect ray / sphere.
2017-09-28 07:52:25 +03:00
bool intersect(const Ray& _ray, const Sphere& _sphere, Hit* _hit = NULL);
2015-11-28 10:45:42 +03:00
/// Intersect ray / triangle.
2019-02-04 06:47:33 +03:00
bool intersect(const Ray& _ray, const Triangle& _triangle, Hit* _hit = NULL);
///
bool overlap(const Aabb& _aabb, const bx::Vec3& _pos);
2019-02-04 09:07:51 +03:00
///
bool overlap(const Aabb& _aabb, const Sphere& _sphere);
2019-02-04 06:47:33 +03:00
///
bool overlap(const Aabb& _aabbA, const Aabb& _aabbB);
///
bool overlap(const Aabb& _aabb, const bx::Plane& _plane);
///
bool overlap(const Aabb& _aabb, const Triangle& _triangle);
///
bool overlap(const Aabb& _aabb, const Cylinder& _cylinder);
///
bool overlap(const Aabb& _aabb, const Capsule& _capsule);
///
bool overlap(const Aabb& _aabb, const Cone& _cone);
///
bool overlap(const Aabb& _aabb, const Disk& _disk);
///
bool overlap(const Aabb& _aabb, const Obb& _obb);
2019-02-06 05:31:42 +03:00
///
bool overlap(const Capsule& _capsule, const bx::Vec3& _pos);
///
bool overlap(const Capsule& _capsule, const Sphere& _sphere);
///
bool overlap(const Capsule& _capsule, const Aabb& _aabb);
///
bool overlap(const Capsule& _capsule, const bx::Plane& _plane);
///
bool overlap(const Capsule& _capsule, const Triangle& _triangle);
///
bool overlap(const Capsule& _capsule, const Cylinder& _cylinder);
///
bool overlap(const Capsule& _capsuleA, const Capsule& _capsuleB);
///
bool overlap(const Capsule& _capsule, const Cone& _cone);
///
bool overlap(const Capsule& _capsule, const Disk& _disk);
///
bool overlap(const Capsule& _capsule, const Obb& _obb);
2019-02-06 05:52:00 +03:00
///
bool overlap(const Sphere& _sphere, const bx::Vec3& _pos);
///
bool overlap(const Sphere& _sphereA, const Sphere& _sphereB);
///
bool overlap(const Sphere& _sphere, const Aabb& _aabb);
///
bool overlap(const Sphere& _sphere, const bx::Plane& _plane);
///
bool overlap(const Sphere& _sphere, const Triangle& _triangle);
///
bool overlap(const Sphere& _sphere, const Cylinder& _cylinder);
///
bool overlap(const Sphere& _sphere, const Capsule& _capsule);
///
bool overlap(const Sphere& _sphere, const Cone& _cone);
///
bool overlap(const Sphere& _sphere, const Disk& _disk);
///
bool overlap(const Sphere& _sphere, const Obb& _obb);
2019-02-04 06:47:33 +03:00
///
bool overlap(const Triangle& _triangle, const bx::Vec3& _pos);
2019-02-04 09:07:51 +03:00
///
bool overlap(const Triangle& _triangle, const Sphere& _sphere);
///
bool overlap(const Triangle& _triangle, const Aabb& _aabb);
2019-02-04 06:47:33 +03:00
///
bool overlap(const Triangle& _triangle, const bx::Plane& _plane);
///
bool overlap(const Triangle& _triangleA, const Triangle& _triangleB);
///
bool overlap(const Triangle& _triangle, const Cylinder& _cylinder);
///
bool overlap(const Triangle& _triangle, const Capsule& _capsule);
///
bool overlap(const Triangle& _triangle, const Cone& _cone);
///
bool overlap(const Triangle& _triangle, const Disk& _disk);
///
bool overlap(const Triangle& _triangle, const Obb& _obb);
2015-11-28 10:45:42 +03:00
#endif // BOUNDS_H_HEADER_GUARD