$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
merge_double_link_closest_aligned.hh
1 // Copyright (C) 2011, 2012 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 
30 
31 #ifndef SCRIBO_PRIMITIVE_LINK_MERGE_DOUBLE_LINK_CLOSEST_ALIGNED_HH
32 # define SCRIBO_PRIMITIVE_LINK_MERGE_DOUBLE_LINK_CLOSEST_ALIGNED_HH
33 
34 
35 # include <mln/util/couple.hh>
36 # include <scribo/core/macros.hh>
37 # include <scribo/core/object_links.hh>
38 # include <scribo/core/tag/anchor.hh>
39 # include <scribo/filter/internal/alignment_angle.hh>
40 
41 namespace scribo
42 {
43 
44  namespace primitive
45  {
46 
47  namespace link
48  {
49 
59  template <typename L>
60  object_links<L>
61  merge_double_link_closest_aligned(const object_links<L>& left,
62  const object_links<L>& right,
63  anchor::Type anchor_angle);
64 
65 # ifndef MLN_INCLUDE_ONLY
66 
67  template <typename L>
68  object_links<L>
69  merge_double_link_closest_aligned(const object_links<L>& left,
70  const object_links<L>& right,
71  anchor::Type anchor_angle)
72  {
73  mln_trace("scribo::primitive::link::merge_double_link_closest_aligned");
74  mln_precondition(left.is_valid());
75  mln_precondition(right.is_valid());
76 
77 
78  const component_set<L>& components = left.components();
79 
81  align_data.resize(value::next(left.nelements()));
82  for (unsigned i = 0; i < align_data.nelements(); ++i)
83  {
84  align_data(i).first() = mln_max(unsigned);
85  align_data(i).second() = 360;
86  }
87 
88  object_links<L> output = right.duplicate();
89 
90  // Init alignment data.
91  for_all_links(l, output)
92  if (right(l) != l)
93  {
94  //FIXME: Handle the case when a component is included in another one ?
95 
96  // Update distance
97  align_data(l).first()
98  = std::abs(components(l).bbox().pcenter().col()
99  - components(right(l)).bbox().pcenter().col());
100 
101  // Update angle
102  align_data(l).second()
103  = filter::internal::alignment_angle(components,
104  l,
105  right(l),
106  anchor_angle);
107  }
108  else
109  {
110  // Update distance
111  align_data(l).first() = mln_max(unsigned);
112 
113  // Update angle
114  align_data(l).second() = 360.f;
115  }
116 
117 
118 
119  // Trying to improve results with left links.
120  for_all_links(l, left)
121  if (left(l) != l && left(l) != right(l))
122  {
123  unsigned dh = std::abs(components(l).bbox().pcenter().col()
124  - components(left(l)).bbox().pcenter().col());
125 
126  float alpha
127  = filter::internal::alignment_angle(components,
128  l,
129  left(l),
130  anchor_angle);
131 
132  if (right(left(l)) == left(l))
133  {
134  output.update(left(l), l);
135  }
136  else if ((dh < align_data(left(l)).first() * 0.66f)
137  || (alpha < align_data(left(l)).second()))
138  {
139  output.update(left(l), l);
140  align_data(left(l)).first() = dh;
141  align_data(left(l)).second() = alpha;
142  }
143  }
144 
145  return output;
146  }
147 
148 # endif // ! MLN_INCLUDE_ONLY
149 
150  } // end of namespace scribo::primitive::link
151 
152  } // end of namespace scribo::primitive
153 
154 } // end of namespace scribo
155 
156 #endif // ! SCRIBO_PRIMITIVE_LINK_MERGE_DOUBLE_LINK_CLOSEST_ALIGNED_HH