Commit 4f6b4ba6 authored by Artur Grunau's avatar Artur Grunau
Browse files

Wrap several CAMPVis classes with SWIG

To test the feasibility of using SWIG to generate Lua bindings for
CAMPVis several classes have been wrapped with SWIG interface files.

If version 2.0.11 of SWIG is used to generate bindings, it must be
patched to fix wrapper compilation errors:
https://github.com/badshah400/swig/commit/ea4a4e435f721380555fea039bd5dc758eda43ae

References #1
parent 82a6d86c
......@@ -29,3 +29,6 @@ moc_*.cxx_parameters
# TBB library in ext/
ext/tbb
# SWIG wrapper code
scripting/*LUA_wrap.cxx
......@@ -14,6 +14,11 @@ ELSE(LUA_FOUND)
MESSAGE(FATAL_ERROR "Lua not found!")
ENDIF(LUA_FOUND)
# SWIG
FIND_PACKAGE(SWIG REQUIRED)
MESSAGE(STATUS "* Found SWIG")
INCLUDE(${SWIG_USE_FILE})
FILE(GLOB CampvisScriptingSources
*.cpp
*.c
......@@ -23,7 +28,23 @@ FILE(GLOB CampvisScriptingHeaders
*.h
)
SET_SOURCE_FILES_PROPERTIES(campvis.i PROPERTIES CPLUSPLUS ON)
SWIG_ADD_MODULE(campvis lua campvis.i)
SWIG_LINK_LIBRARIES(campvis campvis-core tgt ${CampvisGlobalExternalLibs})
SET_SOURCE_FILES_PROPERTIES(vis.i PROPERTIES CPLUSPLUS ON)
SWIG_ADD_MODULE(vis lua vis.i)
SWIG_LINK_LIBRARIES(vis campvis-core campvis-modules tgt ${CampvisGlobalExternalLibs})
ADD_EXECUTABLE(campvis-scripting ${CampvisScriptingSources} ${CampvisScriptingHeaders})
ADD_DEFINITIONS(${CampvisGlobalDefinitions} ${CampvisScriptingDefinitions})
INCLUDE_DIRECTORIES(${CampvisGlobalIncludeDirs})
TARGET_LINK_LIBRARIES(campvis-scripting campvis-core tgt ${CampvisGlobalExternalLibs})
# Copy Lua test scripts into the target directory
ADD_CUSTOM_COMMAND(
TARGET campvis-scripting
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy "script.lua" ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
)
SET_SOURCE_FILES_PROPERTIES(testapp.cpp PROPERTIES OBJECT_DEPENDS "scripting/script.lua")
%module campvis
%include "std_string.i"
%{
#include "ext/tgt/vector.h"
#include "core/properties/genericproperty.h"
#include "core/properties/numericproperty.h"
#include "core/properties/datanameproperty.h"
#include "core/pipeline/abstractprocessor.h"
%}
namespace tgt {
template<class T>
struct Vector2 {
Vector2(T t);
Vector2(T t1, T t2);
static Vector2<T> zero;
};
%template(ivec2) Vector2<int>;
typedef Vector2<int> ivec2;
}
namespace campvis {
class AbstractProcessor {
public:
enum InvalidationLevel {
VALID = 0,
INVALID_RESULT = 1 << 0,
INVALID_SHADER = 1 << 1,
INVALID_PROPERTIES = 1 << 2,
FIRST_FREE_TO_USE_INVALIDATION_LEVEL = 1 << 3
};
virtual const std::string getName() const = 0;
};
class StringProperty {
public:
StringProperty(const std::string& name, const std::string& title, const std::string& value,
int invalidationLevel = AbstractProcessor::INVALID_RESULT);
~StringProperty();
const std::string& getValue();
void setValue(const std::string& value);
};
class DataNameProperty : public StringProperty {
public:
enum DataAccessInfo {
READ,
WRITE
};
DataNameProperty(const std::string& name, const std::string& title, const std::string& value,
DataAccessInfo access, int invalidationLevel = AbstractProcessor::INVALID_RESULT);
virtual ~DataNameProperty();
};
class DataContainer {
public:
DataContainer(const std::string& name);
~DataContainer();
};
template<typename T>
class NumericProperty {
public:
NumericProperty(
const std::string& name,
const std::string& title,
const T& value,
const T& minValue,
const T& maxValue,
const T& stepValue = T(1),
int invalidationLevel = AbstractProcessor::INVALID_RESULT);
virtual ~NumericProperty();
};
%template(IVec2Property) NumericProperty< tgt::Vector2<int> >;
typedef NumericProperty< tgt::Vector2<int> > IVec2Property;
}
-- Start
-- Script: script.lua
print("I am using Lua from within C")
require("campvis")
require("vis")
print(campvis.StringProperty)
print(vis.MhdImageReader)
local nt = campvis.StringProperty("a", "b", "c", campvis.AbstractProcessor_INVALID_RESULT)
print(nt:getValue())
nt:setValue("d")
print(nt:getValue())
local mir = vis.MhdImageReader()
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
......@@ -6,15 +6,22 @@ extern "C" {
int main()
{
int s = 0;
lua_State* L = luaL_newstate();
// load the libs
luaL_openlibs(L);
// run a Lua script here
luaL_dofile(L, "script.lua");
// run a Lua script here; true is returned if there were errors
if (luaL_dofile(L, "script.lua")) {
const char* msg = lua_tostring(L, -1);
if (msg == nullptr)
printf("(error object is not a string)");
else
printf("%s", msg);
lua_pop(L, 1);
}
printf("\nI am done with Lua in C++.\n");
......
%module vis
%include "std_string.i"
%include "campvis.i"
%{
#include "modules/io/processors/mhdimagereader.h"
#include "modules/preprocessing/processors/glimageresampler.h"
#include "modules/vis/processors/volumeexplorer.h"
%}
%inline %{
int first(tgt::ivec2 &v) {
return v[0];
}
%}
namespace campvis {
class MhdImageReader {
public:
MhdImageReader();
~MhdImageReader();
%immutable;
campvis::StringProperty p_url;
campvis::DataNameProperty p_targetImageID;
%mutable;
};
class GlImageResampler {
public:
GlImageResampler(campvis::IVec2Property* viewportSizeProp);
~GlImageResampler();
%immutable;
campvis::DataNameProperty p_outputImage;
%mutable;
};
class VolumeExplorer {
public:
VolumeExplorer(campvis::IVec2Property* viewportSizeProp);
~VolumeExplorer();
%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