View Javadoc
1   package nom.tam.image.tile.operation;
2   
3   /*
4    * #%L
5    * nom.tam FITS library
6    * %%
7    * Copyright (C) 1996 - 2024 nom-tam-fits
8    * %%
9    * This is free and unencumbered software released into the public domain.
10   *
11   * Anyone is free to copy, modify, publish, use, compile, sell, or
12   * distribute this software, either in source code form or as a compiled
13   * binary, for any purpose, commercial or non-commercial, and by any
14   * means.
15   *
16   * In jurisdictions that recognize copyright laws, the author or authors
17   * of this software dedicate any and all copyright interest in the
18   * software to the public domain. We make this dedication for the benefit
19   * of the public at large and to the detriment of our heirs and
20   * successors. We intend this dedication to be an overt act of
21   * relinquishment in perpetuity of all present and future rights to this
22   * software under copyright law.
23   *
24   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27   * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
28   * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
29   * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30   * OTHER DEALINGS IN THE SOFTWARE.
31   * #L%
32   */
33  
34  import java.util.Arrays;
35  
36  /**
37   * The area represented by a 2D tile in an image, including its location inside the image and its size. FITS tiles are
38   * always 2-dimentional, but really images of any dimensions may be covered with such tiles.
39   * 
40   * @see nom.tam.image.ImageTiler
41   */
42  public class TileArea {
43  
44      private int[] startPoint;
45  
46      private int[] endPoint;
47  
48      /**
49       * Sets the pixel boundaries where this tile ends. Alternatively you can specify the tile size with
50       * {@link #size(int...)}
51       * 
52       * @param  newEndPoint the pixel indices along all image dimensions that specify where this tile ends. The tile will
53       *                         end before reaching these indices and will not include them.
54       * 
55       * @return             itself
56       * 
57       * @see                #start(int...)
58       * @see                #size(int...)
59       */
60      public TileArea end(int... newEndPoint) {
61          endPoint = Arrays.copyOf(newEndPoint, newEndPoint.length);
62          return this;
63      }
64  
65      /**
66       * Returns the dimensionality of the tile area.
67       *
68       * @return The dimensionality of the tile area or 0 if the tile is uninitialized.
69       *
70       * @since  1.17
71       */
72      public int dimension() {
73          return startPoint == null ? 0 : startPoint.length;
74      }
75  
76      /**
77       * @param  other                    the tile to test intersection with
78       *
79       * @return                          true if the tile intersects with the specified other tile?
80       *
81       * @throws IllegalArgumentException if the two tiles have different dimensionalities.
82       */
83      public boolean intersects(TileArea other) throws IllegalArgumentException {
84          if (other.dimension() != dimension()) {
85              throw new IllegalArgumentException(
86                      "Tiles of different dimensionalities (" + other.dimension() + " vs " + dimension() + ".");
87          }
88  
89          for (int i = dimension(); --i >= 0;) {
90              if ((other.startPoint[i] >= endPoint[i]) || (startPoint[i] >= other.endPoint[i])) {
91                  return false;
92              }
93          }
94  
95          return true;
96      }
97  
98      /**
99       * Sets the size of this tile area. Alternatively you can specify where the tiles ends in the image via
100      * {@link #end(int...)}.
101      * 
102      * @param  sizes the tile size in pixels. If the sizes are specified in the leading dimensions only, the tile sizes
103      *                   in the remaining dimensions will default to 1.
104      * 
105      * @return       itself
106      * 
107      * @see          #start(int...)
108      * @see          #end(int...)
109      */
110     public TileArea size(int... sizes) {
111         endPoint = new int[startPoint.length];
112         for (int index = 0; index < startPoint.length; index++) {
113             endPoint[index] = startPoint[index] + (index < sizes.length ? sizes[index] : 1);
114         }
115         return this;
116     }
117 
118     /**
119      * Sets the pixel boundaries where this tile begins. {@link #size(int...)}
120      * 
121      * @param  newStartPoint the pixel indices along all image dimensions that specify where this tile begins. The tile
122      *                           will include the pixels at the specified indices.
123      * 
124      * @return               itself
125      * 
126      * @see                  #start(int...)
127      * @see                  #size(int...)
128      */
129     public TileArea start(int... newStartPoint) {
130         startPoint = Arrays.copyOf(newStartPoint, newStartPoint.length);
131         return this;
132     }
133 }