Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ if (NOT WINDOWS)
add_compile_definitions(LUA_USE_POSIX)
endif()

file(GLOB LUA_SRC "include/vendored/lua/src/*.c")
file(GLOB LUA_SRC "include/vendored/lua/src/*.cpp")
include_directories(include/vendored/lua/src)
add_library(Lua STATIC ${LUA_SRC})

Expand Down
4 changes: 4 additions & 0 deletions include/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class Camera

pv = projection*view;
invPv = glm::inverse(pv);
updated = true;
}

/**
Expand Down Expand Up @@ -317,6 +318,8 @@ class Camera
*/
inline int lua_getCameraFieldOfView(lua_State * lua);

bool updated = false;

private:

uint16_t resX;
Expand Down Expand Up @@ -345,6 +348,7 @@ class Camera
invView = glm::inverse(view);
pv = projection*view;
invPv = glm::inverse(pv);
updated = true;
}

};
Expand Down
39 changes: 10 additions & 29 deletions include/cameraWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,6 @@ class CameraWindow
{
public:

/**
* @brief If the Camera or Atoms require updates.
*
*/
struct CameraUpdates
{
CameraUpdates()
: cameraMoved(false),
atomsMoved(false)
{}

bool cameraMoved;
bool atomsMoved;
};

CameraWindow() {}

/**
Expand Down Expand Up @@ -146,77 +131,73 @@ class CameraWindow
*
* @param camera the Camera to update.
* @param atoms the Atoms to update.
* @param atomsMoved signal if atoms have been modified.
* @param options the options to use for actions.
* @param dr the length unit.
* @param dtheta the rotation unit.
* @param dphi the incline unit.
* @return CameraUpdates Whether the Camera or Atoms were updated.
*/
CameraUpdates applyQueueActions
void applyQueueActions
(
Camera & camera,
std::vector<Atom> & atoms,
bool & atomsMoved,
const CommandLine & options,
const float & dr,
const float & dtheta,
const float & dphi
)
{
CameraUpdates moved;

if (zoomQueue != 0)
{
camera.zoom(dr*options.cameraZoomSpeed.value*zoomQueue);
zoomQueue = 0;
moved.cameraMoved = true;
atomsMoved = true;
}

if (rotateQueue != 0)
{
camera.rotate(dtheta*options.cameraRotateSpeed.value*rotateQueue);
rotateQueue = 0;
moved.cameraMoved = true;
atomsMoved = true;
}

if (inclineQueue != 0)
{
camera.incline(dphi*options.cameraInclineSpeed.value*inclineQueue);
inclineQueue = 0;
moved.cameraMoved = true;
atomsMoved = true;
}

if (resetCamera)
{
if (!options.noCentering.value) { center(atoms); }
if (options.focus.value < atoms.size()) { centerOn(atoms, options.focus.value); }
camera.reset(atoms);
moved.atomsMoved = true;
moved.cameraMoved = true;
atomsMoved = true;
resetCamera = false;
}

if (panxQueue != 0)
{
translate(atoms, {dr*options.cameraPanSpeed.value*panxQueue, 0.0, 0.0});
panxQueue = 0;
moved.atomsMoved = true;
atomsMoved = true;
}

if (panyQueue != 0)
{
translate(atoms, {0.0, dr*options.cameraPanSpeed.value*panyQueue, 0.0});
panyQueue = 0;
moved.atomsMoved = true;
atomsMoved = true;
}

if (panzQueue != 0)
{
translate(atoms, {0.0, 0.0, dr*options.cameraPanSpeed.value*panzQueue});
panzQueue = 0;
moved.atomsMoved = true;
atomsMoved = true;
}

return moved;
}

private:
Expand Down
65 changes: 36 additions & 29 deletions include/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
#include <chrono>
#include <string>
#include <sstream>

#include <jLog/jLog.h>
#include <iostream>

#include <lua.h>
#include <LuaNumber.h>
Expand Down Expand Up @@ -178,18 +177,16 @@ class Console
public:

/**
* @brief Construct a new Console with a jLog::Log.
* @brief Construct a new Console.
*
* @param l jLog::Log outputting Lua's messages.
*/
Console
(
jLog::Log & l,
VisualisationState * visualisationState,
CommandLine * options,
Camera * camera
)
: lastCommandOrProgram(""), lastStatus(false), log(l)
: lastCommandOrProgram(""), lastStatus(false)
{
lua = luaL_newstate();
luaL_openlibs(lua);
Expand All @@ -199,6 +196,17 @@ class Console
extraSpace.camera = camera;
extraSpace.exit = &exit;
*static_cast<LuaExtraSpace**>(lua_getextraspace(lua)) = &extraSpace;

const char * init = R"(
function use(module)
for k,v in pairs(module) do
if not _G[k] then
_G[k] = module[k]
end
end
end
use(sfoav))";
runString(init);
}

~Console(){ lua_close(lua); }
Expand All @@ -216,11 +224,7 @@ class Console
{
lastCommandOrProgram = file;
lastStatus = luaL_loadfile(lua, file.c_str());
int epos = lua_gettop(lua);
lua_pushcfunction(lua, traceback);
lua_insert(lua, epos);
lastStatus = lastStatus || lua_pcall(lua, 0, LUA_MULTRET, epos);
lua_remove(lua, epos);
lastStatus = lastStatus || lua_pcall(lua, 0, LUA_MULTRET, 0);
return handleErrors();
}
return false;
Expand All @@ -236,13 +240,31 @@ class Console
bool runString(std::string program)
{
if (luaIsOk())
{ lastCommandOrProgram = program;
{
lastCommandOrProgram = program;
lastStatus = luaL_dostring(lua,program.c_str());
return handleErrors();
}
return false;
}

/**
* @brief Attempt to run a Lua script from std::string.
*
* @param file Lua script.
* @return true Error occured.
* @return false OK.
*/
bool runString(const char * program)
{
if (luaIsOk())
{ lastCommandOrProgram = std::string(program);
lastStatus = luaL_dostring(lua,program);
return handleErrors();
}
return false;
}

bool luaIsOk(){ return lua_status(lua) == LUA_OK ? true : false; }

/**
Expand Down Expand Up @@ -310,27 +332,12 @@ class Console
bool lastStatus;
bool exit = false;

jLog::Log & log;

static int traceback(lua_State * lua) {
if (lua_isstring(lua, -1))
{
stackTrace = lua_tostring(lua, -1);
lua_pop(lua, 1);
}
luaL_traceback(lua, lua, NULL, 1);
stackTrace += std::string("\n") + lua_tostring(lua, -1);
lua_pop(lua, 1);
return 0;
}

bool handleErrors()
{
if (lastStatus)
{
std::string msg = "Exited with error running "+lastCommandOrProgram+"\n";
msg += stackTrace;
jLog::ERR(jLog::ERRORCODE::LUA_ERROR, msg) >> log;
if (lua_isstring(lua, -1)) { std::cerr << lua_tostring(lua, -1) << "\n"; }
else { std::cerr << "Exited with error running "+lastCommandOrProgram+"\n"; }
return true;
}
else
Expand Down
Loading