AGX Dynamics 2.42.1.1
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#pragma once
17
18#include <queue>
19#include <atomic>
20
21#include <agx/PushDisableWarnings.h> // Disabling warnings. Include agx/PopDisableWarnings.h below!
22#include <osg/Camera>
23#include <osg/observer_ptr>
24#include <agx/PopDisableWarnings.h> // End of disabled warnings.
25
27
28#include <agxSDK/Simulation.h>
29#include <agx/Timer.h>
30#include <agx/Thread.h>
31#include <agx/Referenced.h>
32#include <agxOSG/export.h>
34
35
36namespace agxOSG {
37
40 {
41 public:
42 class DrawCallback;
43
44 public:
46
50 void setFps(agx::Real fps);
51
56
60 void setMaxImages(int i);
61
63 virtual void startCapture( int startImageNumber=0 );
64
66 virtual bool stopCapture();
67
69 bool getCaptureStarted() const;
70
73
76
77
78 int getImageNum() const;
79
80 DrawCallback *getDrawCallback();
81
82 void setEnableSyncWithSimulation( bool enableSync );
84
85 void resetTime();
86
87 virtual void apply( osg::RenderInfo& renderInfo );
88
89 // Called in separate consumer thread
90 virtual void writeImage(osg::Image *image, agx::Index index) = 0;
91
92 protected:
94 void setCaptureStarted(bool f);
95 void shutdown();
96
97 private:
98 class ImageVector;
100
101 typedef std::queue<std::pair<osg::ref_ptr<osg::Image>, agx::Index> > ImageVectorT;
102
103 private:
104 agxSDK::SimulationObserver m_simulation;
105
106 agx::UInt m_width;
107 agx::UInt m_height;
108
109 mutable agx::ReentrantMutex m_mutex;
110 bool m_captureEnabled;
111 int m_maxImages;
112 int m_imageNumber;
113 agx::Timer m_timer;
114 bool m_first;
115 bool m_sync;
116 osg::ref_ptr<DrawCallback> m_drawCallback;
117
118 WriteImageThreadRef m_writeImageThread;
119 agxSDK::GraphicsThrottler m_captureThrottler;
120 };
121
127 {
128 public:
129
131
132
133 std::string getPostfix() const
134 {
135 return m_postfix;
136 }
137
143 void setPostfix(const std::string& postfix)
144 {
145 m_postfix = postfix;
146 }
147
148 std::string getPrefix() const
149 {
150 return m_prefix;
151 }
152
153 void setPrefix(const std::string& prefix)
154 {
155 m_prefix = prefix;
156 }
157
158 std::string getDirectoryPath() const
159 {
160 return m_directory;
161 }
162
163 void setDirectoryPath(const std::string& path)
164 {
165 m_directory = path;
166 }
167
168 virtual void writeImage(osg::Image *image, agx::Index index);
169
170 protected:
171
172 virtual ~ImageCapture();
173
174 private:
175 std::string m_prefix;
176 std::string m_postfix;
177 std::string m_directory;
178 };
179
180
181 class ImageCaptureBase::ImageVector : public ImageCaptureBase::ImageVectorT
182 {
183 public:
185
186 void push(ImageVectorT::value_type image_set)
187 {
189 ImageVectorT::push(image_set);
190 m_block.release();
191 }
192
193 size_t size() const
194 {
196 return ImageVectorT::size();
197 }
198
199 void clear()
200 {
202 while(ImageVectorT::size())
203 ImageVectorT::pop();
204 }
205
206 void lock()
207 {
208 m_mutex.lock();
209 }
210
211 void unlock()
212 {
213 m_mutex.unlock();
214 }
215
216 void block()
217 {
218 m_block.block();
219 m_block.reset();
220 }
221
222 void release()
223 {
224 m_block.release();
225 }
226
227 private:
228 agx::Block m_block;
229 mutable agx::ReentrantMutex m_mutex;
230 };
231
232
233
235 {
236 public:
239
240 void push(ImageVectorT::value_type image_set);
241
242 virtual void run();
243 void stop();
244
245 private:
246 ImageVectorT::value_type topAndPop();
247
248 private:
249 ImageCaptureBase *m_imageCapture;
250 ImageVector m_queue;
251 std::atomic<bool> m_stop;
252 agx::Block m_joinBlock;
253 };
254
255
256 class AGXOSG_EXPORT ImageCaptureBase::DrawCallback : public osg::Camera::DrawCallback
257 {
258 public:
260
261 virtual void operator () (osg::RenderInfo& renderInfo) const;
262 using osg::Camera::DrawCallback::operator();
265
266 private:
267 ImageCaptureBase *m_imageCapture;
268 };
269
270}
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
#define AGXOSG_EXPORT
void push(ImageVectorT::value_type image_set)
Definition: ImageCapture.h:186
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:143
void setPrefix(const std::string &prefix)
Definition: ImageCapture.h:153
std::string getPrefix() const
Definition: ImageCapture.h:148
void setDirectoryPath(const std::string &path)
Definition: ImageCapture.h:163
std::string getDirectoryPath() const
Definition: ImageCapture.h:158
virtual void writeImage(osg::Image *image, agx::Index index)
std::string getPostfix() const
Definition: ImageCapture.h:133
Class to throttle a task to a specific frequency (fps).
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:131
Basic wrapper class aroud std::thread.
Definition: Thread.h:83
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:41
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