View Javadoc
1   /*
2    * Copyright (C) 2017 Alberto Irurueta Carro (alberto@irurueta.com)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.irurueta.geometry;
18  
19  import java.io.Serializable;
20  
21  /**
22   * Abstract representation of a box for a point in an n-dimensional representation.
23   *
24   * @param <P> a point implementation.
25   */
26  public abstract class Box<P extends Point<P>> implements Serializable {
27  
28      /**
29       * Low coordinate values.
30       */
31      protected P lo;
32  
33      /**
34       * High coordinate values.
35       */
36      protected P hi;
37  
38      /**
39       * Empty constructor.
40       */
41      protected Box() {
42      }
43  
44      /**
45       * Constructor.
46       *
47       * @param lo low coordinate values.
48       * @param hi high coordinate values.
49       */
50      protected Box(final P lo, final P hi) {
51          internalSetBounds(lo, hi);
52      }
53  
54      /**
55       * Gets low coordinate values.
56       *
57       * @return low coordinate values.
58       */
59      public P getLo() {
60          return lo;
61      }
62  
63      /**
64       * Sets low coordinate values.
65       *
66       * @param lo low coordinate values.
67       */
68      public void setLo(final P lo) {
69          this.lo = lo;
70      }
71  
72      /**
73       * Gets high coordinate values.
74       *
75       * @return high coordinate values.
76       */
77      public P getHi() {
78          return hi;
79      }
80  
81      /**
82       * Sets high coordinate values.
83       *
84       * @param hi high coordinate values.
85       */
86      public void setHi(final P hi) {
87          this.hi = hi;
88      }
89  
90      /**
91       * Sets boundaries.
92       *
93       * @param lo low coordinate values.
94       * @param hi high coordinate values.
95       */
96      public void setBounds(final P lo, final P hi) {
97          internalSetBounds(lo, hi);
98      }
99  
100     /**
101      * Gets square distance of provided point to the boundaries of this box or zero if the point is
102      * inside this box.
103      *
104      * @param point point to be checked.
105      * @return distance of provided point.
106      */
107     public double getSqrDistance(final P point) {
108         final var dim = point.getDimensions();
109 
110         var dd = 0.0;
111         for (var i = 0; i < dim; i++) {
112             final var pointValue = point.getInhomogeneousCoordinate(i);
113             final var loValue = lo.getInhomogeneousCoordinate(i);
114             final var hiValue = hi.getInhomogeneousCoordinate(i);
115 
116             if (pointValue < loValue) {
117                 dd += sqr(pointValue - loValue);
118             }
119             if (pointValue > hiValue) {
120                 dd += sqr(pointValue - hiValue);
121             }
122         }
123         return dd;
124     }
125 
126     /**
127      * Gets distance of provided point to the boundaries of this box or zero if the point is
128      * inside this box.
129      *
130      * @param point point to be checked.
131      * @return distance of provided point.
132      */
133     public double getDistance(final P point) {
134         return Math.sqrt(getSqrDistance(point));
135     }
136 
137     /**
138      * Returns the squared value.
139      *
140      * @param value value to be squared.
141      * @return squared value.
142      */
143     private double sqr(final double value) {
144         return value * value;
145     }
146 
147     /**
148      * Internally sets boundaries.
149      *
150      * @param lo low coordinate values.
151      * @param hi high coordinate values.
152      */
153     private void internalSetBounds(final P lo, final P hi) {
154         this.lo = lo;
155         this.hi = hi;
156     }
157 }