FhSim  3.1.0
Marine systems simulation
Loading...
Searching...
No Matches
SeabedStart.h
1#pragma once
2
13#include <fhsim_environment/EnvironmentProvider.h>
14#include <fhsim_environment/seabed/SeabedCatenary.h>
15
16#include <marenv/marenv.h>
17
18#include <algorithm>
19#include <array>
20#include <cmath>
21#include <limits>
22#include <vector>
23
24namespace seabed_start
25{
26
28constexpr double kEndBelowSeabedTolerance = 1e-6;
29
37inline environment::seabed::SeabedCatenary3D SeabedStartShape(environment::EnvironmentProvider* environment, const double posA[3], const double posB[3], double length)
38{
39 constexpr double nan = std::numeric_limits<double>::quiet_NaN();
40 double seabedDepth = nan;
41 if (environment != nullptr) {
42 const double* deeper = posA[2] >= posB[2] ? posA : posB;
43 double depth = nan;
44 // An end below the seabed (a buried anchor) is off the curve, which ends on the seabed: the start then stays as it is (not valid).
45 if (marenv::IsUsable(environment->GetSeaDepth(deeper, depth)) && deeper[2] <= depth + kEndBelowSeabedTolerance) {
46 seabedDepth = depth;
47 }
48 }
49 return environment::seabed::SeabedCatenary3D(posA, posB, length, seabedDepth);
50}
51
53inline void SeabedStartTangent(const environment::seabed::SeabedCatenary3D& shape, double length, double s, double direction[3])
54{
55 const double step = 1e-4 * length;
56 const double lo = std::fmax(0.0, s - step);
57 const double hi = std::fmin(length, s + step);
58 double a[3];
59 double b[3];
60 shape.Coordinate(lo, a);
61 shape.Coordinate(hi, b);
62 const double norm = std::sqrt((b[0] - a[0]) * (b[0] - a[0]) + (b[1] - a[1]) * (b[1] - a[1]) + (b[2] - a[2]) * (b[2] - a[2]));
63 for (int i = 0; i < 3; i++) {
64 direction[i] = (b[i] - a[i]) / norm;
65 }
66}
67
70{
71 environment::seabed::SeabedCatenary3D shape;
72 double arcLength = 0.0;
73 std::vector<std::array<double, 3>> points;
74 bool valid = false;
75};
76
77namespace detail
78{
79
82inline double NextJointArc(const environment::seabed::SeabedCatenary3D& shape, double arcLength, double arc0, double chord)
83{
84 const auto distanceFrom = [&](const double from[3], double arc) {
85 double p[3];
86 shape.Coordinate(arc, p);
87 return std::sqrt((p[0] - from[0]) * (p[0] - from[0]) + (p[1] - from[1]) * (p[1] - from[1]) + (p[2] - from[2]) * (p[2] - from[2]));
88 };
89 double start[3];
90 shape.Coordinate(arc0, start);
91 const double atEnd = distanceFrom(start, arcLength);
92 if (atEnd < chord) {
93 return arcLength + (chord - atEnd);
94 }
95 constexpr int bisectionSteps = 80;
96 double low = std::min(arc0 + chord, arcLength); // the distance is at most the arc, so it is not above @p chord here
97 double high = arcLength;
98 for (int step = 0; step < bisectionSteps; ++step) {
99 const double middle = 0.5 * (low + high);
100 (distanceFrom(start, middle) < chord ? low : high) = middle;
101 }
102 return 0.5 * (low + high);
103}
104
106inline double LastJointOvershoot(const environment::seabed::SeabedCatenary3D& shape, double arcLength, const std::vector<double>& chords)
107{
108 double arc = 0.0;
109 for (const double chord : chords) {
110 arc = NextJointArc(shape, arcLength, arc, chord);
111 }
112 return arc - arcLength;
113}
114
115} // namespace detail
116
124inline SeabedStartJoints SeabedStartChain(environment::EnvironmentProvider* environment, const double posA[3], const double posB[3], const std::vector<double>& chords)
125{
126 double chordSum = 0.0;
127 for (const double chord : chords) {
128 chordSum += chord;
129 }
130 SeabedStartJoints result {SeabedStartShape(environment, posA, posB, chordSum), chordSum, {}, false};
131 if (!result.shape.Valid()) {
132 return result;
133 }
134 const auto shapeOf = [&](double arcLength) { return SeabedStartShape(environment, posA, posB, arcLength); };
135
136 constexpr double bracketStep = 0.01; // relative
137 constexpr int maxBracket = 30;
138 constexpr int maxBisect = 200;
139 double low = chordSum;
140 double high = chordSum;
141 bool bracketed = detail::LastJointOvershoot(result.shape, low, chords) <= 0.0;
142 for (int step = 0; step < maxBracket && !bracketed; ++step) {
143 high += bracketStep * chordSum;
144 const auto shape = shapeOf(high);
145 if (!shape.Valid()) {
146 return result; // the longer curve has no lying part
147 }
148 bracketed = detail::LastJointOvershoot(shape, high, chords) <= 0.0;
149 if (!bracketed) {
150 low = high;
151 }
152 }
153 if (!bracketed) {
154 return result;
155 }
156 double arcLength = high;
157 for (int step = 0; step < maxBisect && high - low > 1e-12 * chordSum; ++step) {
158 const double middle = 0.5 * (low + high);
159 const auto shape = shapeOf(middle);
160 if (!shape.Valid()) {
161 return result;
162 }
163 (detail::LastJointOvershoot(shape, middle, chords) > 0.0 ? low : high) = middle;
164 }
165 arcLength = 0.5 * (low + high);
166 result.shape = shapeOf(arcLength);
167 if (!result.shape.Valid()) {
168 return result;
169 }
170 result.arcLength = arcLength;
171 double arc = 0.0;
172 for (size_t i = 0; i <= chords.size(); ++i) {
173 std::array<double, 3> point;
174 result.shape.Coordinate(std::min(arc, arcLength), point.data());
175 result.points.push_back(point);
176 if (i < chords.size()) {
177 arc = detail::NextJointArc(result.shape, arcLength, arc, chords[i]);
178 }
179 }
180 result.valid = true;
181 return result;
182}
183
184} // namespace seabed_start
constexpr bool IsUsable(Status status) noexcept
Definition marenv.h:59
Definition FlukeAnchor.h:124
A chain of rigid elements laid on the seabed catenary with closed joints (SeabedStartChain).
Definition SeabedStart.h:70
bool valid
False where the line has no lying part: nothing else is set.
Definition SeabedStart.h:74
double arcLength
The arc length of shape [m].
Definition SeabedStart.h:72
environment::seabed::SeabedCatenary3D shape
The seabed catenary the joints lie on: of the arc length arcLength, a little over the chain length.
Definition SeabedStart.h:71
std::vector< std::array< double, 3 > > points
The joints J_0 (at end A) to J_N (at end B), NED [m]; the distance of J_i and J_i+1 is chords[i].
Definition SeabedStart.h:73