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

[python][constraints] support new result type

parent e98b7afe
......@@ -43,11 +43,12 @@ namespace Detail {
: public EntityNetworkConstraints<ST>
{
using ParentType = EntityNetworkConstraints<ST>;
using Result = typename ParentType::ResultType;
public:
// evaluate the constraint between two geometries
template<class Geo1, class Geo2>
bool evaluateBinary(const Geo1& geo1, const Geo2& geo2) const
Result evaluateBinary(const Geo1& geo1, const Geo2& geo2) const
{ return ParentType::evaluate(getUnwrappedShape(geo1), getUnwrappedShape(geo2)); }
};
......@@ -121,6 +122,24 @@ namespace Detail {
template<class ctype>
void registerConstraints(py::module& module)
{
// register evaluation result type
using Result = ConstraintsEvaluation;
py::class_<Result> evalResult(module, "ConstraintsEvaluation");
// register constraints violation
py::enum_<Result::Violation>(evalResult, "Violation")
.value("none", Result::Violation::none)
.value("distance", Result::Violation::distance)
.value("intersectionAngle", Result::Violation::intersectionAngle)
.value("intersectionMagnitude", Result::Violation::intersectionMagnitude)
.value("intersectionDimension", Result::Violation::intersectionDimension)
.value("intersectionDistance", Result::Violation::intersectionDistance);
// define members of ConstraintsEvaluation
evalResult.def(py::init<Result::Violation>());
evalResult.def("violation", &Result::violation, "return the type of detected violation");
evalResult.def("__bool__", &Result::operator bool, "returns true if no violation was detected");
using Constraints = Detail::EntityNetworkConstraintsWrapper<ctype>;
py::class_<Constraints> cls(module, "_EntityNetworkConstraints");
cls.def(py::init());
......
......@@ -16,14 +16,20 @@ class EntityNetworkConstraints(_entitynetwork._EntityNetworkConstraints):
isListB = isinstance(b, list)
def evalList(theList, geo):
return all( super(EntityNetworkConstraints, self).evaluate(item, b) for item in theList )
for item in theList:
res = super(EntityNetworkConstraints, self).evaluate(item, b)
if not res: return res
return ConstraintsEvaluation(ConstraintsEvaluation.Violation.none)
if isListA and not isListB:
return evalList(a, b)
elif isListB and not isListA:
return evalList(b, a)
elif isListA and isListB:
return all( evalList(b, item) for item in a )
for item in a:
res = evalList(b, item)
if not res: return res
return ConstraintsEvaluation(ConstraintsEvaluation.Violation.none)
# here we assume the two arguments to be geometries
else:
return super().evaluate(a, b)
......@@ -70,7 +76,7 @@ class EntityNetworkConstraintsMatrix:
# no constraints registered for this id
if self.constraintsMatrix.get(id) == None:
return True
return ConstraintsEvaluation(ConstraintsEvaluation.Violation.none)
for otherSetId in self.constraintsMatrix:
entitySet = entitySets.get(otherSetId)
......@@ -83,11 +89,11 @@ class EntityNetworkConstraintsMatrix:
# a constraint is defined between the set with the given id
# and the set with otherSetId. Evaluate constraints for this pair.
constraints = self.constraintsMatrix.get(id).get(otherSetId)
if not constraints.evaluate(entitySet, entity):
return False
res = constraints.evaluate(entitySet, entity)
if not res: return res
# no constraints violation detected
return True
return ConstraintsEvaluation(ConstraintsEvaluation.Violation.none)
......
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