Skip to content

Commit

Permalink
Refs #4315. Export most of geometry namespace to new Python interface
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Dec 12, 2011
1 parent 7d7dca0 commit 06f9a81
Show file tree
Hide file tree
Showing 15 changed files with 324 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,5 @@ add_subdirectory ( api )

# Create an overall target
add_custom_target ( PythonInterface DEPENDS PythonKernelModule PythonAPIModule )
#add_custom_target ( PythonInterface DEPENDS PythonKernelModule PythonGeometryModule PythonAPIModule )
set_property ( TARGET PythonInterface PROPERTY FOLDER "MantidFramework/Python" )
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#############################################################################################
# _geometry Python module
#############################################################################################

set ( MODULE_TEMPLATE src/geometry.cpp.in )

set ( EXPORT_FILES
src/IComponent.cpp
src/ICompAssembly.cpp
src/IObjComponent.cpp
src/IDetector.cpp
src/Component.cpp
src/CompAssembly.cpp
src/ObjComponent.cpp
src/ObjCompAssembly.cpp
src/Detector.cpp
src/DetectorGroup.cpp
src/Instrument.cpp
)

set ( SRC_FILES
)

set ( INC_FILES
)

set ( PY_FILES
__init__.py
)

#############################################################################################
# Generate a source file from the export definitions and provided template
#############################################################################################
CREATE_MODULE ( ${MODULE_TEMPLATE} ${CMAKE_CURRENT_BINARY_DIR}/geometry.cpp EXPORT_FILES SRC_FILES )

#############################################################################################
# Copy over the pure Python files for the module
#############################################################################################
# Set the destination directory
set ( OUTPUT_DIR ${PYTHON_PKG_ROOT}/geometry )

set ( PYTHON_INSTALL_FILES "" )
foreach ( PYFILE ${PY_FILES} )
add_custom_command ( OUTPUT ${OUTPUT_DIR}/${PYFILE}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${PYFILE}
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/${PYFILE}
${OUTPUT_DIR}/${PYFILE}
)
set ( PYTHON_INSTALL_FILES ${PYTHON_INSTALL_FILES} ${OUTPUT_DIR}/${PYFILE} )
endforeach ( PYFILE )

#############################################################################################
# Create the target for this directory
#############################################################################################

add_library ( PythonGeometryModule ${SRC_FILES} ${BOOST_PYTHON_SRC} ${INC_FILES} ${PYTHON_INSTALL_FILES} )
set_python_properties( PythonGeometryModule _geometry )
set_target_output_directory ( PythonGeometryModule ${OUTPUT_DIR} .pyd )
# Add the required dependencies
target_link_libraries ( PythonGeometryModule ${PYTHON_DEPS} )
15 changes: 15 additions & 0 deletions Code/Mantid/Framework/PythonInterface/mantid/geometry/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
mantid.geometry
===============
Defines Python objects that wrap the C++ Geometry namespace.
"""

###############################################################################
# The _api C extension depends on exports defined in the _kernel extension
###############################################################################
from mantid.kernel import dlopen as _dlopen
flags = _dlopen.setup_dlopen() # Ensure the library is open with the correct flags
from _geometry import *
_dlopen.restore_flags(flags)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "MantidGeometry/Instrument/CompAssembly.h"
#include <boost/python/class.hpp>

using Mantid::Geometry::CompAssembly;
using Mantid::Geometry::ICompAssembly;
using Mantid::Geometry::Component;
using namespace boost::python;

/**
* Enables boost.python to automatically "cast" an object up to the
* appropriate CompAssembly leaf type
*/
void export_CompAssembly()
{
class_<CompAssembly, bases<ICompAssembly, Component>, boost::noncopyable>("CompAssembly", no_init)
;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "MantidGeometry/Instrument/Component.h"
#include <boost/python/class.hpp>
#include <boost/python/overloads.hpp>

using Mantid::Geometry::Component;
using Mantid::Geometry::IComponent;
using namespace boost::python;

namespace
{
// Default parameter function overloads
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(Component_getParameterNames,Component::getParameterNames,0,1);
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(Component_hasParameter,Component::hasParameter,1,2);
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(Component_getNumberParameter,Component::getNumberParameter,1,2);
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(Component_getPositionParameter,Component::getPositionParameter,1,2);
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(Component_getRotationParameter,Component::getRotationParameter,1,2);
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(Component_getStringParameter,Component::getStringParameter,1,2);
}

void export_Component()
{
class_<Component, bases<IComponent>, boost::noncopyable>("Component", no_init)
.def("get_parameter_names", &Component::getParameterNames, Component_getParameterNames())
.def("has_parameter", &Component::hasParameter, Component_hasParameter())
.def("get_number_parameter", &Component::getNumberParameter, Component_getNumberParameter())
.def("get_position_parameter", &Component::getPositionParameter, Component_getPositionParameter())
.def("get_rotation_parameter", &Component::getRotationParameter, Component_getRotationParameter())
.def("get_string_parameter", &Component::getStringParameter, Component_getStringParameter())
;

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "MantidGeometry/Instrument/Detector.h"
#include <boost/python/class.hpp>

using Mantid::Geometry::Detector;
using Mantid::Geometry::IDetector;
using Mantid::Geometry::ObjComponent;
using namespace boost::python;

/**
* Enables boost.python to automatically "cast" an object up to the
* appropriate Detector leaf type
*/
void export_Detector()
{
class_<Detector, bases<IDetector, ObjComponent>, boost::noncopyable>("Detector_", no_init)
;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "MantidGeometry/Instrument/DetectorGroup.h"
#include <boost/python/class.hpp>

using Mantid::Geometry::DetectorGroup;
using Mantid::Geometry::IDetector;
using namespace boost::python;

void export_DetectorGroup()
{
class_<DetectorGroup, bases<IDetector>, boost::noncopyable>("DetectorGroup", no_init)
.def("get_detector_ids", &DetectorGroup::getDetectorIDs, "Returns the list of detector IDs within this group")
;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "MantidGeometry/ICompAssembly.h"
#include <boost/python/class.hpp>
#include <boost/python/register_ptr_to_python.hpp>

using Mantid::Geometry::ICompAssembly;
using Mantid::Geometry::IComponent;
using namespace boost::python;

void export_ICompAssembly()
{

register_ptr_to_python<boost::shared_ptr<ICompAssembly> >();

class_<ICompAssembly, boost::python::bases<IComponent>, boost::noncopyable>("ICompAssembly", no_init)
.def("nelements", &ICompAssembly::nelements, "Returns the number of elements in the assembly")
.def("__getitem__", &ICompAssembly::operator[], "Return the component at the given index")
;

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "MantidGeometry/IComponent.h"
#include <boost/python/class.hpp>
#include <boost/python/register_ptr_to_python.hpp>

using Mantid::Geometry::IComponent;
using namespace boost::python;

void export_IComponent()
{
register_ptr_to_python<boost::shared_ptr<IComponent> >();

class_<IComponent, boost::noncopyable>("IComponent", no_init)
.def("get_pos", &IComponent::getPos, "Returns the absolute position of the component")
.def("get_distance", &IComponent::getDistance, "Returns the distance, in metres, between this and the given component")
.def("get_name", &IComponent::getName, "Returns the name of the component")
.def("type", &IComponent::type, "Returns the type of the component represented as a string")
;


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "MantidGeometry/IDetector.h"
#include <boost/python/class.hpp>
#include <boost/python/register_ptr_to_python.hpp>

using Mantid::Geometry::IDetector;
using Mantid::Geometry::IObjComponent;
using namespace boost::python;

void export_IDetector()
{
register_ptr_to_python<boost::shared_ptr<IDetector> >();

class_<IDetector, bases<IObjComponent>, boost::noncopyable>("IDetector", no_init)
.def("getID", &IDetector::getID, "Returns the detector ID")
.def("is_masked", &IDetector::isMasked, "Returns the value of the masked flag. True means ignore this detector")
.def("is_monitor", &IDetector::isMonitor, "Returns True if the detector is marked as a monitor in the IDF")
.def("solid_angle", &IDetector::solidAngle, "Return the solid angle in steradians between this detector and an observer")
.def("get_twotheta", &IDetector::getTwoTheta, "Calculate the angle between this detector, another component and an axis")
.def("get_phi", &IDetector::getPhi, "Returns the azimuthal angle of this detector")
;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "MantidGeometry/IObjComponent.h"
#include <boost/python/class.hpp>
#include <boost/python/register_ptr_to_python.hpp>

using Mantid::Geometry::IObjComponent;
using Mantid::Geometry::IComponent;
using namespace boost::python;

void export_IObjComponent()
{
register_ptr_to_python<boost::shared_ptr<IObjComponent> >();

class_< IObjComponent, boost::python::bases<IComponent>, boost::noncopyable>("IObjComponent", no_init)
;

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "MantidGeometry/Instrument.h"
#include <boost/python/class.hpp>
#include <boost/python/register_ptr_to_python.hpp>

using Mantid::Geometry::Instrument;
using Mantid::Geometry::CompAssembly;
using Mantid::Geometry::IObjComponent;
using Mantid::Geometry::IDetector;
using Mantid::Geometry::IComponent;
using Mantid::detid_t;
using namespace boost::python;

void export_Instrument()
{
register_ptr_to_python<boost::shared_ptr<Instrument> >();

class_<Instrument, bases<CompAssembly>, boost::noncopyable>("Instrument", no_init)
.def("getSample", (boost::shared_ptr<IObjComponent> (Instrument::*)())&Instrument::getSample,
"Return the object that represents the sample")
.def("getSource", (boost::shared_ptr<IObjComponent> (Instrument::*)())&Instrument::getSource,
"Return the object that represents the source")
.def("getComponentByName", (boost::shared_ptr<IComponent> (Instrument::*)(const std::string&))&Instrument::getComponentByName,
"Returns the named component")
.def("getDetector", (boost::shared_ptr<IDetector> (Instrument::*)(const detid_t&)const)&Instrument::getDetector,
"Returns the dector with the given ID")
;

;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "MantidGeometry/Instrument/ObjCompAssembly.h"
#include <boost/python/class.hpp>
#include <boost/python/register_ptr_to_python.hpp>

using Mantid::Geometry::ObjCompAssembly;
using Mantid::Geometry::ICompAssembly;
using Mantid::Geometry::ObjComponent;
using namespace boost::python;

void export_ObjCompAssembly()
{
register_ptr_to_python<boost::shared_ptr<ObjCompAssembly> >();

class_<ObjCompAssembly, boost::python::bases<ICompAssembly, ObjComponent>, boost::noncopyable>("IObjCompAssembly", no_init)
.def("nelements", &ObjCompAssembly::nelements)
.def("__getitem__", &ObjCompAssembly::operator[])
;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "MantidGeometry/Instrument/ObjComponent.h"
#include <boost/python/class.hpp>

using Mantid::Geometry::ObjComponent;
using Mantid::Geometry::IObjComponent;
using Mantid::Geometry::Component;
using namespace boost::python;

void export_ObjComponent()
{
class_<ObjComponent, boost::python::bases<IObjComponent, Component>, boost::noncopyable>("ObjComponent", no_init)
;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <boost/python/module.hpp>
#include <boost/python/docstring_options.hpp>
#include <boost/python/def.hpp>

// See http://docs.scipy.org/doc/numpy/reference/c-api.array.html#PY_ARRAY_UNIQUE_SYMBOL
#define PY_ARRAY_UNIQUE_SYMBOL GEOMETRY_ARRAY_API
#include <numpy/arrayobject.h>

using boost::python::def;

// Forward declare
@EXPORT_DECLARE@

BOOST_PYTHON_MODULE(_geometry)
{
// Doc string options - User defined, python arguments, C++ call signatures
boost::python::docstring_options docstrings(true, true, false);
// Import numpy
_import_array();

@EXPORT_FUNCTIONS@
}

0 comments on commit 06f9a81

Please sign in to comment.