plane.C 11.9 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation
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.
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/>.
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

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

#include "plane.H"
#include "tensor.H"

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

// Calculate base point and unit normal vector from plane equation
void Foam::plane::calcPntAndVec(const scalarList& C)
{
    if (mag(C[0]) > VSMALL)
    {
        basePoint_ = vector((-C[3]/C[0]), 0, 0);
    }
    else
    {
        if (mag(C[1]) > VSMALL)
        {
            basePoint_ = vector(0, (-C[3]/C[1]), 0);
        }
        else
        {
            if (mag(C[2]) > VSMALL)
            {
                basePoint_ = vector(0, 0, (-C[3]/C[2]));
            }
            else
            {
                FatalErrorIn("void plane::calcPntAndVec(const scalarList&)")
                    << "At least one plane coefficient must have a value"
                    << abort(FatalError);
            }
        }
    }

    unitVector_ = vector(C[0], C[1], C[2]);
    scalar magUnitVector(mag(unitVector_));

    if (magUnitVector < VSMALL)
    {
        FatalErrorIn("void plane::calcPntAndVec(const scalarList&)")
            << "Plane normal defined with zero length"
            << abort(FatalError);
    }

    unitVector_ /= magUnitVector;
}


void Foam::plane::calcPntAndVec
(
    const point& point1,
    const point& point2,
    const point& point3
)
{
    basePoint_ = (point1 + point2 + point3)/3;
    vector line12 = point1 - point2;
    vector line23 = point2 - point3;

    if
    (
        mag(line12) < VSMALL
     || mag(line23) < VSMALL
     || mag(point3-point1) < VSMALL
    )
    {
        FatalErrorIn
        (
            "void plane::calcPntAndVec\n"
            "(\n"
            "    const point&,\n"
            "    const point&,\n"
            "    const point&\n"
            ")\n"
mattijs's avatar
mattijs committed
99
100
        )   << "Bad points:" << point1 << ' ' << point2 << ' ' << point3
            << abort(FatalError);
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    }

    unitVector_ = line12 ^ line23;
    scalar magUnitVector(mag(unitVector_));

    if (magUnitVector < VSMALL)
    {
        FatalErrorIn
        (
            "void plane::calcPntAndVec\n"
            "(\n"
            "    const point&,\n"
            "    const point&,\n"
            "    const point&\n"
            ")\n"
mattijs's avatar
mattijs committed
116
117
        )   << "Plane normal defined with zero length" << nl
            << "Bad points:" << point1 << ' ' << point2 << ' ' << point3
118
119
120
121
122
123
124
125
126
127
128
129
130
131
            << abort(FatalError);
    }

    unitVector_ /= magUnitVector;
}


// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

// Construct from normal vector through the origin
Foam::plane::plane(const vector& normalVector)
:
    unitVector_(normalVector),
    basePoint_(vector::zero)
132
133
134
135
136
137
138
139
140
{
    scalar magUnitVector(mag(unitVector_));

    if (magUnitVector > VSMALL)
    {
        unitVector_ /= magUnitVector;
    }
    else
    {
141
        FatalErrorIn("plane::plane(const vector&)")
mattijs's avatar
mattijs committed
142
            << "plane normal has zero length. basePoint:" << basePoint_
143
144
145
            << abort(FatalError);
    }
}
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162


// Construct from point and normal vector
Foam::plane::plane(const point& basePoint, const vector& normalVector)
:
    unitVector_(normalVector),
    basePoint_(basePoint)
{
    scalar magUnitVector(mag(unitVector_));

    if (magUnitVector > VSMALL)
    {
        unitVector_ /= magUnitVector;
    }
    else
    {
        FatalErrorIn("plane::plane(const point&, const vector&)")
mattijs's avatar
mattijs committed
163
            << "plane normal has zero length. basePoint:" << basePoint_
164
            << abort(FatalError);
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
    }
}


// Construct from plane equation
Foam::plane::plane(const scalarList& C)
{
    calcPntAndVec(C);
}


// Construct from three points
Foam::plane::plane
(
    const point& a,
    const point& b,
    const point& c
)
{
    calcPntAndVec(a, b, c);
}


// Construct from dictionary
Foam::plane::plane(const dictionary& dict)
:
    unitVector_(vector::zero),
    basePoint_(point::zero)
{
194
    const word planeType(dict.lookup("planeType"));
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210

    if (planeType == "planeEquation")
    {
        const dictionary& subDict = dict.subDict("planeEquationDict");
        scalarList C(4);

        C[0] = readScalar(subDict.lookup("a"));
        C[1] = readScalar(subDict.lookup("b"));
        C[2] = readScalar(subDict.lookup("c"));
        C[3] = readScalar(subDict.lookup("d"));

        calcPntAndVec(C);

    }
    else if (planeType == "embeddedPoints")
    {
211
        const dictionary& subDict = dict.subDict("embeddedPointsDict");
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228

        point point1(subDict.lookup("point1"));
        point point2(subDict.lookup("point2"));
        point point3(subDict.lookup("point3"));

        calcPntAndVec(point1, point2, point3);
    }
    else if (planeType == "pointAndNormal")
    {
        const dictionary& subDict = dict.subDict("pointAndNormalDict");

        basePoint_ = subDict.lookup("basePoint");
        unitVector_ = subDict.lookup("normalVector");
        unitVector_ /= mag(unitVector_);
    }
    else
    {
229
230
231
232
        FatalIOErrorIn("plane::plane(const dictionary&)", dict)
            << "Invalid plane type: " << planeType << nl
            << "Valid options include: planeEquation, embeddedPoints and "
            << "pointAndNormal"
233
            << abort(FatalIOError);
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
    }
}


// Construct from Istream. Assumes point and normal vector.
Foam::plane::plane(Istream& is)
:
    unitVector_(is),
    basePoint_(is)
{
    scalar magUnitVector(mag(unitVector_));

    if (magUnitVector > VSMALL)
    {
        unitVector_ /= magUnitVector;
    }
    else
    {
        FatalErrorIn("plane::plane(Istream& is)")
mattijs's avatar
mattijs committed
253
            << "plane normal has zero length. basePoint:" << basePoint_
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
            << abort(FatalError);
    }
}


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

// Return plane normal vector
const Foam::vector& Foam::plane::normal() const
{
    return unitVector_;
}


// Return plane base point
const Foam::point& Foam::plane::refPoint() const
{
    return basePoint_;
}


275
276
// Return coefficients for plane equation: ax + by + cz + d = 0
Foam::FixedList<Foam::scalar, 4> Foam::plane::planeCoeffs() const
277
{
278
    FixedList<scalar, 4> C(4);
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

    scalar magX = mag(unitVector_.x());
    scalar magY = mag(unitVector_.y());
    scalar magZ = mag(unitVector_.z());

    if (magX > magY)
    {
        if (magX > magZ)
        {
            C[0] = 1;
            C[1] = unitVector_.y()/unitVector_.x();
            C[2] = unitVector_.z()/unitVector_.x();
        }
        else
        {
294
295
            C[0] = unitVector_.x()/unitVector_.z();
            C[1] = unitVector_.y()/unitVector_.z();
296
297
298
299
300
301
302
            C[2] = 1;
        }
    }
    else
    {
        if (magY > magZ)
        {
303
            C[0] = unitVector_.x()/unitVector_.y();
304
305
306
307
308
            C[1] = 1;
            C[2] = unitVector_.z()/unitVector_.y();
        }
        else
        {
309
310
            C[0] = unitVector_.x()/unitVector_.z();
            C[1] = unitVector_.y()/unitVector_.z();
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
            C[2] = 1;
        }
    }

    C[3] = - C[0] * basePoint_.x()
           - C[1] * basePoint_.y()
           - C[2] * basePoint_.z();

    return C;
}


// Return nearest point in the plane for the given point
Foam::point Foam::plane::nearestPoint(const point& p) const
{
    return p - unitVector_*((p - basePoint_) & unitVector_);
}


// Return distance from the given point to the plane
Foam::scalar Foam::plane::distance(const point& p) const
{
    return mag((p - basePoint_) & unitVector_);
}


// Cutting point for plane and line defined by origin and direction
Foam::scalar Foam::plane::normalIntersect
(
    const point& pnt0,
    const vector& dir
) const
{
    scalar denom = stabilise((dir & unitVector_), VSMALL);

    return ((basePoint_ - pnt0) & unitVector_)/denom;
}


// Cutting line of two planes
Foam::plane::ray Foam::plane::planeIntersect(const plane& plane2) const
{
    // Mathworld plane-plane intersection. Assume there is a point on the
    // intersection line with z=0 and solve the two plane equations
    // for that (now 2x2 equation in x and y)
    // Better: use either z=0 or x=0 or y=0.

    const vector& n1 = normal();
    const vector& n2 = plane2.normal();

    const point& p1 = refPoint();
    const point& p2 = plane2.refPoint();

    scalar n1p1 = n1&p1;
    scalar n2p2 = n2&p2;

    vector dir = n1 ^ n2;

    // Determine zeroed out direction (can be x,y or z) by looking at which
    // has the largest component in dir.
    scalar magX = mag(dir.x());
    scalar magY = mag(dir.y());
    scalar magZ = mag(dir.z());

    direction iZero, i1, i2;

    if (magX > magY)
    {
        if (magX > magZ)
        {
            iZero = 0;
            i1 = 1;
            i2 = 2;
        }
        else
        {
            iZero = 2;
            i1 = 0;
            i2 = 1;
        }
    }
    else
    {
        if (magY > magZ)
        {
            iZero = 1;
            i1 = 2;
            i2 = 0;
        }
        else
        {
            iZero = 2;
            i1 = 0;
            i2 = 1;
        }
    }

    vector pt;

    pt[iZero] = 0;
    pt[i1] = (n2[i2]*n1p1 - n1[i2]*n2p2) / (n1[i1]*n2[i2] - n2[i1]*n1[i2]);
    pt[i2] = (n2[i1]*n1p1 - n1[i1]*n2p2) / (n1[i2]*n2[i1] - n1[i1]*n2[i2]);

    return ray(pt, dir);
}


// Cutting point of three planes
Foam::point Foam::plane::planePlaneIntersect
(
    const plane& plane2,
    const plane& plane3
) const
{
425
426
427
    FixedList<scalar, 4> coeffs1(planeCoeffs());
    FixedList<scalar, 4> coeffs2(plane2.planeCoeffs());
    FixedList<scalar, 4> coeffs3(plane3.planeCoeffs());
428
429
430

    tensor a
    (
431
432
433
        coeffs1[0],coeffs1[1],coeffs1[2],
        coeffs2[0],coeffs2[1],coeffs2[2],
        coeffs3[0],coeffs3[1],coeffs3[2]
434
435
    );

436
    vector b(coeffs1[3],coeffs2[3],coeffs3[3]);
437
438
439
440
441

    return (inv(a) & (-b));
}


442
443
444
445
446
447
448
449
Foam::plane::side Foam::plane::sideOfPlane(const point& p) const
{
    const scalar angle((p - basePoint_) & unitVector_);

    return (angle < 0 ? FLIP : NORMAL);
}


450
451
452
453
454
455
456
457
458
459
460
461
462
void Foam::plane::writeDict(Ostream& os) const
{
    os.writeKeyword("planeType") << "pointAndNormal"
        << token::END_STATEMENT << nl;
    os  << indent << "pointAndNormalDict" << nl
        << indent << token::BEGIN_BLOCK << incrIndent << nl;
    os.writeKeyword("basePoint") << basePoint_ << token::END_STATEMENT << nl;
    os.writeKeyword("normalVector") << unitVector_ << token::END_STATEMENT
        << nl;
    os << decrIndent << indent << token::END_BLOCK << endl;
}


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
// * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //

bool Foam::operator==(const plane& a, const plane& b)
{
    if (a.basePoint_ == b.basePoint_ && a.unitVector_ == b.unitVector_)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool Foam::operator!=(const plane& a, const plane& b)
{
    return !(a == b);
}


// * * * * * * * * * * * * * * * Friend Functions  * * * * * * * * * * * * * //

Foam::Ostream& Foam::operator<<(Ostream& os, const plane& a)
{
    os  << a.unitVector_ << token::SPACE << a.basePoint_;

    return os;
}


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