File: SnapshotEngine.cpp

package info (click to toggle)
yade 2026.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,448 kB
  • sloc: cpp: 97,645; python: 52,173; sh: 677; makefile: 162
file content (55 lines) | stat: -rw-r--r-- 1,907 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifdef YADE_OPENGL

#include "SnapshotEngine.hpp"

namespace yade { // Cannot have #include directive inside.

YADE_PLUGIN((SnapshotEngine));
CREATE_LOGGER(SnapshotEngine);

void SnapshotEngine::action()
{
	if (!OpenGLManager::self) throw logic_error("No OpenGLManager instance?!");
	if (OpenGLManager::self->views.size() == 0) {
		int viewNo = OpenGLManager::self->waitForNewView(static_cast<double>(deadTimeout));
		if (viewNo < 0) {
			if (!ignoreErrors) throw runtime_error("SnapshotEngine: Timeout waiting for new 3d view.");
			else {
				LOG_WARN("Making myself Engine::dead, as I can not live without a 3d view (timeout).");
				dead = true;
				return;
			}
		}
	}
	const shared_ptr<GLViewer>& glv = OpenGLManager::self->views[0];
	std::ostringstream          fss;
	fss << fileBase << setw(5) << setfill('0') << counter++ << "." << boost::algorithm::to_lower_copy(format);
	LOG_DEBUG("GL view → " << fss.str())
	glv->setSnapshotFormat(QString(format.c_str()));
	glv->nextFrameSnapshotFilename = fss.str();
	// wait for the renderer to save the frame (will happen at next postDraw)
	timespec t1, t2;
	t1.tv_sec    = 0;
	t1.tv_nsec   = 10000000; /* 10 ms */
	long waiting = 0;
	while (!glv->nextFrameSnapshotFilename.empty()) {
		nanosleep(&t1, &t2);
		waiting++;
		if (((waiting) % 1000) == 0) LOG_WARN("Already waiting " << waiting / 100 << "s for snapshot to be saved. Something went wrong?");
		if (waiting / 100. > deadTimeout) {
			if (ignoreErrors) {
				LOG_WARN("Timeout waiting for snapshot to be saved, making byself Engine::dead");
				dead = true;
				return;
			} else
				throw runtime_error("SnapshotEngine: Timeout waiting for snapshot to be saved.");
		}
	}
	snapshots.push_back(fss.str());
	usleep((long)(msecSleep * 1000));
	//if(!plot.empty()){ pyRunString("import yade.plot; yade.plot.addImgData("+plot+"='"+fss.str()+"')"); }
}

} // namespace yade

#endif /* YADE_OPENGL */