File size: 11,129 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
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  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/>.

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

#include "star.H"

// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //

template<class FaceEdges>
void Foam::star::cross
(
    const label edgei,
    const label facei,
    const UList<FaceEdges>& faceEdges
)
{
    // Add the new face to the star
    starFaceFaces_.append(facei);
    faceStarFaces_[facei] = starFaceFaces_.size() - 1;

    // Store information about this edge, then remove it from the star
    const label faceEdgei = findIndex(faceEdges[facei], edgei);
    const label starEdgei = edgeStarEdges_[faceEdges[facei][faceEdgei]];
    const label starEdgei0 = starEdgeEdges_[starEdgei].starEdgei0_;
    const label starEdgei1 = starEdgeEdges_[starEdgei].starEdgei1_;
    edgeStarEdges_[starEdgeEdges_[starEdgei].edgei_] = -1;
    starEdgeEdges_[starEdgei] = {-1, -1, -1};

    // Walk backwards and forwards around the face until we find edges not
    // already in the star. Remove edges in the star as we go.
    label faceEdgeiR = faceEdgei, starEdgeiR1 = starEdgei1;
    while (true)
    {
        const label faceEdgeiRR = faceEdges[facei].rcIndex(faceEdgeiR);
        const label edgeiRR = faceEdges[facei][faceEdgeiRR];
        const label starEdgeiRR = edgeStarEdges_[edgeiRR];
        if (starEdgeiRR == -1 || starEdgeiRR != starEdgeiR1) break;
        faceEdgeiR = faceEdgeiRR;
        starEdgeiR1 = starEdgeEdges_[starEdgeiRR].starEdgei1_;
        edgeStarEdges_[starEdgeEdges_[starEdgeiRR].edgei_] = -1;
        starEdgeEdges_[starEdgeiRR] = {-1, -1, -1};
    }
    label faceEdgeiF = faceEdgei, starEdgeiF0 = starEdgei0;
    while (true)
    {
        const label faceEdgeiFF = faceEdges[facei].fcIndex(faceEdgeiF);
        const label edgeiFF = faceEdges[facei][faceEdgeiFF];
        const label starEdgeiFF = edgeStarEdges_[edgeiFF];
        if (starEdgeiFF == -1 || starEdgeiFF != starEdgeiF0) break;
        faceEdgeiF = faceEdgeiFF;
        starEdgeiF0 = starEdgeEdges_[starEdgeiFF].starEdgei0_;
        edgeStarEdges_[starEdgeEdges_[starEdgeiFF].edgei_] = -1;
        starEdgeEdges_[starEdgeiFF] = {-1, -1, -1};
    }

    // Get the first edge after the forwards edge
    label faceEdgej = faceEdges[facei].fcIndex(faceEdgeiF);

    // If there are no face edges not yet in the star, then all edges are
    // to be removed. Just connect up the adjacent edges.
    if (faceEdgej == faceEdgeiR)
    {
        starEdgeEdges_[starEdgeiF0].starEdgei1_ = starEdgeiR1;
        starEdgeEdges_[starEdgeiR1].starEdgei0_ = starEdgeiF0;
    }

    // If there are face edges not yet in the star, then loop over them and
    // add them into the star
    while (faceEdgej != faceEdgeiR)
    {
        const bool isFirst =
            faceEdgej == faceEdges[facei].fcIndex(faceEdgeiF);
        const bool isLast =
            faceEdgej == faceEdges[facei].rcIndex(faceEdgeiR);

        const label starEdgej0 =
            isFirst ? starEdgeiF0 : starEdgeEdges_.size() - 1;
        const label starEdgej = starEdgeEdges_.size();
        const label starEdgej1 =
            isLast ? starEdgeiR1 : starEdgeEdges_.size() + 1;

        const label edgej = faceEdges[facei][faceEdgej];

        if (isFirst && starEdgeiF0 != -1)
        {
            starEdgeEdges_[starEdgeiF0].starEdgei1_ = starEdgej;
        }

        starEdgeEdges_.append({starEdgej0, edgej, starEdgej1});
        edgeStarEdges_[edgej] = starEdgej;

        if (isLast && starEdgeiR1 != -1)
        {
            starEdgeEdges_[starEdgeiR1].starEdgei0_ = starEdgej;
        }

        faceEdgej = faceEdges[facei].fcIndex(faceEdgej);
    }
}



// * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //

template<class CanCross, class FaceEdges>
Foam::star::context Foam::star::populate
(
    const label faceOrEdgei,
    const bool isFace,
    CanCross canCross,
    const UList<FaceEdges>& faceEdges,
    const UList<labelPair>& edgeFaces,
    const label maxIterations
)
{
    // Expand to fit supplied data
    faceStarFaces_.resize(faceEdges.size(), -1);
    edgeStarEdges_.resize(edgeFaces.size(), -1);

    // Initialise
    if (isFace)
    {
        // Add one edge from the to the star and then cross into the face
        const label edgei = faceEdges[faceOrEdgei][0];
        starEdgeEdges_.append({-1, edgei, -1});
        edgeStarEdges_[edgei] = 0;
        cross(edgei, faceOrEdgei, faceEdges);

        // Add the edge back again as it was removed by the crossing. Reuse the
        // first element of the starEdgeEdges array. Join up to close the star.
        const label starEdgei0 = starEdgeEdges_.size() - 1;
        const label starEdgei1 = 1;
        starEdgeEdges_[0] = {starEdgei0, edgei, starEdgei1};
        edgeStarEdges_[edgei] = 0;
        starEdgeEdges_[starEdgei0].starEdgei1_ = 0;
        starEdgeEdges_[starEdgei1].starEdgei0_ = 0;
    }
    else
    {
        bool first = true;

        // Loop the adjacent faces
        forAll(edgeFaces[faceOrEdgei], edgeFacei)
        {
            const label facei = edgeFaces[faceOrEdgei][edgeFacei];
            if (facei == -1) continue;

            // If the first face, add the edge to the star
            if (first)
            {
                starEdgeEdges_.append({-1, faceOrEdgei, -1});
                edgeStarEdges_[faceOrEdgei] = 0;
                first = false;
            }
            // If the second face, add the edge into the star again as it was
            // removed by the previous crossing. Reuse the first element of the
            // starEdgeEdges array. Join up to close the star.
            else
            {
                const label starEdgei0 = starEdgeEdges_.size() - 1;
                const label starEdgei1 = 1;
                starEdgeEdges_[0] = {starEdgei0, faceOrEdgei, starEdgei1};
                edgeStarEdges_[faceOrEdgei] = 0;
                starEdgeEdges_[starEdgei0].starEdgei1_ = 0;
                starEdgeEdges_[starEdgei1].starEdgei0_ = 0;
            }

            // Cross the edge into the face
            cross(faceOrEdgei, facei, faceEdges);
        }
    }

    // Walk the star to get intersected faces and their perimeter
    label iterations = 0;
    forAll(starEdgeEdges_, starEdgei)
    {
        if (iterations == maxIterations) break;
        ++ iterations;

        const label edgei = starEdgeEdges_[starEdgei].edgei_;
        if (edgei == -1) continue;

        // Get the adjacent non-star face
        label facei = -1;
        forAll(edgeFaces[edgei], edgeFacei)
        {
            facei = edgeFaces[edgei][edgeFacei];
            if (facei != -1 && faceStarFaces_[facei] == -1)
            {
                break;
            }
            facei = -1;
        }

        // If the crossing condition is satisfied then expand the star
        if (facei != -1 && canCross(edgei, facei))
        {
            cross(edgei, facei, faceEdges);
        }
    }

    // Remove spikes
    forAll(starEdgeEdges_, starEdgei)
    {
        label starEdgei0 = starEdgei;
        label starEdgei1 = starEdgeEdges_[starEdgei0].starEdgei1_;

        if (starEdgei1 == -1) continue;

        label edgei0 = starEdgeEdges_[starEdgei0].edgei_;
        label edgei1 = starEdgeEdges_[starEdgei1].edgei_;

        while (edgei0 != -1 && edgei1 != -1 && edgei0 == edgei1)
        {
            const label starEdgei00 = starEdgeEdges_[starEdgei0].starEdgei0_;
            const label starEdgei11 = starEdgeEdges_[starEdgei1].starEdgei1_;

            edgeStarEdges_[edgei0] = -1;
            starEdgeEdges_[starEdgei0] = {-1, -1, -1};
            edgeStarEdges_[edgei1] = -1;
            starEdgeEdges_[starEdgei1] = {-1, -1, -1};

            if (starEdgei00 != -1)
            {
                starEdgeEdges_[starEdgei00].starEdgei1_ = starEdgei11;
                starEdgei0 = starEdgei00;
                edgei0 = starEdgeEdges_[starEdgei00].edgei_;
            }
            else
            {
                starEdgei0 = edgei0 = -1;
            }

            if (starEdgei11 != -1)
            {
                starEdgeEdges_[starEdgei11].starEdgei0_ = starEdgei00;
                starEdgei1 = starEdgei11;
                edgei1 = starEdgeEdges_[starEdgei11].edgei_;
            }
            else
            {
                starEdgei1 = edgei0 = -1;
            }
        }
    }

    // Allocate map storage
    DynamicList<label>& oldStarEdgeNewStarEdges = work_;
    oldStarEdgeNewStarEdges.resize(starEdgeEdges_.size());
    oldStarEdgeNewStarEdges = -1;

    // Remove empty edges by shuffling up
    label starEdgei = 0;
    forAll(starEdgeEdges_, starEdgej)
    {
        if (starEdgeEdges_[starEdgej].edgei_ != -1)
        {
            oldStarEdgeNewStarEdges[starEdgej] = starEdgei;
            starEdgeEdges_[starEdgei] = starEdgeEdges_[starEdgej];
            ++ starEdgei;
        }
    }
    starEdgeEdges_.resize(starEdgei);

    // Map
    forAll(starEdgeEdges_, starEdgei)
    {
        label& starEdgei0 = starEdgeEdges_[starEdgei].starEdgei0_;
        if (starEdgei0 != -1)
        {
            starEdgei0 = oldStarEdgeNewStarEdges[starEdgei0];
        }
        label& starEdgei1 = starEdgeEdges_[starEdgei].starEdgei1_;
        if (starEdgei1 != -1)
        {
            starEdgei1 = oldStarEdgeNewStarEdges[starEdgei1];
        }
    }

    /*
    // Randomise swapping
    static randomGenerator rndGen(12345);
    const label starEdgeiA = rndGen.sampleAB<label>(0, starEdgeEdges_.size());
    const label starEdgeiB = rndGen.sampleAB<label>(0, starEdgeEdges_.size());
    swapStarEdges(starEdgeiA, starEdgeiB);
    */

    // If the star is open, put the first edge first, so that forAllStarEdges
    // always starts in the right place
    forAll(starEdgeEdges_, starEdgei)
    {
        if (starEdgeEdges_[starEdgei].starEdgei0_ == -1)
        {
            swapStarEdges(starEdgei, 0);
        }
    }

    return context(*this);
}


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