ome-files  0.5.0
PlaneRegion.h
1 /*
2  * #%L
3  * OME-FILES C++ library for image IO.
4  * Copyright © 2006 - 2015 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_FILES_PLANEREGION_H
39 #define OME_FILES_PLANEREGION_H
40 
41 #include <ome/files/Types.h>
42 
43 #include <ome/xml/model/enums/PixelType.h>
44 
45 #ifdef _MSC_VER
46 #pragma push_macro("min")
47 #undef min
48 #pragma push_macro("max")
49 #undef max
50 #endif
51 
52 namespace ome
53 {
54  namespace files
55  {
56 
63  struct PlaneRegion
64  {
73 
74  public:
81  x(0),
82  y(0),
83  w(0),
84  h(0)
85  {}
86 
93  bool
94  valid() const {
95  return w && h;
96  }
97 
110  x(x),
111  y(y),
112  w(w),
113  h(h)
114  {}
115 
122  area() const
123  {
124  return w * h;
125  }
126  };
127 
138  inline
141  const PlaneRegion& b)
142  {
143  dimension_size_type l1 = a.x;
144  dimension_size_type r1 = a.x + a.w;
145 
146  dimension_size_type l2 = b.x;
147  dimension_size_type r2 = b.x + b.w;
148 
149  if (l1 > r2 || l2 > r1)
150  return PlaneRegion();
151 
152  dimension_size_type t1 = a.y;
153  dimension_size_type b1 = a.y + a.h;
154 
155  dimension_size_type t2 = b.y;
156  dimension_size_type b2 = b.y + b.h;
157 
158  if (t1 > b2 || t2 > b1)
159  return PlaneRegion();
160 
161  dimension_size_type il = std::max(l1, l2);
162  dimension_size_type ir = std::min(r1, r2);
163  dimension_size_type it = std::max(t1, t2);
164  dimension_size_type ib = std::min(b1, b2);
165 
166  return PlaneRegion(il, it, ir-il, ib-it);
167  }
168 
179  inline
182  const PlaneRegion& b)
183  {
184  dimension_size_type l1 = a.x;
185  dimension_size_type r1 = a.x + a.w;
186 
187  dimension_size_type l2 = b.x;
188  dimension_size_type r2 = b.x + b.w;
189 
190  dimension_size_type t1 = a.y;
191  dimension_size_type b1 = a.y + a.h;
192 
193  dimension_size_type t2 = b.y;
194  dimension_size_type b2 = b.y + b.h;
195 
196  if (l1 == l2 && r1 == r2 &&
197  (t1 == b2 || t2 == b1)) // union along top or bottom edges
198  {
199  dimension_size_type it = std::min(t1, t2);
200  dimension_size_type ib = std::max(b1, b2);
201  return PlaneRegion(l1, it, r1-l1, ib-it);
202  }
203  else if (t1 == t2 && b1 == b2 &&
204  (l1 == r2 || l2 == r1)) // union along left or right edges
205  {
206  dimension_size_type il = std::min(l1, l2);
207  dimension_size_type ir = std::max(r1, r2);
208  return PlaneRegion(il, t1, ir-il, b1-t1);
209  }
210  return PlaneRegion();
211  }
212 
220  template<class charT, class traits>
221  inline std::basic_ostream<charT,traits>&
222  operator<< (std::basic_ostream<charT,traits>& os,
223  const PlaneRegion& region)
224  {
225  return os << "x=" << region.x
226  << " y=" << region.y
227  << " w=" << region.w
228  << " h=" << region.h;
229  }
230 
231  }
232 }
233 
234 #ifdef _MSC_VER
235 #pragma pop_macro("min")
236 #pragma pop_macro("max")
237 #endif
238 
239 #endif // OME_FILES_PLANEREGION_H
240 
241 /*
242  * Local Variables:
243  * mode:C++
244  * End:
245  */
std::size_t dimension_size_type
Size type for image dimensions.
Definition: Types.h:58
dimension_size_type w
The width of the region.
Definition: PlaneRegion.h:70
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:106
PlaneRegion operator &(const PlaneRegion &a, const PlaneRegion &b)
Intersect two regions.
Definition: PlaneRegion.h:140
dimension_size_type x
The X coordinate of the upper-left corner of the region.
Definition: PlaneRegion.h:66
bool valid() const
Is the region valid?
Definition: PlaneRegion.h:94
dimension_size_type area() const
Get area.
Definition: PlaneRegion.h:122
Open Microscopy Environment C++.
dimension_size_type y
The Y coordinate of the upper-left corner of the region.
Definition: PlaneRegion.h:68
dimension_size_type h
The height of the region.
Definition: PlaneRegion.h:72
PlaneRegion()
Default construct.
Definition: PlaneRegion.h:80
PlaneRegion operator|(const PlaneRegion &a, const PlaneRegion &b)
Combine (union) two regions.
Definition: PlaneRegion.h:181
A rectangular region.
Definition: PlaneRegion.h:63