View Javadoc

1   /*
2    * Copyright 2011 Stefan C. Mueller.
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  package org.smurn.jply.lwjgldemo;
17  
18  /**
19   * Three-dimensional, axis-oriented rectangular bounds.
20   */
21  class RectBounds {
22  
23      private double minX;
24  
25      private double maxX;
26  
27      private double minY;
28  
29      private double maxY;
30  
31      private double minZ;
32  
33      private double maxZ;
34  
35      /**
36       * Creates an instance with given bounds.
37       * @param minX Minimum value.
38       * @param maxX Maximum value.
39       * @param minY Minimum value.
40       * @param maxY Maximum value.
41       * @param minZ Minimum value.
42       * @param maxZ Maximum value.
43       */
44      RectBounds(final double minX, final double maxX, final double minY,
45              final double maxY, final double minZ, final double maxZ) {
46          this.minX = minX;
47          this.maxX = maxX;
48          this.minY = minY;
49          this.maxY = maxY;
50          this.minZ = minZ;
51          this.maxZ = maxZ;
52      }
53  
54      /**
55       * Creates an instance with initially invalid bounds ready to be used
56       * with {@link #addPoint(double, double, double)}.
57       */
58      RectBounds() {
59          this.minX = Double.POSITIVE_INFINITY;
60          this.maxX = Double.NEGATIVE_INFINITY;
61          this.minY = Double.POSITIVE_INFINITY;
62          this.maxY = Double.NEGATIVE_INFINITY;
63          this.minZ = Double.POSITIVE_INFINITY;
64          this.maxZ = Double.NEGATIVE_INFINITY;
65      }
66  
67      /**
68       * Increases the bounds to include a given point.
69       * <p>If the point is already in the interior of the bounded
70       * area the bounds are not changed.</p>
71       * @param x Coordinate of the point to include.
72       * @param y Coordinate of the point to include.
73       * @param z  Coordinate of the point to include.
74       */
75      public void addPoint(final double x, final double y, final double z) {
76          minX = Math.min(minX, x);
77          minY = Math.min(minY, y);
78          minZ = Math.min(minZ, z);
79          maxX = Math.max(maxX, x);
80          maxY = Math.max(maxY, y);
81          maxZ = Math.max(maxZ, z);
82      }
83  
84      /**
85       * Gets the coordinates of the center point.
86       */
87      public double[] getCenter() {
88          return new double[]{
89                      (minX + maxX) / 2.0,
90                      (minY + maxY) / 2.0,
91                      (minZ + maxZ) / 2.0
92                  };
93      }
94      
95      /**
96       * Gets the scale factor by which the box needs to be multiplied
97       * that it fits into a cube with edge length 1.
98       */
99      public double getScaleToUnityBox(){
100         double largestEdge = 0;
101         largestEdge = Math.max(largestEdge, maxX - minX);
102         largestEdge = Math.max(largestEdge, maxY - minY);
103         largestEdge = Math.max(largestEdge, maxZ - minZ);
104         return 1.0 / largestEdge;
105     }
106 
107     /**
108      * Gets the upper boundary of x.
109      * @return Upper boundary of x.
110      */
111     public double getMaxX() {
112         return maxX;
113     }
114 
115     /**
116      * Gets the upper boundary of y.
117      * @return Upper boundary of y.
118      */
119     public double getMaxY() {
120         return maxY;
121     }
122 
123     /**
124      * Gets the upper boundary of z.
125      * @return Upper boundary of z.
126      */
127     public double getMaxZ() {
128         return maxZ;
129     }
130 
131     /**
132      * Gets the lower boundary of x.
133      * @return Lower boundary of x.
134      */
135     public double getMinX() {
136         return minX;
137     }
138 
139     /**
140      * Gets the lower boundary of y.
141      * @return Lower boundary of y.
142      */
143     public double getMinY() {
144         return minY;
145     }
146 
147     /**
148      * Gets the lower boundary of z.
149      * @return Lower boundary of z.
150      */
151     public double getMinZ() {
152         return minZ;
153     }
154 }