Barry Schwartz avatar Barry Schwartz committed 07a92d3

Checking in before beginning a split into separate compilation units (mainly to help me keep organized).

Comments (0)

Files changed (5)

 
 CAML2GEOM = caml2geom
 PKGCONFIG = pkg-config
-OCAMLC = ocamlfind c -g
-OCAMLOPT = ocamlfind opt -g
+#OCAMLC = ocamlfind c -g
+#OCAMLOPT = ocamlfind opt -g
+OCAMLC = ocamlc -g
+OCAMLOPT = ocamlopt -g
 OCAMLMKLIB = ocamlmklib
-CXXFLAGS = -g -fPIC
+CXXFLAGS = -g -fPIC -Wall -W
 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)
+%.cma: %.cmo
+	$(OCAMLMKLIB) -o $* $< $(LDFLAGS)
+
+%.cmxa: %.cmx
+	$(OCAMLMKLIB) -o $* $< $(LDFLAGS)
+
+dll%.so lib%.a: %_stubs.o
+	$(OCAMLMKLIB) -o $* $< $(LDFLAGS)
 
 #--------------------------------------------------------------------------
 
-default: $(CAML2GEOM).cmi $(CAML2GEOM).cma $(CAML2GEOM).cmxa dll$(CAML2GEOM)_stubs.so
+default: $(CAML2GEOM).cmi $(CAML2GEOM).cma $(CAML2GEOM).cmxa dll$(CAML2GEOM).so
 
-test1: test1.ml $(CAML2GEOM).cmi $(CAML2GEOM).cma dll$(CAML2GEOM)_stubs.so
-	$(OCAMLC) -o $@ -dllib `pwd`/dll$(CAML2GEOM)_stubs.so test1.ml
+test1: test1.ml $(CAML2GEOM).cmi $(CAML2GEOM).cmo dll$(CAML2GEOM).so
+	$(OCAMLC) -o $@ -dllib `pwd`/dll$(CAML2GEOM).so $(CAML2GEOM).cmo test1.ml
+
+#test1: test1.ml $(CAML2GEOM).cmi $(CAML2GEOM).cma dll$(CAML2GEOM).so
+#	$(OCAMLC) -o $@ -dllib dll$(CAML2GEOM) $(CAML2GEOM).cma test1.ml
 
 clean:
-	rm -f *.[oa] *.so *.cm[ixoa] *.cmxa
+	rm -f *.[oa] *.so *.cm[ixoa] *.cmxa test1
 
 #--------------------------------------------------------------------------
 module Coord =
 struct
   type t = float
+  external _get_epsilon : unit -> t = "epsilon_wrapper"
+  let _eps_value = _get_epsilon ()
+  let epsilon = _eps_value
+  external infinity : unit -> t = "infinity_wrapper"
+  external are_near : t -> t -> bool = "are_near_wrapper"
+  external are_near_eps : t -> t -> t -> bool = "are_near_eps_wrapper"
+  external rel_error_bound : t -> t -> bool = "rel_error_bound_wrapper"
+  external rel_error_bound_eps : t -> t -> t -> bool = "rel_error_bound_eps_wrapper"
 end
 
-module Point =
+module IntCoord =
 struct
+  type t = int
+end
+
+module D2 =
+struct
+  module type S =
+  sig
+    type elt
+    type t
+    val make_at_origin : unit -> t
+    val make : elt -> elt -> t
+    val copy : t -> t
+    val coord : t -> int -> elt
+    val set_coord : t -> int -> elt -> unit
+    val length : t -> elt
+    val normalize : t -> unit
+    val ccw : t -> t
+    val cw : t -> t
+    val neg : t -> t
+    val set : t -> t -> unit
+    val add_to : t -> t -> unit
+    val sub_from : t -> t -> unit
+    val mul_by : t -> elt -> unit
+    val div_by : t -> elt -> unit
+  end
+end
+
+module Point : D2.S with type elt = Coord.t =
+struct
+  type elt = Coord.t
   type t
   external make_at_origin : unit -> t = "new_point_at_origin_wrapper"
-  external make : Coord.t -> Coord.t -> t = "new_point_wrapper"
+  external make : elt -> elt -> 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 coord : t -> int -> elt = "point_coord_wrapper"
+  external set_coord : t -> int -> elt -> unit = "set_point_coord_wrapper"
+  external length : t -> elt = "point_length_wrapper"
   external normalize : t -> unit = "point_normalize_wrapper"
   external ccw : t -> t = "point_ccw_wrapper"
   external cw : t -> t = "point_cw_wrapper"
+  external neg : t -> t = "point_neg_wrapper"
+  external set : t -> t -> unit = "point_set_wrapper"
+  external add_to : t -> t -> unit = "point_add_to_wrapper"
+  external sub_from : t -> t -> unit = "point_sub_from_wrapper"
+  external mul_by : t -> elt -> unit = "point_mul_by_wrapper"
+  external div_by : t -> elt -> unit = "point_div_by_wrapper"
 end
 module Coord :
 sig
   type t = float
+  val epsilon : t
+  external infinity : unit -> t = "infinity_wrapper"
+  external are_near : t -> t -> bool = "are_near_wrapper"
+  external are_near_eps : t -> t -> t -> bool = "are_near_eps_wrapper"
+  external rel_error_bound : t -> t -> bool = "rel_error_bound_wrapper"
+  external rel_error_bound_eps : t -> t -> t -> bool = "rel_error_bound_eps_wrapper"
 end
 
-module Point :
+module IntCoord :
 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"
+  type t = int
 end
+
+
+module D2 :
+sig
+  module type S =
+  sig
+    type elt
+    type t
+    val make_at_origin : unit -> t
+    val make : elt -> elt -> t
+    val copy : t -> t
+    val coord : t -> int -> elt
+    val set_coord : t -> int -> elt -> unit
+    val length : t -> elt
+    val normalize : t -> unit
+    val ccw : t -> t
+    val cw : t -> t
+    val neg : t -> t
+    val set : t -> t -> unit
+    val add_to : t -> t -> unit
+    val sub_from : t -> t -> unit
+    val mul_by : t -> elt -> unit
+    val div_by : t -> elt -> unit
+  end
+end
+
+module Point : D2.S with type elt = Coord.t

caml2geom_stubs.c

 
 //-------------------------------------------------------------------------
 
+#include <2geom/affine.h>
 #include <2geom/coord.h>
 #include <2geom/point.h>
+#include <2geom/transforms.h>
 
 extern "C" {
 #include <string.h>
 
 //-------------------------------------------------------------------------
 
+inline Geom::Translate translate_from_ml_floats(value _x, value _y)
+{
+    return Geom::Translate(Double_val(_x), Double_val(_y));
+}
+
+static struct custom_operations translate_ops = {
+    (char *) "lib2geom.Translate",
+    custom_finalize_default,
+    custom_compare_default,
+    custom_hash_default,
+    custom_serialize_default,
+    custom_deserialize_default
+};
+
 inline Geom::Point point_from_ml_floats(value _x, value _y)
 {
     return Geom::Point(Double_val(_x), Double_val(_y));
 
 //-------------------------------------------------------------------------
 
+extern "C" CAMLprim value epsilon_wrapper(void)
+{
+    CAMLparam0();
+    CAMLreturn(caml_copy_double(Geom::EPSILON));
+}
+
+extern "C" CAMLprim value infinity_wrapper(void)
+{
+    CAMLparam0();
+    CAMLreturn(caml_copy_double(Geom::infinity()));
+}
+
+extern "C" CAMLprim value are_near_wrapper(value _a, value _b)
+{
+    CAMLparam2(_a, _b);
+    CAMLreturn(Val_bool(Geom::are_near(Double_val(_a), Double_val(_b))));
+}
+
+extern "C" CAMLprim value are_near_eps_wrapper(value _a, value _b, value _eps)
+{
+    CAMLparam3(_a, _b, _eps);
+    CAMLreturn(Val_bool(Geom::are_near(Double_val(_a), Double_val(_b), Double_val(_eps))));
+}
+
+extern "C" CAMLprim value rel_error_bound_wrapper(value _a, value _b)
+{
+    CAMLparam2(_a, _b);
+    CAMLreturn(Val_bool(Geom::rel_error_bound(Double_val(_a), Double_val(_b))));
+}
+
+extern "C" CAMLprim value rel_error_bound_eps_wrapper(value _a, value _b, value _eps)
+{
+    CAMLparam3(_a, _b, _eps);
+    CAMLreturn(Val_bool(Geom::rel_error_bound(Double_val(_a), Double_val(_b), Double_val(_eps))));
+}
+
+//-------------------------------------------------------------------------
+
+extern "C" CAMLprim value new_translate_wrapper(value _x, value _y)
+{
+    CAMLparam2(_x, _y);
+    CAMLlocal1(_t);
+    _t = caml_alloc_custom(&translate_ops, sizeof(Geom::Translate), 0, 1);
+    *(Geom::Translate *)Data_custom_val(_t) = translate_from_ml_floats(_x, _y);
+    CAMLreturn(_t);
+}
+
+ extern "C" CAMLprim value translate_of_point_wrapper(value _p)
+{
+    CAMLparam1(_p);
+    CAMLlocal1(_t);
+    double x = Geom::Point(*(Geom::Point *)Data_custom_val(_p))[0];
+    double y = Geom::Point(*(Geom::Point *)Data_custom_val(_p))[1];
+    _t = caml_alloc_custom(&translate_ops, sizeof(Geom::Translate), 0, 1);
+    *(Geom::Translate *)Data_custom_val(_t) = Geom::Translate(x, y);
+    CAMLreturn(_t);
+}
+
+//-------------------------------------------------------------------------
+
 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();
+    *(Geom::Point *)Data_custom_val(_p) = Geom::Point();
     CAMLreturn(_p);
 }
 
     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);
+    *(Geom::Point *)Data_custom_val(_p) = point_from_ml_floats(_x, _y);
     CAMLreturn(_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));
+    *(Geom::Point *)Data_custom_val(_p_new) =
+        Geom::Point(*(Geom::Point *)Data_custom_val(_p));
     CAMLreturn(_p_new);
 }
 
     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]));
+    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)
     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);
+    (*(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()));
+    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();
+    (*(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);
-}
+#define unary_point_op(name,op)                                         \
+    extern "C" CAMLprim value name(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)).op();                 \
+        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);
-}
+unary_point_op(point_ccw_wrapper, ccw);
+unary_point_op(point_cw_wrapper, cw);
+unary_point_op(point_neg_wrapper, operator-);
+
+#define point_assign_point_op(name,op)                                  \
+    extern "C" CAMLprim value name(value _p, value _other)              \
+    {                                                                   \
+        CAMLparam2(_p, _other);                                         \
+        (*(Geom::Point *)Data_custom_val(_p)).op(*(Geom::Point *)Data_custom_val(_other)); \
+        CAMLreturn(Val_unit);                                           \
+    }
+
+point_assign_point_op(point_set_wrapper, operator=);
+point_assign_point_op(point_add_to_wrapper, operator+=);
+point_assign_point_op(point_sub_from_wrapper, operator-=);
+
+#define point_assign_double_op(name,op)                                 \
+    extern "C" CAMLprim value name(value _p, value _other)              \
+    {                                                                   \
+        CAMLparam2(_p, _other);                                         \
+        (*(Geom::Point *)Data_custom_val(_p)).op(Double_val(_other));   \
+        CAMLreturn(Val_unit);                                           \
+    }
+
+point_assign_double_op(point_mul_by_wrapper, operator*=);
+point_assign_double_op(point_div_by_wrapper, operator/=);
 
 //-------------------------------------------------------------------------
 open Caml2geom
+open Printf
+
+(*-----------------------------------------------------------------------*)
+
+let _ = printf "Coord.epsilon = %f\n" (Coord.epsilon) ;;
+let _ = printf "Coord.infinity () = %f\n" (Coord.infinity ()) ;;
+let _ = printf "Coord.are_near 4.00 4.09 = %B\n" (Coord.are_near 4.00 4.09) ;;
+let _ = printf "Coord.are_near_eps 0.1 4.00 4.09 = %B\n" (Coord.are_near_eps 0.1 4.00 4.09);;
+let _ = printf "Coord.rel_error_bound 4.00 4.09 = %B\n" (Coord.rel_error_bound 4.00 4.09) ;;
+let _ = printf "Coord.rel_error_bound_eps 0.1 4.00 4.09 = %B\n" (Coord.rel_error_bound_eps 0.1 4.00 4.09);;
+
+(*-----------------------------------------------------------------------*)
 
 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);;
+let _ = 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);;
+let _ = 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 _ = 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 _ = 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);;
+let _ = printf "(%f,%f) %f\n" (Point.coord p 0) (Point.coord (Point.copy p) 1) (Point.length p);;
+let p = Point.neg p ;;
+let _ = printf "(%f,%f) %f\n" (Point.coord p 0) (Point.coord (Point.copy p) 1) (Point.length p);;
+Point.add_to p (Point.make 100. 100.);;
+let _ = printf "(%f,%f) %f\n" (Point.coord p 0) (Point.coord (Point.copy p) 1) (Point.length p);;
+Point.sub_from p (Point.make 100. 100.);;
+let _ = printf "(%f,%f) %f\n" (Point.coord p 0) (Point.coord (Point.copy p) 1) (Point.length p);;
+Point.mul_by p 1000.;;
+let _ = printf "(%f,%f) %f\n" (Point.coord p 0) (Point.coord (Point.copy p) 1) (Point.length p);;
+Point.div_by p 1000.;;
+let _ = printf "(%f,%f) %f\n" (Point.coord p 0) (Point.coord (Point.copy p) 1) (Point.length p);;
+
+let q = Point.make_at_origin ();;
+let _ = printf "(%f,%f) %f\n" (Point.coord q 0) (Point.coord q 1) (Point.length q);;
+Point.set q (Point.make (-50.) 50.);;
+let _ = printf "(%f,%f) %f\n" (Point.coord q 0) (Point.coord q 1) (Point.length q);;
+
+(*-----------------------------------------------------------------------*)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.