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