Commits

Barry Schwartz committed 458d438

Initial checkin.

  • Participants

Comments (0)

Files changed (5)

+#  Copyright (c) 2011 Barry Schwartz
+#
+#  Permission is hereby granted, free of charge, to any person
+#  obtaining a copy of this software and associated documentation
+#  files (the "Software"), to deal in the Software without
+#  restriction, including without limitation the rights to use,
+#  copy, modify, merge, publish, distribute, sublicense, and/or sell
+#  copies of the Software, and to permit persons to whom the
+#  Software is furnished to do so, subject to the following
+#  conditions:
+#
+#  The above copyright notice and this permission notice shall be
+#  included in all copies or substantial portions of the Software.
+#
+#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+#  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+#  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+#  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+#  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+#  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+#  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+#  OTHER DEALINGS IN THE SOFTWARE.
+
+#--------------------------------------------------------------------------
+
+CAML2GEOM = caml2geom
+PKGCONFIG = pkg-config
+OCAMLC = ocamlfind c -g
+OCAMLOPT = ocamlfind opt -g
+OCAMLMKLIB = ocamlmklib
+CXXFLAGS = -g -fPIC
+CPPFLAGS = `$(PKGCONFIG) --cflags 2geom`
+LDFLAGS = `$(PKGCONFIG) --libs 2geom`
+
+#--------------------------------------------------------------------------
+
+#%.mli : %.ml
+#	$(OCAMLC) -i $< > $@
+
+%.cmi: %.mli
+	$(OCAMLC) -c $<
+
+%.cmo: %.ml %.cmi
+	$(OCAMLC) -c $<
+
+%.cma: %.cmo dll%_stubs.so
+	$(OCAMLC) -a -o $@ $< -dllib dll$*_stubs.so
+
+%.cmx: %.ml %.cmi
+	$(OCAMLOPT) -c $<
+
+%.cmxa: %.cmx dll%_stubs.so
+	$(OCAMLOPT) -a -o $@ $< -cclib dll%_stubs.so
+
+%_stubs.o: %_stubs.c
+	$(OCAMLC) -cc "$(CXX)" -ccopt "$(CXXFLAGS)" -ccopt "$(CPPFLAGS)" -c $<
+
+dll%_stubs.so lib_%_stubs.a: %_stubs.o
+	$(OCAMLMKLIB) -o $*_stubs $< $(LDFLAGS)
+
+#--------------------------------------------------------------------------
+
+default: $(CAML2GEOM).cmi $(CAML2GEOM).cma $(CAML2GEOM).cmxa dll$(CAML2GEOM)_stubs.so
+
+test1: test1.ml $(CAML2GEOM).cmi $(CAML2GEOM).cma dll$(CAML2GEOM)_stubs.so
+	$(OCAMLC) -o $@ -dllib `pwd`/dll$(CAML2GEOM)_stubs.so test1.ml
+
+clean:
+	rm -f *.[oa] *.so *.cm[ixoa] *.cmxa
+
+#--------------------------------------------------------------------------
+(*
+  Copyright (c) 2011 Barry Schwartz
+
+  Permission is hereby granted, free of charge, to any person
+  obtaining a copy of this software and associated documentation
+  files (the "Software"), to deal in the Software without
+  restriction, including without limitation the rights to use,
+  copy, modify, merge, publish, distribute, sublicense, and/or sell
+  copies of the Software, and to permit persons to whom the
+  Software is furnished to do so, subject to the following
+  conditions:
+
+  The above copyright notice and this permission notice shall be
+  included in all copies or substantial portions of the Software.
+
+  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+  OTHER DEALINGS IN THE SOFTWARE.
+*)
+
+module Coord =
+struct
+  type t = float
+end
+
+module Point =
+struct
+  type t
+  external make_at_origin : unit -> t = "new_point_at_origin_wrapper"
+  external make : Coord.t -> Coord.t -> t = "new_point_wrapper"
+  external copy : t -> t = "copy_point_wrapper"
+  external coord : t -> int -> Coord.t = "point_coord_wrapper"
+  external set_coord : t -> int -> Coord.t -> unit = "set_point_coord_wrapper"
+  external length : t -> Coord.t = "point_length_wrapper"
+  external normalize : t -> unit = "point_normalize_wrapper"
+  external ccw : t -> t = "point_ccw_wrapper"
+  external cw : t -> t = "point_cw_wrapper"
+end
+(*
+  Copyright (c) 2011 Barry Schwartz
+
+  Permission is hereby granted, free of charge, to any person
+  obtaining a copy of this software and associated documentation
+  files (the "Software"), to deal in the Software without
+  restriction, including without limitation the rights to use,
+  copy, modify, merge, publish, distribute, sublicense, and/or sell
+  copies of the Software, and to permit persons to whom the
+  Software is furnished to do so, subject to the following
+  conditions:
+
+  The above copyright notice and this permission notice shall be
+  included in all copies or substantial portions of the Software.
+
+  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+  OTHER DEALINGS IN THE SOFTWARE.
+*)
+
+module Coord :
+sig
+  type t = float
+end
+
+module Point :
+sig
+  type t
+  external make_at_origin : unit -> t = "new_point_at_origin_wrapper"
+  external make : Coord.t -> Coord.t -> t = "new_point_wrapper"
+  external copy : t -> t = "copy_point_wrapper"
+  external coord : t -> int -> Coord.t = "point_coord_wrapper"
+  external set_coord : t -> int -> Coord.t -> unit = "set_point_coord_wrapper"
+  external length : t -> Coord.t = "point_length_wrapper"
+  external normalize : t -> unit = "point_normalize_wrapper"
+  external ccw : t -> t = "point_ccw_wrapper"
+  external cw : t -> t = "point_cw_wrapper"
+end

caml2geom_stubs.c

+/*
+  Copyright (c) 2011 Barry Schwartz
+
+  Permission is hereby granted, free of charge, to any person
+  obtaining a copy of this software and associated documentation
+  files (the "Software"), to deal in the Software without
+  restriction, including without limitation the rights to use,
+  copy, modify, merge, publish, distribute, sublicense, and/or sell
+  copies of the Software, and to permit persons to whom the
+  Software is furnished to do so, subject to the following
+  conditions:
+
+  The above copyright notice and this permission notice shall be
+  included in all copies or substantial portions of the Software.
+
+  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+  OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+//-------------------------------------------------------------------------
+
+#include <2geom/coord.h>
+#include <2geom/point.h>
+
+extern "C" {
+#include <string.h>
+#include <caml/alloc.h>
+#include <caml/custom.h>
+#include <caml/fail.h>
+#include <caml/memory.h>
+#include <caml/mlvalues.h>
+}
+
+//-------------------------------------------------------------------------
+
+inline Geom::Point point_from_ml_floats(value _x, value _y)
+{
+    return Geom::Point(Double_val(_x), Double_val(_y));
+}
+
+static struct custom_operations point_ops = {
+    (char *) "lib2geom.Point",
+    custom_finalize_default,
+    custom_compare_default,
+    custom_hash_default,
+    custom_serialize_default,
+    custom_deserialize_default
+};
+
+//-------------------------------------------------------------------------
+
+extern "C" CAMLprim value new_point_at_origin_wrapper(void)
+{
+    CAMLparam0();
+    CAMLlocal1(_p);
+    _p = caml_alloc_custom(&point_ops, sizeof(Geom::Point), 0, 1);
+    *(Geom::Point *) Data_custom_val(_p) = Geom::Point();
+    CAMLreturn(_p);
+}
+
+extern "C" CAMLprim value new_point_wrapper(value _x, value _y)
+{
+    CAMLparam2(_x, _y);
+    CAMLlocal1(_p);
+    _p = caml_alloc_custom(&point_ops, sizeof(Geom::Point), 0, 1);
+    *(Geom::Point *) Data_custom_val(_p) = point_from_ml_floats(_x, _y);
+    CAMLreturn(_p);
+}
+
+extern "C" CAMLprim value copy_point_wrapper(value _p)
+{
+    CAMLparam1(_p);
+    CAMLlocal1(_p_new);
+    _p_new = caml_alloc_custom(&point_ops, sizeof(Geom::Point), 0, 1);
+    *(Geom::Point *) Data_custom_val(_p_new) =
+        Geom::Point(*(Geom::Point *) Data_custom_val(_p));
+    CAMLreturn(_p_new);
+}
+
+extern "C" CAMLprim value point_coord_wrapper(value _p, value _i)
+{
+    CAMLparam2(_p, _i);
+    int i = Int_val(_i);
+    if (i < 0 || 1 < i)
+        caml_invalid_argument("Point.coord index out of range");
+    CAMLreturn(caml_copy_double((*(Geom::Point *) Data_custom_val(_p))[i]));
+}
+
+extern "C" CAMLprim value set_point_coord_wrapper(value _p, value _i, value _v)
+{
+    CAMLparam3(_p, _i, _v);
+    int i = Int_val(_i);
+    if (i < 0 || 1 < i)
+        caml_invalid_argument("Point.coord index out of range");
+    (*(Geom::Point *) Data_custom_val(_p))[i] = Double_val(_v);
+    CAMLreturn(Val_unit);
+}
+
+extern "C" CAMLprim value point_length_wrapper(value _p)
+{
+    CAMLparam1(_p);
+    CAMLreturn(caml_copy_double((*(Geom::Point *) Data_custom_val(_p)).length()));
+}
+
+extern "C" CAMLprim value point_normalize_wrapper(value _p)
+{
+    CAMLparam1(_p);
+    (*(Geom::Point *) Data_custom_val(_p)).normalize();
+    CAMLreturn(Val_unit);
+}
+
+extern "C" CAMLprim value point_ccw_wrapper(value _p)
+{
+    CAMLparam1(_p);
+    CAMLlocal1(_p_new);
+    _p_new = caml_alloc_custom(&point_ops, sizeof(Geom::Point), 0, 1);
+    *(Geom::Point *) Data_custom_val(_p_new) =
+        (*(Geom::Point *) Data_custom_val(_p)).ccw();
+    CAMLreturn(_p_new);
+}
+
+extern "C" CAMLprim value point_cw_wrapper(value _p)
+{
+    CAMLparam1(_p);
+    CAMLlocal1(_p_new);
+    _p_new = caml_alloc_custom(&point_ops, sizeof(Geom::Point), 0, 1);
+    *(Geom::Point *) Data_custom_val(_p_new) =
+        (*(Geom::Point *) Data_custom_val(_p)).cw();
+    CAMLreturn(_p_new);
+}
+
+//-------------------------------------------------------------------------
+open Caml2geom
+
+let p = Point.copy (Point.make_at_origin ()) ;;
+Point.set_coord p 1 50. ;;
+Printf.printf "(%f,%f) %f\n" (Point.coord p 0) (Point.coord (Point.copy p) 1) (Point.length p);;
+Point.set_coord p 0 10. ;;
+Printf.printf "(%f,%f) %f\n" (Point.coord p 0) (Point.coord (Point.copy p) 1) (Point.length p);;
+Point.normalize p ;;
+Printf.printf "(%f,%f) %f\n" (Point.coord p 0) (Point.coord (Point.copy p) 1) (Point.length p);;
+let p = Point.ccw p ;;
+Printf.printf "(%f,%f) %f\n" (Point.coord p 0) (Point.coord (Point.copy p) 1) (Point.length p);;
+let p = Point.cw p ;;
+Printf.printf "(%f,%f) %f\n" (Point.coord p 0) (Point.coord (Point.copy p) 1) (Point.length p);;