$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
from_double_link_any.hh
1 // Copyright (C) 2010, 2011 EPITA Research and Development Laboratory
2 // (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef SCRIBO_PRIMITIVE_GROUP_FROM_DOUBLE_LINK_ANY_HH
28 # define SCRIBO_PRIMITIVE_GROUP_FROM_DOUBLE_LINK_ANY_HH
29 
30 
35 
36 
37 # include <mln/core/concept/image.hh>
38 # include <mln/core/site_set/box.hh>
39 
40 # include <mln/accu/shape/bbox.hh>
41 
42 # include <mln/util/array.hh>
43 
44 # include <scribo/core/macros.hh>
45 
46 # include <scribo/core/object_links.hh>
47 # include <scribo/core/object_groups.hh>
48 # include <scribo/core/component_set.hh>
49 # include <scribo/primitive/internal/find_root.hh>
50 
51 
52 namespace scribo
53 {
54 
55  namespace primitive
56  {
57 
58  namespace group
59  {
60 
69  template <typename L>
70  object_groups<L>
71  from_double_link_any(const object_links<L>& left_link,
72  const object_links<L>& right_link);
73 
74 
75 
76 # ifndef MLN_INCLUDE_ONLY
77 
78  namespace internal
79  {
80 
81  template <typename L>
82  mln::util::array<unsigned> make_parent(const object_links<L>& link)
83  {
84  mln::util::array<unsigned> parent = link.comp_to_link();
85  for_all_links(l, link)
86  parent(l) = scribo::internal::find_root(parent, l);
87 
88  return parent;
89  }
90 
91  } // end of namespace scribo::primitive::group::internal
92 
93 
94  template <typename L>
95  inline
96  object_groups<L>
97  from_double_link_any(const object_links<L>& left_link,
98  const object_links<L>& right_link)
99  {
100  mln_trace("scribo::primitive::group::from_double_link_any");
101 
102  object_links<L> merged_link(left_link.components());
103  merged_link.init();
104 
106  lparent = internal::make_parent(left_link),
107  rparent = internal::make_parent(right_link);
108 
109  for_all_links(i, merged_link)
110  {
111  // Looking for new left link
112  unsigned
113  pi = scribo::internal::find_root(lparent, i),
114  pli = scribo::internal::find_root(lparent, left_link(i));
115 
116  if (pi != pli)
117  {
118  merged_link.update(i, left_link(i));
119  if (pi < pli)
120  lparent(pli) = pi;
121  else
122  lparent(pi) = pli;
123  }
124 
125  // Looking for new right link
126  pi = scribo::internal::find_root(lparent, i);
127  unsigned pri = scribo::internal::find_root(rparent, right_link(i));
128 
129  if (pi != pri)
130  {
131  merged_link.update(i, right_link(i));
132  if (pi < pli)
133  lparent(pli) = pi;
134  else
135  lparent(pi) = pli;
136  }
137  }
138 
139  object_groups<L> output(merged_link);
140 
141  return output;
142  }
143 
144 
145 # endif // ! MLN_INCLUDE_ONLY
146 
147 
148  } // end of namespace scribo::primitive::group
149 
150  } // end of namespace scribo::primitive
151 
152 } // end of namespace scribo
153 
154 #endif // ! SCRIBO_PRIMITIVE_GROUP_FROM_DOUBLE_LINK_ANY_HH