Commit aca44330 authored by Dennis Gläser's avatar Dennis Gläser
Browse files

[python] add first bindings for occ utils

parent 2e555f1e
add_subdirectory(common)
add_subdirectory(geometry)
add_subdirectory(occutilities)
add_subdirectory(precision)
add_subdirectory(sampling)
install(FILES
breputilities.hh
brepwrapper.hh
brepwrappers.hh
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/frackit/python/occutilities)
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*****************************************************************************
* See the file COPYING for full copying permissions. *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*****************************************************************************/
#ifndef FRACKIT_PYTHON_BREP_UTILITIES_HH
#define FRACKIT_PYTHON_BREP_UTILITIES_HH
#include <string>
#include <type_traits>
#include <BRepTools.hxx>
#include <frackit/geometry/point.hh>
#include <frackit/geometry/segment.hh>
#include <frackit/geometry/circle.hh>
#include <frackit/geometry/ellipse.hh>
#include <frackit/geometry/ellipsearc.hh>
#include <frackit/geometry/quadrilateral.hh>
#include <frackit/geometry/disk.hh>
#include <frackit/geometry/box.hh>
#include <frackit/geometry/cylinder.hh>
#include <frackit/geometry/cylindersurface.hh>
#include <frackit/occ/breputilities.hh>
#include <frackit/occ/gputilities.hh>
#include <frackit/occ/geomutilities.hh>
#include "brepwrapper.hh"
namespace Frackit::Python {
namespace OCCUtilities {
template<class Geometry>
auto getShape(const Geometry& geometry)
-> BRepWrapper< std::decay_t<decltype(Frackit::OCCUtilities::getShape(geometry))> >
{
using ShapeType = std::decay_t<decltype(Frackit::OCCUtilities::getShape(geometry))>;
using WrapperType = BRepWrapper<ShapeType>;
return WrapperType(Frackit::OCCUtilities::getShape(geometry));
}
ShapeWrapper readShape(const std::string& fileName)
{ return ShapeWrapper(Frackit::OCCUtilities::readShape(fileName)); }
template<class WrappedShape>
void write(const WrappedShape& wrappedShape, const std::string& fileName)
{
BRepTools::Write(wrappedShape.get(), fileName.c_str());
}
} // end namespace OCCUtilities
template<class ctype>
void registerBRepUtilities(pybind11::module& module)
{
namespace py = pybind11;
// register point to shape conversions
using P1 = Point<ctype, 1>; module.def("getShape", &OCCUtilities::getShape<P1>, "Returns the OCC BRep of a 1d point");
using P2 = Point<ctype, 2>; module.def("getShape", &OCCUtilities::getShape<P2>, "Returns the OCC BRep of a 2d point");
using P3 = Point<ctype, 3>; module.def("getShape", &OCCUtilities::getShape<P3>, "Returns the OCC BRep of a 3d point");
// register segment to shape conversions
using S1 = Segment<ctype, 1>; module.def("getShape", &OCCUtilities::getShape<S1>, "Returns the OCC BRep of a 1d segment");
using S2 = Segment<ctype, 2>; module.def("getShape", &OCCUtilities::getShape<S2>, "Returns the OCC BRep of a 2d segment");
using S3 = Segment<ctype, 3>; module.def("getShape", &OCCUtilities::getShape<S3>, "Returns the OCC BRep of a 3d segment");
// register circle/ellipse to shape conversions
module.def("getShape", &OCCUtilities::getShape<Circle<ctype, 3>>, "Returns the OCC BRep of a 3d circle");
module.def("getShape", &OCCUtilities::getShape<Ellipse<ctype, 3>>, "Returns the OCC BRep of a 3d ellipse");
module.def("getShape", &OCCUtilities::getShape<EllipseArc<ctype, 3>>, "Returns the OCC BRep of a 3d ellipse arc");
// register quadrilateral to shape conversions
module.def("getShape", &OCCUtilities::getShape<Quadrilateral<ctype, 3>>, "Returns the OCC BRep of a 3d quadrilateral");
// register further geometry to shape conversions
module.def("getShape", &OCCUtilities::getShape<Disk<ctype>>, "Returns the OCC BRep of a disk");
module.def("getShape", &OCCUtilities::getShape<Box<ctype>>, "Returns the OCC BRep of a box");
module.def("getShape", &OCCUtilities::getShape<Cylinder<ctype>>, "Returns the OCC BRep of a cylinder");
module.def("getShape", &OCCUtilities::getShape<CylinderSurface<ctype>>, "Returns the OCC BRep of the lateral surface of a cylinder");
// register read-in of shapes from a file
module.def("readShape", &OCCUtilities::readShape, "Reads in the shapes from a file");
// register write function for wrapped shapes
using namespace OCCUtilities;
module.def("write", &OCCUtilities::write<ShapeWrapper>, "writes a wrapped shape to a BRep file");
module.def("write", &OCCUtilities::write<VertexWrapper>, "writes a wrapped vertex shape to a BRep file");
module.def("write", &OCCUtilities::write<EdgeWrapper>, "writes a wrapped edge shape to a BRep file");
module.def("write", &OCCUtilities::write<WireWrapper>, "writes a wrapped wire shape to a BRep file");
module.def("write", &OCCUtilities::write<FaceWrapper>, "writes a wrapped face shape to a BRep file");
module.def("write", &OCCUtilities::write<ShellWrapper>, "writes a wrapped shell shape to a BRep file");
module.def("write", &OCCUtilities::write<SolidWrapper>, "writes a wrapped solid shape to a BRep file");
module.def("write", &OCCUtilities::write<CompoundWrapper>, "writes a wrapped compound shape to a BRep file");
}
} // end namespace Frackit::Python
#endif
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*****************************************************************************
* See the file COPYING for full copying permissions. *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*****************************************************************************/
#ifndef FRACKIT_PYTHON_OCC_BREP_WRAPPER_HH
#define FRACKIT_PYTHON_OCC_BREP_WRAPPER_HH
#include <BRep_Builder.hxx>
#include <TopoDS_Compound.hxx>
#include <TopoDS_Solid.hxx>
#include <TopoDS_Shell.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Wire.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopoDS_Shape.hxx>
namespace Frackit::Python::OCCUtilities {
// Wrapper class to be used around occ brep classes
template<class S>
class BRepWrapper
{
public:
//! export underlying shape type
using Shape = S;
BRepWrapper(const Shape& shape) : shape_(shape) {}
const Shape& get() const { return shape_; }
private:
Shape shape_;
};
// Wrapper around TopoDS_Compound
template<>
class BRepWrapper<TopoDS_Compound>
{
using Compound = TopoDS_Compound;
public:
//! export underlying shape type
using Shape = Compound;
//! Default constructor
BRepWrapper()
: builder_()
{
builder_.MakeCompound(compound_);
}
//! Constructor from a compound
BRepWrapper(const Compound& c)
: compound_(c)
, builder_()
{}
//! return the compound
const Compound& get() const
{ return compound_; }
//! add a wrapped shape to the compound
template<class ShapeWrapper>
void add(const ShapeWrapper& shape)
{
builder_.Add(compound_, shape.get());
}
private:
Compound compound_;
BRep_Builder builder_;
};
// aliases to be used for the different wrapper classes
using ShapeWrapper = BRepWrapper<TopoDS_Shape>;
using VertexWrapper = BRepWrapper<TopoDS_Vertex>;
using EdgeWrapper = BRepWrapper<TopoDS_Edge>;
using WireWrapper = BRepWrapper<TopoDS_Wire>;
using FaceWrapper = BRepWrapper<TopoDS_Face>;
using ShellWrapper = BRepWrapper<TopoDS_Shell>;
using SolidWrapper = BRepWrapper<TopoDS_Solid>;
using CompoundWrapper = BRepWrapper<TopoDS_Compound>;
} // end namespace Frackit::Python::OCCUtilities
#endif
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*****************************************************************************
* See the file COPYING for full copying permissions. *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*****************************************************************************/
#ifndef FRACKIT_PYTHON_OCC_BREP_WRAPPERS_HH
#define FRACKIT_PYTHON_OCC_BREP_WRAPPERS_HH
#include <pybind11/pybind11.h>
#include "brepwrapper.hh"
namespace Frackit::Python {
namespace Detail {
template<class Shape>
void registerBRepWrapper(pybind11::module& module,
const std::string& className)
{
using Wrapper = OCCUtilities::BRepWrapper<Shape>;
pybind11::class_<Wrapper> cls(module, className.c_str());
}
void registerCompoundWrapper(pybind11::module& module)
{
using Wrapper = OCCUtilities::CompoundWrapper;
pybind11::class_<Wrapper> cls(module, "OCCCompoundWrapper");
cls.def(pybind11::init<>());
// functions to add shapes to the compound
using namespace Frackit::Python::OCCUtilities;
cls.def("add", &Wrapper::add<ShapeWrapper>, "Add a shape to the compound");
cls.def("add", &Wrapper::add<VertexWrapper>, "Add a vertex to the compound");
cls.def("add", &Wrapper::add<EdgeWrapper>, "Add an edge to the compound");
cls.def("add", &Wrapper::add<WireWrapper>, "Add a wire to the compound");
cls.def("add", &Wrapper::add<FaceWrapper>, "Add a face to the compound");
cls.def("add", &Wrapper::add<ShellWrapper>, "Add a shell to the compound");
cls.def("add", &Wrapper::add<SolidWrapper>, "Add a solid to the compound");
}
// convenience function to create wrapper around compound shape
OCCUtilities::CompoundWrapper makeCompound() { return {}; }
} // end namespace Detail
void registerBRepWrappers(pybind11::module& module)
{
Detail::registerBRepWrapper<TopoDS_Shape>(module, "OCCShapeWrapper");
Detail::registerBRepWrapper<TopoDS_Vertex>(module, "OCCVertexWrapper");
Detail::registerBRepWrapper<TopoDS_Edge>(module, "OCCEdgeWrapper");
Detail::registerBRepWrapper<TopoDS_Wire>(module, "OCCWireWrapper");
Detail::registerBRepWrapper<TopoDS_Face>(module, "OCCFaceWrapper");
Detail::registerBRepWrapper<TopoDS_Shell>(module, "OCCShellWrapper");
Detail::registerBRepWrapper<TopoDS_Solid>(module, "OCCSolidWrapper");
Detail::registerCompoundWrapper(module);
module.def("makeCompound", &Detail::makeCompound, "Creates an empty compound to which shapes can be added");
}
} // end namespace Frackit::Python
#endif
add_subdirectory(common)
add_subdirectory(geometry)
add_subdirectory(occutilities)
add_subdirectory(precision)
add_subdirectory(sampling)
frackit_symlink_or_copy(FILES __init__.py)
pybind11_add_module(_occutilities _occutilities.cc)
target_link_libraries(_occutilities PRIVATE ${OCC_LIBS})
from ._occutilities import *
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*****************************************************************************
* See the file COPYING for full copying permissions. *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*****************************************************************************/
#include <pybind11/pybind11.h>
#include <frackit/python/occutilities/brepwrappers.hh>
#include <frackit/python/occutilities/breputilities.hh>
PYBIND11_MODULE(_occutilities, module)
{
// wrapper classes for OpenCascade BRep shapes
Frackit::Python::registerBRepWrappers(module);
// utility functions for BReps
Frackit::Python::registerBRepUtilities<double>(module);
}
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