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.euclidean.twod;
18  
19  import org.apache.commons.geometry.core.GeometryTestUtils;
20  import org.apache.commons.geometry.core.RegionLocation;
21  import org.apache.commons.geometry.core.partitioning.Split;
22  import org.apache.commons.geometry.core.precision.DoublePrecisionContext;
23  import org.apache.commons.geometry.core.precision.EpsilonDoublePrecisionContext;
24  import org.apache.commons.geometry.euclidean.EuclideanTestUtils;
25  import org.apache.commons.geometry.euclidean.oned.Interval;
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  public class LineSpanningSubsetTest {
30  
31      private static final double TEST_EPS = 1e-10;
32  
33      private static final DoublePrecisionContext TEST_PRECISION =
34              new EpsilonDoublePrecisionContext(TEST_EPS);
35  
36      @Test
37      public void testProperties() {
38          // arrange
39          final Line line = Lines.fromPoints(Vector2D.ZERO, Vector2D.Unit.PLUS_X, TEST_PRECISION);
40  
41          // act
42          final LineSpanningSubset result = new LineSpanningSubset(line);
43  
44          // assert
45          Assert.assertSame(line, result.getHyperplane());
46          Assert.assertSame(line, result.getLine());
47  
48          Assert.assertTrue(result.isFull());
49          Assert.assertFalse(result.isEmpty());
50          Assert.assertTrue(result.isInfinite());
51          Assert.assertFalse(result.isFinite());
52  
53          GeometryTestUtils.assertPositiveInfinity(result.getSize());
54          Assert.assertNull(result.getCentroid());
55          Assert.assertNull(result.getBounds());
56  
57          Assert.assertNull(result.getStartPoint());
58          GeometryTestUtils.assertNegativeInfinity(result.getSubspaceStart());
59          Assert.assertNull(result.getEndPoint());
60          GeometryTestUtils.assertPositiveInfinity(result.getSubspaceEnd());
61      }
62  
63      @Test
64      public void testTransform() {
65          // arrange
66          final AffineTransformMatrix2D t = AffineTransformMatrix2D.createRotation(-0.5 * Math.PI)
67                  .translate(Vector2D.Unit.PLUS_X)
68                  .scale(1, -1);
69  
70          final LineConvexSubset span =
71                  Lines.fromPointAndDirection(Vector2D.of(1, 0), Vector2D.Unit.PLUS_X, TEST_PRECISION).span();
72  
73          // act
74          final LineConvexSubset result = span.transform(t);
75  
76          // assert
77          EuclideanTestUtils.assertCoordinatesEqual(Vector2D.Unit.PLUS_Y, result.getLine().getDirection(), TEST_EPS);
78      }
79  
80      @Test
81      public void testReverse() {
82          // arrange
83          final LineConvexSubset span =
84                  Lines.fromPointAndDirection(Vector2D.of(1, 2), Vector2D.Unit.PLUS_X, TEST_PRECISION).span();
85  
86          // act
87          final LineConvexSubset rev = span.reverse();
88  
89          // assert
90          EuclideanTestUtils.assertCoordinatesEqual(Vector2D.of(0, 2), rev.getLine().getOrigin(), TEST_EPS);
91          EuclideanTestUtils.assertCoordinatesEqual(Vector2D.Unit.MINUS_X, rev.getLine().getDirection(), TEST_EPS);
92      }
93  
94      @Test
95      public void testClosest() {
96          // arrange
97          final Vector2D p1 = Vector2D.of(0, -1);
98          final Vector2D p2 = Vector2D.of(0, 1);
99          final LineConvexSubset span =
100                 Lines.fromPointAndDirection(p1, p1.directionTo(p2), TEST_PRECISION).span();
101 
102         // act/assert
103         EuclideanTestUtils.assertCoordinatesEqual(p1, span.closest(p1), TEST_EPS);
104         EuclideanTestUtils.assertCoordinatesEqual(Vector2D.of(0, -2), span.closest(Vector2D.of(0, -2)), TEST_EPS);
105         EuclideanTestUtils.assertCoordinatesEqual(Vector2D.of(0, -2), span.closest(Vector2D.of(2, -2)), TEST_EPS);
106         EuclideanTestUtils.assertCoordinatesEqual(Vector2D.of(0, -1), span.closest(Vector2D.of(-1, -1)), TEST_EPS);
107 
108         EuclideanTestUtils.assertCoordinatesEqual(p2, span.closest(p2), TEST_EPS);
109         EuclideanTestUtils.assertCoordinatesEqual(Vector2D.of(0, 2), span.closest(Vector2D.of(0, 2)), TEST_EPS);
110         EuclideanTestUtils.assertCoordinatesEqual(Vector2D.of(0, 2), span.closest(Vector2D.of(-2, 2)), TEST_EPS);
111         EuclideanTestUtils.assertCoordinatesEqual(Vector2D.of(0, 1), span.closest(Vector2D.of(-1, 1)), TEST_EPS);
112 
113         EuclideanTestUtils.assertCoordinatesEqual(Vector2D.ZERO, span.closest(Vector2D.ZERO), TEST_EPS);
114         EuclideanTestUtils.assertCoordinatesEqual(Vector2D.of(0, 0.5), span.closest(Vector2D.of(1, 0.5)), TEST_EPS);
115         EuclideanTestUtils.assertCoordinatesEqual(Vector2D.of(0, -0.5), span.closest(Vector2D.of(-2, -0.5)), TEST_EPS);
116     }
117 
118     @Test
119     public void testClassify() {
120         // arrange
121         final LineConvexSubset span =
122                 Lines.fromPointAndDirection(Vector2D.of(1, 1), Vector2D.Unit.PLUS_X, TEST_PRECISION).span();
123 
124         // act/assert
125         for (double x = -10; x <= 10; x += 1) {
126             EuclideanTestUtils.assertRegionLocation(span, RegionLocation.INSIDE, Vector2D.of(x, 1 + 1e-11));
127 
128             EuclideanTestUtils.assertRegionLocation(span, RegionLocation.OUTSIDE,
129                     Vector2D.of(x, 0), Vector2D.of(x, 2));
130         }
131     }
132 
133     @Test
134     public void testSplit() {
135         // --- arrange
136         final Vector2D pt = Vector2D.of(1, 1);
137 
138         final LineConvexSubset span = Lines.fromPointAndDirection(pt, Vector2D.Unit.PLUS_X, TEST_PRECISION).span();
139 
140         // --- act
141         Split<LineConvexSubset> split;
142 
143         // parallel
144         split = span.split(Lines.fromPointAndAngle(Vector2D.of(2, 2), 0, TEST_PRECISION));
145         Assert.assertNull(split.getMinus());
146         Assert.assertSame(span, split.getPlus());
147 
148         split = span.split(Lines.fromPointAndAngle(Vector2D.of(2, 2), Math.PI, TEST_PRECISION));
149         Assert.assertSame(span, split.getMinus());
150         Assert.assertNull(split.getPlus());
151 
152         // coincident
153         split = span.split(Lines.fromPointAndDirection(pt, Vector2D.Unit.PLUS_X, TEST_PRECISION));
154         Assert.assertNull(split.getMinus());
155         Assert.assertNull(split.getPlus());
156 
157         // through point on line
158         checkSplit(span.split(Lines.fromPointAndAngle(pt, 1, TEST_PRECISION)),
159                 null, pt,
160                 pt, null);
161         checkSplit(span.split(Lines.fromPointAndAngle(pt, -1, TEST_PRECISION)),
162                 pt, null,
163                 null, pt);
164     }
165 
166     @Test
167     public void testGetInterval() {
168         // arrange
169         final LineConvexSubset span =
170                 Lines.fromPointAndDirection(Vector2D.of(2, -1), Vector2D.Unit.PLUS_X, TEST_PRECISION).span();
171 
172         // act
173         final Interval interval = span.getInterval();
174 
175         // assert
176         GeometryTestUtils.assertNegativeInfinity(interval.getMin());
177         GeometryTestUtils.assertPositiveInfinity(interval.getMax());
178     }
179 
180     @Test
181     public void testToString() {
182         // arrange
183         final LineConvexSubset span =
184                 Lines.fromPointAndDirection(Vector2D.ZERO, Vector2D.Unit.PLUS_X, TEST_PRECISION).span();
185 
186         // act
187         final String str = span.toString();
188 
189         // assert
190         GeometryTestUtils.assertContains("LineSpanningSubset[origin= (0", str);
191         GeometryTestUtils.assertContains(", direction= (1", str);
192     }
193 
194     private static void checkSplit(final Split<LineConvexSubset> split, final Vector2D minusStart, final Vector2D minusEnd,
195                                    final Vector2D plusStart, final Vector2D plusEnd) {
196 
197         final LineConvexSubset minus = split.getMinus();
198         if (minusStart == null && minusEnd == null) {
199             Assert.assertNull(minus);
200         } else {
201             checkPoint(minusStart, minus.getStartPoint());
202             checkPoint(minusEnd, minus.getEndPoint());
203         }
204 
205 
206         final LineConvexSubset plus = split.getPlus();
207         if (plusStart == null && plusEnd == null) {
208             Assert.assertNull(plus);
209         } else {
210             checkPoint(plusStart, plus.getStartPoint());
211             checkPoint(plusEnd, plus.getEndPoint());
212         }
213     }
214 
215     private static void checkPoint(final Vector2D expected, final Vector2D pt) {
216         if (expected == null) {
217             Assert.assertNull(pt);
218         } else {
219             EuclideanTestUtils.assertCoordinatesEqual(expected, pt, TEST_EPS);
220         }
221     }
222 }