Box partitioning with arrays of boost::intervals

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I need to write a function that takes a box in space, represented as an std::vector of boost::intervals, and split it into several smaller boxes.
First, for convenience, I made some definitions



#include <iostream>
#include <boost/numeric/interval.hpp>
#include <vector>

namespace bn = boost::numeric;
namespace bi = bn::interval_lib;

// Interval typedefs
using Interval = bn::interval<
double,
bi::policies<
bi::save_state<bi::rounded_transc_std<double> >,
bi::checking_base<double>
>
>;

template<typename T> using array = std::vector<T>;


Then, I shamelessly used code from this stackoverflow question that describes how to achieve a Cartesian product of a set (vector) of sets (vectors). I turned it into a Template (for generality purpose) like so:



template <class T>
class Combinator

public:
Combinator(array<array<T>>& tsttors)
: m_tsttors(tsttors)

m_combination.reserve(m_tsttors.size());
for(auto& v : m_tsttors)
m_combination.push_back(v.begin());


bool next()
// iterate through tsttors in reverse order
for(int i = m_tsttors.size() - 1; i >= 0; --i)
array<T>& v = m_tsttors[i];
typename array<T>::iterator& it = m_combination[i];

if(++it != v.end())
return true;
it = v.begin();

return false;


array<typename array<T>::iterator> combination() const
return m_combination;


private:
array<array<T>>& m_tsttors; // reference to data
array<typename array<T>::iterator> m_combination;
;


After that, I decided I would split each dimension of the box (which is not of fixed dimension) into some number n of subintervals and then take their cartesian product to get what I called a grid of boxes. I implemented this like:



array<array<Interval>> box_grid(const array<Interval> &box, const int &parts)
array<array<Interval>> splits;
for(const auto& components : box) // iterating through all box-dimensions
const double &length = width(components);
const double &start = components.lower();
array<Interval> split_oned; // split each dimension

for(int i = 0; i< parts; i++) // evenly into "parts" parts
Interval ival start+ i*length/parts, start + (i+1)*length/parts;
split_oned.push_back(ival);

splits.push_back(split_oned); // collect each split dimension


// and take their cartesian product for the boxes
Combinator<Interval> combinator(splits);
array<array<Interval>> box_array;
do

const array<array<Interval>::iterator> combination = combinator.combination();
array<Interval> single_box;
for(const auto& it : combination)
single_box.push_back(*it);

box_array.push_back(single_box);
while(combinator.next());

return box_array;



As I am new to C++ and not very adept at programming in general, I would love to have some feedback and suggestions on improvement. I will use this code for scientific purposes, so performance is crucial.
Thanks in advance.







share|improve this question



























    up vote
    1
    down vote

    favorite












    I need to write a function that takes a box in space, represented as an std::vector of boost::intervals, and split it into several smaller boxes.
    First, for convenience, I made some definitions



    #include <iostream>
    #include <boost/numeric/interval.hpp>
    #include <vector>

    namespace bn = boost::numeric;
    namespace bi = bn::interval_lib;

    // Interval typedefs
    using Interval = bn::interval<
    double,
    bi::policies<
    bi::save_state<bi::rounded_transc_std<double> >,
    bi::checking_base<double>
    >
    >;

    template<typename T> using array = std::vector<T>;


    Then, I shamelessly used code from this stackoverflow question that describes how to achieve a Cartesian product of a set (vector) of sets (vectors). I turned it into a Template (for generality purpose) like so:



    template <class T>
    class Combinator

    public:
    Combinator(array<array<T>>& tsttors)
    : m_tsttors(tsttors)

    m_combination.reserve(m_tsttors.size());
    for(auto& v : m_tsttors)
    m_combination.push_back(v.begin());


    bool next()
    // iterate through tsttors in reverse order
    for(int i = m_tsttors.size() - 1; i >= 0; --i)
    array<T>& v = m_tsttors[i];
    typename array<T>::iterator& it = m_combination[i];

    if(++it != v.end())
    return true;
    it = v.begin();

    return false;


    array<typename array<T>::iterator> combination() const
    return m_combination;


    private:
    array<array<T>>& m_tsttors; // reference to data
    array<typename array<T>::iterator> m_combination;
    ;


    After that, I decided I would split each dimension of the box (which is not of fixed dimension) into some number n of subintervals and then take their cartesian product to get what I called a grid of boxes. I implemented this like:



    array<array<Interval>> box_grid(const array<Interval> &box, const int &parts)
    array<array<Interval>> splits;
    for(const auto& components : box) // iterating through all box-dimensions
    const double &length = width(components);
    const double &start = components.lower();
    array<Interval> split_oned; // split each dimension

    for(int i = 0; i< parts; i++) // evenly into "parts" parts
    Interval ival start+ i*length/parts, start + (i+1)*length/parts;
    split_oned.push_back(ival);

    splits.push_back(split_oned); // collect each split dimension


    // and take their cartesian product for the boxes
    Combinator<Interval> combinator(splits);
    array<array<Interval>> box_array;
    do

    const array<array<Interval>::iterator> combination = combinator.combination();
    array<Interval> single_box;
    for(const auto& it : combination)
    single_box.push_back(*it);

    box_array.push_back(single_box);
    while(combinator.next());

    return box_array;



    As I am new to C++ and not very adept at programming in general, I would love to have some feedback and suggestions on improvement. I will use this code for scientific purposes, so performance is crucial.
    Thanks in advance.







    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I need to write a function that takes a box in space, represented as an std::vector of boost::intervals, and split it into several smaller boxes.
      First, for convenience, I made some definitions



      #include <iostream>
      #include <boost/numeric/interval.hpp>
      #include <vector>

      namespace bn = boost::numeric;
      namespace bi = bn::interval_lib;

      // Interval typedefs
      using Interval = bn::interval<
      double,
      bi::policies<
      bi::save_state<bi::rounded_transc_std<double> >,
      bi::checking_base<double>
      >
      >;

      template<typename T> using array = std::vector<T>;


      Then, I shamelessly used code from this stackoverflow question that describes how to achieve a Cartesian product of a set (vector) of sets (vectors). I turned it into a Template (for generality purpose) like so:



      template <class T>
      class Combinator

      public:
      Combinator(array<array<T>>& tsttors)
      : m_tsttors(tsttors)

      m_combination.reserve(m_tsttors.size());
      for(auto& v : m_tsttors)
      m_combination.push_back(v.begin());


      bool next()
      // iterate through tsttors in reverse order
      for(int i = m_tsttors.size() - 1; i >= 0; --i)
      array<T>& v = m_tsttors[i];
      typename array<T>::iterator& it = m_combination[i];

      if(++it != v.end())
      return true;
      it = v.begin();

      return false;


      array<typename array<T>::iterator> combination() const
      return m_combination;


      private:
      array<array<T>>& m_tsttors; // reference to data
      array<typename array<T>::iterator> m_combination;
      ;


      After that, I decided I would split each dimension of the box (which is not of fixed dimension) into some number n of subintervals and then take their cartesian product to get what I called a grid of boxes. I implemented this like:



      array<array<Interval>> box_grid(const array<Interval> &box, const int &parts)
      array<array<Interval>> splits;
      for(const auto& components : box) // iterating through all box-dimensions
      const double &length = width(components);
      const double &start = components.lower();
      array<Interval> split_oned; // split each dimension

      for(int i = 0; i< parts; i++) // evenly into "parts" parts
      Interval ival start+ i*length/parts, start + (i+1)*length/parts;
      split_oned.push_back(ival);

      splits.push_back(split_oned); // collect each split dimension


      // and take their cartesian product for the boxes
      Combinator<Interval> combinator(splits);
      array<array<Interval>> box_array;
      do

      const array<array<Interval>::iterator> combination = combinator.combination();
      array<Interval> single_box;
      for(const auto& it : combination)
      single_box.push_back(*it);

      box_array.push_back(single_box);
      while(combinator.next());

      return box_array;



      As I am new to C++ and not very adept at programming in general, I would love to have some feedback and suggestions on improvement. I will use this code for scientific purposes, so performance is crucial.
      Thanks in advance.







      share|improve this question













      I need to write a function that takes a box in space, represented as an std::vector of boost::intervals, and split it into several smaller boxes.
      First, for convenience, I made some definitions



      #include <iostream>
      #include <boost/numeric/interval.hpp>
      #include <vector>

      namespace bn = boost::numeric;
      namespace bi = bn::interval_lib;

      // Interval typedefs
      using Interval = bn::interval<
      double,
      bi::policies<
      bi::save_state<bi::rounded_transc_std<double> >,
      bi::checking_base<double>
      >
      >;

      template<typename T> using array = std::vector<T>;


      Then, I shamelessly used code from this stackoverflow question that describes how to achieve a Cartesian product of a set (vector) of sets (vectors). I turned it into a Template (for generality purpose) like so:



      template <class T>
      class Combinator

      public:
      Combinator(array<array<T>>& tsttors)
      : m_tsttors(tsttors)

      m_combination.reserve(m_tsttors.size());
      for(auto& v : m_tsttors)
      m_combination.push_back(v.begin());


      bool next()
      // iterate through tsttors in reverse order
      for(int i = m_tsttors.size() - 1; i >= 0; --i)
      array<T>& v = m_tsttors[i];
      typename array<T>::iterator& it = m_combination[i];

      if(++it != v.end())
      return true;
      it = v.begin();

      return false;


      array<typename array<T>::iterator> combination() const
      return m_combination;


      private:
      array<array<T>>& m_tsttors; // reference to data
      array<typename array<T>::iterator> m_combination;
      ;


      After that, I decided I would split each dimension of the box (which is not of fixed dimension) into some number n of subintervals and then take their cartesian product to get what I called a grid of boxes. I implemented this like:



      array<array<Interval>> box_grid(const array<Interval> &box, const int &parts)
      array<array<Interval>> splits;
      for(const auto& components : box) // iterating through all box-dimensions
      const double &length = width(components);
      const double &start = components.lower();
      array<Interval> split_oned; // split each dimension

      for(int i = 0; i< parts; i++) // evenly into "parts" parts
      Interval ival start+ i*length/parts, start + (i+1)*length/parts;
      split_oned.push_back(ival);

      splits.push_back(split_oned); // collect each split dimension


      // and take their cartesian product for the boxes
      Combinator<Interval> combinator(splits);
      array<array<Interval>> box_array;
      do

      const array<array<Interval>::iterator> combination = combinator.combination();
      array<Interval> single_box;
      for(const auto& it : combination)
      single_box.push_back(*it);

      box_array.push_back(single_box);
      while(combinator.next());

      return box_array;



      As I am new to C++ and not very adept at programming in general, I would love to have some feedback and suggestions on improvement. I will use this code for scientific purposes, so performance is crucial.
      Thanks in advance.









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jan 29 at 12:31
























      asked Jan 29 at 11:53









      bernhard_e

      162




      162

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186249%2fbox-partitioning-with-arrays-of-boostintervals%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186249%2fbox-partitioning-with-arrays-of-boostintervals%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Python Lists

          Aion

          JavaScript Array Iteration Methods