python binding improvements

metadata
Wenzel Jakob 2011-08-20 17:09:59 -04:00
parent e58a0fa338
commit 9cf5f001e7
27 changed files with 170 additions and 27 deletions

View File

@ -295,6 +295,7 @@ template <typename T> struct TAABB {
* various convenience functions to query or change them.
*
* \ingroup libcore
* \ingroup libpython
*/
struct MTS_EXPORT_CORE AABB : public TAABB<Point> {
public:

View File

@ -26,6 +26,7 @@ MTS_NAMESPACE_BEGIN
/** \brief Bounding sphere data structure in three dimensions
*
* \ingroup libcore
* \ingroup libpython
*/
struct BSphere {
Point center;

View File

@ -130,8 +130,7 @@ public:
* errors in a system based on finite-precision arithmetic, it
* may be a good idea to tolerate at least a few samples without
* immediately rejecting the null hypothesis. This parameter
* sets this threshold. The default value is <number of
* sample> * 1e-4f
* sets this threshold. The default value is \c number-of-samples*1e-4f
*/
inline void setTolerance(Float tolerance) { m_tolerance = tolerance; }

View File

@ -33,6 +33,7 @@ MTS_NAMESPACE_BEGIN
* requests in the XML file.
*
* \ingroup libcore
* \ingroup libpython
*/
class MTS_EXPORT_CORE ConfigurableObject : public SerializableObject {
public:
@ -48,6 +49,9 @@ public:
/// Add a child (default implementation throws an error)
virtual void addChild(const std::string &name, ConfigurableObject *child);
/// Add an unnamed child
inline void addChild(ConfigurableObject *child) { addChild("", child); }
/** \brief Configure the object (called \a once after construction
and addition of all child ConfigurableObjects) */
virtual void configure();

View File

@ -97,6 +97,7 @@ typedef TVector2<float> Vector2f;
typedef TVector2<double> Vector2d;
/// \ingroup libpython
typedef TVector3<Float> Vector;
/// \ingroup libpython
typedef TVector3<Float> Vector3;
/// \ingroup libpython
typedef TVector3<int> Vector3i;
@ -115,6 +116,7 @@ typedef TPoint2<float> Point2f;
typedef TPoint2<double> Point2d;
/// \ingroup libpython
typedef TPoint3<Float> Point;
/// \ingroup libpython
typedef TPoint3<Float> Point3;
/// \ingroup libpython
typedef TPoint3<int> Point3i;

View File

@ -34,7 +34,6 @@ MTS_NAMESPACE_BEGIN
* <tt>\ref Utility</tt> classes for details
*
* \ingroup libcore
* \ingroup libpython
*/
class MTS_EXPORT_CORE Plugin {
typedef void *(*CreateInstanceFunc)(const Properties &props);
@ -83,7 +82,6 @@ private:
CreateUtilityFunc m_createUtility;
};
/**
* \brief The plugin manager is responsible for resolving and
* loading external plugins.
@ -105,7 +103,9 @@ public:
std::vector<std::string> getLoadedPlugins() const;
/**
* \brief Instantiate an object using a plugin
* \brief Instantiate a plugin, verify its type,
* and return the newly created instance.
*
* \param classType Expected type of the plugin. An
* exception will be thrown if it turns out not
* to derive from this class.
@ -118,6 +118,18 @@ public:
const Properties &props
);
/**
* \brief Instantiate a plugin and return the new
* instance (without verifying its type).
*
* \param props A \ref Properties instance containing
* all information required to find and construct
* the plugin.
*/
ConfigurableObject *createObject(
const Properties &props
);
/// Initializes the global plugin manager instance
static void staticInitialization();

View File

@ -422,6 +422,8 @@ public:
/// Add a child object
virtual void addChild(const std::string &string, ConfigurableObject *obj);
/// Add an unnamed child
inline void addChild(ConfigurableObject *child) { addChild("", child); }
/// Serialize this object to a stream
virtual void serialize(Stream *stream, InstanceManager *manager) const;

View File

@ -167,6 +167,8 @@ public:
/// Add a child ConfigurableObject
virtual void addChild(const std::string &name, ConfigurableObject *child);
/// Add an unnamed child
inline void addChild(ConfigurableObject *child) { addChild("", child); }
/// Serialize this camera to a binary data stream
virtual void serialize(Stream *stream, InstanceManager *manager) const;

View File

@ -83,6 +83,8 @@ public:
/// Add a child node
virtual void addChild(const std::string &name, ConfigurableObject *child);
/// Add an unnamed child
inline void addChild(ConfigurableObject *child) { addChild("", child); }
/// Configure the film
virtual void configure();

View File

@ -640,6 +640,7 @@ MTS_NAMESPACE_BEGIN
* can theoretically support any kind of shape. However, subclasses still
* need to provide the following signatures for a functional implementation:
*
* \code
* /// Return the total number of primitives
* inline size_type getPrimitiveCount() const;
*
@ -648,6 +649,7 @@ MTS_NAMESPACE_BEGIN
*
* /// Return the AABB of a primitive when clipped to another AABB
* inline AABB getClippedAABB(index_type primIdx, const AABBType &aabb) const;
* \endcode
*
* This class follows the "Curiously recurring template" design pattern
* so that the above functions can be inlined (in particular, no virtual

View File

@ -365,6 +365,8 @@ public:
/// Add a child (e.g. a medium reference) to this luminaire
void addChild(const std::string &name, ConfigurableObject *child);
/// Add an unnamed child
inline void addChild(ConfigurableObject *child) { addChild("", child); }
//! @}
// =============================================================

View File

@ -174,6 +174,8 @@ public:
/// Add a child ConfigurableObject
virtual void addChild(const std::string &name, ConfigurableObject *child);
/// Add an unnamed child
inline void addChild(ConfigurableObject *child) { addChild("", child); }
/// Return a string representation
virtual std::string toString() const = 0;

View File

@ -84,11 +84,13 @@ private:
* tree to be used for ray tracing. One additional function call
* must be implemented by subclasses:
*
* \code
* /// Check whether a primitive is intersected by the given ray.
* /// Some temporary space is supplied, which can be used to cache
* /// information about the intersection
* bool intersect(const Ray &ray, index_type idx,
* Float mint, Float maxt, Float &t, void *tmp);
* \endcode
*
* This class implements an epsilon-free version of the optimized ray
* traversal algorithm (TA^B_{rec}), which is explained in Vlastimil

View File

@ -124,6 +124,9 @@ public:
/// Add a child node to the scene
void addChild(const std::string &name, ConfigurableObject *child);
/// Add an unnamed child
inline void addChild(ConfigurableObject *child) { addChild("", child); }
/** \brief Configure this object (called _once_ after construction
and addition of all child ConfigurableObjects.) */
void configure();

View File

@ -367,6 +367,8 @@ public:
/// Add a child (e.g. a luminaire/sub surface integrator) to this shape
void addChild(const std::string &name, ConfigurableObject *child);
/// Add an unnamed child
inline void addChild(ConfigurableObject *child) { addChild("", child); }
//! @}
// =============================================================

View File

@ -88,7 +88,7 @@ public:
hkProps.setFloat("thickness", std::numeric_limits<Float>::infinity());
m_hk = static_cast<BSDF *> (PluginManager::getInstance()->
createObject(MTS_CLASS(BSDF), hkProps));
m_hk->addChild("", hg);
m_hk->addChild(hg);
Properties coatingProps(props);
coatingProps.setPluginName("coating");
@ -130,10 +130,10 @@ public:
m_configured = true;
m_hk->configure();
m_dipole->configure();
m_mixture->addChild("", m_hk);
m_mixture->addChild("", m_dipole);
m_mixture->addChild(m_hk);
m_mixture->addChild(m_dipole);
m_mixture->configure();
m_coating->addChild("", m_mixture);
m_coating->addChild(m_mixture);
m_coating->configure();
m_components.clear();

View File

@ -163,7 +163,28 @@ ConfigurableObject *PluginManager::createObject(const Class *classType,
props.getPluginName().c_str());
return object;
}
ConfigurableObject *PluginManager::createObject(const Properties &props) {
ConfigurableObject *object;
m_mutex->lock();
try {
ensurePluginLoaded(props.getPluginName());
object = m_plugins[props.getPluginName()]->createInstance(props);
} catch (std::runtime_error &e) {
m_mutex->unlock();
throw e;
} catch (std::exception &e) {
m_mutex->unlock();
throw e;
}
m_mutex->unlock();
if (object->getClass()->isAbstract())
Log(EError, "Error when loading plugin \"%s\": Identifies itself as an abstract class",
props.getPluginName().c_str());
return object;
}
std::vector<std::string> PluginManager::getLoadedPlugins() const {
std::vector<std::string> list;
m_mutex->lock();

View File

@ -10,6 +10,7 @@
#include <mitsuba/core/appender.h>
#include <mitsuba/core/bitmap.h>
#include <mitsuba/core/random.h>
#include <mitsuba/core/aabb.h>
using namespace mitsuba;
@ -236,6 +237,15 @@ static Point ray_eval(const Ray &ray, Float t) {
return ray(t);
}
void aabb_expandby_aabb(AABB *aabb, const AABB &aabb2) { aabb->expandBy(aabb2); }
void aabb_expandby_point(AABB *aabb, const Point &p) { aabb->expandBy(p); }
Float aabb_distanceto_aabb(AABB *aabb, const AABB &aabb2) { return aabb->distanceTo(aabb2); }
Float aabb_distanceto_point(AABB *aabb, const Point &p) { return aabb->distanceTo(p); }
Float aabb_sqrdistanceto_aabb(AABB *aabb, const AABB &aabb2) { return aabb->squaredDistanceTo(aabb2); }
Float aabb_sqrdistanceto_point(AABB *aabb, const Point &p) { return aabb->squaredDistanceTo(p); }
bool aabb_contains_aabb(AABB *aabb, const AABB &aabb2) { return aabb->contains(aabb2); }
bool aabb_contains_point(AABB *aabb, const Point &p) { return aabb->contains(p); }
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(fromLinearRGB_overloads, fromLinearRGB, 3, 4)
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(fromXYZ_overloads, fromXYZ, 3, 4)
@ -346,9 +356,20 @@ void export_core() {
.export_values();
BP_SETSCOPE(coreModule);
BP_CLASS(SerializableObject, Stream, bp::no_init)
BP_CLASS(SerializableObject, Object, bp::no_init)
.def("serialize", &SerializableObject::serialize);
ConfigurableObject *(ConfigurableObject::*cobject_get_parent)() = &ConfigurableObject::getParent;
void (ConfigurableObject::*cobject_add_child_1)(ConfigurableObject *) = &ConfigurableObject::addChild;
void (ConfigurableObject::*cobject_add_child_2)(const std::string &, ConfigurableObject *) = &ConfigurableObject::addChild;
BP_CLASS(ConfigurableObject, SerializableObject, bp::no_init)
.def("getParent", cobject_get_parent, BP_RETURN_VALUE)
.def("setParent", &ConfigurableObject::setParent)
.def("addChild", cobject_add_child_1)
.def("addChild", cobject_add_child_2)
.def("configure", &ConfigurableObject::configure);
Thread *(Thread::*thread_get_parent)() = &Thread::getParent;
BP_CLASS(Thread, Object, bp::no_init)
.def("getID", &Thread::getID)
@ -475,6 +496,17 @@ void export_core() {
.def("nextFloat", &Random::nextFloat)
.def("serialize", &Random::serialize);
ConfigurableObject *(PluginManager::*pluginmgr_createobject_1)(const Properties &) = &PluginManager::createObject;
ConfigurableObject *(PluginManager::*pluginmgr_createobject_2)(const Class *, const Properties &) = &PluginManager::createObject;
BP_CLASS(PluginManager, Object, bp::no_init)
.def("ensurePluginLoaded", &PluginManager::ensurePluginLoaded)
.def("getLoadedPlugins", &PluginManager::getLoadedPlugins)
.def("createObject", pluginmgr_createobject_1, BP_RETURN_VALUE)
.def("createObject", pluginmgr_createobject_2, BP_RETURN_VALUE)
.def("getInstance", &PluginManager::getInstance, BP_RETURN_VALUE)
.staticmethod("getInstance");
BP_STRUCT(Spectrum, bp::init<>())
.def("__init__", bp::make_constructor(spectrum_array_constructor))
.def(bp::init<Float>())
@ -733,6 +765,54 @@ void export_core() {
.def("eval", &ray_eval, BP_RETURN_VALUE)
.def("__str__", &Ray::toString);
bp::class_<BSphere>("BSphere", bp::init<>())
.def(bp::init<BSphere>())
.def(bp::init<Point, Float>())
.def(bp::init<Stream *>())
.def_readwrite("center", &BSphere::center)
.def_readwrite("radius", &BSphere::radius)
.def("isEmpty", &BSphere::isEmpty)
.def("expandBy", &BSphere::expandBy)
.def("contains", &BSphere::contains)
.def(bp::self == bp::self)
.def(bp::self != bp::self)
.def("rayIntersect", &AABB::rayIntersect)
.def("serialize", &AABB::serialize)
.def("__str__", &AABB::toString);
bp::class_<AABB>("AABB", bp::init<>())
.def(bp::init<AABB>())
.def(bp::init<Point>())
.def(bp::init<Point, Point>())
.def(bp::init<Stream *>())
.def_readwrite("min", &AABB::min)
.def_readwrite("max", &AABB::max)
.def("getSurfaceArea", &AABB::getSurfaceArea)
.def("getVolume", &AABB::getVolume)
.def("getCorner", &AABB::getCorner)
.def("overlaps", &AABB::overlaps)
.def("getCenter", &AABB::getCenter)
.def("reset", &AABB::reset)
.def("clip", &AABB::clip)
.def("isValid", &AABB::isValid)
.def("expandBy", &aabb_expandby_aabb)
.def("expandBy", &aabb_expandby_point)
.def("distanceTo", &aabb_distanceto_aabb)
.def("distanceTo", &aabb_distanceto_point)
.def("squaredDistanceTo", &aabb_sqrdistanceto_aabb)
.def("squaredDistanceTo", &aabb_sqrdistanceto_point)
.def("contains", &aabb_contains_aabb)
.def("contains", &aabb_contains_point)
.def("getLargestAxis", &AABB::getLargestAxis)
.def("getShortestAxis", &AABB::getShortestAxis)
.def("getExtents", &AABB::getExtents)
.def(bp::self == bp::self)
.def(bp::self != bp::self)
.def("rayIntersect", &AABB::rayIntersect)
.def("getBSphere", &AABB::getBSphere)
.def("serialize", &AABB::serialize)
.def("__str__", &AABB::toString);
bp::detail::current_scope = oldScope;
}

View File

@ -40,6 +40,7 @@
#define BP_IMPLEMENT_VECTOR_OPS(Name, Scalar, Size) \
Name ##_struct \
.def(bp::init<Stream *>()) \
.def(bp::init<Name>()) \
.def(bp::self != bp::self) \
.def(bp::self == bp::self) \
.def(-bp::self) \
@ -61,6 +62,7 @@
#define BP_IMPLEMENT_POINT_OPS(Name, Scalar, Size) \
Name ##_struct \
.def(bp::init<Stream *>()) \
.def(bp::init<Name>()) \
.def(bp::self != bp::self) \
.def(bp::self == bp::self) \
.def(Scalar() * bp::self) \

View File

@ -87,7 +87,7 @@ Texture *BSDF::ensureEnergyConservation(Texture *texture,
props.setFloat("scale", scale);
Texture *scaleTexture = static_cast<Texture *> (PluginManager::getInstance()->
createObject(MTS_CLASS(Texture), props));
scaleTexture->addChild("", texture);
scaleTexture->addChild(texture);
scaleTexture->configure();
return scaleTexture;
}
@ -117,9 +117,9 @@ std::pair<Texture *, Texture *> BSDF::ensureEnergyConservation(
createObject(MTS_CLASS(Texture), props));
Texture *scaleTexture2 = static_cast<Texture *> (PluginManager::getInstance()->
createObject(MTS_CLASS(Texture), props));
scaleTexture1->addChild("", tex1);
scaleTexture1->addChild(tex1);
scaleTexture1->configure();
scaleTexture2->addChild("", tex2);
scaleTexture2->addChild(tex2);
scaleTexture2->configure();
return std::make_pair(scaleTexture1, scaleTexture2);
}

View File

@ -288,7 +288,7 @@ void Scene::initialize() {
skyProps.setBoolean("extend", true);
ref<Luminaire> luminaire = static_cast<Luminaire *>(
PluginManager::getInstance()->createObject(MTS_CLASS(Luminaire), skyProps));
addChild("", luminaire);
addChild(luminaire);
luminaire->configure();
}

View File

@ -49,7 +49,7 @@ void Shape::configure() {
tracer implementation to work correctly. */
Properties props("diffuse");
props.setSpectrum("reflectance", Spectrum(0.0f));
addChild("", static_cast<BSDF *> (PluginManager::getInstance()->
addChild(static_cast<BSDF *> (PluginManager::getInstance()->
createObject(MTS_CLASS(BSDF), props)));
}
}

View File

@ -1776,7 +1776,7 @@ SceneContext::SceneContext(SceneContext *ctx) {
if (depth++ == 0)
scene->setIntegrator(integrator);
else
currentIntegrator->addChild("", integrator);
currentIntegrator->addChild(integrator);
currentIntegrator = integrator;
integratorList.push_back(integrator);
oldIntegrator = oldIntegrator->getSubIntegrator();
@ -1790,11 +1790,11 @@ SceneContext::SceneContext(SceneContext *ctx) {
getReconstructionFilter()->getProperties()));
rfilter->configure();
film->addChild("", rfilter);
film->addChild(rfilter);
film->configure();
sampler->configure();
camera->addChild("", sampler);
camera->addChild("", film);
camera->addChild(sampler);
camera->addChild(film);
camera->setViewTransform(oldCamera->getViewTransform());
camera->setFov(oldCamera->getFov());
camera->configure();

View File

@ -331,7 +331,7 @@ void RenderSettingsDialog::apply(SceneContext *ctx) {
m_icNode->putProperties(icProps);
ref<Integrator> ic = static_cast<Integrator *>
(pluginMgr->createObject(MTS_CLASS(Integrator), icProps));
ic->addChild("", integrator);
ic->addChild(integrator);
ic->configure();
integrator = ic;
}
@ -342,7 +342,7 @@ void RenderSettingsDialog::apply(SceneContext *ctx) {
m_aiNode->putProperties(aiProps);
ref<Integrator> ai = static_cast<Integrator *>
(pluginMgr->createObject(MTS_CLASS(Integrator), aiProps));
ai->addChild("", integrator);
ai->addChild(integrator);
ai->configure();
integrator = ai;
}
@ -354,7 +354,7 @@ void RenderSettingsDialog::apply(SceneContext *ctx) {
filmProps.setInteger("width", width, false);
filmProps.setInteger("height", height, false);
ref<Film> film = static_cast<Film *> (pluginMgr->createObject(MTS_CLASS(Film), filmProps));
film->addChild("", rFilter);
film->addChild(rFilter);
film->configure();
if (width != ctx->framebuffer->getWidth() ||
height != ctx->framebuffer->getHeight()) {
@ -367,8 +367,8 @@ void RenderSettingsDialog::apply(SceneContext *ctx) {
Properties cameraProps = oldCamera->getProperties();
ref<PerspectiveCamera> camera = static_cast<PerspectiveCamera *>
(pluginMgr->createObject(MTS_CLASS(Camera), cameraProps));
camera->addChild("", sampler);
camera->addChild("", film);
camera->addChild(sampler);
camera->addChild(film);
camera->setViewTransform(oldCamera->getViewTransform());
camera->setFov(oldCamera->getFov());
camera->setMedium(oldCamera->getMedium());

View File

@ -415,7 +415,7 @@ public:
mesh->incRef();
if (currentMaterial)
mesh->addChild("", currentMaterial);
mesh->addChild(currentMaterial);
m_meshes.push_back(mesh);
Log(EInfo, "%s: Loaded " SIZE_T_FMT " triangles, " SIZE_T_FMT
" vertices (merged " SIZE_T_FMT " vertices).", name.c_str(),

View File

@ -71,7 +71,7 @@ void ShapeGroup::addChild(const std::string &name, ConfigurableObject *child) {
ref<Shape> element = shape->getElement(index++);
if (element == NULL)
break;
addChild("", element);
addChild(element);
} while (true);
} else {
m_kdtree->addShape(shape);

View File

@ -47,6 +47,8 @@ public:
/// Add a child object
void addChild(const std::string &name, ConfigurableObject *child);
/// Add an unnamed child
inline void addChild(ConfigurableObject *child) { addChild("", child); }
/// Return whether or not the shape is a compound object
bool isCompound() const;