SimpleFastOpenAtomicVisualiser
Loading...
Searching...
No Matches
visualisationState.h
Go to the documentation of this file.
1#ifndef VISUALISATIONSTATE_H
2#define VISUALISATIONSTATE_H
3
4#include <vector>
5#include <map>
6#include <cstdint>
7#include <string>
8#include <algorithm>
9
10#include <GLFW/glfw3.h>
11#include <lua.h>
12
13#include <colour.h>
14#include <bond.h>
15#include <atom.h>
16#include <element.h>
17#include <LuaNumber.h>
18#include <util.h>
19#include <commandLine.h>
20
21#include <record.h>
22
23#ifdef WITH_FFMPEG
24 #include <ffmpegRecord.h>
25#else
26 #include <jompegRecord.h>
27#endif
28
34{
45 (
46 std::vector<Atom> & atoms,
47 const std::filesystem::path & atomColours,
48 uint64_t bondFocus,
49 float bondCutoff,
50 bool sizeByMass,
51 const std::map<int, std::string> & keyCodes
52 )
53 : atoms(atoms)
54 {
55 std::set<Element> elements = uniqueElements(atoms);
57
58 atomEmphasisOverrides = std::vector<float>(atoms.size(), 1.0f);
59
60 for (uint8_t i = 0; i < std::min(size_t(6), elements.size()); i++)
61 {
62 Element e = *std::next(elements.begin(), i);
64 std::cout << "Element " << e << " emphasis bound to key " << keyCodes.at(GLFW_KEY_1+i) << "\n";
65 }
66
67 if (!atomColours.empty())
68 {
70 }
71
73
74 if (bondFocus < atoms.size())
75 {
76 bondsFor = {bondFocus};
77 }
78 else
79 {
80 bondsFor.resize(atoms.size());
81 std::iota(bondsFor.begin(), bondsFor.end(), 0);
82 }
83
84 if (bondCutoff > 0.0)
85 {
87 (
89 atoms,
90 bondCutoff
91 );
92 }
93 atomCount = atoms.size();
94
95 atomSizes.resize(atoms.size());
96 if (!sizeByMass)
97 {
98 for (const auto & ei : elementMap)
99 {
100 atomSizes[ei.second] = ELEMENT_RADIUS.at(ei.first);
101 }
102 }
103 else
104 {
105 for (const auto & ei : elementMap)
106 {
107 atomSizes[ei.second] = ELEMENT_MASS.at(ei.first);
108 }
109 }
111
112 text = "";
113 frame = 0;
114 }
115
116 std::vector<Atom> & atoms;
117 std::map<uint64_t, std::set<uint64_t>> bonds;
118 std::vector<uint64_t> bondsFor;
119 std::vector<float> atomEmphasisOverrides;
120 std::map<uint64_t, glm::vec4> atomColourOverrides;
121 std::vector<float> atomSizes;
122 std::multimap<Element, uint64_t> elementMap;
123 std::map<int, Element> emphasisControls;
124 std::string text;
126
128
129 std::unique_ptr<Record> record = nullptr;
130
131 bool recording = false;
132 bool recordClosing = false;
133
140 bool recordWaiting() const { return waitingForRecord; }
141
147 void toggleRecord(const CommandLine & options)
148 {
149 if (!recording)
150 {
151 std::string name = timeStamp()+std::string(".mp4");
152 #ifdef WITH_FFMPEG
153 record = std::make_unique<FFmpegRecord>
154 (
155 name,
156 options.resolution.value,
157 60,
158 options.preset.value,
159 options.profile.value,
160 options.crf.value,
161 options.cq.value,
162 options.qp.value,
163 options.codec.value,
164 options.maxBFrames.value,
165 options.gopSize.value
166 );
167 std::cout << "FFmpeg ";
168 #else
169 record = std::make_unique<JompegRecord>
170 (
171 name,
172 options.resolution.value,
173 60
174 );
175 std::cout << "jo_mpeg ";
176 #endif
177 std::cout << "recording to " + name + "\n";
178 record->open();
179 recording = true;
180 }
181 else if (recording)
182 {
183 if (record->finalise())
184 {
185 record.reset();
186 recording = false;
187 }
188 else
189 {
190 recordClosing = true;
191 }
192 }
193 }
194
202 (
203 uint32_t resX,
204 uint32_t resY
205 )
206 {
207 if (recordClosing || record == nullptr || (!record->isOpen()))
208 {
209 return;
210 }
211
212 if (!waitingForRecord)
213 {
214
215 std::vector<uint8_t> pixels(resX*resY*4, 0);
217 (
218 0,
219 0,
220 resX,
221 resY,
222 GL_RGBA,
224 pixels.data()
225 );
226
227 for(int j = 0; j < int(resY/2); j++)
228 {
229 std::swap_ranges
230 (
231 pixels.begin()+4*resX*j,
232 pixels.begin()+4*resX*(j+1),
233 pixels.begin()+4*resX*(resY-j-1)
234 );
235 }
236
237 record->queueFrame(pixels);
238 }
239
240 if (record->queueSize() >= 32)
241 {
242 record->writeFrames();
243 }
244
245 if (record->framesLeft() >= 64)
246 {
247 waitingForRecord = true;
248 }
249 else
250 {
251 waitingForRecord = false;
252 }
253 }
254
268 inline int lua_setAtomColour(lua_State * lua);
269
278 inline int lua_getAtomColour(lua_State * lua);
279
289 inline int lua_bond(lua_State * lua);
290
300 inline int lua_unbond(lua_State * lua);
301
310 inline int lua_getAtomsBonds(lua_State * lua);
311
318 inline int lua_atomCount(lua_State * lua);
319
330 inline int lua_getAtomsNeighbours(lua_State * lua);
331
340 inline int lua_getAtom(lua_State * lua);
341
350 inline int lua_setText(lua_State * lua);
351
358 inline int lua_getFrame(lua_State * lua);
359
360private:
361
362 bool waitingForRecord = false;
363
364};
365
366#endif /* VISUALISATIONSTATE_H */
367
void applySizes(std::vector< Atom > &atoms, const std::vector< float > sizes)
Apply sizes by index.
Definition atom.h:283
void applyColours(std::vector< Atom > &atoms, const std::map< uint64_t, glm::vec4 > &colours)
Apply colours by index.
Definition atom.h:265
std::multimap< Element, uint64_t > elementIndices(const std::vector< Atom > &atoms)
Obtain indices of each element.
Definition atom.h:248
std::set< Element > uniqueElements(const std::vector< Atom > &atoms)
Determine the unique elements in a list of Atom.
Definition atom.h:216
std::map< uint64_t, std::set< uint64_t > > determineBonds(std::vector< uint64_t > forAtoms, std::vector< Atom > &atoms, float cutOff)
Obtain bonds based on a fixed distance cutOff using Neighbours.
Definition bond.h:20
std::map< uint64_t, glm::vec4 > atomColoursFromFile(std::filesystem::path path)
Read an atom index colour map from a file.
Definition colour.h:197
glm::vec< L, float, glm::qualifier::highp > vec
Definition commandLine.h:214
Element
Representable elements.
Definition element.h:13
const std::map< Element, float > ELEMENT_MASS
Scaled element masses.
Definition element.h:564
const std::map< Element, float > ELEMENT_RADIUS
Map Element to a Van der Waals radius in Angstroms.
Definition element.h:461
T value
Definition commandLine.h:33
Extract command line arguments.
Definition commandLine.h:240
Argument< vec< 2 > > resolution
Definition commandLine.h:348
Holds editable data for the visualisation state.
Definition visualisationState.h:34
bool recording
Definition visualisationState.h:131
std::vector< uint64_t > bondsFor
Definition visualisationState.h:118
bool recordWaiting() const
Video writing is behind.
Definition visualisationState.h:140
std::vector< float > atomSizes
Definition visualisationState.h:121
void recordFrame(uint32_t resX, uint32_t resY)
If recording, obtain the pixels for the current frame and submit for recording.
Definition visualisationState.h:202
std::map< uint64_t, glm::vec4 > atomColourOverrides
Definition visualisationState.h:120
std::multimap< Element, uint64_t > elementMap
Definition visualisationState.h:122
int lua_atomCount(lua_State *lua)
Lua binding to get the Atom count.
Definition atoms.h:114
std::unique_ptr< Record > record
Definition visualisationState.h:129
int lua_setText(lua_State *lua)
Set the on screen text.
Definition utils.h:12
std::map< int, Element > emphasisControls
Definition visualisationState.h:123
uint64_t frame
Definition visualisationState.h:125
std::vector< Atom > & atoms
Definition visualisationState.h:116
int lua_getAtom(lua_State *lua)
Lua binding to get an Atom.
Definition atoms.h:186
std::map< uint64_t, std::set< uint64_t > > bonds
Definition visualisationState.h:117
int lua_setAtomColour(lua_State *lua)
Lua binding to set an Atom's colour by index.
Definition atoms.h:24
std::vector< float > atomEmphasisOverrides
Definition visualisationState.h:119
bool recordClosing
Definition visualisationState.h:132
void toggleRecord(const CommandLine &options)
Toggle recording to video.
Definition visualisationState.h:147
uint64_t atomCount
Definition visualisationState.h:127
int lua_unbond(lua_State *lua)
Lua binding to unbond 2 Atoms.
Definition bonds.h:56
int lua_bond(lua_State *lua)
Lua binding to bond 2 Atoms.
Definition bonds.h:13
int lua_getAtomsNeighbours(lua_State *lua)
Lua binding to get the neighbours of an Atom to a cutoff.
Definition atoms.h:130
int lua_getAtomsBonds(lua_State *lua)
Lua binding to get the bonds of an Atom.
Definition atoms.h:263
int lua_getAtomColour(lua_State *lua)
Lua binding to get a Atom's colour by index.
Definition atoms.h:71
int lua_getFrame(lua_State *lua)
Get the current frame number.
Definition utils.h:36
VisualisationState(std::vector< Atom > &atoms, const std::filesystem::path &atomColours, uint64_t bondFocus, float bondCutoff, bool sizeByMass, const std::map< int, std::string > &keyCodes)
Construct a VisualisationState from a some Atoms.
Definition visualisationState.h:45
std::string text
Definition visualisationState.h:124
const std::map< int, std::string > keyCodes
String name for a GLFW_KEY index.
Definition util.h:260
std::string timeStamp()
Current timestamp.
Definition util.h:179