From 21c386fee208adb7b494d2677d9f49ed49a1c1ce Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Thu, 7 Jan 2021 14:20:54 +0100 Subject: Local point configuration --- path/fixed.ml | 57 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 19 deletions(-) (limited to 'path/fixed.ml') diff --git a/path/fixed.ml b/path/fixed.ml index cb2c27f..2d42566 100755 --- a/path/fixed.ml +++ b/path/fixed.ml @@ -6,6 +6,8 @@ module type P = sig val get_coord : t -> Gg.v2 + val id : t -> int + end module Make(Point:P) = struct @@ -24,7 +26,6 @@ module Make(Point:P) = struct ; ctrl1:Gg.v2 } (* The control point *) type path = - | Empty | Line of Point.t * Point.t | Curve of bezier @@ -74,7 +75,8 @@ module Make(Point:P) = struct let get : int * path list -> path array = fun (n, t) -> - let res = Array.make n Empty in + + let res = Obj.magic (Array.make n 0) in List.iteri t ~f:(fun i elem -> Array.set res (n - i - 1) elem ); res @@ -106,7 +108,6 @@ module Make(Point:P) = struct ~init:(true, repr) ~f:(fun (first, path) element -> match element with - | Empty -> (true, path) | Line (p0, p1) -> let path = if first then @@ -133,7 +134,6 @@ module Make(Point:P) = struct Array.fold_left beziers.path ~init:None ~f:(fun res -> function - | Empty -> None | Line (p0, p1) -> let box = Gg.Box2.of_pts (Point.get_coord p0) (Point.get_coord p1) in begin match Gg.Box2.mem point box with @@ -164,7 +164,6 @@ module Make(Point:P) = struct = fun {id; path} f -> let path = Array.map path ~f:(function - | Empty -> Empty | Line (p1, p2) -> Line (f p1, f p2) | Curve bezier -> Curve {bezier with p0 = f bezier.p0 ; p1 = f bezier.p1} ) in @@ -175,7 +174,6 @@ module Make(Point:P) = struct = fun {path; _} ~f -> Array.iter path ~f:(function - | Empty -> () | Line (p1, p2) -> f p1; f p2 | Curve bezier -> f bezier.p0 ; f bezier.p1 ) @@ -193,14 +191,13 @@ module Make(Point:P) = struct ~f:(fun element -> let res = match element with - | Empty -> false | Line (p0, p1) | Curve {p0;p1;_} -> - if p0 = point then ( + if (Point.id p0) = (Point.id point) then ( idx := Some (!counter) ; true - ) else if p1 = point then ( - idx := Some (!counter +1) ; + ) else if (Point.id p1) = (Point.id point) then ( + idx := Some (!counter+1) ; true ) else false @@ -213,25 +210,47 @@ module Make(Point:P) = struct | Some 0 -> (* Remove the first point *) let path' = Array.init - ((Array.length path) -1) - ~f:( fun i -> Array.get path (i + 1)) in - {id; path=path'} + ((Array.length path)-1) + ~f:( fun i -> Array.get path (i+1)) in + { id + ; path = path' + } | Some n when n = (Array.length path) -> (* Remove the last point *) let path' = Array.init - ((Array.length path) -1) + ((Array.length path)-1) ~f:( fun i -> Array.get path i) in - {id; path=path'} + { id + ; path=path' + } | Some n -> let path' = Array.init - ((Array.length path) -1) + ((Array.length path)-1) ~f:(fun i -> - if i < (n -1) then + if i < (n-1) then Array.get path (i) + else if i = (n-1) then + (* We know that the point is not the first nor the last one. + So it is safe to call n-1 or n + 1 point + + We have to rebuild the point and set that + point_(-1).id = point_(+1).id + *) + let previous_p1 = + match Array.get path (i-1) with + | Line (_, p1) -> p1 + | Curve c -> c.p1 + in + + match Array.get path (i+1) with + | Line (_, p1) -> Line (previous_p1, p1) + | Curve c -> Curve {c with p0 = previous_p1} + else - Array.get path (i +1) + Array.get path (i+1) ) in - {id; path=path'} + { id + ; path=path'} let update : t -> path array -> t -- cgit v1.2.3