slices_2d.hh 5.17 KB
Newer Older
1
2
// Copyright (C) 2008, 2009, 2010 EPITA Research and Development
// Laboratory (LRDE)
3
//
4
// This file is part of Olena.
5
//
6
7
8
9
10
// 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,
11
12
13
14
15
// 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
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
22
23
24
25
// 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.
26
27
28
29

#ifndef MLN_DEBUG_SLICES_2D_HH
# define MLN_DEBUG_SLICES_2D_HH

30
/// \file
31
32
33
///
/// Create a 2D image of the slices of the 3D image \p input.

34
35
# include <cmath>

36
37
38
# include <mln/core/image/image2d.hh>

# include <mln/core/image/image3d.hh>
39
# include <mln/core/image/dmorph/slice_image.hh>
40

41
# include <mln/core/image/dmorph/p2p_image.hh>
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# include <mln/fun/p2p/translation.hh>

# include <mln/data/paste.hh>
# include <mln/data/fill.hh>



namespace mln
{

  namespace debug
  {

    /// Create a 2D image of the slices of the 3D image \p input.
    ///
    template <typename I>
    image2d<mln_value(I)>
    slices_2d(const Image<I>& input,
	      unsigned n_horizontal, unsigned n_vertical,
	      const mln_value(I)& bg);


64
65
66
67
68
69
70
71
72
    /// Create a 2D image of the slices of the 3D image \p input.
    ///
    template <typename I>
    image2d<mln_value(I)>
    slices_2d(const Image<I>& input,
	      float ratio_hv,           // horizontal / vertical
	      const mln_value(I)& bg);


73
74
75
76
77
78
79
80
81
82
83

# ifndef MLN_INCLUDE_ONLY

    template <typename I>
    inline
    image2d<mln_value(I)>
    slices_2d(const Image<I>& input_,
	      unsigned n_horizontal, unsigned n_vertical,
	      const mln_value(I)& bg)
    {
      trace::entering("debug::slices_2d");
84
      mlc_equal(mln_domain(I), box3d)::check();
85
86
87
88
89

      const I& input = exact(input_);

      mln_precondition(input.is_valid());
      mln_precondition(n_horizontal > 0 && n_vertical > 0);
90
      mln_precondition(input.nslis() <= n_horizontal * n_vertical);
91
92
93

      image2d<mln_value(I)> output(input.nrows() * n_vertical,
				   input.ncols() * n_horizontal);
94
      if (input.nslis() != n_horizontal * n_vertical)
95
96
	data::fill(output, bg);

97
98
99
100
      const point3d& p_min = input.domain().pmin();
      def::coord
	sli = p_min.sli(),
	last_sli = input.domain().pmax().sli();
101
102
103
      for (unsigned i = 0; i < n_vertical; ++i)
	for (unsigned j = 0; j < n_horizontal; ++j)
	  {
104
105
106
107
108
109
	    dpoint2d dp(i * input.nrows() - p_min.row(),
			j * input.ncols() - p_min.col());
	    data::paste(apply_p2p(slice(input, sli),
				  fun::p2p::translation(dp)),
			output);
	    if (++sli > last_sli)
110
	      {
111
		// Go out of loops.
112
113
114
115
116
117
118
119
120
121
		i = n_vertical;
		j = n_horizontal;
		break;
	      }
	  }

      trace::exiting("debug::slices_2d");
      return output;
    }

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

    namespace internal
    {

      unsigned round_up(float f)
      {
	unsigned n = static_cast<unsigned>(f + 0.499999f);
	if (n == 0u)
	  ++n;
	if (float(n) < f)
	  ++n;
	return n;
      }

      void slices2d_helper(float nslis, float nrows, float ncols,
			   float ratio_hv,
			   unsigned& n_horizontal,
			   unsigned& n_vertical)
      {
	if (ratio_hv > 1.f)
	  {
	    float n_v = std::sqrt(nslis * ncols / ratio_hv / nrows);
	    n_vertical = internal::round_up(n_v);
	    float n_h = nslis / float(n_vertical);
	    n_horizontal = internal::round_up(n_h);
	  }
	else
	  {
	    float n_h = std::sqrt(nrows * nslis * ratio_hv / ncols);
	    n_horizontal = internal::round_up(n_h);
	    float n_v = nslis / float(n_horizontal);
	    n_vertical = internal::round_up(n_v);
	  }
      }
156

157
158
159
160
161
162
163
164
165
166
    } // end of namespace mln::debug::internal


    template <typename I>
    image2d<mln_value(I)>
    slices_2d(const Image<I>& input_,
	      float ratio_hv,           // horizontal / vertical
	      const mln_value(I)& bg)
    {
      trace::entering("debug::slices_2d");
167
      mlc_equal(mln_domain(I), box3d)::check();
168
169
170
171
172
173

      const I& input = exact(input_);
      mln_precondition(input.is_valid());
      mln_precondition(ratio_hv > 0.f);

      unsigned n_horizontal, n_vertical;
174
      internal::slices2d_helper(input.nslis(), input.nrows(), input.ncols(),
175
176
				ratio_hv,
				n_horizontal, n_vertical);
177
      mln_assertion(n_horizontal * n_vertical >= input.nslis());
178
179
180
181
182
183
184
185

      image2d<mln_value(I)> output = slices_2d(input, n_horizontal, n_vertical, bg);

      trace::exiting("debug::slices_2d");
      return output;
    }


186
187
188
189
190
191
192
193
# endif // ! MLN_INCLUDE_ONLY

  } // end of namespace mln::debug

} // end of namespace mln


#endif // ! MLN_DEBUG_SLICES_2D_HH