FreeFOAM The Cross-Platform CFD Toolkit
polyLine.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  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 #include <OpenFOAM/error.H>
27 #include "polyLine.H"
28 
29 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
30 
32 {
34 
35  if (param_.size())
36  {
37  param_[0] = 0.0;
38 
39  for (label i=1; i < param_.size(); i++)
40  {
41  param_[i] = param_[i-1] + mag(points_[i] - points_[i-1]);
42  }
43 
44  // normalize on the interval 0-1
46  for (label i=1; i < param_.size() - 1; i++)
47  {
48  param_[i] /= lineLength_;
49  }
50  param_[param_.size()-1] = 1.0;
51  }
52  else
53  {
54  lineLength_ = 0.0;
55  }
56 }
57 
58 
59 
60 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
61 
62 Foam::polyLine::polyLine(const pointField& ps, const bool)
63 :
64  points_(ps),
65  lineLength_(0.0),
66  param_(0)
67 {
68  calcParam();
69 }
70 
71 
72 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
73 
75 {
76  return points_;
77 }
78 
79 
80 Foam::label Foam::polyLine::nSegments() const
81 {
82  return points_.size()-1;
83 }
84 
85 
86 Foam::label Foam::polyLine::localParameter(scalar& lambda) const
87 {
88  // check endpoints
89  if (lambda < SMALL)
90  {
91  lambda = 0;
92  return 0;
93  }
94  else if (lambda > 1 - SMALL)
95  {
96  lambda = 1;
97  return nSegments();
98  }
99 
100  // search table of cumulative distances to find which line-segment
101  // we are on. Check the upper bound.
102 
103  label segmentI = 1;
104  while (param_[segmentI] < lambda)
105  {
106  segmentI++;
107  }
108  segmentI--; // we want the corresponding lower bound
109 
110  // the local parameter [0-1] on this line segment
111  lambda =
112  (
113  ( lambda - param_[segmentI] )
114  / ( param_[segmentI+1] - param_[segmentI] )
115  );
116 
117  return segmentI;
118 }
119 
120 
121 Foam::point Foam::polyLine::position(const scalar mu) const
122 {
123  // check endpoints
124  if (mu < SMALL)
125  {
126  return points_[0];
127  }
128  else if (mu > 1 - SMALL)
129  {
130  return points_[points_.size()-1];
131  }
132 
133 
134  scalar lambda = mu;
135  label segment = localParameter(lambda);
136  return position(segment, lambda);
137 }
138 
139 
141 (
142  const label segment,
143  const scalar mu
144 ) const
145 {
146  // out-of-bounds
147  if (segment < 0)
148  {
149  return points_[0];
150  }
151  else if (segment > nSegments())
152  {
153  return points_[points_.size()-1];
154  }
155 
156  const point& p0 = points()[segment];
157  const point& p1 = points()[segment+1];
158 
159  // special cases - no calculation needed
160  if (mu <= 0.0)
161  {
162  return p0;
163  }
164  else if (mu >= 1.0)
165  {
166  return p1;
167  }
168  else
169  {
170  // linear interpolation
171  return points_[segment] + mu * (p1 - p0);
172  }
173 }
174 
175 
176 Foam::scalar Foam::polyLine::length() const
177 {
178  return lineLength_;
179 }
180 
181 
182 // ************************ vim: set sw=4 sts=4 et: ************************ //