count_adjacent_vertices.hh 4.8 KB
Newer Older
1
// Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2
//
3
// This file is part of Olena.
4
//
5
6
7
8
9
// Olena 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, version 2 of the License.
//
// Olena is distributed in the hope that it will be useful,
10
11
12
13
14
// 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
15
// along with Olena.  If not, see <http://www.gnu.org/licenses/>.
16
17
//
// As a special exception, you may use this file as part of a free
18
// software project without restriction.  Specifically, if other files
19
// instantiate templates or use macros or inline functions from this
20
21
22
23
24
// file, or you compile this file and link it with other files to produce
// an executable, this file does not by itself cause the resulting
// executable to be covered by the GNU General Public License.  This
// exception does not however invalidate any other reasons why the
// executable file might be covered by the GNU General Public License.
25
26
27
28

#ifndef MLN_ACCU_COUNT_ADJACENT_VERTICES_HH
# define MLN_ACCU_COUNT_ADJACENT_VERTICES_HH

29
/// \file
Guillaume Lazzara's avatar
Guillaume Lazzara committed
30
///
Guillaume Lazzara's avatar
Guillaume Lazzara committed
31
/// Define an accumulator that counts the vertices adjacent to a
Guillaume Lazzara's avatar
Guillaume Lazzara committed
32
/// set of p_edges psites.
33
34
35

# include <mln/accu/internal/base.hh>
# include <mln/core/concept/meta_accumulator.hh>
Guillaume Lazzara's avatar
Guillaume Lazzara committed
36
# include <mln/pw/image.hh>
37
38
39
40
41
42
43
44
# include <mln/util/pix.hh>

namespace mln
{

  namespace accu
  {

Guillaume Lazzara's avatar
Guillaume Lazzara committed
45
    /// \brief Accumulator class counting the number of vertices
Guillaume Lazzara's avatar
Guillaume Lazzara committed
46
    /// adjacent to a set of mln::p_edges_psite (i.e., a set of
47
48
    /// edges).
    ///
Guillaume Lazzara's avatar
Guillaume Lazzara committed
49
50
    /// The type to be count is mln::util::pix< pw::image<F, S> >
    /// where \p F and \p S are the parameters of this class.
51
52
53
    ///
    /// This accumulator is used by mln::closing_area_on_vertices and
    /// mln::opening_area_on_vertices.
Guillaume Lazzara's avatar
Guillaume Lazzara committed
54
55
56
    ///
    /// \ingroup modaccuimages
    //
Guillaume Lazzara's avatar
Guillaume Lazzara committed
57
    template <typename F, typename S>
58
    struct count_adjacent_vertices
Ugo Jardonnet's avatar
Ugo Jardonnet committed
59
      : public mln::accu::internal::base< unsigned,
Guillaume Lazzara's avatar
Guillaume Lazzara committed
60
					  count_adjacent_vertices<F,S> >
61
    {
Guillaume Lazzara's avatar
Guillaume Lazzara committed
62
      typedef mln::util::pix< pw::image<F,S> > argument;
63

64
      count_adjacent_vertices();
65
66
67
68
69

      /// Manipulators.
      /// \{
      void init();
      void take(const argument& arg);
Guillaume Lazzara's avatar
Guillaume Lazzara committed
70
      void take(const count_adjacent_vertices<F,S>& other);
71

72
      /// Force the value of the counter to \a c.
Ugo Jardonnet's avatar
Ugo Jardonnet committed
73
      void set_value(unsigned c);
74
75
76
      /// \}

      /// Get the value of the accumulator.
Ugo Jardonnet's avatar
Ugo Jardonnet committed
77
      unsigned to_result() const;
78

Guillaume Lazzara's avatar
Guillaume Lazzara committed
79
80
81
      /// Return whether this accu can return a result.
      bool is_valid() const;

82
83
84
85
86
87
    protected:
      /// Update the value of the counter.
      void update_ ();

    protected:
      /// The value of the counter.
Ugo Jardonnet's avatar
Ugo Jardonnet committed
88
      unsigned count__;
89
      /// The set of adjacent vertices.
Guillaume Lazzara's avatar
Guillaume Lazzara committed
90
      std::set<unsigned> vertices_;
91
92
93
    };


94
    namespace meta
95
    {
96

Guillaume Lazzara's avatar
Guillaume Lazzara committed
97
      /// Meta accumulator for count_adjacent_vertices.
98
99
      struct count_adjacent_vertices
	: public Meta_Accumulator< count_adjacent_vertices >
100
      {
Guillaume Lazzara's avatar
Guillaume Lazzara committed
101
	template <typename F, typename S>
102
103
	  struct with
	  {
Guillaume Lazzara's avatar
Guillaume Lazzara committed
104
	    typedef accu::count_adjacent_vertices<F,S> ret;
105
	  };
106
      };
107
108

    } // end of namespace mln::accu::meta
109
110
111
112


# ifndef MLN_INCLUDE_ONLY

Guillaume Lazzara's avatar
Guillaume Lazzara committed
113
    template <typename F, typename S>
114
    inline
Guillaume Lazzara's avatar
Guillaume Lazzara committed
115
    count_adjacent_vertices<F,S>::count_adjacent_vertices()
116
117
118
119
    {
      init();
    }

Guillaume Lazzara's avatar
Guillaume Lazzara committed
120
    template <typename F, typename S>
121
122
    inline
    void
Guillaume Lazzara's avatar
Guillaume Lazzara committed
123
    count_adjacent_vertices<F,S>::init()
124
125
126
127
128
    {
      vertices_.clear();
      update_();
    }

Guillaume Lazzara's avatar
Guillaume Lazzara committed
129
    template <typename F, typename S>
130
131
    inline
    void
Guillaume Lazzara's avatar
Guillaume Lazzara committed
132
    count_adjacent_vertices<F,S>::take(const argument& arg)
133
    {
Guillaume Lazzara's avatar
Guillaume Lazzara committed
134
135
      vertices_.insert(arg.p().v1());
      vertices_.insert(arg.p().v2());
136
137
138
      update_();
    }

Guillaume Lazzara's avatar
Guillaume Lazzara committed
139
    template <typename F, typename S>
140
141
    inline
    void
Guillaume Lazzara's avatar
Guillaume Lazzara committed
142
    count_adjacent_vertices<F,S>::take(const count_adjacent_vertices<F,S>& other)
143
144
145
146
147
    {
      vertices_.insert (other.vertices_.begin(), other.vertices_.end());
      update_();
    }

Guillaume Lazzara's avatar
Guillaume Lazzara committed
148
    template <typename F, typename S>
149
    inline
Ugo Jardonnet's avatar
Ugo Jardonnet committed
150
    unsigned
Guillaume Lazzara's avatar
Guillaume Lazzara committed
151
    count_adjacent_vertices<F,S>::to_result() const
152
153
154
155
    {
      return count__;
    }

Guillaume Lazzara's avatar
Guillaume Lazzara committed
156
    template <typename F, typename S>
157
158
    inline
    void
Guillaume Lazzara's avatar
Guillaume Lazzara committed
159
    count_adjacent_vertices<F,S>::set_value(unsigned c)
160
161
    {
      count__ = c;
162
163
      /// Reset the other member.
      vertices_.clear();
164
165
    }

Guillaume Lazzara's avatar
Guillaume Lazzara committed
166
    template <typename F, typename S>
167
168
    inline
    void
Guillaume Lazzara's avatar
Guillaume Lazzara committed
169
    count_adjacent_vertices<F,S>::update_()
170
171
172
173
    {
      count__ = vertices_.size();
    }

Guillaume Lazzara's avatar
Guillaume Lazzara committed
174
175
176
177
178
179
180
181
    template <typename F, typename S>
    inline
    bool
    count_adjacent_vertices<F,S>::is_valid() const
    {
      return true;
    }

182
183
184
185
186
187
188
189
# endif // ! MLN_INCLUDE_ONLY

  } // end of namespace mln::accu

} // end of namespace mln


#endif // ! MLN_ACCU_COUNT_ADJACENT_VERTICES_HH