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

[python][entitynetwork] add bindings for builder classes

parent 53e84016
install(FILES
constraints.hh
entitynetwork.hh
networkbuilder.hh
networkbuilderwrappers.hh
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/frackit/python/entitynetwork)
// -*- 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_ENTITYNETWORK_BUILDER_HH
#define FRACKIT_PYTHON_ENTITYNETWORK_BUILDER_HH
#include <pybind11/pybind11.h>
// (currently) supported domain geometries
#include <frackit/geometry/box.hh>
#include <frackit/geometry/cylinder.hh>
// (currently) supported entity geometries
#include <frackit/geometry/disk.hh>
#include <frackit/geometry/quadrilateral.hh>
#include <frackit/python/occutilities/brepwrapper.hh>
#include <frackit/common/id.hh>
#include <frackit/entitynetwork/networkbuilder.hh>
#include "networkbuilderwrappers.hh"
namespace Frackit::Python {
namespace py = pybind11;
namespace Detail {
template<class Domain, class Builder, class... Bases>
void registerSubDomainAdder(py::class_<Builder, Bases...>& cls)
{
cls.def("addSubDomain", &Builder::template addSubDomain<Domain>, "add subdomain with the given id");
cls.def("addConfiningSubDomain", &Builder::template addConfiningSubDomain<Domain>, "add confining subdomain with the given id");
}
template<class Entity, class Builder, class... Bases>
void registerEntityAdder(py::class_<Builder, Bases...>& cls)
{
cls.def("addEntity",
&Builder::template addEntity<Entity>,
"add entity to the network");
}
template<class Entity, class Builder, class... Bases>
void registerSubDomainEntityAdder(py::class_<Builder, Bases...>& cls)
{
cls.def("addSubDomainEntity",
&Builder::template addSubDomainEntity<Entity>,
"add entity to be embedded in the subdomain with the given id");
}
template<class ctype, class Builder, class... Bases>
void registerSubDomainAdders(py::class_<Builder, Bases...>& cls)
{
registerSubDomainAdder< Box<ctype> >(cls);
registerSubDomainAdder< Cylinder<ctype> >(cls);
registerSubDomainAdder< OCCUtilities::SolidWrapper >(cls);
}
template<class ctype, class Wrapper, class... Bases>
void registerSubDomainEntityAdders(py::class_<Wrapper, Bases...>& cls)
{
registerSubDomainEntityAdder< Disk<ctype> >(cls);
registerSubDomainEntityAdder< Quadrilateral<ctype, 3> >(cls);
registerSubDomainEntityAdder< OCCUtilities::FaceWrapper >(cls);
}
template<class ctype, class Wrapper, class... Bases>
void registerEntityAdders(py::class_<Wrapper, Bases...>& cls)
{
registerEntityAdder< Disk<ctype> >(cls);
registerEntityAdder< Quadrilateral<ctype, 3> >(cls);
registerEntityAdder< OCCUtilities::FaceWrapper >(cls);
}
} // end namespace detail
template<class ctype>
void registerEntityNetworkBuilders(py::module& module)
{
// register common base class
using Base = EntityNetworkBuilderBase<ctype>;
py::class_<Base> base(module, "_EntityNetworkBuilderBase");
base.def("setEpsilon", &Base::setEpsilon, "define tolerance value for intersections");
base.def("setDefaultEpsilon", &Base::setDefaultEpsilon, "restore default tolerance value for intersections");
base.def("clear", &Base::clear, "clears all inserted data");
// register base classes for builders
using Builder = EntityNetworkBuilder<ctype>;
py::class_<Builder, Base> builder(module, "_EntityNetworkBuilder");
builder.def("build", &Builder::build, "build the network from the inserted entities");
using ContainedBuilder = ContainedEntityNetworkBuilder<ctype>;
py::class_<ContainedBuilder, Base> containedBuilder(module, "_ContainedEntityNetworkBuilder");
containedBuilder.def("build", &ContainedBuilder::build, "build the contained network from the inserted entities");
// register wrapper classes
using BuilderWrapper = Detail::EntityNetworkBuilderWrapper<ctype>;
py::class_<BuilderWrapper, Builder> builderWrapper(module, "_EntityNetworkBuilderWrapper");
builderWrapper.def(py::init());
Detail::registerSubDomainEntityAdders<ctype>(builderWrapper);
Detail::registerEntityAdders<ctype>(builderWrapper);
// register adder functions for (sub-)domains
Detail::registerSubDomainAdders<ctype>(builderWrapper);
using ContainedBuilderWrapper = Detail::ContainedEntityNetworkBuilderWrapper<ctype>;
py::class_<ContainedBuilderWrapper, ContainedBuilder> containedBuilderWrapper(module, "_ContainedEntityNetworkBuilderWrapper");
containedBuilderWrapper.def(py::init());
Detail::registerSubDomainEntityAdders<ctype>(containedBuilderWrapper);
// register adder functions for (sub-)domains
Detail::registerSubDomainAdders<ctype>(containedBuilderWrapper);
}
} // 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_ENTITYNETWORK_BUILDER_WRAPPERS_HH
#define FRACKIT_PYTHON_ENTITYNETWORK_BUILDER_WRAPPERS_HH
#include <frackit/python/occutilities/brepwrapper.hh>
#include <frackit/entitynetwork/networkbuilder.hh>
#include <frackit/common/id.hh>
namespace Frackit::Python {
namespace Detail {
// Wrappers around the builder classes to support brep wrappers
template<class ctype>
class EntityNetworkBuilderWrapper
: public EntityNetworkBuilder<ctype>
{
using ParentType = EntityNetworkBuilder<ctype>;
public:
template<class Entity>
void addSubDomainEntity(const Entity& entity, Id subDomainId)
{
if constexpr (OCCUtilities::IsBRepWrapper<Entity>::value)
ParentType::addSubDomainEntity(entity.get(), subDomainId);
else
ParentType::addSubDomainEntity(entity, subDomainId);
}
template<class Entity>
void addEntity(const Entity& entity)
{
addSubDomainEntity(entity, Id(0));
}
template<class Domain>
void addConfiningSubDomain(const Domain& domain, Id subDomainId)
{
if constexpr (OCCUtilities::IsBRepWrapper<Domain>::value)
ParentType::addConfiningSubDomain(domain.get(), subDomainId);
else
ParentType::addConfiningSubDomain(domain, subDomainId);
}
template<class Domain>
void addSubDomain(const Domain& domain, Id subDomainId)
{
if constexpr (OCCUtilities::IsBRepWrapper<Domain>::value)
ParentType::addSubDomain(domain.get(), subDomainId);
else
ParentType::addSubDomain(domain, subDomainId);
}
};
template<class ctype>
class ContainedEntityNetworkBuilderWrapper
: public ContainedEntityNetworkBuilder<ctype>
{
using ParentType = ContainedEntityNetworkBuilder<ctype>;
public:
template<class Entity>
void addSubDomainEntity(const Entity& entity, Id subDomainId)
{
if constexpr (OCCUtilities::IsBRepWrapper<Entity>::value)
ParentType::addSubDomainEntity(entity.get(), subDomainId);
else
ParentType::addSubDomainEntity(entity, subDomainId);
}
template<class Domain>
void addConfiningSubDomain(const Domain& domain, Id subDomainId)
{
if constexpr (OCCUtilities::IsBRepWrapper<Domain>::value)
ParentType::addConfiningSubDomain(domain.get(), subDomainId);
else
ParentType::addConfiningSubDomain(domain, subDomainId);
}
template<class Domain>
void addSubDomain(const Domain& domain, Id subDomainId)
{
if constexpr (OCCUtilities::IsBRepWrapper<Domain>::value)
ParentType::addSubDomain(domain.get(), subDomainId);
else
ParentType::addSubDomain(domain, subDomainId);
}
};
} // end namespace Detail
} // end namespace Frackit::Python
#endif
from ._entitynetwork import *
# python implementation of the constraints class that accepts evaluation on lists
class EntityNetworkConstraints(_entitynetwork._EntityNetworkConstraints):
def evaluate(self, a, b):
......@@ -26,3 +27,28 @@ class EntityNetworkConstraints(_entitynetwork._EntityNetworkConstraints):
# here we assume the two arguments to be geometries
else:
return super().evaluate(a, b)
# python implementation of the entity network builder class
class EntityNetworkBuilder(_entitynetwork._EntityNetworkBuilderWrapper):
def addEntities(self, entities):
"""
adds all the entities in the provided list to the network.
"""
for entity in entities: super().addEntity(entity)
def addSubDomainEntities(self, entities, subDomainId):
"""
adds all the given entities to be embedded in the subdomain with the given id.
"""
for entity in entities: super().addSubDomainEntity(entity, subDomainId)
# python implementation of the contained entity network builder class
class ContainedEntityNetworkBuilder(_entitynetwork._ContainedEntityNetworkBuilderWrapper):
def addSubDomainEntities(self, entities, subDomainId):
"""
adds all the given entities to be embedded in the subdomain with the given id.
"""
for entity in entities: super().addSubDomainEntity(entity, subDomainId)
......@@ -20,6 +20,7 @@
#include <frackit/python/entitynetwork/constraints.hh>
#include <frackit/python/entitynetwork/entitynetwork.hh>
#include <frackit/python/entitynetwork/networkbuilder.hh>
PYBIND11_MODULE(_entitynetwork, module)
{
......@@ -28,4 +29,7 @@ PYBIND11_MODULE(_entitynetwork, module)
// register entity network classes
Frackit::Python::registerEntityNetworks(module);
// register entity network builder classes
Frackit::Python::registerEntityNetworkBuilders<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