bindings/python/python-simplex.h
author Dmitriy Morozov <dmitriy@mrzv.org>
Mon, 13 Apr 2009 20:38:46 -0700
branchdev
changeset 127 406c6cc00b9c
parent 104 2cc1db3b98c6
permissions -rw-r--r--
Changes in Python Bindings: * the exposed C++ simplex has Empty data * Python Simplex constructor with data as well as data-based comparison functions are implemented in pure Python

#ifndef __PYTHON_SIMPLEX_H__
#define __PYTHON_SIMPLEX_H__

#include <topology/simplex.h>

#include <boost/python.hpp>
#include <boost/python/stl_iterator.hpp>
namespace bp = boost::python;


/**
 * SimplexVD is a base class for Python simplices (it's exposed to python as Simplex)
 *
 * SimplexObject is the representation of Python simplices in C++; i.e. it wraps bp::object and exposes a simplex-like interface.
 */
typedef                             int                                         Vertex;
// typedef                             double                                      Data;
typedef                             Empty<>                                     Data;
typedef                             Simplex<Vertex, Data>                       SimplexVD;


// Wrapper around bp::object that acts like a simplex
class SimplexObject: public bp::object
{
    public:
        typedef                 SimplexObject                                   Self;
        typedef                 bp::object                                      Parent;
        typedef                 SimplexVD::BoundaryIterator                     BoundaryIterator;


                                SimplexObject(Parent o = Parent()): Parent(o)   {}

        BoundaryIterator        boundary_begin() const                          { return bp::extract<const SimplexVD&>(*this)().boundary_begin(); }
        BoundaryIterator        boundary_end() const                            { return bp::extract<const SimplexVD&>(*this)().boundary_end(); }

                                operator SimplexVD() const                      { return bp::extract<const SimplexVD&>(*this); }
                                operator bp::object() const                     { return *this; }

        bp::object              getattribute(const char* name) const            { return this->attr(name); }

        class                   VertexComparison: public SimplexVD::VertexComparison
        {
            public:
                typedef         Self                                            first_argument_type;
                typedef         Self                                            second_argument_type;
                typedef         bool                                            result_type;

                bool            operator()(const SimplexObject& s1, const SimplexObject& s2) const  
                { return SimplexVD::VertexComparison::operator()(bp::extract<const SimplexVD&>(s1), bp::extract<const SimplexVD&>(s2)); }
        };
};

struct SimplexObjectToSimplexVD
{
    static PyObject* convert (const SimplexObject& so)
    {
        return (PyObject*) &so;
    }
};

#endif