meshOctreeNeighbourSearches.C 13.1 KB
Newer Older
Franjo's avatar
Franjo committed
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
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | cfMesh: A library for mesh generation
   \\    /   O peration     |
    \\  /    A nd           | Author: Franjo Juretic (franjo.juretic@c-fields.com)
     \\/     M anipulation  | Copyright (C) Creative Fields, Ltd.
-------------------------------------------------------------------------------
License
    This file is part of cfMesh.

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

    cfMesh 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 cfMesh.  If not, see <http://www.gnu.org/licenses/>.

Description

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

#include "meshOctree.H"
#include "boundBox.H"

//#define OCTREE_DEBUG

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

namespace Foam
{

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Private member functions

label meshOctree::findLeafContainingVertex
(
    const point& p
) const
{
    # ifdef OCTREE_DEBUG
    Info << "Finding leaf for vertex " << p << endl;
    # endif

    const meshOctreeCube* ocPtr = initialCubePtr_;

    if( !ocPtr->isVertexInside(rootBox_, p) )
    {
        # ifdef OCTREE_DEBUG
        Info << "Vertex " << p << " is not in the initial cube" << endl;
        # endif
Franjo's avatar
Franjo committed
57
58
59
60
61
62
63
64

        FatalErrorIn
        (
            "label meshOctree::findLeafContainingVertex(const point&) const"
        ) << "Point " << p << " is not inside the initial cube" << endl;

        throw "Found invalid locations of points";

Franjo's avatar
Franjo committed
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
        return -1;
    }

    bool finished(false);

    do
    {
        if( ocPtr && !ocPtr->isLeaf() )
        {
            //- find a subCube containing the vertex;
            const point c = ocPtr->centre(rootBox_);

            label scI(0);

            if( p.x() >= c.x() )
                scI |= 1;
            if( p.y() >= c.y() )
                scI |= 2;
            if( !isQuadtree_ && p.z() >= c.z() )
                scI |= 4;

            ocPtr = ocPtr->subCube(scI);
        }
        else
        {
            finished = true;
        }
    } while( !finished );

    if( ocPtr )
    {
        return ocPtr->cubeLabel();
    }

    return meshOctreeCubeBasic::OTHERPROC;
}

102
103
104
105
106
107
108
109
110
111
112
113
void meshOctree::findLeavesInSphere
(
    const point& c,
    const scalar r,
    DynList<label>& containedLeaves
) const
{
    containedLeaves.clear();

    initialCubePtr_->leavesInSphere(rootBox_, c, r, containedLeaves);
}

Franjo's avatar
Franjo committed
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
label meshOctree::findNeighbourOverNode
(
    const meshOctreeCubeCoordinates& cc,
    const label nodeI
) const
{
    if( isQuadtree_ )
        return -1;

    const meshOctreeCubeCoordinates nc(cc + regularityPositions_[18+nodeI]);

    const meshOctreeCube* neiPtr = findCubeForPosition(nc);

    if( !neiPtr )
    {
        const label levelLimiter = (1 << cc.level());
        if(
            (nc.posX() >= levelLimiter) || (nc.posX() < 0) ||
            (nc.posY() >= levelLimiter) || (nc.posY() < 0) ||
            (!isQuadtree_ && (nc.posZ() >= levelLimiter || nc.posZ() < 0)) ||
            (isQuadtree_ && (nc.posZ() != initialCubePtr_->posZ()))
        )
        {
            return -1;
        }
        else if( Pstream::parRun() )
        {
            return meshOctreeCubeBasic::OTHERPROC;
        }

        return -1;
    }
    else if( neiPtr->isLeaf() )
    {
        # ifdef OCTREE_DEBUG
        if( leaves_[neiPtr->cubeLabel()] != neiPtr )
            FatalError << "Cube does not correspond to itself"
                << abort(FatalError);
        # endif
        return neiPtr->cubeLabel();
    }
    else
    {
        FixedList<label, 8> sc(-1);
        for(label scI=0;scI<8;++scI)
        {
            meshOctreeCube* scPtr = neiPtr->subCube(scI);
            if( scPtr )
            {
                sc[scI] = scPtr->cubeLabel();
            }
            else if( Pstream::parRun() )
            {
                sc[scI] = meshOctreeCubeBasic::OTHERPROC;
            }
        }

        return sc[7-nodeI];
    }

    FatalErrorIn
    (
        "label meshOctree::findNeighbourOverNode("
        "const meshOctreeCubeCoordinates& cc,"
        "const label nodeI) const"
    ) << "Should not be here!" << abort(FatalError);

    return -1;
}

void meshOctree::findNeighboursOverEdge
(
    const meshOctreeCubeCoordinates& cc,
    const label eI,
    DynList<label>& neighbourLeaves
) const
{
    if( isQuadtree_ && (eI >= 8) )
    {
        neighbourLeaves.append(-1);
        return;
    }

    const meshOctreeCubeCoordinates nc(cc + regularityPositions_[6+eI]);

    const meshOctreeCube* neiPtr = findCubeForPosition(nc);

    if( !neiPtr )
    {
        const label levelLimiter = (1 << cc.level());
        if(
            (nc.posX() >= levelLimiter) || (nc.posX() < 0) ||
            (nc.posY() >= levelLimiter) || (nc.posY() < 0) ||
            (!isQuadtree_ && (nc.posZ() >= levelLimiter || nc.posZ() < 0)) ||
            (isQuadtree_ && (nc.posZ() != initialCubePtr_->posZ()))
        )
        {
            neighbourLeaves.append(-1);
        }
        else if( Pstream::parRun() )
        {
            neighbourLeaves.append(meshOctreeCubeBasic::OTHERPROC);
        }
    }
    else if( neiPtr->isLeaf() )
    {
        # ifdef OCTREE_DEBUG
        if( leaves_[neiPtr->cubeLabel()] != neiPtr )
            FatalError << "Cube does not correspond to itself"
                << abort(FatalError);
        # endif
        neighbourLeaves.append(neiPtr->cubeLabel());
    }
    else
    {
        FixedList<label, 8> sc(-1);
        for(label scI=0;scI<8;++scI)
        {
            meshOctreeCube* scPtr = neiPtr->subCube(scI);

            if( scPtr )
            {
                sc[scI] = scPtr->cubeLabel();
            }
            else if( Pstream::parRun() )
            {
                sc[scI] = meshOctreeCubeBasic::OTHERPROC;
            }
        }

        const label* eNodes = meshOctreeCubeCoordinates::edgeNodes_[eI];

        if( !isQuadtree_)
        {
            neighbourLeaves.append(sc[7-eNodes[1]]);
            neighbourLeaves.append(sc[7-eNodes[0]]);
        }
        else
        {
            if( sc[7-eNodes[1]] >= 0 )
                neighbourLeaves.append(sc[7-eNodes[1]]);
            if( (sc[7-eNodes[0]] >= 0) && (sc[7-eNodes[0]] != sc[7-eNodes[1]]) )
                neighbourLeaves.append(sc[7-eNodes[0]]);
        }
    }
}

void meshOctree::findNeighboursInDirection
(
    const meshOctreeCubeCoordinates& cc,
    const label dir,
    DynList<label>& neighbourLeaves
) const
{
    if( isQuadtree_ && dir >= 4 )
    {
        neighbourLeaves.append(-1);
        return;
    }

    label cpx = cc.posX();
    label cpy = cc.posY();
    label cpz = cc.posZ();
    switch( dir )
    {
        case 0:
        {
            cpx -= 1;
        }
        break;
        case 1:
        {
            cpx += 1;
        }
        break;
        case 2:
        {
            cpy -= 1;
        }
        break;
        case 3:
        {
            cpy += 1;
        }
        break;
        case 4:
        {
            cpz -= 1;
        }
        break;
        case 5:
        {
            cpz += 1;
        }
        break;
    }

    const meshOctreeCube* neiPtr =
        findCubeForPosition
        (
            meshOctreeCubeCoordinates(cpx, cpy, cpz, cc.level())
        );

    if( !neiPtr )
    {
        const label levelLimiter = (1 << cc.level());
        if(
            (cpx >= levelLimiter) || (cpx < 0) ||
            (cpy >= levelLimiter) || (cpy < 0) ||
            (!isQuadtree_ && (cpz >= levelLimiter || cpz < 0)) ||
            (isQuadtree_ && (cpz != initialCubePtr_->posZ()))
        )
        {
            neighbourLeaves.append(-1);
        }
        else if( Pstream::parRun() )
        {
            neighbourLeaves.append(meshOctreeCubeBasic::OTHERPROC);
        }
    }
    else if( neiPtr->isLeaf() )
    {
        # ifdef OCTREE_DEBUG
        if( leaves_[neiPtr->cubeLabel()] != neiPtr )
            FatalError << "Cube does not correspond to itself"
                << abort(FatalError);
        # endif
        neighbourLeaves.append(neiPtr->cubeLabel());
    }
    else
    {
        FixedList<label, 8> sc(-1);
        for(label scI=0;scI<8;++scI)
        {
            meshOctreeCube* scPtr = neiPtr->subCube(scI);

            if( scPtr )
            {
                sc[scI] = scPtr->cubeLabel();
            }
            else if( Pstream::parRun() )
            {
                sc[scI] = meshOctreeCubeBasic::OTHERPROC;
            }
        }

        const label* fNodes = meshOctreeCubeCoordinates::faceNodes_[dir];
        for(label i=0;i<4;++i)
        {
            if( isQuadtree_ && sc[7-fNodes[i]] < 0 )
                continue;

            neighbourLeaves.append(sc[7-fNodes[i]]);
        }
    }
}

void meshOctree::findNeighboursForLeaf
(
    const meshOctreeCubeCoordinates& cc,
    DynList<label>& neighbourLeaves
) const
{
    neighbourLeaves.clear();

    const label nCubeFaces = isQuadtree_?4:6;
    for(label i=0;i<nCubeFaces;++i)
    {
        findNeighboursInDirection(cc, i, neighbourLeaves);
    }
}

void meshOctree::findAllLeafNeighbours
(
    const meshOctreeCubeCoordinates& cc,
    DynList<label>& neighbourLeaves
) const
{
    neighbourLeaves.clear();

    //- neighbours over nodes
    if( !isQuadtree_ )
    {
        for(label i=0;i<8;++i)
            neighbourLeaves.append(findNeighbourOverNode(cc, i));
    }

    //- neighbours over edges
    const label nCubeEdges = isQuadtree_?8:12;
    for(label i=0;i<nCubeEdges;++i)
        findNeighboursOverEdge(cc, i, neighbourLeaves);

    //- neighbours over faces
    const label nCubeFaces = isQuadtree_?4:6;
    for(label i=0;i<nCubeFaces;++i)
        findNeighboursInDirection(cc, i, neighbourLeaves);
}

void meshOctree::findLeavesForCubeVertex
(
    const label leafI,
    const direction vrtI,
    FixedList<label, 8>& neighbours
) const
{
    const meshOctreeCube* oc = leaves_[leafI];
    const meshOctreeCubeCoordinates cc = oc->refineForPosition(vrtI);

    FixedList<meshOctreeCubeCoordinates, 8> positions;

    for(label i=0;i<8;++i)
    {
        positions[i] = cc + vrtLeavesPos_[vrtI][i];
    }

    forAll(positions, posI)
    {
        neighbours[posI] = -1;

        const label nei = findLeafLabelForPosition(positions[posI]);

        if( (nei > -1) && leaves_[nei]->isLeaf() )
            neighbours[posI] = nei;
    }

    # ifdef OCTREE_DEBUG
    Info << "Cube " << *oc << endl;
    Info << "Vertex " << short(vrtI) << endl;
    Info << "Neighbours " << endl;
    forAll(neighbours, nI)
        if( neighbours[nI] )
            Info << *neighbours[nI] << endl;
    # endif
}

meshOctreeCube* meshOctree::findCubeForPosition
(
    const meshOctreeCubeCoordinates& cc
) const
{
    # ifdef OCTREE_DEBUG
    Info << "Finding position " << cc << endl;
    # endif

    const label cpx = cc.posX();
    const label cpy = cc.posY();
    const label cpz = cc.posZ();
    const direction l = cc.level();

    label levelLimiter = (1 << l);
    if(
        (cpx >= levelLimiter) || (cpx < 0) ||
        (cpy >= levelLimiter) || (cpy < 0) ||
        (!isQuadtree_ && (cpz >= levelLimiter || cpz < 0)) ||
        (isQuadtree_ && (cpz != initialCubePtr_->posZ()))
    )
    {
        return NULL;
    }

    meshOctreeCube* neiPtr(initialCubePtr_);

    for(label i=(l-1);i>=0;--i)
    {
        if( neiPtr && !neiPtr->isLeaf() )
        {
            levelLimiter = (1 << i);

            label scI(0);

            if( cpx & levelLimiter )
                scI |= 1;
            if( cpy & levelLimiter )
                scI |= 2;
            if( !isQuadtree_ && (cpz & levelLimiter) )
                scI |= 4;

            neiPtr = neiPtr->subCube(scI);
        }
        else
        {
            break;
        }
    }

    # ifdef OCTREE_DEBUG
    Info << "Found position is " << *neiPtr << endl;
    # endif

    return neiPtr;
}

label meshOctree::findLeafLabelForPosition
(
    const meshOctreeCubeCoordinates& cc
) const
{
    const meshOctreeCube* ocPtr = findCubeForPosition(cc);

    if( ocPtr && ocPtr->isLeaf() )
    {
        return ocPtr->cubeLabel();
    }
    else if( !ocPtr && (neiProcs_.size() != 0) )
    {
        const label levelLimiter = (1 << cc.level());
        if(
            (cc.posX() < levelLimiter) && (cc.posX() >= 0) &&
            (cc.posY() < levelLimiter) && (cc.posY() >= 0) &&
            ((!isQuadtree_ && (cc.posZ() < levelLimiter && cc.posZ() >= 0)) ||
            (isQuadtree_ && (cc.posZ() == initialCubePtr_->posZ())))
        )
        {
            return meshOctreeCubeBasic::OTHERPROC;
        }
    }

    return -1;
}

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

} // End namespace Foam

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