SPH
Controller.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "gui/Settings.h"
4 #include "io/Path.h"
7 #include "run/IRun.h"
8 #include "run/Node.h"
9 #include "system/Settings.h"
10 #include <condition_variable>
11 #include <thread>
12 
13 class wxWindow;
14 class wxSizer;
15 
17 
18 class RunPage;
19 class Storage;
20 class Statistics;
21 class Timer;
22 struct Pixel;
23 class Palette;
24 class JobNode;
25 class Project;
26 class IRenderer;
27 class ICamera;
28 class IColorizer;
29 enum class ColorizerId;
31 
33 enum class RunStatus {
34  RUNNING,
35  PAUSED,
36  STOPPED,
37  QUITTING,
38 };
39 
40 
42 class Controller : public IJobCallbacks {
43 private:
45  RawPtr<RunPage> page;
46 
48  Project& project;
49 
50  struct {
52  std::thread thread;
53 
56 
58 
60 
63 
65  std::atomic_bool shouldContinue;
66 
70  using TimeStepCallback = Function<void(const Storage& storage, const Statistics& stats)>;
72 
73  } sph;
74 
76  struct Vis {
78  Array<Vector> positions;
79 
81  AutoPtr<Statistics> stats;
82 
86  AutoPtr<IRenderer> renderer;
87 
91  SharedPtr<IColorizer> colorizer;
92 
96  AutoPtr<ICamera> camera;
97  mutable std::mutex cameraMutex;
98 
100  Optional<Size> selectedParticle;
101 
106  AutoPtr<wxBitmap> bitmap;
107 
110  std::atomic_bool redrawOnNextTimeStep;
111 
113  mutable std::atomic_bool refreshPending;
114 
116  std::thread renderThread;
117 
119  std::atomic_bool needsRefresh;
120 
122  std::mutex renderThreadMutex;
123  std::condition_variable renderThreadVar;
124 
126  AutoPtr<Timer> timer;
127 
128  Vis();
129 
130  void initialize(const Project& project);
131 
132  bool isInitialized();
133 
134  void refresh();
135  } vis;
136 
138  RunStatus status = RunStatus::STOPPED;
139 
141  std::mutex continueMutex;
142  std::condition_variable continueVar;
143 
144  std::mutex updateMutex;
145  std::condition_variable updateVar;
146 
147 public:
149  Controller(wxWindow* parent);
150 
151  ~Controller();
152 
153  RawPtr<RunPage> getPage() const;
154 
156 
158  bool isRunning() const;
159 
161 
165 
169  const wxBitmap& getRenderedBitmap() const;
170 
173 
176 
182  Optional<Size> getIntersectedParticle(const Pixel position, const float toleranceEps);
183 
185 
186  const Storage& getStorage() const;
187 
190 
191 
193 
200  void update(const Storage& storage, const Statistics& stats);
201 
208  void setColorizer(const SharedPtr<IColorizer>& newColorizer);
209 
216  void setRenderer(AutoPtr<IRenderer>&& newRenderer);
217 
223  void setSelectedParticle(const Optional<Size>& particleIdx);
224 
228  void setPaletteOverride(const Palette palette);
229 
235  bool tryRedraw();
236 
241  void redrawOnNextTimeStep();
242 
248  void refresh(AutoPtr<ICamera>&& camera);
249 
251 
258 
260  void open(const Path& path, const bool sequence = false);
261 
265  void restart();
266 
272  void pause();
273 
280  void stop(const bool waitForFinish = false);
281 
283  RunStatus getStatus() const;
284 
288  void saveState(const Path& path);
289 
291  void quit(const bool waitForFinish = false);
292 
294  void setAutoZoom(const bool enable);
295 
296 private:
297  virtual void onStart(const IJob& job) override;
298 
299  virtual void onEnd(const Storage& storage, const Statistics& stats) override;
300 
301  virtual void onSetUp(const Storage& storage, Statistics& stats) override;
302 
304  virtual void onTimeStep(const Storage& storage, Statistics& stats) override;
305 
307  virtual bool shouldAbortRun() const override;
308 
312  void redraw(const Storage& storage, const Statistics& stats);
313 
314  // main thread call if page exists
315  void safePageCall(Function<void(RunPage*)> func);
316 
317  void startRunThread();
318 
319  void startRenderThread();
320 };
321 
324 
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
ColorizerId
Special colorizers that do not directly correspond to quantities.
Definition: Colorizer.h:136
RunStatus
Status of the code.
Definition: Controller.h:33
@ STOPPED
Run has been stopped by the user.
@ RUNNING
Simulation in progress.
@ QUITTING
quit has been called, waiting for threads to finish
@ PAUSED
Run is paused, can be continued or stopped.
Array< ExtColorizerId > getColorizerIds()
Returns IDs of all colorizers available in the application.
Definition: Controller.cpp:352
Basic interface defining a single run.
#define NAMESPACE_SPH_END
Definition: Object.h:12
Object representing a path on a filesystem, similar to std::filesystem::path in c++17.
Generic dynamically allocated resizable storage.
Definition: Array.h:43
Main GUI class connection the simulation with UI controls.
Definition: Controller.h:42
bool isRunning() const
Returns true if a simulation is running.
Definition: Controller.cpp:348
Controller(wxWindow *parent)
Initialize the controller.
Definition: Controller.cpp:26
void setRenderer(AutoPtr< IRenderer > &&newRenderer)
Sets a new renderer used to draw particles.
Definition: Controller.cpp:520
void setAutoZoom(const bool enable)
Enables or disables auto-zoom during the simulation.
Definition: Controller.cpp:213
RawPtr< RunPage > getPage() const
Definition: Controller.cpp:43
AutoPtr< ICamera > getCurrentCamera() const
Returns the camera currently used for the rendering.
Definition: Controller.cpp:437
std::atomic_bool shouldContinue
Flag read by the simulation; the run is stopped if this is set to zero.
Definition: Controller.h:65
SharedPtr< INode > run
Root node of the simulation.
Definition: Controller.h:55
void quit(const bool waitForFinish=false)
Closes down the model, clears all allocated resources. Must be called only once.
Definition: Controller.cpp:185
Array< SharedPtr< IColorizer > > getColorizerList(const Storage &storage) const
Definition: Controller.cpp:407
void saveState(const Path &path)
Saves the state of the current run to the disk.
Definition: Controller.cpp:150
const wxBitmap & getRenderedBitmap() const
Renders a bitmap of current view.
Definition: Controller.cpp:426
void restart()
Starts the simulation with current setup.
Definition: Controller.cpp:105
SharedPtr< IColorizer > getCurrentColorizer() const
Returns the colorizer currently used for rendering into the window.
Definition: Controller.cpp:432
RunStatus getStatus() const
Returns the current status of the run.
Definition: Controller.cpp:146
Path path
Path to the loaded file, if used.
Definition: Controller.h:62
void open(const Path &path, const bool sequence=false)
Opens a simulation snapshot from given file.
Definition: Controller.cpp:92
void stop(const bool waitForFinish=false)
Stops the current simulation.
Definition: Controller.cpp:132
void redrawOnNextTimeStep()
Redraws the image on the following timestep.
Definition: Controller.cpp:634
void setPaletteOverride(const Palette palette)
Modifies the color palette for current colorizer.
Definition: Controller.cpp:562
Optional< Size > getIntersectedParticle(const Pixel position, const float toleranceEps)
Returns the particle under given image position, or NOTHING if such particle exists.
Definition: Controller.cpp:442
void setSelectedParticle(const Optional< Size > &particleIdx)
Sets a selected particle or changes the current selection.
Definition: Controller.cpp:543
void pause()
Pause the current simulation.
Definition: Controller.cpp:127
void start(SharedPtr< INode > run, const RunSettings &globals)
Sets up and starts a new simulation.
Definition: Controller.cpp:74
void refresh(AutoPtr< ICamera > &&camera)
Re-renders the particles with given camera.
Definition: Controller.cpp:638
Optional< Size > getSelectedParticle() const
Definition: Controller.cpp:570
RunSettings globals
Definition: Controller.h:57
void update(const Storage &storage, const Statistics &stats)
Updates the colorizer list, reset the camera and the renderer, etc.
Definition: Controller.cpp:320
bool tryRedraw()
If possible, redraws the particles with data from storage.
Definition: Controller.cpp:611
RawPtr< const Storage > storage
Definition: Controller.h:59
Locking< Array< TimeStepCallback > > onTimeStepCallbacks
Definition: Controller.h:71
GuiSettings & getParams()
Returns the settings object.
Definition: Controller.cpp:316
void setColorizer(const SharedPtr< IColorizer > &newColorizer)
Sets a new colorizer to be displayed.
Definition: Controller.cpp:509
std::thread thread
Thread running the simulation.
Definition: Controller.h:52
const Storage & getStorage() const
Definition: Controller.cpp:574
Helper type allowing to "derive" from enum class.
Definition: ExtendedEnum.h:24
Interface defining a camera or view, used by a renderer.
Definition: Camera.h:62
Interface for objects assigning colors to particles.
Definition: Colorizer.h:34
Interface used during job evaluation.
Definition: Node.h:25
Base class for all object performing an operation in a simulation hierarchy.
Definition: Job.h:96
Interface used to implement renderers.
Definition: IRenderer.h:166
Building block of a simulation hierarchy.
Definition: Node.h:88
Wraps given object together with a mutex, locking it every time the object is accessed.
Definition: Locking.h:12
Represents a color palette, used for mapping arbitrary number to a color.
Definition: Palette.h:25
Object representing a path on a filesystem.
Definition: Path.h:17
Main frame of the application.
Definition: RunPage.h:44
Object holding various statistics about current run.
Definition: Statistics.h:22
Container storing all quantities used within the simulations.
Definition: Storage.h:230
Basic time-measuring tool. Starts automatically when constructed.
Definition: Timer.h:27
Generic storage and input/output routines of settings.
Vector position(const Float a, const Float e, const Float u)
Computes the position on the elliptic trajectory.
Definition: TwoBody.cpp:82
Definition: Point.h:101