line.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011 OpenFOAM Foundation
9  Copyright (C) 2018-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::line
29 
30 Description
31  A line primitive.
32 
33 SourceFiles
34  lineI.H
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef Foam_line_H
39 #define Foam_line_H
40 
41 #include "point.H"
42 #include "point2D.H"
43 #include "vector.H"
44 #include "pointHit.H"
45 #include "FixedList.H"
46 #include "UList.H"
47 #include "Pair.H"
48 #include "Tuple2.H"
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 namespace Foam
53 {
54 
55 // Forward Declarations
56 
57 template<class Point, class PointRef> class line;
58 
59 template<class Point, class PointRef>
61 
62 template<class Point, class PointRef>
63 inline Ostream& operator<<(Ostream& os, const line<Point, PointRef>& l);
64 
65 
66 // Common Typedefs
67 
68 //- A line using referred points
70 
71 
72 /*---------------------------------------------------------------------------*\
73  Class linePoints Declaration
74 \*---------------------------------------------------------------------------*/
75 
76 //- Line point storage. Default constructable (line is not)
77 class linePoints
78 :
79  public Pair<point>
80 {
81 public:
82 
83  // Generated Methods
84 
85  //- Default construct
86  linePoints() = default;
87 
88  //- Inherit constructors
89  using Pair<point>::Pair;
90 
91 
92  // Constructors
93 
94  //- Construct from point references
95  inline explicit linePoints(const linePointRef& pts);
96 
97  //- Copy construct from subset of points
98  inline linePoints
99  (
100  const UList<point>& points,
101  const FixedList<label, 2>& indices
102  );
103 
104 
105  // Member Functions
106 
107  //- The first vertex
108  const point& a() const noexcept { return Pair<point>::first(); }
109 
110  //- The second vertex
111  const point& b() const noexcept { return Pair<point>::second(); }
112 
113  //- The first vertex
114  point& a() noexcept { return Pair<point>::first(); }
115 
116  //- The second vertex
118 
119  //- Return as line reference
120  inline linePointRef ln() const;
121 
123  // Properties
124 
125  //- Return centre (centroid)
126  inline point centre() const;
128  //- The magnitude (length) of the line
129  inline scalar mag() const;
130 
131  //- Return start-to-end vector
132  inline vector vec() const;
133 
134  //- Return the unit vector (start-to-end)
135  inline vector unitVec() const;
136 
137  //- The enclosing (bounding) box for the line
138  inline Pair<point> box() const;
139 };
140 
141 
142 /*---------------------------------------------------------------------------*\
143  Class line Declaration
144 \*---------------------------------------------------------------------------*/
145 
146 template<class Point, class PointRef>
147 class line
148 {
149  // Private Data
150 
151  //- Reference to the first line point
152  PointRef a_;
153 
154  //- Reference to the second line point
155  PointRef b_;
156 
157 
158 public:
159 
160  // Constructors
161 
162  //- Construct from two points
163  inline line(const Point& from, const Point& to);
164 
165  //- Construct from two points in the list of points
166  // The indices could be from edge etc.
167  inline line
168  (
169  const UList<Point>& points,
170  const FixedList<label, 2>& indices
171  );
172 
173  //- Construct from Istream
174  inline explicit line(Istream& is);
175 
176 
177  // Member Functions
178 
179  // Access
180 
181  //- The first point
182  PointRef a() const noexcept { return a_; }
183 
184  //- The second point
185  PointRef b() const noexcept { return b_; }
186 
187  //- The first point
188  PointRef first() const noexcept { return a_; }
189 
190  //- The second (last) point
191  PointRef second() const noexcept { return b_; }
192 
193  //- The start (first) point
194  PointRef start() const noexcept { return a_; }
195 
196  //- The end (second) point
197  PointRef end() const noexcept { return b_; }
198 
199  //- The last (second) point
200  PointRef last() const noexcept { return b_; }
201 
202 
203  // Line properties (static calculations)
204 
205  //- The enclosing (bounding) box for two points
206  inline static Pair<Point> box(const Point& p0, const Point& p1);
207 
208 
209  // Properties
210 
211  //- Return centre (centroid)
212  inline Point centre() const;
213 
214  //- The magnitude (length) of the line
215  inline scalar mag() const;
216 
217  //- Return start-to-end vector
218  inline Point vec() const;
219 
220  //- Return the unit vector (start-to-end)
221  inline Point unitVec() const;
223  //- The enclosing (bounding) box for the line
224  inline Pair<Point> box() const;
225 
226  //- Return nearest distance to line from a given point
227  // If the nearest point is on the line, return a hit
228  PointHit<Point> nearestDist(const Point& p) const;
229 
230  //- Return nearest distance from line to line.
231  //- Returns distance and sets both points
232  //- (one on *this, one on the provided linePointRef.
233  scalar nearestDist
234  (
236  Point& thisPoint,
237  Point& edgePoint
238  ) const;
239 
240 
241  // IOstream Operators
243  friend Istream& operator>> <Point, PointRef>(Istream&, line&);
244  friend Ostream& operator<< <Point, PointRef>(Ostream&, const line&);
245 };
246 
248 //- 2D specialisation
249 template<>
251 (
253  point2D& thisPoint,
254  point2D& edgePoint
255 ) const;
256 
257 
258 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
259 
260 } // End namespace Foam
261 
262 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
263 
264 #include "lineI.H"
265 
266 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
267 
268 #endif
269 
270 // ************************************************************************* //
Point unitVec() const
Return the unit vector (start-to-end)
Definition: lineI.H:122
line(const Point &from, const Point &to)
Construct from two points.
Definition: lineI.H:45
const T & first() const noexcept
Access the first element.
Definition: Pair.H:137
A line primitive.
Definition: line.H:52
PointRef last() const noexcept
The last (second) point.
Definition: line.H:252
linePointRef ln() const
Return as line reference.
Definition: lineI.H:76
scalar mag() const
The magnitude (length) of the line.
Definition: lineI.H:96
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Pair< point > box() const
The enclosing (bounding) box for the line.
Definition: lineI.H:159
scalar mag() const
The magnitude (length) of the line.
Definition: lineI.H:102
PointRef end() const noexcept
The end (second) point.
Definition: line.H:247
Pair< Point > box() const
The enclosing (bounding) box for the line.
Definition: lineI.H:153
vector vec() const
Return start-to-end vector.
Definition: lineI.H:115
vector unitVec() const
Return the unit vector (start-to-end)
Definition: lineI.H:135
linePoints()=default
Default construct.
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: instant.H:46
line< point, const point & > linePointRef
A line using referred points.
Definition: line.H:66
const pointField & points
PointRef first() const noexcept
The first point.
Definition: line.H:232
An edge is a list of two vertex labels. This can correspond to a directed graph edge or an edge on a ...
Definition: edge.H:59
const point & b() const noexcept
The second vertex.
Definition: line.H:122
Istream & operator>>(Istream &, directionInfo &)
PointRef a() const noexcept
The first point.
Definition: line.H:222
Describes the interaction of a object and a (templated) point. It carries the info of a successful hi...
Definition: pointHit.H:43
Line point storage. Default constructable (line is not)
Definition: line.H:76
PointRef b() const noexcept
The second point.
Definition: line.H:227
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
point centre() const
Return centre (centroid)
Definition: lineI.H:89
CGAL::Point_3< K > Point
PointRef second() const noexcept
The second (last) point.
Definition: line.H:237
Point centre() const
Return centre (centroid)
Definition: lineI.H:83
Point vec() const
Return start-to-end vector.
Definition: lineI.H:109
const point & a() const noexcept
The first vertex.
Definition: line.H:117
volScalarField & p
PointRef start() const noexcept
The start (first) point.
Definition: line.H:242
const T & second() const noexcept
Access the second element.
Definition: Pair.H:147
PointHit< Point > nearestDist(const Point &p) const
Return nearest distance to line from a given point.
Definition: lineI.H:167
const volScalarField & p0
Definition: EEqn.H:36
Namespace for OpenFOAM.
const pointField & pts