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