13#include <fhsim_environment/EnvironmentProvider.h>
14#include <fhsim_environment/seabed/SeabedCatenary.h>
16#include <marenv/marenv.h>
28constexpr double kEndBelowSeabedTolerance = 1e-6;
37inline environment::seabed::SeabedCatenary3D SeabedStartShape(environment::EnvironmentProvider*
environment,
const double posA[3],
const double posB[3],
double length)
39 constexpr double nan = std::numeric_limits<double>::quiet_NaN();
40 double seabedDepth = nan;
42 const double* deeper = posA[2] >= posB[2] ? posA : posB;
49 return environment::seabed::SeabedCatenary3D(posA, posB, length, seabedDepth);
53inline void SeabedStartTangent(
const environment::seabed::SeabedCatenary3D& shape,
double length,
double s,
double direction[3])
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);
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;
71 environment::seabed::SeabedCatenary3D
shape;
73 std::vector<std::array<double, 3>>
points;
82inline double NextJointArc(
const environment::seabed::SeabedCatenary3D& shape,
double arcLength,
double arc0,
double chord)
84 const auto distanceFrom = [&](
const double from[3],
double arc) {
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]));
90 shape.Coordinate(arc0, start);
91 const double atEnd = distanceFrom(start, arcLength);
93 return arcLength + (chord - atEnd);
95 constexpr int bisectionSteps = 80;
96 double low = std::min(arc0 + chord, arcLength);
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;
102 return 0.5 * (low + high);
106inline double LastJointOvershoot(
const environment::seabed::SeabedCatenary3D& shape,
double arcLength,
const std::vector<double>& chords)
109 for (
const double chord : chords) {
110 arc = NextJointArc(shape, arcLength, arc, chord);
112 return arc - arcLength;
124inline SeabedStartJoints SeabedStartChain(environment::EnvironmentProvider*
environment,
const double posA[3],
const double posB[3],
const std::vector<double>& chords)
126 double chordSum = 0.0;
127 for (
const double chord : chords) {
130 SeabedStartJoints result {SeabedStartShape(
environment, posA, posB, chordSum), chordSum, {},
false};
131 if (!result.shape.Valid()) {
134 const auto shapeOf = [&](
double arcLength) {
return SeabedStartShape(
environment, posA, posB, arcLength); };
136 constexpr double bracketStep = 0.01;
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()) {
148 bracketed = detail::LastJointOvershoot(shape, high, chords) <= 0.0;
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()) {
163 (detail::LastJointOvershoot(shape, middle, chords) > 0.0 ? low : high) = middle;
165 arcLength = 0.5 * (low + high);
166 result.shape = shapeOf(arcLength);
167 if (!result.shape.Valid()) {
170 result.arcLength = arcLength;
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]);
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