Tissue Forge C++ 0.2.1
Interactive, particle-based physics, chemistry and biology modeling and simulation environment
Loading...
Searching...
No Matches
tfSimulator.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
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 _SOURCE_TFSIMULATOR_H_
26#define _SOURCE_TFSIMULATOR_H_
27
28#include "TissueForge_private.h"
29
30#include "io/tfFIO.h"
31#ifdef TF_WITHCUDA
32#include "cuda/tfSimulatorConfig.h"
33#endif
34
35#include "tfUniverse.h"
36
37#include <cstdint>
38
39
40namespace TissueForge {
41
42
43 namespace rendering {
44 struct Application;
45 struct GlfwWindow;
46 }
47
48
59 struct CAPI_EXPORT Simulator {
60
61 struct Config;
62 class GLConfig;
63
69 enum WindowFlags : std::uint16_t
70 {
72 Fullscreen = 1 << 0,
73
77 Borderless = 1 << 1,
78
79 Resizable = 1 << 2,
80 Hidden = 1 << 3,
88 Maximized = 1 << 4,
89
90
91 Minimized = 1 << 5,
97 AlwaysOnTop = 1 << 6,
98
99
100
105 AutoIconify = 1 << 7,
106
112 Focused = 1 << 8,
113
124 Contextless = 1 << 9
125
126 };
127
128 // Of the available integrator types, these are supported by Simulator
129 enum class EngineIntegrator : int {
130 FORWARD_EULER = TissueForge::EngineIntegrator::FORWARD_EULER,
131 RUNGE_KUTTA_4 = TissueForge::EngineIntegrator::RUNGE_KUTTA_4
132 };
133
134 enum Key {
135 NONE,
136 WINDOWLESS,
137 GLFW
138 };
139
140 enum Options {
141 Windowless = 1 << 0,
142
143 glfw = 1 << 1,
150 GlForwardCompatible = 1 << 2,
151
159 GlNoError = 1 << 3,
160
161
167 GlDebug = 1 << 4,
168
169 GlStereo = 1 << 5
170 };
171
172 enum class DpiScalingPolicy : std::uint8_t {
173 /* Using 0 for an "unset" value */
174
175 #ifdef CORRADE_TARGET_APPLE
176 Framebuffer = 1,
177 #endif
178
179 #ifndef CORRADE_TARGET_APPLE
180 Virtual = 2,
181
182 Physical = 3,
183 #endif
184
185 Default
186 #ifdef CORRADE_TARGET_APPLE
187 = Framebuffer
188 #else
189 = Virtual
190 #endif
191 };
192
193 int32_t kind;
194 struct rendering::Application *app;
195
196 enum Flags {
197 Running = 1 << 0
198 };
199
203 static Simulator *get();
204
211
212 static HRESULT initConfig(const Config &conf, const GLConfig &glConf);
213
225
243
263 static HRESULT waitEventsTimeout(FloatP_t timeout);
264
270
277 static HRESULT run(FloatP_t et);
278
283 static HRESULT show();
284
288 static HRESULT close();
289
290 static HRESULT destroy();
291
293 static HRESULT redraw();
294
312 static HRESULT swapInterval(int si);
313
315 static const int getNumThreads();
316
317 static const rendering::GlfwWindow *getWindow();
318
319 #ifdef TF_WITHCUDA
325 static cuda::SimulatorConfig *getCUDAConfig();
326
334 static HRESULT makeCUDAConfigCurrent(cuda::SimulatorConfig *config);
335 #endif
336
338 static HRESULT throwExceptions(const bool &_throw);
339
341 static bool throwingExceptions();
342
343 // list of windows.
344 std::vector<rendering::GlfwWindow*> windows;
345 };
346
351 CAPI_FUNC(bool) isTerminalInteractiveShell();
352
356 CAPI_FUNC(HRESULT) setIsTerminalInteractiveShell(const bool &_interactive);
357
361 CPPAPI_FUNC(HRESULT) Simulator_init(const std::vector<std::string> &argv);
362
366 CPPAPI_FUNC(HRESULT) Simulator_init(Simulator::Config &conf, const std::vector<std::string> &appArgv=std::vector<std::string>());
367
368 struct CAPI_EXPORT Simulator::Config {
369
370 /*implicit*/
371 Config();
372
373 ~Config() {};
374
376 std::string title() const
377 {
378 return _title;
379 }
380
387 void setTitle(std::string title)
388 {
389 _title = std::move(title);
390 }
391
394 {
395 return _size;
396 }
397
406 Simulator::DpiScalingPolicy dpiScalingPolicy() const
407 {
408 return _dpiScalingPolicy;
409 }
410
422 {
423 return _dpiScaling;
424 }
425
426 void setDpiScaling(const fVector2 &vec)
427 {
428 _dpiScaling = vec;
429 }
430
431
432 void setSizeAndScaling(const iVector2& size, Simulator::DpiScalingPolicy dpiScalingPolicy = Simulator::DpiScalingPolicy::Default) {
433 _size = size;
434 _dpiScalingPolicy = dpiScalingPolicy;
435
436 }
437
438
439 void setSizeAndScaling(const iVector2& size, const FVector2& dpiScaling) {
440 _size = size;
441 _dpiScaling = dpiScaling;
442 }
443
453 void setWindowSize(const iVector2 &size)
454 {
455 _size = size;
456 }
457
459 uint32_t windowFlags() const
460 {
461 return _windowFlags;
462 }
463
470 void setWindowFlags(uint32_t windowFlags)
471 {
472 _windowFlags = windowFlags;
473 }
474
475 bool windowless() const {
476 return _windowless;
477 }
478
479 void setWindowless(bool val) {
480 _windowless = val;
481 }
482
483 int size() const {
484 return universeConfig.nParticles;
485 }
486
487 void setSize(int i ) {
488 universeConfig.nParticles = i;
489 }
490
492 unsigned int *seed() {
493 return _seed;
494 }
495
497 void setSeed(const unsigned int &seed) {
498 if(_seed != NULL) *_seed = seed;
499 else _seed = new unsigned int(seed);
500 }
501
502 bool throwingExceptions() const {
503 return _throwingExceptions;
504 }
505
506 void setThrowingExceptions(const bool &throwingExceptions) {
507 _throwingExceptions = throwingExceptions;
508 }
509
511 std::string *importDataFilePath() {
512 return _importDataFilePath;
513 }
514
516 bool hasImportDataFilePath() const {
517 return _importDataFilePath;
518 }
519
522 if(_importDataFilePath) {
523 delete _importDataFilePath;
524 _importDataFilePath = NULL;
525 }
526 }
527
528 void setImportDataFilePath(const std::string &_path) {
529 clearImportDataFilePath();
530 _importDataFilePath = new std::string();
531 *_importDataFilePath = _path;
532 }
533
536
537 int queues;
538
539 int argc = 0;
540
541 char** argv = NULL;
542
544 std::vector<FVector4> clipPlanes;
545
546 private:
547 std::string _title;
548 iVector2 _size;
549 uint32_t _windowFlags;
550 Simulator::DpiScalingPolicy _dpiScalingPolicy;
551 FVector2 _dpiScaling;
552 bool _windowless;
553 unsigned int *_seed = NULL;
554 bool _throwingExceptions = false;
555 std::string *_importDataFilePath = NULL;
556 };
557
569 class CAPI_EXPORT Simulator::GLConfig {
570 public:
576 enum Flag: uint32_t {
577 #ifndef MAGNUM_TARGET_GLES
584 ForwardCompatible = 1 << 0,
585 #endif
586
587 #if defined(DOXYGEN_GENERATING_OUTPUT) || defined(GLFW_CONTEXT_NO_ERROR)
595 NoError = 1 << 1,
596 #endif
597
603 Debug = 1 << 2,
604
605 Stereo = 1 << 3
606 };
607
613 typedef uint32_t Flags;
614
615 explicit GLConfig();
616 ~GLConfig();
617
619 Flags flags() const { return _flags; }
620
630 _flags = flags;
631 return *this;
632 }
633
643 _flags |= flags;
644 return *this;
645 }
646
656 _flags &= ~flags;
657 return *this;
658 }
659
661 std::int32_t version() const { return _version; }
662
671 GLConfig& setVersion(std::int32_t version) {
672 _version = version;
673 return *this;
674 }
675
677 iVector4 colorBufferSize() const { return _colorBufferSize; }
678
686 _colorBufferSize = size;
687 return *this;
688 }
689
691 std::int32_t depthBufferSize() const { return _depthBufferSize; }
692
699 GLConfig& setDepthBufferSize(std::int32_t size) {
700 _depthBufferSize = size;
701 return *this;
702 }
703
705 std::int32_t stencilBufferSize() const { return _stencilBufferSize; }
706
713 GLConfig& setStencilBufferSize(std::int32_t size) {
714 _stencilBufferSize = size;
715 return *this;
716 }
717
719 std::int32_t sampleCount() const { return _sampleCount; }
720
729 GLConfig& setSampleCount(std::int32_t count) {
730 _sampleCount = count;
731 return *this;
732 }
733
735 bool isSrgbCapable() const { return _srgbCapable; }
736
744 GLConfig& setSrgbCapable(bool enabled) {
745 _srgbCapable = enabled;
746 return *this;
747 }
748
749
750 private:
751 iVector4 _colorBufferSize;
752 std::int32_t _depthBufferSize, _stencilBufferSize;
753 std::int32_t _sampleCount;
754 std::int32_t _version;
755 Flags _flags;
756 bool _srgbCapable;
757 };
758
759 HRESULT initSimConfigFromFile(Simulator::Config &conf);
760
761 CAPI_FUNC(HRESULT) universe_init(const UniverseConfig &conf);
762
763 CAPI_FUNC(HRESULT) modules_init();
764
765
766 namespace io {
767
768 template <>
769 HRESULT toFile(const Simulator &dataElement, const MetaData &metaData, IOElement &fileElement);
770
771 template <>
772 HRESULT fromFile(const IOElement &fileElement, const MetaData &metaData, Simulator::Config *dataElement);
773
774 }
775
776};
777
778#endif // _SOURCE_TFSIMULATOR_H_
OpenGL context configuration.
Definition tfSimulator.h:569
GLConfig & setDepthBufferSize(std::int32_t size)
Set depth buffer size.
Definition tfSimulator.h:699
std::int32_t version() const
Context version.
Definition tfSimulator.h:661
GLConfig & setStencilBufferSize(std::int32_t size)
Set stencil buffer size.
Definition tfSimulator.h:713
std::int32_t sampleCount() const
Sample count.
Definition tfSimulator.h:719
std::int32_t depthBufferSize() const
Depth buffer size.
Definition tfSimulator.h:691
GLConfig & setFlags(Flags flags)
Set context flags.
Definition tfSimulator.h:629
GLConfig & setSampleCount(std::int32_t count)
Set sample count.
Definition tfSimulator.h:729
std::int32_t stencilBufferSize() const
Stencil buffer size.
Definition tfSimulator.h:705
GLConfig & setSrgbCapable(bool enabled)
Set sRGB-capable default framebuffer.
Definition tfSimulator.h:744
GLConfig & setVersion(std::int32_t version)
Set context version.
Definition tfSimulator.h:671
iVector4 colorBufferSize() const
Color buffer size.
Definition tfSimulator.h:677
GLConfig & addFlags(Flags flags)
Add context flags.
Definition tfSimulator.h:642
bool isSrgbCapable() const
sRGB-capable default framebuffer
Definition tfSimulator.h:735
GLConfig & clearFlags(Flags flags)
Clear context flags.
Definition tfSimulator.h:655
Flag
Context flag.
Definition tfSimulator.h:576
uint32_t Flags
Context flags.
Definition tfSimulator.h:613
Flags flags() const
Context flags.
Definition tfSimulator.h:619
GLConfig & setColorBufferSize(const iVector4 &size)
Set color buffer size.
Definition tfSimulator.h:685
HRESULT toFile(const T &dataElement, const MetaData &metaData, IOElement &fileElement)
Convert an object to an intermediate I/O object.
HRESULT fromFile(const IOElement &fileElement, const MetaData &metaData, T *dataElement)
Instantiate an object from an intermediate I/O object.
Include Python header, disable linking to pythonX_d.lib on Windows in debug mode.
Definition tfAngleConfig.h:26
HRESULT setIsTerminalInteractiveShell(const bool &_interactive)
Set whether running interactively.
bool isTerminalInteractiveShell()
Test whether running interactively.
HRESULT Simulator_init(const std::vector< std::string > &argv)
Definition tfSimulator.h:368
unsigned int * seed()
Definition tfSimulator.h:492
void setWindowFlags(uint32_t windowFlags)
Set window flags.
Definition tfSimulator.h:470
void setTitle(std::string title)
Set window title.
Definition tfSimulator.h:387
UniverseConfig universeConfig
Definition tfSimulator.h:535
void clearImportDataFilePath()
Definition tfSimulator.h:521
void setWindowSize(const iVector2 &size)
Set window size.
Definition tfSimulator.h:453
iVector2 windowSize() const
Window size.
Definition tfSimulator.h:393
std::string * importDataFilePath()
Definition tfSimulator.h:511
std::vector< FVector4 > clipPlanes
Definition tfSimulator.h:544
bool hasImportDataFilePath() const
Definition tfSimulator.h:516
fVector2 dpiScaling() const
Custom DPI scaling.
Definition tfSimulator.h:421
Simulator::DpiScalingPolicy dpiScalingPolicy() const
DPI scaling policy.
Definition tfSimulator.h:406
void setSeed(const unsigned int &seed)
Definition tfSimulator.h:497
std::string title() const
Window title.
Definition tfSimulator.h:376
uint32_t windowFlags() const
Window flags.
Definition tfSimulator.h:459
The Simulator is the entry point to simulation, this is the very first object that needs to be initia...
Definition tfSimulator.h:59
static HRESULT waitEvents()
static HRESULT close()
Closes the main window, while the application / simulation continues to run.
static HRESULT postEmptyEvent()
static HRESULT swapInterval(int si)
static HRESULT show()
Shows any windows that were specified in the config. Does not start the universe time propagation unl...
static HRESULT run(FloatP_t et)
Runs the event loop until all windows close or simulation time expires. Automatically performs univer...
static bool throwingExceptions()
Options
Definition tfSimulator.h:140
static const int getNumThreads()
WindowFlags
Window flag.
Definition tfSimulator.h:70
static HRESULT throwExceptions(const bool &_throw)
static HRESULT redraw()
HRESULT makeCurrent()
Make the instance the global simulator object.
static HRESULT waitEventsTimeout(FloatP_t timeout)
static Simulator * get()
static HRESULT pollEvents()
Initialize an #engine with the given data.
Definition tfUniverse.h:243
CUDA runtime control interface for Simulator.
Definition tfSimulatorConfig.h:40
Definition tfGlfwWindow.h:38
int32_t HRESULT
Definition tf_port.h:255