FhSim  3.1.0
Marine systems simulation
Loading...
Searching...
No Matches
FieldState.h
1#pragma once
2
8#include "Bump.h"
9#include "HashGrid.h"
10#include "TiledGrid.h"
11
12#include <array>
13#include <cstddef>
14#include <cstdint>
15#include <fhsim_environment/AggregationField.h>
16#include <memory>
17#include <optional>
18#include <vector>
19
20namespace environment::field
21{
22
30{
31 std::array<double, 3> origin {0.0, 0.0, 0.0};
32 std::vector<float> x, y, z, a, b, c, cosPhi, sinPhi, amp;
33 std::vector<aggregation::Box> boxes;
34 std::vector<Aggregation> shapes;
35 std::vector<std::array<double, 2>> patchCentres;
36 std::vector<std::size_t> patchIndices;
38 double totalMass = 0.0;
39
40 // The keyed store (ApplyChanges); unused while keyed is false.
41 bool keyed = false;
43 std::vector<AggregationId> ids;
44 std::vector<std::uint8_t> live;
45 std::size_t liveCount = 0;
46 AggregationId nextId = 0;
47
49 [[nodiscard]] std::size_t Size() const { return keyed ? liveCount : x.size(); }
50
52 [[nodiscard]] std::size_t SlotCount() const { return x.size(); }
53
55 [[nodiscard]] bool IsLive(std::size_t slot) const { return !keyed || live[slot] != 0; }
56
58 [[nodiscard]] AggregationId NextId() const { return keyed ? nextId : static_cast<AggregationId>(x.size()); }
59
60 [[nodiscard]] aggregation::Bump Load(std::size_t i) const
61 {
62 return {x[i], y[i], z[i], a[i], b[i], c[i], cosPhi[i], sinPhi[i], amp[i]};
63 }
64
66 template<class Visitor>
67 void ForEachCandidate(const aggregation::Box& query, Visitor&& visit) const
68 {
69 if (keyed) {
70 tiled.ForEachCandidate(query, visit);
71 } else {
72 grid.ForEachCandidate(query, visit);
73 }
74 }
75
77 void Candidates(const aggregation::Box& query, std::vector<std::uint32_t>& out) const
78 {
79 if (keyed) {
80 tiled.Candidates(query, out);
81 } else {
82 grid.Candidates(query, out);
83 }
84 }
85
87 [[nodiscard]] std::vector<double> SlotMassInBox(const OrientedBox& box) const;
88
89 // Keyed store helpers (AggregationFieldChanges.cpp).
90
93 void WriteSlot(std::size_t slot, const Aggregation& aggregation);
94
96 [[nodiscard]] double LiveMass() const;
97
99 [[nodiscard]] std::optional<std::size_t> SlotOf(AggregationId id) const;
100
102 [[nodiscard]] AggregationId IdOf(std::size_t slot) const { return keyed ? ids[slot] : static_cast<AggregationId>(slot); }
103
105 [[nodiscard]] std::vector<aggregation::TiledGrid::Item> AllItems() const;
106
108 [[nodiscard]] aggregation::TiledGrid FirstGrid(const std::optional<AggregationGridBounds>& bounds) const;
109
111 [[nodiscard]] std::shared_ptr<State> CopyWithRoom(std::size_t extra) const;
112
114 [[nodiscard]] std::shared_ptr<State> Compacted() const;
115};
116
118[[nodiscard]] const char* AggregationProblem(const Aggregation& aggregation);
119
120} // namespace environment::field
void Candidates(const Box &query, std::vector< std::uint32_t > &out) const
Appends the ids of the boxes that overlap the query box to out (cleared first).
void ForEachCandidate(const Box &query, Visitor &&visit) const
Definition HashGrid.h:86
void ForEachCandidate(const Box &query, Visitor &&visit) const
Definition TiledGrid.h:114
void Candidates(const Box &query, std::vector< std::uint32_t > &out) const
Appends the ids of the boxes that overlap the query box to out (cleared first).
std::shared_ptr< State > Compacted() const
A keyed state without holes, with the same origin and grid geometry.
AggregationId NextId() const
The id the next add gets.
Definition FieldState.h:58
void ForEachCandidate(const aggregation::Box &query, Visitor &&visit) const
Calls visit(slot) once for each aggregation whose support box overlaps the query box.
Definition FieldState.h:67
double LiveMass() const
The sum of the masses of the live slots, in slot order.
aggregation::TiledGrid FirstGrid(const std::optional< AggregationGridBounds > &bounds) const
The empty grid of a first change: over the bounds, or over the support boxes; none when neither exist...
AggregationId IdOf(std::size_t slot) const
The id of a slot.
Definition FieldState.h:102
void WriteSlot(std::size_t slot, const Aggregation &aggregation)
bool IsLive(std::size_t slot) const
True when the slot holds an aggregation.
Definition FieldState.h:55
std::array< double, 3 > origin
Positions below are relative to this point.
Definition FieldState.h:31
void Candidates(const aggregation::Box &query, std::vector< std::uint32_t > &out) const
The slots of ForEachCandidate in out (cleared first).
Definition FieldState.h:77
aggregation::TiledGrid tiled
The index of a keyed state; no extent until a change leaves a box or gives bounds.
Definition FieldState.h:42
std::size_t Size() const
The number of aggregations.
Definition FieldState.h:49
std::vector< AggregationId > ids
Per slot, rising.
Definition FieldState.h:43
std::optional< std::size_t > SlotOf(AggregationId id) const
The slot of a live id, or none.
std::vector< std::size_t > patchIndices
Per aggregation; empty when patchCentres is.
Definition FieldState.h:36
std::shared_ptr< State > CopyWithRoom(std::size_t extra) const
A copy with room for extra more slots, so appending them does not copy the arrays again....
std::vector< std::array< double, 2 > > patchCentres
Thomas process patch centres of the last Initialise; may be empty.
Definition FieldState.h:35
std::vector< Aggregation > shapes
The aggregations as given, in double precision.
Definition FieldState.h:34
std::vector< aggregation::TiledGrid::Item > AllItems() const
The grid items of every live slot.
AggregationId nextId
The id of the next add.
Definition FieldState.h:46
std::vector< std::uint8_t > live
Per slot; 0 for a hole.
Definition FieldState.h:44
std::size_t SlotCount() const
The number of slots (aggregations and holes).
Definition FieldState.h:52
std::vector< double > SlotMassInBox(const OrientedBox &box) const
The mass [kg] of each slot inside an oriented box (0 for a hole); see MassPerAggregationInBox.
An axis-aligned box, NED [m]. Closed: a box that only touches another one overlaps it.
Definition HashGrid.h:21
One bump with its precomputed constants, in double.
Definition Bump.h:38