bioformats  5.1.8
PlaneRegion.h
1 /*
2  * #%L
3  * OME-BIOFORMATS C++ library for image IO.
4  * Copyright © 2006 - 2016 Open Microscopy Environment:
5  * - Massachusetts Institute of Technology
6  * - National Institutes of Health
7  * - University of Dundee
8  * - Board of Regents of the University of Wisconsin-Madison
9  * - Glencoe Software, Inc.
10  * %%
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  * this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  * this list of conditions and the following disclaimer in the documentation
18  * and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  * The views and conclusions contained in the software and documentation are
33  * those of the authors and should not be interpreted as representing official
34  * policies, either expressed or implied, of any organization.
35  * #L%
36  */
37 
38 #ifndef OME_BIOFORMATS_PLANEREGION_H
39 #define OME_BIOFORMATS_PLANEREGION_H
40 
41 #include <ome/bioformats/Types.h>
42 
43 #include <ome/xml/model/enums/PixelType.h>
44 
45 namespace ome
46 {
47  namespace bioformats
48  {
49 
56  struct PlaneRegion
57  {
66 
67  public:
74  x(0),
75  y(0),
76  w(0),
77  h(0)
78  {}
79 
86  bool
87  valid() const {
88  return w && h;
89  }
90 
103  x(x),
104  y(y),
105  w(w),
106  h(h)
107  {}
108 
115  area() const
116  {
117  return w * h;
118  }
119  };
120 
131  inline
132  PlaneRegion
134  const PlaneRegion& b)
135  {
136  dimension_size_type l1 = a.x;
137  dimension_size_type r1 = a.x + a.w;
138 
139  dimension_size_type l2 = b.x;
140  dimension_size_type r2 = b.x + b.w;
141 
142  if (l1 > r2 || l2 > r1)
143  return PlaneRegion();
144 
145  dimension_size_type t1 = a.y;
146  dimension_size_type b1 = a.y + a.h;
147 
148  dimension_size_type t2 = b.y;
149  dimension_size_type b2 = b.y + b.h;
150 
151  if (t1 > b2 || t2 > b1)
152  return PlaneRegion();
153 
154  dimension_size_type il = std::max(l1, l2);
155  dimension_size_type ir = std::min(r1, r2);
156  dimension_size_type it = std::max(t1, t2);
157  dimension_size_type ib = std::min(b1, b2);
158 
159  return PlaneRegion(il, it, ir-il, ib-it);
160  }
161 
172  inline
173  PlaneRegion
175  const PlaneRegion& b)
176  {
177  dimension_size_type l1 = a.x;
178  dimension_size_type r1 = a.x + a.w;
179 
180  dimension_size_type l2 = b.x;
181  dimension_size_type r2 = b.x + b.w;
182 
183  dimension_size_type t1 = a.y;
184  dimension_size_type b1 = a.y + a.h;
185 
186  dimension_size_type t2 = b.y;
187  dimension_size_type b2 = b.y + b.h;
188 
189  if (l1 == l2 && r1 == r2 &&
190  (t1 == b2 || t2 == b1)) // union along top or bottom edges
191  {
192  dimension_size_type it = std::min(t1, t2);
193  dimension_size_type ib = std::max(b1, b2);
194  return PlaneRegion(l1, it, r1-l1, ib-it);
195  }
196  else if (t1 == t2 && b1 == b2 &&
197  (l1 == r2 || l2 == r1)) // union along left or right edges
198  {
199  dimension_size_type il = std::min(l1, l2);
200  dimension_size_type ir = std::max(r1, r2);
201  return PlaneRegion(il, t1, ir-il, b1-t1);
202  }
203  return PlaneRegion();
204  }
205 
213  template<class charT, class traits>
214  inline std::basic_ostream<charT,traits>&
215  operator<< (std::basic_ostream<charT,traits>& os,
216  const PlaneRegion& region)
217  {
218  return os << "x=" << region.x
219  << " y=" << region.y
220  << " w=" << region.w
221  << " h=" << region.h;
222  }
223 
224  }
225 }
226 
227 #endif // OME_BIOFORMATS_PLANEREGION_H
228 
229 /*
230  * Local Variables:
231  * mode:C++
232  * End:
233  */
PlaneRegion(dimension_size_type x, dimension_size_type y, dimension_size_type w, dimension_size_type h)
Construct from coordinates, width and height.
Definition: PlaneRegion.h:99
dimension_size_type area() const
Get area.
Definition: PlaneRegion.h:115
dimension_size_type h
The height of the region.
Definition: PlaneRegion.h:65
dimension_size_type y
The Y coordinate of the upper-left corner of the region.
Definition: PlaneRegion.h:61
PlaneRegion()
Default construct.
Definition: PlaneRegion.h:73
Open Microscopy Environment C++ implementation.
Definition: CoreMetadata.cpp:40
bool valid() const
Is the region valid?
Definition: PlaneRegion.h:87
dimension_size_type x
The X coordinate of the upper-left corner of the region.
Definition: PlaneRegion.h:59
PlaneRegion operator&(const PlaneRegion &a, const PlaneRegion &b)
Intersect two regions.
Definition: PlaneRegion.h:133
PlaneRegion operator|(const PlaneRegion &a, const PlaneRegion &b)
Combine (union) two regions.
Definition: PlaneRegion.h:174
dimension_size_type w
The width of the region.
Definition: PlaneRegion.h:63
std::size_t dimension_size_type
Size type for image dimensions.
Definition: Types.h:59
A rectangular region.
Definition: PlaneRegion.h:56