AGX Dynamics 2.40.1.2
Loading...
Searching...
No Matches
ImageCapture.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx 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
38namespace agxOSG {
39
42 {
43 public:
44 class DrawCallback;
45
46 public:
48
52 void setFps(agx::Real fps);
53
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
75
78
79
80 int getImageNum() const;
81
82 DrawCallback *getDrawCallback();
83
84 void setEnableSyncWithSimulation( bool enableSync );
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:
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
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:
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:
263
264 virtual void operator () (osg::RenderInfo& renderInfo) const;
265 using osg::Camera::DrawCallback::operator();
268
269 private:
270 ImageCaptureBase *m_imageCapture;
271 };
272
273}
274
275#endif
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
#define AGXOSG_EXPORT
void push(ImageVectorT::value_type image_set)
Definition: ImageCapture.h:189
virtual void run()
This method is invoked by start.
void push(ImageVectorT::value_type image_set)
agxSDK::Simulation * getSimulation()
virtual bool stopCapture()
Return false if stopping an already stopped capture process.
virtual void writeImage(osg::Image *image, agx::Index index)=0
agx::Vec2u getImageResolution() const
bool getEnableSyncWithSimulation() const
void setFps(agx::Real fps)
Set the desired frame rate (rate of capture).
DrawCallback(ImageCaptureBase *capture)
ImageCaptureBase * getImageCapture()
void setImageCapture(ImageCaptureBase *ic)
virtual void apply(osg::RenderInfo &renderInfo)
virtual void startCapture(int startImageNumber=0)
Enable/disable capturing.
DrawCallback * getDrawCallback()
agx::Real getFps()
Return the specified desired frame rate for capture.
void setEnableSyncWithSimulation(bool enableSync)
void setCaptureStarted(bool f)
void setSimulation(agxSDK::Simulation *simulation)
ImageCaptureBase(agxSDK::Simulation *sim=nullptr)
void setImageResolution(agx::UInt width, agx::UInt height)
void setMaxImages(int i)
Set a limit of the maximum number of images that should be captured Set -1 for no limit.
bool getCaptureStarted() const
Return whether the capturing is enabled or not.
ImageCapture(agxSDK::Simulation *sim=nullptr)
void setPostfix(const std::string &postfix)
Sets the postfix.
Definition: ImageCapture.h:146
void setPrefix(const std::string &prefix)
Definition: ImageCapture.h:156
std::string getPrefix() const
Definition: ImageCapture.h:151
void setDirectoryPath(const std::string &path)
Definition: ImageCapture.h:166
std::string getDirectoryPath() const
Definition: ImageCapture.h:161
virtual void writeImage(osg::Image *image, agx::Index index)
std::string getPostfix() const
Definition: ImageCapture.h:136
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:130
Basic wrapper class aroud std::thread.
Definition: Thread.h:84
Block synchronization primitive.
void reset()
Reset the block.
void release()
Release the block.
void block()
Block until released from another thread.
Base class providing referencing counted objects.
Definition: Referenced.h:120
The Timer class permits timing execution speed with the same refinement as the built in hardware cloc...
Definition: Timer.h:62
The agxOSG namespace provides functionality for visualizing AGX simulations with OpenSceneGraph.
uint64_t UInt
Definition: Integer.h:27
double Real
Definition: Real.h:42
std::lock_guard< T > ScopeLock
agx::ScopeLock is an alias for std::lock_guard
Definition: ScopeLock.h:31
std::recursive_mutex ReentrantMutex
Definition: Object.h:48
UInt32 Index
Definition: Integer.h:44