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    *      http://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  package org.apache.commons.geometry.spherical;
18  
19  import java.util.List;
20  
21  import org.apache.commons.geometry.core.RegionLocation;
22  import org.apache.commons.geometry.core.partitioning.Split;
23  import org.apache.commons.geometry.core.precision.DoublePrecisionContext;
24  import org.apache.commons.geometry.core.precision.EpsilonDoublePrecisionContext;
25  import org.apache.commons.geometry.euclidean.threed.Vector3D;
26  import org.apache.commons.geometry.spherical.oned.AngularInterval;
27  import org.apache.commons.geometry.spherical.oned.Point1S;
28  import org.apache.commons.geometry.spherical.oned.RegionBSPTree1S;
29  import org.apache.commons.geometry.spherical.twod.GreatArcPath;
30  import org.apache.commons.geometry.spherical.twod.GreatCircle;
31  import org.apache.commons.geometry.spherical.twod.GreatCircles;
32  import org.apache.commons.geometry.spherical.twod.Point2S;
33  import org.apache.commons.geometry.spherical.twod.RegionBSPTree2S;
34  import org.apache.commons.numbers.angle.PlaneAngleRadians;
35  import org.junit.Assert;
36  import org.junit.Test;
37  
38  /** This class contains code listed as examples in the user guide and other documentation.
39   * If any portion of this code changes, the corresponding examples in the documentation <em>must</em> be updated.
40   */
41  public class DocumentationExamplesTest {
42  
43      private static final double TEST_EPS = 1e-12;
44  
45      @Test
46      public void testAngularIntervalExample() {
47          final DoublePrecisionContext precision = new EpsilonDoublePrecisionContext(1e-6);
48  
49          // create angular intervals of different sizes, one of size pi/2 and one of size 3pi/2
50          final AngularInterval a = AngularInterval.of(0, PlaneAngleRadians.PI_OVER_TWO, precision);
51          final AngularInterval b = AngularInterval.of(Point1S.PI, Point1S.of(PlaneAngleRadians.PI_OVER_TWO), precision);
52  
53          // test some points
54          a.contains(Point1S.of(0.25 * Math.PI)); // true
55          b.contains(Point1S.of(0.25 * Math.PI)); // true
56  
57          final RegionLocation aLocZero = a.classify(Point1S.ZERO); // RegionLocation.BOUNDARY
58          final RegionLocation bLocZero = b.classify(Point1S.ZERO); // RegionLocation.INSIDE
59  
60          // -------------------
61          Assert.assertTrue(a.contains(Point1S.of(0.25 * Math.PI)));
62          Assert.assertTrue(b.contains(Point1S.of(0.25 * Math.PI)));
63  
64          Assert.assertEquals(RegionLocation.BOUNDARY, aLocZero);
65          Assert.assertEquals(RegionLocation.INSIDE, bLocZero);
66      }
67  
68      @Test
69      public void testRegionBSPTree1SExample() {
70          final DoublePrecisionContext precision = new EpsilonDoublePrecisionContext(1e-6);
71  
72          // create a region from the union of multiple angular intervals
73          final RegionBSPTree1S tree = RegionBSPTree1S.empty();
74          tree.add(AngularInterval.of(0, 0.25 * Math.PI, precision));
75          tree.add(AngularInterval.of(0.5 * Math.PI, Math.PI, precision));
76          tree.add(AngularInterval.of(0.75 * Math.PI, 1.5 * Math.PI, precision));
77  
78          // compute the region size in radians
79          final double size = tree.getSize(); // 1.25pi
80  
81          // convert back to intervals
82          final List<AngularInterval> intervals = tree.toIntervals(); //size = 2
83  
84          // ---------------
85          Assert.assertEquals(1.25 * Math.PI, size, TEST_EPS);
86          Assert.assertEquals(2, intervals.size());
87      }
88  
89      @Test
90      public void testGreatCircleIntersectionExample() {
91          final DoublePrecisionContext precision = new EpsilonDoublePrecisionContext(1e-6);
92  
93          // create two great circles
94          final GreatCircle a = GreatCircles.fromPoints(Point2S.PLUS_I, Point2S.PLUS_K, precision);
95          final GreatCircle b = GreatCircles.fromPole(Vector3D.Unit.PLUS_Z, precision);
96  
97          // find the two intersection points of the great circles
98          final Point2S ptA = a.intersection(b); //(pi, pi/2)
99          final Point2S ptB = ptA.antipodal(); // (0, pi/2)
100 
101         // ----------------------
102         SphericalTestUtils.assertPointsEq(Point2S.MINUS_I, ptA, TEST_EPS);
103         SphericalTestUtils.assertPointsEq(Point2S.PLUS_I, ptB, TEST_EPS);
104     }
105 
106     @Test
107     public void testRegionBSPTree2SExample() {
108         final DoublePrecisionContext precision = new EpsilonDoublePrecisionContext(1e-6);
109 
110         // create a path outlining a quadrant triangle
111         final GreatArcPath path = GreatArcPath.builder(precision)
112                 .append(Point2S.PLUS_I)
113                 .append(Point2S.PLUS_J)
114                 .append(Point2S.PLUS_K)
115                 .build(true); // close the path with the starting path
116 
117         // convert to a region
118         final RegionBSPTree2S tree = path.toTree();
119 
120         // split in two through the centroid
121         final GreatCircle splitter = GreatCircles.fromPoints(tree.getCentroid(), Point2S.PLUS_K, precision);
122         final Split<RegionBSPTree2S> split = tree.split(splitter);
123 
124         // compute some properties for the minus side
125         final RegionBSPTree2S minus = split.getMinus();
126 
127         final double minusSize = minus.getSize(); // pi/4
128         final List<GreatArcPath> minusPaths = minus.getBoundaryPaths(); // size = 1
129 
130         // ---------------------
131         Assert.assertEquals(Math.PI / 4, minusSize, TEST_EPS);
132         Assert.assertEquals(1, minusPaths.size());
133     }
134 }