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);
28 {
29 const std::string msg = "setAtomColour expects an atom index and RGB or RGBA arguments.\n";
30 lua_pushlstring(lua, msg.c_str(), msg.length());
31 return lua_error(lua);
32 }
34 uint64_t index;
35 float r, g, b, a;
36 a = 1.0f;
37
38 lua_index.read(lua, 1);
39 index = uint64_t(lua_index.n);
40
41 if (index >= atomCount)
42 {
43 const std::string msg = "setAtomColour atom index larger than atom count.\n";
44 lua_pushlstring(lua, msg.c_str(), msg.length());
45 return lua_error(lua);
46 }
47 lua_r.read(lua, 2);
48 r = std::clamp(float(lua_r.n), 0.0f, 1.0f);
49 lua_g.read(lua, 3);
50 g = std::clamp(float(lua_g.n), 0.0f, 1.0f);
51 lua_b.read(lua, 4);
52 b = std::clamp(float(lua_b.n), 0.0f, 1.0f);
53 if (args == 5)
54 {
55 lua_a.read(lua, 5);
56 a = std::clamp(float(lua_a.n), 0.0f, 1.0f);
57 }
58 atomColourOverrides[index] = glm::vec4(r, g, b, a);
59
60 return 0;
61}
62
72{
73 int args = lua_gettop(lua);
74 if (args != 1)
75 {
76 const std::string msg = "getAtomColour expects an atom index as argument.\n";
77 lua_pushlstring(lua, msg.c_str(), msg.length());
78 return lua_error(lua);
79 }
81 uint64_t index;
82
83 lua_index.read(lua, 1);
84 index = uint64_t(lua_index.n);
85
86 if (index >= atomCount)
87 {
88 const std::string msg = "getAtomColour atom index larger than atom count.\n";
89 lua_pushlstring(lua, msg.c_str(), msg.length());
90 return lua_error(lua);
91 }
92
93 glm::vec4 colour = atoms[index].colour;
94 if (atomColourOverrides.find(index) != atomColourOverrides.cend())
95 {
96 colour = atomColourOverrides[index];
97 }
98 colour.a = atomEmphasisOverrides[index];
99
100 lua_pushnumber(lua, colour.r);
101 lua_pushnumber(lua, colour.g);
102 lua_pushnumber(lua, colour.b);
103 lua_pushnumber(lua, colour.a);
104
105 return 4;
106}
107
115{
117 return 1;
118}
119
131{
132 int args = lua_gettop(lua);
133 if (args < 2)
134 {
135 const std::string msg = "getAtomsNeighbours expects an atom index and cutoff distance as argument.\n";
136 lua_pushlstring(lua, msg.c_str(), msg.length());
137 return lua_error(lua);
138 }
139
141 li.read(lua, 1);
142 cutoff.read(lua, 2);
143
144 bool nearestImage = true;
145 if (args == 3)
146 {
147 LuaBool b;
148 b.read(lua, 3);
149 nearestImage = b.bit;
150 }
151
152 uint64_t i = li.n;
153
154 if (i >= atomCount)
155 {
156 const std::string msg = "getAtom atom index larger than atom count.\n";
157 lua_pushlstring(lua, msg.c_str(), msg.length());
158 return lua_error(lua);
159 }
161 auto n = neighbourList.neighbours(atoms, atoms[i].position, cutoff, false, nearestImage);
162
163 i = 1;
164 lua_createtable(lua, n.size(), 0);
165 for (auto jd : n)
166 {
167 lua_createtable(lua, 2, 0);
168 lua_pushnumber(lua, jd.first);
169 lua_setfield(lua, -2, "index");
170 lua_pushnumber(lua, jd.second);
171 lua_setfield(lua, -2, "distance");
172 lua_rawseti(lua, -2, i);
173 i++;
174 }
175 return 1;
176}
177
187{
188 int args = lua_gettop(lua);
189 if (args != 1)
190 {
191 const std::string msg = "getAtom expects an atom index as argument.\n";
192 lua_pushlstring(lua, msg.c_str(), msg.length());
193 return lua_error(lua);
194 }
195
197 li.read(lua, 1);
198
199 uint64_t i = li.n;
200
201 if (i >= atomCount)
202 {
203 const std::string msg = "getAtom atom index larger than atom count.\n";
204 lua_pushlstring(lua, msg.c_str(), msg.length());
205 return lua_error(lua);
206 }
207
208 Atom & atom = atoms[i];
209 std::string element = STRING_FROM_ELEMENT.at(atom.symbol);
210
211 lua_createtable(lua, 6, 0);
212 lua_pushstring(lua, element.c_str());
213 lua_setfield(lua, -2, "element");
214 lua_pushnumber(lua, atom.scale);
215 lua_setfield(lua, -2, "radius");
216 lua_createtable(lua, 3, 0);
217 lua_pushnumber(lua, atom.position.x);
218 lua_setfield(lua, -2, "x");
219 lua_pushnumber(lua, atom.position.y);
220 lua_setfield(lua, -2, "y");
221 lua_pushnumber(lua, atom.position.z);
222 lua_setfield(lua, -2, "z");
223 lua_setfield(lua, -2, "position");
224 lua_createtable(lua, 3, 0);
225 lua_pushnumber(lua, atom.velocity.x);
226 lua_setfield(lua, -2, "x");
227 lua_pushnumber(lua, atom.velocity.y);
228 lua_setfield(lua, -2, "y");
229 lua_pushnumber(lua, atom.velocity.z);
230 lua_setfield(lua, -2, "z");
231 lua_setfield(lua, -2, "velocity");
232 lua_createtable(lua, 3, 0);
233 lua_pushnumber(lua, atom.force.x);
234 lua_setfield(lua, -2, "x");
235 lua_pushnumber(lua, atom.force.y);
236 lua_setfield(lua, -2, "y");
237 lua_pushnumber(lua, atom.force.z);
238 lua_setfield(lua, -2, "z");
239 lua_setfield(lua, -2, "force");
240 lua_createtable(lua, 4, 0);
241 lua_pushnumber(lua, atom.colour.r);
242 lua_setfield(lua, -2, "r");
243 lua_pushnumber(lua, atom.colour.g);
244 lua_setfield(lua, -2, "g");
245 lua_pushnumber(lua, atom.colour.b);
246 lua_setfield(lua, -2, "b");
247 lua_pushnumber(lua, atom.colour.a);
248 lua_setfield(lua, -2, "a");
249 lua_setfield(lua, -2, "colour");
250
251 return 1;
252
253}
254
264{
265 int args = lua_gettop(lua);
266 if (args != 1)
267 {
268 const std::string msg = "getAtomsBonds expects an atom index as argument.\n";
269 lua_pushlstring(lua, msg.c_str(), msg.length());
270 return lua_error(lua);
271 }
272
274 li.read(lua, 1);
275
276 uint64_t i = li.n;
277
278 if (i >= atomCount)
279 {
280 const std::string msg = "getAtomsBonds atom index larger than atom count.\n";
281 lua_pushlstring(lua, msg.c_str(), msg.length());
282 return lua_error(lua);
283 }
284
285 auto bonded = bonds[i];
286 lua_createtable(lua, bonded.size(), 0);
287 if (bonded.size() > 0)
288 {
289 uint64_t i = 1;
290 for (auto j : bonded)
291 {
292 lua_pushinteger(lua, j);
293 lua_rawseti(lua, -2, i);
294 i++;
295 }
296 }
297
298 return 1;
299}
300
301
302#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:120
int lua_atomCount(lua_State *lua)
Lua binding to get the Atom count.
Definition atoms.h:114
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
uint64_t atomCount
Definition visualisationState.h:127
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