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 elementsUpdated = true;
115 }
116
117 std::vector<Atom> & atoms;
118 std::map<uint64_t, std::set<uint64_t>> bonds;
119 std::vector<uint64_t> bondsFor;
120 std::vector<float> atomEmphasisOverrides;
121 std::map<uint64_t, glm::vec4> atomColourOverrides;
122 std::vector<float> atomSizes;
123 std::multimap<Element, uint64_t> elementMap;
124 std::map<int, Element> emphasisControls;
125 std::string text;
127
129
130 std::unique_ptr<Record> record = nullptr;
131
132 bool recording = false;
133 bool recordClosing = false;
134
135 bool elementsUpdated = false;
136
143 bool recordWaiting() const { return waitingForRecord; }
144
150 void toggleRecord(const CommandLine & options)
151 {
152 if (!recording)
153 {
154 std::string name = options.videoName.value;
155 if (name == "") { name = timeStamp()+std::string(".mp4"); }
156 #ifdef WITH_FFMPEG
157 record = std::make_unique<FFmpegRecord>
158 (
159 name,
160 options.resolution.value,
161 60,
162 options.preset.value,
163 options.profile.value,
164 options.crf.value,
165 options.cq.value,
166 options.qp.value,
167 options.codec.value,
168 options.maxBFrames.value,
169 options.gopSize.value
170 );
171 std::cout << "FFmpeg ";
172 #else
173 record = std::make_unique<JompegRecord>
174 (
175 name,
176 options.resolution.value,
177 60
178 );
179 std::cout << "jo_mpeg ";
180 #endif
181 std::cout << "recording to " + name + "\n";
182 record->open();
183 recording = true;
184 }
185 else if (recording)
186 {
187 if (record->finalise())
188 {
189 record.reset();
190 recording = false;
191 }
192 else
193 {
194 recordClosing = true;
195 }
196 }
197 }
198
206 (
207 uint32_t resX,
208 uint32_t resY
209 )
210 {
211 if (recordClosing || record == nullptr || (!record->isOpen()))
212 {
213 return;
214 }
215
216 if (!waitingForRecord)
217 {
218
219 std::vector<uint8_t> pixels(resX*resY*4, 0);
221 (
222 0,
223 0,
224 resX,
225 resY,
226 GL_RGBA,
228 pixels.data()
229 );
230
231 for(int j = 0; j < int(resY/2); j++)
232 {
233 std::swap_ranges
234 (
235 pixels.begin()+4*resX*j,
236 pixels.begin()+4*resX*(j+1),
237 pixels.begin()+4*resX*(resY-j-1)
238 );
239 }
240
241 record->queueFrame(pixels);
242 }
243
244 if (record->queueSize() >= 32)
245 {
246 record->writeFrames();
247 }
248
249 if (record->framesLeft() >= 64)
250 {
251 waitingForRecord = true;
252 }
253 else
254 {
255 waitingForRecord = false;
256 }
257 }
258
272 inline int lua_setAtomColour(lua_State * lua);
273
282 inline int lua_getAtomColour(lua_State * lua);
283
293 inline int lua_bond(lua_State * lua);
294
304 inline int lua_unbond(lua_State * lua);
305
314 inline int lua_getAtomsBonds(lua_State * lua);
315
322 inline int lua_atomCount(lua_State * lua);
323
334 inline int lua_getAtomsNeighbours(lua_State * lua);
335
344 inline int lua_getAtom(lua_State * lua);
345
354 inline int lua_setText(lua_State * lua);
355
362 inline int lua_getFrame(lua_State * lua);
363
364private:
365
366 bool waitingForRecord = false;
367
368};
369
370#endif /* VISUALISATIONSTATE_H */
371
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:356
Argument< std::string > videoName
Definition commandLine.h:355
Holds editable data for the visualisation state.
Definition visualisationState.h:34
bool recording
Definition visualisationState.h:132
std::vector< uint64_t > bondsFor
Definition visualisationState.h:119
bool recordWaiting() const
Video writing is behind.
Definition visualisationState.h:143
std::vector< float > atomSizes
Definition visualisationState.h:122
void recordFrame(uint32_t resX, uint32_t resY)
If recording, obtain the pixels for the current frame and submit for recording.
Definition visualisationState.h:206
std::map< uint64_t, glm::vec4 > atomColourOverrides
Definition visualisationState.h:121
std::multimap< Element, uint64_t > elementMap
Definition visualisationState.h:123
int lua_atomCount(lua_State *lua)
Lua binding to get the Atom count.
Definition atoms.h:125
std::unique_ptr< Record > record
Definition visualisationState.h:130
int lua_setText(lua_State *lua)
Set the on screen text.
Definition utils.h:12
std::map< int, Element > emphasisControls
Definition visualisationState.h:124
uint64_t frame
Definition visualisationState.h:126
std::vector< Atom > & atoms
Definition visualisationState.h:117
int lua_getAtom(lua_State *lua)
Lua binding to get an Atom.
Definition atoms.h:207
bool elementsUpdated
Definition visualisationState.h:135
std::map< uint64_t, std::set< uint64_t > > bonds
Definition visualisationState.h:118
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:120
bool recordClosing
Definition visualisationState.h:133
void toggleRecord(const CommandLine &options)
Toggle recording to video.
Definition visualisationState.h:150
uint64_t atomCount
Definition visualisationState.h:128
int lua_unbond(lua_State *lua)
Lua binding to unbond 2 Atoms.
Definition bonds.h:62
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:146
int lua_getAtomsBonds(lua_State *lua)
Lua binding to get the bonds of an Atom.
Definition atoms.h:289
int lua_getAtomColour(lua_State *lua)
Lua binding to get a Atom's colour by index.
Definition atoms.h:77
int lua_getFrame(lua_State *lua)
Get the current frame number.
Definition utils.h:41
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:125
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