mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-01-06 03:29:44 -06:00
Add Scaling, Rotation, and Translation that is based on evaluation of Lua scripts
This commit is contained in:
21
data/scene/examples/luatransforms/luatransforms.mod
Normal file
21
data/scene/examples/luatransforms/luatransforms.mod
Normal file
@@ -0,0 +1,21 @@
|
||||
return {
|
||||
-- Earth barycenter module
|
||||
{
|
||||
Name = "Lua Transformation",
|
||||
Parent = "SolarSystemBarycenter",
|
||||
Transform = {
|
||||
Rotation = {
|
||||
Type = "LuaRotation",
|
||||
Script = "rotation.lua"
|
||||
},
|
||||
Scale = {
|
||||
Type = "LuaScale",
|
||||
Script = "scale.lua"
|
||||
},
|
||||
Translation = {
|
||||
Type = "LuaTranslation",
|
||||
Script = "translate.lua"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
6
data/scene/examples/luatransforms/rotation.lua
Normal file
6
data/scene/examples/luatransforms/rotation.lua
Normal file
@@ -0,0 +1,6 @@
|
||||
-- t1: Ingame seconds past the J2000 epoch
|
||||
-- t2: Wallclock milliseconds past the J2000 epoch
|
||||
|
||||
function rotation(t1, t2)
|
||||
return 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0
|
||||
end
|
||||
6
data/scene/examples/luatransforms/scale.lua
Normal file
6
data/scene/examples/luatransforms/scale.lua
Normal file
@@ -0,0 +1,6 @@
|
||||
-- t1: Ingame seconds past the J2000 epoch
|
||||
-- t2: Wallclock milliseconds past the J2000 epoch
|
||||
|
||||
function scale(t1, t2)
|
||||
return 1.0;
|
||||
end
|
||||
6
data/scene/examples/luatransforms/translate.lua
Normal file
6
data/scene/examples/luatransforms/translate.lua
Normal file
@@ -0,0 +1,6 @@
|
||||
-- t1: Ingame seconds past the J2000 epoch
|
||||
-- t2: Wallclock milliseconds past the J2000 epoch
|
||||
|
||||
function translation(t1, t2)
|
||||
return 0.0, 0.0, 0.0
|
||||
end
|
||||
@@ -47,10 +47,13 @@ public:
|
||||
virtual ~Scale() = default;
|
||||
|
||||
virtual bool initialize();
|
||||
virtual double scaleValue() const = 0;
|
||||
virtual double scaleValue() const;
|
||||
virtual void update(const UpdateData& data);
|
||||
|
||||
static documentation::Documentation Documentation();
|
||||
|
||||
protected:
|
||||
double _scale;
|
||||
};
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
@@ -48,7 +48,7 @@ public:
|
||||
virtual ~Translation() = default;
|
||||
virtual bool initialize();
|
||||
|
||||
virtual glm::dvec3 position() const = 0;
|
||||
virtual glm::dvec3 position() const;
|
||||
virtual void update(const UpdateData& data);
|
||||
|
||||
glm::dvec3 position(double time);
|
||||
@@ -63,6 +63,8 @@ protected:
|
||||
void notifyObservers();
|
||||
|
||||
std::function<void()> _onParameterChangeCallback;
|
||||
|
||||
glm::dvec3 _positionValue;
|
||||
};
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
@@ -37,8 +37,11 @@ set(HEADER_FILES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/rendering/screenspaceframebuffer.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/rendering/screenspaceimagelocal.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/rendering/screenspaceimageonline.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/translation/luatranslation.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/translation/statictranslation.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/rotation/luarotation.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/rotation/staticrotation.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/scale/luascale.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/scale/staticscale.h
|
||||
)
|
||||
source_group("Header Files" FILES ${HEADER_FILES})
|
||||
@@ -56,8 +59,11 @@ set(SOURCE_FILES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/rendering/screenspaceframebuffer.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/rendering/screenspaceimagelocal.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/rendering/screenspaceimageonline.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/translation/luatranslation.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/translation/statictranslation.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/rotation/luarotation.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/rotation/staticrotation.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/scale/luascale.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/scale/staticscale.cpp
|
||||
)
|
||||
source_group("Source Files" FILES ${SOURCE_FILES})
|
||||
|
||||
@@ -43,10 +43,13 @@
|
||||
#include <modules/base/rendering/screenspaceimageonline.h>
|
||||
#include <modules/base/rendering/screenspaceframebuffer.h>
|
||||
|
||||
#include <modules/base/translation/luatranslation.h>
|
||||
#include <modules/base/translation/statictranslation.h>
|
||||
|
||||
#include <modules/base/rotation/luarotation.h>
|
||||
#include <modules/base/rotation/staticrotation.h>
|
||||
|
||||
#include <modules/base/scale/luascale.h>
|
||||
#include <modules/base/scale/staticscale.h>
|
||||
|
||||
#include <ghoul/filesystem/filesystem>
|
||||
@@ -85,16 +88,19 @@ void BaseModule::internalInitialize() {
|
||||
auto fTranslation = FactoryManager::ref().factory<Translation>();
|
||||
ghoul_assert(fTranslation, "Ephemeris factory was not created");
|
||||
|
||||
fTranslation->registerClass<LuaTranslation>("LuaTranslation");
|
||||
fTranslation->registerClass<StaticTranslation>("StaticTranslation");
|
||||
|
||||
auto fRotation = FactoryManager::ref().factory<Rotation>();
|
||||
ghoul_assert(fRotation, "Rotation factory was not created");
|
||||
|
||||
fRotation->registerClass<LuaRotation>("LuaRotation");
|
||||
fRotation->registerClass<StaticRotation>("StaticRotation");
|
||||
|
||||
auto fScale = FactoryManager::ref().factory<Scale>();
|
||||
ghoul_assert(fScale, "Scale factory was not created");
|
||||
|
||||
fScale->registerClass<LuaScale>("LuaScale");
|
||||
fScale->registerClass<StaticScale>("StaticScale");
|
||||
|
||||
auto fModelGeometry = FactoryManager::ref().factory<modelgeometry::ModelGeometry>();
|
||||
@@ -112,8 +118,11 @@ std::vector<documentation::Documentation> BaseModule::documentations() const {
|
||||
ScreenSpaceFramebuffer::Documentation(),
|
||||
ScreenSpaceImageLocal::Documentation(),
|
||||
ScreenSpaceImageOnline::Documentation(),
|
||||
LuaRotation::Documentation(),
|
||||
StaticRotation::Documentation(),
|
||||
LuaScale::Documentation(),
|
||||
StaticScale::Documentation(),
|
||||
LuaTranslation::Documentation(),
|
||||
StaticTranslation::Documentation(),
|
||||
modelgeometry::ModelGeometry::Documentation(),
|
||||
};
|
||||
|
||||
133
modules/base/rotation/luarotation.cpp
Normal file
133
modules/base/rotation/luarotation.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
|
||||
* software and associated documentation files (the "Software"), to deal in the Software *
|
||||
* without restriction, including without limitation the rights to use, copy, modify, *
|
||||
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
|
||||
* permit persons to whom the Software is furnished to do so, subject to the following *
|
||||
* conditions: *
|
||||
* *
|
||||
* The above copyright notice and this permission notice shall be included in all copies *
|
||||
* or substantial portions of the Software. *
|
||||
* *
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
|
||||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
|
||||
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
|
||||
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
|
||||
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
|
||||
****************************************************************************************/
|
||||
|
||||
#include <modules/base/rotation/luarotation.h>
|
||||
|
||||
#include <openspace/documentation/documentation.h>
|
||||
#include <openspace/documentation/verifier.h>
|
||||
#include <openspace/util/updatestructures.h>
|
||||
|
||||
#include <ghoul/lua/ghoul_lua.h>
|
||||
#include <ghoul/lua/lua_helper.h>
|
||||
#include <ghoul/filesystem/filesystem.h>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace {
|
||||
static const openspace::properties::Property::PropertyInfo ScriptInfo = {
|
||||
"Script",
|
||||
"Script",
|
||||
"This value is the path to the Lua script that will be executed to compute the "
|
||||
"rotation for this transformation. The script needs to define a function "
|
||||
"'rotation' that takes the current simulation time in seconds past the J2000 "
|
||||
"epoch as the first argument, the current wall time as milliseconds past the "
|
||||
"J2000 epoch as the second argument and computes the rotation returned as 9 "
|
||||
"values."
|
||||
};} // namespace
|
||||
|
||||
namespace openspace {
|
||||
|
||||
documentation::Documentation LuaRotation::Documentation() {
|
||||
using namespace openspace::documentation;
|
||||
return {
|
||||
"Lua Rotation",
|
||||
"base_transform_rotation_lua",
|
||||
{
|
||||
{
|
||||
"Type",
|
||||
new StringEqualVerifier("LuaRotation"),
|
||||
Optional::No
|
||||
},
|
||||
{
|
||||
ScriptInfo.identifier,
|
||||
new StringVerifier,
|
||||
Optional::No,
|
||||
ScriptInfo.description
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
LuaRotation::LuaRotation()
|
||||
: _luaScriptFile(ScriptInfo)
|
||||
, _state(false)
|
||||
{
|
||||
addProperty(_luaScriptFile);
|
||||
}
|
||||
|
||||
LuaRotation::LuaRotation(const ghoul::Dictionary& dictionary)
|
||||
: LuaRotation()
|
||||
{
|
||||
documentation::testSpecificationAndThrow(
|
||||
Documentation(),
|
||||
dictionary,
|
||||
"LuaRotation"
|
||||
);
|
||||
|
||||
_luaScriptFile = absPath(dictionary.value<std::string>(ScriptInfo.identifier));
|
||||
}
|
||||
|
||||
void LuaRotation::update(const UpdateData& data) {
|
||||
ghoul::lua::runScriptFile(_state, _luaScriptFile);
|
||||
|
||||
// Get the scaling function
|
||||
lua_getglobal(_state, "rotation");
|
||||
bool isFunction = lua_isfunction(_state, -1);
|
||||
if (!isFunction) {
|
||||
LERRORC(
|
||||
"LuaRotation",
|
||||
"Script '" << _luaScriptFile << "' does not have a function 'rotation'"
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// First argument is the number of seconds past the J2000 epoch in ingame time
|
||||
lua_pushnumber(_state, data.time.j2000Seconds());
|
||||
|
||||
// Second argument is the number of milliseconds past the J2000 epoch in wallclock
|
||||
using namespace std::chrono;
|
||||
auto now = high_resolution_clock::now();
|
||||
lua_pushnumber(
|
||||
_state,
|
||||
duration_cast<milliseconds>(now.time_since_epoch()).count()
|
||||
);
|
||||
|
||||
// Execute the scaling function
|
||||
int success = lua_pcall(_state, 2, 9, 0);
|
||||
if (success != 0) {
|
||||
LERRORC(
|
||||
"LuaScale",
|
||||
"Error executing 'rotation': " << lua_tostring(_state, -1)
|
||||
);
|
||||
}
|
||||
|
||||
double values[9];
|
||||
for (int i = 0; i < 9; ++i) {
|
||||
values[i] = luaL_checknumber(_state, -1 - i);
|
||||
}
|
||||
|
||||
_matrix = glm::make_mat3(values);
|
||||
}
|
||||
|
||||
} // namespace openspace
|
||||
54
modules/base/rotation/luarotation.h
Normal file
54
modules/base/rotation/luarotation.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
|
||||
* software and associated documentation files (the "Software"), to deal in the Software *
|
||||
* without restriction, including without limitation the rights to use, copy, modify, *
|
||||
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
|
||||
* permit persons to whom the Software is furnished to do so, subject to the following *
|
||||
* conditions: *
|
||||
* *
|
||||
* The above copyright notice and this permission notice shall be included in all copies *
|
||||
* or substantial portions of the Software. *
|
||||
* *
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
|
||||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
|
||||
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
|
||||
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
|
||||
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
|
||||
****************************************************************************************/
|
||||
|
||||
#ifndef __OPENSPACE_MODULE_BASE___LUAROTATION___H__
|
||||
#define __OPENSPACE_MODULE_BASE___LUAROTATION___H__
|
||||
|
||||
#include <openspace/scene/rotation.h>
|
||||
|
||||
#include <openspace/properties/stringproperty.h>
|
||||
|
||||
#include <ghoul/lua/luastate.h>
|
||||
|
||||
namespace openspace {
|
||||
|
||||
namespace documentation { struct Documentation; }
|
||||
|
||||
class LuaRotation : public Rotation {
|
||||
public:
|
||||
LuaRotation();
|
||||
LuaRotation(const ghoul::Dictionary& dictionary);
|
||||
|
||||
void update(const UpdateData& data) override;
|
||||
|
||||
static documentation::Documentation Documentation();
|
||||
|
||||
private:
|
||||
properties::StringProperty _luaScriptFile;
|
||||
ghoul::lua::LuaState _state;
|
||||
};
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
#endif // __OPENSPACE_MODULE_BASE___LUAROTATION___H__
|
||||
119
modules/base/scale/luascale.cpp
Normal file
119
modules/base/scale/luascale.cpp
Normal file
@@ -0,0 +1,119 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
|
||||
* software and associated documentation files (the "Software"), to deal in the Software *
|
||||
* without restriction, including without limitation the rights to use, copy, modify, *
|
||||
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
|
||||
* permit persons to whom the Software is furnished to do so, subject to the following *
|
||||
* conditions: *
|
||||
* *
|
||||
* The above copyright notice and this permission notice shall be included in all copies *
|
||||
* or substantial portions of the Software. *
|
||||
* *
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
|
||||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
|
||||
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
|
||||
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
|
||||
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
|
||||
****************************************************************************************/
|
||||
|
||||
#include <modules/base/scale/luascale.h>
|
||||
|
||||
#include <openspace/documentation/documentation.h>
|
||||
#include <openspace/documentation/verifier.h>
|
||||
#include <openspace/util/updatestructures.h>
|
||||
|
||||
#include <ghoul/lua/ghoul_lua.h>
|
||||
#include <ghoul/lua/lua_helper.h>
|
||||
#include <ghoul/filesystem/filesystem.h>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace {
|
||||
static const openspace::properties::Property::PropertyInfo ScriptInfo = {
|
||||
"Script",
|
||||
"Script",
|
||||
"This value is the path to the Lua script that will be executed to compute the "
|
||||
"scaling factor for this transformation. The script needs to define a function "
|
||||
"'scale' that takes the current simulation time in seconds past the J2000 epoch "
|
||||
"as the first argument, the current wall time as milliseconds past the J2000 "
|
||||
"epoch the second argument and computes the scaling factor."
|
||||
};
|
||||
} // namespace
|
||||
|
||||
namespace openspace {
|
||||
|
||||
documentation::Documentation LuaScale::Documentation() {
|
||||
using namespace openspace::documentation;
|
||||
return {
|
||||
"Lua Scaling",
|
||||
"base_scale_lua",
|
||||
{
|
||||
{
|
||||
ScriptInfo.identifier,
|
||||
new StringVerifier,
|
||||
Optional::No,
|
||||
ScriptInfo.description
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
LuaScale::LuaScale()
|
||||
: _luaScriptFile(ScriptInfo)
|
||||
, _state(false)
|
||||
{
|
||||
addProperty(_luaScriptFile);
|
||||
}
|
||||
|
||||
LuaScale::LuaScale(const ghoul::Dictionary& dictionary)
|
||||
: LuaScale()
|
||||
{
|
||||
documentation::testSpecificationAndThrow(Documentation(), dictionary, "LuaScale");
|
||||
|
||||
_luaScriptFile = absPath(dictionary.value<std::string>(ScriptInfo.identifier));
|
||||
}
|
||||
|
||||
void LuaScale::update(const UpdateData& data) {
|
||||
ghoul::lua::runScriptFile(_state, _luaScriptFile);
|
||||
|
||||
// Get the scaling function
|
||||
lua_getglobal(_state, "scale");
|
||||
bool isFunction = lua_isfunction(_state, -1);
|
||||
if (!isFunction) {
|
||||
LERRORC(
|
||||
"LuaScale",
|
||||
"Script '" << _luaScriptFile << "' does not have a function 'scale'"
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// First argument is the number of seconds past the J2000 epoch in ingame time
|
||||
lua_pushnumber(_state, data.time.j2000Seconds());
|
||||
|
||||
// Second argument is the number of milliseconds past the J2000 epoch in wallclock
|
||||
using namespace std::chrono;
|
||||
auto now = high_resolution_clock::now();
|
||||
lua_pushnumber(
|
||||
_state,
|
||||
duration_cast<milliseconds>(now.time_since_epoch()).count()
|
||||
);
|
||||
|
||||
// Execute the scaling function
|
||||
int success = lua_pcall(_state, 2, 1, 0);
|
||||
if (success != 0) {
|
||||
LERRORC(
|
||||
"LuaScale",
|
||||
"Error executing 'scale': " << lua_tostring(_state, -1)
|
||||
);
|
||||
}
|
||||
|
||||
_scale = luaL_checknumber(_state, -1);
|
||||
}
|
||||
|
||||
} // namespace openspace
|
||||
54
modules/base/scale/luascale.h
Normal file
54
modules/base/scale/luascale.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
|
||||
* software and associated documentation files (the "Software"), to deal in the Software *
|
||||
* without restriction, including without limitation the rights to use, copy, modify, *
|
||||
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
|
||||
* permit persons to whom the Software is furnished to do so, subject to the following *
|
||||
* conditions: *
|
||||
* *
|
||||
* The above copyright notice and this permission notice shall be included in all copies *
|
||||
* or substantial portions of the Software. *
|
||||
* *
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
|
||||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
|
||||
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
|
||||
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
|
||||
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
|
||||
****************************************************************************************/
|
||||
|
||||
#ifndef __OPENSPACE_MODULE_BASE___LUASCALE___H__
|
||||
#define __OPENSPACE_MODULE_BASE___LUASCALE___H__
|
||||
|
||||
#include <openspace/scene/scale.h>
|
||||
|
||||
#include <openspace/properties/stringproperty.h>
|
||||
|
||||
#include <ghoul/lua/luastate.h>
|
||||
|
||||
namespace openspace {
|
||||
|
||||
namespace documentation { struct Documentation; }
|
||||
|
||||
class LuaScale : public Scale {
|
||||
public:
|
||||
LuaScale();
|
||||
LuaScale(const ghoul::Dictionary& dictionary);
|
||||
|
||||
void update(const UpdateData& data) override;
|
||||
|
||||
static documentation::Documentation Documentation();
|
||||
|
||||
private:
|
||||
properties::StringProperty _luaScriptFile;
|
||||
ghoul::lua::LuaState _state;
|
||||
};
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
#endif // __OPENSPACE_MODULE_BASE___LUASCALE___H__
|
||||
@@ -58,6 +58,8 @@ StaticScale::StaticScale()
|
||||
: _scaleValue(ScaleInfo, 1.0, 1.0, 1e6)
|
||||
{
|
||||
addProperty(_scaleValue);
|
||||
|
||||
_scaleValue.onChange([&](){ _scale = _scaleValue; });
|
||||
}
|
||||
|
||||
StaticScale::StaticScale(const ghoul::Dictionary& dictionary)
|
||||
@@ -68,8 +70,4 @@ StaticScale::StaticScale(const ghoul::Dictionary& dictionary)
|
||||
_scaleValue = static_cast<float>(dictionary.value<double>(ScaleInfo.identifier));
|
||||
}
|
||||
|
||||
double StaticScale::scaleValue() const {
|
||||
return _scaleValue;
|
||||
}
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
@@ -38,8 +38,6 @@ public:
|
||||
StaticScale();
|
||||
StaticScale(const ghoul::Dictionary& dictionary);
|
||||
|
||||
double scaleValue() const override;
|
||||
|
||||
static documentation::Documentation Documentation();
|
||||
|
||||
private:
|
||||
|
||||
141
modules/base/translation/luatranslation.cpp
Normal file
141
modules/base/translation/luatranslation.cpp
Normal file
@@ -0,0 +1,141 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
|
||||
* software and associated documentation files (the "Software"), to deal in the Software *
|
||||
* without restriction, including without limitation the rights to use, copy, modify, *
|
||||
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
|
||||
* permit persons to whom the Software is furnished to do so, subject to the following *
|
||||
* conditions: *
|
||||
* *
|
||||
* The above copyright notice and this permission notice shall be included in all copies *
|
||||
* or substantial portions of the Software. *
|
||||
* *
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
|
||||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
|
||||
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
|
||||
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
|
||||
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
|
||||
****************************************************************************************/
|
||||
|
||||
#include <modules/base/translation/luatranslation.h>
|
||||
|
||||
#include <openspace/documentation/documentation.h>
|
||||
#include <openspace/documentation/verifier.h>
|
||||
#include <openspace/util/updatestructures.h>
|
||||
|
||||
#include <ghoul/lua/ghoul_lua.h>
|
||||
#include <ghoul/lua/lua_helper.h>
|
||||
#include <ghoul/filesystem/filesystem.h>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace {
|
||||
static const openspace::properties::Property::PropertyInfo ScriptInfo = {
|
||||
"Script",
|
||||
"Script",
|
||||
"This value is the path to the Lua script that will be executed to compute the "
|
||||
"translation for this transformation. The script needs to define a function "
|
||||
"'translate' that takes the current simulation time in seconds past the J2000 "
|
||||
"epoch as the first argument, the current wall time as milliseconds past the "
|
||||
"J2000 epoch as the second argument and computes the translation."
|
||||
};
|
||||
} // namespace
|
||||
|
||||
namespace openspace {
|
||||
|
||||
documentation::Documentation LuaTranslation::Documentation() {
|
||||
using namespace documentation;
|
||||
return {
|
||||
"Lua Translation",
|
||||
"base_transform_translation_lua",
|
||||
{
|
||||
{
|
||||
"Type",
|
||||
new StringEqualVerifier("LuaTranslation"),
|
||||
Optional::No
|
||||
},
|
||||
{
|
||||
ScriptInfo.identifier,
|
||||
new StringVerifier,
|
||||
Optional::No,
|
||||
ScriptInfo.description
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
LuaTranslation::LuaTranslation()
|
||||
: _luaScriptFile(ScriptInfo)
|
||||
, _state(false)
|
||||
{
|
||||
addProperty(_luaScriptFile);
|
||||
|
||||
_luaScriptFile.onChange([&](){
|
||||
_fileHandle = std::make_unique<ghoul::filesystem::File>(_luaScriptFile);
|
||||
_fileHandle->setCallback([&](const ghoul::filesystem::File&){
|
||||
notifyObservers();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
LuaTranslation::LuaTranslation(const ghoul::Dictionary& dictionary)
|
||||
: LuaTranslation()
|
||||
{
|
||||
documentation::testSpecificationAndThrow(
|
||||
Documentation(),
|
||||
dictionary,
|
||||
"StaticTranslation"
|
||||
);
|
||||
|
||||
_luaScriptFile = absPath(dictionary.value<std::string>(ScriptInfo.identifier));
|
||||
}
|
||||
|
||||
void LuaTranslation::update(const UpdateData& data) {
|
||||
ghoul::lua::runScriptFile(_state, _luaScriptFile);
|
||||
|
||||
// Get the scaling function
|
||||
lua_getglobal(_state, "translation");
|
||||
bool isFunction = lua_isfunction(_state, -1);
|
||||
if (!isFunction) {
|
||||
LERRORC(
|
||||
"LuaScale",
|
||||
"Script '" << _luaScriptFile << "' does not have a function 'translation'"
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// First argument is the number of seconds past the J2000 epoch in ingame time
|
||||
lua_pushnumber(_state, data.time.j2000Seconds());
|
||||
|
||||
// Second argument is the number of milliseconds past the J2000 epoch in wallclock
|
||||
using namespace std::chrono;
|
||||
auto now = high_resolution_clock::now();
|
||||
lua_pushnumber(
|
||||
_state,
|
||||
duration_cast<milliseconds>(now.time_since_epoch()).count()
|
||||
);
|
||||
|
||||
// Execute the scaling function
|
||||
int success = lua_pcall(_state, 2, 3, 0);
|
||||
if (success != 0) {
|
||||
LERRORC(
|
||||
"LuaScale",
|
||||
"Error executing 'translation': " << lua_tostring(_state, -1)
|
||||
);
|
||||
}
|
||||
|
||||
double values[3];
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
values[i] = luaL_checknumber(_state, -1 - i);
|
||||
}
|
||||
|
||||
_positionValue = glm::make_vec3(values);
|
||||
}
|
||||
|
||||
} // namespace openspace
|
||||
59
modules/base/translation/luatranslation.h
Normal file
59
modules/base/translation/luatranslation.h
Normal file
@@ -0,0 +1,59 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
|
||||
* software and associated documentation files (the "Software"), to deal in the Software *
|
||||
* without restriction, including without limitation the rights to use, copy, modify, *
|
||||
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
|
||||
* permit persons to whom the Software is furnished to do so, subject to the following *
|
||||
* conditions: *
|
||||
* *
|
||||
* The above copyright notice and this permission notice shall be included in all copies *
|
||||
* or substantial portions of the Software. *
|
||||
* *
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
|
||||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
|
||||
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
|
||||
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
|
||||
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
|
||||
****************************************************************************************/
|
||||
|
||||
#ifndef __OPENSPACE_MODULE_BASE___LUATRANSLATION___H__
|
||||
#define __OPENSPACE_MODULE_BASE___LUATRANSLATION___H__
|
||||
|
||||
#include <openspace/scene/translation.h>
|
||||
|
||||
#include <openspace/properties/stringproperty.h>
|
||||
|
||||
#include <ghoul/filesystem/file.h>
|
||||
#include <ghoul/lua/luastate.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace openspace {
|
||||
|
||||
namespace documentation { struct Documentation; }
|
||||
|
||||
class LuaTranslation : public Translation {
|
||||
public:
|
||||
LuaTranslation();
|
||||
LuaTranslation(const ghoul::Dictionary& dictionary);
|
||||
|
||||
virtual void update(const UpdateData& data) override;
|
||||
|
||||
static documentation::Documentation Documentation();
|
||||
|
||||
private:
|
||||
properties::StringProperty _luaScriptFile;
|
||||
ghoul::lua::LuaState _state;
|
||||
|
||||
std::unique_ptr<ghoul::filesystem::File> _fileHandle;
|
||||
};
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
#endif // __OPENSPACE_MODULE_BASE___LUATRANSLATION___H__
|
||||
@@ -69,6 +69,8 @@ StaticTranslation::StaticTranslation()
|
||||
)
|
||||
{
|
||||
addProperty(_position);
|
||||
|
||||
_position.onChange([&](){ _positionValue = _position; });
|
||||
}
|
||||
|
||||
StaticTranslation::StaticTranslation(const ghoul::Dictionary& dictionary)
|
||||
@@ -83,8 +85,4 @@ StaticTranslation::StaticTranslation(const ghoul::Dictionary& dictionary)
|
||||
_position = dictionary.value<glm::dvec3>(PositionInfo.identifier);
|
||||
}
|
||||
|
||||
glm::dvec3 StaticTranslation::position() const {
|
||||
return _position;
|
||||
}
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
@@ -38,8 +38,6 @@ public:
|
||||
StaticTranslation();
|
||||
StaticTranslation(const ghoul::Dictionary& dictionary);
|
||||
|
||||
virtual glm::dvec3 position() const override;
|
||||
|
||||
static documentation::Documentation Documentation();
|
||||
|
||||
private:
|
||||
|
||||
@@ -72,12 +72,17 @@ std::unique_ptr<Scale> Scale::createFromDictionary(const ghoul::Dictionary& dict
|
||||
|
||||
Scale::Scale()
|
||||
: properties::PropertyOwner({ "Scale" })
|
||||
, _scale(1.0)
|
||||
{}
|
||||
|
||||
bool Scale::initialize() {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
double Scale::scaleValue() const {
|
||||
return _scale;
|
||||
}
|
||||
|
||||
void Scale::update(const UpdateData&) {}
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
@@ -73,6 +73,7 @@ std::unique_ptr<Translation> Translation::createFromDictionary(
|
||||
|
||||
Translation::Translation()
|
||||
: properties::PropertyOwner({ "Translation" })
|
||||
, _positionValue(glm::dvec3(0.0))
|
||||
{}
|
||||
|
||||
bool Translation::initialize() {
|
||||
@@ -81,6 +82,10 @@ bool Translation::initialize() {
|
||||
|
||||
void Translation::update(const UpdateData&) {}
|
||||
|
||||
glm::dvec3 Translation::position() const {
|
||||
return _positionValue;
|
||||
}
|
||||
|
||||
glm::dvec3 Translation::position(double time) {
|
||||
update({
|
||||
{},
|
||||
|
||||
Reference in New Issue
Block a user