Commit 31df41df authored by Artur Grunau's avatar Artur Grunau
Browse files

Lua and C++ parts of a pipeline can now interact

Lua functions invoked by LuaPipeline had no access to the pipeline
instance and, as a result, couldn't configure or modify it in any way.

In order to make it possible to actually define pipelines in Lua, each
script can now access the C++ pipeline object it's supposed to augment
via a global variable named instance. This variable is injected into the
script by LuaPipeline and can be used to call standard pipeline methods
(e.g. addProcessor).

The functionality described has been tested by implementing a pipeline
constructor that creates and registers several processors.

References #1
parent 473dba97
......@@ -29,6 +29,3 @@ moc_*.cxx_parameters
# TBB library in ext/
ext/tbb
# SWIG wrapper code
scripting/*LUA_wrap.cxx
# SWIG wrapper code
/*LUA_wrap.cxx
# Header with SWIG run-time functions
/swigluarun.h
......@@ -41,6 +41,13 @@ ADD_DEFINITIONS(${CampvisGlobalDefinitions} ${CampvisScriptingDefinitions})
INCLUDE_DIRECTORIES(${CampvisGlobalIncludeDirs})
TARGET_LINK_LIBRARIES(campvis-scripting campvis-core campvis-modules tgt ${CampvisGlobalExternalLibs})
# Generate header with SWIG run-time functions
ADD_CUSTOM_COMMAND(
TARGET campvis-scripting
PRE_BUILD
COMMAND ${SWIG_EXECUTABLE} -c++ -lua -external-runtime "swigluarun.h"
)
# Copy Lua test scripts into the target directory
ADD_CUSTOM_COMMAND(
TARGET campvis-scripting
......
......@@ -6,6 +6,8 @@
#include "core/properties/numericproperty.h"
#include "core/properties/datanameproperty.h"
#include "core/pipeline/abstractprocessor.h"
#include "core/pipeline/autoevaluationpipeline.h"
#include "core/pipeline/visualizationprocessor.h"
%}
namespace tgt {
......@@ -32,9 +34,22 @@ namespace campvis {
FIRST_FREE_TO_USE_INVALIDATION_LEVEL = 1 << 3
};
virtual const std::string getName() const = 0;
const std::string getName() const = 0;
};
class VisualizationProcessor : public AbstractProcessor {
public:
explicit VisualizationProcessor(IVec2Property* viewportSizeProp);
~VisualizationProcessor();
};
%nodefaultctor AutoEvaluationPipeline;
class AutoEvaluationPipeline {
public:
virtual void addProcessor(AbstractProcessor* processor);
virtual ~AutoEvaluationPipeline();
};
class StringProperty {
public:
......@@ -81,3 +96,13 @@ namespace campvis {
%template(IVec2Property) NumericProperty< tgt::Vector2<int> >;
typedef NumericProperty< tgt::Vector2<int> > IVec2Property;
}
%luacode {
function campvis.newPipeline (o)
o = o or {} -- create object if user does not provide one
setmetatable(o, {__index = instance})
return o
end
print("Module campvis loaded")
}
......@@ -5,6 +5,8 @@ extern "C" {
#include "lauxlib.h"
}
#include "swigluarun.h"
namespace campvis {
......@@ -29,6 +31,16 @@ namespace campvis {
// load the libs
luaL_openlibs(_luaState);
luaL_dostring(_luaState, "require(\"campvis\")");
swig_type_info* autoEvaluationPipelineType = SWIG_TypeQuery(_luaState, "campvis::AutoEvaluationPipeline *");
if (autoEvaluationPipelineType == nullptr)
printf("SWIG wrapper for campvis::AutoEvaluationPipeline not found");
else {
SWIG_NewPointerObj(_luaState, this, autoEvaluationPipelineType, 0);
lua_setglobal(_luaState, "instance");
}
// run a Lua script here; true is returned if there were errors
if (luaL_dofile(_luaState, scriptPath.c_str())) {
......@@ -46,7 +58,13 @@ namespace campvis {
if (!lua_istable(_luaState, -1))
printf("No valid Lua pipeline found (pipeline is %s)\n", lua_typename(_luaState, lua_type(_luaState, -1)));
else {
lua_getfield(_luaState, -1, "ctor");
lua_getglobal(_luaState, "pipeline");
callLuaFunc(_luaState, 1, 0);
}
// Pop the pipeline table
lua_pop(_luaState, 1);
}
......
-- Start
-- Script: script.lua
require("campvis")
require("vis")
print(campvis.StringProperty)
print(vis.MhdImageReader)
pipeline = campvis.newPipeline()
pipeline = {}
function pipeline:ctor()
print("I'm being constructed!")
self.mir = vis.MhdImageReader()
self.addProcessor(instance, self.mir)
local iv = campvis.ivec2(50, 10)
print(vis.first(iv))
print(vis.first(campvis.ivec2_zero))
local ivp = campvis.IVec2Property("a", "b", iv, campvis.ivec2_zero, campvis.ivec2(100, 30))
self.ve = vis.VolumeExplorer(ivp)
self.gir = vis.GlImageResampler(ivp)
--self.addProcessor(instance, self.gir)
--self.addProcessor(instance, self.ve)
end
function pipeline:init()
print("I'm being inited!")
self.mir = vis.MhdImageReader()
end
function pipeline:deinit()
......@@ -21,15 +32,3 @@ local nt = campvis.StringProperty("a", "b", "c", campvis.AbstractProcessor_INVAL
print(nt:getValue())
nt:setValue("d")
print(nt:getValue())
local iv = campvis.ivec2(50, 10)
print(vis.first(iv))
print(vis.first(campvis.ivec2_zero))
local ivp = campvis.IVec2Property("a", "b", iv, campvis.ivec2_zero, campvis.ivec2(100, 30))
local ve = vis.VolumeExplorer(ivp)
local gir = vis.GlImageResampler(ivp)
print("I am using Lua from within C++")
-- End
......@@ -17,7 +17,5 @@ int main()
p->init();
p->deinit();
printf("\nI am done with Lua in C++.\n");
return 0;
}
......@@ -15,32 +15,44 @@ int first(tgt::ivec2 &v) {
namespace campvis {
class MhdImageReader {
class AbstractImageReader : public AbstractProcessor {
public:
AbstractImageReader();
~AbstractImageReader();
};
class MhdImageReader : public AbstractImageReader {
public:
MhdImageReader();
~MhdImageReader();
const std::string getName() const;
%immutable;
campvis::StringProperty p_url;
campvis::DataNameProperty p_targetImageID;
%mutable;
};
class GlImageResampler {
class GlImageResampler : public VisualizationProcessor {
public:
GlImageResampler(campvis::IVec2Property* viewportSizeProp);
~GlImageResampler();
const std::string getName() const;
%immutable;
campvis::DataNameProperty p_outputImage;
%mutable;
};
class VolumeExplorer {
class VolumeExplorer : public VisualizationProcessor {
public:
VolumeExplorer(campvis::IVec2Property* viewportSizeProp);
~VolumeExplorer();
const std::string getName() const;
%immutable;
campvis::DataNameProperty p_outputImage;
%mutable;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment