diff --git a/CMakeLists.txt b/CMakeLists.txt index 7781f69..24dc70e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,7 +30,25 @@ include_directories("include") add_library(J3ML SHARED ${J3ML_SRC} src/J3ML/LinearAlgebra/AxisAngle.cpp - include/J3ML/LinearAlgebra/Vector.h) + include/J3ML/LinearAlgebra/Vector.h + include/J3ML/Geometry/Plane.h + include/J3ML/Geometry/AABB.h + include/J3ML/Geometry/Frustum.h + include/J3ML/Geometry/OBB.h + include/J3ML/Geometry/Capsule.h + include/J3ML/Geometry/Sphere.h + include/J3ML/Geometry/Ray.h + include/J3ML/Geometry/QuadTree.h + include/J3ML/Geometry/LineSegment.h + include/J3ML/Geometry/TriangleMesh.h + include/J3ML/Geometry/Polygon.h + include/J3ML/Geometry/Triangle.h + include/J3ML/Geometry/Triangle2D.h + src/J3ML/Geometry/AABB.cpp + src/J3ML/Geometry/Plane.cpp + src/J3ML/Geometry/Sphere.cpp + src/J3ML/Geometry/Frustum.cpp + src/J3ML/Geometry/OBB.cpp) set_target_properties(J3ML PROPERTIES LINKER_LANGUAGE CXX) install(TARGETS ${PROJECT_NAME} DESTINATION lib/${PROJECT_NAME}) diff --git a/include/J3ML/Geometry.h b/include/J3ML/Geometry.h index 9ca5021..cf4bdc6 100644 --- a/include/J3ML/Geometry.h +++ b/include/J3ML/Geometry.h @@ -20,12 +20,21 @@ namespace Geometry { class Triangle2D; class Polygon2D; + class Sphere; + class LineSegment + { + Vector3 A; + Vector3 B; + }; + struct IntersectionResult2D {}; bool Intersects2D(LineSegment2D seg, Rectangle rect); IntersectionResult2D GetIntersection2D(LineSegment2D seg, Rectangle rect); + class OBB; + // A 3D axis-aligned bounding box // This data structure can be used to represent coarse bounds of objects, in situations where detailed triangle-level // computations can be avoided. In physics systems, bounding boxes are used as an efficient early-out test for geometry @@ -34,38 +43,19 @@ namespace Geometry { // axes of the world space coordinate system. This makes computation involving AABB's very fast, since AABB's cannot // be arbitrarily oriented in the space with respect to each other. // If you need to represent a box in 3D space with arbitrary orientation, see the class OBB. */ - class AABB; + class Capsule; class Line; - class LineSegment - { - Vector3 A; - Vector3 B; - }; + class Ray { Vector3 Origin; Vector3 Direction; }; - class OBB; - class Plane - { - public: - Vector3 Position; - Vector3 Normal; - float distance = 0.f; - }; - class Frustum { - public: - Plane TopFace; - Plane BottomFace; - Plane RightFace; - Plane LeftFace; - Plane FarFace; - Plane NearFace; - }; + + class Camera { public: @@ -75,28 +65,13 @@ namespace Geometry { Vector3 Up; }; - static Frustum CreateFrustumFromCamera(const Camera& cam, float aspect, float fovY, float zNear, float zFar) - { - Frustum frustum; - const float halfVSide = zFar * tanf(fovY * 0.5f); - const float halfHSide = halfVSide * aspect; - const Vector3 frontMultFar = cam.Front * zFar; - - frustum.NearFace = Plane{cam.Position + cam.Front * zNear, cam.Front}; - frustum.FarFace = Plane{cam.Position + frontMultFar, -cam.Front}; - frustum.RightFace = Plane{cam.Position, Vector3::Cross(frontMultFar - cam.Right * halfHSide, cam.Up)}; - frustum.LeftFace = Plane{cam.Position, Vector3::Cross(cam.Up, frontMultFar+cam.Right*halfHSide)}; - frustum.TopFace = Plane{cam.Position, Vector3::Cross(cam.Right, frontMultFar - cam.Up * halfVSide)}; - frustum.BottomFace = Plane{cam.Position, Vector3::Cross(frontMultFar + cam.Up * halfVSide, cam.Right)}; - return frustum; - } class Polygon; class Polyhedron; class QuadTree; class OctTree; - class Sphere; + class Triangle; class TriangleMesh; } \ No newline at end of file diff --git a/include/J3ML/Geometry/AABB.h b/include/J3ML/Geometry/AABB.h new file mode 100644 index 0000000..c13e0b3 --- /dev/null +++ b/include/J3ML/Geometry/AABB.h @@ -0,0 +1,62 @@ +#pragma once + +#include +#include "Plane.h" +#include "Sphere.h" + +using namespace LinearAlgebra; + +namespace Geometry +{ + class AABB + { + static AABB FromCenterAndSize(const Vector3 FromSize); + + float MinX(); + + // Returns the smallest sphere that contains this AABB. + // This function computes the minimal volume sphere that contains all the points inside this AABB + Sphere MinimalEnclosingSphere() const; + // Returns the largest sphere that can fit inside this AABB + // This function computes the largest sphere that can fit inside this AABB. + Sphere MaximalContainedSphere() const; + Vector3 GetCentroid() const; + // Quickly returns an arbitrary point inside this AABB + Vector3 AnyPointFast() const; + + Vector3 PointInside(float x, float y, float z) const; + // Returns an edge of this AABB + LineSegment Edge(int edgeIndex) const; + Vector3 CornerPoint(int cornerIndex); + Vector3 ExtremePoint(const Vector3& direction) const; + Vector3 ExtremePoint(const Vector3& direction, float projectionDistance); + Vector3 PointOnEdge(int edgeIndex, float u) const; + Vector3 FaceCenterPoint(int faceIndex) const; + Vector3 FacePoint(int faceIndex, float u, float v) const; + Vector3 FaceNormal(int faceIndex) const; + Plane FacePlane(int faceIndex); + static AABB MinimalEnclosingAABB(const Vector3* pointArray, int numPoints); + Vector3 GetSize(); + Vector3 GetVolume(); + float GetVolumeCubed(); + float GetSurfaceArea(); + Vector3 GetRandomPointInside(); + Vector3 GetRandomPointOnSurface(); + Vector3 GetRandomPointOnEdge(); + Vector3 GetRandomCornerPoint(); + AABB Translated(const Vector3& offset) const; + AABB TransformAABB(const Matrix3x3& transform); + AABB TransformAABB(const Matrix4x4& transform); + AABB TransformAABB(const Quaternion& transform); + OBB Transform(const Matrix3x3& transform); + OBB Transform(const Matrix4x4& transform); + OBB Transform(const Quaternion& transform); + + bool Contains(const Vector3& point) const; + bool Contains(const LineSegment& lineSegment) const; + bool Contains(const AABB& aabb) const; + bool Contains(const OBB& obb) const; + bool Contains(const Sphere& sphere) const; + bool C + }; +} diff --git a/include/J3ML/Geometry/Capsule.h b/include/J3ML/Geometry/Capsule.h new file mode 100644 index 0000000..e8f8617 --- /dev/null +++ b/include/J3ML/Geometry/Capsule.h @@ -0,0 +1,8 @@ +// +// Created by dawsh on 1/25/24. +// + +#ifndef J3ML_CAPSULE_H +#define J3ML_CAPSULE_H + +#endif //J3ML_CAPSULE_H diff --git a/include/J3ML/Geometry/Frustum.h b/include/J3ML/Geometry/Frustum.h new file mode 100644 index 0000000..2a090f3 --- /dev/null +++ b/include/J3ML/Geometry/Frustum.h @@ -0,0 +1,38 @@ +// +// Created by dawsh on 1/25/24. +// +#pragma once + +#include "Plane.h" + +namespace Geometry +{ + class Frustum { + public: + Plane TopFace; + Plane BottomFace; + Plane RightFace; + Plane LeftFace; + Plane FarFace; + Plane NearFace; + static Frustum CreateFrustumFromCamera(const Camera& cam, float aspect, float fovY, float zNear, float zFar); + + }; + + Frustum Frustum::CreateFrustumFromCamera(const Camera &cam, float aspect, float fovY, float zNear, float zFar) { + Frustum frustum; + const float halfVSide = zFar * tanf(fovY * 0.5f); + const float halfHSide = halfVSide * aspect; + + const Vector3 frontMultFar = cam.Front * zFar; + + frustum.NearFace = Plane{cam.Position + cam.Front * zNear, cam.Front}; + frustum.FarFace = Plane{cam.Position + frontMultFar, -cam.Front}; + frustum.RightFace = Plane{cam.Position, Vector3::Cross(frontMultFar - cam.Right * halfHSide, cam.Up)}; + frustum.LeftFace = Plane{cam.Position, Vector3::Cross(cam.Up, frontMultFar+cam.Right*halfHSide)}; + frustum.TopFace = Plane{cam.Position, Vector3::Cross(cam.Right, frontMultFar - cam.Up * halfVSide)}; + frustum.BottomFace = Plane{cam.Position, Vector3::Cross(frontMultFar + cam.Up * halfVSide, cam.Right)}; + return frustum; + } + +} \ No newline at end of file diff --git a/include/J3ML/Geometry/LineSegment.h b/include/J3ML/Geometry/LineSegment.h new file mode 100644 index 0000000..f349a01 --- /dev/null +++ b/include/J3ML/Geometry/LineSegment.h @@ -0,0 +1,8 @@ +// +// Created by dawsh on 1/25/24. +// + +#ifndef J3ML_LINESEGMENT_H +#define J3ML_LINESEGMENT_H + +#endif //J3ML_LINESEGMENT_H diff --git a/include/J3ML/Geometry/OBB.h b/include/J3ML/Geometry/OBB.h new file mode 100644 index 0000000..3fa2837 --- /dev/null +++ b/include/J3ML/Geometry/OBB.h @@ -0,0 +1,8 @@ +// +// Created by dawsh on 1/25/24. +// + +#ifndef J3ML_OBB_H +#define J3ML_OBB_H + +#endif //J3ML_OBB_H diff --git a/include/J3ML/Geometry/Plane.h b/include/J3ML/Geometry/Plane.h new file mode 100644 index 0000000..4626b7b --- /dev/null +++ b/include/J3ML/Geometry/Plane.h @@ -0,0 +1,13 @@ +#pragma once +#include + +using namespace LinearAlgebra; + +class Plane +{ +public: + Vector3 Position; + Vector3 Normal; + float distance = 0.f; + +}; \ No newline at end of file diff --git a/include/J3ML/Geometry/Polygon.h b/include/J3ML/Geometry/Polygon.h new file mode 100644 index 0000000..fd70959 --- /dev/null +++ b/include/J3ML/Geometry/Polygon.h @@ -0,0 +1,8 @@ +// +// Created by dawsh on 1/25/24. +// + +#ifndef J3ML_POLYGON_H +#define J3ML_POLYGON_H + +#endif //J3ML_POLYGON_H diff --git a/include/J3ML/Geometry/QuadTree.h b/include/J3ML/Geometry/QuadTree.h new file mode 100644 index 0000000..509151b --- /dev/null +++ b/include/J3ML/Geometry/QuadTree.h @@ -0,0 +1,8 @@ +// +// Created by dawsh on 1/25/24. +// + +#ifndef J3ML_QUADTREE_H +#define J3ML_QUADTREE_H + +#endif //J3ML_QUADTREE_H diff --git a/include/J3ML/Geometry/Ray.h b/include/J3ML/Geometry/Ray.h new file mode 100644 index 0000000..b797d36 --- /dev/null +++ b/include/J3ML/Geometry/Ray.h @@ -0,0 +1,8 @@ +// +// Created by dawsh on 1/25/24. +// + +#ifndef J3ML_RAY_H +#define J3ML_RAY_H + +#endif //J3ML_RAY_H diff --git a/include/J3ML/Geometry/Sphere.h b/include/J3ML/Geometry/Sphere.h new file mode 100644 index 0000000..a8e180a --- /dev/null +++ b/include/J3ML/Geometry/Sphere.h @@ -0,0 +1,9 @@ +#pragma once + +namespace Geometry +{ + class Sphere + { + + }; +} \ No newline at end of file diff --git a/include/J3ML/Geometry/Triangle.h b/include/J3ML/Geometry/Triangle.h new file mode 100644 index 0000000..515a4ae --- /dev/null +++ b/include/J3ML/Geometry/Triangle.h @@ -0,0 +1,8 @@ +// +// Created by dawsh on 1/25/24. +// + +#ifndef J3ML_TRIANGLE_H +#define J3ML_TRIANGLE_H + +#endif //J3ML_TRIANGLE_H diff --git a/include/J3ML/Geometry/Triangle2D.h b/include/J3ML/Geometry/Triangle2D.h new file mode 100644 index 0000000..188caf2 --- /dev/null +++ b/include/J3ML/Geometry/Triangle2D.h @@ -0,0 +1,8 @@ +// +// Created by dawsh on 1/25/24. +// + +#ifndef J3ML_TRIANGLE2D_H +#define J3ML_TRIANGLE2D_H + +#endif //J3ML_TRIANGLE2D_H diff --git a/include/J3ML/Geometry/TriangleMesh.h b/include/J3ML/Geometry/TriangleMesh.h new file mode 100644 index 0000000..6523991 --- /dev/null +++ b/include/J3ML/Geometry/TriangleMesh.h @@ -0,0 +1,8 @@ +// +// Created by dawsh on 1/25/24. +// + +#ifndef J3ML_TRIANGLEMESH_H +#define J3ML_TRIANGLEMESH_H + +#endif //J3ML_TRIANGLEMESH_H diff --git a/src/J3ML/Geometry/AABB.cpp b/src/J3ML/Geometry/AABB.cpp new file mode 100644 index 0000000..239e3d1 --- /dev/null +++ b/src/J3ML/Geometry/AABB.cpp @@ -0,0 +1 @@ +#include \ No newline at end of file diff --git a/src/J3ML/Geometry/Frustum.cpp b/src/J3ML/Geometry/Frustum.cpp new file mode 100644 index 0000000..23931a1 --- /dev/null +++ b/src/J3ML/Geometry/Frustum.cpp @@ -0,0 +1 @@ +#include diff --git a/src/J3ML/Geometry/OBB.cpp b/src/J3ML/Geometry/OBB.cpp new file mode 100644 index 0000000..fa1ab33 --- /dev/null +++ b/src/J3ML/Geometry/OBB.cpp @@ -0,0 +1,3 @@ +// +// Created by dawsh on 1/25/24. +// diff --git a/src/J3ML/Geometry/Plane.cpp b/src/J3ML/Geometry/Plane.cpp new file mode 100644 index 0000000..b6d5319 --- /dev/null +++ b/src/J3ML/Geometry/Plane.cpp @@ -0,0 +1 @@ +#include \ No newline at end of file diff --git a/src/J3ML/Geometry/Sphere.cpp b/src/J3ML/Geometry/Sphere.cpp new file mode 100644 index 0000000..3676de6 --- /dev/null +++ b/src/J3ML/Geometry/Sphere.cpp @@ -0,0 +1 @@ +#include \ No newline at end of file