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.twod;
23  
24  import java.util.List;
25  
26  import org.hipparchus.geometry.euclidean.oned.Euclidean1D;
27  import org.hipparchus.geometry.euclidean.oned.IntervalsSet;
28  import org.hipparchus.geometry.partitioning.RegionFactory;
29  import org.junit.Assert;
30  import org.junit.Test;
31  
32  public class SubLineTest {
33  
34      @Test
35      public void testEndPoints() {
36          Vector2D p1 = new Vector2D(-1, -7);
37          Vector2D p2 = new Vector2D(7, -1);
38          Segment segment = new Segment(p1, p2, new Line(p1, p2, 1.0e-10));
39          SubLine sub = new SubLine(segment);
40          List<Segment> segments = sub.getSegments();
41          Assert.assertEquals(1, segments.size());
42          Assert.assertEquals(0.0, new Vector2D(-1, -7).distance(segments.get(0).getStart()), 1.0e-10);
43          Assert.assertEquals(0.0, new Vector2D( 7, -1).distance(segments.get(0).getEnd()), 1.0e-10);
44      }
45  
46      @Test
47      public void testNoEndPoints() {
48          SubLine wholeLine = new Line(new Vector2D(-1, 7), new Vector2D(7, 1), 1.0e-10).wholeHyperplane();
49          List<Segment> segments = wholeLine.getSegments();
50          Assert.assertEquals(1, segments.size());
51          Assert.assertTrue(Double.isInfinite(segments.get(0).getStart().getX()) &&
52                            segments.get(0).getStart().getX() < 0);
53          Assert.assertTrue(Double.isInfinite(segments.get(0).getStart().getY()) &&
54                            segments.get(0).getStart().getY() > 0);
55          Assert.assertTrue(Double.isInfinite(segments.get(0).getEnd().getX()) &&
56                            segments.get(0).getEnd().getX() > 0);
57          Assert.assertTrue(Double.isInfinite(segments.get(0).getEnd().getY()) &&
58                            segments.get(0).getEnd().getY() < 0);
59      }
60  
61      @Test
62      public void testNoSegments() {
63          SubLine empty = new SubLine(new Line(new Vector2D(-1, -7), new Vector2D(7, -1), 1.0e-10),
64                                      new RegionFactory<Euclidean1D>().getComplement(new IntervalsSet(1.0e-10)));
65          List<Segment> segments = empty.getSegments();
66          Assert.assertEquals(0, segments.size());
67      }
68  
69      @Test
70      public void testSeveralSegments() {
71          SubLine twoSubs = new SubLine(new Line(new Vector2D(-1, -7), new Vector2D(7, -1), 1.0e-10),
72                                      new RegionFactory<Euclidean1D>().union(new IntervalsSet(1, 2, 1.0e-10),
73                                                                             new IntervalsSet(3, 4, 1.0e-10)));
74          List<Segment> segments = twoSubs.getSegments();
75          Assert.assertEquals(2, segments.size());
76      }
77  
78      @Test
79      public void testHalfInfiniteNeg() {
80          SubLine empty = new SubLine(new Line(new Vector2D(-1, -7), new Vector2D(7, -1), 1.0e-10),
81                                      new IntervalsSet(Double.NEGATIVE_INFINITY, 0.0, 1.0e-10));
82          List<Segment> segments = empty.getSegments();
83          Assert.assertEquals(1, segments.size());
84          Assert.assertTrue(Double.isInfinite(segments.get(0).getStart().getX()) &&
85                            segments.get(0).getStart().getX() < 0);
86          Assert.assertTrue(Double.isInfinite(segments.get(0).getStart().getY()) &&
87                            segments.get(0).getStart().getY() < 0);
88          Assert.assertEquals(0.0, new Vector2D(3, -4).distance(segments.get(0).getEnd()), 1.0e-10);
89      }
90  
91      @Test
92      public void testHalfInfinitePos() {
93          SubLine empty = new SubLine(new Line(new Vector2D(-1, -7), new Vector2D(7, -1), 1.0e-10),
94                                      new IntervalsSet(0.0, Double.POSITIVE_INFINITY, 1.0e-10));
95          List<Segment> segments = empty.getSegments();
96          Assert.assertEquals(1, segments.size());
97          Assert.assertEquals(0.0, new Vector2D(3, -4).distance(segments.get(0).getStart()), 1.0e-10);
98          Assert.assertTrue(Double.isInfinite(segments.get(0).getEnd().getX()) &&
99                            segments.get(0).getEnd().getX() > 0);
100         Assert.assertTrue(Double.isInfinite(segments.get(0).getEnd().getY()) &&
101                           segments.get(0).getEnd().getY() > 0);
102     }
103 
104     @Test
105     public void testIntersectionInsideInside() {
106         SubLine sub1 = new SubLine(new Vector2D(1, 1), new Vector2D(3, 1), 1.0e-10);
107         SubLine sub2 = new SubLine(new Vector2D(2, 0), new Vector2D(2, 2), 1.0e-10);
108         Assert.assertEquals(0.0, new Vector2D(2, 1).distance(sub1.intersection(sub2, true)),  1.0e-12);
109         Assert.assertEquals(0.0, new Vector2D(2, 1).distance(sub1.intersection(sub2, false)), 1.0e-12);
110     }
111 
112     @Test
113     public void testIntersectionInsideBoundary() {
114         SubLine sub1 = new SubLine(new Vector2D(1, 1), new Vector2D(3, 1), 1.0e-10);
115         SubLine sub2 = new SubLine(new Vector2D(2, 0), new Vector2D(2, 1), 1.0e-10);
116         Assert.assertEquals(0.0, new Vector2D(2, 1).distance(sub1.intersection(sub2, true)),  1.0e-12);
117         Assert.assertNull(sub1.intersection(sub2, false));
118     }
119 
120     @Test
121     public void testIntersectionInsideOutside() {
122         SubLine sub1 = new SubLine(new Vector2D(1, 1), new Vector2D(3, 1), 1.0e-10);
123         SubLine sub2 = new SubLine(new Vector2D(2, 0), new Vector2D(2, 0.5), 1.0e-10);
124         Assert.assertNull(sub1.intersection(sub2, true));
125         Assert.assertNull(sub1.intersection(sub2, false));
126     }
127 
128     @Test
129     public void testIntersectionBoundaryBoundary() {
130         SubLine sub1 = new SubLine(new Vector2D(1, 1), new Vector2D(2, 1), 1.0e-10);
131         SubLine sub2 = new SubLine(new Vector2D(2, 0), new Vector2D(2, 1), 1.0e-10);
132         Assert.assertEquals(0.0, new Vector2D(2, 1).distance(sub1.intersection(sub2, true)),  1.0e-12);
133         Assert.assertNull(sub1.intersection(sub2, false));
134     }
135 
136     @Test
137     public void testIntersectionBoundaryOutside() {
138         SubLine sub1 = new SubLine(new Vector2D(1, 1), new Vector2D(2, 1), 1.0e-10);
139         SubLine sub2 = new SubLine(new Vector2D(2, 0), new Vector2D(2, 0.5), 1.0e-10);
140         Assert.assertNull(sub1.intersection(sub2, true));
141         Assert.assertNull(sub1.intersection(sub2, false));
142     }
143 
144     @Test
145     public void testIntersectionOutsideOutside() {
146         SubLine sub1 = new SubLine(new Vector2D(1, 1), new Vector2D(1.5, 1), 1.0e-10);
147         SubLine sub2 = new SubLine(new Vector2D(2, 0), new Vector2D(2, 0.5), 1.0e-10);
148         Assert.assertNull(sub1.intersection(sub2, true));
149         Assert.assertNull(sub1.intersection(sub2, false));
150     }
151 
152     @Test
153     public void testIntersectionParallel() {
154         final SubLine sub1 = new SubLine(new Vector2D(0, 1), new Vector2D(0, 2), 1.0e-10);
155         final SubLine sub2 = new SubLine(new Vector2D(66, 3), new Vector2D(66, 4), 1.0e-10);
156         Assert.assertNull(sub1.intersection(sub2, true));
157         Assert.assertNull(sub1.intersection(sub2, false));
158     }
159 
160 }