aboutsummaryrefslogtreecommitdiff
path: root/script.it/worker.ml
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@chimrod.com>2021-01-07 21:54:46 +0100
committerSébastien Dailly <sebastien@chimrod.com>2021-01-09 06:27:18 +0100
commit32618a5ce8e2b306af102e4c16711b090c36b840 (patch)
tree1c22b5bcf9f29e9ff0118cfa9aedd6fb05c9ab0f /script.it/worker.ml
parent6e5c6bf7beadc72e64e5d929e301b473b01c9303 (diff)
Allow point movement
Diffstat (limited to 'script.it/worker.ml')
-rwxr-xr-xscript.it/worker.ml67
1 files changed, 12 insertions, 55 deletions
diff --git a/script.it/worker.ml b/script.it/worker.ml
index 6f425cd..4ea9220 100755
--- a/script.it/worker.ml
+++ b/script.it/worker.ml
@@ -1,65 +1,22 @@
-open StdLabels
open Js_of_ocaml
type message = [
- | `Complete of (int * (Path.Fixed.path array))
- | `DeletePoint of (int * Path.Point.t * Path.Fixed.t)
+ | `Complete of Path.Fixed.t
+ | `DeletePoint of (Path.Point.t * Path.Fixed.t)
]
-let get_point
- : Path.Fixed.path -> Gg.v2
- = function
- | Line (_, p1) -> Path.Point.get_coord p1
- | Curve bezier -> Path.Point.get_coord bezier.Path.Fixed.p1
-
-let first_point
- : Path.Fixed.path -> Gg.v2
- = function
- | Line (p0, _) -> Path.Point.get_coord p0
- | Curve bezier -> Path.Point.get_coord bezier.Path.Fixed.p0
-
-let assoc_point
- : Shapes.Bezier.t -> Path.Fixed.path -> Path.Fixed.path
- = fun bezier -> function
- | Line (p0, p1)
- | Curve {p0; p1; _} ->
- let p0' = Path.Point.copy p0 bezier.Shapes.Bezier.p0
- and p1' = Path.Point.copy p1 bezier.Shapes.Bezier.p1 in
- Curve
- { Path.Fixed.p0 = p0'
- ; Path.Fixed.p1 = p1'
- ; Path.Fixed.ctrl0 = bezier.Shapes.Bezier.ctrl0
- ; Path.Fixed.ctrl1 = bezier.Shapes.Bezier.ctrl1
- }
-
-let rebuild (id, paths) =
- (* Convert all the points in list *)
- let points = List.init
- ~len:((Array.length paths) )
- ~f:(fun i -> get_point (Array.get paths i)) in
- let p0 = first_point (Array.get paths 0)in
-
- let points = p0::points in
-
- (* We process the whole curve in a single block *)
- begin match Shapes.Bspline.to_bezier points with
- | Error `InvalidPath -> ()
- | Ok beziers ->
-
- (* Now for each point, reassociate the same point information,
- We should have as many points as before *)
- let rebuilded = Array.map2 beziers paths ~f:assoc_point in
- Worker.post_message (`Complete (id, rebuilded))
- end
-
let execute (command: [> message]) =
match command with
- | `Complete (id, paths) ->
- rebuild (id, paths)
- | `DeletePoint (id, point, path) ->
- let path = Path.Fixed.remove_point path point in
- (* TODO Handle when there are less than 4 points *)
- rebuild (id, Path.Fixed.path path)
+ | `Complete path ->
+ begin match Path.Fixed.rebuild path with
+ | Some path -> Worker.post_message (`Complete path)
+ | None -> ()
+ end
+ | `DeletePoint (point, path) ->
+ begin match Path.Fixed.remove_point path point with
+ | Some path -> Worker.post_message (`Complete path)
+ | None -> ()
+ end
| any ->
Worker.post_message (`Other any)