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