|
Java example source code file (PolyhedronsSetTest.java)
The PolyhedronsSetTest.java Java example source code/* * 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.math3.geometry.euclidean.threed; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.lang.reflect.Field; import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.ExceptionContext; import org.apache.commons.math3.exception.util.Localizable; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.geometry.Vector; import org.apache.commons.math3.geometry.euclidean.twod.Euclidean2D; import org.apache.commons.math3.geometry.euclidean.twod.PolygonsSet; import org.apache.commons.math3.geometry.euclidean.twod.SubLine; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; import org.apache.commons.math3.geometry.partitioning.BSPTree; import org.apache.commons.math3.geometry.partitioning.BSPTreeVisitor; import org.apache.commons.math3.geometry.partitioning.BoundaryAttribute; import org.apache.commons.math3.geometry.partitioning.Region; import org.apache.commons.math3.geometry.partitioning.RegionDumper; import org.apache.commons.math3.geometry.partitioning.RegionFactory; import org.apache.commons.math3.geometry.partitioning.RegionParser; import org.apache.commons.math3.geometry.partitioning.SubHyperplane; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well1024a; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class PolyhedronsSetTest { @Test public void testBox() { PolyhedronsSet tree = new PolyhedronsSet(0, 1, 0, 1, 0, 1, 1.0e-10); Assert.assertEquals(1.0, tree.getSize(), 1.0e-10); Assert.assertEquals(6.0, tree.getBoundarySize(), 1.0e-10); Vector3D barycenter = (Vector3D) tree.getBarycenter(); Assert.assertEquals(0.5, barycenter.getX(), 1.0e-10); Assert.assertEquals(0.5, barycenter.getY(), 1.0e-10); Assert.assertEquals(0.5, barycenter.getZ(), 1.0e-10); for (double x = -0.25; x < 1.25; x += 0.1) { boolean xOK = (x >= 0.0) && (x <= 1.0); for (double y = -0.25; y < 1.25; y += 0.1) { boolean yOK = (y >= 0.0) && (y <= 1.0); for (double z = -0.25; z < 1.25; z += 0.1) { boolean zOK = (z >= 0.0) && (z <= 1.0); Region.Location expected = (xOK && yOK && zOK) ? Region.Location.INSIDE : Region.Location.OUTSIDE; Assert.assertEquals(expected, tree.checkPoint(new Vector3D(x, y, z))); } } } checkPoints(Region.Location.BOUNDARY, tree, new Vector3D[] { new Vector3D(0.0, 0.5, 0.5), new Vector3D(1.0, 0.5, 0.5), new Vector3D(0.5, 0.0, 0.5), new Vector3D(0.5, 1.0, 0.5), new Vector3D(0.5, 0.5, 0.0), new Vector3D(0.5, 0.5, 1.0) }); checkPoints(Region.Location.OUTSIDE, tree, new Vector3D[] { new Vector3D(0.0, 1.2, 1.2), new Vector3D(1.0, 1.2, 1.2), new Vector3D(1.2, 0.0, 1.2), new Vector3D(1.2, 1.0, 1.2), new Vector3D(1.2, 1.2, 0.0), new Vector3D(1.2, 1.2, 1.0) }); } @Test public void testTetrahedron() throws MathArithmeticException { Vector3D vertex1 = new Vector3D(1, 2, 3); Vector3D vertex2 = new Vector3D(2, 2, 4); Vector3D vertex3 = new Vector3D(2, 3, 3); Vector3D vertex4 = new Vector3D(1, 3, 4); @SuppressWarnings("unchecked") PolyhedronsSet tree = (PolyhedronsSet) new RegionFactory<Euclidean3D>().buildConvex( new Plane(vertex3, vertex2, vertex1, 1.0e-10), new Plane(vertex2, vertex3, vertex4, 1.0e-10), new Plane(vertex4, vertex3, vertex1, 1.0e-10), new Plane(vertex1, vertex2, vertex4, 1.0e-10)); Assert.assertEquals(1.0 / 3.0, tree.getSize(), 1.0e-10); Assert.assertEquals(2.0 * FastMath.sqrt(3.0), tree.getBoundarySize(), 1.0e-10); Vector3D barycenter = (Vector3D) tree.getBarycenter(); Assert.assertEquals(1.5, barycenter.getX(), 1.0e-10); Assert.assertEquals(2.5, barycenter.getY(), 1.0e-10); Assert.assertEquals(3.5, barycenter.getZ(), 1.0e-10); double third = 1.0 / 3.0; checkPoints(Region.Location.BOUNDARY, tree, new Vector3D[] { vertex1, vertex2, vertex3, vertex4, new Vector3D(third, vertex1, third, vertex2, third, vertex3), new Vector3D(third, vertex2, third, vertex3, third, vertex4), new Vector3D(third, vertex3, third, vertex4, third, vertex1), new Vector3D(third, vertex4, third, vertex1, third, vertex2) }); checkPoints(Region.Location.OUTSIDE, tree, new Vector3D[] { new Vector3D(1, 2, 4), new Vector3D(2, 2, 3), new Vector3D(2, 3, 4), new Vector3D(1, 3, 3) }); } @Test public void testIsometry() throws MathArithmeticException, MathIllegalArgumentException { Vector3D vertex1 = new Vector3D(1.1, 2.2, 3.3); Vector3D vertex2 = new Vector3D(2.0, 2.4, 4.2); Vector3D vertex3 = new Vector3D(2.8, 3.3, 3.7); Vector3D vertex4 = new Vector3D(1.0, 3.6, 4.5); @SuppressWarnings("unchecked") PolyhedronsSet tree = (PolyhedronsSet) new RegionFactory<Euclidean3D>().buildConvex( new Plane(vertex3, vertex2, vertex1, 1.0e-10), new Plane(vertex2, vertex3, vertex4, 1.0e-10), new Plane(vertex4, vertex3, vertex1, 1.0e-10), new Plane(vertex1, vertex2, vertex4, 1.0e-10)); Vector3D barycenter = (Vector3D) tree.getBarycenter(); Vector3D s = new Vector3D(10.2, 4.3, -6.7); Vector3D c = new Vector3D(-0.2, 2.1, -3.2); Rotation r = new Rotation(new Vector3D(6.2, -4.4, 2.1), 0.12, RotationConvention.VECTOR_OPERATOR); tree = tree.rotate(c, r).translate(s); Vector3D newB = new Vector3D(1.0, s, 1.0, c, 1.0, r.applyTo(barycenter.subtract(c))); Assert.assertEquals(0.0, newB.subtract((Vector<Euclidean3D>) tree.getBarycenter()).getNorm(), 1.0e-10); final Vector3D[] expectedV = new Vector3D[] { new Vector3D(1.0, s, 1.0, c, 1.0, r.applyTo(vertex1.subtract(c))), new Vector3D(1.0, s, 1.0, c, 1.0, r.applyTo(vertex2.subtract(c))), new Vector3D(1.0, s, 1.0, c, 1.0, r.applyTo(vertex3.subtract(c))), new Vector3D(1.0, s, 1.0, c, 1.0, r.applyTo(vertex4.subtract(c))) }; tree.getTree(true).visit(new BSPTreeVisitor<Euclidean3D>() { public Order visitOrder(BSPTree<Euclidean3D> node) { return Order.MINUS_SUB_PLUS; } public void visitInternalNode(BSPTree<Euclidean3D> node) { @SuppressWarnings("unchecked") BoundaryAttribute<Euclidean3D> attribute = (BoundaryAttribute<Euclidean3D>) node.getAttribute(); if (attribute.getPlusOutside() != null) { checkFacet((SubPlane) attribute.getPlusOutside()); } if (attribute.getPlusInside() != null) { checkFacet((SubPlane) attribute.getPlusInside()); } } public void visitLeafNode(BSPTree<Euclidean3D> node) { } private void checkFacet(SubPlane facet) { Plane plane = (Plane) facet.getHyperplane(); Vector2D[][] vertices = ((PolygonsSet) facet.getRemainingRegion()).getVertices(); Assert.assertEquals(1, vertices.length); for (int i = 0; i < vertices[0].length; ++i) { Vector3D v = plane.toSpace(vertices[0][i]); double d = Double.POSITIVE_INFINITY; for (int k = 0; k < expectedV.length; ++k) { d = FastMath.min(d, v.subtract(expectedV[k]).getNorm()); } Assert.assertEquals(0, d, 1.0e-10); } } }); } @Test public void testBuildBox() { double x = 1.0; double y = 2.0; double z = 3.0; double w = 0.1; double l = 1.0; PolyhedronsSet tree = new PolyhedronsSet(x - l, x + l, y - w, y + w, z - w, z + w, 1.0e-10); Vector3D barycenter = (Vector3D) tree.getBarycenter(); Assert.assertEquals(x, barycenter.getX(), 1.0e-10); Assert.assertEquals(y, barycenter.getY(), 1.0e-10); Assert.assertEquals(z, barycenter.getZ(), 1.0e-10); Assert.assertEquals(8 * l * w * w, tree.getSize(), 1.0e-10); Assert.assertEquals(8 * w * (2 * l + w), tree.getBoundarySize(), 1.0e-10); } @Test public void testCross() { double x = 1.0; double y = 2.0; double z = 3.0; double w = 0.1; double l = 1.0; PolyhedronsSet xBeam = new PolyhedronsSet(x - l, x + l, y - w, y + w, z - w, z + w, 1.0e-10); PolyhedronsSet yBeam = new PolyhedronsSet(x - w, x + w, y - l, y + l, z - w, z + w, 1.0e-10); PolyhedronsSet zBeam = new PolyhedronsSet(x - w, x + w, y - w, y + w, z - l, z + l, 1.0e-10); RegionFactory<Euclidean3D> factory = new RegionFactory Other Java examples (source code examples)Here is a short list of links related to this Java PolyhedronsSetTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.