treeDataPoint.C 5.24 KB
Newer Older
mattijs's avatar
mattijs committed
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation
mattijs's avatar
mattijs committed
6
7
8
9
10
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

11
12
13
14
    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.
mattijs's avatar
mattijs committed
15
16
17
18
19
20
21

    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
22
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
mattijs's avatar
mattijs committed
23
24
25
26
27
28
29
30

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

#include "treeDataPoint.H"
#include "treeBoundBox.H"
#include "indexedOctree.H"
#include "triangleFuncs.H"

mattijs's avatar
mattijs committed
31
32
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

33
34
35
36
namespace Foam
{
defineTypeNameAndDebug(treeDataPoint, 0);
}
mattijs's avatar
mattijs committed
37
38


mattijs's avatar
mattijs committed
39
40
41
42
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::treeDataPoint::treeDataPoint(const pointField& points)
:
43
44
    points_(points),
    useSubset_(false)
mattijs's avatar
mattijs committed
45
46
47
{}


48
49
50
51
52
53
54
Foam::treeDataPoint::treeDataPoint
(
    const pointField& points,
    const labelList& pointLabels
)
:
    points_(points),
55
56
    pointLabels_(pointLabels),
    useSubset_(true)
57
58
59
{}


mattijs's avatar
mattijs committed
60
61
// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

62
Foam::pointField Foam::treeDataPoint::shapePoints() const
mattijs's avatar
mattijs committed
63
{
64
    if (useSubset_)
65
66
67
68
69
70
71
    {
        return pointField(points_, pointLabels_);
    }
    else
    {
        return points_;
    }
mattijs's avatar
mattijs committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
}


//- Get type (inside,outside,mixed,unknown) of point w.r.t. surface.
//  Only makes sense for closed surfaces.
Foam::label Foam::treeDataPoint::getVolumeType
(
    const indexedOctree<treeDataPoint>& oc,
    const point& sample
) const
{
    return indexedOctree<treeDataPoint>::UNKNOWN;
}


// Check if any point on shape is inside cubeBb.
bool Foam::treeDataPoint::overlaps
(
    const label index,
    const treeBoundBox& cubeBb
) const
{
94
    label pointI = (useSubset_ ? pointLabels_[index] : index);
95
    return cubeBb.contains(points_[pointI]);
mattijs's avatar
mattijs committed
96
97
98
}


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Check if any point on shape is inside sphere.
bool Foam::treeDataPoint::overlaps
(
    const label index,
    const point& centre,
    const scalar radiusSqr
) const
{
    label pointI = (useSubset_ ? pointLabels_[index] : index);

    if (magSqr(points_[pointI] - centre) <= radiusSqr)
    {
        return true;
    }

    return false;
}


mattijs's avatar
mattijs committed
118
119
120
121
// Calculate nearest point to sample. Updates (if any) nearestDistSqr, minIndex,
// nearestPoint.
void Foam::treeDataPoint::findNearest
(
122
    const labelUList& indices,
mattijs's avatar
mattijs committed
123
124
125
126
127
128
129
130
131
    const point& sample,

    scalar& nearestDistSqr,
    label& minIndex,
    point& nearestPoint
) const
{
    forAll(indices, i)
    {
132
        const label index = indices[i];
133
        label pointI = (useSubset_ ? pointLabels_[index] : index);
mattijs's avatar
mattijs committed
134

135
        const point& pt = points_[pointI];
mattijs's avatar
mattijs committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

        scalar distSqr = magSqr(pt - sample);

        if (distSqr < nearestDistSqr)
        {
            nearestDistSqr = distSqr;
            minIndex = index;
            nearestPoint = pt;
        }
    }
}


//- Calculates nearest (to line) point in shape.
//  Returns point and distance (squared)
void Foam::treeDataPoint::findNearest
(
153
    const labelUList& indices,
mattijs's avatar
mattijs committed
154
155
156
157
158
159
160
161
162
    const linePointRef& ln,

    treeBoundBox& tightest,
    label& minIndex,
    point& linePoint,
    point& nearestPoint
) const
{
    // Best so far
163
164
165
166
167
    scalar nearestDistSqr = GREAT;
    if (minIndex >= 0)
    {
        nearestDistSqr = magSqr(linePoint - nearestPoint);
    }
mattijs's avatar
mattijs committed
168
169
170

    forAll(indices, i)
    {
171
        const label index = indices[i];
172
        label pointI = (useSubset_ ? pointLabels_[index] : index);
mattijs's avatar
mattijs committed
173

174
        const point& shapePt = points_[pointI];
mattijs's avatar
mattijs committed
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

        if (tightest.contains(shapePt))
        {
            // Nearest point on line
            pointHit pHit = ln.nearestDist(shapePt);
            scalar distSqr = sqr(pHit.distance());

            if (distSqr < nearestDistSqr)
            {
                nearestDistSqr = distSqr;
                minIndex = index;
                linePoint = pHit.rawPoint();
                nearestPoint = shapePt;

                {
                    point& minPt = tightest.min();
                    minPt = min(ln.start(), ln.end());
                    minPt.x() -= pHit.distance();
                    minPt.y() -= pHit.distance();
                    minPt.z() -= pHit.distance();
                }
                {
                    point& maxPt = tightest.max();
                    maxPt = max(ln.start(), ln.end());
                    maxPt.x() += pHit.distance();
                    maxPt.y() += pHit.distance();
                    maxPt.z() += pHit.distance();
                }
            }
        }
    }
}


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