View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.geometry.euclidean.oned;
23  
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import org.hipparchus.geometry.partitioning.BSPTree;
29  import org.hipparchus.geometry.partitioning.Region;
30  import org.hipparchus.geometry.partitioning.RegionFactory;
31  import org.hipparchus.util.FastMath;
32  import org.hipparchus.util.Precision;
33  import org.junit.Assert;
34  import org.junit.Test;
35  
36  public class IntervalsSetTest {
37  
38      @Test
39      public void testInterval() {
40          IntervalsSet set = new IntervalsSet(Arrays.asList(new OrientedPoint(new Vector1D(2.3), false, 1.0e-10).wholeHyperplane(),
41                                                            new OrientedPoint(new Vector1D(5.7), true, 1.0e-10).wholeHyperplane()),
42                                              1.0e-10);
43          Assert.assertEquals(3.4, set.getSize(), 1.0e-10);
44          Assert.assertEquals(4.0, ((Vector1D) set.getBarycenter()).getX(), 1.0e-10);
45          Assert.assertEquals(Region.Location.BOUNDARY, set.checkPoint(new Vector1D(2.3)));
46          Assert.assertEquals(Region.Location.BOUNDARY, set.checkPoint(new Vector1D(5.7)));
47          Assert.assertEquals(Region.Location.OUTSIDE,  set.checkPoint(new Vector1D(1.2)));
48          Assert.assertEquals(Region.Location.OUTSIDE,  set.checkPoint(new Vector1D(8.7)));
49          Assert.assertEquals(Region.Location.INSIDE,   set.checkPoint(new Vector1D(3.0)));
50          Assert.assertEquals(2.3, set.getInf(), 1.0e-10);
51          Assert.assertEquals(5.7, set.getSup(), 1.0e-10);
52          OrientedPoint op = (OrientedPoint) set.getTree(false).getCut().getHyperplane();
53          Assert.assertEquals(0.0, op.emptyHyperplane().getSize(), 1.0e-10);
54          Assert.assertEquals(1.0e-10, op.getTolerance(), 1.0e-20);
55          Assert.assertTrue(Double.isInfinite(op.wholeSpace().getSize()));
56          Assert.assertEquals(2.3, op.getLocation().getX(), 1.0e-10);
57          Assert.assertEquals(-0.7, op.getOffset(new Vector1D(3.0)), 1.0e-10);
58          Assert.assertSame(op.getLocation(), op.project(new Vector1D(3.0)));
59          op.revertSelf();
60          Assert.assertEquals(+0.7, op.getOffset(new Vector1D(3.0)), 1.0e-10);
61      }
62  
63      @Test
64      public void testNoBoundaries() {
65          IntervalsSet set = new IntervalsSet(new ArrayList<>(), 1.0e-10);
66          Assert.assertEquals(Region.Location.INSIDE, set.checkPoint(new Vector1D(-Double.MAX_VALUE)));
67          Assert.assertEquals(Region.Location.INSIDE, set.checkPoint(new Vector1D(+Double.MAX_VALUE)));
68          Assert.assertTrue(Double.isInfinite(set.getSize()));
69      }
70  
71      @Test
72      public void testInfinite() {
73          IntervalsSet set = new IntervalsSet(9.0, Double.POSITIVE_INFINITY, 1.0e-10);
74          Assert.assertEquals(Region.Location.BOUNDARY, set.checkPoint(new Vector1D(9.0)));
75          Assert.assertEquals(Region.Location.OUTSIDE,  set.checkPoint(new Vector1D(8.4)));
76          for (double e = 1.0; e <= 6.0; e += 1.0) {
77              Assert.assertEquals(Region.Location.INSIDE,
78                                  set.checkPoint(new Vector1D(FastMath.pow(10.0, e))));
79          }
80          Assert.assertTrue(Double.isInfinite(set.getSize()));
81          Assert.assertEquals(9.0, set.getInf(), 1.0e-10);
82          Assert.assertTrue(Double.isInfinite(set.getSup()));
83  
84          set = (IntervalsSet) new RegionFactory<Euclidean1D>().getComplement(set);
85          Assert.assertEquals(9.0, set.getSup(), 1.0e-10);
86          Assert.assertTrue(Double.isInfinite(set.getInf()));
87  
88      }
89  
90      @Test
91      public void testMultiple() {
92          RegionFactory<Euclidean1D> factory = new RegionFactory<Euclidean1D>();
93          IntervalsSet set = (IntervalsSet)
94          factory.intersection(factory.union(factory.difference(new IntervalsSet(1.0, 6.0, 1.0e-10),
95                                                                new IntervalsSet(3.0, 5.0, 1.0e-10)),
96                                                                new IntervalsSet(9.0, Double.POSITIVE_INFINITY, 1.0e-10)),
97                                                                new IntervalsSet(Double.NEGATIVE_INFINITY, 11.0, 1.0e-10));
98          Assert.assertEquals(5.0, set.getSize(), 1.0e-10);
99          Assert.assertEquals(5.9, ((Vector1D) set.getBarycenter()).getX(), 1.0e-10);
100         Assert.assertEquals(Region.Location.OUTSIDE,  set.checkPoint(new Vector1D(0.0)));
101         Assert.assertEquals(Region.Location.OUTSIDE,  set.checkPoint(new Vector1D(4.0)));
102         Assert.assertEquals(Region.Location.OUTSIDE,  set.checkPoint(new Vector1D(8.0)));
103         Assert.assertEquals(Region.Location.OUTSIDE,  set.checkPoint(new Vector1D(12.0)));
104         Assert.assertEquals(Region.Location.INSIDE,   set.checkPoint(new Vector1D(1.2)));
105         Assert.assertEquals(Region.Location.INSIDE,   set.checkPoint(new Vector1D(5.9)));
106         Assert.assertEquals(Region.Location.INSIDE,   set.checkPoint(new Vector1D(9.01)));
107         Assert.assertEquals(Region.Location.BOUNDARY, set.checkPoint(new Vector1D(5.0)));
108         Assert.assertEquals(Region.Location.BOUNDARY, set.checkPoint(new Vector1D(11.0)));
109         Assert.assertEquals( 1.0, set.getInf(), 1.0e-10);
110         Assert.assertEquals(11.0, set.getSup(), 1.0e-10);
111 
112         List<Interval> list = set.asList();
113         Assert.assertEquals(3, list.size());
114         Assert.assertEquals( 1.0, list.get(0).getInf(), 1.0e-10);
115         Assert.assertEquals( 3.0, list.get(0).getSup(), 1.0e-10);
116         Assert.assertEquals( 5.0, list.get(1).getInf(), 1.0e-10);
117         Assert.assertEquals( 6.0, list.get(1).getSup(), 1.0e-10);
118         Assert.assertEquals( 9.0, list.get(2).getInf(), 1.0e-10);
119         Assert.assertEquals(11.0, list.get(2).getSup(), 1.0e-10);
120 
121     }
122 
123     @Test
124     public void testSinglePoint() {
125         IntervalsSet set = new IntervalsSet(1.0, 1.0, 1.0e-10);
126         Assert.assertEquals(0.0, set.getSize(), Precision.SAFE_MIN);
127         Assert.assertEquals(1.0, ((Vector1D) set.getBarycenter()).getX(), Precision.EPSILON);
128         try {
129             set.iterator().remove();
130             Assert.fail("an exception should have been thrown");
131         } catch (UnsupportedOperationException uoe) {
132             // expected
133         }
134     }
135 
136     @Test
137     public void testShuffledTreeNonRepresentable() {
138         doTestShuffledTree(FastMath.toRadians( 85.0), FastMath.toRadians( 95.0),
139                            FastMath.toRadians(265.0), FastMath.toRadians(275.0),
140                            1.0e-10);
141     }
142 
143     @Test
144     public void testShuffledTreeRepresentable() {
145         doTestShuffledTree(1.0, 2.0,
146                            4.0, 5.0,
147                            1.0e-10);
148     }
149 
150     private void doTestShuffledTree(final double a0, final double a1,
151                                     final double a2, final double a3,
152                                     final double tol) {
153 
154         // intervals set [ a0 ; a1 ] U [ a2 ; a3 ]
155         final IntervalsSet setA =
156                         new IntervalsSet(new BSPTree<>(new OrientedPoint(new Vector1D(a0), false, tol).wholeHyperplane(),
157                                                        new BSPTree<>(Boolean.FALSE),
158                                                        new BSPTree<>(new OrientedPoint(new Vector1D(a1), true, tol).wholeHyperplane(),
159                                                                      new BSPTree<>(new OrientedPoint(new Vector1D(a2), false, tol).wholeHyperplane(),
160                                                                                    new BSPTree<>(Boolean.FALSE),
161                                                                                    new BSPTree<>(new OrientedPoint(new Vector1D(a3), true, tol).wholeHyperplane(),
162                                                                                                  new BSPTree<>(Boolean.FALSE),
163                                                                                                  new BSPTree<>(Boolean.TRUE),
164                                                                                                  null),
165                                                                                    null),
166                                                                      new BSPTree<>(Boolean.TRUE),
167                                                                      null),
168                                                        null),
169                                          1.0e-10);
170         Assert.assertEquals((a1 - a0) + (a3 - a2), setA.getSize(), 1.0e-10);
171         Assert.assertEquals(2, setA.asList().size());
172         Assert.assertEquals(a0, setA.asList().get(0).getInf(), 1.0e-15);
173         Assert.assertEquals(a1, setA.asList().get(0).getSup(), 1.0e-15);
174         Assert.assertEquals(a2, setA.asList().get(1).getInf(), 1.0e-15);
175         Assert.assertEquals(a3, setA.asList().get(1).getSup(), 1.0e-15);
176 
177         // same intervals set [ a0 ; a1 ] U [ a2 ; a3 ], but with a different tree organization
178         final IntervalsSet setB =
179                         new IntervalsSet(new BSPTree<>(new OrientedPoint(new Vector1D(a2), false, tol).wholeHyperplane(),
180                                                        new BSPTree<>(new OrientedPoint(new Vector1D(a0), false, tol).wholeHyperplane(),
181                                                                      new BSPTree<>(Boolean.FALSE),
182                                                                      new BSPTree<>(new OrientedPoint(new Vector1D(a1), true, tol).wholeHyperplane(),
183                                                                                    new BSPTree<>(Boolean.FALSE),
184                                                                                    new BSPTree<>(Boolean.TRUE),
185                                                                                    null),
186                                                                      null),
187                                                        new BSPTree<>(new OrientedPoint(new Vector1D(a3), true, tol).wholeHyperplane(),
188                                                                      new BSPTree<>(Boolean.FALSE),
189                                                                      new BSPTree<>(Boolean.TRUE),
190                                                                      null),
191                                                        null),
192                                          1.0e-10);
193         Assert.assertEquals((a1 - a0) + (a3 - a2), setB.getSize(), 1.0e-10);
194         Assert.assertEquals(2, setB.asList().size());
195         Assert.assertEquals(a0, setB.asList().get(0).getInf(), 1.0e-15);
196         Assert.assertEquals(a1, setB.asList().get(0).getSup(), 1.0e-15);
197         Assert.assertEquals(a2, setB.asList().get(1).getInf(), 1.0e-15);
198         Assert.assertEquals(a3, setB.asList().get(1).getSup(), 1.0e-15);
199 
200         final IntervalsSet intersection = (IntervalsSet) new RegionFactory<Euclidean1D>().intersection(setA, setB);
201         Assert.assertEquals((a1 - a0) + (a3 - a2), intersection.getSize(), 1.0e-10);
202         Assert.assertEquals(2, intersection.asList().size());
203         Assert.assertEquals(a0, intersection.asList().get(0).getInf(), 1.0e-15);
204         Assert.assertEquals(a1, intersection.asList().get(0).getSup(), 1.0e-15);
205         Assert.assertEquals(a2, intersection.asList().get(1).getInf(), 1.0e-15);
206         Assert.assertEquals(a3, intersection.asList().get(1).getSup(), 1.0e-15);
207 
208     }
209 
210 }