Cogs.Core
BeamGroupComponent.cpp
1#include "BeamGroupComponent.h"
2#include "Context.h"
3#include "Types.h"
4#include "ExtensionRegistry.h"
5
6#include "DataSetComponent.h"
7#include "DataRefComponent.h"
8#include "../Systems/DataSetSystem.h"
9
10using namespace Cogs::Core;
11using namespace Cogs::Reflection;
12using namespace Cogs::Core::EchoSounder;
13
14void BeamGroupComponent::registerType()
15{
16
17 static constexpr EnumeratorDef enumerators[] = {
18 { "SingleBeam", Topology::SingleBeam },
19 { "VerticalPlane", Topology::VerticalPlane },
20 { "TransversalPlane", Topology::TransversalPlane },
21 { "ConeSector", Topology::ConeSector },
22 { "Grid", Topology::Grid },
23 { "Cone", Topology::Cone }
24 };
25 TypeDatabase::createType<Topology>().setEnumerators(enumerators);
26
27 Field fields[] = {
28 Field(Name("topology"), &BeamGroupComponent::topology),
29 Field(Name("majorCount"), &BeamGroupComponent::majorCount),
30 Field(Name("minorCount"), &BeamGroupComponent::minorCount),
31 Field(Name("beams"), &BeamGroupComponent::beams)
32 };
33 Method methods[] = {
34 Method(Name("initialize"), &BeamGroupComponent::initialize),
35 Method(Name("update"), &BeamGroupComponent::update),
36 };
37 DynamicComponent::registerDerivedType<BeamGroupComponent>()
38 .setFields(fields)
39 .setMethods(methods);
40}
41
42void BeamGroupComponent::initialize(Context * context)
43{
44 this->context = context;
45}
46
47void BeamGroupComponent::update()
48{
49 auto * dataRefComp = getComponent<DataRefComponent>();
50 DataSetSystem* dataSystem = nullptr;
51 DataSetComponent* dataComp = nullptr;
52 bool changed = false;
53
54 if (!dataRefComp) goto insane;
55 if (!dataRefComp->data) goto insane;
56
57 dataComp = dataRefComp->data->getComponent<DataSetComponent>();
58 if (!dataComp) goto insane;
59
60 dataSystem = ExtensionRegistry::getExtensionSystem<DataSetSystem>(context);
61 if (!dataSystem) goto insane;
62 {
63 auto& dataData = dataSystem->getData(dataComp);
64
65 changed =
66 (dataComp_ != dataComp_) || //FIXME philipb : Should be be dataComp_ != dataComp?
67 (dataConfGen != dataData.configGen);
68
69 if (!changed || hasChanged()) {
70 // Check if something actually has changed.
71 changed = changed || (majorCount != majorCount_);
72 changed = changed || (minorCount != minorCount_);
73 changed = changed || (beams.size() != beams_.size());
74 if (!changed) {
75 for (size_t i = 0; i < beams.size() && !changed; i++) {
76 changed = changed || (beams[i] != beams_[i]);
77 }
78 }
79 }
80
81 if (changed) {
82 gen++;
83 dataComp_ = dataComp_;
84 dataConfGen = dataData.configGen;
85 majorCount_ = majorCount;
86 minorCount_ = minorCount;
87 beams_ = beams;
88
89 if ((majorCount == 0) || (minorCount == 0)) goto insane;
90
91 switch (topology)
92 {
93 case Cogs::Core::EchoSounder::Topology::SingleBeam:
94 if ((majorCount != 1) || (minorCount != 1)) goto insane;
95 minorClosed = false;
96 break;
97 case Cogs::Core::EchoSounder::Topology::VerticalPlane:
98 if (majorCount != 1) goto insane;
99 minorClosed = false;
100 break;
101 case Cogs::Core::EchoSounder::Topology::TransversalPlane:
102 if (majorCount != 1) goto insane;
103 minorClosed = false;
104 break;
105 case Cogs::Core::EchoSounder::Topology::ConeSector:
106 if (majorCount != 1) goto insane;
107 minorClosed = false;
108 break;
109 case Cogs::Core::EchoSounder::Topology::Grid:
110 minorClosed = false;
111 break;
112 case Cogs::Core::EchoSounder::Topology::Cone:
113 if (majorCount != 1) goto insane;
114 minorClosed = true;
115 break;
116 default:
117 assert(false && "Unhandled case");
118 }
119
120 const auto& config = dataData.persistent->config;
121
122 if (majorCount * minorCount != beams.size()) goto insane;
123 for (auto ix : beams) {
124 if (config.beamCount <= ix) goto insane;
125 }
126 this->sane = true;
127
128 directionX.resize(beams.size());
129 directionY.resize(beams.size());
130 minDirectionX = std::numeric_limits<float>::max();
131 minDirectionY = std::numeric_limits<float>::max();
132 maxDirectionX = -std::numeric_limits<float>::max();
133 maxDirectionY = -std::numeric_limits<float>::max();
134 maxBeamWidthX = -std::numeric_limits<float>::max();
135 maxBeamWidthY = -std::numeric_limits<float>::max();
136 for (size_t i = 0; i < beams.size(); i++) {
137 directionX[i] = config.directionX[i];
138 directionY[i] = config.directionY[i];
139 maxDirectionX = glm::max(maxDirectionX, directionX[i]);
140 minDirectionX = glm::min(minDirectionX, directionX[i]);
141 maxDirectionY = glm::max(maxDirectionY, directionY[i]);
142 minDirectionY = glm::min(minDirectionY, directionY[i]);
143 maxBeamWidthX = glm::max(maxBeamWidthX, config.beamWidthX[i]);
144 maxBeamWidthY = glm::max(maxBeamWidthY, config.beamWidthY[i]);
145 }
146
147 if (majorCount == 1) {
148 // FIXME: SN90 probably requires different logic.
151 }
152 minDistance = config.depthOffset;
153 maxDistance = minDistance + config.sampleCount * config.depthStep;
154 }
155 if (callback) {
156 callback(context);
157 callback = nullptr;
158 }
159 return;
160 }
161
162insane:
163 this->sane = false;
164 if (callback) {
165 callback(context);
166 callback = nullptr;
167 }
168}
ComponentType * getComponent() const
Definition: Component.h:159
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Field definition describing a single data member of a data structure.
Definition: Field.h:68
Simple method definition.
Definition: Method.h:72
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
Contains reflection support.
Definition: Component.h:11
bool minorClosed
Maintained by BeamGroupComponent::update.
std::vector< float > directionY
Subset of directions, maintained by BeamGroupComponent::update.
float minDistance
Along ray, maintained by BeamGroupComponent::update.
float maxBeamWidthY
Maintained by BeamGroupComponent::update.
float maxBeamWidthX
Maintained by BeamGroupComponent::update.
float minDirectionY
Maintained by BeamGroupComponent::update.
float minDirectionX
Maintained by BeamGroupComponent::update.
std::vector< float > directionX
Subset of directions, maintained by BeamGroupComponent::update.
float maxDirectionX
Maintained by BeamGroupComponent::update.
float maxDistance
Along ray, maintained by BeamGroupComponent::update.
float maxDirectionY
Maintained by BeamGroupComponent::update.
Represents an unique name.
Definition: Name.h:70