/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.math4.geometry.euclidean.threed; import org.apache.commons.math4.exception.MathArithmeticException; import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.geometry.euclidean.threed.Line; import org.apache.commons.math4.geometry.euclidean.threed.Cartesian3D; import org.apache.commons.math4.util.FastMath; import org.junit.Assert; import org.junit.Test; public class LineTest { @Test public void testContains() throws MathIllegalArgumentException, MathArithmeticException { Cartesian3D p1 = new Cartesian3D(0, 0, 1); Line l = new Line(p1, new Cartesian3D(0, 0, 2), 1.0e-10); Assert.assertTrue(l.contains(p1)); Assert.assertTrue(l.contains(new Cartesian3D(1.0, p1, 0.3, l.getDirection()))); Cartesian3D u = l.getDirection().orthogonal(); Cartesian3D v = Cartesian3D.crossProduct(l.getDirection(), u); for (double alpha = 0; alpha < 2 * FastMath.PI; alpha += 0.3) { Assert.assertTrue(! l.contains(p1.add(new Cartesian3D(FastMath.cos(alpha), u, FastMath.sin(alpha), v)))); } } @Test public void testSimilar() throws MathIllegalArgumentException, MathArithmeticException { Cartesian3D p1 = new Cartesian3D (1.2, 3.4, -5.8); Cartesian3D p2 = new Cartesian3D (3.4, -5.8, 1.2); Line lA = new Line(p1, p2, 1.0e-10); Line lB = new Line(p2, p1, 1.0e-10); Assert.assertTrue(lA.isSimilarTo(lB)); Assert.assertTrue(! lA.isSimilarTo(new Line(p1, p1.add(lA.getDirection().orthogonal()), 1.0e-10))); } @Test public void testPointDistance() throws MathIllegalArgumentException { Line l = new Line(new Cartesian3D(0, 1, 1), new Cartesian3D(0, 2, 2), 1.0e-10); Assert.assertEquals(FastMath.sqrt(3.0 / 2.0), l.distance(new Cartesian3D(1, 0, 1)), 1.0e-10); Assert.assertEquals(0, l.distance(new Cartesian3D(0, -4, -4)), 1.0e-10); } @Test public void testLineDistance() throws MathIllegalArgumentException { Line l = new Line(new Cartesian3D(0, 1, 1), new Cartesian3D(0, 2, 2), 1.0e-10); Assert.assertEquals(1.0, l.distance(new Line(new Cartesian3D(1, 0, 1), new Cartesian3D(1, 0, 2), 1.0e-10)), 1.0e-10); Assert.assertEquals(0.5, l.distance(new Line(new Cartesian3D(-0.5, 0, 0), new Cartesian3D(-0.5, -1, -1), 1.0e-10)), 1.0e-10); Assert.assertEquals(0.0, l.distance(l), 1.0e-10); Assert.assertEquals(0.0, l.distance(new Line(new Cartesian3D(0, -4, -4), new Cartesian3D(0, -5, -5), 1.0e-10)), 1.0e-10); Assert.assertEquals(0.0, l.distance(new Line(new Cartesian3D(0, -4, -4), new Cartesian3D(0, -3, -4), 1.0e-10)), 1.0e-10); Assert.assertEquals(0.0, l.distance(new Line(new Cartesian3D(0, -4, -4), new Cartesian3D(1, -4, -4), 1.0e-10)), 1.0e-10); Assert.assertEquals(FastMath.sqrt(8), l.distance(new Line(new Cartesian3D(0, -4, 0), new Cartesian3D(1, -4, 0), 1.0e-10)), 1.0e-10); } @Test public void testClosest() throws MathIllegalArgumentException { Line l = new Line(new Cartesian3D(0, 1, 1), new Cartesian3D(0, 2, 2), 1.0e-10); Assert.assertEquals(0.0, l.closestPoint(new Line(new Cartesian3D(1, 0, 1), new Cartesian3D(1, 0, 2), 1.0e-10)).distance(new Cartesian3D(0, 0, 0)), 1.0e-10); Assert.assertEquals(0.5, l.closestPoint(new Line(new Cartesian3D(-0.5, 0, 0), new Cartesian3D(-0.5, -1, -1), 1.0e-10)).distance(new Cartesian3D(-0.5, 0, 0)), 1.0e-10); Assert.assertEquals(0.0, l.closestPoint(l).distance(new Cartesian3D(0, 0, 0)), 1.0e-10); Assert.assertEquals(0.0, l.closestPoint(new Line(new Cartesian3D(0, -4, -4), new Cartesian3D(0, -5, -5), 1.0e-10)).distance(new Cartesian3D(0, 0, 0)), 1.0e-10); Assert.assertEquals(0.0, l.closestPoint(new Line(new Cartesian3D(0, -4, -4), new Cartesian3D(0, -3, -4), 1.0e-10)).distance(new Cartesian3D(0, -4, -4)), 1.0e-10); Assert.assertEquals(0.0, l.closestPoint(new Line(new Cartesian3D(0, -4, -4), new Cartesian3D(1, -4, -4), 1.0e-10)).distance(new Cartesian3D(0, -4, -4)), 1.0e-10); Assert.assertEquals(0.0, l.closestPoint(new Line(new Cartesian3D(0, -4, 0), new Cartesian3D(1, -4, 0), 1.0e-10)).distance(new Cartesian3D(0, -2, -2)), 1.0e-10); } @Test public void testIntersection() throws MathIllegalArgumentException { Line l = new Line(new Cartesian3D(0, 1, 1), new Cartesian3D(0, 2, 2), 1.0e-10); Assert.assertNull(l.intersection(new Line(new Cartesian3D(1, 0, 1), new Cartesian3D(1, 0, 2), 1.0e-10))); Assert.assertNull(l.intersection(new Line(new Cartesian3D(-0.5, 0, 0), new Cartesian3D(-0.5, -1, -1), 1.0e-10))); Assert.assertEquals(0.0, l.intersection(l).distance(new Cartesian3D(0, 0, 0)), 1.0e-10); Assert.assertEquals(0.0, l.intersection(new Line(new Cartesian3D(0, -4, -4), new Cartesian3D(0, -5, -5), 1.0e-10)).distance(new Cartesian3D(0, 0, 0)), 1.0e-10); Assert.assertEquals(0.0, l.intersection(new Line(new Cartesian3D(0, -4, -4), new Cartesian3D(0, -3, -4), 1.0e-10)).distance(new Cartesian3D(0, -4, -4)), 1.0e-10); Assert.assertEquals(0.0, l.intersection(new Line(new Cartesian3D(0, -4, -4), new Cartesian3D(1, -4, -4), 1.0e-10)).distance(new Cartesian3D(0, -4, -4)), 1.0e-10); Assert.assertNull(l.intersection(new Line(new Cartesian3D(0, -4, 0), new Cartesian3D(1, -4, 0), 1.0e-10))); } @Test public void testRevert() { // setup Line line = new Line(new Cartesian3D(1653345.6696423641, 6170370.041579291, 90000), new Cartesian3D(1650757.5050732433, 6160710.879908984, 0.9), 1.0e-10); Cartesian3D expected = line.getDirection().negate(); // action Line reverted = line.revert(); // verify Assert.assertArrayEquals(expected.toArray(), reverted.getDirection().toArray(), 0); } }