Source

caml2geom / 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/affine.h>
#include <2geom/coord.h>
#include <2geom/point.h>
#include <2geom/transforms.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::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));
}

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 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();
    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);
}

#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);                                             \
    }

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/=);

//-------------------------------------------------------------------------