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.Binary64;
27  import org.hipparchus.util.Binary64Field;
28  import org.hipparchus.util.FastMath;
29  import org.junit.jupiter.api.Test;
30  
31  import static org.junit.jupiter.api.Assertions.assertEquals;
32  import static org.junit.jupiter.api.Assertions.assertFalse;
33  import static org.junit.jupiter.api.Assertions.assertNull;
34  import static org.junit.jupiter.api.Assertions.assertTrue;
35  
36  class FieldLineTest {
37  
38      @Test
39      void testContains() throws MathIllegalArgumentException, MathRuntimeException {
40          FieldVector3D<Binary64> p1 = FieldVector3D.getPlusK(Binary64Field.getInstance());
41          FieldLine<Binary64> l = new FieldLine<>(p1,
42                                                   new FieldVector3D<>(new Binary64(0),
43                                                                       new Binary64(0),
44                                                                       new Binary64(2)),
45                                                   1.0e-10);
46          assertTrue(l.contains(p1));
47          assertTrue(l.contains(new FieldVector3D<>(1.0, p1, 0.3, l.getDirection())));
48          assertTrue(l.contains(new Vector3D(1.0, p1.toVector3D(), 0.3, l.getDirection().toVector3D())));
49          FieldVector3D<Binary64> u = l.getDirection().orthogonal();
50          FieldVector3D<Binary64> v = FieldVector3D.crossProduct(l.getDirection(), u);
51          for (double a = 0; a < 2 * FastMath.PI; a += 0.3) {
52              Binary64 alpha = new Binary64(a);
53              assertFalse(l.contains(p1.add(new FieldVector3D<>(alpha.cos(), u, alpha.sin(), v))));
54          }
55      }
56  
57      @Test
58      void testSimilar() throws MathIllegalArgumentException, MathRuntimeException {
59          FieldVector3D<Binary64> p1  = createVector(1.2, 3.4, -5.8);
60          FieldVector3D<Binary64> p2  = createVector(3.4, -5.8, 1.2);
61          FieldLine<Binary64>     lA  = new FieldLine<>(p1, p2, 1.0e-10);
62          FieldLine<Binary64>     lB  = new FieldLine<>(p2, p1, 1.0e-10);
63          assertTrue(lA.isSimilarTo(lB));
64          assertFalse(lA.isSimilarTo(new FieldLine<>(p1, p1.add(lA.getDirection().orthogonal()), 1.0e-10)));
65      }
66  
67      @Test
68      void testPointDistance() throws MathIllegalArgumentException {
69          FieldLine<Binary64> l = new FieldLine<>(createVector(0, 1, 1),
70                                                   createVector(0, 2, 2),
71                                                   1.0e-10);
72          assertEquals(FastMath.sqrt(3.0 / 2.0),
73                              l.distance(createVector(1, 0, 1)).getReal(),
74                              1.0e-10);
75          assertEquals(0,
76                              l.distance(createVector(0, -4, -4)).getReal(),
77                              1.0e-10);
78          assertEquals(1.0e-10, l.getTolerance(), 1.0e-20);
79          assertEquals(0.0, l.getAbscissa(Vector3D.ZERO).getReal(), 1.0e-20);
80          assertEquals(0.0, l.getAbscissa(FieldVector3D.getZero(Binary64Field.getInstance())).getReal(), 1.0e-20);
81          assertEquals(0.0, l.getOrigin().getX().getReal(), 1.0e-20);
82          assertEquals(0.0, l.getOrigin().getY().getReal(), 1.0e-20);
83          assertEquals(0.0, l.getOrigin().getZ().getReal(), 1.0e-20);
84          assertEquals(0.0, l.pointAt(new Binary64(1.0)).toVector3D().getX(), 1.0e-20);
85          assertEquals(0.5 * FastMath.sqrt(2), l.pointAt(new Binary64(1.0)).toVector3D().getY(), 1.0e-15);
86          assertEquals(0.5 * FastMath.sqrt(2), l.pointAt(new Binary64(1.0)).toVector3D().getZ(), 1.0e-15);
87          assertEquals(0.0, l.pointAt(1.0).toVector3D().getX(), 1.0e-20);
88          assertEquals(0.5 * FastMath.sqrt(2), l.pointAt(1.0).toVector3D().getY(), 1.0e-15);
89          assertEquals(0.5 * FastMath.sqrt(2), l.pointAt(1.0).toVector3D().getZ(), 1.0e-15);
90      }
91  
92      @Test
93      void testLineDistance() throws MathIllegalArgumentException {
94          FieldLine<Binary64> l = new FieldLine<>(createVector(0, 1, 1),
95                                                   createVector(0, 2, 2),
96                                                   1.0e-10);
97          assertEquals(1.0,
98                              l.distance(new FieldLine<>(createVector(1, 0, 1),
99                                                         createVector(1, 0, 2),
100                                                        1.0e-10)).getReal(),
101                             1.0e-10);
102         assertEquals(0.5,
103                             l.distance(new FieldLine<>(createVector(-0.5, 0, 0),
104                                                        createVector(-0.5, -1, -1),
105                                                        1.0e-10)).getReal(),
106                             1.0e-10);
107         assertEquals(0.0,
108                             l.distance(l).getReal(),
109                             1.0e-10);
110         assertEquals(0.0,
111                             l.distance(new FieldLine<>(createVector(0, -4, -4),
112                                                        createVector(0, -5, -5),
113                                                        1.0e-10)).getReal(),
114                             1.0e-10);
115         assertEquals(0.0,
116                             l.distance(new FieldLine<>(createVector(0, -4, -4),
117                                                        createVector(0, -3, -4),
118                                                        1.0e-10)).getReal(),
119                             1.0e-10);
120         assertEquals(0.0,
121                             l.distance(new FieldLine<>(createVector(0, -4, -4),
122                                                        createVector(1, -4, -4),
123                                                        1.0e-10)).getReal(),
124                             1.0e-10);
125         assertEquals(FastMath.sqrt(8),
126                             l.distance(new FieldLine<>(createVector(0, -4, 0),
127                                                        createVector(1, -4, 0),
128                                                        1.0e-10)).getReal(),
129                             1.0e-10);
130     }
131 
132     @Test
133     void testClosest() throws MathIllegalArgumentException {
134         FieldLine<Binary64> l = new FieldLine<>(createVector(0, 1, 1),
135                                                  createVector(0, 2, 2),
136                                                  1.0e-10);
137         assertEquals(0.0,
138                             l.closestPoint(new FieldLine<>(createVector(1, 0, 1),
139                                                            createVector(1, 0, 2),
140                                                            1.0e-10)).distance(createVector(0, 0, 0)).getReal(),
141                             1.0e-10);
142         assertEquals(0.5,
143                             l.closestPoint(new FieldLine<>(createVector(-0.5, 0, 0),
144                                                            createVector(-0.5, -1, -1),
145                                                            1.0e-10)).distance(createVector(-0.5, 0, 0)).getReal(),
146                             1.0e-10);
147         assertEquals(0.0,
148                             l.closestPoint(l).distance(createVector(0, 0, 0)).getReal(),
149                             1.0e-10);
150         assertEquals(0.0,
151                             l.closestPoint(new FieldLine<>(createVector(0, -4, -4),
152                                                            createVector(0, -5, -5),
153                                                            1.0e-10)).distance(createVector(0, 0, 0)).getReal(),
154                             1.0e-10);
155         assertEquals(0.0,
156                             l.closestPoint(new FieldLine<>(createVector(0, -4, -4),
157                                                            createVector(0, -3, -4),
158                                                            1.0e-10)).distance(createVector(0, -4, -4)).getReal(),
159                             1.0e-10);
160         assertEquals(0.0,
161                             l.closestPoint(new FieldLine<>(createVector(0, -4, -4),
162                                                            createVector(1, -4, -4),
163                                                            1.0e-10)).distance(createVector(0, -4, -4)).getReal(),
164                             1.0e-10);
165         assertEquals(0.0,
166                             l.closestPoint(new FieldLine<>(createVector(0, -4, 0),
167                                                            createVector(1, -4, 0),
168                                                            1.0e-10)).distance(createVector(0, -2, -2)).getReal(),
169                             1.0e-10);
170     }
171 
172     @Test
173     void testIntersection() throws MathIllegalArgumentException {
174         FieldLine<Binary64> l = new FieldLine<>(createVector(0, 1, 1), createVector(0, 2, 2), 1.0e-10);
175         assertNull(l.intersection(new FieldLine<>(createVector(1, 0, 1), createVector(1, 0, 2), 1.0e-10)));
176         assertNull(l.intersection(new FieldLine<>(createVector(-0.5, 0, 0), createVector(-0.5, -1, -1), 1.0e-10)));
177         assertEquals(0.0,
178                             l.intersection(l).distance(createVector(0, 0, 0)).getReal(),
179                             1.0e-10);
180         assertEquals(0.0,
181                             l.intersection(new FieldLine<>(createVector(0, -4, -4), createVector(0, -5, -5), 1.0e-10)).distance(createVector(0, 0, 0)).getReal(),
182                             1.0e-10);
183         assertEquals(0.0,
184                             l.intersection(new FieldLine<>(createVector(0, -4, -4), createVector(0, -3, -4), 1.0e-10)).distance(createVector(0, -4, -4)).getReal(),
185                             1.0e-10);
186         assertEquals(0.0,
187                             l.intersection(new FieldLine<>(createVector(0, -4, -4), createVector(1, -4, -4), 1.0e-10)).distance(createVector(0, -4, -4)).getReal(),
188                             1.0e-10);
189         assertNull(l.intersection(new FieldLine<>(createVector(0, -4, 0), createVector(1, -4, 0), 1.0e-10)));
190     }
191 
192     @Test
193     void testRevert() {
194 
195         // setup
196         FieldLine<Binary64> line = new FieldLine<>(createVector(1653345.6696423641, 6170370.041579291, 90000),
197                                                     createVector(1650757.5050732433, 6160710.879908984, 0.9),
198                                                     1.0e-10);
199         FieldVector3D<Binary64> expected = line.getDirection().negate();
200 
201         // action
202         FieldLine<Binary64> reverted = line.revert();
203 
204         // verify
205         Binary64[] e = expected.toArray();
206         Binary64[] r = reverted.getDirection().toArray();
207         assertEquals(e.length, e.length);
208         for (int i = 0; i < e.length; ++i) {
209             assertEquals(e[i].getReal(), r[i].getReal(), 1.0e-10);
210         }
211 
212     }
213 
214     private FieldVector3D<Binary64> createVector(double x, double y, double z) {
215         return new FieldVector3D<>(new Binary64(x), new Binary64(y), new Binary64(z));
216     }
217 
218 }