Mon, 10 Apr 2023 14:46:36 +0300
Move bezier curve to its own file
CMakeLists.txt | file | annotate | diff | comparison | revisions | |
src/bezier_curve.cpp | file | annotate | diff | comparison | revisions | |
src/bezier_curve.h | file | annotate | diff | comparison | revisions | |
src/geometry.cpp | file | annotate | diff | comparison | revisions | |
src/geometry.h | file | annotate | diff | comparison | revisions |
--- a/CMakeLists.txt Mon Apr 10 14:25:19 2023 +0300 +++ b/CMakeLists.txt Mon Apr 10 14:46:36 2023 +0300 @@ -63,6 +63,7 @@ add_subdirectory(widgets) set(SOURCE_FILES + src/bezier_curve.cpp src/colors.cpp src/documentmanager.cpp src/geometry.cpp @@ -97,6 +98,7 @@ ) set(HEADER_FILES src/basics.h + src/bezier_curve.h src/circularprimitive.h src/colors.h src/documentmanager.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/bezier_curve.cpp Mon Apr 10 14:46:36 2023 +0300 @@ -0,0 +1,47 @@ +#include "bezier_curve.h" + +/** + * @brief computes the point on a Bezier curve + * @param curve + * @param t scalar between 0 and 1, with t=0 being P0 and t=1 being P3 + * @return point on curve + */ +glm::vec3 point_on_bezier_curve(const struct bezier_curve &curve, float t) +{ + // clamp t as rounding errors might make it slightly out of bounds + t = std::clamp(t, 0.0f, 1.0f); + const float t_squared = t * t; + const float t_cubed = t * t * t; + const float coefficients[3] = { + -1*t_cubed +3*t_squared -3*t +1, + +3*t_cubed -6*t_squared +3*t, + -3*t_cubed +3*t_squared, + }; + return coefficients[0] * curve.points[0] + + coefficients[1] * curve.points[1] + + coefficients[2] * curve.points[2] + + t_cubed * curve.points[3]; +} + +/** + * @brief computes the derivative of a point on a Bezier curve + * @param curve + * @param t scalar between 0 and 1, with t=0 being P0 and t=1 being P3 + * @return point on curve + */ +glm::vec3 derivate_on_bezier_curve(const struct bezier_curve &curve, float t) +{ + // clamp t as rounding errors might make it slightly out of bounds + t = std::clamp(t, 0.0f, 1.0f); + const float t_cubed = t * t; + const float coefficients[4] = { + -3*t_cubed + 6*t -3, + +9*t_cubed -12*t +3, + -9*t_cubed + 6*t, + +3*t_cubed + }; + return coefficients[0] * curve.points[0] + + coefficients[1] * curve.points[1] + + coefficients[2] * curve.points[2] + + coefficients[3] * curve.points[3]; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/bezier_curve.h Mon Apr 10 14:46:36 2023 +0300 @@ -0,0 +1,13 @@ +#ifndef BEZIER_CURVE_H +#define BEZIER_CURVE_H +#include "src/basics.h" + +struct bezier_curve +{ + glm::vec3 points[4]; +}; + +glm::vec3 point_on_bezier_curve(const struct bezier_curve& curve, float t); +glm::vec3 derivate_on_bezier_curve(const struct bezier_curve& curve, float t); + +#endif // BEZIER_CURVE_H
--- a/src/geometry.cpp Mon Apr 10 14:25:19 2023 +0300 +++ b/src/geometry.cpp Mon Apr 10 14:46:36 2023 +0300 @@ -256,43 +256,3 @@ } return (sum < 0) ? winding_e::anticlockwise : winding_e::clockwise; } - -/** - * @brief computes the point on a Bezier curve - * @param curve - * @param t scalar between 0 and 1, with t=0 being P0 and t=1 being P3 - * @return point on curve - */ -glm::vec3 pointOnCurve(const BezierCurve &curve, float t) -{ - // clamp t as rounding errors might make it slightly out of bounds - t = std::clamp(t, 0.0f, 1.0f); - const float t_2 = t * t; - const float t_3 = t * t * t; - const float coeffs[3] = { - -1*t_3 +3*t_2 -3*t +1, - +3*t_3 -6*t_2 +3*t, - -3*t_3 +3*t_2, - }; - return coeffs[0] * curve[0] + coeffs[1] * curve[1] + coeffs[2] * curve[2] + t_3 * curve[3]; -} - -/** - * @brief computes the derivative of a point on a Bezier curve - * @param curve - * @param t scalar between 0 and 1, with t=0 being P0 and t=1 being P3 - * @return point on curve - */ -glm::vec3 derivativeOnCurve(const BezierCurve &curve, float t) -{ - // clamp t as rounding errors might make it slightly out of bounds - t = std::clamp(t, 0.0f, 1.0f); - const float t_2 = t * t; - const float coeffs[4] = { - -3*t_2 + 6*t -3, - +9*t_2 -12*t +3, - -9*t_2 + 6*t, - +3*t_2 - }; - return coeffs[0] * curve[0] + coeffs[1] * curve[1] + coeffs[2] * curve[2] + coeffs[3] * curve[3]; -}
--- a/src/geometry.h Mon Apr 10 14:25:19 2023 +0300 +++ b/src/geometry.h Mon Apr 10 14:46:36 2023 +0300 @@ -164,24 +164,6 @@ }; } -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); - template<typename Iter> std::optional<glm::mat3> calculateNormal(Iter&& begin, Iter&& end) {