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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
/*
  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/exception.h>
#include <2geom/interval.h>
#include <2geom/path.h>
#include <2geom/point.h>
#include <2geom/rect.h>
#include <2geom/sbasis-to-bezier.h>
#include <2geom/transforms.h>

extern "C" {
#include <stdio.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);

#define CUSTOM_OPS(t, name)                             \
    void finalize_##t(value _v)                         \
    {                                                   \
        delete OPAQUE_P(Geom::t, _v);                   \
    }                                                   \
    static struct custom_operations name = {            \
        (char *) "Lib2geom." #t,                        \
        finalize_##t,                                   \
        custom_compare_default,                         \
        custom_hash_default,                            \
        custom_serialize_default,                       \
        custom_deserialize_default                      \
    };

// We know that Geom::X == 0 and Geom::Y == 1, but let's ignore that
// fact and write our code this safer way that avoids casting int to
// Geom::Dim2.
Geom::Dim2 dim2[2] = { Geom::X, Geom::Y };

// Similarly for enum Geom::Path::Stitching.
enum Geom::Path::Stitching stitching[2] = { Geom::Path::NO_STITCHING,
                                            Geom::Path::STITCH_DISCONTINUOUS };

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

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

CUSTOM_OPS(Translate, translate_ops);
CUSTOM_OPS(Point, point_ops);
CUSTOM_OPS(Interval, interval_ops);
CUSTOM_OPS(Rect, rect_ops);
CUSTOM_OPS(Bezier, bezier_ops);
CUSTOM_OPS(Curve, curve_ops);
CUSTOM_OPS(BezierCurve, bezier_curve_ops);
CUSTOM_OPS(LineSegment, line_segment_ops);
CUSTOM_OPS(QuadraticBezier, quadratic_bezier_ops);
CUSTOM_OPS(CubicBezier, cubic_bezier_ops);
CUSTOM_OPS(Path, path_ops);

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

#define getval_wrapper(t, name, func, typeconv)                         \
    extern "C" CAMLprim value name(value _v)                            \
    {                                                                   \
        CAMLparam1(_v);                                                 \
        CAMLreturn(typeconv(OPAQUE_P(t, _v)->func()));                  \
    }

#define setval_wrapper(t, name, func, typeconv)                         \
    extern "C" CAMLprim value name(value _v, value _setting)            \
    {                                                                   \
        CAMLparam2(_v, _setting);                                       \
        OPAQUE_P(t, _v)->func(typeconv(_setting));                      \
        CAMLreturn(Val_unit);                                           \
    }

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

#define fragment_wrappers(t, name)                                      \
                                                                        \
    extern "C" CAMLprim value new_default_##name##_wrapper(void)        \
    {                                                                   \
        CAMLparam0();                                                   \
        CAMLlocal1(_v);                                                 \
        _v = OPAQUE_P_ALLOC(t, name##_ops);                             \
        OPAQUE_P(t, _v) = new t();                                      \
        CAMLreturn(_v);                                                 \
    }                                                                   \
                                                                        \
    getval_wrapper(t, name##_is_zero_wrapper, isZero, Val_bool);        \
    getval_wrapper(t, name##_is_constant_wrapper, isConstant, Val_bool); \
    getval_wrapper(t, name##_is_finite_wrapper, isFinite, Val_bool);    \
    getval_wrapper(t, name##_at0_wrapper, at0, caml_copy_double);       \
    getval_wrapper(t, name##_at1_wrapper, at1, caml_copy_double);       \
                                                                        \
    extern "C" CAMLprim value name##_value_at_wrapper(value _v, value _s) \
    {                                                                   \
        CAMLparam2(_v, _s);                                             \
        CAMLreturn(caml_copy_double(OPAQUE_P(t, _v)->valueAt(Double_val(_s)))); \
    }

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

#define curve_wrappers(t, name)                                         \
                                                                        \
    extern "C" CAMLprim value name##_initial_point_wrapper(value _curve) \
    {                                                                   \
        CAMLparam1(_curve);                                             \
        CAMLlocal1(_p);                                                 \
        t *curve = OPAQUE_P(t, _curve);                                 \
        Geom::Point pt = curve->initialPoint();                         \
        _p = OPAQUE_P_ALLOC(Geom::Point, point_ops);                    \
        OPAQUE_P(Geom::Point, _p) = new Geom::Point(pt);                \
        CAMLreturn(_p);                                                 \
    }                                                                   \
                                                                        \
    extern "C" CAMLprim value name##_final_point_wrapper(value _curve)  \
    {                                                                   \
        CAMLparam1(_curve);                                             \
        CAMLlocal1(_p);                                                 \
        t *curve = OPAQUE_P(t, _curve);                                 \
        Geom::Point pt = curve->finalPoint();                           \
        _p = OPAQUE_P_ALLOC(Geom::Point, point_ops);                    \
        OPAQUE_P(Geom::Point, _p) = new Geom::Point(pt);                \
        CAMLreturn(_p);                                                 \
    }                                                                   \
                                                                        \
    extern "C" CAMLprim value name##_is_degenerate_wrapper(value _curve) \
    {                                                                   \
        CAMLparam1(_curve);                                             \
        t *curve = OPAQUE_P(t, _curve);                                 \
        CAMLreturn(Val_bool(curve->isDegenerate()));                    \
    }                                                                   \
                                                                        \
    extern "C" CAMLprim value name##_point_at_wrapper(value _curve, value _t) \
    {                                                                   \
        CAMLparam2(_curve, _t);                                         \
        CAMLlocal1(_p);                                                 \
        t *curve = OPAQUE_P(t, _curve);                                 \
        Geom::Point pt = curve->pointAt(Double_val(_t));                \
        _p = OPAQUE_P_ALLOC(Geom::Point, point_ops);                    \
        OPAQUE_P(Geom::Point, _p) = new Geom::Point(pt);                \
        CAMLreturn(_p);                                                 \
    }                                                                   \
                                                                        \
    extern "C" CAMLprim value name##_value_at_wrapper(value _curve, value _t, value _dim2) \
    {                                                                   \
        CAMLparam3(_curve, _dim2, _t);                                  \
        t *curve = OPAQUE_P(t, _curve);                                 \
        CAMLreturn(caml_copy_double(curve->valueAt(Double_val(_t), dim2[Int_val(_dim2)]))); \
    }                                                                   \
                                                                        \
    extern "C" CAMLprim value name##_point_and_derivatives_wrapper(value _curve, value _t, value _n) \
    {                                                                   \
        CAMLparam3(_curve, _t, _n);                                     \
        CAMLlocal2(_p_array, _p);                                       \
        t *curve = OPAQUE_P(t, _curve);                                 \
        double s = Double_val(_t);                                      \
        int n = Int_val(_n);                                            \
        std::vector<Geom::Point> p = curve->pointAndDerivatives(s, n);  \
        _p_array = caml_alloc(p.size(), 0);                             \
        for (unsigned int i = 0; i < p.size(); i++) {                   \
            _p = OPAQUE_P_ALLOC(Geom::Point, point_ops);                \
            OPAQUE_P(Geom::Point, _p) = new Geom::Point(p[i]);          \
            Store_field(_p_array, i, _p);                               \
        }                                                               \
        CAMLreturn(_p_array);                                           \
    }                                                                   \
                                                                        \
    extern "C" CAMLprim value name##_set_initial_wrapper(value _curve, value _p) \
    {                                                                   \
        CAMLparam2(_curve, _p);                                         \
        t *curve = OPAQUE_P(t, _curve);                                 \
        curve->setInitial(*OPAQUE_P(Geom::Point, _p));                  \
        CAMLreturn(Val_unit);                                           \
    }                                                                   \
                                                                        \
    extern "C" CAMLprim value name##_set_final_wrapper(value _curve, value _p) \
    {                                                                   \
        CAMLparam2(_curve, _p);                                         \
        t *curve = OPAQUE_P(t, _curve);                                 \
        curve->setFinal(*OPAQUE_P(Geom::Point, _p));                    \
        CAMLreturn(Val_unit);                                           \
    }                                                                   \
                                                                        \
    extern "C" CAMLprim value name##_bounds_fast_wrapper(value _curve)  \
    {                                                                   \
        CAMLparam1(_curve);                                             \
        CAMLlocal1(_rect);                                              \
        t *curve = OPAQUE_P(t, _curve);                                 \
        Geom::Rect rect = curve->boundsFast();                          \
        _rect = OPAQUE_P_ALLOC(Geom::Rect, rect_ops);                   \
        OPAQUE_P(Geom::Rect, _rect) = new Geom::Rect(rect);             \
        CAMLreturn(_rect);                                              \
    }                                                                   \
                                                                        \
    extern "C" CAMLprim value name##_bounds_exact_wrapper(value _curve) \
    {                                                                   \
        CAMLparam1(_curve);                                             \
        CAMLlocal1(_rect);                                              \
        t *curve = OPAQUE_P(t, _curve);                                 \
        Geom::Rect rect = curve->boundsExact();                         \
        _rect = OPAQUE_P_ALLOC(Geom::Rect, rect_ops);                   \
        OPAQUE_P(Geom::Rect, _rect) = new Geom::Rect(rect);             \
        CAMLreturn(_rect);                                              \
    }                                                                   \
                                                                        \
    extern "C" CAMLprim value name##_portion_wrapper(value _curve, value _a, value _b) \
    {                                                                   \
        CAMLparam3(_curve, _a, _b);                                     \
        CAMLlocal1(_reverse);                                           \
        t *curve = OPAQUE_P(t, _curve);                                 \
        _reverse = OPAQUE_P_ALLOC(t, name##_ops);                       \
        OPAQUE_P(t, _reverse) =                                         \
            (t*)curve->portion(Double_val(_a), Double_val(_b));         \
        CAMLreturn(_reverse);                                           \
    }                                                                   \
                                                                        \
    extern "C" CAMLprim value name##_reverse_wrapper(value _curve)      \
    {                                                                   \
        CAMLparam1(_curve);                                             \
        CAMLlocal1(_reverse);                                           \
        t *curve = OPAQUE_P(t, _curve);                                 \
        _reverse = OPAQUE_P_ALLOC(t, name##_ops);                       \
        OPAQUE_P(t, _reverse) = (t*)curve->reverse();                   \
        CAMLreturn(_reverse);                                           \
    }                                                                   \
                                                                        \
    extern "C" CAMLprim value name##_derivative_wrapper(value _curve)   \
    {                                                                   \
        CAMLparam1(_curve);                                             \
        CAMLlocal1(_derivative);                                        \
        t *curve = OPAQUE_P(t, _curve);                                 \
        _derivative = OPAQUE_P_ALLOC(t, name##_ops);                    \
        OPAQUE_P(t, _derivative) = (t*)curve->derivative();             \
        CAMLreturn(_derivative);                                        \
    }                                                                   \
                                                                        \
    extern "C" CAMLprim value name##_length_wrapper(value _curve, value _tolerance) \
    {                                                                   \
        CAMLparam2(_curve, _tolerance);                                 \
        t *curve = OPAQUE_P(t, _curve);                                 \
        CAMLreturn(caml_copy_double(curve->length(Double_val(_tolerance)))); \
    }                                                                   \
                                                                        \
    extern "C" CAMLprim value name##_winding_wrapper(value _curve, value _point) \
    {                                                                   \
        CAMLparam2(_curve, _point);                                     \
        t *curve = OPAQUE_P(t, _curve);                                 \
        CAMLreturn(Int_val(curve->winding(*OPAQUE_P(Geom::Point, _point)))); \
    }                                                                   \
                                                                        \
    extern "C" CAMLprim value name##_unit_tangent_at_wrapper(value _num_derivs, value _curve, value _t) \
    {                                                                   \
        CAMLparam3(_num_derivs, _curve, _t);                            \
        CAMLlocal1(_p);                                                 \
        double time = Double_val(_t);                                   \
        if (time == 1.0) {                                              \
            Geom::Curve *reversed_curve = OPAQUE_P(t, _curve)->reverse(); \
            _p = OPAQUE_P_ALLOC(Geom::Point, point_ops);                \
            if (_num_derivs == Val_int(0)) {                            \
                OPAQUE_P(Geom::Point, _p) = new Geom::Point(-reversed_curve->unitTangentAt(0.0)); \
            } else {                                                    \
                int num_derivs = Int_val(Field(_num_derivs, 0));        \
                OPAQUE_P(Geom::Point, _p) = new Geom::Point(-reversed_curve->unitTangentAt(0.0, num_derivs)); \
            }                                                           \
            delete reversed_curve;                                      \
        } else {                                                        \
            t *curve = OPAQUE_P(t, _curve);                             \
            _p = OPAQUE_P_ALLOC(Geom::Point, point_ops);                \
            if (_num_derivs == Val_int(0)) {                            \
                OPAQUE_P(Geom::Point, _p) = new Geom::Point(curve->unitTangentAt(time)); \
            } else {                                                    \
                int num_derivs = Int_val(Field(_num_derivs, 0));        \
                OPAQUE_P(Geom::Point, _p) = new Geom::Point(curve->unitTangentAt(time, num_derivs)); \
            }                                                           \
        }                                                               \
        CAMLreturn(_p);                                                 \
    }                                                                   \
                                                                        \
    extern "C" CAMLprim value name##_degrees_of_freedom_wrapper(value _curve) \
    {                                                                   \
        CAMLparam1(_curve);                                             \
        t *curve = OPAQUE_P(t, _curve);                                 \
        CAMLreturn(Val_int(curve->degreesOfFreedom()));                 \
    }

#define bezier_curve_wrappers(t, name)                                  \
                                                                        \
    curve_wrappers(t, name);                                            \
    getval_wrapper(t, name##_order_wrapper, order, Val_int);            \
                                                                        \
    extern "C" CAMLprim value name##_points_wrapper(value _bezcurve)    \
    {                                                                   \
        CAMLparam1(_bezcurve);                                          \
        CAMLlocal2(_point, _result);                                    \
        t *bc = OPAQUE_P(t, _bezcurve);                                 \
        std::vector<Geom::Point> points = bc->points();                 \
        int n = points.size();                                          \
        _result = caml_alloc(n, 0);                                     \
        for (int i = 0; i < n; i++) {                                   \
            _point = OPAQUE_P_ALLOC(Geom::Point, point_ops);            \
            OPAQUE_P(Geom::Point, _point) = new Geom::Point(points[i]); \
            Store_field(_result, i, _point);                            \
        }                                                               \
        CAMLreturn(_result);                                            \
    }

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

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);
    Geom::Point *pt = OPAQUE_P(Geom::Point, _p);
    double x = (*pt)[0];
    double y = (*pt)[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_failwith("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_failwith("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_interval_wrapper(void)
{
    CAMLparam0();
    CAMLlocal1(_interval);
    _interval = OPAQUE_P_ALLOC(Geom::Interval, interval_ops);
    OPAQUE_P(Geom::Interval, _interval) = new Geom::Interval();
    CAMLreturn(_interval);
}

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

extern "C" CAMLprim value new_default_rect_wrapper(void)
{
    CAMLparam0();
    CAMLlocal1(_rect);
    _rect = OPAQUE_P_ALLOC(Geom::Rect, rect_ops);
    OPAQUE_P(Geom::Rect, _rect) = new Geom::Rect();
    CAMLreturn(_rect);
}

extern "C" CAMLprim value rect_min_wrapper(value _rect)
{
    CAMLparam1(_rect);
    CAMLlocal1(_point);
    Geom::Rect *rect = OPAQUE_P(Geom::Rect, _rect);
    Geom::Point pt = rect->min();
    _point = OPAQUE_P_ALLOC(Geom::Point, point_ops);
    OPAQUE_P(Geom::Point, _point) = new Geom::Point(pt);
    CAMLreturn(_point);
}

extern "C" CAMLprim value rect_max_wrapper(value _rect)
{
    CAMLparam1(_rect);
    CAMLlocal1(_point);
    Geom::Rect *rect = OPAQUE_P(Geom::Rect, _rect);
    Geom::Point pt = rect->max();
    _point = OPAQUE_P_ALLOC(Geom::Point, point_ops);
    OPAQUE_P(Geom::Point, _point) = new Geom::Point(pt);
    CAMLreturn(_point);
}

extern "C" CAMLprim value rect_corner_wrapper(value _rect, value _i)
{
    CAMLparam2(_rect, _i);
    CAMLlocal1(_point);
    Geom::Rect *rect = OPAQUE_P(Geom::Rect, _rect);
    Geom::Point pt = rect->corner(Int_val(_i));
    _point = OPAQUE_P_ALLOC(Geom::Point, point_ops);
    OPAQUE_P(Geom::Point, _point) = new Geom::Point(pt);
    CAMLreturn(_point);
}

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

fragment_wrappers(Geom::Bezier, bezier);

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

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

curve_wrappers(Geom::Curve, curve);

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

bezier_curve_wrappers(Geom::BezierCurve, bezier_curve);

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);
    OPAQUE_P(Geom::BezierCurve, _bezcurve) =
        new Geom::BezierCurve(*OPAQUE_P(Geom::Bezier, _bez0),
                              *OPAQUE_P(Geom::Bezier, _bez1));
    CAMLreturn(_bezcurve);
}

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

extern "C" CAMLprim value bezier_curve_of_three_points_wrapper(value _p0, value _p1, value _p2)
{
    CAMLparam3(_p0, _p1, _p2);
    CAMLlocal1(_bezcurve);
    Geom::BezierCurve *bc = new Geom::BezierCurve(*OPAQUE_P(Geom::Point, _p0),
                                                  *OPAQUE_P(Geom::Point, _p1),
                                                  *OPAQUE_P(Geom::Point, _p2));
    _bezcurve = OPAQUE_P_ALLOC(Geom::BezierCurve, bezier_curve_ops);
    OPAQUE_P(Geom::BezierCurve, _bezcurve) = bc;
    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);
    Geom::BezierCurve *bc = new Geom::BezierCurve(*OPAQUE_P(Geom::Point, _p0),
                                                  *OPAQUE_P(Geom::Point, _p1),
                                                  *OPAQUE_P(Geom::Point, _p2),
                                                  *OPAQUE_P(Geom::Point, _p3));
    _bezcurve = OPAQUE_P_ALLOC(Geom::BezierCurve, bezier_curve_ops);
    OPAQUE_P(Geom::BezierCurve, _bezcurve) = bc;
    CAMLreturn(_bezcurve);
}

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

bezier_curve_wrappers(Geom::LineSegment, line_segment);
bezier_curve_wrappers(Geom::QuadraticBezier, quadratic_bezier);
bezier_curve_wrappers(Geom::CubicBezier, cubic_bezier);

extern "C" CAMLprim value line_segment_of_two_points_wrapper(value _p0, value _p1)
{
    CAMLparam2(_p0, _p1);
    CAMLlocal1(_bezcurve);
    Geom::LineSegment *bc = new Geom::LineSegment(*OPAQUE_P(Geom::Point, _p0),
                                                  *OPAQUE_P(Geom::Point, _p1));
    _bezcurve = OPAQUE_P_ALLOC(Geom::LineSegment, line_segment_ops);
    OPAQUE_P(Geom::LineSegment, _bezcurve) = bc;
    CAMLreturn(_bezcurve);
}

extern "C" CAMLprim value quadratic_bezier_of_three_points_wrapper(value _p0, value _p1, value _p2)
{
    CAMLparam3(_p0, _p1, _p2);
    CAMLlocal1(_bezcurve);
    Geom::QuadraticBezier *bc = new Geom::QuadraticBezier(*OPAQUE_P(Geom::Point, _p0),
                                                          *OPAQUE_P(Geom::Point, _p1),
                                                          *OPAQUE_P(Geom::Point, _p2));
    _bezcurve = OPAQUE_P_ALLOC(Geom::QuadraticBezier, quadratic_bezier_ops);
    OPAQUE_P(Geom::QuadraticBezier, _bezcurve) = bc;
    CAMLreturn(_bezcurve);
}

extern "C" CAMLprim value cubic_bezier_of_four_points_wrapper(value _p0, value _p1, value _p2, value _p3)
{
    CAMLparam4(_p0, _p1, _p2, _p3);
    CAMLlocal1(_bezcurve);
    Geom::CubicBezier *bc = new Geom::CubicBezier(*OPAQUE_P(Geom::Point, _p0),
                                                  *OPAQUE_P(Geom::Point, _p1),
                                                  *OPAQUE_P(Geom::Point, _p2),
                                                  *OPAQUE_P(Geom::Point, _p3));
    _bezcurve = OPAQUE_P_ALLOC(Geom::CubicBezier, cubic_bezier_ops);
    OPAQUE_P(Geom::CubicBezier, _bezcurve) = bc;
    CAMLreturn(_bezcurve);
}

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

extern "C" CAMLprim value new_default_path_wrapper(void)
{
    CAMLparam0();
    CAMLlocal1(_path);
    _path = OPAQUE_P_ALLOC(Geom::Path, path_ops);
    OPAQUE_P(Geom::Path, _path) = new Geom::Path();
    CAMLreturn(_path);
}

extern "C" CAMLprim value new_path_wrapper(value _point)
{
    CAMLparam1(_point);
    CAMLlocal1(_path);
    _path = OPAQUE_P_ALLOC(Geom::Path, path_ops);
    OPAQUE_P(Geom::Path, _path) = new Geom::Path(*OPAQUE_P(Geom::Point, _point));
    CAMLreturn(_path);
}

extern "C" CAMLprim value path_point_at_wrapper(value _path, value _t)
{
    CAMLparam2(_path, _t);
    CAMLlocal1(_point);
    Geom::Path *path = OPAQUE_P(Geom::Path, _path);
    double time = Double_val(_t);
    Geom::Point point;
    try {
        point = path->pointAt(time);
    }
    catch (Geom::RangeError&) {
        caml_failwith("Parameter t out of bounds");
    }
    _point = OPAQUE_P_ALLOC(Geom::Point, point_ops);
    OPAQUE_P(Geom::Point, _point) = new Geom::Point(point);
    CAMLreturn(_point);
}

extern "C" CAMLprim value path_value_at_wrapper(value _path, value _t, value _dim2)
{
    CAMLparam3(_path, _t, _dim2);
    Geom::Path *path = OPAQUE_P(Geom::Path, _path);
    double time = Double_val(_t);
    double val;
    try {
        val = path->valueAt(time, dim2[Int_val(_dim2)]);
    }
    catch (Geom::RangeError&) {
        caml_failwith("Parameter t out of bounds");
    }
    CAMLreturn(caml_copy_double(val));
}

extern "C" CAMLprim value path_portion_wrapper(value _path, value _a, value _b)
{
    CAMLparam3(_path, _a, _b);
    CAMLlocal1(_portion);
    Geom::Path *path = OPAQUE_P(Geom::Path, _path);
    _portion = OPAQUE_P_ALLOC(Geom::Path, path_ops);
    OPAQUE_P(Geom::Path, _portion) = new Geom::Path(path->portion(Double_val(_a), Double_val(_b)));
    CAMLreturn(_portion);
}

extern "C" CAMLprim value path_reverse_wrapper(value _path)
{
    CAMLparam1(_path);
    CAMLlocal1(_reversed);
    Geom::Path *path = OPAQUE_P(Geom::Path, _path);
    _reversed = OPAQUE_P_ALLOC(Geom::Path, path_ops);
    OPAQUE_P(Geom::Path, _reversed) = new Geom::Path(path->reverse());
    CAMLreturn(_reversed);
}

extern "C" CAMLprim value path_append_curve_wrapper(value _stitch, value _path, value _curve)
{
    CAMLparam3(_stitch, _path, _curve);
    Geom::Path *path = OPAQUE_P(Geom::Path, _path);
    Geom::Curve *curve = OPAQUE_P(Geom::Curve, _curve);
    try {
        if (_stitch == Val_int(0))
            path->append(*curve);
        else
            path->append(*curve, stitching[Int_val(Field(_stitch, 0))]);
    }
    catch (Geom::ContinuityError&) {
        caml_failwith("Non-contiguous path");
    }
    CAMLreturn(Val_unit);
}

extern "C" CAMLprim value path_append_wrapper(value _stitch, value _path, value _other)
{
    CAMLparam3(_stitch, _path, _other);
    Geom::Path *path = OPAQUE_P(Geom::Path, _path);
    Geom::Path *other = OPAQUE_P(Geom::Path, _other);
    try {
        if (_stitch == Val_int(0))
            path->append(*other);
        else
            path->append(*other, stitching[Int_val(Field(_stitch, 0))]);
    }
    catch (Geom::ContinuityError&) {
        caml_failwith("Non-contiguous path");
    }
    CAMLreturn(Val_unit);
}

extern "C" CAMLprim value path_nearest_point_wrapper(value _from_t, value _to_t, value _path, value _point)
{
    CAMLparam4(_from_t, _to_t, _path, _point);
    CAMLlocal1(_result);
    Geom::Path *path = OPAQUE_P(Geom::Path, _path);
    Geom::Point *point = OPAQUE_P(Geom::Point, _point);
    double from_t = _from_t == Val_int(0) ? 0 : Double_val(Field(_from_t, 0));
    double to_t = _to_t == Val_int(0) ? path->size_default() : Double_val(Field(_to_t, 0));
    double time;
    double distance_squared;
    try {
        time = path->nearestPoint(*point, from_t, to_t, &distance_squared);
    }
    catch (Geom::RangeError&) {
        caml_failwith("[from,to] interval out of bounds");
    }
    _result = caml_alloc(2, 0);
    Store_field(_result, 0, caml_copy_double(time));
    Store_field(_result, 1, caml_copy_double(distance_squared));
    CAMLreturn(_result);
}

getval_wrapper(Geom::Path, path_size_open_wrapper, size_open, Val_int);
getval_wrapper(Geom::Path, path_size_closed_wrapper, size_closed, Val_int);
getval_wrapper(Geom::Path, path_size_default_wrapper, size_default, Val_int);
getval_wrapper(Geom::Path, path_size_wrapper, size, Val_int);
getval_wrapper(Geom::Path, path_max_size_wrapper, max_size, Val_int);

getval_wrapper(Geom::Path, path_empty_wrapper, empty, Val_bool);
getval_wrapper(Geom::Path, path_closed_wrapper, closed, Val_bool);
setval_wrapper(Geom::Path, path_close_wrapper, close, Bool_val);

extern "C" CAMLprim value path_bounds_fast_wrapper(value _path)
{
    CAMLparam1(_path);
    CAMLlocal2(_opt_rect, _rect);
    Geom::Path *path = OPAQUE_P(Geom::Path, _path);
    Geom::OptRect opt_rect = path->boundsFast();
    if (opt_rect.isEmpty()) {
        _opt_rect = Val_int(0);
    } else {
        _opt_rect = caml_alloc(1, 0);
        _rect = OPAQUE_P_ALLOC(Geom::Rect, rect_ops);
        OPAQUE_P(Geom::Rect, _rect) = new Geom::Rect(opt_rect.get());
        Store_field(_opt_rect, 0, _rect);
    }
    CAMLreturn(_opt_rect);
}

extern "C" CAMLprim value path_bounds_exact_wrapper(value _path)
{
    CAMLparam1(_path);
    CAMLlocal2(_opt_rect, _rect);
    Geom::Path *path = OPAQUE_P(Geom::Path, _path);
    Geom::OptRect opt_rect = path->boundsExact();
    if (opt_rect.isEmpty()) {
        _opt_rect = Val_int(0);
    } else {
        _opt_rect = caml_alloc(1, 0);
        _rect = OPAQUE_P_ALLOC(Geom::Rect, rect_ops);
        OPAQUE_P(Geom::Rect, _rect) = new Geom::Rect(*(Geom::Rect*)&opt_rect);
        Store_field(_opt_rect, 0, _rect);
    }
    CAMLreturn(_opt_rect);
}

extern "C" CAMLprim value path_to_cubic_bezier_curves_wrapper(value _path, value _tol)
{
    CAMLparam2(_path, _tol);
    CAMLlocal3(_list, _node, _curve);
    Geom::Path *path = OPAQUE_P(Geom::Path, _path);
    _list = Val_emptylist;
    Geom::Path::const_iterator i = path->end_default();
    while (i != path->begin()) {
        i--;
        Geom::Curve *curve = i->duplicate();
        Geom::BezierCurve *bc = dynamic_cast<Geom::BezierCurve*>(curve);
        if (bc != NULL && (bc->order() == 1 || bc->order() == 3)) {
            _curve = OPAQUE_P_ALLOC(Geom::BezierCurve, cubic_bezier_ops);
            OPAQUE_P(Geom::BezierCurve, _curve) = bc;
            _node = caml_alloc(2, 0);
            Store_field(_node, 0, _curve);
            Store_field(_node, 1, _list);
            _list = _node;
        } else {
            Geom::Path p = cubicbezierpath_from_sbasis(curve->toSBasis(), Double_val(_tol));
            Geom::Path::const_iterator j = p.end_default();
            while (j != p.begin()) {
                j--;
                bc = dynamic_cast<Geom::BezierCurve*>(j->duplicate());
                _curve = OPAQUE_P_ALLOC(Geom::BezierCurve, cubic_bezier_ops);
                OPAQUE_P(Geom::BezierCurve, _curve) = bc;
                _node = caml_alloc(2, 0);
                Store_field(_node, 0, _curve);
                Store_field(_node, 1, _list);
                _list = _node;
            }
        }
    }
    CAMLreturn(_list);
}

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