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 org.hipparchus.exception.MathIllegalArgumentException;
25  import org.hipparchus.exception.MathRuntimeException;
26  import org.hipparchus.util.FastMath;
27  import org.junit.jupiter.api.Test;
28  
29  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
30  import static org.junit.jupiter.api.Assertions.assertEquals;
31  import static org.junit.jupiter.api.Assertions.assertFalse;
32  import static org.junit.jupiter.api.Assertions.assertNull;
33  import static org.junit.jupiter.api.Assertions.assertTrue;
34  
35  class LineTest {
36  
37      @Test
38      void testContains() throws MathIllegalArgumentException, MathRuntimeException {
39          Vector3D p1 = new Vector3D(0, 0, 1);
40          Line l = new Line(p1, new Vector3D(0, 0, 2), 1.0e-10);
41          assertTrue(l.contains(p1));
42          assertTrue(l.contains(new Vector3D(1.0, p1, 0.3, l.getDirection())));
43          Vector3D u = l.getDirection().orthogonal();
44          Vector3D v = Vector3D.crossProduct(l.getDirection(), u);
45          for (double alpha = 0; alpha < 2 * FastMath.PI; alpha += 0.3) {
46              assertFalse(l.contains(p1.add(new Vector3D(FastMath.cos(alpha), u,
47                  FastMath.sin(alpha), v))));
48          }
49      }
50  
51      @Test
52      void testSimilar() throws MathIllegalArgumentException, MathRuntimeException {
53          Vector3D p1  = new Vector3D (1.2, 3.4, -5.8);
54          Vector3D p2  = new Vector3D (3.4, -5.8, 1.2);
55          Line     lA  = new Line(p1, p2, 1.0e-10);
56          Line     lB  = new Line(p2, p1, 1.0e-10);
57          assertTrue(lA.isSimilarTo(lB));
58          assertFalse(lA.isSimilarTo(new Line(p1, p1.add(lA.getDirection().orthogonal()), 1.0e-10)));
59      }
60  
61      @Test
62      void testPointDistance() throws MathIllegalArgumentException {
63          Line l = new Line(new Vector3D(0, 1, 1), new Vector3D(0, 2, 2), 1.0e-10);
64          assertEquals(FastMath.sqrt(3.0 / 2.0), l.distance(new Vector3D(1, 0, 1)), 1.0e-10);
65          assertEquals(0, l.distance(new Vector3D(0, -4, -4)), 1.0e-10);
66      }
67  
68      @Test
69      void testLineDistance() throws MathIllegalArgumentException {
70          Line l = new Line(new Vector3D(0, 1, 1), new Vector3D(0, 2, 2), 1.0e-10);
71          assertEquals(1.0,
72                              l.distance(new Line(new Vector3D(1, 0, 1), new Vector3D(1, 0, 2), 1.0e-10)),
73                              1.0e-10);
74          assertEquals(0.5,
75                              l.distance(new Line(new Vector3D(-0.5, 0, 0), new Vector3D(-0.5, -1, -1), 1.0e-10)),
76                              1.0e-10);
77          assertEquals(0.0,
78                              l.distance(l),
79                              1.0e-10);
80          assertEquals(0.0,
81                              l.distance(new Line(new Vector3D(0, -4, -4), new Vector3D(0, -5, -5), 1.0e-10)),
82                              1.0e-10);
83          assertEquals(0.0,
84                              l.distance(new Line(new Vector3D(0, -4, -4), new Vector3D(0, -3, -4), 1.0e-10)),
85                              1.0e-10);
86          assertEquals(0.0,
87                              l.distance(new Line(new Vector3D(0, -4, -4), new Vector3D(1, -4, -4), 1.0e-10)),
88                              1.0e-10);
89          assertEquals(FastMath.sqrt(8),
90                              l.distance(new Line(new Vector3D(0, -4, 0), new Vector3D(1, -4, 0), 1.0e-10)),
91                              1.0e-10);
92      }
93  
94      @Test
95      void testClosest() throws MathIllegalArgumentException {
96          Line l = new Line(new Vector3D(0, 1, 1), new Vector3D(0, 2, 2), 1.0e-10);
97          assertEquals(0.0,
98                              l.closestPoint(new Line(new Vector3D(1, 0, 1), new Vector3D(1, 0, 2), 1.0e-10)).distance(new Vector3D(0, 0, 0)),
99                              1.0e-10);
100         assertEquals(0.5,
101                             l.closestPoint(new Line(new Vector3D(-0.5, 0, 0), new Vector3D(-0.5, -1, -1), 1.0e-10)).distance(new Vector3D(-0.5, 0, 0)),
102                             1.0e-10);
103         assertEquals(0.0,
104                             l.closestPoint(l).distance(new Vector3D(0, 0, 0)),
105                             1.0e-10);
106         assertEquals(0.0,
107                             l.closestPoint(new Line(new Vector3D(0, -4, -4), new Vector3D(0, -5, -5), 1.0e-10)).distance(new Vector3D(0, 0, 0)),
108                             1.0e-10);
109         assertEquals(0.0,
110                             l.closestPoint(new Line(new Vector3D(0, -4, -4), new Vector3D(0, -3, -4), 1.0e-10)).distance(new Vector3D(0, -4, -4)),
111                             1.0e-10);
112         assertEquals(0.0,
113                             l.closestPoint(new Line(new Vector3D(0, -4, -4), new Vector3D(1, -4, -4), 1.0e-10)).distance(new Vector3D(0, -4, -4)),
114                             1.0e-10);
115         assertEquals(0.0,
116                             l.closestPoint(new Line(new Vector3D(0, -4, 0), new Vector3D(1, -4, 0), 1.0e-10)).distance(new Vector3D(0, -2, -2)),
117                             1.0e-10);
118     }
119 
120     @Test
121     void testIntersection() throws MathIllegalArgumentException {
122         Line l = new Line(new Vector3D(0, 1, 1), new Vector3D(0, 2, 2), 1.0e-10);
123         assertNull(l.intersection(new Line(new Vector3D(1, 0, 1), new Vector3D(1, 0, 2), 1.0e-10)));
124         assertNull(l.intersection(new Line(new Vector3D(-0.5, 0, 0), new Vector3D(-0.5, -1, -1), 1.0e-10)));
125         assertEquals(0.0,
126                             l.intersection(l).distance(new Vector3D(0, 0, 0)),
127                             1.0e-10);
128         assertEquals(0.0,
129                             l.intersection(new Line(new Vector3D(0, -4, -4), new Vector3D(0, -5, -5), 1.0e-10)).distance(new Vector3D(0, 0, 0)),
130                             1.0e-10);
131         assertEquals(0.0,
132                             l.intersection(new Line(new Vector3D(0, -4, -4), new Vector3D(0, -3, -4), 1.0e-10)).distance(new Vector3D(0, -4, -4)),
133                             1.0e-10);
134         assertEquals(0.0,
135                             l.intersection(new Line(new Vector3D(0, -4, -4), new Vector3D(1, -4, -4), 1.0e-10)).distance(new Vector3D(0, -4, -4)),
136                             1.0e-10);
137         assertNull(l.intersection(new Line(new Vector3D(0, -4, 0), new Vector3D(1, -4, 0), 1.0e-10)));
138     }
139 
140     @Test
141     void testRevert() {
142 
143         // setup
144         Line line = new Line(new Vector3D(1653345.6696423641, 6170370.041579291, 90000),
145                              new Vector3D(1650757.5050732433, 6160710.879908984, 0.9),
146                              1.0e-10);
147         Vector3D expected = line.getDirection().negate();
148 
149         // action
150         Line reverted = line.revert();
151 
152         // verify
153         assertArrayEquals(expected.toArray(), reverted.getDirection().toArray(), 0);
154 
155     }
156 
157     /** Test for issue #78. */
158     @Test
159     void testCancellation() {
160          // setup
161         // point
162         Vector3D p = new Vector3D(1e16, 1e16, 1e16);
163         // unit vector
164         Vector3D u = new Vector3D(-1, -1, -1);
165 
166         // action
167         Line line = Line.fromDirection(p, u, 0);
168 
169         // verify
170         double ulp = FastMath.ulp(p.getNorm());
171         assertArrayEquals(line.getOrigin().toArray(), Vector3D.ZERO.toArray(), ulp);
172         assertArrayEquals(line.getDirection().toArray(), u.normalize().toArray(), 0);
173         assertEquals(0, line.getTolerance(), 0);
174     }
175 
176 }