File: connected_components_iterators.hpp

package info (click to toggle)
gamera 1:3.4.2+git20160808.1725654-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 22,312 kB
  • ctags: 24,991
  • sloc: xml: 122,324; ansic: 52,869; cpp: 50,664; python: 35,034; makefile: 118; sh: 101
file content (603 lines) | stat: -rw-r--r-- 18,713 bytes parent folder | download | duplicates (4)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/*
 *
 * Copyright (C) 2001-2005 Ichiro Fujinaga, Michael Droettboom, Karl MacMillan
 *               2009      Jonathan Koch
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef kwm01022002_connected_component_iterators_hpp
#define kwm01022002_connected_component_iterators_hpp

#include "accessor.hpp"
#include "iterator_base.hpp"
#include <map>

namespace Gamera {
  namespace CCDetail {

    /*
      CCProxy

      This class is used so that the filtered assignment and derefencing can
      happen for the ColIterator and RowIterator (which require that the values returned
      from operator *, ->, and [] return an lvalue). So far it has worked well, but
      more testing probably needs to be done.

      The basic idea is that instead of returning a reference to a value in the image_data, we
      return an object that holds a reference to the data and the label for the ConnectedComponent.
      The object contains a conversion operator and an assignment operator. When conversion to the
      type of the value is requested, the label is checked against the value. If they match, then the
      value is returned, otherwise 0 is returned. Assignment makes similar checks. This type of
      proxying can cause numerous problems, so care is required. KWM
    */

    template<class T, class I>
    class CCProxy {
    public:
      /*
        constructor - we store a pointer to the value so that
        changing the ConnectedComponent from another place (say
        the set function of ConnectedComponent) doesn't invalidate
        this proxy object. This is probably not a big concern. KWM
      */
      CCProxy(I i, T label) : m_iter(i), m_label(label) { }
      // conversion to T
      operator T() const {
        T tmp = m_accessor(m_iter);
        if (tmp == m_label)
          return tmp;
        else
          return 0;
      }
      // assignment only happens if the label matches
      void operator=(T value) {
        if (m_accessor(m_iter) == m_label)
          m_accessor.set(value, m_iter);
        }
    private:
      I m_iter;
      T m_label;
      ImageAccessor<T> m_accessor;
    };
    
    template<class Image, class T> class ColIterator;

    template<class Image, class T>
    class RowIterator : public RowIteratorBase<Image, RowIterator<Image, T>, T> {
    public:
      using RowIteratorBase<Image, RowIterator<Image, T>, T>::m_iterator;
      using RowIteratorBase<Image, RowIterator<Image, T>, T>::m_image;

      // Typedefs for rows
      typedef ColIterator<Image, T> iterator;
      typedef typename Image::value_type value_type;
      typedef ImageAccessor<value_type> accessor;

      typedef RowIterator self;
      typedef RowIteratorBase<Image, self, T> base;
      typedef CCProxy<value_type, T> proxy_type;

      // Constructor
      RowIterator(Image* image, const T iterator) : base(image, iterator) { }
      RowIterator() { }

      proxy_type operator*() const {
        return proxy_type(m_iterator, m_image->label());
      }

      value_type get() const {
      if (m_accessor(m_iterator) == m_image->label())
        return m_accessor(m_iterator);
      else
        return 0;
      }

      void set(const value_type& v) {
      if (m_accessor(m_iterator) == m_image->label())
        m_accessor.set(v, m_iterator);
      }

      iterator begin() const {
        return iterator(m_image, m_iterator);
      }

      iterator end() const {
        return iterator(m_image, m_iterator + m_image->ncols());
      }
    private:
      accessor m_accessor;
    };

    template<class Image, class T>
    class ColIterator : public ColIteratorBase<Image, ColIterator<Image, T>, T> {
    public:
      using ColIteratorBase<Image, ColIterator<Image, T>, T>::m_iterator;
      using ColIteratorBase<Image, ColIterator<Image, T>, T>::m_image;

      // Typedefs for Cols
      typedef RowIterator<Image, T> iterator;
      typedef typename Image::value_type value_type;
      typedef ImageAccessor<value_type> accessor;

      // Convenience typedefs
      typedef ColIterator self;
      typedef ColIteratorBase<Image, self, T> base;
      typedef CCProxy<value_type, T> proxy_type;

      // Constructor
      ColIterator(Image* image, const T iterator) : base(image, iterator) { }
      ColIterator() { }

      proxy_type operator*() const {
      return proxy_type(m_iterator, m_image->label());
      }      

      // Image specific
      value_type get() const {
      if (m_accessor(m_iterator) == m_image->label())
        return m_accessor(m_iterator);
      else
        return 0;
      }
      
      void set(const value_type& v) {
        if (m_accessor(m_iterator) == m_image->label())
          m_accessor.set(v, m_iterator);
      }

      iterator begin() const {
        return iterator(m_image, m_iterator);
      }
      iterator end() const {
        return iterator(m_image, m_iterator) + m_image->nrows();
      }
    private:
      accessor m_accessor;
    };

    template<class Image, class T> class ConstColIterator;

    template<class Image, class T>
    class ConstRowIterator : public RowIteratorBase<Image, ConstRowIterator<Image, T>, T> {
    public:
      using RowIteratorBase<Image, ConstRowIterator<Image, T>, T>::m_iterator;
      using RowIteratorBase<Image, ConstRowIterator<Image, T>, T>::m_image;

      // Typedefs for rows
      typedef ConstColIterator<Image, T> iterator;

      // Convenience typedefs
      typedef ConstRowIterator self;
      typedef RowIteratorBase<Image, self, T> base;
      typedef typename Image::value_type value_type;
      typedef ImageAccessor<value_type> accessor;

      // Constructor
      ConstRowIterator(Image* image, const T iterator) : base(image, iterator) { }
      ConstRowIterator() { }

      typename Image::value_type operator*() const {
      return get();
      }

      value_type get() const {
      if (m_accessor(m_iterator) == m_image->label())
        return m_accessor(m_iterator);
      else
        return 0;
      }

      iterator begin() const {
        return iterator(m_image, m_iterator);
      }

      iterator end() const {
        return iterator(m_image, m_iterator + m_image->ncols());
      }
    private:
      accessor m_accessor;
    };

    template<class Image, class T>
    class ConstColIterator : public ColIteratorBase<Image, ConstColIterator<Image, T>, T> {
    public:
      using ColIteratorBase<Image, ConstColIterator<Image, T>, T>::m_iterator;
      using ColIteratorBase<Image, ConstColIterator<Image, T>, T>::m_image;

      // Typedefs for Cols
      typedef ConstRowIterator<Image, T> iterator;

      // Convenience typedefs
      typedef ConstColIterator self;
      typedef ColIteratorBase<Image, self, T> base;
      typedef typename Image::value_type value_type;
      typedef ImageAccessor<value_type> accessor;

      // Constructor
      ConstColIterator(Image* image, const T iterator) : base(image, iterator) { }
      ConstColIterator() { }

      typename Image::value_type operator*() const {
        return get();
      }

      // Image specific
      value_type get() const {
        if (m_accessor(m_iterator) == m_image->label())
          return m_accessor(m_iterator);
        else
          return 0;
      }

      iterator begin() const {
        return iterator(m_image, m_iterator);
      }
      iterator end() const {
        return iterator(m_image, m_iterator) + m_image->nrows();
      }
    private:
      accessor m_accessor;
    };


    template<class Image, class Row, class Col>
    class VecIterator : public VecIteratorBase<Image, Row, Col,
					       VecIterator<Image, Row, Col> > {
    public:
      using VecIteratorBase<Image, Row, Col, VecIterator<Image, Row, Col> >::m_coliterator;

      typedef VecIterator self;
      typedef VecIteratorBase<Image, Row, Col, self> base;
      typedef typename Image::value_type value_type;
      typedef CCProxy<value_type, typename Image::data_type::iterator> proxy_type;
      typedef ImageAccessor<value_type> accessor;

      // Constructor
      VecIterator(const Row iterator) : base(iterator) { }
      VecIterator() { }

      // Operators
      proxy_type operator*() const {
        return proxy_type(m_coliterator.m_iterator, m_coliterator.m_image->label());
      }

      value_type get() const {
      if (m_accessor(m_coliterator) == m_coliterator.m_image->label())
        return m_accessor(m_coliterator);
      else
        return 0;
      }
      
      void set(const value_type& v) {
        if (m_accessor(m_coliterator) == m_coliterator.m_image->label())
          m_accessor.set(v, m_coliterator);
      }
    private:
      accessor m_accessor;
    };

    template<class Image, class Row, class Col>
    class ConstVecIterator : public VecIteratorBase<Image, Row, Col,
						    ConstVecIterator<Image, Row, Col> > {
    public:
      using VecIteratorBase<Image, Row, Col, ConstVecIterator<Image, Row, Col> >::m_coliterator;

      typedef ConstVecIterator self;
      typedef VecIteratorBase<Image, Row, Col, self> base;
      typedef typename Image::value_type value_type;
      typedef ImageAccessor<value_type> accessor;

      // Constructor
      ConstVecIterator(const Row iterator) : base(iterator) { }
      ConstVecIterator() { }

      // Operators
      typename Image::value_type operator*() const {
        return get();
      }

      value_type get() const {
        if (m_accessor(m_coliterator) == m_coliterator.m_image->label())
          return m_accessor(m_coliterator);
        else
          return 0;
      }
    private:
      accessor m_accessor;
    };
  } // namespace



/*****************************************************************************/

  namespace MLCCDetail {

    template<class T, class I>
    class MLCCProxy {
    public:
      MLCCProxy(I i, const typename std::map<T, Rect*> *labels) : m_iter(i){ 
        this->m_labels=labels;
      }

      // conversion to T
      operator T() {
        T tmp = m_accessor(m_iter);
        if (m_labels->find(tmp) != m_labels->end())
          return tmp;
        else
          return 0;
      }
      // assignment only happens if the label matches
      void operator=(T value) {
        T tmp=m_accessor(m_iter);
        if (m_labels->find(tmp) != m_labels->end())
          m_accessor.set(value, m_iter);
        }
    private:
      I m_iter;
      const typename std::map<T, Rect*> *m_labels;
      ImageAccessor<T> m_accessor;
    };
    
    template<class Image, class T> class ColIterator;

    template<class Image, class T>
    class RowIterator : public RowIteratorBase<Image, RowIterator<Image, T>, T> {
    public:
      using RowIteratorBase<Image, RowIterator<Image, T>, T>::m_iterator;
      using RowIteratorBase<Image, RowIterator<Image, T>, T>::m_image;

      // Typedefs for rows
      typedef ColIterator<Image, T> iterator;
      typedef typename Image::value_type value_type;
      typedef ImageAccessor<value_type> accessor;

      typedef RowIterator self;
      typedef RowIteratorBase<Image, self, T> base;
      typedef MLCCProxy<value_type, T> proxy_type;

      // Constructor
      RowIterator(Image* image, const T iterator) : base(image, iterator) { }
      RowIterator() { }

      proxy_type operator*() const {
        return proxy_type(m_iterator, m_image->get_labels_pointer());
      }

      value_type get() const {
        if (m_image->has_label(m_accessor(m_iterator)))
          return m_accessor(m_iterator);
        else
          return 0;
      }

      void set(const value_type& v) {
        if (m_image->has_label(m_accessor(m_iterator)))
          m_accessor.set(v, m_iterator);
      }

      iterator begin() const {
        return iterator(m_image, m_iterator);
      }

      iterator end() const {
        return iterator(m_image, m_iterator + m_image->ncols());
      }
    private:
      accessor m_accessor;
    };

    template<class Image, class T>
    class ColIterator : public ColIteratorBase<Image, ColIterator<Image, T>, T> {
    public:
      using ColIteratorBase<Image, ColIterator<Image, T>, T>::m_iterator;
      using ColIteratorBase<Image, ColIterator<Image, T>, T>::m_image;

      // Typedefs for Cols
      typedef RowIterator<Image, T> iterator;
      typedef typename Image::value_type value_type;
      typedef ImageAccessor<value_type> accessor;

      // Convenience typedefs
      typedef ColIterator self;
      typedef ColIteratorBase<Image, self, T> base;
      typedef MLCCProxy<value_type, T> proxy_type;

      // Constructor
      ColIterator(Image* image, const T iterator) : base(image, iterator) { }
      ColIterator() { }

      proxy_type operator*() const {
        return proxy_type(m_iterator, m_image->get_labels_pointer());
      }      

      // Image specific
      value_type get() const {
      if (m_image->has_label(m_accessor(m_iterator)))
        return m_accessor(m_iterator);
      else
        return 0;
      }
      
      void set(const value_type& v) {
        if (m_image->has_label(m_accessor(m_iterator)))
          m_accessor.set(v, m_iterator);
      }

      iterator begin() const {
        return iterator(m_image, m_iterator);
      }
      iterator end() const {
        return iterator(m_image, m_iterator) + m_image->nrows();
      }
    private:
      accessor m_accessor;
    };

    template<class Image, class T> class ConstColIterator;

    template<class Image, class T>
    class ConstRowIterator : public RowIteratorBase<Image, ConstRowIterator<Image, T>, T> {
    public:
      using RowIteratorBase<Image, ConstRowIterator<Image, T>, T>::m_iterator;
      using RowIteratorBase<Image, ConstRowIterator<Image, T>, T>::m_image;

      // Typedefs for rows
      typedef ConstColIterator<Image, T> iterator;

      // Convenience typedefs
      typedef ConstRowIterator self;
      typedef RowIteratorBase<Image, self, T> base;
      typedef typename Image::value_type value_type;
      typedef ImageAccessor<value_type> accessor;

      // Constructor
      ConstRowIterator(Image* image, const T iterator) : base(image, iterator) { }
      ConstRowIterator() { }

      typename Image::value_type operator*() const {
        return get();
      }

      value_type get() const {
        if (m_image->has_label(m_accessor(m_iterator)))
          return m_accessor(m_iterator);
        else
          return 0;
      }

      iterator begin() const {
        return iterator(m_image, m_iterator);
      }

      iterator end() const {
        return iterator(m_image, m_iterator + m_image->ncols());
      }
    private:
      accessor m_accessor;
    };

    template<class Image, class T>
    class ConstColIterator : public ColIteratorBase<Image, ConstColIterator<Image, T>, T> {
    public:
      using ColIteratorBase<Image, ConstColIterator<Image, T>, T>::m_iterator;
      using ColIteratorBase<Image, ConstColIterator<Image, T>, T>::m_image;

      // Typedefs for Cols
      typedef ConstRowIterator<Image, T> iterator;

      // Convenience typedefs
      typedef ConstColIterator self;
      typedef ColIteratorBase<Image, self, T> base;
      typedef typename Image::value_type value_type;
      typedef ImageAccessor<value_type> accessor;

      // Constructor
      ConstColIterator(Image* image, const T iterator) : base(image, iterator) { }
      ConstColIterator() { }

      typename Image::value_type operator*() const {
        return get();
      }

      // Image specific
      value_type get() const {
        if (m_image->has_label(m_accessor(m_iterator)))
          return m_accessor(m_iterator);
        else
          return 0;
      }

      iterator begin() const {
        return iterator(m_image, m_iterator);
      }
      iterator end() const {
        return iterator(m_image, m_iterator) + m_image->nrows();
      }
    private:
      accessor m_accessor;
    };


    template<class Image, class Row, class Col>
    class VecIterator : public VecIteratorBase<Image, Row, Col,
					       VecIterator<Image, Row, Col> > {
    public:
      using VecIteratorBase<Image, Row, Col, VecIterator<Image, Row, Col> >::m_coliterator;

      typedef VecIterator self;
      typedef VecIteratorBase<Image, Row, Col, self> base;
      typedef typename Image::value_type value_type;
      typedef MLCCProxy<value_type, typename Image::data_type::iterator> proxy_type;
      typedef ImageAccessor<value_type> accessor;

      // Constructor
      VecIterator(const Row iterator) : base(iterator) { }
      VecIterator() { }

      // Operators
      proxy_type operator*() const {
        return proxy_type(m_coliterator.m_iterator, m_coliterator.m_image->get_labels_pointer());
      }

      value_type get() const {
        if (m_coliterator.m_image->has_label(m_accessor(m_coliterator)))
          return m_accessor(m_coliterator);
        else
          return 0;
      }
      
      void set(const value_type& v) {
        if (m_coliterator.m_image->has_label(m_accessor(m_coliterator)))
          m_accessor.set(v, m_coliterator);
      }
    private:
      accessor m_accessor;
    };

    template<class Image, class Row, class Col>
    class ConstVecIterator : public VecIteratorBase<Image, Row, Col,
						    ConstVecIterator<Image, Row, Col> > {
    public:
      using VecIteratorBase<Image, Row, Col, ConstVecIterator<Image, Row, Col> >::m_coliterator;

      typedef ConstVecIterator self;
      typedef VecIteratorBase<Image, Row, Col, self> base;
      typedef typename Image::value_type value_type;
      typedef ImageAccessor<value_type> accessor;

      // Constructor
      ConstVecIterator(const Row iterator) : base(iterator) { }
      ConstVecIterator() { }

      // Operators
      typename Image::value_type operator*() const {
        return get();
      }

      value_type get() const {
        if (m_coliterator.m_image->has_label(m_accessor(m_coliterator)))
          return m_accessor(m_coliterator);
        else
          return 0;
      }
    private:
      accessor m_accessor;
    };
  } // namespace
} // namespace
#endif