$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
alignment_angle.hh
1 // Copyright (C) 2010, 2011, 2013 EPITA Research and Development
2 // Laboratory (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_FILTER_INTERNAL_ALIGNMENT_ANGLE_HH
28 # define SCRIBO_FILTER_INTERNAL_ALIGNMENT_ANGLE_HH
29 
34 
35 
36 # include <mln/util/array.hh>
37 # include <mln/math/abs.hh>
38 # include <mln/math/pi.hh>
39 
40 # include <scribo/core/macros.hh>
41 # include <scribo/core/tag/anchor.hh>
42 # include <scribo/core/component_set.hh>
43 
44 namespace scribo
45 {
46 
47  namespace filter
48  {
49 
50  namespace internal
51  {
52 
53  using namespace mln;
54 
55 
60  template <typename L>
61  float
62  alignment_angle(const component_set<L>& comps,
63  unsigned current_object, unsigned nbh_object,
64  anchor::Type anchor);
65 
66 
67 # ifndef MLN_INCLUDE_ONLY
68 
69 
70  template <typename L>
71  float
72  alignment_angle(const component_set<L>& comps,
73  unsigned current_object, unsigned nbh_object,
74  anchor::Type anchor)
75  {
76  mln_trace("scribo::filter::internal::alignment_angle");
77 
78  mln_precondition(comps.is_valid());
79 
80  float dr, dc, result = 0;
81 
82  if (nbh_object == current_object)
83  return 0;
84 
85  switch(anchor)
86  {
87  // Center
88  case anchor::Center:
89  {
90  dr = math::abs(comps(current_object).bbox().pcenter().row()
91  - comps(nbh_object).bbox().pcenter().row());
92  dc = math::abs(comps(current_object).bbox().pcenter().col()
93  - comps(nbh_object).bbox().pcenter().col());
94 
95  result = std::atan(dr / dc);
96  }
97  break;
98 
99  // Mass Center
100  case anchor::MassCenter:
101  {
102  dr = math::abs(comps(current_object).mass_center().row()
103  - comps(nbh_object).mass_center().row());
104  dc = math::abs(comps(current_object).mass_center().col()
105  - comps(nbh_object).mass_center().col());
106 
107  result = std::atan(dr / dc);
108  }
109  break;
110 
111  // Top
112  case anchor::StrictTopCenter:
113  case anchor::TopStrictLeft:
114  case anchor::Top:
115  {
116  dr = math::abs(comps(current_object).bbox().pmin().row()
117  - comps(nbh_object).bbox().pmin().row());
118  dc = math::abs(comps(current_object).bbox().pcenter().col()
119  - comps(nbh_object).bbox().pcenter().col());
120 
121  result = std::atan(dr / dc);
122  }
123  break;
124 
125  // Bottom
126  case anchor::StrictBottomCenter:
127  case anchor::BottomStrictRight:
128  case anchor::Bottom:
129  {
130  dr = math::abs(comps(current_object).bbox().pmax().row()
131  - comps(nbh_object).bbox().pmax().row());
132  dc = math::abs(comps(current_object).bbox().pcenter().col()
133  - comps(nbh_object).bbox().pcenter().col());
134 
135  result = std::atan(dr / dc);
136  }
137  break;
138 
139  // Left
140  case anchor::Left:
141  {
142  dr = math::abs(comps(current_object).bbox().pcenter().row()
143  - comps(nbh_object).bbox().pcenter().row());
144  dc = math::abs(comps(current_object).bbox().pmin().col()
145  - comps(nbh_object).bbox().pmin().col());
146 
147  result = std::atan(dc / dr);
148  }
149  break;
150 
151  // Right
152  case anchor::Right:
153  {
154  dr = math::abs(comps(current_object).bbox().pcenter().row()
155  - comps(nbh_object).bbox().pcenter().row());
156  dc = math::abs(comps(current_object).bbox().pmax().col()
157  - comps(nbh_object).bbox().pmax().col());
158 
159  result = std::atan(dc / dr);
160  }
161  break;
162 
163  default:
164  mln_trace_warning("scribo::filter::internal::alignment_angle,"
165  " Invalid anchor value... Aborting computation.");
166  }
167 
168  return result;
169  }
170 
171 # endif // ! MLN_INCLUDE_ONLY
172 
173  } // end of namespace scribo::filter::internal
174 
175  } // end of namespace scribo::filter
176 
177 } // end of namespace scribo
178 
179 #endif // SCRIBO_FILTER_INTERNAL_ALIGNMENT_ANGLE_HH