PolyLineCollectionT.hh 9.48 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
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
*                                                                            *
\*===========================================================================*/

Christopher Tenter's avatar
Christopher Tenter committed
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
#pragma once

#include <ObjectTypes/PolyLine/PolyLineT.hh>

#include <queue>

template <typename T>
class PolyLineCollectionT{
public:
    typedef typename ACG::PolyLineT<T> PolyLine;

    struct iterator{
        iterator(typename std::vector<PolyLine*>::iterator _begin, typename std::vector<PolyLine*>::iterator _end) :
            i_(0),
            it_(_begin),
            end_(_end)
        { }

        PolyLine*& operator* (){
            return *it_;
        }

        PolyLine* operator-> (){
            return *it_;
        }

        void operator++(){
            ++i_;
            ++it_;
        }

        int idx(){
            return i_;
        }

        operator bool() const{
            return it_ != end_;
        }

        int i_;
        typename std::vector<PolyLine*>::iterator it_;
        typename std::vector<PolyLine*>::iterator end_;
    };

    struct index_iterator{
87
        index_iterator(std::vector<PolyLine*>& _lines, typename std::vector<size_t>::iterator _begin, typename std::vector<size_t>::iterator _end) :
Christopher Tenter's avatar
Christopher Tenter committed
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
            lines_(_lines),
            it_(_begin),
            end_(_end)
        { }

        PolyLine*& operator* (){
            return lines_[*it_];
        }

        PolyLine* operator-> (){
            return lines_[*it_];
        }

        void operator++(){
            ++it_;
        }

        int idx(){
            return *it_;
        }

        operator bool() const{
            return it_ != end_;
        }

        std::vector<PolyLine*>& lines_;
114 115
        typename std::vector<size_t>::iterator it_;
        typename std::vector<size_t>::iterator end_;
Christopher Tenter's avatar
Christopher Tenter committed
116 117 118 119 120
    };

public:


121 122 123 124
    /** \brief Destructor
     *
     *  The destructor will call delete on all PolyLines in the PolyLine Collection!
     */
Christopher Tenter's avatar
Christopher Tenter committed
125
    virtual ~PolyLineCollectionT(){
126 127
        const size_t n = poly_lines_.size();
        for(size_t i = 0; i < n; ++i){
Christopher Tenter's avatar
Christopher Tenter committed
128 129 130 131
            delete poly_lines_[i];
        }
    }

132 133 134 135 136 137
    /** Create a new polyline in the collection
     *
     * @param _poly_line The new polyline to be added to the collection
     * @return           Index of the new polyline in the collection
     */
    size_t add_poly_line(PolyLine* _poly_line){
Christopher Tenter's avatar
Christopher Tenter committed
138

139
        ;
Christopher Tenter's avatar
Christopher Tenter committed
140
        if(empty_slots_.empty()){
141 142 143 144 145 146 147 148 149

            const size_t new_idx = poly_lines_.size();
            poly_lines_.push_back(_poly_line);
            visible_.push_back(new_idx);
            return new_idx;

        } else {

            const size_t new_idx = empty_slots_.front();
Christopher Tenter's avatar
Christopher Tenter committed
150
            empty_slots_.pop();
151 152 153
            poly_lines_[new_idx] = _poly_line;
            visible_.push_back(new_idx);
            return new_idx;
Christopher Tenter's avatar
Christopher Tenter committed
154

155
        }
Christopher Tenter's avatar
Christopher Tenter committed
156 157 158

    }

159 160 161 162
    /** \brief Create empty polyline and return id
     *
     * @return Index of new polyline in collection
     */
Christopher Tenter's avatar
Christopher Tenter committed
163
    int new_poly_line(){
164

Christopher Tenter's avatar
Christopher Tenter committed
165 166 167 168 169 170 171 172 173 174 175
        PolyLine* pl = new PolyLine();
        pl->request_vertex_selections();
        pl->request_edge_selections();
        pl->request_vertex_vhandles();
        pl->request_vertex_ehandles();
        pl->request_vertex_fhandles();
        pl->request_edge_scalars();

        return add_poly_line(pl);
    }

176 177
    /// Reserve space for additional _count polylines
    void reserve(size_t _count){
Christopher Tenter's avatar
Christopher Tenter committed
178 179 180 181
        poly_lines_.reserve(poly_lines_.size() + _count);
    }


182 183 184 185 186 187
     //===========================================================================
     /** @name Access functions
     * @{ */
     //===========================================================================
      inline size_t n_polylines(){return poly_lines_.size();}
      inline PolyLine* polyline(size_t _i){return poly_lines_[_i];}
Christopher Tenter's avatar
Christopher Tenter committed
188

189 190 191
      inline size_t n_visible_polylines(){return visible_.size();}
      inline PolyLine* visible_polyline(size_t _i){return poly_lines_[visible_[_i]];}
    /** @} */
Christopher Tenter's avatar
Christopher Tenter committed
192

193 194 195 196 197 198 199 200 201 202 203 204 205

    //===========================================================================
    /** @name Iterators
    * @{ */
    //===========================================================================
      inline iterator iter(){return iterator(poly_lines_.begin(), poly_lines_.end());}
      inline index_iterator visible_iter(){return index_iterator(poly_lines_, visible_.begin(), visible_.end());}
      inline index_iterator selected_iter(){return index_iterator(poly_lines_, selected_.begin(), selected_.end());}
    /** @} */

    /** \brief Set all polylines to visible
     *
     */
Christopher Tenter's avatar
Christopher Tenter committed
206 207
    void set_visible_all()
    {
208
        size_t n_lines = poly_lines_.size();
Christopher Tenter's avatar
Christopher Tenter committed
209
        visible_.resize(n_lines);
210
        for(size_t i = 0; i < n_lines; ++i){
Christopher Tenter's avatar
Christopher Tenter committed
211 212 213
            visible_[i] = i;
        }
    }
214 215 216 217 218 219

    /** \brief Set given polylines to visible
     *
     * @param _visible PolyLine indices to set visible
     */
    void set_visible(const std::vector<size_t>& _visible)
Christopher Tenter's avatar
Christopher Tenter committed
220 221 222 223
    {
        visible_ = _visible;
    }

224 225 226 227 228
    /** \brief Set given polylines to selected
     *
     * @param _selected PolyLine indices to set selected
     */
    void set_selected(const std::vector<size_t>& _selected)
Christopher Tenter's avatar
Christopher Tenter committed
229 230 231 232
    {
        selected_ = _selected;
    }

233
    /// unselect all polylines in collection
Christopher Tenter's avatar
Christopher Tenter committed
234 235 236 237
    void clear_selection(){
        selected_.clear();
    }

238 239 240 241
    /** \brief Clear Collection
     *
     * This will call delete on all polylines in the collection!
     */
Christopher Tenter's avatar
Christopher Tenter committed
242 243 244 245 246 247 248
    void clear(){
        for (size_t i = 0; i < poly_lines_.size(); ++i)
        {
            delete poly_lines_[i];
        }
        poly_lines_.clear();
        visible_.clear();
249

Christopher Tenter's avatar
Christopher Tenter committed
250 251 252
        while(!empty_slots_.empty()) empty_slots_.pop();
    }

253 254 255 256 257 258
    /** \brief Remove one polyline from the collection
     *
     * This will also call delete on the given polyline!
     *
     * @param _id Id of the polyline to be removed
     */
Christopher Tenter's avatar
Christopher Tenter committed
259 260 261 262 263 264 265 266
    void remove_polyline(int _id){
        if(_id < int(poly_lines_.size()) && poly_lines_[_id] != 0){
            delete poly_lines_[_id];
            poly_lines_[_id] = 0;
            empty_slots_.push(_id);
        }
    }

267 268 269 270 271 272 273
    //===========================================================================
    /** @name Coloring
    * @{ */
    //===========================================================================
      inline void set_color(size_t _edge_scalar, const ACG::Vec4uc& _color) {color_map_[_edge_scalar] = _color;}
      inline ACG::Vec4uc color(size_t _edge_scalar) {return color_map_[_edge_scalar];}
    /** @} */
Christopher Tenter's avatar
Christopher Tenter committed
274 275 276

protected:
    std::vector<PolyLine*> poly_lines_;
277 278
    std::vector<size_t> visible_;
    std::vector<size_t> selected_;
Christopher Tenter's avatar
Christopher Tenter committed
279

280
    std::queue<size_t> empty_slots_;
Christopher Tenter's avatar
Christopher Tenter committed
281 282

    // Color map for edge scalars
283
    std::map<size_t, ACG::Vec4uc> color_map_;
Christopher Tenter's avatar
Christopher Tenter committed
284
};