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.junit.Assert;
27 import org.junit.Test;
28
29 public class PlaneTest {
30
31 @Test
32 public void testContains() throws MathRuntimeException {
33 Plane p = new Plane(new Vector3D(0, 0, 1), new Vector3D(0, 0, 1), 1.0e-10);
34 Assert.assertTrue(p.contains(new Vector3D(0, 0, 1)));
35 Assert.assertTrue(p.contains(new Vector3D(17, -32, 1)));
36 Assert.assertTrue(! p.contains(new Vector3D(17, -32, 1.001)));
37 }
38
39 @Test
40 public void testOffset() throws MathRuntimeException {
41 Vector3D p1 = new Vector3D(1, 1, 1);
42 Plane p = new Plane(p1, new Vector3D(0.2, 0, 0), 1.0e-10);
43 Assert.assertEquals(-5.0, p.getOffset(new Vector3D(-4, 0, 0)), 1.0e-10);
44 Assert.assertEquals(+5.0, p.getOffset(new Vector3D(6, 10, -12)), 1.0e-10);
45 Assert.assertEquals(0.3,
46 p.getOffset(new Vector3D(1.0, p1, 0.3, p.getNormal())),
47 1.0e-10);
48 Assert.assertEquals(-0.3,
49 p.getOffset(new Vector3D(1.0, p1, -0.3, p.getNormal())),
50 1.0e-10);
51 }
52
53 @Test
54 public void testPoint() throws MathRuntimeException {
55 Plane p = new Plane(new Vector3D(2, -3, 1), new Vector3D(1, 4, 9), 1.0e-10);
56 Assert.assertTrue(p.contains(p.getOrigin()));
57 }
58
59 @Test
60 public void testThreePoints() throws MathRuntimeException {
61 Vector3D p1 = new Vector3D(1.2, 3.4, -5.8);
62 Vector3D p2 = new Vector3D(3.4, -5.8, 1.2);
63 Vector3D p3 = new Vector3D(-2.0, 4.3, 0.7);
64 Plane p = new Plane(p1, p2, p3, 1.0e-10);
65 Assert.assertTrue(p.contains(p1));
66 Assert.assertTrue(p.contains(p2));
67 Assert.assertTrue(p.contains(p3));
68 }
69
70 @Test
71 public void testRotate() throws MathRuntimeException, MathIllegalArgumentException {
72 Vector3D p1 = new Vector3D(1.2, 3.4, -5.8);
73 Vector3D p2 = new Vector3D(3.4, -5.8, 1.2);
74 Vector3D p3 = new Vector3D(-2.0, 4.3, 0.7);
75 Plane p = new Plane(p1, p2, p3, 1.0e-10);
76 Vector3D oldNormal = p.getNormal();
77
78 p = p.rotate(p2, new Rotation(p2.subtract(p1), 1.7, RotationConvention.VECTOR_OPERATOR));
79 Assert.assertTrue(p.contains(p1));
80 Assert.assertTrue(p.contains(p2));
81 Assert.assertTrue(! p.contains(p3));
82
83 p = p.rotate(p2, new Rotation(oldNormal, 0.1, RotationConvention.VECTOR_OPERATOR));
84 Assert.assertTrue(! p.contains(p1));
85 Assert.assertTrue(p.contains(p2));
86 Assert.assertTrue(! p.contains(p3));
87
88 p = p.rotate(p1, new Rotation(oldNormal, 0.1, RotationConvention.VECTOR_OPERATOR));
89 Assert.assertTrue(! p.contains(p1));
90 Assert.assertTrue(! p.contains(p2));
91 Assert.assertTrue(! p.contains(p3));
92
93 }
94
95 @Test
96 public void testTranslate() throws MathRuntimeException {
97 Vector3D p1 = new Vector3D(1.2, 3.4, -5.8);
98 Vector3D p2 = new Vector3D(3.4, -5.8, 1.2);
99 Vector3D p3 = new Vector3D(-2.0, 4.3, 0.7);
100 Plane p = new Plane(p1, p2, p3, 1.0e-10);
101
102 p = p.translate(new Vector3D(2.0, p.getU(), -1.5, p.getV()));
103 Assert.assertTrue(p.contains(p1));
104 Assert.assertTrue(p.contains(p2));
105 Assert.assertTrue(p.contains(p3));
106
107 p = p.translate(new Vector3D(-1.2, p.getNormal()));
108 Assert.assertTrue(! p.contains(p1));
109 Assert.assertTrue(! p.contains(p2));
110 Assert.assertTrue(! p.contains(p3));
111
112 p = p.translate(new Vector3D(+1.2, p.getNormal()));
113 Assert.assertTrue(p.contains(p1));
114 Assert.assertTrue(p.contains(p2));
115 Assert.assertTrue(p.contains(p3));
116
117 }
118
119 @Test
120 public void testIntersection() throws MathRuntimeException, MathIllegalArgumentException {
121 Plane p = new Plane(new Vector3D(1, 2, 3), new Vector3D(-4, 1, -5), 1.0e-10);
122 Line l = new Line(new Vector3D(0.2, -3.5, 0.7), new Vector3D(1.2, -2.5, -0.3), 1.0e-10);
123 Vector3D point = p.intersection(l);
124 Assert.assertTrue(p.contains(point));
125 Assert.assertTrue(l.contains(point));
126 Assert.assertNull(p.intersection(new Line(new Vector3D(10, 10, 10),
127 new Vector3D(10, 10, 10).add(p.getNormal().orthogonal()),
128 1.0e-10)));
129 }
130
131 @Test
132 public void testIntersection2() throws MathRuntimeException {
133 Vector3D p1 = new Vector3D (1.2, 3.4, -5.8);
134 Vector3D p2 = new Vector3D (3.4, -5.8, 1.2);
135 Plane pA = new Plane(p1, p2, new Vector3D (-2.0, 4.3, 0.7), 1.0e-10);
136 Plane pB = new Plane(p1, new Vector3D (11.4, -3.8, 5.1), p2, 1.0e-10);
137 Line l = pA.intersection(pB);
138 Assert.assertTrue(l.contains(p1));
139 Assert.assertTrue(l.contains(p2));
140 Assert.assertNull(pA.intersection(pA));
141 }
142
143 @Test
144 public void testIntersection3() throws MathRuntimeException {
145 Vector3D reference = new Vector3D (1.2, 3.4, -5.8);
146 Plane p1 = new Plane(reference, new Vector3D(1, 3, 3), 1.0e-10);
147 Plane p2 = new Plane(reference, new Vector3D(-2, 4, 0), 1.0e-10);
148 Plane p3 = new Plane(reference, new Vector3D(7, 0, -4), 1.0e-10);
149 Vector3D p = Plane.intersection(p1, p2, p3);
150 Assert.assertEquals(reference.getX(), p.getX(), 1.0e-10);
151 Assert.assertEquals(reference.getY(), p.getY(), 1.0e-10);
152 Assert.assertEquals(reference.getZ(), p.getZ(), 1.0e-10);
153 }
154
155 @Test
156 public void testSimilar() throws MathRuntimeException {
157 Vector3D p1 = new Vector3D (1.2, 3.4, -5.8);
158 Vector3D p2 = new Vector3D (3.4, -5.8, 1.2);
159 Vector3D p3 = new Vector3D (-2.0, 4.3, 0.7);
160 Plane pA = new Plane(p1, p2, p3, 1.0e-10);
161 Plane pB = new Plane(p1, new Vector3D (11.4, -3.8, 5.1), p2, 1.0e-10);
162 Assert.assertTrue(! pA.isSimilarTo(pB));
163 Assert.assertTrue(pA.isSimilarTo(pA));
164 Assert.assertTrue(pA.isSimilarTo(new Plane(p1, p3, p2, 1.0e-10)));
165 Vector3D shift = new Vector3D(0.3, pA.getNormal());
166 Assert.assertTrue(! pA.isSimilarTo(new Plane(p1.add(shift),
167 p3.add(shift),
168 p2.add(shift),
169 1.0e-10)));
170 }
171
172 }