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

[python] port entity network

parent efe2f4e1
install(FILES
constituents.hh
constraints.hh
entitynetwork.hh
networkbuilder.hh
networkbuilderwrappers.hh
traits.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_ENTITY_NETWORK_CONSTITUENTS_HH
#define FRACKIT_PYTHON_ENTITY_NETWORK_CONSTITUENTS_HH
#include <pybind11/pybind11.h>
#include <TopoDS_Shape.hxx>
#include <frackit/python/geometry/brepwrapper.hh>
namespace Frackit::Python {
namespace EntityNetworkConstituents {
class Constituent
{
using OCCShape = TopoDS_Shape;
public:
using Shape = ShapeWrapper;
Constituent(const Shape& s, std::size_t idx) : shape_(s) , index_(idx) {}
Constituent(const OCCShape& s, std::size_t idx) : shape_(s) , index_(idx) {}
const Shape& shape() const { return shape_; }
std::size_t index() const { return index_; }
private:
Shape shape_;
std::size_t index_;
};
class SubDomainFragment : public Constituent { public: using Constituent::Constituent; };
class EntityFragment : public Constituent { public: using Constituent::Constituent; };
class IntersectionFragment : public Constituent { public: using Constituent::Constituent; };
class IntersectionJunction : public Constituent { public: using Constituent::Constituent; };
} // end namespace EntityNetworkConstituents
namespace py = pybind11;
void registerConstituents(py::module& module)
{
using Base = Frackit::Python::EntityNetworkConstituents::Constituent;
using Domain = Frackit::Python::EntityNetworkConstituents::SubDomainFragment;
using Entity = Frackit::Python::EntityNetworkConstituents::EntityFragment;
using Intersection = Frackit::Python::EntityNetworkConstituents::IntersectionFragment;
using Junction = Frackit::Python::EntityNetworkConstituents::IntersectionJunction;
py::class_<Base> baseConstituent(module, "BaseConstituent");
baseConstituent.def("shape", &Base::shape, "returns the shape of the constituent");
baseConstituent.def("index", &Base::index, "returns the index of the constituent within the network");
py::class_<Domain, Base>(module, "DomainFragment");
py::class_<Entity, Base>(module, "EntityFragment");
py::class_<Intersection, Base>(module, "IntersectionFragment");
py::class_<Junction, Base>(module, "IntersectionJunction");
}
} // end namespace Frackit::Python
#endif // FRACKIT_PYTHON_sENTITY_NETWORK_CONSTITUENTS_HH
......@@ -21,7 +21,9 @@
#include <pybind11/pybind11.h>
#include <frackit/common/id.hh>
#include <frackit/entitynetwork/entitynetwork.hh>
#include "traits.hh"
namespace Frackit::Python {
......@@ -29,7 +31,28 @@ namespace py = pybind11;
void registerEntityNetworks(py::module& module)
{
py::class_<EntityNetwork<>>(module, "EntityNetwork");
registerConstituents(module);
using EN = EntityNetwork<PythonEntityNetworkTraits>;
py::class_<EN> en(module, "EntityNetwork");
en.def("entityDimension", &EN::entityDimension, "return the dimension of the network entities");
en.def("domainDimension", &EN::domainDimension, "return the dimension of the embedding domain (if defined)");
en.def("isContained", &EN::isContained, "returns true if the domain is contained in a domain");
// return functions for network size
en.def("numSubDomains", &EN::numSubDomains, "returns the number of primary subdomains");
en.def("numSubDomainFragments", &EN::numSubDomainFragments, "returns the number of all subdomain fragments");
en.def("numEntities", &EN::numEntities, "returns the number of primary entities");
en.def("numEntityFragments", &EN::numEntityFragments, "returns the number of all entity fragments");
en.def("numIntersections", &EN::numIntersections, "returns the number of primary entity intersections");
en.def("numIntersectionFragments", &EN::numIntersectionFragments, "returns the number of all entity intersection fragments");
en.def("numIntersectionJunctions", &EN::numIntersectionJunctions, "returns the number of intersection junctions");
// return functions for primary ids
en.def("subDomainId", &EN::subDomainId, "returns the primary subdomain id the given fragment belongs to");
en.def("entityId", &EN::entityId, "returns the primary entity id the given fragment belongs to");
en.def("intersectionId", &EN::intersectionId, "returns the primary entity intersection id the given fragment belongs to");
en.def("intersectionJunctionId", &EN::intersectionJunctionId, "returns the primary intersection junction id for the given junction");
}
} // end namespace Frackit::Python
......
......@@ -34,6 +34,7 @@
#include <frackit/common/id.hh>
#include <frackit/entitynetwork/networkbuilder.hh>
#include "networkbuilderwrappers.hh"
#include "traits.hh"
namespace Frackit::Python {
......@@ -53,7 +54,7 @@ namespace Detail {
{
cls.def("addEntity",
&Builder::template addEntity<Entity>,
"add entity to the network");
"add entity to the network (returns the id given to the entity)");
}
template<class Entity, class Builder, class... Bases>
......@@ -61,7 +62,7 @@ namespace Detail {
{
cls.def("addSubDomainEntity",
&Builder::template addSubDomainEntity<Entity>,
"add entity to be embedded in the subdomain with the given id");
"add entity to be embedded in the subdomain with the given id (returns the id given to the entity)");
}
template<class ctype, class Builder, class... Bases>
......@@ -96,7 +97,7 @@ template<class ctype>
void registerEntityNetworkBuilders(py::module& module)
{
// register common base class
using EN = Frackit::EntityNetwork<>;
using EN = Frackit::EntityNetwork<PythonEntityNetworkTraits>;
using Base = EntityNetworkBuilderBase<ctype, EN>;
py::class_<Base> base(module, "_EntityNetworkBuilderBase");
base.def("setConfineToSubDomainUnion",
......@@ -122,6 +123,7 @@ void registerEntityNetworkBuilders(py::module& module)
builderWrapper.def(py::init());
Detail::registerSubDomainEntityAdders<ctype>(builderWrapper);
Detail::registerEntityAdders<ctype>(builderWrapper);
builderWrapper.def("curEntityIndex", &BuilderWrapper::curEntityIndex);
// register adder functions for (sub-)domains
Detail::registerSubDomainAdders<ctype>(builderWrapper);
......@@ -130,6 +132,7 @@ void registerEntityNetworkBuilders(py::module& module)
py::class_<ContainedBuilderWrapper, ContainedBuilder> containedBuilderWrapper(module, "_ContainedEntityNetworkBuilderWrapper");
containedBuilderWrapper.def(py::init());
Detail::registerSubDomainEntityAdders<ctype>(containedBuilderWrapper);
containedBuilderWrapper.def("curEntityIndex", &ContainedBuilderWrapper::curEntityIndex);
// register adder functions for (sub-)domains
Detail::registerSubDomainAdders<ctype>(containedBuilderWrapper);
......
......@@ -37,12 +37,12 @@ namespace Detail {
public:
template<class Entity>
void addSubDomainEntity(const Entity& entity, Id subDomainId)
{ ParentType::addSubDomainEntity(getUnwrappedShape(entity), subDomainId); }
Id addSubDomainEntity(const Entity& entity, Id subDomainId)
{ return ParentType::addSubDomainEntity(getUnwrappedShape(entity), subDomainId); }
template<class Entity>
void addEntity(const Entity& entity)
{ addSubDomainEntity(entity, Id(0)); }
Id addEntity(const Entity& entity)
{ return addSubDomainEntity(entity, Id(0)); }
template<class Domain>
void addConfiningSubDomain(const Domain& domain, Id subDomainId)
......@@ -51,6 +51,10 @@ namespace Detail {
template<class Domain>
void addSubDomain(const Domain& domain, Id subDomainId)
{ ParentType::addSubDomain(getUnwrappedShape(domain), subDomainId); }
// give the python classes access to the current index
std::size_t curEntityIndex() const
{ return ParentType::curEntityIdx_; }
};
template<class ctype, class EN>
......@@ -61,8 +65,8 @@ namespace Detail {
public:
template<class Entity>
void addSubDomainEntity(const Entity& entity, Id subDomainId)
{ ParentType::addSubDomainEntity(getUnwrappedShape(entity), subDomainId); }
Id addSubDomainEntity(const Entity& entity, Id subDomainId)
{ return ParentType::addSubDomainEntity(getUnwrappedShape(entity), subDomainId); }
template<class Domain>
void addConfiningSubDomain(const Domain& domain, Id subDomainId)
......@@ -71,6 +75,10 @@ namespace Detail {
template<class Domain>
void addSubDomain(const Domain& domain, Id subDomainId)
{ ParentType::addSubDomain(getUnwrappedShape(domain), subDomainId); }
// give the python classes access to the current index
std::size_t curEntityIndex() const
{ return ParentType::curEntityIdx_; }
};
} // end namespace Detail
......
// -*- 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_ENTITY_NETWORK_TRAITS_HH
#define FRACKIT_PYTHON_ENTITY_NETWORK_TRAITS_HH
#include "constituents.hh"
namespace Frackit::Python {
// traits to be used for entity networks in python
struct PythonEntityNetworkTraits
{
using SubDomainFragment = EntityNetworkConstituents::SubDomainFragment;
using EntityFragment = EntityNetworkConstituents::EntityFragment;
using IntersectionFragment = EntityNetworkConstituents::IntersectionFragment;
using IntersectionJunction = EntityNetworkConstituents::IntersectionJunction;
};
} // end namespace Frackit::Python
#endif // FRACKIT_PYTHON_ENTITY_NETWORK_TRAITS_HH
......@@ -23,7 +23,7 @@
#include <frackit/io/gmshwriter.hh>
#include <frackit/entitynetwork/entitynetwork.hh>
#include <frackit/entitynetwork/containedentitynetwork.hh>
#include <frackit/python/entitynetwork/traits.hh>
namespace Frackit::Python {
......@@ -32,7 +32,7 @@ namespace py = pybind11;
template<class ctype>
void registerGmshWriter(py::module& module)
{
using EntityNetwork = Frackit::EntityNetwork<>;
using EntityNetwork = Frackit::EntityNetwork<PythonEntityNetworkTraits>;
py::class_<GmshWriter> cls(module, "GmshWriter");
cls.def(py::init<const EntityNetwork&>());
......
add_subdirectory(iterators)
frackit_symlink_or_copy(FILES __init__.py)
pybind11_add_module(_entitynetwork _entitynetwork.cc)
target_link_libraries(_entitynetwork PRIVATE ${OCC_LIBS})
from ._entitynetwork import *
from frackit.common import Id
# python implementation of the constraints class that accepts evaluation on lists
class EntityNetworkConstraints(_entitynetwork._EntityNetworkConstraints):
......@@ -103,14 +104,32 @@ class EntityNetworkBuilder(_entitynetwork._EntityNetworkBuilderWrapper):
def addEntities(self, entities):
"""
adds all the entities in the provided list to the network.
Returns: the range of ids given to the inserted entities
in the form of a list with [firstId, lastId]
"""
for entity in entities: super().addEntity(entity)
if not entities:
curIdx = super().curEntityIndex()
return [Id(curIdx), Id(curIdx)]
for count, entity in enumerate(entities):
id = super().addEntity(entity)
return [Id(id.get()-count), id]
def addSubDomainEntities(self, entities, subDomainId):
"""
adds all the given entities to be embedded in the subdomain with the given id.
Returns: the range of ids given to the inserted entities
in the form of a list with [firstId, lastId]
"""
for entity in entities: super().addSubDomainEntity(entity, subDomainId)
if not entities:
curIdx = super().curEntityIndex()
return [Id(curIdx), Id(curIdx)]
for count, entity in enumerate(entities):
id = super().addSubDomainEntity(entity, subDomainId)
return [Id(id.get()-count), id]
# python implementation of the contained entity network builder class
class ContainedEntityNetworkBuilder(_entitynetwork._ContainedEntityNetworkBuilderWrapper):
......@@ -118,5 +137,14 @@ class ContainedEntityNetworkBuilder(_entitynetwork._ContainedEntityNetworkBuilde
def addSubDomainEntities(self, entities, subDomainId):
"""
adds all the given entities to be embedded in the subdomain with the given id.
Returns: the range of ids given to the inserted entities
in the form of a list with [firstId, lastId]
"""
for entity in entities: super().addSubDomainEntity(entity, subDomainId)
if not entities:
curIdx = super().curEntityIndex()
return [Id(curIdx), Id(curIdx)]
for count, entity in enumerate(entities):
id = super().addSubDomainEntity(entity, subDomainId)
return [Id(id.get()-count), id]
frackit_symlink_or_copy(FILES __init__.py)
pybind11_add_module(_iterators _iterators.cc)
target_link_libraries(_iterators PRIVATE ${OCC_LIBS})
// -*- 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/common/id.hh>
#include <frackit/occ/breputilities.hh>
#include <frackit/entitynetwork/entitynetwork.hh>
#include <frackit/python/entitynetwork/traits.hh>
PYBIND11_MODULE(_iterators, module)
{
namespace py = pybind11;
using namespace Frackit;
using T = Python::PythonEntityNetworkTraits;
using EN = EntityNetwork<T>;
using Entity = T::EntityFragment;
using Intersection = T::IntersectionFragment;
using Junction = T::IntersectionJunction;
module.def("subDomainFragments",
[] (const EN& network)
{ return py::make_iterator<py::return_value_policy::automatic>(subDomainFragments(network).begin(), subDomainFragments(network).end()); },
"Iterator over the subdomain fragments of the given network");
module.def("entityFragments",
[] (const EN& network)
{ return py::make_iterator<py::return_value_policy::automatic>(entityFragments(network).begin(), entityFragments(network).end()); },
"Iterator over the entity fragments of the given network");
module.def("intersectionFragments",
[] (const EN& network)
{ return py::make_iterator<py::return_value_policy::automatic>(intersectionFragments(network).begin(), intersectionFragments(network).end()); },
"Iterator over the intersection fragments of the given network");
module.def("intersectionJunctions",
[] (const EN& network)
{ return py::make_iterator<py::return_value_policy::automatic>(intersectionJunctions(network).begin(), intersectionJunctions(network).end()); },
"Iterator over the intersection junctions of the given network");
module.def("subDomainFragments",
[] (const EN& network, const Id& id)
{ return py::make_iterator<py::return_value_policy::automatic>(subDomainFragments(network, id).begin(), subDomainFragments(network, id).end()); },
"Iterator over the subdomain fragments of the given network that belong to the primary subdomain with the given id");
module.def("entityFragments",
[] (const EN& network, const Id& id)
{ return py::make_iterator<py::return_value_policy::automatic>(entityFragments(network, id).begin(), entityFragments(network, id).end()); },
"Iterator over the entity fragments of the given network that belong to the primary entity with the given id");
module.def("intersectionFragments",
[] (const EN& network, const Id& id)
{ return py::make_iterator<py::return_value_policy::automatic>(intersectionFragments(network, id).begin(), intersectionFragments(network, id).end()); },
"Iterator over the intersection fragments of the given network that belong to the primary intersection with the given id");
module.def("embeddedFragments",
[] (const EN& network, const Entity& ent)
{ return py::make_iterator<py::return_value_policy::automatic>(embeddedFragments(network, ent).begin(), embeddedFragments(network, ent).end()); },
"Iterator over the intersection fragments in which the given entity fragment intersects with other ones");
module.def("embeddedFragments",
[] (const EN& network, const Intersection& is)
{ return py::make_iterator<py::return_value_policy::automatic>(embeddedFragments(network, is).begin(), embeddedFragments(network, is).end()); },
"Iterator over the intersection junctions that the given intersection is connected to");
module.def("adjacentFragments",
[] (const EN& network, const Intersection& is)
{ return py::make_iterator<py::return_value_policy::automatic>(adjacentFragments(network, is).begin(), adjacentFragments(network, is).end()); },
"Iterator over the entity fragments that intersect in the given intersection fragment");
module.def("adjacentFragments",
[] (const EN& network, const Junction& j)
{ return py::make_iterator<py::return_value_policy::automatic>(adjacentFragments(network, j).begin(), adjacentFragments(network, j).end()); },
"Iterator over the entity fragments that intersect in the given intersection fragment");
}
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