diff -r ca23936b455b -r 5d201ee4a9c3 src/geometry.h --- a/src/geometry.h Mon Jun 06 22:01:22 2022 +0300 +++ b/src/geometry.h Tue Jun 07 01:37:26 2022 +0300 @@ -2,176 +2,172 @@ #include #include "basics.h" -namespace geom +struct Plane +{ + glm::vec3 normal; + glm::vec3 anchor; +}; + +template +struct Line +{ + glm::vec direction; + glm::vec anchor; +}; + +template +struct Ray +{ + glm::vec direction; + glm::vec anchor; +}; + +inline const glm::vec3 origin = {0, 0, 0}; +inline const Plane XY = {{0, 0, 1}, origin}; +inline const Plane XZ = {{0, 1, 0}, origin}; +inline const Plane YZ = {{1, 0, 0}, origin}; + +struct LineSegment +{ + glm::vec3 p1, p2; +}; +struct Triangle +{ + glm::vec3 p1, p2, p3; +}; +struct Quadrilateral { - struct Plane - { - glm::vec3 normal; - glm::vec3 anchor; - }; + glm::vec3 p1, p2, p3, p4; +}; +struct ConditionalEdge +{ + glm::vec3 p1, p2; + glm::vec3 c1, c2; +}; +struct LineSegment2D +{ + glm::vec2 p1, p2; +}; - template - struct Line - { - glm::vec direction; - glm::vec anchor; - }; +// get polygon type from amount of points +template +struct PolygonType {}; +template<> +struct PolygonType<2> { using type = LineSegment; }; +template<> +struct PolygonType<3> { using type = Triangle; }; +template<> +struct PolygonType<4> { using type = Quadrilateral; }; +template +using Polygon = typename PolygonType::type; + +/** + * @brief Computes a line from two points + * @param point_1 + * @param point_2 + * @return line + */ +template +Line lineFromPoints(const glm::vec& point_1, const glm::vec& point_2) +{ + return {point_2 - point_1, point_1}; +} - template - struct Ray - { - glm::vec direction; - glm::vec anchor; - }; +template +Ray rayFromPoints(const glm::vec& point_1, const glm::vec& point_2) +{ + return {point_2 - point_1, point_1}; +} + +template +Line rayToLine(const Ray& ray) +{ + return {ray.direction, ray.anchor}; +} - inline const glm::vec3 origin = {0, 0, 0}; - inline const Plane XY = {{0, 0, 1}, origin}; - inline const Plane XZ = {{0, 1, 0}, origin}; - inline const Plane YZ = {{1, 0, 0}, origin}; +enum class RectangleSide +{ + Top, + Left, + Bottom, + Right +}; + +struct PointOnRectagle +{ + glm::vec2 position; + RectangleSide side; +}; - struct LineSegment - { - glm::vec3 p1, p2; - }; - struct Triangle - { - glm::vec3 p1, p2, p3; - }; - struct Quadrilateral - { - glm::vec3 p1, p2, p3, p4; +std::optional lineLineIntersection(const Line<2>& line_1, const Line<2>& line_2); +std::optional rayLineSegmentIntersection(const Ray<2>& ray, const LineSegment2D& line); +std::optional rayRectangleIntersection(const Ray<2>& ray, const QRectF& rectangle); +Plane planeFromTriangle(const Triangle& triangle); +glm::vec3 normalVector(const Triangle& triangle); +std::optional linePlaneIntersection( + const Line<3>& line, + const Plane& plane, const float epsilon = 1e-6f); +glm::vec3 scalingVector(const glm::mat4 matrix); +LineSegment2D top(const QRectF& rectangle); +LineSegment2D bottom(const QRectF& rectangle); +LineSegment2D left(const QRectF& rectangle); +LineSegment2D right(const QRectF& rectangle); +bool isConvex(const std::vector& polygon); +Winding winding(const QPolygonF& polygon); +struct ScalingExtract +{ + glm::vec3 scaling; + glm::mat4 unscaled; +}; +ScalingExtract extractScaling(const glm::mat4& matrix); + +struct NPolygon +{ + std::vector points; +}; + +inline constexpr bool isclose(const glm::vec3& a, const glm::vec3& b) +{ + return qFuzzyCompare(a.x, b.x) + and qFuzzyCompare(a.y, b.y) + and qFuzzyCompare(a.z, b.z); +} + +struct CircleF +{ + QPointF center; + qreal radius; +}; + +/** + * @brief Inscribes a circle + * @param circle + * @return a QRectF that inscribes the specified circle + */ +inline constexpr QRectF inscribe(const CircleF& circle) +{ + return { + circle.center.x() - circle.radius, + circle.center.y() - circle.radius, + circle.radius * 2, + circle.radius * 2 }; - struct ConditionalEdge - { - glm::vec3 p1, p2; - glm::vec3 c1, c2; - }; - struct LineSegment2D - { - glm::vec2 p1, p2; - }; - - // get polygon type from amount of points - template - struct PolygonType {}; - template<> - struct PolygonType<2> { using type = LineSegment; }; - template<> - struct PolygonType<3> { using type = Triangle; }; - template<> - struct PolygonType<4> { using type = Quadrilateral; }; - template - using Polygon = typename PolygonType::type; +} - /** - * @brief Computes a line from two points - * @param point_1 - * @param point_2 - * @return line - */ - template - Line lineFromPoints(const glm::vec& point_1, const glm::vec& point_2) - { - return {point_2 - point_1, point_1}; - } - - template - Ray rayFromPoints(const glm::vec& point_1, const glm::vec& point_2) - { - return {point_2 - point_1, point_1}; - } - - template - Line rayToLine(const Ray& ray) - { - return {ray.direction, ray.anchor}; - } - - enum class RectangleSide +struct BezierCurve +{ + glm::vec3 points[4]; + const glm::vec3& operator[](int x) const { - Top, - Left, - Bottom, - Right - }; - - struct PointOnRectagle - { - glm::vec2 position; - RectangleSide side; - }; - - std::optional lineLineIntersection(const Line<2>& line_1, const Line<2>& line_2); - std::optional rayLineSegmentIntersection(const Ray<2>& ray, const LineSegment2D& line); - std::optional rayRectangleIntersection(const Ray<2>& ray, const QRectF& rectangle); - Plane planeFromTriangle(const Triangle& triangle); - glm::vec3 normalVector(const Triangle& triangle); - std::optional linePlaneIntersection( - const Line<3>& line, - const Plane& plane, const float epsilon = 1e-6f); - glm::vec3 scalingVector(const glm::mat4 matrix); - LineSegment2D top(const QRectF& rectangle); - LineSegment2D bottom(const QRectF& rectangle); - LineSegment2D left(const QRectF& rectangle); - LineSegment2D right(const QRectF& rectangle); - bool isConvex(const std::vector& polygon); - Winding winding(const QPolygonF& polygon); - struct ScalingExtract - { - glm::vec3 scaling; - glm::mat4 unscaled; - }; - ScalingExtract extractScaling(const glm::mat4& matrix); - - struct NPolygon - { - std::vector points; - }; - - inline constexpr bool isclose(const glm::vec3& a, const glm::vec3& b) + Q_ASSERT(x >= 0 and x < 4); + return this->points[x]; + } + glm::vec3& operator[](int x) { - return qFuzzyCompare(a.x, b.x) - and qFuzzyCompare(a.y, b.y) - and qFuzzyCompare(a.z, b.z); + Q_ASSERT(x >= 0 and x < 4); + return this->points[x]; } - - struct CircleF - { - QPointF center; - qreal radius; - }; +}; - /** - * @brief Inscribes a circle - * @param circle - * @return a QRectF that inscribes the specified circle - */ - inline constexpr QRectF inscribe(const CircleF& circle) - { - return { - circle.center.x() - circle.radius, - circle.center.y() - circle.radius, - circle.radius * 2, - circle.radius * 2 - }; - } - - struct BezierCurve - { - glm::vec3 points[4]; - const glm::vec3& operator[](int x) const - { - Q_ASSERT(x >= 0 and x < 4); - return this->points[x]; - } - glm::vec3& operator[](int x) - { - Q_ASSERT(x >= 0 and x < 4); - return this->points[x]; - } - }; - - glm::vec3 pointOnCurve(const BezierCurve& curve, float t); - glm::vec3 derivativeOnCurve(const BezierCurve& curve, float t); -} -using namespace geom; +glm::vec3 pointOnCurve(const BezierCurve& curve, float t); +glm::vec3 derivativeOnCurve(const BezierCurve& curve, float t);