File size: 44,104 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
// SPDX-License-Identifier: LGPL-2.1-or-later

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

 *   Copyright (c) 2005 Imetric 3D GmbH                                    *

 *                                                                         *

 *   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                                *

 *                                                                         *

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

#ifndef MESH_ELEMENTS_H
#define MESH_ELEMENTS_H

#include <cstring>
#include <functional>
#include <limits>
#include <vector>

#include <Base/BoundBox.h>
#include <Base/Matrix.h>

#include "Definitions.h"


// Cannot use namespace Base in constructors of MeshPoint
#ifdef _MSC_VER
using Base::Vector3f;
#endif

namespace MeshCore
{

class MeshHelpEdge;
class MeshPoint;

/**

 * Helper class providing an operator for comparison

 * of two edges. The class holds the point indices of the

 * underlying edge.

 */
class MeshExport MeshHelpEdge

{
public:
    inline bool operator==(const MeshHelpEdge& rclEdge) const;
    PointIndex _ulIndex[2];  // point indices
};

/**

 * Structure that holds the facet index with the two corner point

 * indices of the facet's orientation this edge is attached to.

 */
class MeshExport MeshIndexEdge

{
public:
    FacetIndex _ulFacetIndex;      // Facet index
    unsigned short _ausCorner[2];  // corner point indices of the facet
};

/** MeshEdge just a pair of two point indices */
using MeshEdge = std::pair<PointIndex, PointIndex>;

struct MeshExport EdgeCollapse

{
    PointIndex _fromPoint;
    PointIndex _toPoint;
    std::vector<PointIndex> _adjacentFrom;  // adjacent points to _fromPoint
    std::vector<PointIndex> _adjacentTo;    // adjacent points to _toPoint
    std::vector<FacetIndex> _removeFacets;
    std::vector<FacetIndex> _changeFacets;
};

struct MeshExport VertexCollapse

{
    PointIndex _point;
    std::vector<PointIndex> _circumPoints;
    std::vector<FacetIndex> _circumFacets;
};

/**

 * The MeshPoint class represents a point in the mesh data structure. The class inherits from

 * Vector3f and provides some additional information such as flag state and property value.

 * The flags can be modified by the Set() and Reset() and queried by IsFlag().

 * A point can temporary be in an invalid state (e.g during deletion of several points), but

 * must not be set in general, i.e. always usable within a mesh-internal algorithm.

 *

 * Note: The status flag SEGMENT mark a point to be part of certain subset, a segment.

 * This flag must not be set by any algorithm unless it adds or removes points to a segment.

 *

 * Note: The status flag SELECTED mark a point to be selected which is e.g. used in the GUI.

 * This flag must not be set by any algorithm unless it adds or removes points to the selection.

 */
class MeshExport MeshPoint: public Base::Vector3f
{
public:
    enum TFlagType

    {
        INVALID = 1,
        VISIT = 2,
        SEGMENT = 4,
        MARKED = 8,
        SELECTED = 16,
        REV = 32,
        TMP0 = 64,
        TMP1 = 128
    };

    /** @name Construction */
    //@{
    MeshPoint()
        : _ucFlag(0)
        , _ulProp(0)
    {}
    inline MeshPoint(float x, float y, float z);
    inline MeshPoint(const Base::Vector3f& rclPt);  // explicit bombs
    inline MeshPoint(const MeshPoint& rclPt) = default;
    inline MeshPoint(MeshPoint&& rclPt) = default;
    ~MeshPoint() = default;
    //@}

public:
    /** @name Flag state

     * @note All flag methods are const as they do NOT change the actual behaviour of the object

     */
    //@{
    void SetFlag(TFlagType tF) const
    {
        _ucFlag |= static_cast<unsigned char>(tF);
    }
    void ResetFlag(TFlagType tF) const
    {
        _ucFlag &= ~static_cast<unsigned char>(tF);
    }
    bool IsFlag(TFlagType tF) const
    {
        return (_ucFlag & static_cast<unsigned char>(tF)) == static_cast<unsigned char>(tF);
    }
    void ResetInvalid() const
    {
        ResetFlag(INVALID);
    }
    void SetInvalid() const
    {
        SetFlag(INVALID);
    }
    bool IsValid() const
    {
        return !IsFlag(INVALID);
    }
    void SetProperty(unsigned long uP) const
    {
        _ulProp = uP;
    }
    //@}

    // Assignment
    inline MeshPoint& operator=(const MeshPoint& rclPt) = default;
    inline MeshPoint& operator=(MeshPoint&& rclPt) = default;

    // compare operator
    inline bool operator==(const MeshPoint& rclPt) const;
    inline bool operator==(const Base::Vector3f& rclV) const;
    inline bool operator<(const MeshPoint& rclPt) const;

public:
    mutable unsigned char _ucFlag; /**< Flag member */
    mutable unsigned long _ulProp; /**< Free usable property */
};

/**

 * The MeshGeomEdge class is geometric counterpart to MeshEdge that holds the

 * geometric data points of an edge.

 */
class MeshExport MeshGeomEdge

{
public:
    MeshGeomEdge() = default;

    /** Checks if the edge is inside the bounding box or intersects with it. */
    bool ContainedByOrIntersectBoundingBox(const Base::BoundBox3f& rclBB) const;
    /** Returns the bounding box of the edge. */
    Base::BoundBox3f GetBoundBox() const;
    /** Checks if the edge intersects with the given bounding box. */
    bool IntersectBoundingBox(const Base::BoundBox3f& rclBB) const;
    /** Calculates the intersection point of the line defined by the base \a rclPt and the direction

     * \a rclDir with the edge. The intersection must be inside the edge. If there is no

     * intersection false is returned.

     */
    bool IntersectWithLine(

        const Base::Vector3f& rclPt,

        const Base::Vector3f& rclDir,

        Base::Vector3f& rclRes

    ) const;
    /** Calculates the intersection point of an edge with this edge.

     * The intersection must be inside both edges. If there is no intersection false is returned.

     */
    bool IntersectWithEdge(const MeshGeomEdge& edge, Base::Vector3f& res) const;
    /** Calculates the intersection point of the plane defined by the base \a rclPt and the

     * direction \a rclDir with the edge. The intersection must be inside the edge. If there is no

     * intersection false is returned.

     */
    bool IntersectWithPlane(

        const Base::Vector3f& rclPt,

        const Base::Vector3f& rclDir,

        Base::Vector3f& rclRes

    ) const;
    /**

     * Calculates the projection of a point onto the line defined by the edge. The caller must check

     * if the projection point is inside the edge.

     */
    void ProjectPointToLine(const Base::Vector3f& rclPoint, Base::Vector3f& rclProj) const;
    /**

     * Get the closest points \a rclPnt1 and \a rclPnt2 of the line defined by this edge and the

     * line defined by \a rclPt and \a rclDir. If the two points are identical then both lines

     * intersect each other.

     */
    void ClosestPointsToLine(

        const Base::Vector3f& linePt,

        const Base::Vector3f& lineDir,

        Base::Vector3f& rclPnt1,

        Base::Vector3f& rclPnt2

    ) const;
    /**

     * Checks if the point is part of the edge. A point is regarded as part

     * of an edge if the distance is lower than \a fDistance to the projected point

     * of \a rclPoint on the edge.

     */
    bool IsPointOf(const Base::Vector3f& rclPoint, float fDistance) const;
    /**

     * Checks if the projection point of \a point lies on the edge.

     */
    bool IsProjectionPointOf(const Base::Vector3f& point) const;
    /**

     * Checks if the two edges are parallel.

     * \note Parallel edges could be collinear.

     */
    bool IsParallel(const MeshGeomEdge& edge) const;
    /**

     * Checks if the two edges are collinear.

     * \note Collinear edges always are parallel.

     */
    bool IsCollinear(const MeshGeomEdge& edge) const;

public:
    Base::Vector3f _aclPoints[2]; /**< Corner points */
    bool _bBorder {false};        /**< Set to true if border edge */
};

/**

 * The MeshFacet class represent a triangle facet in the mesh data.structure. A facet indexes

 * three neighbour facets and also three corner points.

 * This class only keeps topologic information but no geometric information at all.

 *

 * Here are the most important conventions concerning the facet's orientation:

 * \li neighbour or edge number of 0 is defined by corner 0 and 1

 * \li neighbour or edge number of 1 is defined by corner 1 and 2

 * \li neighbour or edge number of 2 is defined by corner 2 and 0

 * \li neighbour index is set to FACET_INDEX_MAX if there is no neighbour facet

 *

 * Note: The status flag SEGMENT mark a facet to be part of certain subset, a segment.

 * This flag must not be set by any algorithm unless it adds or removes facets to a segment.

 *

 * Note: The status flag SELECTED mark a facet to be selected which is e.g. used in the GUI.

 * This flag must not be set by any algorithm unless it adds or removes facets to the selection.

 */
class MeshFacet

{
public:
    enum TFlagType

    {
        INVALID = 1,
        VISIT = 2,
        SEGMENT = 4,
        MARKED = 8,
        SELECTED = 16,
        REV = 32,
        TMP0 = 64,
        TMP1 = 128
    };

public:
    /** @name Construction */
    //@{
    inline MeshFacet();
    inline MeshFacet(const MeshFacet& rclF) = default;
    inline MeshFacet(MeshFacet&& rclF) = default;
    inline MeshFacet(

        PointIndex p1,

        PointIndex p2,

        PointIndex p3,

        FacetIndex n1 = FACET_INDEX_MAX,

        FacetIndex n2 = FACET_INDEX_MAX,

        FacetIndex n3 = FACET_INDEX_MAX

    );
    ~MeshFacet() = default;
    //@}

    /** @name Flag state

     * @note All flag methods are const as they do NOT change the actual behaviour of the object

     */
    //@{
    void SetFlag(TFlagType tF) const
    {
        _ucFlag |= static_cast<unsigned char>(tF);
    }
    void ResetFlag(TFlagType tF) const
    {
        _ucFlag &= ~static_cast<unsigned char>(tF);
    }
    bool IsFlag(TFlagType tF) const
    {
        return (_ucFlag & static_cast<unsigned char>(tF)) == static_cast<unsigned char>(tF);
    }
    void ResetInvalid() const
    {
        ResetFlag(INVALID);
    }
    void SetProperty(unsigned long uP) const
    {
        _ulProp = uP;
    }
    /**

     * Marks a facet as invalid. Should be used only temporary from within an algorithm

     * (e.g. deletion of several facets) but must not be set permanently.

     * From outside the data-structure must not have invalid facets.

     */
    void SetInvalid() const
    {
        SetFlag(INVALID);
    }
    bool IsValid() const
    {
        return !IsFlag(INVALID);
    }
    //@}

    // Assignment
    inline MeshFacet& operator=(const MeshFacet& rclF) = default;
    inline MeshFacet& operator=(MeshFacet&& rclF) = default;
    inline void SetVertices(PointIndex, PointIndex, PointIndex);
    inline void SetNeighbours(FacetIndex, FacetIndex, FacetIndex);

    /**

     * Returns the indices of the corner points of the given edge number.

     */
    inline void GetEdge(unsigned short usSide, MeshHelpEdge& rclEdge) const;
    /**

     * Returns the indices of the corner points of the given edge number.

     */
    inline std::pair<PointIndex, PointIndex> GetEdge(unsigned short usSide) const;
    /**

     * Returns the edge-number to the given index of neighbour facet.

     * If \a ulNIndex is not a neighbour USHRT_MAX is returned.

     */
    inline unsigned short Side(FacetIndex ulNIndex) const;
    /**

     * Returns the edge-number defined by two points. If one point is

     * not a corner point USHRT_MAX is returned.

     */
    inline unsigned short Side(PointIndex ulP0, PointIndex P1) const;
    /**

     * Returns the edge-number defined by the shared edge of both facets. If the facets don't

     * share a common edge USHRT_MAX is returned.

     */
    inline unsigned short Side(const MeshFacet& rFace) const;
    /**

     * Returns true if this facet shares the same three points as \a rcFace.

     * The orientation is not of interest in this case.

     */
    inline bool IsEqual(const MeshFacet& rcFace) const;
    /**

     * Replaces the index of the corner point that is equal to \a ulOrig

     * by \a ulNew. If the facet does not have a corner point with this index

     * nothing happens.

     */
    inline void Transpose(PointIndex ulOrig, PointIndex ulNew);
    /**

     * Decrement the index for each corner point that is higher than \a ulIndex.

     */
    inline void Decrement(PointIndex ulIndex);
    /**

     * Checks if the facets references the given point index.

     */
    inline bool HasPoint(PointIndex) const;
    /**

     * Replaces the index of the neighbour facet that is equal to \a ulOrig

     * by \a ulNew. If the facet does not have a neighbourt with this index

     * nothing happens.

     */
    inline void ReplaceNeighbour(FacetIndex ulOrig, FacetIndex ulNew);
    /**

     * Checks if the neighbour exists at the given edge-number.

     */
    bool HasNeighbour(unsigned short usSide) const
    {
        return (_aulNeighbours[usSide] != FACET_INDEX_MAX);
    }
    /**

     * Checks if the given index is a neighbour facet.

     */
    bool IsNeighbour(FacetIndex index) const
    {
        return Side(index) < 3;
    }
    /** Counts the number of edges without neighbour. */
    inline unsigned short CountOpenEdges() const;
    /** Returns true if there is an edge without neighbour, otherwise false. */
    inline bool HasOpenEdge() const;
    /** Returns true if the two facets have the same orientation, false otherwise

     * Therefore the two facets must be adjacent.

     */
    inline bool HasSameOrientation(const MeshFacet&) const;
    /** Checks whether the facet is degenerated to a line of point. */
    inline bool IsDegenerated() const;
    /** Flips the orientation of the facet. */
    void FlipNormal()
    {
        std::swap(_aulPoints[1], _aulPoints[2]);
        std::swap(_aulNeighbours[0], _aulNeighbours[2]);
    }

public:
    mutable unsigned char _ucFlag; /**< Flag member. */
    mutable unsigned long _ulProp; /**< Free usable property. */
    PointIndex _aulPoints[3];      /**< Indices of corner points. */
    FacetIndex _aulNeighbours[3];  /**< Indices of neighbour facets. */
};

/**

 * The MeshGeomFacet class is geometric counterpart to MeshFacet that holds the

 * geometric data points of a triangle.

 */
class MeshExport MeshGeomFacet

{
public:
    /** @name Construction */
    //@{
    /// default constructor
    MeshGeomFacet();
    /// Constructor with the corner points
    MeshGeomFacet(const Base::Vector3f& v1, const Base::Vector3f& v2, const Base::Vector3f& v3);
    MeshGeomFacet(const MeshGeomFacet&) = default;
    MeshGeomFacet(MeshGeomFacet&&) = default;
    /// Destruction
    ~MeshGeomFacet() = default;
    //@}

    MeshGeomFacet& operator=(const MeshGeomFacet&) = default;
    MeshGeomFacet& operator=(MeshGeomFacet&&) = default;

public:
    /**

     * Checks if the point is part of the facet. A point is regarded as part

     * of a facet if the distance is lower \a fDistance and the projected point

     * in the facet normal direction is inside the triangle.

     */
    bool IsPointOf(const Base::Vector3f& rclPoint, float fDistance) const;
    /**

     * Checks if the point is inside or at the border of the facet. The point

     * must already exactly lie on the plane defined by the facet, which is not

     * checked. This method is very efficient.

     */
    bool IsPointOf(const Base::Vector3f& P) const;
    /** Checks whether the given point is inside the facet with tolerance \a fDistance.

     * This method does actually the same as IsPointOf() but this implementation

     * is done more effective through comparison of normals.

     */
    bool IsPointOfFace(const Base::Vector3f& rclP, float fDistance) const;
    /** Calculates the weights \a w1, ...,  \a w3 of the corners to get the point \a rclP, i.e.

     * rclP = w0*v0 + w1*v1 + w2*v2 (v0-v2 are the corners corners).

     * If w0+w1+w2==1.0 then the point rclP lies on the plane that is spanned by the facet,

     * otherwise the point doesn't lie on the plane. If the sum of wi is 1 and if each wi is between

     * [0,1] than the point lies inside the facet or on the border, respectively.

     *

     * If the point doesn't lie on the plane false is returned, true otherwise.

     */
    bool Weights(const Base::Vector3f& rclP, float& w0, float& w1, float& w2) const;
    /**

     * Calculates the distance of a point to the plane defined by the triangle.

     */
    inline float DistancePlaneToPoint(const Base::Vector3f& rclPoint) const;
    /**

     * Calculates the projection of a point onto the plane defined by the triangle.

     */
    void ProjectPointToPlane(const Base::Vector3f& rclPoint, Base::Vector3f& rclProj) const;
    /**

     * Calculates the projection of a facet onto the plane defined by the triangle.

     */
    void ProjectFacetToPlane(MeshGeomFacet& rclFacet) const;
    /**

     * Checks whether the triangle is degenerated. A triangle is degenerated if its area

     * is less than an epsilon.

     */
    bool IsDegenerated(float epsilon) const;
    /**

     * Checks whether the triangle is deformed. A triangle is deformed if an angle

     * exceeds a given maximum angle or falls below a given minimum angle.

     * For performance reasons the cosine of minimum and maximum angle is expected.

     */
    bool IsDeformed(float fCosOfMinAngle, float fCosOfMaxAngle) const;
    /**

     * Enlarges the triangle.

     */
    void Enlarge(float fDist);
    /**

     * Calculates the facet normal for storing internally.

     */
    inline void CalcNormal() const;
    /**

     * Arrange the facet normal so the both vectors have the same orientation.

     */
    inline void ArrangeNormal(const Base::Vector3f& rclN);
    /**

     * Adjusts the facet's orientation to its normal.

     */
    inline void AdjustCirculationDirection();
    /** Invalidate the normal. It will be recomputed when querying it. */
    void NormalInvalid()
    {
        _bNormalCalculated = false;
    }
    /** Query the flag state of the facet. */
    bool IsFlag(MeshFacet::TFlagType tF) const
    {
        return (_ucFlag & static_cast<unsigned char>(tF)) == static_cast<unsigned char>(tF);
    }
    /** Set flag state */
    void SetFlag(MeshFacet::TFlagType tF)
    {
        _ucFlag |= static_cast<unsigned char>(tF);
    }
    /** Reset flag state */
    void ResetFlag(MeshFacet::TFlagType tF)
    {
        _ucFlag &= ~static_cast<unsigned char>(tF);
    }
    /** Calculates the facet's gravity point. */
    inline Base::Vector3f GetGravityPoint() const;
    /** Returns the normal of the facet. */
    inline Base::Vector3f GetNormal() const;
    /** Sets the facet's normal. */
    inline void SetNormal(const Base::Vector3f& rclNormal);
    /** Returns the wrapping bounding box. */
    inline Base::BoundBox3f GetBoundBox() const;
    /** Calculates the perimeter of the facet. */
    inline float Perimeter() const;
    /** Calculates the area of a facet. */
    inline float Area() const;
    /** Calculates the maximum angle of a facet. */
    float MaximumAngle() const;
    /** Calculates the minimum angle of a facet. */
    float MinimumAngle() const;
    /** Checks if the facet is inside the bounding box or intersects with it. */
    inline bool ContainedByOrIntersectBoundingBox(const Base::BoundBox3f& rcBB) const;
    /** Checks if the facet intersects with the given bounding box. */
    bool IntersectBoundingBox(const Base::BoundBox3f& rclBB) const;
    /** This method checks if both facets intersect.

     */
    bool IntersectWithFacet(const MeshGeomFacet& rclFacet) const;
    /**

     * Intersect the facet with the other facet

     * The result is line given by two points (if intersected).

     * Return is the number of intersections points: 0: no intersection, 1: one intersection point

     * (rclPt0), 2: two intersections points (rclPt0, rclPt1)

     */
    int IntersectWithFacet(const MeshGeomFacet& facet, Base::Vector3f& rclPt0, Base::Vector3f& rclPt1) const;
    /** Calculates the shortest distance from the line segment defined by \a rcP1 and \a rcP2 to

     * this facet.

     */
    float DistanceToLineSegment(const Base::Vector3f& rcP1, const Base::Vector3f& rcP2) const;
    /** Calculates the shortest distance from the point \a rcPt to the facet. */
    float DistanceToPoint(const Base::Vector3f& rcPt) const
    {
        Base::Vector3f res;
        return DistanceToPoint(rcPt, res);
    }
    /** Calculates the shortest distance from the point \a rcPt to the facet. \a rclNt is the point

     * of the facet with shortest distance.

     */
    float DistanceToPoint(const Base::Vector3f& rclPt, Base::Vector3f& rclNt) const;
    /** Calculates the intersection point of the line defined by the base \a rclPt and the direction

     * \a rclDir with the facet. The intersection must be inside the facet. If there is no

     * intersection false is returned.

     */
    bool IntersectWithLine(

        const Base::Vector3f& rclPt,

        const Base::Vector3f& rclDir,

        Base::Vector3f& rclRes

    ) const;
    /** Calculates the intersection point of the line defined by the base \a rclPt and the direction

     * \a rclDir with the facet. The intersection must be inside the facet. If there is no

     * intersection false is returned. This does actually the same as IntersectWithLine() with one

     * additionally constraint that the angle between the direction of the line and the normal of

     * the plane must not exceed \a fMaxAngle.

     */
    bool Foraminate(

        const Base::Vector3f& P,

        const Base::Vector3f& dir,

        Base::Vector3f& I,

        float fMaxAngle = Mathf::PI

    ) const;
    /** Checks if the facet intersects with the plane defined by the base \a rclBase and the normal

     * \a rclNormal and returns true if two points are found, false otherwise.

     */
    bool IntersectWithPlane(

        const Base::Vector3f& rclBase,

        const Base::Vector3f& rclNormal,

        Base::Vector3f& rclP1,

        Base::Vector3f& rclP2

    ) const;
    /**

     * Checks if the facet intersects with the plane defined by the base \a rclBase and the normal

     * \a rclNormal.

     */
    inline bool IntersectWithPlane(const Base::Vector3f& rclBase, const Base::Vector3f& rclNormal) const;
    /** Checks if the plane defined by the facet \a rclFacet intersects with the line defined by the

     * base \a rclBase and the direction \a rclNormal and returns the intersection point \a rclRes

     * if possible.

     */
    bool IntersectPlaneWithLine(

        const Base::Vector3f& rclPt,

        const Base::Vector3f& rclDir,

        Base::Vector3f& rclRes

    ) const;
    /** Calculates the volume of the prism defined by two facets.

     * \note The two facets must not intersect.

     */
    float VolumeOfPrism(const MeshGeomFacet& rclF) const;
    /** Subsamples the facet into points with resolution \a fStep. */
    void SubSample(float fStep, std::vector<Base::Vector3f>& rclPoints) const;
    /** Calculates the center and radius of the inscribed circle of the facet. */
    float CenterOfInscribedCircle(Base::Vector3f& rclCenter) const;
    /** Calculates the center and radius of the circum circle of the facet. */
    float CenterOfCircumCircle(Base::Vector3f& rclCenter) const;
    /** Returns the edge number of the facet that is nearest to the point \a rclPt. */
    unsigned short NearestEdgeToPoint(const Base::Vector3f& rclPt) const;
    /** Returns the edge number \a side of the facet and the distance to the edge that is nearest to

     * the point \a rclPt. */
    void NearestEdgeToPoint(const Base::Vector3f& rclPt, float& fDistance, unsigned short& side) const;
    /** Returns the edge for \a side. */
    MeshGeomEdge GetEdge(short side) const;
    /** The center and radius of the circum circle define a sphere in 3D. If the point \a rP is part

     * of this sphere true is returned, otherwise false.

     */
    bool IsPointOfSphere(const Base::Vector3f& rP) const;
    /** This is an overloaded member function, provided for convenience. It behaves essentially like

     * the above function. If one of the facet's points is inside the sphere true is returned,

     * otherwise false.

     */
    bool IsPointOfSphere(const MeshGeomFacet& rFacet) const;
    /** The aspect ratio is the longest edge length divided by its height.

     */
    float AspectRatio() const;
    /** The alternative aspect ration is the ratio of the radius of the circum-circle and twice the

     * radius of the in-circle.

     */
    float AspectRatio2() const;
    /** The roundness is in the range between 0.0 (colinear) and 1.0 (equilateral).

     */
    float Roundness() const;
    /** Apply a transformation on the triangle.

     */
    void Transform(const Base::Matrix4D&);
    /**

     * Checks if the two triangles are coplanar.

     */
    bool IsCoplanar(const MeshGeomFacet& facet) const;

private:
    mutable Base::Vector3f _clNormal; /**< Normal of the facet. */
    mutable bool _bNormalCalculated;  /**< True if the normal is already calculated. */

public:
    // NOLINTBEGIN
    Base::Vector3f _aclPoints[3]; /**< Geometric corner points. */
    unsigned char _ucFlag;        /**< Flag property */
    unsigned long _ulProp;        /**< Free usable property. */
                                  // NOLINTEND
};

using TMeshPointArray = std::vector<MeshPoint>;
/**

 * Stores all data points of the mesh structure.

 */
class MeshExport MeshPointArray: public TMeshPointArray
{
public:
    // Iterator interface
    using _TIterator = std::vector<MeshPoint>::iterator;
    using _TConstIterator = std::vector<MeshPoint>::const_iterator;

    /** @name Construction */
    //@{
    // constructor
    MeshPointArray() = default;
    // constructor
    explicit MeshPointArray(PointIndex ulSize)
        : TMeshPointArray(ulSize)
    {}
    /// copy-constructor
    MeshPointArray(const MeshPointArray&);
    MeshPointArray(MeshPointArray&&);
    // Destructor
    ~MeshPointArray() = default;
    //@}

    /** @name Flag state

     * @note All flag methods are const as they do NOT change the actual properties of the object

     */
    //@{
    /// Sets the flag for all points
    void SetFlag(MeshPoint::TFlagType tF) const;
    /// Resets the flag for all points
    void ResetFlag(MeshPoint::TFlagType tF) const;
    /// Sets all points invalid
    void ResetInvalid() const;
    /// Sets the property for all points
    void SetProperty(unsigned long ulVal) const;
    //@}

    // Assignment
    MeshPointArray& operator=(const MeshPointArray& rclPAry);
    MeshPointArray& operator=(MeshPointArray&& rclPAry);
    void Transform(const Base::Matrix4D&);
    /**

     * Searches for the first point index  Two points are equal if the distance is less

     * than EPSILON. If no such points is found POINT_INDEX_MAX is returned.

     */
    PointIndex Get(const MeshPoint& rclPoint);
    /**

     * Searches for the first point index  Two points are equal if the distance is less

     * than EPSILON. If no such points is found the point is added to the array at end

     * and its index is returned.

     */
    PointIndex GetOrAddIndex(const MeshPoint& rclPoint);
};

using TMeshFacetArray = std::vector<MeshFacet>;

/**

 * Stores all facets of the mesh data-structure.

 */
class MeshExport MeshFacetArray: public TMeshFacetArray
{
public:
    // Iterator interface
    using _TIterator = std::vector<MeshFacet>::iterator;
    using _TConstIterator = std::vector<MeshFacet>::const_iterator;

    /** @name Construction */
    //@{
    /// constructor
    MeshFacetArray() = default;
    /// constructor
    explicit MeshFacetArray(FacetIndex ulSize)
        : TMeshFacetArray(ulSize)
    {}
    /// copy-constructor
    MeshFacetArray(const MeshFacetArray&);
    MeshFacetArray(MeshFacetArray&&);
    /// destructor
    ~MeshFacetArray() = default;
    //@}

    /** @name Flag state

     * @note All flag methods are const as they do NOT change the actual properties

     * of the object

     */
    //@{
    /// Sets the flag for all facets.
    void SetFlag(MeshFacet::TFlagType tF) const;
    /// Resets the flag for all facets.
    void ResetFlag(MeshFacet::TFlagType tF) const;
    /// Sets all facets invalid
    void ResetInvalid() const;
    /// Sets the property for all facets
    void SetProperty(unsigned long ulVal) const;
    //@}

    // Assignment
    MeshFacetArray& operator=(const MeshFacetArray& rclFAry);
    MeshFacetArray& operator=(MeshFacetArray&& rclFAry);

    /**

     * Removes the facet from the array the iterator points to. All neighbour

     * indices of the other facets get adjusted.

     */
    void Erase(_TIterator pIter);
    /**

     * Checks and flips the point indices if needed. @see MeshFacet::Transpose().

     */
    void TransposeIndices(PointIndex ulOrig, PointIndex ulNew);
    /**

     * Decrements all point indices that are higher than \a ulIndex.

     */
    void DecrementIndices(PointIndex ulIndex);
};

/**

 * MeshPointModifier is a helper class that allows one to modify the

 * point array of a mesh kernel but with limited access.

 */
class MeshExport MeshPointModifier

{
public:
    explicit MeshPointModifier(MeshPointArray& points)
        : rPoints(points)
    {}
    ~MeshPointModifier() = default;

    MeshPointArray& GetPoints() const
    {
        return rPoints;
    }

    MeshPointModifier(const MeshPointModifier& c) = default;
    MeshPointModifier(MeshPointModifier&& c) = default;

    MeshPointModifier& operator=(const MeshPointModifier& c) = delete;
    MeshPointModifier& operator=(MeshPointModifier&& c) = delete;

private:
    MeshPointArray& rPoints;
};

/**

 * MeshFacetModifier is a helper class that allows one to modify the

 * facet array of a mesh kernel but with limited access.

 */
class MeshExport MeshFacetModifier

{
public:
    explicit MeshFacetModifier(MeshFacetArray& facets)
        : rFacets(facets)
    {}
    ~MeshFacetModifier() = default;

    MeshFacetModifier(const MeshFacetModifier& c) = default;
    MeshFacetModifier(MeshFacetModifier&& c) = default;
    MeshFacetModifier& operator=(const MeshFacetModifier& c) = delete;
    MeshFacetModifier& operator=(MeshFacetModifier&& c) = delete;

    /**

     * Replaces the index of the corner point of the facet at position \a pos

     * that is equal to \a old by \a now. If the facet does not have a corner

     * point with this index nothing happens.

     */
    void Transpose(PointIndex pos, PointIndex old, PointIndex now)
    {
        rFacets[pos].Transpose(old, now);
    }

private:
    MeshFacetArray& rFacets;
};

inline MeshPoint::MeshPoint(float x, float y, float z)
    : Base::Vector3f(x, y, z)
    , _ucFlag(0)
    , _ulProp(0)
{}

inline MeshPoint::MeshPoint(const Base::Vector3f& rclPt)
    : Base::Vector3f(rclPt)
    , _ucFlag(0)
    , _ulProp(0)
{}

inline bool MeshPoint::operator==(const MeshPoint& rclPt) const
{
    return Base::DistanceP2(*this, rclPt) < MeshDefinitions::_fMinPointDistanceP2;
}

inline bool MeshPoint::operator==(const Base::Vector3f& rclV) const
{
    return Base::DistanceP2(*this, rclV) < MeshDefinitions::_fMinPointDistanceP2;
}

inline bool MeshPoint::operator<(const MeshPoint& rclPt) const
{
    if (std::fabs(this->x - rclPt.x) >= MeshDefinitions::_fMinPointDistanceD1) {
        return this->x < rclPt.x;
    }
    if (std::fabs(this->y - rclPt.y) >= MeshDefinitions::_fMinPointDistanceD1) {
        return this->y < rclPt.y;
    }
    if (std::fabs(this->z - rclPt.z) >= MeshDefinitions::_fMinPointDistanceD1) {
        return this->z < rclPt.z;
    }
    return false;  // points are considered to be equal
}

inline float MeshGeomFacet::DistancePlaneToPoint(const Base::Vector3f& rclPoint) const
{
    // internal normal is forced to have length equal to 1
    return float(fabs(rclPoint.DistanceToPlane(_aclPoints[0], GetNormal())));
}

inline void MeshGeomFacet::CalcNormal() const
{
    _clNormal = (_aclPoints[1] - _aclPoints[0]) % (_aclPoints[2] - _aclPoints[0]);
    _clNormal.Normalize();
    _bNormalCalculated = true;
}

inline Base::Vector3f MeshGeomFacet::GetNormal() const
{
    if (!_bNormalCalculated) {
        CalcNormal();
    }
    return _clNormal;
}

inline void MeshGeomFacet::SetNormal(const Base::Vector3f& rclNormal)
{
    if (rclNormal.Sqr() == 0.0F) {
        return;
    }
    _clNormal = rclNormal;
    _clNormal.Normalize();
    _bNormalCalculated = true;
}

inline void MeshGeomFacet::ArrangeNormal(const Base::Vector3f& rclN)
{
    // force internal normal to be computed if not done yet
    if ((rclN * GetNormal()) < 0.0F) {
        _clNormal = -_clNormal;
    }
}

inline Base::Vector3f MeshGeomFacet::GetGravityPoint() const
{
    return (1.0F / 3.0F) * (_aclPoints[0] + _aclPoints[1] + _aclPoints[2]);
}

inline void MeshGeomFacet::AdjustCirculationDirection()
{
    Base::Vector3f clN = (_aclPoints[1] - _aclPoints[0]) % (_aclPoints[2] - _aclPoints[0]);
    if ((clN * _clNormal) < 0.0F) {
        std::swap(_aclPoints[1], _aclPoints[2]);
    }
}

inline Base::BoundBox3f MeshGeomFacet::GetBoundBox() const
{
    return {_aclPoints, 3};
}

inline float MeshGeomFacet::Perimeter() const
{
    float perimeter = 0.0F;
    perimeter += Base::Distance(_aclPoints[0], _aclPoints[1]);
    perimeter += Base::Distance(_aclPoints[1], _aclPoints[2]);
    perimeter += Base::Distance(_aclPoints[2], _aclPoints[0]);
    return perimeter;
}

inline float MeshGeomFacet::Area() const
{
    return ((_aclPoints[1] - _aclPoints[0]) % (_aclPoints[2] - _aclPoints[0])).Length() / 2.0F;
}

inline bool MeshGeomFacet::ContainedByOrIntersectBoundingBox(const Base::BoundBox3f& rclBB) const
{
    // Test, if all corner points of the facet are on one of the 6 sides of the BB
    if (!(GetBoundBox() && rclBB)) {
        return false;
    }

    // Test, whether Facet-BB is completely within BB
    if (rclBB.IsInBox(GetBoundBox())) {
        return true;
    }

    // Test, whether one of the corner points is in BB
    for (auto pnt : _aclPoints) {
        if (rclBB.IsInBox(pnt)) {
            return true;
        }
    }

    // "real" test for cutting
    return (IntersectBoundingBox(rclBB));
}

inline bool MeshGeomFacet::IntersectWithPlane(

    const Base::Vector3f& rclBase,

    const Base::Vector3f& rclNormal

) const
{
    bool bD0 = (_aclPoints[0].DistanceToPlane(rclBase, rclNormal) > 0.0F);
    return !(
        (bD0 == (_aclPoints[1].DistanceToPlane(rclBase, rclNormal) > 0.0F))
        && (bD0 == (_aclPoints[2].DistanceToPlane(rclBase, rclNormal) > 0.0F))
    );
}

inline MeshFacet::MeshFacet()  // NOLINT
    : _ucFlag(0)
    , _ulProp(0)
{
    memset(_aulNeighbours, 0xff, sizeof(FacetIndex) * 3);
    memset(_aulPoints, 0xff, sizeof(PointIndex) * 3);
}

inline MeshFacet::MeshFacet(

    PointIndex p1,

    PointIndex p2,

    PointIndex p3,

    FacetIndex n1,

    FacetIndex n2,

    FacetIndex n3

)
    : _ucFlag(0)
    , _ulProp(0)
    , _aulPoints {p1, p2, p3}
    , _aulNeighbours {n1, n2, n3}
{}

void MeshFacet::SetVertices(PointIndex p1, PointIndex p2, PointIndex p3)
{
    _aulPoints[0] = p1;
    _aulPoints[1] = p2;
    _aulPoints[2] = p3;
}

void MeshFacet::SetNeighbours(FacetIndex n1, FacetIndex n2, FacetIndex n3)
{
    _aulNeighbours[0] = n1;
    _aulNeighbours[1] = n2;
    _aulNeighbours[2] = n3;
}

inline void MeshFacet::GetEdge(unsigned short usSide, MeshHelpEdge& rclEdge) const
{
    rclEdge._ulIndex[0] = _aulPoints[usSide];
    rclEdge._ulIndex[1] = _aulPoints[(usSide + 1) % 3];
}

inline std::pair<PointIndex, PointIndex> MeshFacet::GetEdge(unsigned short usSide) const
{
    return {_aulPoints[usSide], _aulPoints[(usSide + 1) % 3]};
}

inline void MeshFacet::Transpose(PointIndex ulOrig, PointIndex ulNew)
{
    if (_aulPoints[0] == ulOrig) {
        _aulPoints[0] = ulNew;
    }
    else if (_aulPoints[1] == ulOrig) {
        _aulPoints[1] = ulNew;
    }
    else if (_aulPoints[2] == ulOrig) {
        _aulPoints[2] = ulNew;
    }
}

inline void MeshFacet::Decrement(PointIndex ulIndex)
{
    if (_aulPoints[0] > ulIndex) {
        _aulPoints[0]--;
    }
    if (_aulPoints[1] > ulIndex) {
        _aulPoints[1]--;
    }
    if (_aulPoints[2] > ulIndex) {
        _aulPoints[2]--;
    }
}

inline bool MeshFacet::HasPoint(PointIndex ulIndex) const
{
    if (_aulPoints[0] == ulIndex) {
        return true;
    }
    if (_aulPoints[1] == ulIndex) {
        return true;
    }
    if (_aulPoints[2] == ulIndex) {
        return true;
    }
    return false;
}

inline void MeshFacet::ReplaceNeighbour(FacetIndex ulOrig, FacetIndex ulNew)
{
    if (_aulNeighbours[0] == ulOrig) {
        _aulNeighbours[0] = ulNew;
    }
    else if (_aulNeighbours[1] == ulOrig) {
        _aulNeighbours[1] = ulNew;
    }
    else if (_aulNeighbours[2] == ulOrig) {
        _aulNeighbours[2] = ulNew;
    }
}

inline unsigned short MeshFacet::CountOpenEdges() const
{
    unsigned short ct = 0;
    for (unsigned short i = 0; i < 3; i++) {
        if (!HasNeighbour(i)) {
            ct++;
        }
    }
    return ct;
}

inline bool MeshFacet::HasOpenEdge() const
{
    return (CountOpenEdges() != 0);
}

inline bool MeshFacet::HasSameOrientation(const MeshFacet& f) const
{
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (_aulPoints[i] == f._aulPoints[j]) {
                if ((_aulPoints[(i + 1) % 3] == f._aulPoints[(j + 1) % 3])
                    || (_aulPoints[(i + 2) % 3] == f._aulPoints[(j + 2) % 3])) {
                    return false;
                }
            }
        }
    }

    return true;
}

inline bool MeshFacet::IsDegenerated() const
{
    if (_aulPoints[0] == _aulPoints[1]) {
        return true;
    }
    if (_aulPoints[1] == _aulPoints[2]) {
        return true;
    }
    if (_aulPoints[2] == _aulPoints[0]) {
        return true;
    }
    return false;
}

inline unsigned short MeshFacet::Side(FacetIndex ulNIndex) const
{
    if (_aulNeighbours[0] == ulNIndex) {
        return 0;
    }
    if (_aulNeighbours[1] == ulNIndex) {
        return 1;
    }
    if (_aulNeighbours[2] == ulNIndex) {
        return 2;
    }

    return std::numeric_limits<unsigned short>::max();
}

inline unsigned short MeshFacet::Side(PointIndex ulP0, PointIndex ulP1) const
{
    if (_aulPoints[0] == ulP0) {
        if (_aulPoints[1] == ulP1) {
            return 0;  // Edge 0-1 ==> 0
        }
        if (_aulPoints[2] == ulP1) {
            return 2;  // Edge 0-2 ==> 2
        }
    }
    else if (_aulPoints[1] == ulP0) {
        if (_aulPoints[0] == ulP1) {
            return 0;  // Edge 1-0 ==> 0
        }
        if (_aulPoints[2] == ulP1) {
            return 1;  // Edge 1-2 ==> 1
        }
    }
    else if (_aulPoints[2] == ulP0) {
        if (_aulPoints[0] == ulP1) {
            return 2;  // Edge 2-0 ==> 2
        }
        if (_aulPoints[1] == ulP1) {
            return 1;  // Edge 2-1 ==> 1
        }
    }

    return std::numeric_limits<unsigned short>::max();
}

inline unsigned short MeshFacet::Side(const MeshFacet& rFace) const
{
    unsigned short side {};
    for (int i = 0; i < 3; i++) {
        side = Side(rFace._aulPoints[i], rFace._aulPoints[(i + 1) % 3]);
        if (side != std::numeric_limits<unsigned short>::max()) {
            return side;
        }
    }

    return std::numeric_limits<unsigned short>::max();
}

inline bool MeshFacet::IsEqual(const MeshFacet& rcFace) const
{
    for (int i = 0; i < 3; i++) {
        if (this->_aulPoints[0] == rcFace._aulPoints[i]) {
            if (this->_aulPoints[1] == rcFace._aulPoints[(i + 1) % 3]
                && this->_aulPoints[2] == rcFace._aulPoints[(i + 2) % 3]) {
                return true;
            }
            if (this->_aulPoints[1] == rcFace._aulPoints[(i + 2) % 3]
                && this->_aulPoints[2] == rcFace._aulPoints[(i + 1) % 3]) {
                return true;
            }
        }
    }

    return false;
}

/**

 * Binary function to query the flags for use with generic STL functions.

 */
template<class TCLASS>
class MeshIsFlag

{
public:
    using first_argument_type = TCLASS;
    using second_argument_type = typename TCLASS::TFlagType;
    using result_type = bool;
    bool operator()(const TCLASS& rclElem, typename TCLASS::TFlagType tFlag) const
    {
        return rclElem.IsFlag(tFlag);
    }
};

/**

 * Binary function to query the flags for use with generic STL functions.

 */
template<class TCLASS>
class MeshIsNotFlag

{
public:
    using first_argument_type = TCLASS;
    using second_argument_type = typename TCLASS::TFlagType;
    using result_type = bool;
    bool operator()(const TCLASS& rclElem, typename TCLASS::TFlagType tFlag) const
    {
        return !rclElem.IsFlag(tFlag);
    }
};

/**

 * Binary function to set the flags for use with generic STL functions.

 */
template<class TCLASS>
class MeshSetFlag

{
public:
    using first_argument_type = TCLASS;
    using second_argument_type = typename TCLASS::TFlagType;
    using result_type = bool;
    bool operator()(const TCLASS& rclElem, typename TCLASS::TFlagType tFlag) const
    {
        rclElem.SetFlag(tFlag);
        return true;
    }
};

/**

 * Binary function to reset the flags for use with generic STL functions.

 */
template<class TCLASS>
class MeshResetFlag

{
public:
    using first_argument_type = TCLASS;
    using second_argument_type = typename TCLASS::TFlagType;
    using result_type = bool;
    bool operator()(const TCLASS& rclElem, typename TCLASS::TFlagType tFlag) const
    {
        rclElem.ResetFlag(tFlag);
        return true;
    }
};

}  // namespace MeshCore

#endif  // MESH_ELEMENTS_H