Skip to content

Commit

Permalink
[ctc] added CtcCtcBoundary, CtcPolygon + binding + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonRohou committed Oct 24, 2024
1 parent 43c2b19 commit 147517e
Show file tree
Hide file tree
Showing 18 changed files with 548 additions and 7 deletions.
2 changes: 2 additions & 0 deletions python/src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
contractors/codac2_py_Ctc.h
contractors/codac2_py_CtcAction.cpp
contractors/codac2_py_CtcCartProd.cpp
contractors/codac2_py_CtcCtcBoundary.cpp
contractors/codac2_py_CtcEmpty.cpp
contractors/codac2_py_CtcFixpoint.cpp
contractors/codac2_py_CtcInnerOuter.cpp
Expand All @@ -23,6 +24,7 @@
contractors/codac2_py_CtcLazy.cpp
contractors/codac2_py_CtcNot.cpp
contractors/codac2_py_CtcPolar.cpp
contractors/codac2_py_CtcPolygon.cpp
contractors/codac2_py_CtcProj.cpp
contractors/codac2_py_CtcSegment.cpp
contractors/codac2_py_CtcUnion.cpp
Expand Down
4 changes: 4 additions & 0 deletions python/src/core/codac2_py_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ void export_OctaSym(py::module& m);
py::class_<CtcBase<IntervalVector>,pyCtcIntervalVector> export_CtcIntervalVector(py::module& m/*, py::class_<Ctc,pyCtc>& py_ctc*/);
void export_CtcAction(py::module& m, py::class_<CtcBase<IntervalVector>,pyCtcIntervalVector>& ctc);
void export_CtcCartProd(py::module& m, py::class_<CtcBase<IntervalVector>,pyCtcIntervalVector>& ctc);
void export_CtcCtcBoundary(py::module& m, py::class_<CtcBase<IntervalVector>,pyCtcIntervalVector>& ctc);
void export_CtcEmpty(py::module& m, py::class_<CtcBase<IntervalVector>,pyCtcIntervalVector>& ctc);
void export_CtcFixpoint(py::module& m, py::class_<CtcBase<IntervalVector>,pyCtcIntervalVector>& ctc);
void export_CtcIdentity(py::module& m, py::class_<CtcBase<IntervalVector>,pyCtcIntervalVector>& ctc);
Expand All @@ -39,6 +40,7 @@ void export_CtcInter(py::module& m, py::class_<CtcBase<IntervalVector>,pyCtcInte
void export_CtcLazy(py::module& m, py::class_<CtcBase<IntervalVector>,pyCtcIntervalVector>& ctc);
void export_CtcNot(py::module& m, py::class_<CtcBase<IntervalVector>,pyCtcIntervalVector>& ctc);
void export_CtcPolar(py::module& m, py::class_<CtcBase<IntervalVector>,pyCtcIntervalVector>& ctc);
void export_CtcPolygon(py::module& m, py::class_<CtcBase<IntervalVector>,pyCtcIntervalVector>& ctc);
void export_CtcProj(py::module& m, py::class_<CtcBase<IntervalVector>,pyCtcIntervalVector>& ctc);
void export_CtcSegment(py::module& m, py::class_<CtcBase<IntervalVector>,pyCtcIntervalVector>& ctc);
void export_CtcUnion(py::module& m, py::class_<CtcBase<IntervalVector>,pyCtcIntervalVector>& ctc);
Expand Down Expand Up @@ -123,6 +125,7 @@ PYBIND11_MODULE(_core, m)
auto py_ctc_iv = export_CtcIntervalVector(m/*,py_ctc*/);
export_CtcAction(m, py_ctc_iv);
export_CtcCartProd(m, py_ctc_iv);
export_CtcCtcBoundary(m, py_ctc_iv);
export_CtcEmpty(m, py_ctc_iv);
export_CtcFixpoint(m, py_ctc_iv);
export_CtcIdentity(m, py_ctc_iv);
Expand All @@ -135,6 +138,7 @@ PYBIND11_MODULE(_core, m)
export_CtcLazy(m, py_ctc_iv);
export_CtcNot(m, py_ctc_iv);
export_CtcPolar(m, py_ctc_iv);
export_CtcPolygon(m, py_ctc_iv);
export_CtcProj(m, py_ctc_iv);
export_CtcSegment(m, py_ctc_iv);
export_CtcUnion(m, py_ctc_iv);
Expand Down
41 changes: 41 additions & 0 deletions python/src/core/contractors/codac2_py_CtcCtcBoundary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Codac binding (core)
* ----------------------------------------------------------------------------
* \date 2024
* \author Simon Rohou
* \copyright Copyright 2024 Codac Team
* \license GNU Lesser General Public License (LGPL)
*/

#include <pybind11/pybind11.h>
#include <pybind11/operators.h>
#include <pybind11/functional.h>
#include <pybind11/stl.h>
#include <codac2_template_tools.h>
#include <codac2_CtcCtcBoundary.h>
#include "codac2_py_Ctc.h"
#include "codac2_py_CtcCtcBoundary_docs.h" // Generated file from Doxygen XML (doxygen2docstring.py):

using namespace std;
using namespace codac2;
namespace py = pybind11;
using namespace pybind11::literals;

void export_CtcCtcBoundary(py::module& m, py::class_<CtcBase<IntervalVector>,pyCtcIntervalVector>& pyctc)
{
py::class_<CtcCtcBoundary> exported(m, "CtcCtcBoundary", pyctc, CTCCTCBOUNDARY_MAIN);
exported

.def(py::init(
[](const CtcBase<IntervalVector>& ctc_boundary, const std::function<BoolInterval(const Vector&)>& inside_test)
{
return std::make_unique<CtcCtcBoundary>(ctc_boundary.copy(),inside_test);
}),
CTCCTCBOUNDARY_CTCCTCBOUNDARY_CONST_C_REF_CONST_FUNCTION_BOOLINTERVAL_CONST_VECTOR_REF__REF,
"ctc_boundary"_a, "inside_test"_a)

.def(CONTRACT_BOX_METHOD(CtcCtcBoundary,
VOID_CTCCTCBOUNDARY_CONTRACT_INTERVALVECTOR_REF_CONST))

;
}
38 changes: 38 additions & 0 deletions python/src/core/contractors/codac2_py_CtcPolygon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Codac binding (core)
* ----------------------------------------------------------------------------
* \date 2024
* \author Simon Rohou
* \copyright Copyright 2024 Codac Team
* \license GNU Lesser General Public License (LGPL)
*/

#include <pybind11/pybind11.h>
#include <pybind11/operators.h>
#include <pybind11/stl.h>
#include <codac2_template_tools.h>
#include <codac2_CtcPolygon.h>
#include "codac2_py_Ctc.h"
#include "codac2_py_CtcPolygon_docs.h" // Generated file from Doxygen XML (doxygen2docstring.py):

using namespace std;
using namespace codac2;
namespace py = pybind11;
using namespace pybind11::literals;

void export_CtcPolygon(py::module& m, py::class_<CtcBase<IntervalVector>,pyCtcIntervalVector>& pyctc)
{
py::class_<CtcPolygon> exported(m, "CtcPolygon", pyctc, CTCPOLYGON_MAIN);
exported

.def(py::init<const Polygon&>(),
CTCPOLYGON_CTCPOLYGON_CONST_POLYGON_REF,
"p"_a)

.def(CONTRACT_BOX_METHOD(CtcPolygon,
VOID_CTCPOLYGON_CONTRACT_INTERVALVECTOR_REF_CONST))

;

py::implicitly_convertible<Polygon,CtcPolygon>();
}
4 changes: 4 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
${CMAKE_CURRENT_SOURCE_DIR}/contractors/codac2_CtcAction.cpp
${CMAKE_CURRENT_SOURCE_DIR}/contractors/codac2_CtcAction.h
${CMAKE_CURRENT_SOURCE_DIR}/contractors/codac2_CtcCartProd.h
${CMAKE_CURRENT_SOURCE_DIR}/contractors/codac2_CtcCtcBoundary.cpp
${CMAKE_CURRENT_SOURCE_DIR}/contractors/codac2_CtcCtcBoundary.h
${CMAKE_CURRENT_SOURCE_DIR}/contractors/codac2_CtcDist.cpp
${CMAKE_CURRENT_SOURCE_DIR}/contractors/codac2_CtcDist.h
${CMAKE_CURRENT_SOURCE_DIR}/contractors/codac2_CtcEllipse.cpp
Expand All @@ -35,6 +37,8 @@
${CMAKE_CURRENT_SOURCE_DIR}/contractors/codac2_CtcUnion.h
${CMAKE_CURRENT_SOURCE_DIR}/contractors/codac2_CtcPolar.cpp
${CMAKE_CURRENT_SOURCE_DIR}/contractors/codac2_CtcPolar.h
${CMAKE_CURRENT_SOURCE_DIR}/contractors/codac2_CtcPolygon.cpp
${CMAKE_CURRENT_SOURCE_DIR}/contractors/codac2_CtcPolygon.h
${CMAKE_CURRENT_SOURCE_DIR}/contractors/codac2_CtcProj.cpp
${CMAKE_CURRENT_SOURCE_DIR}/contractors/codac2_CtcProj.h
${CMAKE_CURRENT_SOURCE_DIR}/contractors/codac2_CtcSegment.cpp
Expand Down
52 changes: 52 additions & 0 deletions src/core/contractors/codac2_CtcCtcBoundary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* codac2_CtcCtcBoundary.cpp
* ----------------------------------------------------------------------------
* \date 2024
* \author Simon Rohou, Benoit Desrochers
* \copyright Copyright 2024 Codac Team
* \license GNU Lesser General Public License (LGPL)
*/

#include "codac2_CtcCtcBoundary.h"

using namespace std;
using namespace codac2;

void CtcCtcBoundary::contract(IntervalVector& x) const
{
assert_release(x.size() == this->size());

size_t attempt_nb = 5;
IntervalVector prev_x(x);
_ctc_boundary.front().contract(x);

for(const auto& b : prev_x.diff(x,false))
{
if(b.is_empty())
continue;

Vector m = b.mid(); // first try: midpoint of the box
BoolInterval d;
size_t k = 0;

do
{
d = _inside_test(m);
if(d == BoolInterval::UNKNOWN)
m = b.rand(); // now, trying a random vector in the box
} while(d == BoolInterval::UNKNOWN && k++ < attempt_nb);

switch(d)
{
case BoolInterval::TRUE:
case BoolInterval::UNKNOWN:
x |= b;
break;
case BoolInterval::FALSE:
break;
case BoolInterval::EMPTY:
assert(false && "BoolInterval::EMPTY should not happen");
break;
}
}
}
38 changes: 38 additions & 0 deletions src/core/contractors/codac2_CtcCtcBoundary.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* \file codac2_CtcCtcBoundary.h
* ----------------------------------------------------------------------------
* \date 2024
* \author Simon Rohou, Benoit Desrochers
* \copyright Copyright 2024 Codac Team
* \license GNU Lesser General Public License (LGPL)
*/

#pragma once

#include <functional>
#include "codac2_Ctc.h"
#include "codac2_IntervalVector.h"
#include "codac2_Collection.h"
#include "codac2_BoolInterval.h"
#include "codac2_template_tools.h"

namespace codac2
{
class CtcCtcBoundary : public Ctc<CtcCtcBoundary,IntervalVector>
{
public:

template<typename C>
requires IsCtcBaseOrPtr<C,IntervalVector>
CtcCtcBoundary(const C& ctc_boundary, const std::function<BoolInterval(const Vector&)>& inside_test)
: Ctc<CtcCtcBoundary,IntervalVector>(size_of(ctc_boundary)), _ctc_boundary(ctc_boundary), _inside_test(inside_test)
{ }

void contract(IntervalVector& x) const;

protected:

const Collection<CtcBase<IntervalVector>> _ctc_boundary;
const std::function<BoolInterval(const Vector&)> _inside_test;
};
}
45 changes: 45 additions & 0 deletions src/core/contractors/codac2_CtcPolygon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* CtcPolygon.cpp
* ----------------------------------------------------------------------------
* \date 2024
* \author Simon Rohou, Benoit Desrochers
* \copyright Copyright 2024 Codac Team
* \license GNU Lesser General Public License (LGPL)
*/

#include <list>
#include "codac2_CtcPolygon.h"
#include "codac2_CtcUnion.h"
#include "codac2_CtcSegment.h"
#include "codac2_geometry.h"

using namespace std;

namespace codac2
{
CtcPolygon::CtcPolygon(const Polygon& p)
: CtcCtcBoundary(

// Contractor on the boundary
[p]
{
CtcUnion<IntervalVector> ctc_segments(2);
for(const auto& edge_i : p.edges())
ctc_segments |= CtcSegment(edge_i[0],edge_i[1]);
return ctc_segments;
}(),

// Tests if a point of a box is inside the polygon
[p](const Vector& x) -> BoolInterval
{
assert_release(x.size() == 2);
return p.contains(x);
}
)
{ }

void CtcPolygon::contract(IntervalVector& x) const
{
CtcCtcBoundary::contract(x);
}
}
25 changes: 25 additions & 0 deletions src/core/contractors/codac2_CtcPolygon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* \file codac2_CtcPolygon.h
* ----------------------------------------------------------------------------
* \date 2024
* \author Simon Rohou, Benoit Desrochers
* \copyright Copyright 2024 Codac Team
* \license GNU Lesser General Public License (LGPL)
*/

#pragma once

#include "codac2_Polygon.h"
#include "codac2_CtcCtcBoundary.h"
#include "codac2_CtcUnion.h"

namespace codac2
{
class CtcPolygon : public CtcCtcBoundary
{
public:

CtcPolygon(const Polygon& p);
void contract(IntervalVector& x) const;
};
}
1 change: 1 addition & 0 deletions src/core/separators/codac2_SepPolygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <list>
#include "codac2_SepPolygon.h"
#include "codac2_CtcUnion.h"
#include "codac2_CtcSegment.h"
#include "codac2_geometry.h"

Expand Down
1 change: 0 additions & 1 deletion src/core/separators/codac2_SepPolygon.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include "codac2_Polygon.h"
#include "codac2_SepCtcBoundary.h"
#include "codac2_CtcUnion.h"

namespace codac2
{
Expand Down
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ list(APPEND SRC_TESTS # listing files without extension

core/contractors/codac2_tests_CtcAction
core/contractors/codac2_tests_CtcCartProd
core/contractors/codac2_tests_CtcCtcBoundary
core/contractors/codac2_tests_CtcFixpoint
core/contractors/codac2_tests_CtcInverse
core/contractors/codac2_tests_CtcInverseNotIn
core/contractors/codac2_tests_CtcLazy
core/contractors/codac2_tests_CtcPolygon
core/contractors/codac2_tests_CtcSegment
core/contractors/codac2_tests_linear_ctc

Expand Down
Loading

0 comments on commit 147517e

Please sign in to comment.