File size: 10,701 Bytes
5a600cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     | Website:  https://openfoam.org
    \\  /    A nd           | Copyright (C) 2021-2024 OpenFOAM Foundation
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM 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 General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

Class
    Foam::polygonTriangulate

Description
    Triangulation of three-dimensional polygons

SourceFiles
    polygonTriangulate.C

\*---------------------------------------------------------------------------*/

#ifndef polygonTriangulate_H
#define polygonTriangulate_H

#include "DynamicList.H"
#include "HashSet.H"
#include "labelPair.H"
#include "triFace.H"

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

namespace Foam
{

/*---------------------------------------------------------------------------*\
                          Class polygonTriangulate Declaration
\*---------------------------------------------------------------------------*/

class polygonTriangulate
{
    // Private Data

        //- Indirect addressing into point field to form the current as yet
        //  un-triangulated polygon
        DynamicList<label> pointis_;

        //- Edge indices of the un-triangulated polygon
        DynamicList<label> edges_;

        //- The interior angles of the vertices of the un-triangulated polygon
        DynamicList<scalar> angle_;

        //- Flags to denote whether points of the un-triangulated polygon are
        //  "ears"; i.e., that the triangle formed by adding a diagonal between
        //  two adjacent points contains no other point
        DynamicList<bool> ear_;

        //- Set of tri-faces used to mark previously considered configurations
        //  in optimisation and prevent unnecessary repetition
        HashSet<triFace, Hash<triFace>> triPointsSet_;

        //- Point array. Used if the input points cannot be SubList-d.
        DynamicList<point> points_;

        //- Tri-points
        DynamicList<triFace> triPoints_;

        //- Tri-edge associations
        DynamicList<FixedList<label, 3>> triEdges_;

        //- Edge-tri associations
        DynamicList<labelPair> edgeTris_;


    // Private Static Member Functions

        // Renumber a polygon label to global indexing
        static inline label renumberPolyToGlobal
        (
            const label triPoly,
            const UList<label>& polyToGlobal
        );

        // Renumber a container of polygon labels to global indexing
        template<class Type>
        static inline Type renumberPolyToGlobal
        (
            const Type& triPoly,
            const UList<label>& polyToGlobal
        );

        //- Return the area of a triangle projected in a normal direction
        template<class PointField>
        static scalar area
        (
            const triFace& triPoints,
            const PointField& points,
            const vector& normal
        );

        //- Return the quality of a triangle projected in a normal direction
        template<class PointField>
        static scalar quality
        (
            const triFace& triPoints,
            const PointField& points,
            const vector& normal
        );

        //- Return whether or not two edges intersect in the plane defined by a
        //  normal
        template<class PointField>
        static bool intersection
        (
            const edge& edgePointsA,
            const edge& edgePointsB,
            const PointField& points,
            const vector& normal
        );

        //- Return the number of intersections of an edge within a polygon
        template<class PointField>
        static label nIntersections
        (
            const edge& edgePoints,
            const PointField& points,
            const vector& normal
        );

        //- Return the interior angle of the point in a polygon in the plane
        //  defined by a normal
        template<class PointField>
        static scalar angle
        (
            const label pointi,
            const PointField& points,
            const vector& normal
        );

        //- Return whether a point in a polygon is an "ear"; i.e., that the
        //  triangle formed by adding a diagonal between the two adjacent
        //  points contains no other point
        template<class PointField>
        static bool ear
        (
            const label pointi,
            const PointField& points,
            const vector& normal
        );


    // Private Member Functions

        //- Optimise the triangulation quality by flipping edges
        template<class PointField>
        void optimiseTriangulation
        (
            const label trii,
            const PointField& points,
            const vector& normal,
            UList<triFace>& triPoints,
            UList<FixedList<label, 3>>& triEdges,
            UList<labelPair>& edgeTris
        );

        //- Perform triangulation of a polygon by ear clipping, assuming that
        //  the polygon is simple/non-intersecting
        void simpleTriangulate
        (
            const UList<point>& points,
            const vector& normal,
            UList<triFace>& triPoints,
            UList<FixedList<label, 3>>& triEdges,
            UList<labelPair>& edgeTris,
            const bool optimal
        );

        //- Perform triangulation of a polygon, first by adding a spanning
        //  triangle between a point and an edge, then recursing into the
        //  sub-polygons on either side
        void partitionTriangulate
        (
            const UList<point>& points,
            const vector& normal,
            const label spanPointi,
            const label spanEdgei,
            UList<triFace>& triPoints,
            UList<FixedList<label, 3>>& triEdges,
            UList<labelPair>& edgeTris,
            const bool simple,
            const bool optimal
        );

        //- Perform triangulation of the given polygon, detecting self
        //  intersections and resolving them by adding spanning triangles prior
        //  to simple triangulation
        void complexTriangulate
        (
            const UList<point>& points,
            const vector& normal,
            UList<triFace>& triPoints,
            UList<FixedList<label, 3>>& triEdges,
            UList<labelPair>& edgeTris,
            const bool optimal
        );

        //- Perform triangulation of the given polygon
        void triangulate
        (
            const UList<point>& points,
            const vector& normal,
            UList<triFace>& triPoints,
            UList<FixedList<label, 3>>& triEdges,
            UList<labelPair>& edgeTris,
            const bool simple = false,
            const bool optimal = true
        );


public:

    // Static Member Functions

        //- Generate a random polygon for testing purposes
        static List<point> randomPolygon
        (
            randomGenerator& rndGen,
            const label n,
            const scalar error
        );



    // Constructors

        //- Construct null
        polygonTriangulate();

        //- Disallow default bitwise copy construction
        polygonTriangulate(const polygonTriangulate&) = delete;


    //- Destructor
    ~polygonTriangulate();


    // Member Functions

        // Access

            //- Get the triangles' points
            inline const UList<triFace>& triPoints() const;

            //- Get the triangles' renumbered points
            inline List<triFace> triPoints
            (
                const UList<label>& polyPoints
            ) const;

            //- Get a triangle's renumbered points
            inline triFace triPoints
            (
                const label trii,
                const UList<label>& polyPoints
            ) const;

            //- Get the triangles' edges
            inline const UList<FixedList<label, 3>>& triEdges() const;

            //- Get the triangles' renumbered edges
            inline List<FixedList<label, 3>> triEdges
            (
                const UList<label>& polyEdges
            ) const;

            //- Get a triangle's renumbered edges
            inline FixedList<label, 3> triEdges
            (
                const label trii,
                const UList<label>& polyEdges
            ) const;


        //- Perform triangulation of the given polygon
        const UList<triFace>& triangulate
        (
            const UList<point>& points,
            const vector& normal,
            const bool simple = false,
            const bool optimal = true
        );

        //- Perform triangulation of the given polygon
        const UList<triFace>& triangulate
        (
            const UList<point>& points,
            const bool simple = false,
            const bool optimal = true
        );

        //- Perform triangulation of the given polygon
        inline const UList<triFace>& triangulate
        (
            const UIndirectList<point>& points,
            const vector& normal,
            const bool simple = false,
            const bool optimal = true
        );

        //- Perform triangulation of the given polygon
        inline const UList<triFace>& triangulate
        (
            const UIndirectList<point>& points,
            const bool simple = false,
            const bool optimal = true
        );


    // Member Operators

        //- Disallow default bitwise assignment
        void operator=(const polygonTriangulate&) = delete;
};


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

} // End namespace Foam

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

#include "polygonTriangulateI.H"

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

#endif

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