SimpleFastOpenAtomicVisualiser
Loading...
Searching...
No Matches
atoms.h
Go to the documentation of this file.
1#ifndef ATOMS_H
2#define ATOMS_H
3
4#include <string>
5
6#include <atom.h>
7#include <element.h>
9#include <LuaNumber.h>
10
25{
26 int args = lua_gettop(lua);
27 if (lua_gettop(lua) >= 1 && lua_isstring(lua, 1) && std::string(lua_tostring(lua, 1)) == "help")
28 {
29 lua_writestring("setAtomColour:\n Arguments:\n i [number]\n r [number]\n g [nummber]\n b [number]\n a [number]\n Set atom i's colour.\n");
30 return 0;
31 };
33 {
34 const std::string msg = "setAtomColour expects an atom index and RGB or RGBA arguments.\n";
35 lua_pushlstring(lua, msg.c_str(), msg.length());
36 return lua_error(lua);
37 }
39 uint64_t index;
40 float r, g, b, a;
41 a = 1.0f;
42
43 lua_index.read(lua, 1);
44 index = uint64_t(lua_index.n);
45
46 if (index >= atomCount)
47 {
48 const std::string msg = "setAtomColour atom index larger than atom count.\n";
49 lua_pushlstring(lua, msg.c_str(), msg.length());
50 return lua_error(lua);
51 }
52 lua_r.read(lua, 2);
53 r = std::clamp(float(lua_r.n), 0.0f, 1.0f);
54 lua_g.read(lua, 3);
55 g = std::clamp(float(lua_g.n), 0.0f, 1.0f);
56 lua_b.read(lua, 4);
57 b = std::clamp(float(lua_b.n), 0.0f, 1.0f);
58 if (args == 5)
59 {
60 lua_a.read(lua, 5);
61 a = std::clamp(float(lua_a.n), 0.0f, 1.0f);
62 }
63 atomColourOverrides[index] = glm::vec4(r, g, b, a);
64 elementsUpdated = true;
65
66 return 0;
67}
68
78{
79 int args = lua_gettop(lua);
80 if (lua_gettop(lua) >= 1 && lua_isstring(lua, 1) && std::string(lua_tostring(lua, 1)) == "help")
81 {
82 lua_writestring("setAtomColour:\n Arguments: none\n Get atom i's colour.\n");
83 return 0;
84 };
85 if (args != 1)
86 {
87 const std::string msg = "getAtomColour expects an atom index as argument.\n";
88 lua_pushlstring(lua, msg.c_str(), msg.length());
89 return lua_error(lua);
90 }
92 uint64_t index;
93
94 lua_index.read(lua, 1);
95 index = uint64_t(lua_index.n);
96
97 if (index >= atomCount)
98 {
99 const std::string msg = "getAtomColour atom index larger than atom count.\n";
100 lua_pushlstring(lua, msg.c_str(), msg.length());
101 return lua_error(lua);
102 }
103
104 glm::vec4 colour = atoms[index].colour;
105 if (atomColourOverrides.find(index) != atomColourOverrides.cend())
106 {
107 colour = atomColourOverrides[index];
108 }
109 colour.a = atomEmphasisOverrides[index];
110
111 lua_pushnumber(lua, colour.r);
112 lua_pushnumber(lua, colour.g);
113 lua_pushnumber(lua, colour.b);
114 lua_pushnumber(lua, colour.a);
115
116 return 4;
117}
118
126{
127 if (lua_gettop(lua) >= 1 && lua_isstring(lua, 1) && std::string(lua_tostring(lua, 1)) == "help")
128 {
129 lua_writestring("atomCount:\n Arguments: none\n Get number of atoms.\n");
130 return 0;
131 };
133 return 1;
134}
135
147{
148 int args = lua_gettop(lua);
149 if (lua_gettop(lua) >= 1 && lua_isstring(lua, 1) && std::string(lua_tostring(lua, 1)) == "help")
150 {
151 lua_writestring("setAtomColour:\n Arguments:\n i [number]\n cutoff [number]\n Get atom i's neighbours within cutoff.\n");
152 return 0;
153 };
154 if (args < 2)
155 {
156 const std::string msg = "getAtomsNeighbours expects an atom index and cutoff distance as argument.\n";
157 lua_pushlstring(lua, msg.c_str(), msg.length());
158 return lua_error(lua);
159 }
160
162 li.read(lua, 1);
163 cutoff.read(lua, 2);
164
165 bool nearestImage = true;
166 if (args == 3)
167 {
168 LuaBool b;
169 b.read(lua, 3);
170 nearestImage = b.bit;
171 }
172
173 uint64_t i = li.n;
174
175 if (i >= atomCount)
176 {
177 const std::string msg = "getAtom atom index larger than atom count.\n";
178 lua_pushlstring(lua, msg.c_str(), msg.length());
179 return lua_error(lua);
180 }
182 auto n = neighbourList.neighbours(atoms, atoms[i].position, cutoff, false, nearestImage);
183
184 i = 1;
185 lua_createtable(lua, n.size(), 0);
186 for (auto jd : n)
187 {
188 lua_createtable(lua, 2, 0);
189 lua_pushnumber(lua, jd.first);
190 lua_setfield(lua, -2, "index");
191 lua_pushnumber(lua, jd.second);
192 lua_setfield(lua, -2, "distance");
193 lua_rawseti(lua, -2, i);
194 i++;
195 }
196 return 1;
197}
198
208{
209 int args = lua_gettop(lua);
210 if (lua_gettop(lua) >= 1 && lua_isstring(lua, 1) && std::string(lua_tostring(lua, 1)) == "help")
211 {
212 lua_writestring("getAtom:\n Arguments: \n i [number]\n Get atom i's state.\n");
213 return 0;
214 };
215 if (args != 1)
216 {
217 const std::string msg = "getAtom expects an atom index as argument.\n";
218 lua_pushlstring(lua, msg.c_str(), msg.length());
219 return lua_error(lua);
220 }
221
223 li.read(lua, 1);
224
225 uint64_t i = li.n;
226
227 if (i >= atomCount)
228 {
229 const std::string msg = "getAtom atom index larger than atom count.\n";
230 lua_pushlstring(lua, msg.c_str(), msg.length());
231 return lua_error(lua);
232 }
233
234 Atom & atom = atoms[i];
235 std::string element = STRING_FROM_ELEMENT.at(atom.symbol);
236
237 lua_createtable(lua, 6, 0);
238 lua_pushstring(lua, element.c_str());
239 lua_setfield(lua, -2, "element");
240 lua_pushnumber(lua, atom.scale);
241 lua_setfield(lua, -2, "radius");
242 lua_createtable(lua, 3, 0);
243 lua_pushnumber(lua, atom.position.x);
244 lua_setfield(lua, -2, "x");
245 lua_pushnumber(lua, atom.position.y);
246 lua_setfield(lua, -2, "y");
247 lua_pushnumber(lua, atom.position.z);
248 lua_setfield(lua, -2, "z");
249 lua_setfield(lua, -2, "position");
250 lua_createtable(lua, 3, 0);
251 lua_pushnumber(lua, atom.velocity.x);
252 lua_setfield(lua, -2, "x");
253 lua_pushnumber(lua, atom.velocity.y);
254 lua_setfield(lua, -2, "y");
255 lua_pushnumber(lua, atom.velocity.z);
256 lua_setfield(lua, -2, "z");
257 lua_setfield(lua, -2, "velocity");
258 lua_createtable(lua, 3, 0);
259 lua_pushnumber(lua, atom.force.x);
260 lua_setfield(lua, -2, "x");
261 lua_pushnumber(lua, atom.force.y);
262 lua_setfield(lua, -2, "y");
263 lua_pushnumber(lua, atom.force.z);
264 lua_setfield(lua, -2, "z");
265 lua_setfield(lua, -2, "force");
266 lua_createtable(lua, 4, 0);
267 lua_pushnumber(lua, atom.colour.r);
268 lua_setfield(lua, -2, "r");
269 lua_pushnumber(lua, atom.colour.g);
270 lua_setfield(lua, -2, "g");
271 lua_pushnumber(lua, atom.colour.b);
272 lua_setfield(lua, -2, "b");
273 lua_pushnumber(lua, atom.colour.a);
274 lua_setfield(lua, -2, "a");
275 lua_setfield(lua, -2, "colour");
276
277 return 1;
278
279}
280
290{
291 int args = lua_gettop(lua);
292 if (lua_gettop(lua) >= 1 && lua_isstring(lua, 1) && std::string(lua_tostring(lua, 1)) == "help")
293 {
294 lua_writestring("getAtomsBonds:\n Arguments:\n i [number]\n Get atom i's bonds.\n");
295 return 0;
296 };
297 if (args != 1)
298 {
299 const std::string msg = "getAtomsBonds expects an atom index as argument.\n";
300 lua_pushlstring(lua, msg.c_str(), msg.length());
301 return lua_error(lua);
302 }
303
305 li.read(lua, 1);
306
307 uint64_t i = li.n;
308
309 if (i >= atomCount)
310 {
311 const std::string msg = "getAtomsBonds atom index larger than atom count.\n";
312 lua_pushlstring(lua, msg.c_str(), msg.length());
313 return lua_error(lua);
314 }
315
316 auto bonded = bonds[i];
317 lua_createtable(lua, bonded.size(), 0);
318 if (bonded.size() > 0)
319 {
320 uint64_t i = 1;
321 for (auto j : bonded)
322 {
323 lua_pushinteger(lua, j);
324 lua_rawseti(lua, -2, i);
325 i++;
326 }
327 }
328
329 return 1;
330}
331
332
333#endif /* ATOMS_H */
An atom structure.
Definition atom.h:20
Calculate neighbour lists.
Definition neighbours.h:18
glm::vec< L, float, glm::qualifier::highp > vec
Definition commandLine.h:214
const std::map< Element, std::string > STRING_FROM_ELEMENT
Map Element to string symbols.
Definition element.h:348
Interop for booleans Lua.
Definition LuaBool.h:11
void read(lua_State *lua, int index)
Read the bool from stack index index.
Definition LuaBool.h:27
bool bit
Definition LuaBool.h:85
Interop for a number in Lua.
Definition LuaNumber.h:11
void read(lua_State *lua, int index)
Read the number from stack index index.
Definition LuaNumber.h:26
std::map< uint64_t, glm::vec4 > atomColourOverrides
Definition visualisationState.h:121
int lua_atomCount(lua_State *lua)
Lua binding to get the Atom count.
Definition atoms.h:125
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
uint64_t atomCount
Definition visualisationState.h:128
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