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
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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************

 *   Copyright (c) 2008 Jürgen Riegel <juergen.riegel@web.de>              *

 *                                                                         *

 *   This file is part of the FreeCAD CAx development system.              *

 *                                                                         *

 *   This library is free software; you can redistribute it and/or         *

 *   modify it under the terms of the GNU Library General Public           *

 *   License as published by the Free Software Foundation; either          *

 *   version 2 of the License, or (at your option) any later version.      *

 *                                                                         *

 *   This library  is distributed in the hope that it will be useful,      *

 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *

 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *

 *   GNU Library General Public License for more details.                  *

 *                                                                         *

 *   You should have received a copy of the GNU Library General Public     *

 *   License along with this library; see the file COPYING.LIB. If not,    *

 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *

 *   Suite 330, Boston, MA  02111-1307, USA                                *

 *                                                                         *

 ***************************************************************************/

#include <Geom_BSplineSurface.hxx>
#include <TColgp_Array1OfPnt.hxx>


#include <Base/Console.h>
#include <Base/Converter.h>
#include <Base/GeometryPyCXX.h>
#include <Base/Interpreter.h>
#include <Base/PyWrapParseTupleAndKeywords.h>
#include <Mod/Mesh/App/MeshPy.h>
#include <Mod/Part/App/BSplineSurfacePy.h>
#include <Mod/Points/App/PointsPy.h>
#if defined(HAVE_PCL_FILTERS)
# include <pcl/filters/passthrough.h>
# include <pcl/filters/voxel_grid.h>
# include <pcl/point_types.h>
#endif

#include "ApproxSurface.h"
#include "BSplineFitting.h"
#include "RegionGrowing.h"
#include "SampleConsensus.h"
#include "Segmentation.h"
#include "SurfaceTriangulation.h"

// clang-format off
/*

Dependency of pcl components:

common: none

features: common, kdtree, octree, search, (range_image)

filters: common, kdtree, octree, sample_consenus, search

geometry: common

io: common, octree

kdtree: common

keypoints: common, features, filters, kdtree, octree, search, (range_image)

octree: common

recognition: common, features, search

registration: common, features, kdtree, sample_consensus

sample_consensus: common

search: common, kdtree, octree

segmentation: common, kdtree, octree, sample_consensus, search

surface: common, kdtree, octree, search

*/

using namespace Reen;

namespace Reen {
class Module : public Py::ExtensionModule<Module>
{
public:
    Module() : Py::ExtensionModule<Module>("ReverseEngineering")
    {
        add_keyword_method("approxCurve", &Module::approxCurve, "Approximate curve");
        add_keyword_method("approxSurface",&Module::approxSurface,
            "approxSurface(Points, UDegree=3, VDegree=3, NbUPoles=6, NbVPoles=6,\n"
            "Smooth=True, Weight=0.1, Grad=1.0, Bend=0.0, Curv=0.0\n"
            "Iterations=5, Correction=True, PatchFactor=1.0, UVDirs=((ux, uy, uz), (vx, vy, vz)))\n\n"
            "Points: the input data (e.g. a point cloud or mesh)\n"
            "UDegree: the degree in u parametric direction\n"
            "VDegree: the degree in v parametric direction\n"
            "NbUPoles: the number of control points in u parametric direction\n"
            "NbVPoles: the number of control points in v parametric direction\n"
            "Smooth: use energy terms to create a smooth surface\n"
            "Weight: weight of the energy terms altogether\n"
            "Grad: weight of the gradient term\n"
            "Bend: weight of the bending energy term\n"
            "Curv: weight of the deviation of curvature term\n"
            "Iterations: number of iterations\n"
            "Correction: perform a parameter correction of each iteration step\n"
            "PatchFactor: create an extended surface\n"
            "UVDirs: set the u,v parameter directions as tuple of two vectors\n"
            "        If not set then they will be determined by computing a best-fit plane\n"
        );
#if defined(HAVE_PCL_SURFACE)
        add_keyword_method("triangulate",&Module::triangulate,
            "triangulate(PointKernel,searchRadius[,mu=2.5])."
        );
        add_keyword_method("poissonReconstruction",&Module::poissonReconstruction,
            "poissonReconstruction(PointKernel)."
        );
        add_keyword_method("viewTriangulation",&Module::viewTriangulation,
            "viewTriangulation(PointKernel, width, height)."
        );
        add_keyword_method("gridProjection",&Module::gridProjection,
            "gridProjection(PointKernel)."
        );
        add_keyword_method("marchingCubesRBF",&Module::marchingCubesRBF,
            "marchingCubesRBF(PointKernel)."
        );
        add_keyword_method("marchingCubesHoppe",&Module::marchingCubesHoppe,
            "marchingCubesHoppe(PointKernel)."
        );
#endif
#if defined(HAVE_PCL_OPENNURBS)
        add_keyword_method("fitBSpline",&Module::fitBSpline,
            "fitBSpline(PointKernel)."
        );
#endif
#if defined(HAVE_PCL_FILTERS)
        add_keyword_method("filterVoxelGrid",&Module::filterVoxelGrid,
            "filterVoxelGrid(dim)."
        );
        add_keyword_method("normalEstimation",&Module::normalEstimation,
            "normalEstimation(Points,[KSearch=0, SearchRadius=0]) -> Normals\n"
            "KSearch is an int and used to search the k-nearest neighbours in\n"
            "the k-d tree. Alternatively, SearchRadius (a float) can be used\n"
            "as spatial distance to determine the neighbours of a point\n"
            "Example:\n"
            "\n"
            "import ReverseEngineering as Reen\n"
            "pts=App.ActiveDocument.ActiveObject.Points\n"
            "nor=Reen.normalEstimation(pts,KSearch=5)\n"
            "\n"
            "f=App.ActiveDocument.addObject('Points::FeaturePython','Normals')\n"
            "f.addProperty('Points::PropertyNormalList','Normal')\n"
            "f.Points=pts\n"
            "f.Normal=nor\n"
            "f.ViewObject.Proxy=0\n"
            "f.ViewObject.DisplayMode=1\n"
        );
#endif
#if defined(HAVE_PCL_SEGMENTATION)
        add_keyword_method("regionGrowingSegmentation",&Module::regionGrowingSegmentation,
            "regionGrowingSegmentation()."
        );
        add_keyword_method("featureSegmentation",&Module::featureSegmentation,
            "featureSegmentation()."
        );
#endif
#if defined(HAVE_PCL_SAMPLE_CONSENSUS)
        add_keyword_method("sampleConsensus",&Module::sampleConsensus,
            "sampleConsensus()."
        );
#endif
        initialize("This module is the ReverseEngineering module."); // register with Python
    }

private:
    static std::vector<Base::Vector3d> getPoints(PyObject* pts, bool closed)

    {
        std::vector<Base::Vector3d> data;
        if (PyObject_TypeCheck(pts, &(Points::PointsPy::Type))) {
            std::vector<Base::Vector3d> normal;
            auto pypts = static_cast<Points::PointsPy*>(pts);
            Points::PointKernel* points = pypts->getPointKernelPtr();
            points->getPoints(data, normal, 0.0);
        }
        else {
            Py::Sequence l(pts);
            data.reserve(l.size());
            for (Py::Sequence::iterator it = l.begin(); it != l.end(); ++it) {
                Py::Tuple t(*it);
                data.emplace_back(
                    Py::Float(t.getItem(0)),
                    Py::Float(t.getItem(1)),
                    Py::Float(t.getItem(2))
                );
            }
        }

        if (closed) {
            if (!data.empty()) {
                data.push_back(data.front());
            }
        }

        return data;
    }

    static PyObject* approx1(const Py::Tuple& args, const Py::Dict& kwds)

    {
        PyObject* pts {};
        PyObject* closed = Py_False;
        int minDegree = 3;  // NOLINT
        int maxDegree = 8;  // NOLINT
        int cont = int(GeomAbs_C2);
        double tol3d = 1.0e-3;  // NOLINT

        static const std::array<const char *, 7> kwds_approx{"Points",
                                                             "Closed",
                                                             "MinDegree",
                                                             "MaxDegree",
                                                             "Continuity",
                                                             "Tolerance",
                                                             nullptr};
        if (!Base::Wrapped_ParseTupleAndKeywords(args.ptr(), kwds.ptr(), "O|O!iiid", kwds_approx,
                                                 &pts, &PyBool_Type, &closed, &minDegree,
                                                 &maxDegree, &cont, &tol3d)) {
            return nullptr;
        }

        std::vector<Base::Vector3d> data = getPoints(pts, Base::asBoolean(closed));

        Part::GeomBSplineCurve curve;
        curve.approximate(data, minDegree, maxDegree, GeomAbs_Shape(cont), tol3d);
        return curve.getPyObject();
    }

    static PyObject* approx2(const Py::Tuple& args, const Py::Dict& kwds)

    {
        PyObject* pts {};
        char* parType {};
        PyObject* closed = Py_False;
        int minDegree = 3;  // NOLINT
        int maxDegree = 8;  // NOLINT
        int cont = int(GeomAbs_C2);
        double tol3d = 1.0e-3;  // NOLINT

        static const std::array<const char *, 8> kwds_approx{"Points",
                                                             "ParametrizationType",
                                                             "Closed",
                                                             "MinDegree",
                                                             "MaxDegree",
                                                             "Continuity",
                                                             "Tolerance",
                                                             nullptr};
        if (!Base::Wrapped_ParseTupleAndKeywords(args.ptr(), kwds.ptr(), "Os|O!iiid", kwds_approx,
                                                 &pts, &parType, &PyBool_Type, &closed, &minDegree,
                                                 &maxDegree, &cont, &tol3d)) {
            return nullptr;
        }

        std::vector<Base::Vector3d> data = getPoints(pts, Base::asBoolean(closed));

        Approx_ParametrizationType pt {Approx_ChordLength};
        std::string pstr = parType;
        if (pstr == "Uniform") {
            pt = Approx_IsoParametric;
        }
        else if (pstr == "Centripetal") {
            pt = Approx_Centripetal;
        }

        Part::GeomBSplineCurve curve;
        curve.approximate(data, pt, minDegree, maxDegree, GeomAbs_Shape(cont), tol3d);
        return curve.getPyObject();
    }

    static PyObject* approx3(const Py::Tuple& args, const Py::Dict& kwds)

    {
        PyObject* pts {};
        double weight1 {};
        double weight2 {};
        double weight3 {};
        PyObject* closed = Py_False;
        int maxDegree = 8;  // NOLINT
        int cont = int(GeomAbs_C2);
        double tol3d = 1.0e-3;  // NOLINT

        static const std::array<const char *, 9> kwds_approx{"Points",
                                                             "Weight1",
                                                             "Weight2",
                                                             "Weight3",
                                                             "Closed",
                                                             "MaxDegree",
                                                             "Continuity",
                                                             "Tolerance",
                                                             nullptr};
        if (!Base::Wrapped_ParseTupleAndKeywords(args.ptr(), kwds.ptr(), "Oddd|O!iid", kwds_approx,
                                                 &pts, &weight1, &weight2, &weight3,
                                                 &PyBool_Type, &closed,
                                                 &maxDegree, &cont, &tol3d)) {
            return nullptr;
        }

        std::vector<Base::Vector3d> data = getPoints(pts, Base::asBoolean(closed));

        Part::GeomBSplineCurve curve;
        curve.approximate(data, weight1, weight2, weight3, maxDegree, GeomAbs_Shape(cont), tol3d);
        return curve.getPyObject();
    }

    Py::Object approxCurve(const Py::Tuple& args, const Py::Dict& kwds)

    {
        try {
            using approxFunc = std::function<PyObject*(const Py::Tuple& args, const Py::Dict& kwds)>;

            std::vector<approxFunc> funcs;
            funcs.emplace_back(approx3);
            funcs.emplace_back(approx2);
            funcs.emplace_back(approx1);

            for (const auto& func : funcs) {
                if (PyObject* py = func(args, kwds)) {
                    return Py::asObject(py);
                }

                PyErr_Clear();
            }

            throw Py::ValueError("Wrong arguments ReverseEngineering.approxCurve()");
        }
        catch (const Base::Exception& e) {
            std::string msg = e.what();
            if (msg.empty()) {
                msg = "ReverseEngineering.approxCurve() failed";
            }
            throw Py::RuntimeError(msg);
        }
    }

    Py::Object approxSurface(const Py::Tuple& args, const Py::Dict& kwds)

    {
        PyObject *o;
        PyObject *uvdirs = nullptr;
        // spline parameters
        int uDegree = 3;
        int vDegree = 3;
        int uPoles = 6;
        int vPoles = 6;
        // smoothing
        PyObject* smooth = Py_True;
        double weight = 0.1;
        double grad = 1.0;  //0.5
        double bend = 0.0; //0.2
        double curv = 0.0; //0.3
        // other parameters
        int iteration = 5;
        PyObject* correction = Py_True;
        double factor = 1.0;

        static const std::array<const char *, 15> kwds_approx{"Points", "UDegree", "VDegree", "NbUPoles", "NbVPoles",
                                                              "Smooth", "Weight", "Grad", "Bend", "Curv", "Iterations",
                                                              "Correction", "PatchFactor", "UVDirs", nullptr};
        if (!Base::Wrapped_ParseTupleAndKeywords(args.ptr(), kwds.ptr(), "O|iiiiO!ddddiO!dO!", kwds_approx,
                                                 &o, &uDegree, &vDegree, &uPoles, &vPoles,
                                                 &PyBool_Type, &smooth, &weight, &grad, &bend, &curv,
                                                 &iteration, &PyBool_Type, &correction, &factor,
                                                 &PyTuple_Type, &uvdirs)) {
            throw Py::Exception();
        }

        int uOrder = uDegree + 1;
        int vOrder = vDegree + 1;

        // error checking
        if (grad < 0.0 || grad > 1.0) {
            throw Py::ValueError("Value of Grad out of range [0,1]");
        }
        if (bend < 0.0 || bend > 1.0) {
            throw Py::ValueError("Value of Bend out of range [0,1]");
        }
        if (curv < 0.0 || curv > 1.0) {
            throw Py::ValueError("Value of Curv out of range [0,1]");
        }
        if (uDegree < 1 || uOrder > uPoles) {
            throw Py::ValueError("Value of uDegree out of range [1,NbUPoles-1]");
        }
        if (vDegree < 1 || vOrder > vPoles) {
            throw Py::ValueError("Value of vDegree out of range [1,NbVPoles-1]");
        }

        double sum = (grad + bend + curv);
        if (sum > 0)
            weight = weight / sum;

        try {
            std::vector<Base::Vector3f> pts;
            if (PyObject_TypeCheck(o, &(Points::PointsPy::Type))) {
                Points::PointsPy* pPoints = static_cast<Points::PointsPy*>(o);
                Points::PointKernel* points = pPoints->getPointKernelPtr();
                pts = points->getBasicPoints();
            }
            else if (PyObject_TypeCheck(o, &(Mesh::MeshPy::Type))) {
                const Mesh::MeshObject* mesh = static_cast<Mesh::MeshPy*>(o)->getMeshObjectPtr();
                const MeshCore::MeshPointArray& points = mesh->getKernel().GetPoints();
                pts.insert(pts.begin(), points.begin(), points.end());
            }
            else {
                Py::Sequence l(o);
                pts.reserve(l.size());
                for (Py::Sequence::iterator it = l.begin(); it != l.end(); ++it) {
                    Py::Tuple t(*it);
                    pts.emplace_back(
                        Py::Float(t.getItem(0)),
                        Py::Float(t.getItem(1)),
                        Py::Float(t.getItem(2))
                    );
                }
            }

            TColgp_Array1OfPnt clPoints(0, pts.size()-1);
            if (clPoints.Length() < uPoles * vPoles) {
                throw Py::ValueError("Too less data points for the specified number of poles");
            }

            int index=0;
            for (const auto & pt : pts) {
                clPoints(index++) = gp_Pnt(pt.x, pt.y, pt.z);
            }

            Reen::BSplineParameterCorrection pc(uOrder,vOrder,uPoles,vPoles);
            Handle(Geom_BSplineSurface) hSurf;

            if (uvdirs) {
                Py::Tuple t(uvdirs);
                Base::Vector3d u = Py::Vector(t.getItem(0)).toVector();
                Base::Vector3d v = Py::Vector(t.getItem(1)).toVector();
                pc.SetUV(u, v);
            }
            pc.EnableSmoothing(Base::asBoolean(smooth), weight, grad, bend, curv);
            hSurf = pc.CreateSurface(clPoints, iteration, Base::asBoolean(correction), factor);
            if (!hSurf.IsNull()) {
                return Py::asObject(new Part::BSplineSurfacePy(new Part::GeomBSplineSurface(hSurf)));
            }

            throw Py::RuntimeError("Computation of B-spline surface failed");
        }
        catch (const Py::Exception&) {
            // re-throw
            throw;
        }
        catch (Standard_Failure &e) {
            std::string str;
            Standard_CString msg = e.GetMessageString();
            str += typeid(e).name();
            str += " ";
            if (msg) {str += msg;}
            else     {str += "No OCCT Exception Message";}
            throw Py::RuntimeError(str);
        }
        catch (const Base::Exception &e) {
            throw Py::RuntimeError(e.what());
        }
        catch (...) {
            throw Py::RuntimeError("Unknown C++ exception");
        }
    }
#if defined(HAVE_PCL_SURFACE)
    /*

import ReverseEngineering as Reen

import Points

import Mesh

import random



r=random.Random()



p=Points.Points()

pts=[]

for i in range(21):

  for j in range(21):

    pts.append(App.Vector(i,j,r.gauss(5,0.05)))



p.addPoints(pts)

m=Reen.triangulate(Points=p,SearchRadius=2.2)

Mesh.show(m)

    */
    Py::Object triangulate(const Py::Tuple& args, const Py::Dict& kwds)

    {
        PyObject *pts;
        double searchRadius;
        PyObject *vec = 0;
        int ksearch=5;
        double mu=2.5;

        static const std::array<const char*,6> kwds_greedy {"Points", "SearchRadius", "Mu", "KSearch",
                                      "Normals", NULL};
        if (!Base::Wrapped_ParseTupleAndKeywords(args.ptr(), kwds.ptr(), "O!d|diO", kwds_greedy,
                                        &(Points::PointsPy::Type), &pts,
                                        &searchRadius, &mu, &ksearch, &vec))
            throw Py::Exception();

        Points::PointKernel* points = static_cast<Points::PointsPy*>(pts)->getPointKernelPtr();

        Mesh::MeshObject* mesh = new Mesh::MeshObject();
        SurfaceTriangulation tria(*points, *mesh);
        tria.setMu(mu);
        tria.setSearchRadius(searchRadius);
        if (vec) {
            Py::Sequence list(vec);
            std::vector<Base::Vector3f> normals;
            normals.reserve(list.size());
            for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
                Base::Vector3d v = Py::Vector(*it).toVector();
                normals.push_back(Base::convertTo<Base::Vector3f>(v));
            }
            tria.perform(normals);
        }
        else {
            tria.perform(ksearch);
        }

        return Py::asObject(new Mesh::MeshPy(mesh));
    }
    Py::Object poissonReconstruction(const Py::Tuple& args, const Py::Dict& kwds)

    {
        PyObject *pts;
        PyObject *vec = 0;
        int ksearch=5;
        int octreeDepth=-1;
        int solverDivide=-1;
        double samplesPerNode=-1.0;

        static const std::array<const char*,7> kwds_poisson {"Points", "KSearch", "OctreeDepth", "SolverDivide",
                                      "SamplesPerNode", "Normals", NULL};
        if (!Base::Wrapped_ParseTupleAndKeywords(args.ptr(), kwds.ptr(), "O!|iiidO", kwds_poisson,
                                        &(Points::PointsPy::Type), &pts,
                                        &ksearch, &octreeDepth, &solverDivide, &samplesPerNode, &vec))
            throw Py::Exception();

        Points::PointKernel* points = static_cast<Points::PointsPy*>(pts)->getPointKernelPtr();

        Mesh::MeshObject* mesh = new Mesh::MeshObject();
        Reen::PoissonReconstruction poisson(*points, *mesh);
        poisson.setDepth(octreeDepth);
        poisson.setSolverDivide(solverDivide);
        poisson.setSamplesPerNode(samplesPerNode);
        if (vec) {
            Py::Sequence list(vec);
            std::vector<Base::Vector3f> normals;
            normals.reserve(list.size());
            for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
                Base::Vector3d v = Py::Vector(*it).toVector();
                normals.push_back(Base::convertTo<Base::Vector3f>(v));
            }
            poisson.perform(normals);
        }
        else {
            poisson.perform(ksearch);
        }

        return Py::asObject(new Mesh::MeshPy(mesh));
    }
   /*

import ReverseEngineering as Reen

import Points

import Mesh

import random

import math

r=random.Random()

p=Points.Points()

pts=[]

for i in range(21):

  for j in range(21):

    pts.append(App.Vector(i,j,r.random()))

p.addPoints(pts)

m=Reen.viewTriangulation(p,21,21)

Mesh.show(m)

def boxmueller():

  r1,r2=random.random(),random.random()

  return math.sqrt(-2*math.log(r1))*math.cos(2*math.pi*r2)

p=Points.Points()

pts=[]

for i in range(21):

  for j in range(21):

    pts.append(App.Vector(i,j,r.gauss(5,0.05)))

p.addPoints(pts)

m=Reen.viewTriangulation(p,21,21)

Mesh.show(m)

    */
    Py::Object viewTriangulation(const Py::Tuple& args, const Py::Dict& kwds)

    {
        PyObject *pts;
        int width;
        int height;

        static const std::array<const char*,4> kwds_view {"Points", "Width", "Height", NULL};
        if (!Base::Wrapped_ParseTupleAndKeywords(args.ptr(), kwds.ptr(), "O!|ii", kwds_view,
                                        &(Points::PointsPy::Type), &pts,
                                        &width, &height))
            throw Py::Exception();

        Points::PointKernel* points = static_cast<Points::PointsPy*>(pts)->getPointKernelPtr();

        try {
            Mesh::MeshObject* mesh = new Mesh::MeshObject();
            ImageTriangulation view(width, height, *points, *mesh);
            view.perform();

            return Py::asObject(new Mesh::MeshPy(mesh));
        }
        catch (const Base::Exception& e) {
            throw Py::RuntimeError(e.what());
        }
    }
    Py::Object gridProjection(const Py::Tuple& args, const Py::Dict& kwds)

    {
        PyObject *pts;
        PyObject *vec = 0;
        int ksearch=5;

        static const std::array<const char*,4> kwds_greedy {"Points", "KSearch", "Normals", NULL};
        if (!Base::Wrapped_ParseTupleAndKeywords(args.ptr(), kwds.ptr(), "O!|iO", kwds_greedy,
                                        &(Points::PointsPy::Type), &pts,
                                        &ksearch, &vec))
            throw Py::Exception();

        Points::PointKernel* points = static_cast<Points::PointsPy*>(pts)->getPointKernelPtr();

        Mesh::MeshObject* mesh = new Mesh::MeshObject();
        GridReconstruction tria(*points, *mesh);
        if (vec) {
            Py::Sequence list(vec);
            std::vector<Base::Vector3f> normals;
            normals.reserve(list.size());
            for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
                Base::Vector3d v = Py::Vector(*it).toVector();
                normals.push_back(Base::convertTo<Base::Vector3f>(v));
            }
            tria.perform(normals);
        }
        else {
            tria.perform(ksearch);
        }

        return Py::asObject(new Mesh::MeshPy(mesh));
    }
    Py::Object marchingCubesRBF(const Py::Tuple& args, const Py::Dict& kwds)

    {
        PyObject *pts;
        PyObject *vec = 0;
        int ksearch=5;

        static const std::array<const char*,4> kwds_greedy {"Points", "KSearch", "Normals", NULL};
        if (!Base::Wrapped_ParseTupleAndKeywords(args.ptr(), kwds.ptr(), "O!|iO", kwds_greedy,
                                        &(Points::PointsPy::Type), &pts,
                                        &ksearch, &vec))
            throw Py::Exception();

        Points::PointKernel* points = static_cast<Points::PointsPy*>(pts)->getPointKernelPtr();

        Mesh::MeshObject* mesh = new Mesh::MeshObject();
        MarchingCubesRBF tria(*points, *mesh);
        if (vec) {
            Py::Sequence list(vec);
            std::vector<Base::Vector3f> normals;
            normals.reserve(list.size());
            for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
                Base::Vector3d v = Py::Vector(*it).toVector();
                normals.push_back(Base::convertTo<Base::Vector3f>(v));
            }
            tria.perform(normals);
        }
        else {
            tria.perform(ksearch);
        }

        return Py::asObject(new Mesh::MeshPy(mesh));
    }
/*

import ReverseEngineering as Reen

import Points

import Mesh

import random

r=random.Random()

p=Points.Points()

pts=[]

for i in range(21):

  for j in range(21):

    pts.append(App.Vector(i,j,r.gauss(5,0.05)))

p.addPoints(pts)

m=Reen.marchingCubesHoppe(Points=p)

Mesh.show(m)

    */
    Py::Object marchingCubesHoppe(const Py::Tuple& args, const Py::Dict& kwds)

    {
        PyObject *pts;
        PyObject *vec = 0;
        int ksearch=5;

        static const std::array<const char*,4> kwds_greedy {"Points", "KSearch", "Normals", NULL};
        if (!Base::Wrapped_ParseTupleAndKeywords(args.ptr(), kwds.ptr(), "O!|iO", kwds_greedy,
                                        &(Points::PointsPy::Type), &pts,
                                        &ksearch, &vec))
            throw Py::Exception();

        Points::PointKernel* points = static_cast<Points::PointsPy*>(pts)->getPointKernelPtr();

        Mesh::MeshObject* mesh = new Mesh::MeshObject();
        MarchingCubesHoppe tria(*points, *mesh);
        if (vec) {
            Py::Sequence list(vec);
            std::vector<Base::Vector3f> normals;
            normals.reserve(list.size());
            for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
                Base::Vector3d v = Py::Vector(*it).toVector();
                normals.push_back(Base::convertTo<Base::Vector3f>(v));
            }
            tria.perform(normals);
        }
        else {
            tria.perform(ksearch);
        }

        return Py::asObject(new Mesh::MeshPy(mesh));
    }
#endif
#if defined(HAVE_PCL_OPENNURBS)
    Py::Object fitBSpline(const Py::Tuple& args, const Py::Dict& kwds)

    {
        PyObject *pts;
        int degree = 2;
        int refinement = 4;
        int iterations = 10;
        double interiorSmoothness = 0.2;
        double interiorWeight = 1.0;
        double boundarySmoothness = 0.2;
        double boundaryWeight = 0.0;

        static const std::array<const char*,9> kwds_approx {"Points", "Degree", "Refinement", "Iterations",
                                      "InteriorSmoothness", "InteriorWeight", "BoundarySmoothness", "BoundaryWeight", NULL};
        if (!Base::Wrapped_ParseTupleAndKeywords(args.ptr(), kwds.ptr(), "O!|iiidddd", kwds_approx,
                                        &(Points::PointsPy::Type), &pts,
                                        &degree, &refinement, &iterations,
                                        &interiorSmoothness, &interiorWeight,
                                        &boundarySmoothness, &boundaryWeight))
            throw Py::Exception();

        Points::PointKernel* points = static_cast<Points::PointsPy*>(pts)->getPointKernelPtr();

        BSplineFitting fit(points->getBasicPoints());
        fit.setOrder(degree+1);
        fit.setRefinement(refinement);
        fit.setIterations(iterations);
        fit.setInteriorSmoothness(interiorSmoothness);
        fit.setInteriorWeight(interiorWeight);
        fit.setBoundarySmoothness(boundarySmoothness);
        fit.setBoundaryWeight(boundaryWeight);
        Handle(Geom_BSplineSurface) hSurf = fit.perform();

        if (!hSurf.IsNull()) {
            return Py::asObject(new Part::BSplineSurfacePy(new Part::GeomBSplineSurface(hSurf)));
        }

        throw Py::RuntimeError("Computation of B-spline surface failed");
    }
#endif
#if defined(HAVE_PCL_FILTERS)
    Py::Object filterVoxelGrid(const Py::Tuple& args, const Py::Dict& kwds)

    {
        PyObject *pts;
        double voxDimX = 0;
        double voxDimY = 0;
        double voxDimZ = 0;

        static const std::array<const char*,5>  kwds_voxel {"Points", "DimX", "DimY", "DimZ", NULL};
        if (!Base::Wrapped_ParseTupleAndKeywords(args.ptr(), kwds.ptr(), "O!d|dd", kwds_voxel,
                                        &(Points::PointsPy::Type), &pts,
                                        &voxDimX, &voxDimY, &voxDimZ))
            throw Py::Exception();

        if (voxDimY == 0)
            voxDimY = voxDimX;

        if (voxDimZ == 0)
            voxDimZ = voxDimX;

        Points::PointKernel* points = static_cast<Points::PointsPy*>(pts)->getPointKernelPtr();

        pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
        cloud->reserve(points->size());
        for (Points::PointKernel::const_iterator it = points->begin(); it != points->end(); ++it) {
            cloud->push_back(pcl::PointXYZ(it->x, it->y, it->z));
        }

        // Create the filtering object
        pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_downSmpl (new pcl::PointCloud<pcl::PointXYZ>);
        pcl::VoxelGrid<pcl::PointXYZ> voxG;
        voxG.setInputCloud (cloud);
        voxG.setLeafSize (voxDimX, voxDimY, voxDimZ);
        voxG.filter (*cloud_downSmpl);

        Points::PointKernel* points_sample = new Points::PointKernel();
        points_sample->reserve(cloud_downSmpl->size());
        for (pcl::PointCloud<pcl::PointXYZ>::const_iterator it = cloud_downSmpl->begin();it!=cloud_downSmpl->end();++it) {
            points_sample->push_back(Base::Vector3d(it->x,it->y,it->z));
        }

        return Py::asObject(new Points::PointsPy(points_sample));
    }
#endif
#if defined(HAVE_PCL_FILTERS)
    Py::Object normalEstimation(const Py::Tuple& args, const Py::Dict& kwds)

    {
        PyObject *pts;
        int ksearch=0;
        double searchRadius=0;

        static const std::array<const char*,4> kwds_normals {"Points", "KSearch", "SearchRadius", NULL};
        if (!Base::Wrapped_ParseTupleAndKeywords(args.ptr(), kwds.ptr(), "O!|id", kwds_normals,
                                        &(Points::PointsPy::Type), &pts,
                                        &ksearch, &searchRadius))
            throw Py::Exception();

        Points::PointKernel* points = static_cast<Points::PointsPy*>(pts)->getPointKernelPtr();

        std::vector<Base::Vector3d> normals;
        NormalEstimation estimate(*points);
        estimate.setKSearch(ksearch);
        estimate.setSearchRadius(searchRadius);
        estimate.perform(normals);

        Py::List list;
        for (std::vector<Base::Vector3d>::iterator it = normals.begin(); it != normals.end(); ++it) {
            list.append(Py::Vector(*it));
        }

        return list;
    }
#endif
#if defined(HAVE_PCL_SEGMENTATION)
    Py::Object regionGrowingSegmentation(const Py::Tuple& args, const Py::Dict& kwds)

    {
        PyObject *pts;
        PyObject *vec = 0;
        int ksearch=5;

        static const std::array<const char*,4> kwds_segment {"Points", "KSearch", "Normals", NULL};
        if (!Base::Wrapped_ParseTupleAndKeywords(args.ptr(), kwds.ptr(), "O!|iO", kwds_segment,
                                        &(Points::PointsPy::Type), &pts,
                                        &ksearch, &vec))
            throw Py::Exception();

        Points::PointKernel* points = static_cast<Points::PointsPy*>(pts)->getPointKernelPtr();

        std::list<std::vector<int> > clusters;
        RegionGrowing segm(*points, clusters);
        if (vec) {
            Py::Sequence list(vec);
            std::vector<Base::Vector3f> normals;
            normals.reserve(list.size());
            for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
                Base::Vector3d v = Py::Vector(*it).toVector();
                normals.push_back(Base::convertTo<Base::Vector3f>(v));
            }
            segm.perform(normals);
        }
        else {
            segm.perform(ksearch);
        }

        Py::List lists;
        for (std::list<std::vector<int> >::iterator it = clusters.begin(); it != clusters.end(); ++it) {
            Py::Tuple tuple(it->size());
            for (std::size_t i = 0; i < it->size(); i++) {
                tuple.setItem(i, Py::Long((*it)[i]));
            }
            lists.append(tuple);
        }

        return lists;
    }
    Py::Object featureSegmentation(const Py::Tuple& args, const Py::Dict& kwds)

    {
        PyObject *pts;
        int ksearch=5;

        static const std::array<const char*,3> kwds_segment {"Points", "KSearch", NULL};
        if (!Base::Wrapped_ParseTupleAndKeywords(args.ptr(), kwds.ptr(), "O!|i", kwds_segment,
                                        &(Points::PointsPy::Type), &pts, &ksearch))
            throw Py::Exception();

        Points::PointKernel* points = static_cast<Points::PointsPy*>(pts)->getPointKernelPtr();

        std::list<std::vector<int> > clusters;
        Segmentation segm(*points, clusters);
        segm.perform(ksearch);

        Py::List lists;
        for (std::list<std::vector<int> >::iterator it = clusters.begin(); it != clusters.end(); ++it) {
            Py::Tuple tuple(it->size());
            for (std::size_t i = 0; i < it->size(); i++) {
                tuple.setItem(i, Py::Long((*it)[i]));
            }
            lists.append(tuple);
        }

        return lists;
    }
#endif
#if defined(HAVE_PCL_SAMPLE_CONSENSUS)
/*

import ReverseEngineering as reen

import Points

import Part

p = App.ActiveDocument.Points.Points

data = p.Points

n = reen.normalEstimation(p, 10)

model = reen.sampleConsensus(SacModel="Plane", Points=p)

indices = model["Model"]

param = model["Parameters"]

plane = Part.Plane()

plane.Axis = param[0:3]

plane.Position = -plane.Axis * param[3]

np = Points.Points()

np.addPoints([data[i] for i in indices])

Points.show(np)

# sort in descending order

indices = list(indices)

indices.sort(reverse=True)

# remove points of segment

for i in indices:

    del data[i]

    del n[i]

p = Points.Points()

p.addPoints(data)

model = reen.sampleConsensus(SacModel="Cylinder", Points=p, Normals=n)

indices = model["Model"]

np = Points.Points()

np.addPoints([data[i] for i in indices])

Points.show(np)

    */
    Py::Object sampleConsensus(const Py::Tuple& args, const Py::Dict& kwds)

    {
        PyObject *pts;
        PyObject *vec = nullptr;
        const char* sacModelType = nullptr;

        static const std::array<const char*,4> kwds_sample {"SacModel", "Points", "Normals", NULL};
        if (!Base::Wrapped_ParseTupleAndKeywords(args.ptr(), kwds.ptr(), "sO!|O", kwds_sample,
                                        &sacModelType, &(Points::PointsPy::Type), &pts, &vec))
            throw Py::Exception();

        Points::PointKernel* points = static_cast<Points::PointsPy*>(pts)->getPointKernelPtr();
        std::vector<Base::Vector3d> normals;
        if (vec) {
            Py::Sequence list(vec);
            normals.reserve(list.size());
            for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
                Base::Vector3d v = Py::Vector(*it).toVector();
                normals.push_back(v);
            }
        }

        SampleConsensus::SacModel sacModel = SampleConsensus::SACMODEL_PLANE;
        if (sacModelType) {
            if (strcmp(sacModelType, "Cylinder") == 0)
                sacModel = SampleConsensus::SACMODEL_CYLINDER;
            else if (strcmp(sacModelType, "Sphere") == 0)
                sacModel = SampleConsensus::SACMODEL_SPHERE;
            else if (strcmp(sacModelType, "Cone") == 0)
                sacModel = SampleConsensus::SACMODEL_CONE;
        }

        std::vector<float> parameters;
        SampleConsensus sample(sacModel, *points, normals);
        std::vector<int> model;
        double probability = sample.perform(parameters, model);

        Py::Dict dict;
        Py::Tuple tuple(parameters.size());
        for (std::size_t i = 0; i < parameters.size(); i++)
            tuple.setItem(i, Py::Float(parameters[i]));
        Py::Tuple data(model.size());
        for (std::size_t i = 0; i < model.size(); i++)
            data.setItem(i, Py::Long(model[i]));
        dict.setItem(Py::String("Probability"), Py::Float(probability));
        dict.setItem(Py::String("Parameters"), tuple);
        dict.setItem(Py::String("Model"), data);

        return dict;
    }
#endif
};

PyObject* initModule()

{
    return Base::Interpreter().addModule(new Module);
}

} // namespace Reen


/* Python entry */
PyMOD_INIT_FUNC(ReverseEngineering)
{
    // load dependent module
    try {
        Base::Interpreter().loadModule("Part");
        Base::Interpreter().loadModule("Mesh");
    }
    catch(const Base::Exception& e) {
        PyErr_SetString(PyExc_ImportError, e.what());
        PyMOD_Return(nullptr);
    }

    PyObject* mod = Reen::initModule();
    Base::Console().log("Loading Reverse Engineering module… done\n");
    PyMOD_Return(mod);
}
// clang-format on