diff options
author | Fox Caminiti <fox@foxcam.net> | 2023-01-03 16:40:57 -0500 |
---|---|---|
committer | Fox Caminiti <fox@foxcam.net> | 2023-01-03 16:40:57 -0500 |
commit | a37ea807e93886e6a6ebc22a878a5649e97f015a (patch) | |
tree | c5af6ddc8544d97e38276bc1b83dbf1b29a8180b /src/bezier.cpp | |
parent | 375c120d30456738897c4bd775e38aa1db7d239c (diff) |
shape layer work
Diffstat (limited to 'src/bezier.cpp')
-rw-r--r-- | src/bezier.cpp | 76 |
1 files changed, 40 insertions, 36 deletions
diff --git a/src/bezier.cpp b/src/bezier.cpp index 6fca5cc..ac89ed3 100644 --- a/src/bezier.cpp +++ b/src/bezier.cpp @@ -102,6 +102,46 @@ Bezier_Add(memory *Memory, memory_table_list TableName, property_channel *Proper } } +// return all points +static void Bezier_CubicCalcPointsCasteljauStep(void *Data, uint32 Size, uint32 *Increment, real32 x1, real32 y1, real32 x2, real32 y2, real32 x3, real32 y3, real32 x4, real32 y4, real32 tess_tol, int level) +{ + real32 dx = x4 - x1; + real32 dy = y4 - y1; + real32 d2 = ((x2 - x4) * dy - (y2 - y4) * dx); + real32 d3 = ((x3 - x4) * dy - (y3 - y4) * dx); + d2 = (d2 >= 0) ? d2 : -d2; + d3 = (d3 >= 0) ? d3 : -d3; + if ((d2 + d3) * (d2 + d3) < tess_tol * (dx * dx + dy * dy)) + { + real32 *Address = (real32 *)((uint8 *)Data + *Increment*Size); + *Address = x4; + *(Address + 1) = y4; + *(Address + 2) = 0; + *Increment += 1; + } + else if (level < 10) + { + real32 x12 = (x1 + x2)*0.5f, y12 = (y1 + y2)*0.5f; + real32 x23 = (x2 + x3)*0.5f, y23 = (y2 + y3)*0.5f; + real32 x34 = (x3 + x4)*0.5f, y34 = (y3 + y4)*0.5f; + real32 x123 = (x12 + x23)*0.5f, y123 = (y12 + y23)*0.5f; + real32 x234 = (x23 + x34)*0.5f, y234 = (y23 + y34)*0.5f; + real32 x1234 = (x123 + x234)*0.5f, y1234 = (y123 + y234)*0.5f; + Bezier_CubicCalcPointsCasteljauStep(Data, Size, Increment, x1, y1, x12, y12, x123, y123, x1234, y1234, tess_tol, level + 1); + Bezier_CubicCalcPointsCasteljauStep(Data, Size, Increment, x1234, y1234, x234, y234, x34, y34, x4, y4, tess_tol, level + 1); + } +} + +uint32 Bezier_CubicCalcPoints(v2 p1, v2 p2, v2 p3, v2 p4, void *Data, uint32 Size) +{ + uint32 Increment = 0; + real32 tess_tol = TESS_TOL; + void *Pointer = Data; + Bezier_CubicCalcPointsCasteljauStep(Pointer, Size, &Increment, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y, tess_tol, 0); + return Increment; +} + + #if 0 // A modified version of the bezier code in ImGui with extra features for bitmap and path interaction. @@ -214,35 +254,6 @@ static void Bezier_CubicMinMaxCasteljauStep(v2 *p_min, v2 *p_max, real32 x1, rea } } -// return all points -static void Bezier_CubicCalcPointsCasteljauStep(void *Data, uint32 *Increment, real32 x1, real32 y1, real32 x2, real32 y2, real32 x3, real32 y3, real32 x4, real32 y4, real32 tess_tol, int level) -{ - real32 dx = x4 - x1; - real32 dy = y4 - y1; - real32 d2 = ((x2 - x4) * dy - (y2 - y4) * dx); - real32 d3 = ((x3 - x4) * dy - (y3 - y4) * dx); - d2 = (d2 >= 0) ? d2 : -d2; - d3 = (d3 >= 0) ? d3 : -d3; - if ((d2 + d3) * (d2 + d3) < tess_tol * (dx * dx + dy * dy)) - { - *((real32 *)Data + *Increment*3) = x4; - *((real32 *)Data + *Increment*3 + 1) = y4; - *((real32 *)Data + *Increment*3 + 2) = 0; - *Increment += 1; - } - else if (level < 10) - { - real32 x12 = (x1 + x2)*0.5f, y12 = (y1 + y2)*0.5f; - real32 x23 = (x2 + x3)*0.5f, y23 = (y2 + y3)*0.5f; - real32 x34 = (x3 + x4)*0.5f, y34 = (y3 + y4)*0.5f; - real32 x123 = (x12 + x23)*0.5f, y123 = (y12 + y23)*0.5f; - real32 x234 = (x23 + x34)*0.5f, y234 = (y23 + y34)*0.5f; - real32 x1234 = (x123 + x234)*0.5f, y1234 = (y123 + y234)*0.5f; - Bezier_CubicCalcPointsCasteljauStep(Data, Increment, x1, y1, x12, y12, x123, y123, x1234, y1234, tess_tol, level + 1); - Bezier_CubicCalcPointsCasteljauStep(Data, Increment, x1234, y1234, x234, y234, x34, y34, x4, y4, tess_tol, level + 1); - } -} - real32 Bezier_CubicRatioOfPoint(v2 p1, v2 p2, v2 p3, v2 p4, v2 p) { real32 tess_tol = TESS_TOL; @@ -254,13 +265,6 @@ real32 Bezier_CubicRatioOfPoint(v2 p1, v2 p2, v2 p3, v2 p4, v2 p) return ratio; } -void Bezier_CubicCalcPoints(v2 p1, v2 p2, v2 p3, v2 p4, void *Data, uint32 *Increment) -{ - real32 tess_tol = TESS_TOL; - void *Pointer = Data; - Bezier_CubicCalcPointsCasteljauStep(Pointer, Increment, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y, tess_tol, 0); -} - // These functions will become more generalized as shapes are added. static void |