From 6e5c6bf7beadc72e64e5d929e301b473b01c9303 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Thu, 7 Jan 2021 18:41:30 +0100 Subject: Update --- path/fixed.ml | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'path/fixed.ml') diff --git a/path/fixed.ml b/path/fixed.ml index 2d42566..176d818 100755 --- a/path/fixed.ml +++ b/path/fixed.ml @@ -76,6 +76,9 @@ module Make(Point:P) = struct : int * path list -> path array = fun (n, t) -> + (* The array is initialized with a magic number, and just after + filled with the values from the list in reverse. All the elements are set. + *) let res = Obj.magic (Array.make n 0) in List.iteri t ~f:(fun i elem -> Array.set res (n - i - 1) elem ); @@ -125,10 +128,18 @@ module Make(Point:P) = struct ) in Repr.stop repr + + type approx = + { distance : float + ; closest_point : Gg.v2 + ; ratio : float + ; p0 : Point.t + ; p1 : Point.t } + (** Return the distance between a given point and the curve. May return None if the point is out of the curve *) let distance - : Gg.v2 -> t -> (Gg.v2 * float * Point.t * Point.t) option + : Gg.v2 -> t -> approx option = fun point beziers -> Array.fold_left beziers.path @@ -151,12 +162,16 @@ module Make(Point:P) = struct ; ctrl0 = bezier.ctrl0 ; ctrl1 = bezier.ctrl1 } ) in - let _, point' = Shapes.Bezier.get_closest_point point bezier' in - let distance = Gg.V2.( norm (point - point') ) in + let ratio, point' = Shapes.Bezier.get_closest_point point bezier' in + let distance' = Gg.V2.( norm (point - point') ) in match res with - | None -> Some (point', distance, bezier.p0, bezier.p1) - | Some (_, d, _, _) when d < distance -> res - | _ -> (Some (point', distance, bezier.p0, bezier.p1)) + | Some {distance; _} when distance < distance' -> res + | _ -> Some + { closest_point = point' + ; distance = distance' + ; p0 = bezier.p0 + ; p1 = bezier.p1 + ; ratio } ) let map_point -- cgit v1.2.3