Skip to content

Commit

Permalink
[ctc] New implementation of CtcUnion
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonRohou committed Feb 11, 2024
1 parent 3b13a0f commit 946bfd0
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 11 deletions.
33 changes: 30 additions & 3 deletions python/src/core/contractors/static/codac_py_Ctc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ py::class_<Ctc,pyCtc> export_Ctc(py::module& m)

.def("__or__", [](Ctc& c1, Ctc& c2)
{
return new CtcUnion(c1, c2);
CtcUnion *cu = new CtcUnion(2);
cu->add_raw_ptr(&c1);
cu->add_raw_ptr(&c2);
// todo: manage delete
return cu;
},
DOC_CTC_OR,
py::return_value_policy::take_ownership,
Expand All @@ -77,9 +80,33 @@ py::class_<Ctc,pyCtc> export_Ctc(py::module& m)
;

// Export CtcUnion
py::class_<CtcUnion>(m, "CtcUnion", ctc, DOC_CTCUNION_TYPE)
.def(py::init<ibex::Array<Ctc>>(), py::keep_alive<1,2>(), "list"_a)
py::class_<CtcUnion>(m, "CtcUnion", ctc, "todo")
.def(py::init([](Ctc& c1)
{
CtcUnion *cu = new CtcUnion(c1.nb_var);
cu->add_raw_ptr(&c1);
return cu;
}),
py::keep_alive<0,1>(), "c1"_a)
.def(py::init([](Ctc& c1, Ctc& c2)
{
CtcUnion *cu = new CtcUnion(c1.nb_var);
cu->add_raw_ptr(&c1);
cu->add_raw_ptr(&c2);
return cu;
}),
py::keep_alive<0,1>(), py::keep_alive<0,2>(), "c1"_a, "c2"_a)
.def(py::init([](Ctc& c1, Ctc& c2, Ctc& c3)
{
CtcUnion *cu = new CtcUnion(c1.nb_var);
cu->add_raw_ptr(&c1);
cu->add_raw_ptr(&c2);
cu->add_raw_ptr(&c3);
return cu;
}),
py::keep_alive<0,1>(), py::keep_alive<0,2>(), py::keep_alive<0,3>(), "c1"_a, "c2"_a, "c3"_a)
.def("contract", (void (Ctc::*)(IntervalVector&)) &CtcUnion::contract)
.def("__ior__", [](CtcUnion& cu, Ctc& c) { return cu.add_raw_ptr(&c); })
;

// Export CtcCompo
Expand Down
8 changes: 7 additions & 1 deletion src/core/contractors/static/codac_CtcSegment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ using namespace std;
namespace codac {

CtcSegment::CtcSegment(double ax, double ay, double bx, double by) : Ctc(2),
X_with_params(2+4) {
X_with_params(2+4)
{

init();

Expand All @@ -24,6 +25,11 @@ CtcSegment::CtcSegment(double ax, double ay, double bx, double by) : Ctc(2),
X_with_params[5] = Interval(by);
}

CtcSegment::CtcSegment(const CtcSegment& ctc) : CtcSegment(ctc.X_with_params[2].lb(),ctc.X_with_params[3].lb(),ctc.X_with_params[4].lb(),ctc.X_with_params[5].lb())
{

}

CtcSegment::CtcSegment() : Ctc(6), X_with_params(0 /* unused */) {
init();
}
Expand Down
8 changes: 5 additions & 3 deletions src/core/contractors/static/codac_CtcSegment.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
#include "ibex_CtcFwdBwd.h"


namespace codac {


using ibex::Interval;
using ibex::IntervalVector;
using ibex::Ctc;
using ibex::NumConstraint;
using ibex::CtcFwdBwd;


namespace codac {

/**
* \ingroup geometry
*
Expand Down Expand Up @@ -58,6 +58,8 @@ class CtcSegment : public Ctc {
*/
CtcSegment();

CtcSegment(const CtcSegment& ctc);

/**
* \brief Contract a box.
* \param box to be contracted
Expand Down
69 changes: 65 additions & 4 deletions src/core/contractors/static/codac_CtcUnion.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,82 @@
* \file
* CtcUnion class
* ----------------------------------------------------------------------------
* \date 2022
* \date 2024
* \author Simon Rohou
* \copyright Copyright 2022 Codac Team
* \copyright Copyright 2024 Codac Team
* \license This program is distributed under the terms of
* the GNU Lesser General Public License (LGPL).
*/

#ifndef __CODAC_CTCUNION_H__
#define __CODAC_CTCUNION_H__

#include "ibex_CtcUnion.h"
#include <memory>
//#include "ibex_CtcUnion.h"
#include "codac_Ctc.h"

namespace codac
{
using ibex::CtcUnion;
class CtcUnion : public Ctc
{
public:

CtcUnion(int nb_var) : Ctc(nb_var)
{ }

template<typename C1>
CtcUnion(const C1& c1) : Ctc(c1.nb_var)
{
_v_ctc.push_back(std::make_shared<C1>(c1));
}

template<typename C1,typename... C>
CtcUnion(const C1& c1, const C&... c) : CtcUnion(c1)
{
(_v_ctc.push_back(std::make_shared<C>(c)), ...);
for(const auto& ci : _v_ctc) { assert(ci->nb_var == nb_var); }
}

void contract(IntervalVector& x)
{
IntervalVector result(nb_var, Interval::empty_set());

for(auto& ci : _v_ctc)
{
IntervalVector saved_x = x;
ci->contract(saved_x);
result |= saved_x;
}

for(auto& ci : _v_ctc_ptrs)
{
IntervalVector saved_x = x;
ci->contract(saved_x);
result |= saved_x;
}

x = result;
}

template<typename C>
CtcUnion& operator|=(const C& c)
{
assert(c.nb_var == nb_var());
_v_ctc.push_back(std::make_shared<C>(c));
return *this;
}

CtcUnion& add_raw_ptr(Ctc *c)
{
_v_ctc_ptrs.push_back(c);
return *this;
}

protected:

std::vector<std::shared_ptr<Ctc>> _v_ctc;
std::vector<Ctc*> _v_ctc_ptrs;
};
}

#endif

0 comments on commit 946bfd0

Please sign in to comment.