MeshKernel
Splines.hpp
1 //---- GPL ---------------------------------------------------------------------
2 //
3 // Copyright (C) Stichting Deltares, 2011-2021.
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation version 3.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // contact: delft3d.support@deltares.nl
18 // Stichting Deltares
19 // P.O. Box 177
20 // 2600 MH Delft, The Netherlands
21 //
22 // All indications and logos of, and references to, "Delft3D" and "Deltares"
23 // are registered trademarks of Stichting Deltares, and remain the property of
24 // Stichting Deltares. All rights reserved.
25 //
26 //------------------------------------------------------------------------------
27 
28 #pragma once
29 
30 #include "MeshKernel/BoundingBox.hpp"
31 #include "MeshKernel/Constants.hpp"
32 #include "MeshKernel/Entities.hpp"
33 #include "MeshKernel/LandBoundary.hpp"
34 #include "MeshKernel/Operations.hpp"
35 #include "MeshKernel/Utilities/LinearAlgebra.hpp"
36 
37 namespace meshkernel
38 {
39  class CurvilinearGrid;
40 
47  class Splines
48  {
49 
50  public:
52  Splines() = default;
53 
56  explicit Splines(Projection projection);
57 
60  explicit Splines(CurvilinearGrid const& grid);
61 
66  // void AddSpline(const std::vector<Point>& splines, UInt start, UInt size);
67  void AddSpline(const std::vector<Point>& splines, UInt start, UInt size);
68 
71  void AddSpline(const std::vector<Point>& splines);
72 
76  void Replace(const UInt splineIndex, const std::vector<Point>& splinePoints);
77 
81  void SwapSplines(const UInt firstSpline, const UInt secondSpline);
82 
84  void Reverse(const UInt splineId);
85 
91  void SnapSpline(const size_t splineIndex,
92  const LandBoundary& landBoundary,
93  const int numberOfIterations = constants::numeric::defaultSnappingIterations);
94 
103  bool GetSplinesIntersection(UInt first,
104  UInt second,
105  double& crossProductIntersection,
106  Point& intersectionPoint,
107  double& firstSplineRatio,
108  double& secondSplineRatio) const;
109 
119  [[nodiscard]] double ComputeSplineLength(UInt index,
120  double startAdimensionalCoordinate,
121  double endAdimensionalCoordinate,
122  UInt numSamples = 100,
123  bool accountForCurvature = false,
124  double height = 1.0,
125  double assignedDelta = -1.0) const;
126 
133  std::tuple<std::vector<Point>, std::vector<double>> ComputePointOnSplineFromAdimensionalDistance(UInt index,
134  double maximumGridHeight,
135  bool isSpacingCurvatureAdapted,
136  const std::vector<double>& distances) const;
137 
144  Point ComputeClosestPointOnSplineSegment(UInt index, double startSplineSegment, double endSplineSegment, Point point) const;
145 
150  Point ComputeClosestPoint(UInt index, Point point) const;
151 
156  Point Evaluate(const UInt whichSpline, const double lambda) const;
157 
159  BoundingBox GetBoundingBox(const UInt splineIndex) const;
160 
163  auto GetNumSplines() const { return static_cast<UInt>(m_splineNodes.size()); }
164 
167  UInt Size(const UInt whichSpline) const;
168 
170  UInt MaxSizeIndex() const;
171 
172  std::vector<std::vector<Point>> m_splineNodes;
173  std::vector<std::vector<Point>> m_splineDerivatives;
174  std::vector<double> m_splinesLength;
175  Projection m_projection = Projection::cartesian;
176 
177  private:
181  void AddPointInExistingSpline(UInt splineIndex, const Point& point);
182 
187  std::tuple<Point, Point, double> ComputeCurvatureOnSplinePoint(UInt splineIndex,
188  double adimensionalPointCoordinate) const;
189 
192  void DeleteSpline(UInt splineIndex);
193 
195  void AllocateSplinesProperties();
196 
198  static std::vector<Point> ComputeSplineDerivative(const std::vector<Point>& splinesNodes);
199  };
200 
203  {
210  UInt splineIndex,
211  bool isSpacingCurvatureAdapted,
212  double h) : m_spline(splines),
213  m_splineIndex(splineIndex),
214  m_isSpacingCurvatureAdapted(isSpacingCurvatureAdapted),
215  m_h(h) {}
216 
224  UInt splineIndex,
225  bool isSpacingCurvatureAdapted,
226  double h,
227  double distance) : m_spline(splines),
228  m_splineIndex(splineIndex),
229  m_isSpacingCurvatureAdapted(isSpacingCurvatureAdapted),
230  m_h(h),
231  m_DimensionalDistance(distance) {}
232 
235  void SetDimensionalDistance(double distance)
236  {
237  m_DimensionalDistance = distance;
238  }
239 
241  double operator()(double adimensionalDistanceReferencePoint) const
242  {
243  auto distanceFromReferencePoint = m_spline.ComputeSplineLength(m_splineIndex, 0.0, adimensionalDistanceReferencePoint, m_numSamples, m_isSpacingCurvatureAdapted, m_h, 0.1);
244  distanceFromReferencePoint = std::abs(distanceFromReferencePoint - m_DimensionalDistance);
245  return distanceFromReferencePoint;
246  }
247 
248  const Splines& m_spline;
251  double m_h;
253  double m_DimensionalDistance = 0.0;
254  };
255 
258  {
264  UInt splineIndex,
265  Point point) : m_spline(splines),
266  m_splineIndex(splineIndex),
267  m_point(point) {}
268 
270  double operator()(double adimensionalDistanceReferencePoint) const
271  {
272  const auto pointOnSpline = ComputePointOnSplineAtAdimensionalDistance(m_spline.m_splineNodes[m_splineIndex], m_spline.m_splineDerivatives[m_splineIndex], adimensionalDistanceReferencePoint);
273  return ComputeDistance(m_point, pointOnSpline, Projection::cartesian);
274  }
275 
276  const Splines& m_spline;
279  double m_DimensionalDistance = 0.0;
280  };
281 
282 } // namespace meshkernel
meshkernel::Projection
Projection
Enumerator describing the supported projections.
Definition: Definitions.hpp:41
meshkernel::FuncAdimensionalToDimensionalDistanceOnSpline
This structure is used to create a function for converting an adimensional distance on a spline to a ...
Definition: Splines.hpp:202
meshkernel::Splines::GetNumSplines
auto GetNumSplines() const
Get the number of splines.
Definition: Splines.hpp:163
meshkernel::FuncDistanceFromAPoint::m_point
Point m_point
The point from where the distance is calculated.
Definition: Splines.hpp:278
meshkernel::Splines::SnapSpline
void SnapSpline(const size_t splineIndex, const LandBoundary &landBoundary, const int numberOfIterations=constants::numeric::defaultSnappingIterations)
Snap the spline to the land boundary (snap_spline)
meshkernel::Splines::SwapSplines
void SwapSplines(const UInt firstSpline, const UInt secondSpline)
Swap all the data for two splines.
meshkernel::FuncDistanceFromAPoint::FuncDistanceFromAPoint
FuncDistanceFromAPoint(const Splines &splines, UInt splineIndex, Point point)
Constructor.
Definition: Splines.hpp:263
meshkernel::Splines::Size
UInt Size(const UInt whichSpline) const
Get the size of a specific spline.
meshkernel::ComputePointOnSplineAtAdimensionalDistance
T ComputePointOnSplineAtAdimensionalDistance(const std::vector< T > &coordinates, const std::vector< T > &coordinatesDerivatives, double pointAdimensionalCoordinate)
Computes the coordinate of a point on a spline, given the dimensionless distance from the first corne...
Definition: Operations.hpp:488
meshkernel::BoundingBox
A class defining a bounding box.
Definition: BoundingBox.hpp:39
meshkernel::Point
A struct describing a point in a two-dimensional space.
Definition: Point.hpp:40
meshkernel::FuncDistanceFromAPoint::m_spline
const Splines & m_spline
Reference to splines.
Definition: Splines.hpp:276
meshkernel::Splines::AddSpline
void AddSpline(const std::vector< Point > &splines, UInt start, UInt size)
Adds a new spline to m_splineCornerPoints.
meshkernel::LandBoundary
A class containing the land boundary polylines.
Definition: LandBoundary.hpp:39
meshkernel::Splines::GetBoundingBox
BoundingBox GetBoundingBox(const UInt splineIndex) const
Compute the boundary box for the spline indicated by splineIndex.
meshkernel::Splines::m_splinesLength
std::vector< double > m_splinesLength
The length of each spline.
Definition: Splines.hpp:174
meshkernel::FuncAdimensionalToDimensionalDistanceOnSpline::m_spline
const Splines & m_spline
Reference to splines.
Definition: Splines.hpp:248
meshkernel::Splines::m_splineNodes
std::vector< std::vector< Point > > m_splineNodes
The spline corner points.
Definition: Splines.hpp:172
meshkernel::Splines::Evaluate
Point Evaluate(const UInt whichSpline, const double lambda) const
Computes the coordinate of a point on a spline, given the dimensionless distance from the first corne...
meshkernel::Splines::Splines
Splines()=default
Default constructor.
meshkernel::Splines::Replace
void Replace(const UInt splineIndex, const std::vector< Point > &splinePoints)
Replaces an existing spline.
meshkernel
Contains the logic of the C++ static library.
Definition: AveragingInterpolation.hpp:36
meshkernel::FuncDistanceFromAPoint::m_DimensionalDistance
double m_DimensionalDistance
Dimensional distance.
Definition: Splines.hpp:279
meshkernel::FuncAdimensionalToDimensionalDistanceOnSpline::m_splineIndex
UInt m_splineIndex
Spline index.
Definition: Splines.hpp:249
meshkernel::UInt
std::uint32_t UInt
Integer type used when indexing mesh graph entities.
Definition: Definitions.hpp:38
meshkernel::Splines::Reverse
void Reverse(const UInt splineId)
Reverse the order of the points in the spline.
meshkernel::FuncAdimensionalToDimensionalDistanceOnSpline::m_numSamples
UInt m_numSamples
Number of samples.
Definition: Splines.hpp:252
meshkernel::Splines::m_projection
Projection m_projection
The map projection.
Definition: Splines.hpp:175
meshkernel::Splines::m_splineDerivatives
std::vector< std::vector< Point > > m_splineDerivatives
The spline derivatives at the corner points.
Definition: Splines.hpp:173
meshkernel::FuncDistanceFromAPoint::m_splineIndex
UInt m_splineIndex
Spline index.
Definition: Splines.hpp:277
meshkernel::FuncAdimensionalToDimensionalDistanceOnSpline::FuncAdimensionalToDimensionalDistanceOnSpline
FuncAdimensionalToDimensionalDistanceOnSpline(const Splines &splines, UInt splineIndex, bool isSpacingCurvatureAdapted, double h)
Constructor.
Definition: Splines.hpp:209
meshkernel::ComputeDistance
double ComputeDistance(const Point &firstPoint, const Point &secondPoint, const Projection &projection)
Computes the distance between two points (dbdistance)
meshkernel::FuncDistanceFromAPoint
This structure is used to compute the point on a spline closest to another point.
Definition: Splines.hpp:257
meshkernel::Splines::ComputeClosestPointOnSplineSegment
Point ComputeClosestPointOnSplineSegment(UInt index, double startSplineSegment, double endSplineSegment, Point point) const
Computes the point on a spline segment which is the closest to another point.
meshkernel::FuncAdimensionalToDimensionalDistanceOnSpline::m_h
double m_h
When accounting for curvature, the height to use.
Definition: Splines.hpp:251
meshkernel::FuncAdimensionalToDimensionalDistanceOnSpline::SetDimensionalDistance
void SetDimensionalDistance(double distance)
Set dimensional distance.
Definition: Splines.hpp:235
meshkernel::Splines::GetSplinesIntersection
bool GetSplinesIntersection(UInt first, UInt second, double &crossProductIntersection, Point &intersectionPoint, double &firstSplineRatio, double &secondSplineRatio) const
Computes the intersection of two splines (sect3r)
meshkernel::Splines::ComputePointOnSplineFromAdimensionalDistance
std::tuple< std::vector< Point >, std::vector< double > > ComputePointOnSplineFromAdimensionalDistance(UInt index, double maximumGridHeight, bool isSpacingCurvatureAdapted, const std::vector< double > &distances) const
Compute the points on a spline lying at certain distance.
meshkernel::FuncDistanceFromAPoint::operator()
double operator()(double adimensionalDistanceReferencePoint) const
This is the function we want to find the root of.
Definition: Splines.hpp:270
meshkernel::FuncAdimensionalToDimensionalDistanceOnSpline::m_isSpacingCurvatureAdapted
bool m_isSpacingCurvatureAdapted
Is spacing curvature adapted.
Definition: Splines.hpp:250
meshkernel::FuncAdimensionalToDimensionalDistanceOnSpline::FuncAdimensionalToDimensionalDistanceOnSpline
FuncAdimensionalToDimensionalDistanceOnSpline(const Splines &splines, UInt splineIndex, bool isSpacingCurvatureAdapted, double h, double distance)
Constructor.
Definition: Splines.hpp:223
meshkernel::FuncAdimensionalToDimensionalDistanceOnSpline::operator()
double operator()(double adimensionalDistanceReferencePoint) const
This is the function we want to find the root of.
Definition: Splines.hpp:241
meshkernel::Splines::ComputeSplineLength
double ComputeSplineLength(UInt index, double startAdimensionalCoordinate, double endAdimensionalCoordinate, UInt numSamples=100, bool accountForCurvature=false, double height=1.0, double assignedDelta=-1.0) const
Computes the spline length in s coordinates (GETDIS)
meshkernel::Splines::MaxSizeIndex
UInt MaxSizeIndex() const
Get the index of the spline with the largest number of spline points.
meshkernel::FuncAdimensionalToDimensionalDistanceOnSpline::m_DimensionalDistance
double m_DimensionalDistance
Dimensional distance.
Definition: Splines.hpp:253
meshkernel::Splines::ComputeClosestPoint
Point ComputeClosestPoint(UInt index, Point point) const
Computes the point on a spline segment which is the closest to another point.
meshkernel::Splines
A class describing splines.
Definition: Splines.hpp:47