Tissue Forge C++ 0.2.1
Interactive, particle-based physics, chemistry and biology modeling and simulation environment
Loading...
Searching...
No Matches
tfMeshSolver.h
Go to the documentation of this file.
1/*******************************************************************************
2 * This file is part of Tissue Forge.
3 * Copyright (c) 2022-2024 T.J. Sego and Tien Comlekoglu
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 ******************************************************************************/
19
25#ifndef _MODELS_VERTEX_SOLVER_TFMESHSOLVER_H_
26#define _MODELS_VERTEX_SOLVER_TFMESHSOLVER_H_
27
28#include <tf_port.h>
29
30#include "tfMesh.h"
31#include "tfMeshLogger.h"
32
33#include <tfSubEngine.h>
34#include <tf_cycle.h>
35
36
37namespace TissueForge::models::vertex {
38
39
40 class MeshRenderer;
41
42
49 HRESULT VertexForce(const Vertex *v, FloatP_t *f);
50
57 static HRESULT VertexForce(const Vertex *v, FVector3 &f) { return VertexForce(v, f.data()); }
58
64 static FVector3 VertexForce(const Vertex *v) {
65 FVector3 f;
66 VertexForce(v, f.data());
67 return f;
68 }
69
70
74 struct CAPI_EXPORT MeshSolverTimers {
75
79 enum Section : unsigned int {
80 FORCE=0,
81 ADVANCE,
82 UPDATE,
83 QUALITY,
84 RENDERING,
85 LAST
86 };
87
94 HRESULT append(const Section &section, const ticks _ticks) { timers[section] += _ticks; return S_OK; }
95
103 double ms(const Section &section, const bool &avg=true) const;
104
109 for(size_t i = 0; i < Section::LAST; i++) timers[i] = 0;
110 return S_OK;
111 }
112
116 std::string str() const;
117
118 private:
119
120 ticks timers[Section::LAST];
121
122 };
123
124
130 class CAPI_EXPORT MeshSolverTimerInstance {
131
133 ticks tic;
134
135 public:
136
139
140 };
141
142
148 struct CAPI_EXPORT MeshSolver : SubEngine {
149
150 const char *name = "MeshSolver";
151
152 Mesh *mesh;
153
156
160 static HRESULT init();
161
165 static MeshSolver *get();
166
170 static HRESULT compact();
171
176
181
185 static bool isDirty();
186
188 static HRESULT setDirty(const bool &_isDirty);
189
193 static Mesh *getMesh();
194
202 static bool is3D();
203
210
217
223 static SurfaceType *findSurfaceFromName(const std::string &_name);
224
230 static BodyType *findBodyFromName(const std::string &_name);
231
237 static BodyType *getBodyType(const unsigned int &typeId);
238
244 static SurfaceType *getSurfaceType(const unsigned int &typeId);
245
249 static const int numBodyTypes();
250
254 static const int numSurfaceTypes();
255
259 static unsigned int numVertices();
260
264 static unsigned int numSurfaces();
265
269 static unsigned int numBodies();
270
274 static unsigned int sizeVertices();
275
279 static unsigned int sizeSurfaces();
280
284 static unsigned int sizeBodies();
285
290
296 static HRESULT update(const bool &_force=false);
297
302
306 static std::vector<unsigned int> getSurfaceVertexIndices();
307
312
316 static std::vector<unsigned int> getSurfaceVertexIndicesAsyncJoin();
317
321 static std::vector<MeshLogEvent> getLog() {
322 return MeshLogger::events();
323 }
324
333 static HRESULT log(const MeshLogEventType &type, const std::vector<int> &objIDs, const std::vector<MeshObjTypeLabel> &objTypes, const std::string &name="");
334
335 friend MeshRenderer;
336
337 private:
338
339 FloatP_t *_forces;
340 unsigned int _bufferSize;
341 unsigned int _surfaceVertices;
342 unsigned int _totalVertices;
343 bool _isDirty;
344 std::mutex _engineLock;
345 std::vector<unsigned int> _surfaceVertexIndices;
346
347 std::vector<BodyType*> _bodyTypes;
348 std::vector<SurfaceType*> _surfaceTypes;
349
351 HRESULT _compactInst();
352
354 bool _isDirtyInst() const;
355
357 HRESULT _setDirtyInst(const bool &_isDirty);
358
360 Mesh *_newMeshInst();
361
363 HRESULT _loadMeshInst(Mesh *mesh);
364
366 HRESULT _unloadMeshInst(Mesh *mesh);
367
369 HRESULT _registerTypeInst(BodyType *_type);
370
372 HRESULT _registerTypeInst(SurfaceType *_type);
373
375 SurfaceType *_findSurfaceFromNameInst(const std::string &_name);
376
378 BodyType *_findBodyFromNameInst(const std::string &_name);
379
381 BodyType *_getBodyTypeInst(const unsigned int &typeId) const;
382
384 SurfaceType *_getSurfaceTypeInst(const unsigned int &typeId) const;
385
387 HRESULT _positionChangedInst();
388
390 std::vector<unsigned int> _getSurfaceVertexIndicesInst() const;
391
393 HRESULT _getSurfaceVertexIndicesAsyncStartInst();
394
396 std::vector<unsigned int> _getSurfaceVertexIndicesAsyncJoinInst();
397
399 HRESULT _logInst(const MeshLogEventType &type, const std::vector<int> &objIDs, const std::vector<MeshObjTypeLabel> &objTypes, const std::string &name="");
400
401 };
402
403}
404
405#endif // _MODELS_VERTEX_SOLVER_TFMESHSOLVER_H_
Contains all Vertex, Surface and Body instances.
Definition tfMesh.h:52
Convenience class to time mesh solver performance.
Definition tfMeshSolver.h:130
The mesh vertex is a volume of a mesh centered at a point in a space.
Definition tfVertex.h:71
A SubEngine is a singleton object that injects dynamics into the Tissue Forge engine....
Definition tfSubEngine.h:36
Mesh body type.
Definition tfBody.h:608
Definition tfMeshRenderer.h:35
Vertex model mesh solver.
Definition tfMeshSolver.h:148
static HRESULT getSurfaceVertexIndicesAsyncStart()
Start getting the starting vertex index for each surface.
static const int numBodyTypes()
Get the number of registered body types.
static HRESULT init()
Initialize the solver.
static bool is3D()
Test whether the mesh is 3D.
static unsigned int sizeBodies()
Get the size of the list of bodies.
static unsigned int numSurfaces()
Get the number of surfaces.
static HRESULT registerType(BodyType *_type)
Register a body type.
static Mesh * getMesh()
Get the mesh.
HRESULT preStepJoin() override
Last call before forces are calculated for a step.
static const int numSurfaceTypes()
Get the number of registered surface types.
static HRESULT log(const MeshLogEventType &type, const std::vector< int > &objIDs, const std::vector< MeshObjTypeLabel > &objTypes, const std::string &name="")
Log an event.
static unsigned int numBodies()
Get the number of bodies.
static HRESULT positionChanged()
Update internal data due to a change in position.
static HRESULT engineUnlock()
Unlocks the engine for thread-safe engine operations.
static HRESULT compact()
Reduce internal buffers and storage.
static HRESULT update(const bool &_force=false)
Update the solver if dirty.
MeshSolverTimers timers
Definition tfMeshSolver.h:155
HRESULT postStepStart() override
First call after forces are calculated, and before events are executed, for a step.
static std::vector< unsigned int > getSurfaceVertexIndicesAsyncJoin()
Finish getting the starting vertex index for each surface.
HRESULT postStepJoin() override
Last call after forces are calculated, and before events are executed, for a step.
static HRESULT registerType(SurfaceType *_type)
Register a surface type.
static std::vector< unsigned int > getSurfaceVertexIndices()
Get the starting vertex index for each surface.
static MeshSolver * get()
Get the solver singleton.
HRESULT preStepStart() override
First call before forces are calculated for a step.
static BodyType * findBodyFromName(const std::string &_name)
Find a registered body type by name.
static SurfaceType * findSurfaceFromName(const std::string &_name)
Find a registered surface type by name.
static SurfaceType * getSurfaceType(const unsigned int &typeId)
Get a surface type by id.
static std::vector< MeshLogEvent > getLog()
Get the current logger events.
Definition tfMeshSolver.h:321
static bool isDirty()
Test whether the current mesh state needs updated.
static unsigned int sizeSurfaces()
Get the size of the list of surfaces.
static HRESULT engineLock()
Locks the engine for thread-safe engine operations.
static unsigned int sizeVertices()
Get the size of the list of vertices.
static HRESULT setDirty(const bool &_isDirty)
static unsigned int numVertices()
Get the number of vertices.
static BodyType * getBodyType(const unsigned int &typeId)
Get a body type by id.
Mesh solver performance timers.
Definition tfMeshSolver.h:74
HRESULT reset()
Reset the timers.
Definition tfMeshSolver.h:108
HRESULT append(const Section &section, const ticks _ticks)
Append time to a section.
Definition tfMeshSolver.h:94
std::string str() const
Get a string representation of the current timer average values.
Section
Solver routine sections.
Definition tfMeshSolver.h:79
double ms(const Section &section, const bool &avg=true) const
Get the recorded runtime of a solver section, in ms.
Mesh surface type.
Definition tfSurface.h:1099
int32_t HRESULT
Definition tf_port.h:255
MeshLogEventType
Types of log events.
Definition tfMeshLogger.h:45
HRESULT VertexForce(const Vertex *v, FloatP_t *f)
Calculate the force on a vertex.