diff options
author | Sébastien Dailly <sebastien@chimrod.com> | 2020-12-19 20:26:26 +0100 |
---|---|---|
committer | Sébastien Dailly <sebastien@chimrod.com> | 2020-12-19 22:12:48 +0100 |
commit | 01c0f5faf98b78d44aaae7f70e0cf4229ad8ed91 (patch) | |
tree | c821e0547b3bfc9201bb05610aa922217ea0fd63 | |
parent | 0faaa5fda396f0eca6bebf69f3624a344278fa6e (diff) |
Save the point information in each bezier curve
-rwxr-xr-x | path/builder.ml | 92 | ||||
-rwxr-xr-x | path/draw.ml | 2 |
2 files changed, 42 insertions, 52 deletions
diff --git a/path/builder.ml b/path/builder.ml index 4fe8951..01dfb35 100755 --- a/path/builder.ml +++ b/path/builder.ml @@ -82,10 +82,10 @@ module Make(Point:P) = struct lastPoint p4 p3 p2 p1 in let bezier_point = - { p0 = lastPoint - ; p1 = p4 - ; ctrl0 = bezier.Shapes.Bezier.ctrl0 - ; ctrl1 = bezier.Shapes.Bezier.ctrl1 + { p0 = p2 + ; p1 = p1 + ; ctrl0 = bezier.Shapes.Bezier.ctrl1 + ; ctrl1 = bezier.Shapes.Bezier.ctrl0 } in (* We remove the last point and add the bezier curve in the list*) @@ -148,21 +148,23 @@ module Make(Point:P) = struct ; p1 = Point.empty } in let _ = List.fold_left points - ~init:(true, first_point, 0) - ~f:(fun (first, prev_point, i) point -> - if first then (false, prev_point, i) + ~init:(first_point, -1) + ~f:(fun (prev_point, i) point -> + (* In the first step, prev_point = point *) + if i < 0 then + ( prev_point + , 0) else let bezier_curve = Array.get beziers i in - Array.set curves i - { p0 = prev_point - ; ctrl0 = bezier_curve.Shapes.Bezier.ctrl1 - ; ctrl1 = bezier_curve.Shapes.Bezier.ctrl0 - ; p1 = point }; - + { p0 = Point.copy prev_point bezier_curve.Shapes.Bezier.p0 + ; ctrl0 = bezier_curve.Shapes.Bezier.ctrl0 + ; ctrl1 = bezier_curve.Shapes.Bezier.ctrl1 + ; p1 = Point.copy point bezier_curve.Shapes.Bezier.p1 }; - (false, point, i + 1) + ( point + , i + 1) ) in curves @@ -174,19 +176,16 @@ module Make(Point:P) = struct = fun (points, beziers) -> let path = Point.create_path () in - let path, last = match points with + let path = match points with | [] -> - ( path - , None ) + ( path ) | p1::[] -> - ( Point.start p1 path - , Some p1 ) + ( Point.start p1 path ) | p1::p2::[] -> let path = Point.start p1 path |> Point.line_to p2 in - ( path - , Some p2 ) + ( path ) | p0::p1::p2::[] -> let path = Point.start p0 path in @@ -201,14 +200,13 @@ module Make(Point:P) = struct and ctrl1 = Point.copy p1 b.Shapes.Bezier.ctrl1 and p2' = Point.copy p1 b.Shapes.Bezier.p1 in - ( Point.quadratic_to p0' ctrl0 ctrl1 p2' path - , Some p2 ) + ( Point.quadratic_to p0' ctrl0 ctrl1 p2' path ) | (p0::_ as points) -> let (let*) v f = match v with | Ok beziers -> f beziers - | _ -> path, None in + | _ -> path in let points' = List.map ~f:Point.get_coord points in let connexion = match beziers with @@ -226,44 +224,36 @@ module Make(Point:P) = struct let path = ref path in let point = ref p0 in - List.iteri points + List.iteri + points ~f:(fun i pt -> - if i < Array.length beziers then ( - - let bezier = Array.get beziers i in + (* The first iteration is ignored, as we need both previous and + current point for the two point in the curve *) + if i > 0 then ( - let p0' = Point.copy pt bezier.Shapes.Bezier.p0 + let bezier = Array.get beziers (i - 1) in + let p0' = !point and ctrl0 = Point.copy (!point) bezier.Shapes.Bezier.ctrl0 and ctrl1 = Point.copy pt bezier.Shapes.Bezier.ctrl1 - and p1' = Point.copy pt bezier.Shapes.Bezier.p1 in + and p1' = pt in path := Point.quadratic_to p0' ctrl0 ctrl1 p1' (!path); - let () = if i > 0 then - point := pt in - () + point := pt; ) ); - ( !path - , Some !point ) + ( !path ) in - let path = match last with - | None -> path - | Some pt -> - - (* TODO : instead of copying the last point, keeep a track for each - point as declared in the type P.t *) - - List.fold_left beziers - ~init:path - ~f:(fun path bezier -> - let p0' = bezier.p0 - and ctrl0 = Point.copy pt bezier.ctrl0 - and ctrl1 = Point.copy pt bezier.ctrl1 - and p1' = bezier.p1 in - Point.quadratic_to p0' ctrl0 ctrl1 p1' path - ) + let path = List.fold_left beziers + ~init:path + ~f:(fun path bezier -> + let p0' = bezier.p0 + and ctrl0 = Point.copy bezier.p0 bezier.ctrl0 + and ctrl1 = Point.copy bezier.p1 bezier.ctrl1 + and p1' = bezier.p1 in + Point.quadratic_to p0' ctrl0 ctrl1 p1' path + ) in Point.stop path end diff --git a/path/draw.ml b/path/draw.ml index 5e05e01..b4b7e28 100755 --- a/path/draw.ml +++ b/path/draw.ml @@ -217,7 +217,7 @@ let to_path let* beziers = Shapes.Bspline.to_bezier points' in let curves = Path_Builder.points_to_beziers points beziers in {id; path=Curve curves} - | _, _ -> + | beziers, _ -> let (let*) v f = match v with | Ok beziers -> f beziers |