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