AGX Dynamics  2.37.2.0
ImageCapture.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGXOSG_IMAGECAPTURE_H
18 #define AGXOSG_IMAGECAPTURE_H
19 
20 #include <queue>
21 #include <atomic>
22 
23 #include <agx/PushDisableWarnings.h> // Disabling warnings. Include agx/PopDisableWarnings.h below!
24 #include <osg/Camera>
25 #include <osg/observer_ptr>
26 #include <agx/PopDisableWarnings.h> // End of disabled warnings.
27 
29 
30 #include <agxSDK/Simulation.h>
31 #include <agx/Timer.h>
32 #include <agx/Thread.h>
33 #include <agx/Referenced.h>
34 #include <agxOSG/export.h>
35 
36 
37 
38 namespace agxOSG {
39 
42  {
43  public:
44  class DrawCallback;
45 
46  public:
47  ImageCaptureBase(agxSDK::Simulation *sim = nullptr);
48 
52  void setFps(agx::Real fps);
53 
57  agx::Real getFps();
58 
62  void setMaxImages(int i);
63 
65  virtual void startCapture( int startImageNumber=0 );
66 
68  virtual bool stopCapture();
69 
71  bool getCaptureStarted() const;
72 
73  agxSDK::Simulation *getSimulation();
74  void setSimulation(agxSDK::Simulation *simulation);
75 
76  void setImageResolution(agx::UInt width, agx::UInt height);
77  agx::Vec2u getImageResolution() const;
78 
79 
80  int getImageNum() const;
81 
82  DrawCallback *getDrawCallback();
83 
84  void setEnableSyncWithSimulation( bool enableSync );
85  bool getEnableSyncWithSimulation( ) const;
86 
87  void resetTime();
88 
89  virtual void apply( osg::RenderInfo& renderInfo );
90 
91  // Called in separate consumer thread
92  virtual void writeImage(osg::Image *image, agx::Index index) = 0;
93 
94  protected:
95  virtual ~ImageCaptureBase();
96  void setCaptureStarted(bool f);
97  void shutdown();
98 
99  private:
100  class ImageVector;
102 
103  typedef std::queue<std::pair<osg::ref_ptr<osg::Image>, agx::Index> > ImageVectorT;
104 
105  private:
106  agxSDK::SimulationObserver m_simulation;
107 
108  agx::UInt m_width;
109  agx::UInt m_height;
110 
111  mutable agx::ReentrantMutex m_mutex;
112  bool m_captureEnabled;
113  int m_maxImages;
114  int m_imageNumber;
115  agx::Timer m_timer;
116  agx::Real m_lastTime;
117  bool m_first;
118  float m_fps;
119  bool m_sync;
120  osg::ref_ptr<DrawCallback> m_drawCallback;
121 
122  WriteImageThreadRef m_writeImageThread;
123  };
124 
130  {
131  public:
132 
133  ImageCapture(agxSDK::Simulation *sim = nullptr);
134 
135 
136  std::string getPostfix() const
137  {
138  return m_postfix;
139  }
140 
146  void setPostfix(const std::string& postfix)
147  {
148  m_postfix = postfix;
149  }
150 
151  std::string getPrefix() const
152  {
153  return m_prefix;
154  }
155 
156  void setPrefix(const std::string& prefix)
157  {
158  m_prefix = prefix;
159  }
160 
161  std::string getDirectoryPath() const
162  {
163  return m_directory;
164  }
165 
166  void setDirectoryPath(const std::string& path)
167  {
168  m_directory = path;
169  }
170 
171  virtual void writeImage(osg::Image *image, agx::Index index);
172 
173  protected:
174 
175  virtual ~ImageCapture();
176 
177  private:
178  std::string m_prefix;
179  std::string m_postfix;
180  std::string m_directory;
181  };
182 
183 
184  class ImageCaptureBase::ImageVector : public ImageCaptureBase::ImageVectorT
185  {
186  public:
188 
189  void push(ImageVectorT::value_type image_set)
190  {
192  ImageVectorT::push(image_set);
193  m_block.release();
194  }
195 
196  size_t size() const
197  {
199  return ImageVectorT::size();
200  }
201 
202  void clear()
203  {
205  while(ImageVectorT::size())
206  ImageVectorT::pop();
207  }
208 
209  void lock()
210  {
211  m_mutex.lock();
212  }
213 
214  void unlock()
215  {
216  m_mutex.unlock();
217  }
218 
219  void block()
220  {
221  m_block.block();
222  m_block.reset();
223  }
224 
225  void release()
226  {
227  m_block.release();
228  }
229 
230  private:
231  agx::Block m_block;
232  mutable agx::ReentrantMutex m_mutex;
233  };
234 
235 
236 
238  {
239  public:
241  virtual ~WriteImageThread();
242 
243  void push(ImageVectorT::value_type image_set);
244 
245  virtual void run();
246  void stop();
247 
248  private:
249  ImageVectorT::value_type topAndPop();
250 
251  private:
252  ImageCaptureBase *m_imageCapture;
253  ImageVector m_queue;
254  std::atomic<bool> m_stop;
255  agx::Block m_joinBlock;
256  };
257 
258 
259  class AGXOSG_EXPORT ImageCaptureBase::DrawCallback : public osg::Camera::DrawCallback
260  {
261  public:
262  DrawCallback(ImageCaptureBase* capture);
263 
264  virtual void operator () (osg::RenderInfo& renderInfo) const;
265  using osg::Camera::DrawCallback::operator();
266  ImageCaptureBase* getImageCapture();
267  void setImageCapture( ImageCaptureBase* ic );
268 
269  private:
270  ImageCaptureBase *m_imageCapture;
271  };
272 
273 }
274 
275 #endif
Basic wrapper class aroud std::thread.
Definition: Thread.h:82
std::string getPrefix() const
Definition: ImageCapture.h:151
void AGXPHYSICS_EXPORT shutdown()
Shutdown of the AGX Dynamics API will be done when the number of shutdown matches the number of calls...
#define AGXOSG_EXPORT
void setPrefix(const std::string &prefix)
Definition: ImageCapture.h:156
std::string getPostfix() const
Definition: ImageCapture.h:136
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
The Timer class permits timing execution speed with the same refinement as the built in hardware cloc...
Definition: Timer.h:74
Block synchronization primitive.
Base class providing referencing counted objects.
Definition: Referenced.h:119
void setPostfix(const std::string &postfix)
Sets the postfix.
Definition: ImageCapture.h:146
uint32_t UInt
Definition: Integer.h:34
void push(ImageVectorT::value_type image_set)
Definition: ImageCapture.h:189
std::lock_guard< T > ScopeLock
agx::ScopeLock is an alias for std::lock_guard
Definition: ScopeLock.h:31
The agxOSG namespace provides functionality for visualizing AGX simulations with OpenSceneGraph.
UInt32 Index
Definition: Integer.h:52
std::string getDirectoryPath() const
Definition: ImageCapture.h:161
std::recursive_mutex ReentrantMutex
Definition: Object.h:48
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:126
double Real
Definition: Real.h:42
void setDirectoryPath(const std::string &path)
Definition: ImageCapture.h:166