GenericSlicer.h 4.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
#pragma once

#include <assert.h>
#include <iostream>

#include "utils.h"

//! @brief Base class for all waypoint managers.
template <class ElementType, template <class, class...> class Container /*e.g. QVector*/>
class GenericSlicer
{
public:
    typedef Container<ElementType> ContainerType;
    GenericSlicer();


    // Slicing.
    void slice      (const ContainerType &source, ContainerType &slice);
    void next       (const ContainerType &source, ContainerType &slice);
    void previous   (const ContainerType &source, ContainerType &slice);
    void reset      (const ContainerType &source, ContainerType &slice);


    // Slicing parameters.
    //! @brief Sets the overlap.
    //!
    //! @param overlap The number of overlapping vertices
    //! between on and the next slice.
    void setOverlap            (uint32_t overlap);
    //! @brief Sets the number of vertices per slice.
    //!
    //! @param N The number of vertices per slice.
    void setN   (std::uint32_t N);
    //! @brief Sets the start index.
    //!
    //! @param idxStart The start index.
    void setStartIndex         (int idxStart);

    //! @return Returns the overlap.
    uint32_t      overlap       ();
    //! @return Returns the number of vertices per slice N.
    uint32_t      N             ();
    //! @return Returns the start index.
    int           startIndex    ();


private:
    void _updateIdx(std::size_t size);

    long          _idxStart;
    long          _idxEnd;
    long          _idxNext;
    long          _idxPrevious;
    uint32_t      _overlap;
    uint32_t      _N;
    bool          _idxValid;
    bool          _atEnd;


};


template <class ElementType, template <class, class...> class Container /*e.g. QVector*/>
GenericSlicer<ElementType, Container>::GenericSlicer():
    _idxValid(false)
  , _atEnd(false)
{}

template <class ElementType, template <class, class...> class Container /*e.g. QVector*/>
void GenericSlicer<ElementType, Container>::setOverlap(std::uint32_t overlap)
{
    _idxValid = false;
    _overlap = overlap;
}

template <class ElementType, template <class, class...> class Container /*e.g. QVector*/>
void GenericSlicer<ElementType, Container>::setN(uint32_t N)
{
    _idxValid = false;
    _N = N > 0 ? N : 1;
}

template <class ElementType, template <class, class...> class Container /*e.g. QVector*/>
void GenericSlicer<ElementType, Container>::setStartIndex(int idxStart)
{
    _idxValid = false;
    _idxStart = idxStart;
}

template <class ElementType, template <class, class...> class Container /*e.g. QVector*/>
uint32_t GenericSlicer<ElementType, Container>::overlap()
{
    return _overlap;
}

template <class ElementType, template <class, class...> class Container /*e.g. QVector*/>
uint32_t GenericSlicer<ElementType, Container>::N()
{
    return _N;
}

template <class ElementType, template <class, class...> class Container /*e.g. QVector*/>
int GenericSlicer<ElementType, Container>::startIndex()
{
    return _idxStart;
}


template <class ElementType, template <class, class...> class Container /*e.g. QVector*/>
void GenericSlicer<ElementType, Container>::_updateIdx(std::size_t size)
{
    _idxValid = true;
    _atEnd    = false;
    if ( _idxStart >= size-1 ) {
        _idxStart   = size-1;
        _idxEnd     = _idxStart;
        _idxNext    = _idxStart;
        _atEnd      = true;
        return;
    }

    _idxStart < 0 ? 0 : _idxStart;

    _idxEnd = _idxStart + _N - 1;
    _idxEnd = _idxEnd < size ? _idxEnd : size-1;

    _idxNext = _idxEnd + 1 - _overlap;
    _idxNext = _idxNext < 0    ? 0        : _idxNext;
    _idxNext = _idxNext < size ? _idxNext : size-1;

    _idxPrevious = _idxStart - 1 + _overlap;
    _idxPrevious = _idxPrevious < 0    ? 0            : _idxPrevious;
    _idxPrevious = _idxPrevious < size ? _idxPrevious : size-1;
}

template <class ElementType, template <class, class...> class Container /*e.g. QVector*/>
void GenericSlicer<ElementType, Container>::slice(const ContainerType &source,
                                                  Container<ElementType> &slice){

    if ( !_idxValid)
        _updateIdx(source.size());

    WaypointManager::Utils::extract(source, slice, _idxStart, _idxEnd);
    // extract waypoints

}

template <class ElementType, template <class, class...> class Container /*e.g. QVector*/>
void GenericSlicer<ElementType, Container>::next(const ContainerType &source, Container<ElementType> &slice){
    setStartIndex(_idxNext);
    slice(source, slice);
}

template <class ElementType, template <class, class...> class Container /*e.g. QVector*/>
void GenericSlicer<ElementType, Container>::previous(const ContainerType &source, Container<ElementType> &slice){
    setStartIndex(_idxPrevious);
    slice(source, slice);
}

template <class ElementType, template <class, class...> class Container /*e.g. QVector*/>
void GenericSlicer<ElementType, Container>::reset(const ContainerType &source, Container<ElementType> &slice){
    setStartIndex(0);
    slice(source, slice);
}