Source

caml2geom / caml2geom_stubs.c

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
  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/bezier.h>
#include <2geom/bezier-curve.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>
}

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

#define OPAQUE_P(t, name) (*(t**)Data_custom_val((name)))
#define OPAQUE_P_ALLOC(t, ops) caml_alloc_custom(&(ops), sizeof(t), 0, 1);

inline Geom::Translate *translate_from_ml_floats(value _x, value _y)
{
    return new Geom::Translate(Double_val(_x), Double_val(_y));
}

void finalize_translate(value _v)
{
    delete OPAQUE_P(Geom::Translate, _v);
}

static struct custom_operations translate_ops = {
    (char *) "lib2geom.Translate",
    finalize_translate,
    custom_compare_default,
    custom_hash_default,
    custom_serialize_default,
    custom_deserialize_default
};

inline Geom::Point *point_from_ml_floats(value _x, value _y)
{
    return new Geom::Point(Double_val(_x), Double_val(_y));
}

void finalize_point(value _v)
{
    delete OPAQUE_P(Geom::Point, _v);
}

static struct custom_operations point_ops = {
    (char *) "lib2geom.Point",
    finalize_point,
    custom_compare_default,
    custom_hash_default,
    custom_serialize_default,
    custom_deserialize_default
};

void finalize_bezier(value _v)
{
    delete OPAQUE_P(Geom::Bezier, _v);
}

static struct custom_operations bezier_ops = {
    (char *) "lib2geom.Bezier",
    finalize_bezier,
    custom_compare_default,
    custom_hash_default,
    custom_serialize_default,
    custom_deserialize_default
};

void finalize_bezier_curve(value _v)
{
    delete OPAQUE_P(Geom::BezierCurve, _v);
}

static struct custom_operations bezier_curve_ops = {
    (char *) "lib2geom.Bezier_curve",
    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 = OPAQUE_P_ALLOC(Geom::Translate, translate_ops);
    OPAQUE_P(Geom::Translate, _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 = OPAQUE_P_ALLOC(Geom::Translate, translate_ops);
    OPAQUE_P(Geom::Translate, _t) = new Geom::Translate(x, y);
    CAMLreturn(_t);
}

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

extern "C" CAMLprim value new_default_point_wrapper(void)
{
    CAMLparam0();
    CAMLlocal1(_p);
    _p = OPAQUE_P_ALLOC(Geom::Point, point_ops);
    OPAQUE_P(Geom::Point, _p) = new Geom::Point();
    CAMLreturn(_p);
}

extern "C" CAMLprim value new_point_wrapper(value _x, value _y)
{
    CAMLparam2(_x, _y);
    CAMLlocal1(_p);
    _p = OPAQUE_P_ALLOC(Geom::Point, point_ops);
    OPAQUE_P(Geom::Point, _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 = OPAQUE_P_ALLOC(Geom::Point, point_ops);
    OPAQUE_P(Geom::Point, _p_new) = new Geom::Point(*OPAQUE_P(Geom::Point, _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((*OPAQUE_P(Geom::Point, _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");
    (*OPAQUE_P(Geom::Point, _p))[i] = Double_val(_v);
    CAMLreturn(Val_unit);
}

extern "C" CAMLprim value point_length_wrapper(value _p)
{
    CAMLparam1(_p);
    CAMLreturn(caml_copy_double(OPAQUE_P(Geom::Point, _p)->length()));
}

extern "C" CAMLprim value point_normalize_wrapper(value _p)
{
    CAMLparam1(_p);
    OPAQUE_P(Geom::Point, _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 = OPAQUE_P_ALLOC(Geom::Point, point_ops);                \
        OPAQUE_P(Geom::Point, _p_new) =                                 \
            new Geom::Point(OPAQUE_P(Geom::Point, _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);                                         \
        OPAQUE_P(Geom::Point, _p)->op(*OPAQUE_P(Geom::Point, _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);                                         \
        OPAQUE_P(Geom::Point, _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/=);

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

extern "C" CAMLprim value new_default_bezier_wrapper(void)
{
    CAMLparam0();
    CAMLlocal1(_bez);
    _bez = OPAQUE_P_ALLOC(Geom::Bezier, bezier_ops);
    OPAQUE_P(Geom::Bezier, _bez) = new Geom::Bezier();
    CAMLreturn(_bez);
}

extern "C" CAMLprim value new_bezier_order0_wrapper(value _c0)
{
    CAMLparam1(_c0);
    CAMLlocal1(_bez);
    _bez = OPAQUE_P_ALLOC(Geom::Bezier, bezier_ops);
    OPAQUE_P(Geom::Bezier, _bez) = new Geom::Bezier(Double_val(_c0));
    CAMLreturn(_bez);
}

extern "C" CAMLprim value new_bezier_order1_wrapper(value _c0, value _c1)
{
    CAMLparam2(_c0, _c1);
    CAMLlocal1(_bez);
    _bez = OPAQUE_P_ALLOC(Geom::Bezier, bezier_ops);
    OPAQUE_P(Geom::Bezier, _bez) = new Geom::Bezier(Double_val(_c0), Double_val(_c1));
    CAMLreturn(_bez);
}

extern "C" CAMLprim value new_bezier_order2_wrapper(value _c0, value _c1, value _c2)
{
    CAMLparam3(_c0, _c1, _c2);
    CAMLlocal1(_bez);
    _bez = OPAQUE_P_ALLOC(Geom::Bezier, bezier_ops);
    OPAQUE_P(Geom::Bezier, _bez) =
        new Geom::Bezier(Double_val(_c0), Double_val(_c1), Double_val(_c2));
    CAMLreturn(_bez);
}

extern "C" CAMLprim value new_bezier_order3_wrapper(value _c0, value _c1, value _c2, value _c3)
{
    CAMLparam4(_c0, _c1, _c2, _c3);
    CAMLlocal1(_bez);
    _bez = OPAQUE_P_ALLOC(Geom::Bezier, bezier_ops);
    OPAQUE_P(Geom::Bezier, _bez) =
        new Geom::Bezier(Double_val(_c0), Double_val(_c1), Double_val(_c2), Double_val(_c3));
    CAMLreturn(_bez);
}

#define bezier_getval_wrapper(name, func, typeconv)                     \
    extern "C" CAMLprim value name(value _bez)                          \
    {                                                                   \
        CAMLparam1(_bez);                                               \
        CAMLreturn(typeconv(OPAQUE_P(Geom::Bezier, _bez)->func())); \
    }

bezier_getval_wrapper(bezier_is_zero_wrapper, isZero, Val_bool);
bezier_getval_wrapper(bezier_is_constant_wrapper, isConstant, Val_bool);
bezier_getval_wrapper(bezier_is_finite_wrapper, isFinite, Val_bool);
bezier_getval_wrapper(bezier_at0_wrapper, at0, caml_copy_double);
bezier_getval_wrapper(bezier_at1_wrapper, at1, caml_copy_double);

extern "C" CAMLprim value bezier_value_at_wrapper(value _bez, value _t)
{
    CAMLparam2(_bez, _t);
    CAMLreturn(caml_copy_double(OPAQUE_P(Geom::Bezier, _bez)->valueAt(Double_val(_t))));
}

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

extern "C" CAMLprim value bezier_curve_of_two_beziers_wrapper(value _bez0, value _bez1)
{
    CAMLparam2(_bez0, _bez1);
    CAMLlocal1(_bezcurve);
    _bezcurve = OPAQUE_P_ALLOC(Geom::BezierCurve, bezier_curve_ops);
    *(Geom::BezierCurve**)Data_custom_val(_bezcurve) =
        new Geom::BezierCurve(*(Geom::Bezier*)Data_custom_val(_bez0),
                              *(Geom::Bezier*)Data_custom_val(_bez1));
    CAMLreturn(_bezcurve);
}

extern "C" CAMLprim value bezier_curve_of_two_points_wrapper(value _p0, value _p1)
{
    CAMLparam2(_p0, _p1);
    CAMLlocal1(_bezcurve);
    _bezcurve = OPAQUE_P_ALLOC(Geom::BezierCurve, bezier_curve_ops);
    *(Geom::BezierCurve**)Data_custom_val(_bezcurve) =
        new Geom::BezierCurve(*(Geom::Point*)Data_custom_val(_p0),
                              *(Geom::Point*)Data_custom_val(_p1));
    CAMLreturn(_bezcurve);
}

extern "C" CAMLprim value bezier_curve_of_three_points_wrapper(value _p0, value _p1, value _p2)
{
    CAMLparam3(_p0, _p1, _p2);
    CAMLlocal1(_bezcurve);
    _bezcurve = OPAQUE_P_ALLOC(Geom::BezierCurve, bezier_curve_ops);
    *(Geom::BezierCurve**)Data_custom_val(_bezcurve) =
        new Geom::BezierCurve(*(Geom::Point*)Data_custom_val(_p0),
                              *(Geom::Point*)Data_custom_val(_p1),
                              *(Geom::Point*)Data_custom_val(_p2));
    CAMLreturn(_bezcurve);
}

extern "C" CAMLprim value bezier_curve_of_four_points_wrapper(value _p0, value _p1, value _p2, value _p3)
{
    CAMLparam4(_p0, _p1, _p2, _p3);
    CAMLlocal1(_bezcurve);
    _bezcurve = OPAQUE_P_ALLOC(Geom::BezierCurve, bezier_curve_ops);
    *(Geom::BezierCurve**)Data_custom_val(_bezcurve) =
        new Geom::BezierCurve(*(Geom::Point*)Data_custom_val(_p0),
                              *(Geom::Point*)Data_custom_val(_p1),
                              *(Geom::Point*)Data_custom_val(_p2),
                              *(Geom::Point*)Data_custom_val(_p3));
    CAMLreturn(_bezcurve);
}

extern "C" CAMLprim value bezier_curve_initial_point_wrapper(value _bezcurve)
{
    CAMLparam1(_bezcurve);
    CAMLlocal1(_p);
    _p = OPAQUE_P_ALLOC(Geom::Point, point_ops);
    *(Geom::Point*)Data_custom_val(_p) =
        (*(Geom::BezierCurve**)Data_custom_val(_bezcurve))->initialPoint();
    CAMLreturn(_p);
}

extern "C" CAMLprim value bezier_curve_final_point_wrapper(value _bezcurve)
{
    CAMLparam1(_bezcurve);
    CAMLlocal1(_p);
    _p = OPAQUE_P_ALLOC(Geom::Point, point_ops);
    *(Geom::Point*)Data_custom_val(_p) = OPAQUE_P(Geom::BezierCurve, _bezcurve)->finalPoint();
    CAMLreturn(_p);
}

//-------------------------------------------------------------------------
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.