edge_image.hh 8.09 KB
Newer Older
Guillaume Lazzara's avatar
Guillaume Lazzara committed
1
2
// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE)
//
3
// This file is part of Olena.
Guillaume Lazzara's avatar
Guillaume Lazzara committed
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,
Guillaume Lazzara's avatar
Guillaume Lazzara committed
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/>.
Guillaume Lazzara's avatar
Guillaume Lazzara committed
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
Guillaume Lazzara's avatar
Guillaume Lazzara committed
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.
Guillaume Lazzara's avatar
Guillaume Lazzara committed
25
26
27
28

#ifndef MLN_MAKE_EDGE_IMAGE_HH
# define MLN_MAKE_EDGE_IMAGE_HH

29
/// \file
Guillaume Lazzara's avatar
Guillaume Lazzara committed
30
31
32
33
34
35
36
///
/// Routine to create a edge image.

# include <mln/core/image/edge_image.hh>
# include <mln/core/image/vertex_image.hh>
# include <mln/core/concept/function.hh>
# include <mln/util/internal/id2element.hh>
Guillaume Lazzara's avatar
Guillaume Lazzara committed
37
# include <mln/data/fill.hh>
Guillaume Lazzara's avatar
Guillaume Lazzara committed
38
39
40
41
42
43
44
45
46
47
48


namespace mln
{

  namespace make
  {


    /// Construct an edge image.
    ///
Roland Levillain's avatar
Roland Levillain committed
49
    /// \param[in] g  A graph.
Guillaume Lazzara's avatar
Guillaume Lazzara committed
50
51
52
53
54
55
56
57
    /// \param[in] fv A function mapping edge ids to values.
    ///
    /// \return an edge image.
    //
    template <typename V, typename G>
    mln::edge_image<void,V,G>
    edge_image(const Graph<G>& g, const fun::i2v::array<V>& fv);

Edwin Carlinet's avatar
Edwin Carlinet committed
58
59
    /// Construct an edge image.
    ///
Roland Levillain's avatar
Roland Levillain committed
60
    /// \param[in] g  A graph.
Edwin Carlinet's avatar
Edwin Carlinet committed
61
62
63
64
65
66
67
    /// \param[in] fv A function mapping edge ids to values.
    ///
    /// \return an edge image.
    //
    template <typename FV, typename G>
    mln::edge_image<void, mln_result(FV), G>
    edge_image(const Graph<G>& g, const Function_v2v<FV>& fv);
Guillaume Lazzara's avatar
Guillaume Lazzara committed
68

Guillaume Lazzara's avatar
Guillaume Lazzara committed
69

Guillaume Lazzara's avatar
Guillaume Lazzara committed
70
71
    /// Construct an edge image.
    ///
Roland Levillain's avatar
Roland Levillain committed
72
    /// \param[in] g_ A graph.
Guillaume Lazzara's avatar
Guillaume Lazzara committed
73
74
75
76
77
78
79
80
    /// \param[in] fp A function mapping edge ids to sites.
    /// \param[in] fv A function mapping edge ids to values.
    ///
    /// \return an edge image.
    //
    template <typename FP, typename FV, typename G>
    mln::edge_image<mln_result(FP),mln_result(FV),G>
    edge_image(const Graph<G>& g_,
81
82
	       const Function_v2v<FP>& fp,
	       const Function_v2v<FV>& fv);
Guillaume Lazzara's avatar
Guillaume Lazzara committed
83
84


Guillaume Lazzara's avatar
Guillaume Lazzara committed
85

Guillaume Lazzara's avatar
Guillaume Lazzara committed
86
87
88
    /// Construct an edge image.
    ///
    /// \param[in] v_ima_ A vertex image.
Roland Levillain's avatar
Roland Levillain committed
89
    /// \param[in] pe	  A p_edges mapping graph elements to sites.
Guillaume Lazzara's avatar
Guillaume Lazzara committed
90
    /// \param[in] fv_	  A function mapping two vertex ids to a value.
Guillaume Lazzara's avatar
Guillaume Lazzara committed
91
92
93
94
    ///			  The result is associated to the corresponding edge.
    ///
    /// \return an edge image.
    //
Guillaume Lazzara's avatar
Guillaume Lazzara committed
95
    template <typename P, typename V, typename G, typename FP, typename FV>
Guillaume Lazzara's avatar
Guillaume Lazzara committed
96
    mln::edge_image<mln_result(FP),mln_result(FV),G>
Guillaume Lazzara's avatar
Guillaume Lazzara committed
97
    edge_image(const mln::vertex_image<P,V,G>& v_ima_,
Guillaume Lazzara's avatar
Guillaume Lazzara committed
98
	       const p_edges<G,FP> pe,
Guillaume Lazzara's avatar
Guillaume Lazzara committed
99
100
101
102
	       const Function_vv2v<FV>& fv_);



Guillaume Lazzara's avatar
Guillaume Lazzara committed
103
104
105
    /// Construct an edge image.
    ///
    /// \param[in] v_ima_ A vertex image.
Roland Levillain's avatar
Roland Levillain committed
106
    /// \param[in] fv_	  A function mapping two vertices' values to a value.
Guillaume Lazzara's avatar
Guillaume Lazzara committed
107
108
    ///			  The result is associated to the corresponding edge.
    ///
Guillaume Lazzara's avatar
Guillaume Lazzara committed
109
110
    /// \return an edge image without localization information mapped to
    /// graph elements.
Guillaume Lazzara's avatar
Guillaume Lazzara committed
111
112
113
    //
    template <typename P, typename V, typename G, typename FV>
    mln::edge_image<void,mln_result(FV),G>
Guillaume Lazzara's avatar
Guillaume Lazzara committed
114
    edge_image(const mln::vertex_image<P,V,G>& v_ima_,
Guillaume Lazzara's avatar
Guillaume Lazzara committed
115
116
117
	       const Function_vv2v<FV>& fv_);


118
119
120
    /// Construct an edge image.
    ///
    /// \param[in] v_ima_ A vertex image.
Roland Levillain's avatar
Roland Levillain committed
121
122
123
    /// \param[in] fv_	  A predicate on a vertex's value.
    ///			  The (Boolean) result is associated to the
    ///			  edges adjacent to the vertex.
124
125
126
127
128
129
130
131
132
133
    ///
    /// \return an edge image without localization information mapped to
    /// graph elements.
    //
    template <typename P, typename V, typename G, typename F>
    mln::edge_image<void,bool,G>
    edge_image(const mln::vertex_image<P,V,G>& v_ima_,
	       const Function_v2b<F>& fv_);


Guillaume Lazzara's avatar
Guillaume Lazzara committed
134
135
136
# ifndef MLN_INCLUDE_ONLY


Guillaume Lazzara's avatar
Guillaume Lazzara committed
137

Guillaume Lazzara's avatar
Guillaume Lazzara committed
138
    template <typename V, typename G>
139
    inline
Guillaume Lazzara's avatar
Guillaume Lazzara committed
140
141
142
143
144
145
    mln::edge_image<void,V,G>
    edge_image(const Graph<G>& g, const fun::i2v::array<V>& fv)
    {
      trace::entering("make::edge_image");
      mln_precondition(exact(g).is_valid());

Roland Levillain's avatar
Roland Levillain committed
146
147
      p_edges<G> pe(g);
      mln::edge_image<void,V,G> ima(pe, fv);
Guillaume Lazzara's avatar
Guillaume Lazzara committed
148
149
150
151
152

      trace::exiting("make::edge_image");
      return ima;
    }

Edwin Carlinet's avatar
Edwin Carlinet committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
    template <typename FV, typename G>
    mln::edge_image<void,mln_result(FV),G>
    edge_image(const Graph<G>& g_,
	       const Function_v2v<FV>& fv)
    {
      trace::entering("make::edge_image");
      const G& g = exact(g_);
      const FV& f = exact(fv);
      mln_precondition(g.is_valid());

      p_edges<G> pe(g);
      typedef mln::edge_image<void,mln_result(FV),G> ima_t;
      ima_t ima(pe);

      mln_piter(ima_t) e(ima.domain());
      for_all(e)
	ima(e) = f(e.id());

      trace::exiting("make::edge_image");
      return ima;
    }
Guillaume Lazzara's avatar
Guillaume Lazzara committed
174

Guillaume Lazzara's avatar
Guillaume Lazzara committed
175

Guillaume Lazzara's avatar
Guillaume Lazzara committed
176
    template <typename FP, typename FV, typename G>
177
    inline
Guillaume Lazzara's avatar
Guillaume Lazzara committed
178
179
    mln::edge_image<mln_result(FP),mln_result(FV),G>
    edge_image(const Graph<G>& g_,
180
181
	       const Function_v2v<FP>& fp,
	       const Function_v2v<FV>& fv)
Guillaume Lazzara's avatar
Guillaume Lazzara committed
182
183
184
185
186
    {
      trace::entering("make::edge_image");
      const G& g = exact(g_);
      mln_precondition(g.is_valid());

Roland Levillain's avatar
Roland Levillain committed
187
188
      p_edges<G,FP> pe(g,fp);
      mln::edge_image<mln_result(FP),mln_result(FV),G> ima(pe, fv);
Guillaume Lazzara's avatar
Guillaume Lazzara committed
189
190
191
192
193
194

      trace::exiting("make::edge_image");
      return ima;
    }


Guillaume Lazzara's avatar
Guillaume Lazzara committed
195

Guillaume Lazzara's avatar
Guillaume Lazzara committed
196
    template <typename P, typename V, typename G, typename FP, typename FV>
197
    inline
Guillaume Lazzara's avatar
Guillaume Lazzara committed
198
    mln::edge_image<mln_result(FP),mln_result(FV),G>
Guillaume Lazzara's avatar
Guillaume Lazzara committed
199
    edge_image(const mln::vertex_image<P,V,G>& v_ima_,
Guillaume Lazzara's avatar
Guillaume Lazzara committed
200
	       const p_edges<G,FP> pe,
Guillaume Lazzara's avatar
Guillaume Lazzara committed
201
202
203
204
205
	       const Function_vv2v<FV>& fv_)
    {
      trace::entering("make::edge_image");

      const FV& fv = exact(fv_);
Guillaume Lazzara's avatar
Guillaume Lazzara committed
206
      const mln::vertex_image<P,V,G>& v_ima = exact(v_ima_);
Guillaume Lazzara's avatar
Guillaume Lazzara committed
207
208
209
      mln_precondition(v_ima.is_valid());

      typedef mln::edge_image<mln_result(FP),mln_result(FV),G> edge_ima_t;
Guillaume Lazzara's avatar
Guillaume Lazzara committed
210
211
212
213
214
215
216
217
218
219
220
221
222
      edge_ima_t ima_e(pe);

      mln_piter(edge_ima_t) e(ima_e.domain());
      for_all(e)
	ima_e(e) = fv(e.element().v1(), e.element().v2());

      trace::exiting("make::edge_image");
      return ima_e;
    }



    template <typename P, typename V, typename G, typename FV>
223
    inline
Guillaume Lazzara's avatar
Guillaume Lazzara committed
224
    mln::edge_image<void,mln_result(FV),G>
Guillaume Lazzara's avatar
Guillaume Lazzara committed
225
    edge_image(const mln::vertex_image<P,V,G>& v_ima_,
Guillaume Lazzara's avatar
Guillaume Lazzara committed
226
227
228
229
230
	       const Function_vv2v<FV>& fv_)
    {
      trace::entering("make::edge_image");

      const FV& fv = exact(fv_);
Guillaume Lazzara's avatar
Guillaume Lazzara committed
231
      const mln::vertex_image<P,V,G>& v_ima = exact(v_ima_);
Guillaume Lazzara's avatar
Guillaume Lazzara committed
232
233
      mln_precondition(v_ima.is_valid());

Guillaume Lazzara's avatar
Guillaume Lazzara committed
234
      p_edges<G> pe(v_ima.domain().graph());
Guillaume Lazzara's avatar
Guillaume Lazzara committed
235
236
      typedef mln::edge_image<void,mln_result(FV),G> edge_ima_t;
      edge_ima_t ima_e(pe);
Guillaume Lazzara's avatar
Guillaume Lazzara committed
237
238
239

      mln_piter(edge_ima_t) e(ima_e.domain());
      for_all(e)
Guillaume Lazzara's avatar
Guillaume Lazzara committed
240
	ima_e(e) = fv(v_ima(e.element().v1()), v_ima(e.element().v2()));
Guillaume Lazzara's avatar
Guillaume Lazzara committed
241
242
243
244
245
246

      trace::exiting("make::edge_image");
      return ima_e;
    }


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
    template <typename P, typename V, typename G, typename F>
    inline
    mln::edge_image<void,bool,G>
    edge_image(const mln::vertex_image<P,V,G>& v_ima_,
	       const Function_v2b<F>& fv_)
    {
      trace::entering("make::edge_image");

      const F& fv = exact(fv_);
      typedef mln::vertex_image<P,V,G> v_ima_t;
      const v_ima_t& v_ima = exact(v_ima_);
      mln_precondition(v_ima.is_valid());

      p_edges<G> pe(v_ima.domain().graph());
      typedef mln::edge_image<void,bool,G> edge_ima_t;
      edge_ima_t ima_e(pe);
      data::fill(ima_e, true);

      mln_piter(v_ima_t) p(v_ima.domain());
      for_all(p)
	if (!fv(v_ima(p)))
	{
	  typename v_ima_t::edge_win_t win;
	  mln_qiter(v_ima_t::edge_win_t) q(win, p);
	  for_all(q)
Roland Levillain's avatar
Roland Levillain committed
272
	    ima_e(q) = false;
273
274
275
276
277
278
279
	}

      trace::exiting("make::edge_image");
      return ima_e;
    }


Guillaume Lazzara's avatar
Guillaume Lazzara committed
280

Guillaume Lazzara's avatar
Guillaume Lazzara committed
281
282
283
284
285
286
287
# endif // ! MLN_INCLUDE_ONLY


  } // end of namespace mln::make

} // end of namespace mln

Guillaume Lazzara's avatar
Guillaume Lazzara committed
288
#endif // ! MLN_MAKE_EDGE_IMAGE_HH