slices_2d.hh 5.3 KB
Newer Older
1
2
// Copyright (C) 2008, 2009 EPITA Research and Development Laboratory
// (LRDE)
3
//
4
// This file is part of the Milena Library.  This library is free
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License version 2 as published by the
// Free Software Foundation.
//
// This library is distributed in the hope that it will be useful,
// 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
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 51 Franklin Street, Fifth Floor,
// Boston, MA 02111-1307, USA.
//
// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// 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.

#ifndef MLN_DEBUG_SLICES_2D_HH
# define MLN_DEBUG_SLICES_2D_HH

/// \file mln/debug/slices_2d.hh
///
/// Create a 2D image of the slices of the 3D image \p input.

36
37
# include <cmath>

38
39
40
# include <mln/core/image/image2d.hh>

# include <mln/core/image/image3d.hh>
41
# include <mln/core/image/dmorph/slice_image.hh>
42

43
# include <mln/core/image/dmorph/p2p_image.hh>
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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);


66
67
68
69
70
71
72
73
74
    /// 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);


75
76
77
78
79
80
81
82
83
84
85

# 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");
86
      mlc_equal(mln_domain(I), box3d)::check();
87
88
89
90
91
92
93
94
95
96
97
98

      const I& input = exact(input_);

      mln_precondition(input.is_valid());
      mln_precondition(n_horizontal > 0 && n_vertical > 0);
      mln_precondition(input.nslices() <= n_horizontal * n_vertical);

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

99
100
101
102
      const point3d& p_min = input.domain().pmin();
      def::coord
	sli = p_min.sli(),
	last_sli = input.domain().pmax().sli();
103
104
105
      for (unsigned i = 0; i < n_vertical; ++i)
	for (unsigned j = 0; j < n_horizontal; ++j)
	  {
106
107
108
109
110
111
	    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)
112
	      {
113
		// Go out of loops.
114
115
116
117
118
119
120
121
122
123
		i = n_vertical;
		j = n_horizontal;
		break;
	      }
	  }

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

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
156
157
158
159
160
161
162
163
164
165
166
167
168

    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);
	  }
      }
      
    } // 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");
169
      mlc_equal(mln_domain(I), box3d)::check();
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187

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

      unsigned n_horizontal, n_vertical;
      internal::slices2d_helper(input.nslices(), input.nrows(), input.ncols(),
				ratio_hv,
				n_horizontal, n_vertical);
      mln_assertion(n_horizontal * n_vertical >= input.nslices());

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

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


188
189
190
191
192
193
194
195
# endif // ! MLN_INCLUDE_ONLY

  } // end of namespace mln::debug

} // end of namespace mln


#endif // ! MLN_DEBUG_SLICES_2D_HH