Cogs.Core
MultiphaseFlowComponent.h
1#pragma once
2
3#include "EntityDefinition.h"
4#include "Resources/Resources.h"
5#include "Components/Core/DynamicComponent.h"
6#include "Resources/VertexFormats.h"
7
8#include <glm/vec3.hpp>
9#include <glm/vec4.hpp>
10
11#include <string>
12
13namespace Cogs
14{
15 namespace Core
16 {
18 {
19 public:
20 std::vector<glm::vec3> trajectoryPoints;
21 std::vector<float> radius;
22 std::vector<glm::vec3> flowFraction;
23 std::vector<float> wallValues; // normalized 0..1
24 float heightScale = 1.f;
25 float radiusScale = 1.f;
26 bool showWater = true;
27 bool showOil = true;
28 bool showGas = true;
29 float unitScale = 1.f;
30 std::string unitText = "m";
31 bool updateNeeded = true;
32 glm::vec3 waterColor;
33 glm::vec3 oilColor;
34 glm::vec3 gasColor;
35 float fontSize = 20.f;
36 bool showAxialExtents = true;
37 bool showShadow = true;
38 bool showGrid = true;
39 bool centerMesh = true;
40 bool showIndicatorRing = false;
41 float indicatorRingPosition = 0.f;
42 glm::vec3 indicatorRingColor;
43 void updateFontSize();
44
46 void initialize(Context * context);
47 void update();
48
49 static void registerType();
50
52 {
53 glm::vec3 pos;
54 glm::vec3 flowFraction;
55 float md = 0.0f;
56 float radius = 0.0f;
57 float wallValue = 0.0f;
58 glm::quat rotation;
59 };
60
61 private:
63 {
64 float widthWaterOil = 0.f;
65 float widthOilGas = 0.f;
66 float heightWaterOil = 0.f;
67 float heightOilGas = 0.f;
68 float water = 0.f;
69 float oil = 0.f;
70 float gas = 0.f;
71 };
72
74 {
75 glm::vec3 position;
76 float radius = 1.f;
77 glm::quat rotation;
78 glm::vec3 direction;
80 float wall = 0.f;
81 bool invertedFlow = false; // Inverted flow for pipes loops.
82 float invertedFlowRatio = 0.f;
83 bool annularFlow = false;
84 float annularFlowRatio = 0.f;
85 glm::vec3 rotationUpVector = glm::vec3(0, 1, 0);
86 };
87
88 struct Vertex
89 {
90 glm::vec3 position;
91 glm::vec4 normalAndOpacity;
92
93 static VertexFormatHandle getVertexFormat()
94 {
95 const VertexElement elements[] = {
96 { 0, DataFormat::X32Y32Z32_FLOAT, ElementSemantic::Position, 0, InputType::VertexData, 0 },
97 { 3 * sizeof(float), DataFormat::X32Y32Z32W32_FLOAT, ElementSemantic::Normal, 0, InputType::VertexData, 0 },
98 };
99
100 static VertexFormatHandle vertexFormat = Cogs::VertexFormats::createVertexFormat(elements, 2);
101
102 return vertexFormat;
103 }
104 };
105
107 {
108 glm::vec3 position;
109 glm::vec4 normalAndOpacity;
110 glm::vec3 color;
111
112 static VertexFormatHandle getVertexFormat()
113 {
114 const VertexElement elements[] = {
115 { 0, DataFormat::X32Y32Z32_FLOAT, ElementSemantic::Position, 0, InputType::VertexData, 0 },
116 { 3 * sizeof(float), DataFormat::X32Y32Z32W32_FLOAT, ElementSemantic::Normal, 0, InputType::VertexData, 0 },
117 { 7 * sizeof(float), DataFormat::X32Y32Z32_FLOAT, ElementSemantic::Color, 0, InputType::VertexData, 0 },
118 };
119
120 static VertexFormatHandle vertexFormat = Cogs::VertexFormats::createVertexFormat(elements, 3);
121
122 return vertexFormat;
123 }
124 };
125
126 enum MeshType
127 {
128 // Opaque
129 StratifiedWater,
130 AnnularWater,
131 StratifiedOil,
132
133 // Transparent
134 AnnularOil,
135 StratifiedGas,
136 AnnularGas,
137
138 Wall,
139
140 Shadow,
141
142 First = 0,
143 Last = 7
144 };
145
146 private:
147 static bool isStratified(MeshType liquidType);
148 static bool isAnnular(MeshType liquidType);
149 static unsigned int getNumOfSegmentsInCrossSection(MeshType liquidType);
150
151 void createMeshEntity(EntityPtr& mesh, MeshType liquidType);
152 void updateMaterial(EntityPtr mesh, MeshType liquidType);
153 void generateMultiphaseFlowMesh(Mesh* mesh, const std::vector<ControlPoint>& controlPoints, MeshType liquidType);
154
155 void createGridEntity();
156 void createTextEntity();
157 void createIndicatorRing();
158
159 void updateGroundGrid();
160 void updateAxisAnnotations();
161 void updateIndicatorRing(const std::vector<TrajectoryPoint>& trajectory);
162
163 void generateExtrusionVertices(std::vector<Vertex>& vertexBuffer, std::vector<unsigned int>& indexBuffer, const std::vector<ControlPoint>& controlPoints, MeshType liquidType);
164 void generateExtrusionVerticesWall(std::vector<WallVertex>& vertexBuffer, std::vector<unsigned int>& indexBuffer, const std::vector<ControlPoint>& controlPoints, MeshType liquidType);
165 void generateClosingCaps(std::vector<Vertex>& vertexBuffer, std::vector<unsigned int>& indexBuffer, const ControlPoint& controlPoint, MeshType liquidType);
166
167 void transformCrossection(float ratio, float alpha, std::vector<Vertex>& vertexBuffer, const ControlPoint& controlPoint, MeshType liquidType, bool isClosingCap = false);
168
169 void removeDuplicatedPoints(std::vector<TrajectoryPoint>& points);
170 void applyScaling(std::vector<TrajectoryPoint>& points);
171 void center(std::vector<TrajectoryPoint>& points);
172
173 void updateMeshes();
174 void updateShadowVisibility();
175
176 void finalizeControlPoints(std::vector<ControlPoint>& controlPoints);
177
178 static glm::vec3 wallValueToColor(const ControlPoint& controlPoint);
179
180 static void calculateNormals(std::vector<Vertex>& vertexBuffer, std::vector<unsigned int>& indexBuffer);
181
182 private:
183 enum AnnotationAxis
184 {
185 X,
186 YMin,
187 YMax,
188 Y,
189 Z,
190 AxisFirst = X,
191 AxisLast = Z
192 };
193
194 private:
195 Context* context = nullptr;
196 Material* mainMaterial = nullptr;
197 VariableKey diffuseColorMaterialPropertyKey;
198 VariableKey emissiveColorMaterialPropertyKey;
199 VariableKey specularColorMaterialPropertyKey;
200 EntityPtr meshEntity[MeshType::Last + 1];
201 EntityPtr wallEntity;
202 EntityPtr groundGridMeshEntity;
203 EntityPtr annotationAxisMeshEntity;
204 EntityPtr indicatorRingEntity;
205
206 EntityPtr textEntity[AnnotationAxis::AxisLast + 1];
207
208 glm::vec3 geometryMin;
209 glm::vec3 geometryMax;
210
211 glm::vec3 scaledTrajectoryMin;
212 glm::vec3 scaledTrajectoryMax;
213
214 float currentFontSize = -1.f;
215
216 bool currentShowAxialExtents = true;
217 bool currentShowGrid = true;
218 bool currentCenterMesh = true;
219 bool currentShowIndicatorRing = false;
220 };
221 } // namespace Core
222} // namespace Cogs
223
224template<> inline Cogs::StringView getName<Cogs::Core::MultiphaseFlowComponent>() { return "MultiphaseFlowComponent"; }
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Base class for components implementing dynamic behavior.
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
std::shared_ptr< ComponentModel::Entity > EntityPtr
Smart pointer for Entity access.
Definition: EntityPtr.h:12
uint16_t VariableKey
Used to lookup material properties.
Definition: Resources.h:46
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
@ VertexData
Per vertex data.
@ Position
Position semantic.
@ Normal
Normal semantic.
@ Color
Color semantic.
Meshes contain streams of vertex data in addition to index data and options defining geometry used fo...
Definition: Mesh.h:265
Vertex element structure used to describe a single data element in a vertex for the input assembler.
Definition: VertexFormat.h:38