1#include "RiserTelescopicJointComponent.h"
5#include "EntityStore.h"
7#include "Components/Data/TrajectoryComponent.h"
8#include "Components/Geometry/VariableExtrusionComponent.h"
9#include "Components/Appearance/MaterialComponent.h"
11#include "ProfileGenerator.h"
13#include "Foundation/Geometry/SampleListGenerator.hpp"
18void Cogs::Core::RiserTelescopicJointComponent::registerType()
46 Method(
Name(
"initialize"), &RiserTelescopicJointComponent::initialize),
47 Method(
Name(
"update"), &RiserTelescopicJointComponent::update),
50 DynamicComponent::registerDerivedType<RiserTelescopicJointComponent>().setFields(fields).setMethods(methods);
53void Cogs::Core::RiserTelescopicJointComponent::initialize(Context * context)
55 body = context->store->createChildEntity(
"VariableExtrusionShape", getContainer());
56 arm = context->store->createChildEntity(
"VariableExtrusionShape", getContainer());
57 top = context->store->createChildEntity(
"VariableExtrusionShape", getContainer());
60void Cogs::Core::RiserTelescopicJointComponent::update()
62 auto variableExtrusion = getComponent<VariableExtrusionComponent>();
64 assert(variableExtrusion &&
"No variable extrusion available for riser.");
66 if (!variableExtrusion->trajectory)
return;
68 auto trajectory = variableExtrusion->trajectory->getComponent<TrajectoryComponent>();
70 if (!trajectory)
return;
72 if (!this->hasChanged() && !variableExtrusion->hasChanged()) {
73 if (this->getComponent<MaterialComponent>()->hasChanged()) {
80 std::vector<float> trajectoryDepths;
82 SampleListGenerator::generateIndexedSamples(startDepth, endDepth, trajectory->indexes.data(),
static_cast<int>(trajectory->indexes.size()), trajectoryDepths);
84 if (trajectoryDepths.size() < 2)
return;
86 if (trajectoryDepths[0] > startDepth || trajectoryDepths[trajectoryDepths.size() - 1] < endDepth)
return;
90 updateBody(trajectoryDepths);
91 updateArm(trajectoryDepths);
92 updateTop(trajectoryDepths);
102 std::vector<glm::vec2> profile;
103 std::vector<float> depths;
105 const float startDepth = this->startDepth + this->topLength;
106 const float endDepth = startDepth + this->armLength;
107 const float pistonLength = this->pistonLength * variableExtrusion->
radiusScale;
109 const float innerRadius = this->armInnerRadius;
110 const float outerRadius = this->armOuterRadius;
111 const float bodyInnerRadius = this->pistonRadius;
113 profile.push_back(glm::vec2(innerRadius, 0)); depths.push_back(startDepth);
114 profile.push_back(glm::vec2(outerRadius, 0)); depths.push_back(startDepth);
116 ProfileGenerator::insertPoints(depths, profile, outerRadius, startDepth, endDepth - pistonLength, trajectoryDepths.cbegin(), trajectoryDepths.cend());
117 ProfileGenerator::generateConnector(depths, profile, endDepth - pistonLength, endDepth, outerRadius, bodyInnerRadius, innerRadius, endDepth);
118 ProfileGenerator::insertPointsReverse(depths, profile, innerRadius, startDepth, endDepth, trajectoryDepths.cend());
120 profile.push_back(glm::vec2(innerRadius, 0)); depths.push_back(startDepth);
126 variableExtrusion->depths = std::move(depths);
127 variableExtrusion->profile = std::move(profile);
129 variableExtrusion->setChanged();
133 material->diffuseColor = armColor;
134 material->setChanged();
144 std::vector<glm::vec2> profile;
145 std::vector<float> depths;
147 const float startDepth = this->startDepth;
148 const float endDepth = startDepth + this->topLength;
150 const float connectorLength = this->connectorLength * variableExtrusion->
radiusScale;
152 const float innerRadius = this->armInnerRadius;
153 const float outerRadius = this->armOuterRadius;
154 const float connectorRadius = this->connectorRadius;
156 ProfileGenerator::generateConnector(depths, profile, startDepth, startDepth + connectorLength, innerRadius, connectorRadius, outerRadius, startDepth);
157 ProfileGenerator::insertPoints(depths, profile, outerRadius, startDepth + connectorLength, endDepth, trajectoryDepths.cbegin(), trajectoryDepths.cend());
159 profile.push_back(glm::vec2(outerRadius, 0)); depths.push_back(endDepth);
160 profile.push_back(glm::vec2(innerRadius, 0)); depths.push_back(endDepth);
161 profile.push_back(glm::vec2(innerRadius, 0)); depths.push_back(startDepth);
167 variableExtrusion->depths = std::move(depths);
168 variableExtrusion->profile = std::move(profile);
170 variableExtrusion->setChanged();
180 std::vector<glm::vec2> profile;
181 std::vector<float> depths;
183 const float endDepth = this->endDepth;
184 const float startDepth = endDepth - this->cylinderLength;
186 const float connectorLength = this->connectorLength * variableExtrusion->
radiusScale;
187 const float connectorRadius = this->connectorRadius;
189 const float riserInnerRadius = this->armInnerRadius;
190 const float innerInnerRadius = this->armOuterRadius;
191 const float innerRadius = this->pistonRadius;
192 const float outerRadius = this->cylinderOuterRadius;
193 const float innerCylinderLength = this->innerCylinderLength;
195 ProfileGenerator::generateConnector(depths, profile, startDepth, startDepth + connectorLength, innerInnerRadius, connectorRadius, outerRadius, startDepth);
196 ProfileGenerator::insertPoints(depths, profile, outerRadius, startDepth + connectorLength, endDepth - connectorLength, trajectoryDepths.cbegin(), trajectoryDepths.cend());
197 ProfileGenerator::generateConnector(depths, profile, endDepth - connectorLength, endDepth, outerRadius, connectorRadius, riserInnerRadius, endDepth);
199 profile.push_back(glm::vec2(riserInnerRadius, 0)); depths.push_back(startDepth + innerCylinderLength);
200 profile.push_back(glm::vec2(innerRadius, 0)); depths.push_back(startDepth + innerCylinderLength);
202 ProfileGenerator::insertPointsReverse(depths, profile, innerRadius, startDepth + connectorLength, startDepth + innerCylinderLength, trajectoryDepths.cend());
204 profile.push_back(glm::vec2(innerRadius, 0)); depths.push_back(startDepth + connectorLength);
205 profile.push_back(glm::vec2(innerInnerRadius, 0)); depths.push_back(startDepth + connectorLength);
206 profile.push_back(glm::vec2(innerInnerRadius, 0)); depths.push_back(startDepth);
212 variableExtrusion->depths = std::move(depths);
213 variableExtrusion->profile = std::move(profile);
215 variableExtrusion->setChanged();
218void Cogs::Core::RiserTelescopicJointComponent::updateChildren()
220 const auto ves = getComponent<VariableExtrusionComponent>();
222 const auto children = {
228 for (
auto & c : children) {
230 c->closed = ves->closed;
231 c->radiusScale = ves->radiusScale;
234 const auto material = getComponent<MaterialComponent>();
236 const auto materials = {
237 body->getComponent<MaterialComponent>(),
238 top->getComponent<MaterialComponent>(),
241 for (
auto & m : materials) {
242 m->diffuseColor = material->diffuseColor;
ComponentType * getComponent() const
float armOuterRadius
Outer radius of the arm.
void updateBody(const std::vector< float > &trajectoryDepths)
Updates the extrusion profile for the cylinder.
float endDepth
End depth of the component.
float connectorRadius
Length of the connectors.
float pistonRadius
Piston radius.
float innerCylinderLength
Length of the inner cylinder (inner stroke length).
void updateTop(const std::vector< float > &trajectoryDepths)
Update the extrusion profile for the arm.
float cylinderOuterRadius
Outer radius of the cylinder part.
bool reverse
If the joint profile should be reversed, i.e the arm after the cylinder.
float cylinderLength
Length of the cylinder.
float connectorLength
Radius of the connectors.
float armInnerRadius
Inner radius of the arm.
glm::vec4 armColor
Arm color.
float topLength
Length of the arm tip.
float armLength
Length of the stroke arm.
float pistonLength
Piston length.
void updateArm(const std::vector< float > &trajectoryDepths)
Update the extrusion profile for the arm.
float startDepth
Starting depth of the component.
Field definition describing a single data member of a data structure.
Simple method definition.
void reverseProfile(std::vector< float > &depths, std::vector< glm::vec2 > &profile, const float startDepth, const float endDepth)
Reverses the given profile.
Contains geometry calculations and generation.
Contains reflection support.
Exposes material properties for legacy entities and code.
float radiusScale
Radius scale applied to the entire shape perpendicular to the direction of the trajectory.
std::shared_ptr< ComponentModel::Entity > trajectory
The trajectory this component is connected to.
Represents an unique name.