Skip to content

Commit

Permalink
Make left() and right() of components accessible from Python
Browse files Browse the repository at this point in the history
  • Loading branch information
saraedum committed Jan 18, 2024
1 parent 0173729 commit 58a85ce
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
3 changes: 3 additions & 0 deletions doc/news/leftright.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**Fixed:**

* Fixed access to `left()` and `right()` of a component from Python.
14 changes: 1 addition & 13 deletions libintervalxt/intervalxt/forward.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This file is part of intervalxt.
*
* Copyright (C) 2019 Vincent Delecroix
* Copyright (C) 2019-2021 Julian Rüth
* Copyright (C) 2019-2024 Julian Rüth
*
* intervalxt is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -59,19 +59,7 @@ class Serializable;
template <typename T>
struct Serialization;

// Cling cannot handle the variant used in recent C++ standard library
// implementations yet, see https://sft.its.cern.ch/jira/browse/ROOT-10220.
// Since we heavily rely on cling through cppyy, we should probably use a
// non-standard variant implementation that does not have these issues, see
// https://github.com/flatsurf/intervalxt/issues/144. Here we just provide a
// dirty workaround: this makes Side not usable in cppyy but at least you can
// `import pyintervalxt` and use functionality that does not use the exposed
// Side interface.
#ifndef __CLING__
using Side = std::variant<Connection, HalfEdge>;
#else
using Side = std::array<char, 32>;
#endif

} // namespace intervalxt

Expand Down
12 changes: 11 additions & 1 deletion pyintervalxt/src/pyintervalxt/cppyy_intervalxt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This file is part of intervalxt.
#
# Copyright (C) 2019-2020 Vincent Delecroix
# Copyright (C) 2019-2022 Julian Rüth
# Copyright (C) 2019-2024 Julian Rüth
#
# intervalxt is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -131,6 +131,16 @@ def from_vector_vector_mpq(v):
cppyy.py.add_pythonization(filtered("IntervalExchangeTransformation")(wrap_method("boshernitzanEquations")(lambda self, equations: from_vector_vector_mpq(equations()))), "intervalxt")
cppyy.py.add_pythonization(filtered("IntervalExchangeTransformation")(wrap_method("boshernitzanSaddleConnectionValues")(lambda self, values, top, bottom: from_vector_mpq(values(top, bottom)))), "intervalxt")

# Make left & right of components functional.
def connections(variants):
connections = [cppyy.gbl.std.get[0](c) for c in variants]
for variant, c in zip(variants, connections):
c._variant = variant
return connections

cppyy.py.add_pythonization(filtered("Component")(wrap_method("left")(lambda self, left: connections(left()))), "intervalxt")
cppyy.py.add_pythonization(filtered("Component")(wrap_method("right")(lambda self, right: connections(right()))), "intervalxt")

# Expose methods on type-erased intervalxt::Lengths.
cppyy.py.add_pythonization(filtered("any<intervalxt::LengthsInterface,boost::type_erasure::_self>")(expose("push")), "boost::type_erasure")
cppyy.py.add_pythonization(filtered("any<intervalxt::LengthsInterface,boost::type_erasure::_self>")(expose("pop")), "boost::type_erasure")
Expand Down
8 changes: 8 additions & 0 deletions pyintervalxt/test/interval_exchange_transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ def test_decompose(iet):

assert cyls + mins == decomposition.components().size()

# Test that all sides of cylinders are glued somewhere
connections = []
for component in decomposition.components():
connections.extend(component.left())
connections.extend(component.right())

assert all(-connection in connections for connection in connections)


def test_lengths(iet, lengths):
r"""
Expand Down

0 comments on commit 58a85ce

Please sign in to comment.