Game of Ur 0.3.3
This is a computer adaptation of Game of Ur, written in C++ mainly using SDL and OpenGL.
Loading...
Searching...
No Matches
spatial_query_basic_types.hpp
Go to the documentation of this file.
1
11
12#ifndef FOOLSENGINE_SPATIALQUERYGEOMETRY_H
13#define FOOLSENGINE_SPATIALQUERYGEOMETRY_H
14
15#include <cmath>
16#include <array>
17#include <glm/glm.hpp>
18
19namespace ToyMaker {
25 using BoxCorner = uint8_t;
26
33 RIGHT=0x1,
34 TOP=0x2,
35 FRONT=0x4
36 };
37
38
47 inline bool isFinite(float number) {
48 return std::isfinite(number);
49 }
50
58 inline bool isFinite(const glm::vec3& vector) {
59 return isFinite(vector.x) && isFinite(vector.y) && isFinite(vector.z);
60 }
61
70 inline bool isPositive(float number) {
71 return number > 0.f;
72 }
73
82 inline bool isPositive(const glm::vec3& vector) {
83 return isPositive(vector.x) && isPositive(vector.y) && isPositive(vector.z);
84 }
85
94 inline bool isNonNegative(float number) {
95 return number >= 0.f;
96 }
97
106 inline bool isNonNegative(const glm::vec3& vector) {
107 return isNonNegative(vector.x) && isNonNegative(vector.y) && isNonNegative(vector.z);
108 }
109
110 template <typename TDerived>
111 struct Volume;
112
113
121 struct VolumeBase_ {
130 inline static constexpr std::array<glm::vec3, 8> GetCornerSignsArray() {
131 std::array<glm::vec3, 8> cornerSigns {};
132 for(BoxCorner corner {0}; corner < 8; ++corner) {
133 cornerSigns[corner].x = corner&BoxCornerSpecifier::RIGHT? 1.f: -1.f;
134 cornerSigns[corner].y = corner&BoxCornerSpecifier::TOP? 1.f: -1.f;
135 cornerSigns[corner].z = corner&BoxCornerSpecifier::FRONT? 1.f: -1.f;
136 }
137 return cornerSigns;
138 }
139
146 inline static std::array<glm::vec3, 8> ComputeBoxCorners(const glm::vec3& boxDimensions) {
147 const std::array<glm::vec3, 8> cornerSignsArray { GetCornerSignsArray() };
148 glm::vec3 absoluteCornerOffset { .5f * boxDimensions };
149 std::array<glm::vec3, 8> cornerArray { .5f * boxDimensions };
150 for(uint8_t corner{0}; corner < 8; ++corner) {
151 cornerArray[corner] = cornerSignsArray[corner] * absoluteCornerOffset;
152 }
153 return cornerArray;
154 }
155
164 template <typename TDerived>
165 inline std::array<glm::vec3, 8> getVolumeRelativeBoxCorners() const {
167 }
168 };
169
171 template <typename TDerived>
172 struct Volume: public VolumeBase_ {
179 inline std::array<glm::vec3, 8> getVolumeRelativeBoxCorners() const {
180 return TDerived::getVolumeRelativeBoxCorners();
181 }
182 };
183
189 struct VolumeBox: public Volume<VolumeBox> {
194 glm::vec3 mDimensions {0.f, 0.f, 0.f};
195
201 inline std::array<glm::vec3, 8> getVolumeRelativeBoxCorners () const {
203 }
204
211 inline bool isSensible() const {
213 }
214 };
215
221 struct VolumeCapsule: public Volume<VolumeCapsule> {
226 float mHeight {0.f};
227
232 float mRadius {0.f};
233
239 inline std::array<glm::vec3, 8> getVolumeRelativeBoxCorners () const {
240 const glm::vec3 boxDimensions { 2.f*mRadius, mHeight + 2.f*mRadius, 2.f*mRadius};
241 return ComputeBoxCorners(boxDimensions);
242 }
243
252 inline bool isSensible() const {
253 return (
256 );
257 }
258 };
259
265 struct VolumeSphere: public Volume<VolumeSphere> {
270 float mRadius {0.f};
271
277 inline std::array<glm::vec3, 8> getVolumeRelativeBoxCorners () const {
278 return ComputeBoxCorners(glm::vec3{2*mRadius});
279 }
280
287 inline bool isSensible() const {
289 }
290 };
291
302 std::array<glm::vec3, 3> mPoints {};
303
310 inline bool isSensible() const {
311 return (
312 (
314 ) && (
316 glm::cross(
317 mPoints[2] - mPoints[0], mPoints[1] - mPoints[0]
318 ).length()
319 )
320 )
321 );
322 }
323 };
324
330 struct AreaCircle {
335 float mRadius { 0.f };
336
341 glm::vec3 mCenter { 0.f };
342
347 glm::vec3 mNormal { 0.f, -1.f, 0.f };
348
355 inline bool isSensible() const {
356 return (
357 (
359 ) && (
360 isFinite(mNormal) && isPositive(mNormal.length())
361 ) && (
363 )
364 );
365 }
366 };
367
368
374 struct Ray {
379 glm::vec3 mStart { 0.f };
380
385 glm::vec3 mDirection { 0.f, 0.f, -1.f };
386
391 float mLength { std::numeric_limits<float>::infinity() };
392
399 inline bool isSensible() const {
400 return (
401 (
403 ) && (
405 ) && (
407 )
408 );
409 }
410 };
411
417 struct Plane {
422 glm::vec3 mPointOnPlane { 0.f };
423
428 glm::vec3 mNormal { 0.f, 0.f, -1.f };
429
436 inline bool isSensible() const {
437 return (
438 (
439 isFinite(mNormal) && isPositive(mNormal.length())
440 ) && (
442 )
443 );
444 }
445 };
446}
447
448#endif
uint8_t BoxCorner
Type used to represent the name of the corner of a box.
Definition spatial_query_basic_types.hpp:25
bool isPositive(float number)
Tests whether a number is positive.
Definition spatial_query_basic_types.hpp:70
BoxCornerSpecifier
Enum values correspond to bits on a BoxCorner which help specify which side of the box on each axis i...
Definition spatial_query_basic_types.hpp:32
bool isNonNegative(float number)
Tests whether a number is non-negative.
Definition spatial_query_basic_types.hpp:94
bool isFinite(float number)
Tests whether a given number is finite.
Definition spatial_query_basic_types.hpp:47
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition camera_system.hpp:20
A set of numbers representing a single circle situated somewhere in the world.
Definition spatial_query_basic_types.hpp:330
glm::vec3 mCenter
The real-world coordinates of the center of the circle.
Definition spatial_query_basic_types.hpp:341
glm::vec3 mNormal
A vector normal to the surface of the circle, in whose direction it may be assumed the circle is faci...
Definition spatial_query_basic_types.hpp:347
bool isSensible() const
Tests whether the circle described by these parameters is valid (as opposed to invalid,...
Definition spatial_query_basic_types.hpp:355
float mRadius
The radius of the circle.
Definition spatial_query_basic_types.hpp:335
A set of 3 points located in the world forming a (hopefully sensible) triangle.
Definition spatial_query_basic_types.hpp:297
std::array< glm::vec3, 3 > mPoints
The points of the triangle, where each point has 3 components.
Definition spatial_query_basic_types.hpp:302
bool isSensible() const
Tests whether the points describing the triangle are sensible (as opposed to invalid,...
Definition spatial_query_basic_types.hpp:310
A set of numbers describing a plane situated somewhere in the world.
Definition spatial_query_basic_types.hpp:417
glm::vec3 mNormal
A vector normal to the plane.
Definition spatial_query_basic_types.hpp:428
glm::vec3 mPointOnPlane
A known point on the plane.
Definition spatial_query_basic_types.hpp:422
bool isSensible() const
Tests whether the plane described is sensible (as opposed to invalid, infinite, or degenerate).
Definition spatial_query_basic_types.hpp:436
A set of numbers describing a ray with its source at some finite point in the world,...
Definition spatial_query_basic_types.hpp:374
float mLength
The length of the ray, infinite by default.
Definition spatial_query_basic_types.hpp:391
glm::vec3 mStart
A point representing the starting point of the ray.
Definition spatial_query_basic_types.hpp:379
glm::vec3 mDirection
The direction the ray is pointing in.
Definition spatial_query_basic_types.hpp:385
bool isSensible() const
Tests whether the ray is sensible (as opposed to invalid or degenerate).
Definition spatial_query_basic_types.hpp:399
The base class of all spatial query volumes.
Definition spatial_query_basic_types.hpp:121
static std::array< glm::vec3, 8 > ComputeBoxCorners(const glm::vec3 &boxDimensions)
Computes the model relative corners of a box, given the dimensions of the box.
Definition spatial_query_basic_types.hpp:146
static constexpr std::array< glm::vec3, 8 > GetCornerSignsArray()
Returns an array populated with axis-wise sign multipliers, where the positions on the array correspo...
Definition spatial_query_basic_types.hpp:130
std::array< glm::vec3, 8 > getVolumeRelativeBoxCorners() const
Poor man's vtable which doesn't break Volume's ability to be aggregate initialized.
Definition spatial_query_basic_types.hpp:165
Holds the parameters describing the spatial query volume of a simple three-dimensionsal box.
Definition spatial_query_basic_types.hpp:189
bool isSensible() const
Tests whether the values representing the box are valid (as opposed to invalid, infinite,...
Definition spatial_query_basic_types.hpp:211
glm::vec3 mDimensions
The dimensions of the box, its width, height, and depth.
Definition spatial_query_basic_types.hpp:194
std::array< glm::vec3, 8 > getVolumeRelativeBoxCorners() const
Returns an array of coordinates corresponding to the corners of the box.
Definition spatial_query_basic_types.hpp:201
Holds the parameters describing the spatial query volume of a simple three-dimensionsal capsule (or p...
Definition spatial_query_basic_types.hpp:221
float mRadius
The radius of the hemispheres on either end of the capsule.
Definition spatial_query_basic_types.hpp:232
std::array< glm::vec3, 8 > getVolumeRelativeBoxCorners() const
Gets an array containing the coordinates of the corners of the volume aligned box just containing the...
Definition spatial_query_basic_types.hpp:239
float mHeight
The height of the cylindrical section of the capsule.
Definition spatial_query_basic_types.hpp:226
bool isSensible() const
Tests whether the values representing the capsule make sense (as opposed to being invalid,...
Definition spatial_query_basic_types.hpp:252
Holds parameters describing a spherical spatial query volume.
Definition spatial_query_basic_types.hpp:265
float mRadius
The radius of the sphere.
Definition spatial_query_basic_types.hpp:270
std::array< glm::vec3, 8 > getVolumeRelativeBoxCorners() const
Gets an array of coordinates of corners of a box just encapsulating the sphere.
Definition spatial_query_basic_types.hpp:277
bool isSensible() const
Tests whether this volumes parameters are sensible (as opposed to invalid, infinite,...
Definition spatial_query_basic_types.hpp:287
Definition spatial_query_basic_types.hpp:172
std::array< glm::vec3, 8 > getVolumeRelativeBoxCorners() const
Poor man's vtable cont'd.
Definition spatial_query_basic_types.hpp:179