iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LayoutImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author Laurent Itti
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 #ifndef INCLUDE_NRT_CORE_IMAGE_DETAILS_LAYOUTIMPL_H
37 #define INCLUDE_NRT_CORE_IMAGE_DETAILS_LAYOUTIMPL_H
38 
40 
41 // ######################################################################
42 // Layout implementation
43 // ######################################################################
44 template <class T> inline
46  itsLeafImg(), itsDir(d), itsDims()
47 { }
48 
49 // ######################################################################
50 template <class T> inline
52  itsLeafImg(p), itsDir(d), itsDims(p.dims())
53 { }
54 
55 // ######################################################################
56 template <class T> inline
58  itsDir(d),
59  itsDims(d == nrt::LayoutDir::H
60  ? nrt::Dims<nrt::int32>(p1.width() + p2.width(), std::max(p1.height(), p2.height()))
61  : nrt::Dims<nrt::int32>(std::max(p1.width(), p2.width()), p1.height() + p2.height()))
62 { itsParts.push_back(p1); itsParts.push_back(p2); }
63 
64 // ######################################################################
65 template <class T> inline
67  itsDir(d), itsDims()
68 {
69  nrt::int32 w = 0, h = 0; nrt::int32 const nimgs = imgs.size();
70 
71  for (nrt::int32 i = 0; i < nimgs; ++i) {
72 
73  nrt::Image<T> im = imgs.at(i);
74  itsParts.push_back(im);
75 
76  if (d == nrt::LayoutDir::H) { w += im.width(); h = std::max(h, im.height()); }
77  else { w = std::max(w, im.width()); h += im.height(); }
78  }
79 
80  itsDims = nrt::Dims<nrt::int32>(w, h);
81 }
82 
83 // ######################################################################
84 template <class T> inline
85 nrt::Layout<T>::Layout(std::vector<nrt::Layout<T> > const & layouts, nrt::LayoutDir const d)
86  : itsDir(d), itsDims()
87 {
88  nrt::int32 w = 0, h = 0;
89 
90  for (nrt::uint32 i = 0; i < layouts.size(); ++i) {
91  nrt::Layout<T> const & la = layouts[i];
92  itsParts.push_back(la);
93  if (d == nrt::LayoutDir::H) { w += la.width(); h = std::max(h, la.height()); }
94  else { w = std::max(w, la.width()); h += la.height(); }
95  }
96 
97  itsDims = nrt::Dims<nrt::int32>(w, h);
98 }
99 
100 // ######################################################################
101 template <class T> inline
103 { return itsDims.empty(); }
104 
105 // ######################################################################
106 template <class T> inline
108 { return itsDims; }
109 
110 // ######################################################################
111 template <class T> inline
112 nrt::int32 nrt::Layout<T>::width() const
113 { return itsDims.width(); }
114 
115 // ######################################################################
116 template <class T> inline
117 nrt::int32 nrt::Layout<T>::height() const
118 { return itsDims.height(); }
119 
120 // ######################################################################
121 template <class T> inline
123 { return itsDir; }
124 
125 // ######################################################################
126 template <class T> inline
127 nrt::int32 nrt::Layout<T>::size() const
128 { return itsParts.size(); }
129 
130 // ######################################################################
131 template <class T> inline
132 nrt::Layout<T> const & nrt::Layout<T>::part(nrt::int32 const i) const
133 { return itsParts[i]; }
134 
135 // ######################################################################
136 template <class T> inline
138 { return itsLeafImg; }
139 
140 // ######################################################################
141 template <class T> inline
143 {
144  if (itsParts.size() == 0) return itsLeafImg;
145 
146  if (itsRenderCache.size() == 0) {
147  itsRenderCache = nrt::Image<T>(this->dims(), nrt::ImageInitPolicy::Zeros);
148  this->renderInto(itsRenderCache, nrt::Point2D<int>(0, 0));
149  }
150 
151  return itsRenderCache;
152 }
153 
154 // ######################################################################
155 template <class T> inline
156 nrt::Image<T> nrt::Layout<T>::render(T const & bgcol) const
157 {
158  if (itsParts.size() == 0) return itsLeafImg;
159 
160  if (itsRenderCache.size() == 0) {
161  itsRenderCache = nrt::Image<T>(this->dims(), bgcol);
162  this->renderInto(itsRenderCache, nrt::Point2D<int>(0, 0));
163  }
164 
165  return itsRenderCache;
166 }
167 
168 // ######################################################################
169 template <class T> inline
171 {
172  if (itsParts.size() == 0) {
173  if (itsLeafImg.size()) nrt::paste(x, itsLeafImg, p);
174  } else if (itsDir == nrt::LayoutDir::H) {
175  nrt::Point2D<int> pp = p;
176  for (size_t i = 0; i < itsParts.size(); ++i) {
177  itsParts[i].renderInto(x, pp);
178  pp = nrt::Point2D<int>(pp.x() + itsParts[i].width(), pp.y());
179  }
180  } else {
181  nrt::Point2D<int> pp = p;
182  for (size_t i = 0; i < itsParts.size(); ++i) {
183  itsParts[i].renderInto(x, pp);
184  pp = nrt::Point2D<int>(pp.x(), pp.y() + itsParts[i].height());
185  }
186  }
187 }
188 
189 // ######################################################################
190 // ######################################################################
191 // ######################################################################
192 
193 template <class T> inline
195 { return nrt::Layout<T>(p1, p2, nrt::LayoutDir::H); }
196 
197 template <class T> inline
199 { return nrt::Layout<T>(p1, p2, nrt::LayoutDir::H); }
200 
201 template <class T> inline
203 { return nrt::Layout<T>(p1, p2, nrt::LayoutDir::H); }
204 
205 template <class T> inline
207 { return nrt::Layout<T>(p1, p2, nrt::LayoutDir::H); }
208 
209 template <class T> inline
210 nrt::Layout<T> nrt::hcat(nrt::Layout<T> const & p1, nrt::Layout<T> const & p2, nrt::Layout<T> const & p3)
211 { return nrt::Layout<T>(p1, p2, p3, nrt::LayoutDir::H); }
212 
213 template <class T> inline
215 { return nrt::Layout<T>(imgs, nrt::LayoutDir::H); }
216 
217 template <class T> inline
219 { return nrt::Layout<T>(p1, p2, nrt::LayoutDir::V); }
220 
221 template <class T> inline
223 { return nrt::Layout<T>(p1, p2, nrt::LayoutDir::V); }
224 
225 template <class T> inline
227 { return nrt::Layout<T>(p1, p2, nrt::LayoutDir::V); }
228 
229 template <class T> inline
231 { return nrt::Layout<T>(p1, p2, nrt::LayoutDir::V); }
232 
233 template <class T> inline
234 nrt::Layout<T> nrt::vcat(nrt::Layout<T> const & p1, nrt::Layout<T> const & p2, nrt::Layout<T> const & p3)
235 { return nrt::Layout<T>(p1, p2, p3, nrt::LayoutDir::V); }
236 
237 template <class T> inline
239 { return nrt::Layout<T>(imgs, nrt::LayoutDir::V); }
240 
241 template <class T> inline
242 nrt::Layout<T> nrt::arrcat(nrt::ImageSet<T> const imgs, nrt::int32 const nx)
243 {
244  nrt::int32 const nimgs = imgs.size();
245  std::vector<nrt::Layout<T> > rows;
246 
247  for (nrt::int32 n = 0; n < nimgs; n += nx) rows.push_back(nrt::hcat(imgs.subset(n, n + std::min(nimgs - n, nx))));
248 
249  return nrt::Layout<T>(rows, nrt::LayoutDir::V);
250 }
251 
252 
253 
254 
255 #endif // INCLUDE_NRT_CORE_IMAGE_DETAILS_LAYOUTIMPL_H
256