A complete generic implementation of Binary Space Partitioning Trees (BSP trees)
has been added. A few specializations of this implementation are also provided for 1D, 2D and 3D Euclidean geometry. This allows support for arbitrary intervals sets (1D), polygons sets (2D) and polyhedrons sets (3D) with all sets operations (union, intersection, symmetric difference, difference, complement), with predicates (point inside/outside/on boundary, emptiness, other region contained), with geometrical computation (barycenter, size, boundary size) and with conversions from and to boundary representation. JIRA: MATH-576 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1103438 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e401d4ac06
commit
03fa7c1431
|
@ -65,7 +65,7 @@
|
|||
|
||||
<!-- The following equality test is intentional and needed for semantic purposes -->
|
||||
<Match>
|
||||
<Class name="org.apache.commons.math.geometry.Vector3D" />
|
||||
<Class name="org.apache.commons.math.geometry.euclidean.threeD.Vector3D" />
|
||||
<Method name="equals" params="java.lang.Object" returns="boolean" />
|
||||
<Bug pattern="FE_FLOATING_POINT_EQUALITY" />
|
||||
</Match>
|
||||
|
|
|
@ -77,6 +77,7 @@ public enum LocalizedFormats implements Localizable {
|
|||
CONTRACTION_CRITERIA_SMALLER_THAN_EXPANSION_FACTOR("contraction criteria ({0}) smaller than the expansion factor ({1}). This would lead to a never ending loop of expansion and contraction as a newly expanded internal storage array would immediately satisfy the criteria for contraction."),
|
||||
CONTRACTION_CRITERIA_SMALLER_THAN_ONE("contraction criteria smaller than one ({0}). This would lead to a never ending loop of expansion and contraction as an internal storage array length equal to the number of elements would satisfy the contraction criteria."),
|
||||
CONVERGENCE_FAILED("convergence failed"), /* keep */
|
||||
CROSSING_BOUNDARY_LOOPS("some outline boundary loops cross each other"),
|
||||
CUMULATIVE_PROBABILITY_RETURNED_NAN("Cumulative probability function returned NaN for argument {0} p = {1}"),
|
||||
DIFFERENT_ROWS_LENGTHS("some rows have length {0} while others have length {1}"),
|
||||
DIGEST_NOT_INITIALIZED("digest not initialized"),
|
||||
|
@ -163,6 +164,7 @@ public enum LocalizedFormats implements Localizable {
|
|||
ROBUSTNESS_ITERATIONS("number of robustness iterations ({0})"),
|
||||
START_POSITION("start position ({0})"), /* keep */
|
||||
NON_CONVERGENT_CONTINUED_FRACTION("Continued fraction convergents failed to converge (in less than {0} iterations) for value {1}"),
|
||||
NON_INVERTIBLE_TRANSFORM("non-invertible affine transform collapses some lines into single points"),
|
||||
NON_POSITIVE_MICROSPHERE_ELEMENTS("number of microsphere elements must be positive, but got {0}"),
|
||||
NON_POSITIVE_POLYNOMIAL_DEGREE("polynomial degree must be positive: degree={0}"),
|
||||
NON_REAL_FINITE_ABSCISSA("all abscissae must be finite real numbers, but {0}-th is {1}"),
|
||||
|
@ -219,6 +221,7 @@ public enum LocalizedFormats implements Localizable {
|
|||
NOT_STRICTLY_INCREASING_NUMBER_OF_POINTS("points {0} and {1} are not strictly increasing ({2} >= {3})"),
|
||||
NOT_STRICTLY_INCREASING_SEQUENCE("points {3} and {2} are not strictly increasing ({1} >= {0})"), /* keep */
|
||||
NOT_SUBTRACTION_COMPATIBLE_MATRICES("{0}x{1} and {2}x{3} matrices are not subtraction compatible"),
|
||||
NOT_SUPPORTED_IN_DIMENSION_N("method not supported in dimension {0}"),
|
||||
NOT_SYMMETRIC_MATRIX("not symmetric matrix"),
|
||||
NON_SYMMETRIC_MATRIX("non symmetric matrix: the difference between entries at ({0},{1}) and ({1},{0}) is larger than {2}"), /* keep */
|
||||
NO_BIN_SELECTED("no bin selected"),
|
||||
|
@ -259,6 +262,7 @@ public enum LocalizedFormats implements Localizable {
|
|||
OUT_OF_RANGE_ROOT_OF_UNITY_INDEX("out of range root of unity index {0} (must be in [{1};{2}])"),
|
||||
OUT_OF_RANGE("out of range"), /* keep */
|
||||
OUT_OF_RANGE_SIMPLE("{0} out of [{1}, {2}] range"), /* keep */
|
||||
OUTLINE_BOUNDARY_LOOP_OPEN("an outline boundary loop is open"),
|
||||
OVERFLOW_IN_FRACTION("overflow in fraction {0}/{1}, cannot negate"),
|
||||
OVERFLOW_IN_ADDITION("overflow in addition: {0} + {1}"),
|
||||
OVERFLOW_IN_SUBTRACTION("overflow in subtraction: {0} - {1}"),
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* 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.math.geometry.euclidean.oneD;
|
||||
|
||||
|
||||
/** This class represents a 1D interval.
|
||||
* @see IntervalsSet
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public class Interval {
|
||||
|
||||
/** The lower bound of the interval. */
|
||||
private final double lower;
|
||||
|
||||
/** The upper bound of the interval. */
|
||||
private final double upper;
|
||||
|
||||
/** Simple constructor.
|
||||
* @param lower lower bound of the interval
|
||||
* @param upper upper bound of the interval
|
||||
*/
|
||||
public Interval(final double lower, final double upper) {
|
||||
this.lower = lower;
|
||||
this.upper = upper;
|
||||
}
|
||||
|
||||
/** Get the lower bound of the interval.
|
||||
* @return lower bound of the interval
|
||||
*/
|
||||
public double getLower() {
|
||||
return lower;
|
||||
}
|
||||
|
||||
/** Get the upper bound of the interval.
|
||||
* @return upper bound of the interval
|
||||
*/
|
||||
public double getUpper() {
|
||||
return upper;
|
||||
}
|
||||
|
||||
/** Get the length of the interval.
|
||||
* @return length of the interval
|
||||
*/
|
||||
public double getLength() {
|
||||
return upper - lower;
|
||||
}
|
||||
|
||||
/** Get the midpoint of the interval.
|
||||
* @return midpoint of the interval
|
||||
*/
|
||||
public double getMidPoint() {
|
||||
return 0.5 * (lower + upper);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,239 @@
|
|||
/*
|
||||
* 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.math.geometry.euclidean.oneD;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.math.geometry.partitioning.BSPTree;
|
||||
import org.apache.commons.math.geometry.partitioning.Region;
|
||||
import org.apache.commons.math.geometry.partitioning.SubHyperplane;
|
||||
|
||||
/** This class represents a 1D region: a set of intervals.
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public class IntervalsSet extends Region {
|
||||
|
||||
/** Build an intervals set representing the whole real line.
|
||||
*/
|
||||
public IntervalsSet() {
|
||||
super();
|
||||
}
|
||||
|
||||
/** Build an intervals set corresponding to a single interval.
|
||||
* @param lower lower bound of the interval, must be lesser or equal
|
||||
* to {@code upper} (may be {@code Double.NEGATIVE_INFINITY})
|
||||
* @param upper upper bound of the interval, must be greater or equal
|
||||
* to {@code lower} (may be {@code Double.POSITIVE_INFINITY})
|
||||
*/
|
||||
public IntervalsSet(final double lower, final double upper) {
|
||||
super(buildTree(lower, upper));
|
||||
}
|
||||
|
||||
/** Build an intervals set from an inside/outside BSP tree.
|
||||
* <p>The leaf nodes of the BSP tree <em>must</em> have a
|
||||
* {@code Boolean} attribute representing the inside status of
|
||||
* the corresponding cell (true for inside cells, false for outside
|
||||
* cells). In order to avoid building too many small objects, it is
|
||||
* recommended to use the predefined constants
|
||||
* {@code Boolean.TRUE} and {@code Boolean.FALSE}</p>
|
||||
* @param tree inside/outside BSP tree representing the intervals set
|
||||
*/
|
||||
public IntervalsSet(final BSPTree tree) {
|
||||
super(tree);
|
||||
}
|
||||
|
||||
/** Build an intervals set from a Boundary REPresentation (B-rep).
|
||||
* <p>The boundary is provided as a collection of {@link
|
||||
* SubHyperplane sub-hyperplanes}. Each sub-hyperplane has the
|
||||
* interior part of the region on its minus side and the exterior on
|
||||
* its plus side.</p>
|
||||
* <p>The boundary elements can be in any order, and can form
|
||||
* several non-connected sets (like for example polygons with holes
|
||||
* or a set of disjoints polyhedrons considered as a whole). In
|
||||
* fact, the elements do not even need to be connected together
|
||||
* (their topological connections are not used here). However, if the
|
||||
* boundary does not really separate an inside open from an outside
|
||||
* open (open having here its topological meaning), then subsequent
|
||||
* calls to the {@link
|
||||
* Region#checkPoint(org.apache.commons.math.geometry.partitioning.Point)
|
||||
* checkPoint} method will not be meaningful anymore.</p>
|
||||
* <p>If the boundary is empty, the region will represent the whole
|
||||
* space.</p>
|
||||
* @param boundary collection of boundary elements
|
||||
*/
|
||||
public IntervalsSet(final Collection<SubHyperplane> boundary) {
|
||||
super(boundary);
|
||||
}
|
||||
|
||||
/** Build an inside/outside tree representing a single interval.
|
||||
* @param lower lower bound of the interval, must be lesser or equal
|
||||
* to {@code upper} (may be {@code Double.NEGATIVE_INFINITY})
|
||||
* @param upper upper bound of the interval, must be greater or equal
|
||||
* to {@code lower} (may be {@code Double.POSITIVE_INFINITY})
|
||||
* @return the built tree
|
||||
*/
|
||||
private static BSPTree buildTree(final double lower, final double upper) {
|
||||
if (Double.isInfinite(lower) && (lower < 0)) {
|
||||
if (Double.isInfinite(upper) && (upper > 0)) {
|
||||
// the tree must cover the whole real line
|
||||
return new BSPTree(Boolean.TRUE);
|
||||
}
|
||||
// the tree must be open on the negative infinity side
|
||||
final SubHyperplane upperCut =
|
||||
new SubHyperplane(new OrientedPoint(new Point1D(upper), true));
|
||||
return new BSPTree(upperCut,
|
||||
new BSPTree(Boolean.FALSE),
|
||||
new BSPTree(Boolean.TRUE),
|
||||
null);
|
||||
}
|
||||
final SubHyperplane lowerCut =
|
||||
new SubHyperplane(new OrientedPoint(new Point1D(lower), false));
|
||||
if (Double.isInfinite(upper) && (upper > 0)) {
|
||||
// the tree must be open on the positive infinity side
|
||||
return new BSPTree(lowerCut,
|
||||
new BSPTree(Boolean.FALSE),
|
||||
new BSPTree(Boolean.TRUE),
|
||||
null);
|
||||
}
|
||||
|
||||
// the tree must be bounded on the two sides
|
||||
final SubHyperplane upperCut =
|
||||
new SubHyperplane(new OrientedPoint(new Point1D(upper), true));
|
||||
return new BSPTree(lowerCut,
|
||||
new BSPTree(Boolean.FALSE),
|
||||
new BSPTree(upperCut,
|
||||
new BSPTree(Boolean.FALSE),
|
||||
new BSPTree(Boolean.TRUE),
|
||||
null),
|
||||
null);
|
||||
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public Region buildNew(final BSPTree tree) {
|
||||
return new IntervalsSet(tree);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
protected void computeGeometricalProperties() {
|
||||
if (getTree(false).getCut() == null) {
|
||||
setBarycenter(Point1D.UNDEFINED);
|
||||
setSize(((Boolean) getTree(false).getAttribute()) ? Double.POSITIVE_INFINITY : 0);
|
||||
} else {
|
||||
double size = 0.0;
|
||||
double sum = 0.0;
|
||||
for (final Interval interval : asList()) {
|
||||
size += interval.getLength();
|
||||
sum += interval.getLength() * interval.getMidPoint();
|
||||
}
|
||||
setSize(size);
|
||||
setBarycenter(Double.isInfinite(size) ? Point1D.UNDEFINED : new Point1D(sum / size));
|
||||
}
|
||||
}
|
||||
|
||||
/** Get the lowest value belonging to the instance.
|
||||
* @return lowest value belonging to the instance
|
||||
* ({@code Double.NEGATIVE_INFINITY} if the instance doesn't
|
||||
* have any low bound, {@code Double.POSITIVE_INFINITY} if the
|
||||
* instance is empty)
|
||||
*/
|
||||
public double getInf() {
|
||||
BSPTree node = getTree(false);
|
||||
double inf = Double.POSITIVE_INFINITY;
|
||||
while (node.getCut() != null) {
|
||||
final OrientedPoint op = (OrientedPoint) node.getCut().getHyperplane();
|
||||
inf = op.getLocation().getAbscissa();
|
||||
node = op.isDirect() ? node.getMinus() : node.getPlus();
|
||||
}
|
||||
return ((Boolean) node.getAttribute()) ? Double.NEGATIVE_INFINITY : inf;
|
||||
}
|
||||
|
||||
/** Get the highest value belonging to the instance.
|
||||
* @return highest value belonging to the instance
|
||||
* ({@code Double.POSITIVE_INFINITY} if the instance doesn't
|
||||
* have any high bound, {@code Double.NEGATIVE_INFINITY} if the
|
||||
* instance is empty)
|
||||
*/
|
||||
public double getSup() {
|
||||
BSPTree node = getTree(false);
|
||||
double sup = Double.NEGATIVE_INFINITY;
|
||||
while (node.getCut() != null) {
|
||||
final OrientedPoint op = (OrientedPoint) node.getCut().getHyperplane();
|
||||
sup = op.getLocation().getAbscissa();
|
||||
node = op.isDirect() ? node.getPlus() : node.getMinus();
|
||||
}
|
||||
return ((Boolean) node.getAttribute()) ? Double.POSITIVE_INFINITY : sup;
|
||||
}
|
||||
|
||||
/** Build an ordered list of intervals representing the instance.
|
||||
* <p>This method builds this intervals set as an ordered list of
|
||||
* {@link Interval Interval} elements. If the intervals set has no
|
||||
* lower limit, the first interval will have its low bound equal to
|
||||
* {@code Double.NEGATIVE_INFINITY}. If the intervals set has
|
||||
* no upper limit, the last interval will have its upper bound equal
|
||||
* to {@code Double.POSITIVE_INFINITY}. An empty tree will
|
||||
* build an empty list while a tree representing the whole real line
|
||||
* will build a one element list with both bounds beeing
|
||||
* infinite.</p>
|
||||
* @return a new ordered list containing {@link Interval Interval}
|
||||
* elements
|
||||
*/
|
||||
public List<Interval> asList() {
|
||||
final List<Interval> list = new ArrayList<Interval>();
|
||||
recurseList(getTree(false), list,
|
||||
Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
|
||||
return list;
|
||||
}
|
||||
|
||||
/** Update an intervals list.
|
||||
* @param node current node
|
||||
* @param list list to update
|
||||
* @param lower lower bound of the current convex cell
|
||||
* @param upper upper bound of the current convex cell
|
||||
*/
|
||||
private void recurseList(final BSPTree node, final List<Interval> list,
|
||||
final double lower, final double upper) {
|
||||
|
||||
if (node.getCut() == null) {
|
||||
if ((Boolean) node.getAttribute()) {
|
||||
// this leaf cell is an inside cell: an interval
|
||||
list.add(new Interval(lower, upper));
|
||||
}
|
||||
} else {
|
||||
final OrientedPoint op = (OrientedPoint) node.getCut().getHyperplane();
|
||||
final Point1D loc = op.getLocation();
|
||||
double x = loc.getAbscissa();
|
||||
|
||||
// make sure we explore the tree in increasing order
|
||||
final BSPTree low = op.isDirect() ? node.getMinus() : node.getPlus();
|
||||
final BSPTree high = op.isDirect() ? node.getPlus() : node.getMinus();
|
||||
|
||||
recurseList(low, list, lower, x);
|
||||
if ((checkPoint(low, loc) == Location.INSIDE) &&
|
||||
(checkPoint(high, loc) == Location.INSIDE)) {
|
||||
// merge the last interval added and the first one of the high sub-tree
|
||||
x = ((Interval) list.remove(list.size() - 1)).getLower();
|
||||
}
|
||||
recurseList(high, list, x, upper);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,222 @@
|
|||
/*
|
||||
* 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.math.geometry.euclidean.oneD;
|
||||
|
||||
import org.apache.commons.math.exception.MathUnsupportedOperationException;
|
||||
import org.apache.commons.math.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math.geometry.partitioning.BSPTree;
|
||||
import org.apache.commons.math.geometry.partitioning.Hyperplane;
|
||||
import org.apache.commons.math.geometry.partitioning.Point;
|
||||
import org.apache.commons.math.geometry.partitioning.Region;
|
||||
import org.apache.commons.math.geometry.partitioning.SubHyperplane;
|
||||
import org.apache.commons.math.geometry.partitioning.SubSpace;
|
||||
|
||||
/** This class represents a 1D oriented hyperplane.
|
||||
* <p>An hyperplane in 1D is a simple point, its orientation being a
|
||||
* boolean.</p>
|
||||
* <p>Instances of this class are guaranteed to be immutable.</p>
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public class OrientedPoint implements Hyperplane {
|
||||
|
||||
/** Dummy region returned by the {@link #wholeHyperplane} method. */
|
||||
private static final Region DUMMY_REGION = new DummyRegion();
|
||||
|
||||
/** Point location. */
|
||||
private Point1D location;
|
||||
|
||||
/** Orientation. */
|
||||
private boolean direct;
|
||||
|
||||
/** Simple constructor.
|
||||
* @param location location of the hyperplane
|
||||
* @param direct if true, the plus side of the hyperplane is towards
|
||||
* abscissae greater than {@code location}
|
||||
*/
|
||||
public OrientedPoint(final Point1D location, final boolean direct) {
|
||||
this.location = location;
|
||||
this.direct = direct;
|
||||
}
|
||||
|
||||
/** Copy the instance.
|
||||
* <p>Since instances are immutable, this method directly returns
|
||||
* the instance.</p>
|
||||
* @return the instance itself
|
||||
*/
|
||||
public Hyperplane copySelf() {
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Get the offset (oriented distance) of a point to the hyperplane.
|
||||
* @param point point to check
|
||||
* @return offset of the point
|
||||
*/
|
||||
public double getOffset(final Point point) {
|
||||
final double delta = ((Point1D) point).getAbscissa() - location.getAbscissa();
|
||||
return direct ? delta : -delta;
|
||||
}
|
||||
|
||||
/** Transform a space point into a sub-space point.
|
||||
* <p>Since this class represent zero dimension spaces which does
|
||||
* not have lower dimension sub-spaces, this method cannot be
|
||||
* supported here. It always throws a {@code RuntimeException}
|
||||
* when called.</p>
|
||||
* @param point n-dimension point of the space
|
||||
* @return (n-1)-dimension point of the sub-space corresponding to
|
||||
* the specified space point
|
||||
* @see #toSpace
|
||||
*/
|
||||
public Point toSubSpace(final Point point) {
|
||||
throw new MathUnsupportedOperationException(LocalizedFormats.NOT_SUPPORTED_IN_DIMENSION_N, 1);
|
||||
}
|
||||
|
||||
/** Transform a sub-space point into a space point.
|
||||
* <p>Since this class represent zero dimension spaces which does
|
||||
* not have lower dimension sub-spaces, this method cannot be
|
||||
* supported here. It always throws a {@code RuntimeException}
|
||||
* when called.</p>
|
||||
* @param point (n-1)-dimension point of the sub-space
|
||||
* @return n-dimension point of the space corresponding to the
|
||||
* specified sub-space point
|
||||
* @see #toSubSpace
|
||||
*/
|
||||
public Point toSpace(final Point point) {
|
||||
throw new MathUnsupportedOperationException(LocalizedFormats.NOT_SUPPORTED_IN_DIMENSION_N, 1);
|
||||
}
|
||||
|
||||
/** Build the sub-space shared by the instance and another hyperplane.
|
||||
* <p>Since this class represent zero dimension spaces which does
|
||||
* not have lower dimension sub-spaces, this method cannot be
|
||||
* supported here. It always throws a {@code RuntimeException}
|
||||
* when called.</p>
|
||||
* @param other other sub-space (must have the same dimension as the
|
||||
* instance)
|
||||
* @return a sub-space at the intersection of the instance and the
|
||||
* other sub-space (it has a dimension one unit less than the
|
||||
* instance)
|
||||
*/
|
||||
public SubSpace intersection(final Hyperplane other) {
|
||||
throw new MathUnsupportedOperationException(LocalizedFormats.NOT_SUPPORTED_IN_DIMENSION_N, 1);
|
||||
}
|
||||
|
||||
/** Build a region covering the whole hyperplane.
|
||||
* <p>Since this class represent zero dimension spaces which does
|
||||
* not have lower dimension sub-spaces, this method returns a dummy
|
||||
* implementation of a {@link Region Region} (always the same
|
||||
* instance). This implementation is only used to allow the {@link
|
||||
* SubHyperplane SubHyperplane} class implementation to work
|
||||
* properly, it should <em>not</em> be used otherwise.</p>
|
||||
* @return a dummy region
|
||||
*/
|
||||
public Region wholeHyperplane() {
|
||||
return DUMMY_REGION;
|
||||
}
|
||||
|
||||
/** Build a region covering the whole space.
|
||||
* @return a region containing the instance (really an {@link
|
||||
* IntervalsSet IntervalsSet} instance)
|
||||
*/
|
||||
public Region wholeSpace() {
|
||||
return new IntervalsSet();
|
||||
}
|
||||
|
||||
/** Check if the instance has the same orientation as another hyperplane.
|
||||
* <p>This method is expected to be called on parallel hyperplanes
|
||||
* (i.e. when the {@link #side side} method would return {@link
|
||||
* org.apache.commons.math.geometry.partitioning.Hyperplane.Side#HYPER}
|
||||
* for some sub-hyperplane having the specified hyperplane
|
||||
* as its underlying hyperplane). The method should <em>not</em>
|
||||
* re-check for parallelism, only for orientation, typically by
|
||||
* testing something like the sign of the dot-products of
|
||||
* normals.</p>
|
||||
* @param other other hyperplane to check against the instance
|
||||
* @return true if the instance and the other hyperplane have
|
||||
* the same orientation
|
||||
*/
|
||||
public boolean sameOrientationAs(final Hyperplane other) {
|
||||
return !(direct ^ ((OrientedPoint) other).direct);
|
||||
}
|
||||
|
||||
/** Compute the relative position of a sub-hyperplane with respect
|
||||
* to the instance.
|
||||
* @param sub sub-hyperplane to check
|
||||
* @return one of {@link org.apache.commons.math.geometry.partitioning.Hyperplane.Side#PLUS PLUS},
|
||||
* {@link org.apache.commons.math.geometry.partitioning.Hyperplane.Side#MINUS MINUS}
|
||||
* or {@link org.apache.commons.math.geometry.partitioning.Hyperplane.Side#HYPER HYPER}
|
||||
* (in dimension 1, this method <em>never</em> returns {@link
|
||||
* org.apache.commons.math.geometry.partitioning.Hyperplane.Side#BOTH BOTH})
|
||||
*
|
||||
*/
|
||||
public Side side(final SubHyperplane sub) {
|
||||
final double global = getOffset(((OrientedPoint) sub.getHyperplane()).location);
|
||||
return (global < -1.0e-10) ? Side.MINUS : ((global > 1.0e-10) ? Side.PLUS : Side.HYPER);
|
||||
}
|
||||
|
||||
/** Split a sub-hyperplane in two parts by the instance.
|
||||
* @param sub sub-hyperplane to split
|
||||
* @return an object containing both the part of the sub-hyperplane
|
||||
* on the plus side of the instance and the part of the
|
||||
* sub-hyperplane on the minus side of the instance
|
||||
*/
|
||||
public SplitSubHyperplane split(final SubHyperplane sub) {
|
||||
final double global = getOffset(((OrientedPoint) sub.getHyperplane()).location);
|
||||
return (global < -1.0e-10) ? new SplitSubHyperplane(null, sub) : new SplitSubHyperplane(sub, null);
|
||||
}
|
||||
|
||||
/** Get the hyperplane location on the real line.
|
||||
* @return the hyperplane location
|
||||
*/
|
||||
public Point1D getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
/** Check if the hyperplane orientation is direct.
|
||||
* @return true if the plus side of the hyperplane is towards
|
||||
* abscissae greater than hyperplane location
|
||||
*/
|
||||
public boolean isDirect() {
|
||||
return direct;
|
||||
}
|
||||
|
||||
/** Revert the instance.
|
||||
*/
|
||||
public void revertSelf() {
|
||||
direct = !direct;
|
||||
}
|
||||
|
||||
/** Dummy region representing the whole set of reals. */
|
||||
private static class DummyRegion extends Region {
|
||||
|
||||
/** Simple constructor.
|
||||
*/
|
||||
public DummyRegion() {
|
||||
super();
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public Region buildNew(final BSPTree tree) {
|
||||
return this;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
protected void computeGeometricalProperties() {
|
||||
setSize(0);
|
||||
setBarycenter(Point1D.ZERO);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* 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.math.geometry.euclidean.oneD;
|
||||
|
||||
import org.apache.commons.math.geometry.partitioning.Point;
|
||||
|
||||
/** This class represents a 1D point.
|
||||
* <p>Instances of this class are guaranteed to be immutable.</p>
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public class Point1D implements Point {
|
||||
|
||||
/** Point at 0.0 abscissa. */
|
||||
public static final Point1D ZERO = new Point1D(0.0);
|
||||
|
||||
/** Point at 1.0 abscissa. */
|
||||
public static final Point1D ONE = new Point1D(1.0);
|
||||
|
||||
/** Point at undefined (NaN) abscissa. */
|
||||
public static final Point1D UNDEFINED = new Point1D(Double.NaN);
|
||||
|
||||
/** Abscissa of the point. */
|
||||
private double x;
|
||||
|
||||
/** Simple constructor.
|
||||
* @param x abscissa of the point
|
||||
*/
|
||||
public Point1D(final double x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
/** Get the abscissa of the point.
|
||||
* @return abscissa of the point
|
||||
*/
|
||||
public double getAbscissa() {
|
||||
return x;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
<html>
|
||||
<!--
|
||||
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.
|
||||
-->
|
||||
<!-- $Revision$ -->
|
||||
<body>
|
||||
<p>
|
||||
This package provides basic 1D geometry components.
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
|
@ -15,7 +15,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math.geometry;
|
||||
package org.apache.commons.math.geometry.euclidean.threeD;
|
||||
|
||||
import org.apache.commons.math.MathException;
|
||||
import org.apache.commons.math.exception.util.LocalizedFormats;
|
|
@ -0,0 +1,153 @@
|
|||
/*
|
||||
* 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.math.geometry.euclidean.threeD;
|
||||
|
||||
import org.apache.commons.math.geometry.euclidean.oneD.Point1D;
|
||||
import org.apache.commons.math.geometry.partitioning.Point;
|
||||
import org.apache.commons.math.geometry.partitioning.SubSpace;
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
|
||||
/** The class represent lines in a three dimensional space.
|
||||
|
||||
* <p>Each oriented line is intrinsically associated with an abscissa
|
||||
* wich is a coordinate on the line. The point at abscissa 0 is the
|
||||
* orthogonal projection of the origin on the line, another equivalent
|
||||
* way to express this is to say that it is the point of the line
|
||||
* which is closest to the origin. Abscissa increases in the line
|
||||
* direction.</p>
|
||||
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public class Line implements SubSpace {
|
||||
|
||||
/** Line direction. */
|
||||
private Vector3D direction;
|
||||
|
||||
/** Line point closest to the origin. */
|
||||
private Point3D zero;
|
||||
|
||||
/** Build a line from a point and a direction.
|
||||
* @param p point belonging to the line (this can be any point)
|
||||
* @param direction direction of the line
|
||||
* @exception IllegalArgumentException if the direction norm is too small
|
||||
*/
|
||||
public Line(final Vector3D p, final Vector3D direction) {
|
||||
reset(p, direction);
|
||||
}
|
||||
|
||||
/** Reset the instance as if built from a point and a normal.
|
||||
* @param p point belonging to the line (this can be any point)
|
||||
* @param dir direction of the line
|
||||
* @exception IllegalArgumentException if the direction norm is too small
|
||||
*/
|
||||
public void reset(final Vector3D p, final Vector3D dir) {
|
||||
final double norm = dir.getNorm();
|
||||
if (norm == 0.0) {
|
||||
throw new IllegalArgumentException("null norm");
|
||||
}
|
||||
this.direction = new Vector3D(1.0 / norm, dir);
|
||||
zero = new Point3D(1.0, p, -Vector3D.dotProduct(p, this.direction), this.direction);
|
||||
}
|
||||
|
||||
/** Revert the line direction.
|
||||
*/
|
||||
public void revertSelf() {
|
||||
direction = direction.negate();
|
||||
}
|
||||
|
||||
/** Get the normalized direction vector.
|
||||
* @return normalized direction vector
|
||||
*/
|
||||
public Vector3D getDirection() {
|
||||
return direction;
|
||||
}
|
||||
|
||||
/** Get the abscissa of a point with respect to the line.
|
||||
* <p>The abscissa is 0 if the projection of the point and the
|
||||
* projection of the frame origin on the line are the same
|
||||
* point.</p>
|
||||
* @param point point to check (must be a {@link Vector3D Vector3D}
|
||||
* instance)
|
||||
* @return abscissa of the point (really a
|
||||
* {org.apache.commons.math.geometry.euclidean.oneD.Point1D Point1D} instance)
|
||||
*/
|
||||
public Point toSubSpace(final Point point) {
|
||||
final double x = Vector3D.dotProduct(((Vector3D) point).subtract(zero), direction);
|
||||
return new Point1D(x);
|
||||
}
|
||||
|
||||
/** Get one point from the line.
|
||||
* @param point desired abscissa for the point (must be a
|
||||
* {org.apache.commons.math.geometry.euclidean.oneD.Point1D Point1D} instance)
|
||||
* @return one point belonging to the line, at specified abscissa
|
||||
* (really a {@link Vector3D Vector3D} instance)
|
||||
*/
|
||||
public Point toSpace(final Point point) {
|
||||
return new Point3D(1.0, zero, ((Point1D) point).getAbscissa(), direction);
|
||||
}
|
||||
|
||||
/** Check if the instance is similar to another line.
|
||||
* <p>Lines are considered similar if they contain the same
|
||||
* points. This does not mean they are equal since they can have
|
||||
* opposite directions.</p>
|
||||
* @param line line to which instance should be compared
|
||||
* @return true if the lines are similar
|
||||
*/
|
||||
public boolean isSimilarTo(final Line line) {
|
||||
final double angle = Vector3D.angle(direction, line.direction);
|
||||
return ((angle < 1.0e-10) || (angle > (FastMath.PI - 1.0e-10))) && contains(line.zero);
|
||||
}
|
||||
|
||||
/** Check if the instance contains a point.
|
||||
* @param p point to check
|
||||
* @return true if p belongs to the line
|
||||
*/
|
||||
public boolean contains(final Vector3D p) {
|
||||
return distance(p) < 1.0e-10;
|
||||
}
|
||||
|
||||
/** Compute the distance between the instance and a point.
|
||||
* @param p to check
|
||||
* @return distance between the instance and the point
|
||||
*/
|
||||
public double distance(final Vector3D p) {
|
||||
final Vector3D d = p.subtract(zero);
|
||||
final Vector3D n = new Vector3D(1.0, d, -Vector3D.dotProduct(d, direction), direction);
|
||||
return n.getNorm();
|
||||
}
|
||||
|
||||
/** Compute the shortest distance between the instance and another line.
|
||||
* @param line line to check agains the instance
|
||||
* @return shortest distance between the instance and the line
|
||||
*/
|
||||
public double distance(final Line line) {
|
||||
|
||||
final Vector3D normal = Vector3D.crossProduct(direction, line.direction);
|
||||
if (normal.getNorm() < 1.0e-10) {
|
||||
// lines are parallel
|
||||
return distance(line.zero);
|
||||
}
|
||||
|
||||
// separating middle plane
|
||||
final Plane middle = new Plane(new Vector3D(0.5, zero, 0.5, line.zero), normal);
|
||||
|
||||
// the lines are at the same distance on either side of the plane
|
||||
return 2 * FastMath.abs(middle.getOffset(zero));
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -15,7 +15,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math.geometry;
|
||||
package org.apache.commons.math.geometry.euclidean.threeD;
|
||||
|
||||
import org.apache.commons.math.MathException;
|
||||
import org.apache.commons.math.exception.util.Localizable;
|
|
@ -0,0 +1,250 @@
|
|||
/*
|
||||
* 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.math.geometry.euclidean.threeD;
|
||||
|
||||
import org.apache.commons.math.geometry.euclidean.twoD.Point2D;
|
||||
import org.apache.commons.math.geometry.euclidean.twoD.PolygonsSet;
|
||||
import org.apache.commons.math.geometry.partitioning.BSPTree;
|
||||
import org.apache.commons.math.geometry.partitioning.BSPTreeVisitor;
|
||||
import org.apache.commons.math.geometry.partitioning.Region;
|
||||
import org.apache.commons.math.geometry.partitioning.SubHyperplane;
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/** Extractor for {@link PolygonsSet polyhedrons sets} outlines.
|
||||
* <p>This class extracts the 2D outlines from {{@link PolygonsSet
|
||||
* polyhedrons sets} in a specified projection plane.</p>
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public class OutlineExtractor {
|
||||
|
||||
/** Abscissa axis of the projection plane. */
|
||||
private Vector3D u;
|
||||
|
||||
/** Ordinate axis of the projection plane. */
|
||||
private Vector3D v;
|
||||
|
||||
/** Normal of the projection plane (viewing direction). */
|
||||
private Vector3D w;
|
||||
|
||||
/** Build an extractor for a specific projection plane.
|
||||
* @param u abscissa axis of the projection point
|
||||
* @param v ordinate axis of the projection point
|
||||
*/
|
||||
public OutlineExtractor(final Vector3D u, final Vector3D v) {
|
||||
this.u = u;
|
||||
this.v = v;
|
||||
w = Vector3D.crossProduct(u, v);
|
||||
}
|
||||
|
||||
/** Extract the outline of a polyhedrons set.
|
||||
* @param polyhedronsSet polyhedrons set whose outline must be extracted
|
||||
* @return an outline, as an array of loops.
|
||||
*/
|
||||
public Point2D[][] getOutline(final PolyhedronsSet polyhedronsSet) {
|
||||
|
||||
// project all boundary facets into one polygons set
|
||||
final BoundaryProjector projector = new BoundaryProjector();
|
||||
polyhedronsSet.getTree(true).visit(projector);
|
||||
final PolygonsSet projected = projector.getProjected();
|
||||
|
||||
// Remove the spurious intermediate vertices from the outline
|
||||
final Point2D[][] outline = projected.getVertices();
|
||||
for (int i = 0; i < outline.length; ++i) {
|
||||
final Point2D[] rawLoop = outline[i];
|
||||
int end = rawLoop.length;
|
||||
int j = 0;
|
||||
while (j < end) {
|
||||
if (pointIsBetween(rawLoop, end, j)) {
|
||||
// the point should be removed
|
||||
for (int k = j; k < (end - 1); ++k) {
|
||||
rawLoop[k] = rawLoop[k + 1];
|
||||
}
|
||||
--end;
|
||||
} else {
|
||||
// the point remains in the loop
|
||||
++j;
|
||||
}
|
||||
}
|
||||
if (end != rawLoop.length) {
|
||||
// resize the array
|
||||
outline[i] = new Point2D[end];
|
||||
System.arraycopy(rawLoop, 0, outline[i], 0, end);
|
||||
}
|
||||
}
|
||||
|
||||
return outline;
|
||||
|
||||
}
|
||||
|
||||
/** Check if a point is geometrically between its neighbour in an array.
|
||||
* <p>The neighbours are computed considering the array is a loop
|
||||
* (i.e. point at index (n-1) is before point at index 0)</p>
|
||||
* @param loop points array
|
||||
* @param n number of points to consider in the array
|
||||
* @param i index of the point to check (must be between 0 and n-1)
|
||||
* @return true if the point is exactly between its neighbours
|
||||
*/
|
||||
private boolean pointIsBetween(final Point2D[] loop, final int n, final int i) {
|
||||
final Point2D previous = loop[(i + n - 1) % n];
|
||||
final Point2D current = loop[i];
|
||||
final Point2D next = loop[(i + 1) % n];
|
||||
final double dx1 = current.x - previous.x;
|
||||
final double dy1 = current.y - previous.y;
|
||||
final double dx2 = next.x - current.x;
|
||||
final double dy2 = next.y - current.y;
|
||||
final double cross = dx1 * dy2 - dx2 * dy1;
|
||||
final double dot = dx1 * dx2 + dy1 * dy2;
|
||||
final double d1d2 = FastMath.sqrt((dx1 * dx1 + dy1 * dy1) * (dx2 * dx2 + dy2 * dy2));
|
||||
return (FastMath.abs(cross) <= (1.0e-6 * d1d2)) && (dot >= 0.0);
|
||||
}
|
||||
|
||||
/** Visitor projecting the boundary facets on a plane. */
|
||||
private class BoundaryProjector implements BSPTreeVisitor {
|
||||
|
||||
/** Projection of the polyhedrons set on the plane. */
|
||||
private PolygonsSet projected;
|
||||
|
||||
/** Simple constructor.
|
||||
*/
|
||||
public BoundaryProjector() {
|
||||
projected = new PolygonsSet(new BSPTree(Boolean.FALSE));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public Order visitOrder(final BSPTree node) {
|
||||
return Order.MINUS_SUB_PLUS;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void visitInternalNode(final BSPTree node) {
|
||||
final Region.BoundaryAttribute attribute =
|
||||
(Region.BoundaryAttribute) node.getAttribute();
|
||||
if (attribute.getPlusOutside() != null) {
|
||||
addContribution(attribute.getPlusOutside(), false);
|
||||
}
|
||||
if (attribute.getPlusInside() != null) {
|
||||
addContribution(attribute.getPlusInside(), true);
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void visitLeafNode(final BSPTree node) {
|
||||
}
|
||||
|
||||
/** Add he contribution of a boundary facet.
|
||||
* @param facet boundary facet
|
||||
* @param reversed if true, the facet has the inside on its plus side
|
||||
*/
|
||||
private void addContribution(final SubHyperplane facet, final boolean reversed) {
|
||||
|
||||
// extract the vertices of the facet
|
||||
final Plane plane = (Plane) facet.getHyperplane();
|
||||
Point2D[][] vertices =
|
||||
((PolygonsSet) facet.getRemainingRegion()).getVertices();
|
||||
|
||||
final double scal = Vector3D.dotProduct(plane.getNormal(), w);
|
||||
if (FastMath.abs(scal) > 1.0e-3) {
|
||||
|
||||
if ((scal < 0) ^ reversed) {
|
||||
// the facet is seen from the inside,
|
||||
// we need to invert its boundary orientation
|
||||
final Point2D[][] newVertices = new Point2D[vertices.length][];
|
||||
for (int i = 0; i < vertices.length; ++i) {
|
||||
final Point2D[] loop = vertices[i];
|
||||
final Point2D[] newLoop = new Point2D[loop.length];
|
||||
if (loop[0] == null) {
|
||||
newLoop[0] = null;
|
||||
for (int j = 1; j < loop.length; ++j) {
|
||||
newLoop[j] = loop[loop.length - j];
|
||||
}
|
||||
} else {
|
||||
for (int j = 0; j < loop.length; ++j) {
|
||||
newLoop[j] = loop[loop.length - (j + 1)];
|
||||
}
|
||||
}
|
||||
newVertices[i] = newLoop;
|
||||
}
|
||||
|
||||
// use the reverted vertices
|
||||
vertices = newVertices;
|
||||
|
||||
}
|
||||
|
||||
// compute the projection of the facet in the outline plane
|
||||
final ArrayList<SubHyperplane> edges = new ArrayList<SubHyperplane>();
|
||||
for (Point2D[] loop : vertices) {
|
||||
final boolean closed = loop[0] != null;
|
||||
int previous = closed ? (loop.length - 1) : 1;
|
||||
Vector3D previous3D = (Vector3D) plane.toSpace(loop[previous]);
|
||||
int current = (previous + 1) % loop.length;
|
||||
Point2D pPoint = new Point2D(Vector3D.dotProduct(previous3D, u),
|
||||
Vector3D.dotProduct(previous3D, v));
|
||||
while (current < loop.length) {
|
||||
|
||||
final Vector3D current3D = (Vector3D) plane.toSpace(loop[current]);
|
||||
final Point2D cPoint = new Point2D(Vector3D.dotProduct(current3D, u),
|
||||
Vector3D.dotProduct(current3D, v));
|
||||
final org.apache.commons.math.geometry.euclidean.twoD.Line line =
|
||||
new org.apache.commons.math.geometry.euclidean.twoD.Line(pPoint, cPoint);
|
||||
SubHyperplane edge = new SubHyperplane(line);
|
||||
|
||||
if (closed || (previous != 1)) {
|
||||
// the previous point is a real vertex
|
||||
// it defines one bounding point of the edge
|
||||
final double angle = line.getAngle() + 0.5 * FastMath.PI;
|
||||
final org.apache.commons.math.geometry.euclidean.twoD.Line l =
|
||||
new org.apache.commons.math.geometry.euclidean.twoD.Line(pPoint, angle);
|
||||
edge = l.split(edge).getPlus();
|
||||
}
|
||||
|
||||
if (closed || (current != (loop.length - 1))) {
|
||||
// the current point is a real vertex
|
||||
// it defines one bounding point of the edge
|
||||
final double angle = line.getAngle() + 0.5 * FastMath.PI;
|
||||
final org.apache.commons.math.geometry.euclidean.twoD.Line l =
|
||||
new org.apache.commons.math.geometry.euclidean.twoD.Line(cPoint, angle);
|
||||
edge = l.split(edge).getMinus();
|
||||
}
|
||||
|
||||
edges.add(edge);
|
||||
|
||||
previous = current++;
|
||||
previous3D = current3D;
|
||||
pPoint = cPoint;
|
||||
|
||||
}
|
||||
}
|
||||
final PolygonsSet projectedFacet = new PolygonsSet(edges);
|
||||
|
||||
// add the contribution of the facet to the global outline
|
||||
projected = (PolygonsSet) Region.union(projected, projectedFacet);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/** Get the projecion of the polyhedrons set on the plane.
|
||||
* @return projecion of the polyhedrons set on the plane
|
||||
*/
|
||||
public PolygonsSet getProjected() {
|
||||
return projected;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,526 @@
|
|||
/*
|
||||
* 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.math.geometry.euclidean.threeD;
|
||||
|
||||
import org.apache.commons.math.geometry.euclidean.oneD.Point1D;
|
||||
import org.apache.commons.math.geometry.euclidean.twoD.Point2D;
|
||||
import org.apache.commons.math.geometry.euclidean.twoD.PolygonsSet;
|
||||
import org.apache.commons.math.geometry.partitioning.BSPTree;
|
||||
import org.apache.commons.math.geometry.partitioning.Hyperplane;
|
||||
import org.apache.commons.math.geometry.partitioning.Point;
|
||||
import org.apache.commons.math.geometry.partitioning.Region;
|
||||
import org.apache.commons.math.geometry.partitioning.SubHyperplane;
|
||||
import org.apache.commons.math.geometry.partitioning.SubSpace;
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
|
||||
/** The class represent planes in a three dimensional space.
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public class Plane implements Hyperplane {
|
||||
|
||||
/** Offset of the origin with respect to the plane. */
|
||||
private double originOffset;
|
||||
|
||||
/** Origin of the plane frame. */
|
||||
private Point3D origin;
|
||||
|
||||
/** First vector of the plane frame (in plane). */
|
||||
private Vector3D u;
|
||||
|
||||
/** Second vector of the plane frame (in plane). */
|
||||
private Vector3D v;
|
||||
|
||||
/** Third vector of the plane frame (plane normal). */
|
||||
private Vector3D w;
|
||||
|
||||
/** Build a plane normal to a given direction and containing the origin.
|
||||
* @param normal normal direction to the plane
|
||||
* @exception IllegalArgumentException if the normal norm is too small
|
||||
*/
|
||||
public Plane(final Vector3D normal) {
|
||||
setNormal(normal);
|
||||
originOffset = 0;
|
||||
setFrame();
|
||||
}
|
||||
|
||||
/** Build a plane from a point and a normal.
|
||||
* @param p point belonging to the plane
|
||||
* @param normal normal direction to the plane
|
||||
* @exception IllegalArgumentException if the normal norm is too small
|
||||
*/
|
||||
public Plane(final Vector3D p, final Vector3D normal) {
|
||||
setNormal(normal);
|
||||
originOffset = -Vector3D.dotProduct(p, w);
|
||||
setFrame();
|
||||
}
|
||||
|
||||
/** Build a plane from three points.
|
||||
* <p>The plane is oriented in the direction of
|
||||
* {@code (p2-p1) ^ (p3-p1)}</p>
|
||||
* @param p1 first point belonging to the plane
|
||||
* @param p2 second point belonging to the plane
|
||||
* @param p3 third point belonging to the plane
|
||||
* @exception IllegalArgumentException if the points do not constitute a plane
|
||||
*/
|
||||
public Plane(final Vector3D p1, final Vector3D p2, final Vector3D p3) {
|
||||
this(p1, Vector3D.crossProduct(p2.subtract(p1), p3.subtract(p1)));
|
||||
}
|
||||
|
||||
/** Copy constructor.
|
||||
* <p>The instance created is completely independant of the original
|
||||
* one. A deep copy is used, none of the underlying object are
|
||||
* shared.</p>
|
||||
* @param plane plane to copy
|
||||
*/
|
||||
public Plane(final Plane plane) {
|
||||
originOffset = plane.originOffset;
|
||||
origin = plane.origin;
|
||||
u = plane.u;
|
||||
v = plane.v;
|
||||
w = plane.w;
|
||||
}
|
||||
|
||||
/** Copy the instance.
|
||||
* <p>The instance created is completely independant of the original
|
||||
* one. A deep copy is used, none of the underlying objects are
|
||||
* shared (except for immutable objects).</p>
|
||||
* @return a new hyperplane, copy of the instance
|
||||
*/
|
||||
public Hyperplane copySelf() {
|
||||
return new Plane(this);
|
||||
}
|
||||
|
||||
/** Reset the instance as if built from a point and a normal.
|
||||
* @param p point belonging to the plane
|
||||
* @param normal normal direction to the plane
|
||||
*/
|
||||
public void reset(final Vector3D p, final Vector3D normal) {
|
||||
setNormal(normal);
|
||||
originOffset = -Vector3D.dotProduct(p, w);
|
||||
setFrame();
|
||||
}
|
||||
|
||||
/** Reset the instance from another one.
|
||||
* <p>The updated instance is completely independant of the original
|
||||
* one. A deep reset is used none of the underlying object is
|
||||
* shared.</p>
|
||||
* @param original plane to reset from
|
||||
*/
|
||||
public void reset(final Plane original) {
|
||||
originOffset = original.originOffset;
|
||||
origin = original.origin;
|
||||
u = original.u;
|
||||
v = original.v;
|
||||
w = original.w;
|
||||
}
|
||||
|
||||
/** Set the normal vactor.
|
||||
* @param normal normal direction to the plane (will be copied)
|
||||
* @exception IllegalArgumentException if the normal norm is too small
|
||||
*/
|
||||
private void setNormal(final Vector3D normal) {
|
||||
final double norm = normal.getNorm();
|
||||
if (norm < 1.0e-10) {
|
||||
throw new IllegalArgumentException("null norm");
|
||||
}
|
||||
w = new Vector3D(1.0 / norm, normal);
|
||||
}
|
||||
|
||||
/** Reset the plane frame.
|
||||
*/
|
||||
private void setFrame() {
|
||||
origin = new Point3D(-originOffset, w);
|
||||
u = w.orthogonal();
|
||||
v = Vector3D.crossProduct(w, u);
|
||||
}
|
||||
|
||||
/** Get the origin point of the plane frame.
|
||||
* <p>The point returned is the orthogonal projection of the
|
||||
* 3D-space origin in the plane.</p>
|
||||
* @return the origin point of the plane frame (point closest to the
|
||||
* 3D-space origin)
|
||||
*/
|
||||
public Point3D getOrigin() {
|
||||
return origin;
|
||||
}
|
||||
|
||||
/** Get the normalized normal vector.
|
||||
* <p>The frame defined by ({@link #getU getU}, {@link #getV getV},
|
||||
* {@link #getNormal getNormal}) is a rigth-handed orthonormalized
|
||||
* frame).</p>
|
||||
* @return normalized normal vector
|
||||
* @see #getU
|
||||
* @see #getV
|
||||
*/
|
||||
public Vector3D getNormal() {
|
||||
return w;
|
||||
}
|
||||
|
||||
/** Get the plane first canonical vector.
|
||||
* <p>The frame defined by ({@link #getU getU}, {@link #getV getV},
|
||||
* {@link #getNormal getNormal}) is a rigth-handed orthonormalized
|
||||
* frame).</p>
|
||||
* @return normalized first canonical vector
|
||||
* @see #getV
|
||||
* @see #getNormal
|
||||
*/
|
||||
public Vector3D getU() {
|
||||
return u;
|
||||
}
|
||||
|
||||
/** Get the plane second canonical vector.
|
||||
* <p>The frame defined by ({@link #getU getU}, {@link #getV getV},
|
||||
* {@link #getNormal getNormal}) is a rigth-handed orthonormalized
|
||||
* frame).</p>
|
||||
* @return normalized second canonical vector
|
||||
* @see #getU
|
||||
* @see #getNormal
|
||||
*/
|
||||
public Vector3D getV() {
|
||||
return v;
|
||||
}
|
||||
|
||||
/** Revert the plane.
|
||||
* <p>Replace the instance by a similar plane with opposite orientation.</p>
|
||||
* <p>The new plane frame is chosen in such a way that a 3D point that had
|
||||
* {@code (x, y)} in-plane coordinates and {@code z} offset with
|
||||
* respect to the plane and is unaffected by the change will have
|
||||
* {@code (y, x)} in-plane coordinates and {@code -z} offset with
|
||||
* respect to the new plane. This means that the {@code u} and {@code v}
|
||||
* vectors returned by the {@link #getU} and {@link #getV} methods are exchanged,
|
||||
* and the {@code w} vector returned by the {@link #getNormal} method is
|
||||
* reversed.</p>
|
||||
*/
|
||||
public void revertSelf() {
|
||||
final Vector3D tmp = u;
|
||||
u = v;
|
||||
v = tmp;
|
||||
w = w.negate();
|
||||
originOffset = -originOffset;
|
||||
}
|
||||
|
||||
/** Transform a 3D space point into an in-plane point.
|
||||
* @param point point of the space (must be a {@link Vector3D
|
||||
* Vector3D} instance)
|
||||
* @return in-plane point (really a {@link
|
||||
* org.apache.commons.math.geometry.euclidean.twoD.Point2D Point2D} instance)
|
||||
* @see #toSpace
|
||||
*/
|
||||
public Point toSubSpace(final Point point) {
|
||||
final Vector3D p3D = (Vector3D) point;
|
||||
return new Point2D(Vector3D.dotProduct(p3D, u),
|
||||
Vector3D.dotProduct(p3D, v));
|
||||
}
|
||||
|
||||
/** Transform an in-plane point into a 3D space point.
|
||||
* @param point in-plane point (must be a {@link
|
||||
* org.apache.commons.math.geometry.euclidean.twoD.Point2D Point2D} instance)
|
||||
* @return 3D space point (really a {@link Vector3D Vector3D} instance)
|
||||
* @see #toSubSpace
|
||||
*/
|
||||
public Point toSpace(final Point point) {
|
||||
final Point2D p2D = (Point2D) point;
|
||||
return new Point3D(p2D.x, u, p2D.y, v, -originOffset, w);
|
||||
}
|
||||
|
||||
/** Get one point from the 3D-space.
|
||||
* @param inPlane desired in-plane coordinates for the point in the
|
||||
* plane
|
||||
* @param offset desired offset for the point
|
||||
* @return one point in the 3D-space, with given coordinates and offset
|
||||
* relative to the plane
|
||||
*/
|
||||
public Vector3D getPointAt(final Point2D inPlane, final double offset) {
|
||||
return new Vector3D(inPlane.x, u, inPlane.y, v, offset - originOffset, w);
|
||||
}
|
||||
|
||||
/** Check if the instance is similar to another plane.
|
||||
* <p>Planes are considered similar if they contain the same
|
||||
* points. This does not mean they are equal since they can have
|
||||
* opposite normals.</p>
|
||||
* @param plane plane to which the instance is compared
|
||||
* @return true if the planes are similar
|
||||
*/
|
||||
public boolean isSimilarTo(final Plane plane) {
|
||||
final double angle = Vector3D.angle(w, plane.w);
|
||||
return ((angle < 1.0e-10) && (FastMath.abs(originOffset - plane.originOffset) < 1.0e-10)) ||
|
||||
((angle > (FastMath.PI - 1.0e-10)) && (FastMath.abs(originOffset + plane.originOffset) < 1.0e-10));
|
||||
}
|
||||
|
||||
/** Rotate the plane around the specified point.
|
||||
* <p>The instance is not modified, a new instance is created.</p>
|
||||
* @param center rotation center
|
||||
* @param rotation vectorial rotation operator
|
||||
* @return a new plane
|
||||
*/
|
||||
public Plane rotate(final Vector3D center, final Rotation rotation) {
|
||||
|
||||
final Vector3D delta = origin.subtract(center);
|
||||
final Plane plane = new Plane(center.add(rotation.applyTo(delta)),
|
||||
rotation.applyTo(w));
|
||||
|
||||
// make sure the frame is transformed as desired
|
||||
plane.u = rotation.applyTo(u);
|
||||
plane.v = rotation.applyTo(v);
|
||||
|
||||
return plane;
|
||||
|
||||
}
|
||||
|
||||
/** Translate the plane by the specified amount.
|
||||
* <p>The instance is not modified, a new instance is created.</p>
|
||||
* @param translation translation to apply
|
||||
* @return a new plane
|
||||
*/
|
||||
public Plane translate(final Vector3D translation) {
|
||||
|
||||
final Plane plane = new Plane(origin.add(translation), w);
|
||||
|
||||
// make sure the frame is transformed as desired
|
||||
plane.u = u;
|
||||
plane.v = v;
|
||||
|
||||
return plane;
|
||||
|
||||
}
|
||||
|
||||
/** Get the intersection of a line with the instance.
|
||||
* @param line line intersecting the instance
|
||||
* @return intersection point between between the line and the
|
||||
* instance (null if the line is parallel to the instance)
|
||||
*/
|
||||
public Point3D intersection(final Line line) {
|
||||
final Vector3D direction = line.getDirection();
|
||||
final double dot = Vector3D.dotProduct(w, direction);
|
||||
if (FastMath.abs(dot) < 1.0e-10) {
|
||||
return null;
|
||||
}
|
||||
final Vector3D point = (Vector3D) line.toSpace(Point1D.ZERO);
|
||||
final double k = -(originOffset + Vector3D.dotProduct(w, point)) / dot;
|
||||
return new Point3D(1.0, point, k, direction);
|
||||
}
|
||||
|
||||
/** Build the line shared by the instance and another plane.
|
||||
* @param other other plane
|
||||
* @return line at the intersection of the instance and the
|
||||
* other plane (really a {@link Line Line} instance)
|
||||
*/
|
||||
public SubSpace intersection(final Hyperplane other) {
|
||||
final Plane otherP = (Plane) other;
|
||||
final Vector3D direction = Vector3D.crossProduct(w, otherP.w);
|
||||
if (direction.getNorm() < 1.0e-10) {
|
||||
return null;
|
||||
}
|
||||
return new Line(intersection(this, otherP, new Plane(direction)),
|
||||
direction);
|
||||
}
|
||||
|
||||
/** Get the intersection point of three planes.
|
||||
* @param plane1 first plane1
|
||||
* @param plane2 second plane2
|
||||
* @param plane3 third plane2
|
||||
* @return intersection point of three planes, null if some planes are parallel
|
||||
*/
|
||||
public static Vector3D intersection(final Plane plane1, final Plane plane2, final Plane plane3) {
|
||||
|
||||
// coefficients of the three planes linear equations
|
||||
final double a1 = plane1.w.getX();
|
||||
final double b1 = plane1.w.getY();
|
||||
final double c1 = plane1.w.getZ();
|
||||
final double d1 = plane1.originOffset;
|
||||
|
||||
final double a2 = plane2.w.getX();
|
||||
final double b2 = plane2.w.getY();
|
||||
final double c2 = plane2.w.getZ();
|
||||
final double d2 = plane2.originOffset;
|
||||
|
||||
final double a3 = plane3.w.getX();
|
||||
final double b3 = plane3.w.getY();
|
||||
final double c3 = plane3.w.getZ();
|
||||
final double d3 = plane3.originOffset;
|
||||
|
||||
// direct Cramer resolution of the linear system
|
||||
// (this is still feasible for a 3x3 system)
|
||||
final double a23 = b2 * c3 - b3 * c2;
|
||||
final double b23 = c2 * a3 - c3 * a2;
|
||||
final double c23 = a2 * b3 - a3 * b2;
|
||||
final double determinant = a1 * a23 + b1 * b23 + c1 * c23;
|
||||
if (FastMath.abs(determinant) < 1.0e-10) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final double r = 1.0 / determinant;
|
||||
return new Vector3D(
|
||||
(-a23 * d1 - (c1 * b3 - c3 * b1) * d2 - (c2 * b1 - c1 * b2) * d3) * r,
|
||||
(-b23 * d1 - (c3 * a1 - c1 * a3) * d2 - (c1 * a2 - c2 * a1) * d3) * r,
|
||||
(-c23 * d1 - (b1 * a3 - b3 * a1) * d2 - (b2 * a1 - b1 * a2) * d3) * r);
|
||||
|
||||
}
|
||||
|
||||
/** Build a region covering the whole hyperplane.
|
||||
* @return a region covering the whole hyperplane
|
||||
*/
|
||||
public Region wholeHyperplane() {
|
||||
return new PolygonsSet();
|
||||
}
|
||||
|
||||
/** Build a region covering the whole space.
|
||||
* @return a region containing the instance (really a {@link
|
||||
* PolyhedronsSet PolyhedronsSet} instance)
|
||||
*/
|
||||
public Region wholeSpace() {
|
||||
return new PolyhedronsSet();
|
||||
}
|
||||
|
||||
/** Check if the instance contains a point.
|
||||
* @param p point to check
|
||||
* @return true if p belongs to the plane
|
||||
*/
|
||||
public boolean contains(final Point3D p) {
|
||||
return FastMath.abs(getOffset(p)) < 1.0e-10;
|
||||
}
|
||||
|
||||
/** Get the offset (oriented distance) of a parallel plane.
|
||||
* <p>This method should be called only for parallel planes otherwise
|
||||
* the result is not meaningful.</p>
|
||||
* <p>The offset is 0 if both planes are the same, it is
|
||||
* positive if the plane is on the plus side of the instance and
|
||||
* negative if it is on the minus side, according to its natural
|
||||
* orientation.</p>
|
||||
* @param plane plane to check
|
||||
* @return offset of the plane
|
||||
*/
|
||||
public double getOffset(final Plane plane) {
|
||||
return originOffset + (sameOrientationAs(plane) ? -plane.originOffset : plane.originOffset);
|
||||
}
|
||||
|
||||
/** Get the offset (oriented distance) of a point.
|
||||
* <p>The offset is 0 if the point is on the underlying hyperplane,
|
||||
* it is positive if the point is on one particular side of the
|
||||
* hyperplane, and it is negative if the point is on the other side,
|
||||
* according to the hyperplane natural orientation.</p>
|
||||
* @param point point to check
|
||||
* @return offset of the point
|
||||
*/
|
||||
public double getOffset(final Point point) {
|
||||
return Vector3D.dotProduct((Vector3D) point, w) + originOffset;
|
||||
}
|
||||
|
||||
/** Check if the instance has the same orientation as another hyperplane.
|
||||
* @param other other hyperplane to check against the instance
|
||||
* @return true if the instance and the other hyperplane have
|
||||
* the same orientation
|
||||
*/
|
||||
public boolean sameOrientationAs(final Hyperplane other) {
|
||||
return Vector3D.dotProduct(((Plane) other).w, w) > 0.0;
|
||||
}
|
||||
|
||||
/** Compute the relative position of a sub-hyperplane with respect
|
||||
* to the instance.
|
||||
* @param sub sub-hyperplane to check
|
||||
* @return one of {@link org.apache.commons.math.geometry.partitioning.Hyperplane.Side#PLUS PLUS},
|
||||
* {@link org.apache.commons.math.geometry.partitioning.Hyperplane.Side#MINUS MINUS},
|
||||
* {@link org.apache.commons.math.geometry.partitioning.Hyperplane.Side#BOTH BOTH},
|
||||
* {@link org.apache.commons.math.geometry.partitioning.Hyperplane.Side#HYPER HYPER}
|
||||
*/
|
||||
public Side side(final SubHyperplane sub) {
|
||||
|
||||
final Plane otherPlane = (Plane) sub.getHyperplane();
|
||||
final Line inter = (Line) intersection(otherPlane);
|
||||
|
||||
if (inter == null) {
|
||||
// the hyperplanes are parallel,
|
||||
// any point can be used to check their relative position
|
||||
final double global = getOffset(otherPlane);
|
||||
return (global < -1.0e-10) ? Side.MINUS : ((global > 1.0e-10) ? Side.PLUS : Side.HYPER);
|
||||
}
|
||||
|
||||
// create a 2D line in the otherPlane canonical 2D frame such that:
|
||||
// - the line is the crossing line of the two planes in 3D
|
||||
// - the line splits the otherPlane in two half planes with an
|
||||
// orientation consistent with the orientation of the instance
|
||||
// (i.e. the 3D half space on the plus side (resp. minus side)
|
||||
// of the instance contains the 2D half plane on the plus side
|
||||
// (resp. minus side) of the 2D line
|
||||
Point2D p = (Point2D) otherPlane.toSubSpace(inter.toSpace(Point1D.ZERO));
|
||||
Point2D q = (Point2D) otherPlane.toSubSpace(inter.toSpace(Point1D.ONE));
|
||||
if (Vector3D.dotProduct(Vector3D.crossProduct(inter.getDirection(),
|
||||
otherPlane.getNormal()),
|
||||
w) < 0) {
|
||||
final Point2D tmp = p;
|
||||
p = q;
|
||||
q = tmp;
|
||||
}
|
||||
final Hyperplane line2D = new org.apache.commons.math.geometry.euclidean.twoD.Line(p, q);
|
||||
|
||||
// check the side on the 2D plane
|
||||
return sub.getRemainingRegion().side(line2D);
|
||||
|
||||
}
|
||||
|
||||
/** Split a sub-hyperplane in two parts by the instance.
|
||||
* @param sub sub-hyperplane to split
|
||||
* @return an object containing both the part of the sub-hyperplane
|
||||
* on the plus side of the instance and the part of the
|
||||
* sub-hyperplane on the minus side of the instance
|
||||
*/
|
||||
public SplitSubHyperplane split(final SubHyperplane sub) {
|
||||
|
||||
final Plane otherPlane = (Plane) sub.getHyperplane();
|
||||
final Line inter = (Line) intersection(otherPlane);
|
||||
|
||||
if (inter == null) {
|
||||
// the hyperplanes are parallel
|
||||
final double global = getOffset(otherPlane);
|
||||
return (global < -1.0e-10) ? new SplitSubHyperplane(null, sub) : new SplitSubHyperplane(sub, null);
|
||||
}
|
||||
|
||||
// the hyperplanes do intersect
|
||||
Point2D p = (Point2D) otherPlane.toSubSpace(inter.toSpace(Point1D.ZERO));
|
||||
Point2D q = (Point2D) otherPlane.toSubSpace(inter.toSpace(Point1D.ONE));
|
||||
if (Vector3D.dotProduct(Vector3D.crossProduct(inter.getDirection(),
|
||||
otherPlane.getNormal()),
|
||||
w) < 0) {
|
||||
final Point2D tmp = p;
|
||||
p = q;
|
||||
q = tmp;
|
||||
}
|
||||
final SubHyperplane l2DMinus =
|
||||
new SubHyperplane(new org.apache.commons.math.geometry.euclidean.twoD.Line(p, q));
|
||||
final SubHyperplane l2DPlus =
|
||||
new SubHyperplane(new org.apache.commons.math.geometry.euclidean.twoD.Line(q, p));
|
||||
|
||||
final BSPTree splitTree =
|
||||
sub.getRemainingRegion().getTree(false).split(l2DMinus);
|
||||
final BSPTree plusTree = Region.isEmpty(splitTree.getPlus()) ?
|
||||
new BSPTree(Boolean.FALSE) :
|
||||
new BSPTree(l2DPlus, new BSPTree(Boolean.FALSE),
|
||||
splitTree.getPlus(), null);
|
||||
|
||||
final BSPTree minusTree = Region.isEmpty(splitTree.getMinus()) ?
|
||||
new BSPTree(Boolean.FALSE) :
|
||||
new BSPTree(l2DMinus, new BSPTree(Boolean.FALSE),
|
||||
splitTree.getMinus(), null);
|
||||
|
||||
return new SplitSubHyperplane(new SubHyperplane(otherPlane.copySelf(),
|
||||
new PolygonsSet(plusTree)),
|
||||
new SubHyperplane(otherPlane.copySelf(),
|
||||
new PolygonsSet(minusTree)));
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,112 @@
|
|||
/*
|
||||
* 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.math.geometry.euclidean.threeD;
|
||||
|
||||
import org.apache.commons.math.geometry.partitioning.Point;
|
||||
|
||||
/** This class represents a 3D point.
|
||||
* <p>Instances of this class are guaranteed to be immutable.</p>
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public class Point3D extends Vector3D implements Point {
|
||||
|
||||
/** Point at undefined (NaN) coordinates. */
|
||||
public static final Point3D UNDEFINED = new Point3D(Double.NaN, Double.NaN, Double.NaN);
|
||||
|
||||
/** Serializable UID. */
|
||||
private static final long serialVersionUID = 9128130934224884451L;
|
||||
|
||||
/** Simple constructor.
|
||||
* Build a vector from its coordinates
|
||||
* @param x abscissa
|
||||
* @param y ordinate
|
||||
* @param z height
|
||||
* @see #getX()
|
||||
* @see #getY()
|
||||
* @see #getZ()
|
||||
*/
|
||||
public Point3D(final double x, final double y, final double z) {
|
||||
super(x, y, z);
|
||||
}
|
||||
|
||||
/** Simple constructor.
|
||||
* Build a vector from its azimuthal coordinates
|
||||
* @param alpha azimuth (α) around Z
|
||||
* (0 is +X, π/2 is +Y, π is -X and 3π/2 is -Y)
|
||||
* @param delta elevation (δ) above (XY) plane, from -π/2 to +π/2
|
||||
* @see #getAlpha()
|
||||
* @see #getDelta()
|
||||
*/
|
||||
public Point3D(final double alpha, final double delta) {
|
||||
super(alpha, delta);
|
||||
}
|
||||
|
||||
/** Multiplicative constructor
|
||||
* Build a vector from another one and a scale factor.
|
||||
* The vector built will be a * u
|
||||
* @param a scale factor
|
||||
* @param u base (unscaled) vector
|
||||
*/
|
||||
public Point3D(final double a, final Vector3D u) {
|
||||
super(a, u);
|
||||
}
|
||||
|
||||
/** Linear constructor
|
||||
* Build a vector from two other ones and corresponding scale factors.
|
||||
* The vector built will be a1 * u1 + a2 * u2
|
||||
* @param a1 first scale factor
|
||||
* @param u1 first base (unscaled) vector
|
||||
* @param a2 second scale factor
|
||||
* @param u2 second base (unscaled) vector
|
||||
*/
|
||||
public Point3D(final double a1, final Vector3D u1, final double a2, final Vector3D u2) {
|
||||
super(a1, u1, a2, u2);
|
||||
}
|
||||
|
||||
/** Linear constructor
|
||||
* Build a vector from three other ones and corresponding scale factors.
|
||||
* The vector built will be a1 * u1 + a2 * u2 + a3 * u3
|
||||
* @param a1 first scale factor
|
||||
* @param u1 first base (unscaled) vector
|
||||
* @param a2 second scale factor
|
||||
* @param u2 second base (unscaled) vector
|
||||
* @param a3 third scale factor
|
||||
* @param u3 third base (unscaled) vector
|
||||
*/
|
||||
public Point3D(final double a1, final Vector3D u1, final double a2, final Vector3D u2,
|
||||
final double a3, final Vector3D u3) {
|
||||
super(a1, u1, a2, u2, a3, u3);
|
||||
}
|
||||
|
||||
/** Linear constructor
|
||||
* Build a vector from four other ones and corresponding scale factors.
|
||||
* The vector built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4
|
||||
* @param a1 first scale factor
|
||||
* @param u1 first base (unscaled) vector
|
||||
* @param a2 second scale factor
|
||||
* @param u2 second base (unscaled) vector
|
||||
* @param a3 third scale factor
|
||||
* @param u3 third base (unscaled) vector
|
||||
* @param a4 fourth scale factor
|
||||
* @param u4 fourth base (unscaled) vector
|
||||
*/
|
||||
public Point3D(final double a1, final Vector3D u1, final double a2, final Vector3D u2,
|
||||
final double a3, final Vector3D u3, final double a4, final Vector3D u4) {
|
||||
super(a1, u1, a2, u2, a3, u3, a4, u4);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,413 @@
|
|||
/*
|
||||
* 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.math.geometry.euclidean.threeD;
|
||||
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.apache.commons.math.geometry.euclidean.twoD.Point2D;
|
||||
import org.apache.commons.math.geometry.partitioning.BSPTree;
|
||||
import org.apache.commons.math.geometry.partitioning.BSPTreeVisitor;
|
||||
import org.apache.commons.math.geometry.partitioning.Hyperplane;
|
||||
import org.apache.commons.math.geometry.partitioning.Point;
|
||||
import org.apache.commons.math.geometry.partitioning.Region;
|
||||
import org.apache.commons.math.geometry.partitioning.SubHyperplane;
|
||||
import org.apache.commons.math.geometry.partitioning.Transform;
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
|
||||
/** This class represents a 3D region: a set of polyhedrons.
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public class PolyhedronsSet extends Region {
|
||||
|
||||
/** Build a polyhedrons set representing the whole real line.
|
||||
*/
|
||||
public PolyhedronsSet() {
|
||||
super();
|
||||
}
|
||||
|
||||
/** Build a polyhedrons set from a BSP tree.
|
||||
* <p>The leaf nodes of the BSP tree <em>must</em> have a
|
||||
* {@code Boolean} attribute representing the inside status of
|
||||
* the corresponding cell (true for inside cells, false for outside
|
||||
* cells). In order to avoid building too many small objects, it is
|
||||
* recommended to use the predefined constants
|
||||
* {@code Boolean.TRUE} and {@code Boolean.FALSE}</p>
|
||||
* @param tree inside/outside BSP tree representing the region
|
||||
*/
|
||||
public PolyhedronsSet(final BSPTree tree) {
|
||||
super(tree);
|
||||
}
|
||||
|
||||
/** Build a polyhedrons set from a Boundary REPresentation (B-rep).
|
||||
* <p>The boundary is provided as a collection of {@link
|
||||
* SubHyperplane sub-hyperplanes}. Each sub-hyperplane has the
|
||||
* interior part of the region on its minus side and the exterior on
|
||||
* its plus side.</p>
|
||||
* <p>The boundary elements can be in any order, and can form
|
||||
* several non-connected sets (like for example polyhedrons with holes
|
||||
* or a set of disjoints polyhedrons considered as a whole). In
|
||||
* fact, the elements do not even need to be connected together
|
||||
* (their topological connections are not used here). However, if the
|
||||
* boundary does not really separate an inside open from an outside
|
||||
* open (open having here its topological meaning), then subsequent
|
||||
* calls to the {@link Region#checkPoint(Point) checkPoint} method will
|
||||
* not be meaningful anymore.</p>
|
||||
* <p>If the boundary is empty, the region will represent the whole
|
||||
* space.</p>
|
||||
* @param boundary collection of boundary elements, as a
|
||||
* collection of {@link SubHyperplane SubHyperplane} objects
|
||||
*/
|
||||
public PolyhedronsSet(final Collection<SubHyperplane> boundary) {
|
||||
super(boundary);
|
||||
}
|
||||
|
||||
/** Build a parallellepipedic box.
|
||||
* @param xMin low bound along the x direction
|
||||
* @param xMax high bound along the x direction
|
||||
* @param yMin low bound along the y direction
|
||||
* @param yMax high bound along the y direction
|
||||
* @param zMin low bound along the z direction
|
||||
* @param zMax high bound along the z direction
|
||||
*/
|
||||
public PolyhedronsSet(final double xMin, final double xMax,
|
||||
final double yMin, final double yMax,
|
||||
final double zMin, final double zMax) {
|
||||
this(buildConvex(Arrays.asList(new Hyperplane[] {
|
||||
new Plane(new Vector3D(xMin, 0, 0), Vector3D.MINUS_I),
|
||||
new Plane(new Vector3D(xMax, 0, 0), Vector3D.PLUS_I),
|
||||
new Plane(new Vector3D(0, yMin, 0), Vector3D.MINUS_J),
|
||||
new Plane(new Vector3D(0, yMax, 0), Vector3D.PLUS_J),
|
||||
new Plane(new Vector3D(0, 0, zMin), Vector3D.MINUS_K),
|
||||
new Plane(new Vector3D(0, 0, zMax), Vector3D.PLUS_K)
|
||||
})).getTree(false));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public Region buildNew(final BSPTree tree) {
|
||||
return new PolyhedronsSet(tree);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
protected void computeGeometricalProperties() {
|
||||
|
||||
// compute the contribution of all boundary facets
|
||||
getTree(true).visit(new FacetsContributionVisitor());
|
||||
|
||||
if (getSize() < 0) {
|
||||
// the polyhedrons set as a finite outside
|
||||
// surrounded by an infinite inside
|
||||
setSize(Double.POSITIVE_INFINITY);
|
||||
setBarycenter(Point3D.UNDEFINED);
|
||||
} else {
|
||||
// the polyhedrons set is finite, apply the remaining scaling factors
|
||||
setSize(getSize() / 3.0);
|
||||
setBarycenter(new Point3D(1.0 / (4 * getSize()), (Vector3D) getBarycenter()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** Visitor computing geometrical properties. */
|
||||
private class FacetsContributionVisitor implements BSPTreeVisitor {
|
||||
|
||||
/** Simple constructor. */
|
||||
public FacetsContributionVisitor() {
|
||||
setSize(0);
|
||||
setBarycenter(new Point3D(0, 0, 0));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public Order visitOrder(final BSPTree node) {
|
||||
return Order.MINUS_SUB_PLUS;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void visitInternalNode(final BSPTree node) {
|
||||
final BoundaryAttribute attribute = (BoundaryAttribute) node.getAttribute();
|
||||
if (attribute.getPlusOutside() != null) {
|
||||
addContribution(attribute.getPlusOutside(), false);
|
||||
}
|
||||
if (attribute.getPlusInside() != null) {
|
||||
addContribution(attribute.getPlusInside(), true);
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void visitLeafNode(final BSPTree node) {
|
||||
}
|
||||
|
||||
/** Add he contribution of a boundary facet.
|
||||
* @param facet boundary facet
|
||||
* @param reversed if true, the facet has the inside on its plus side
|
||||
*/
|
||||
private void addContribution(final SubHyperplane facet, final boolean reversed) {
|
||||
|
||||
final Region polygon = facet.getRemainingRegion();
|
||||
final double area = polygon.getSize();
|
||||
|
||||
if (Double.isInfinite(area)) {
|
||||
setSize(Double.POSITIVE_INFINITY);
|
||||
setBarycenter(Point3D.UNDEFINED);
|
||||
} else {
|
||||
|
||||
final Plane plane = (Plane) facet.getHyperplane();
|
||||
final Vector3D facetB = (Point3D) plane.toSpace(polygon.getBarycenter());
|
||||
double scaled = area * Vector3D.dotProduct(facetB, plane.getNormal());
|
||||
if (reversed) {
|
||||
scaled = -scaled;
|
||||
}
|
||||
|
||||
setSize(getSize() + scaled);
|
||||
setBarycenter(new Point3D(1.0, (Point3D) getBarycenter(), scaled, facetB));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** Get the first sub-hyperplane crossed by a semi-infinite line.
|
||||
* @param point start point of the part of the line considered
|
||||
* @param line line to consider (contains point)
|
||||
* @return the first sub-hyperplaned crossed by the line after the
|
||||
* given point, or null if the line does not intersect any
|
||||
* sub-hyperplaned
|
||||
*/
|
||||
public SubHyperplane firstIntersection(final Vector3D point, final Line line) {
|
||||
return recurseFirstIntersection(getTree(true), point, line);
|
||||
}
|
||||
|
||||
/** Get the first sub-hyperplane crossed by a semi-infinite line.
|
||||
* @param node current node
|
||||
* @param point start point of the part of the line considered
|
||||
* @param line line to consider (contains point)
|
||||
* @return the first sub-hyperplaned crossed by the line after the
|
||||
* given point, or null if the line does not intersect any
|
||||
* sub-hyperplaned
|
||||
*/
|
||||
private SubHyperplane recurseFirstIntersection(final BSPTree node,
|
||||
final Vector3D point,
|
||||
final Line line) {
|
||||
|
||||
final SubHyperplane cut = node.getCut();
|
||||
if (cut == null) {
|
||||
return null;
|
||||
}
|
||||
final BSPTree minus = node.getMinus();
|
||||
final BSPTree plus = node.getPlus();
|
||||
final Plane plane = (Plane) cut.getHyperplane();
|
||||
|
||||
// establish search order
|
||||
final double offset = plane.getOffset((Point) point);
|
||||
final boolean in = FastMath.abs(offset) < 1.0e-10;
|
||||
final BSPTree near;
|
||||
final BSPTree far;
|
||||
if (offset < 0) {
|
||||
near = minus;
|
||||
far = plus;
|
||||
} else {
|
||||
near = plus;
|
||||
far = minus;
|
||||
}
|
||||
|
||||
if (in) {
|
||||
// search in the cut hyperplane
|
||||
final SubHyperplane facet = boundaryFacet(point, node);
|
||||
if (facet != null) {
|
||||
return facet;
|
||||
}
|
||||
}
|
||||
|
||||
// search in the near branch
|
||||
final SubHyperplane crossed = recurseFirstIntersection(near, point, line);
|
||||
if (crossed != null) {
|
||||
return crossed;
|
||||
}
|
||||
|
||||
if (!in) {
|
||||
// search in the cut hyperplane
|
||||
final Vector3D hit3D = plane.intersection(line);
|
||||
if (hit3D != null) {
|
||||
final SubHyperplane facet = boundaryFacet(hit3D, node);
|
||||
if (facet != null) {
|
||||
return facet;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// search in the far branch
|
||||
return recurseFirstIntersection(far, point, line);
|
||||
|
||||
}
|
||||
|
||||
/** Check if a point belongs to the boundary part of a node.
|
||||
* @param point point to check
|
||||
* @param node node containing the boundary facet to check
|
||||
* @return the boundary facet this points belongs to (or null if it
|
||||
* does not belong to any boundary facet)
|
||||
*/
|
||||
private SubHyperplane boundaryFacet(final Vector3D point, final BSPTree node) {
|
||||
final Point point2D = node.getCut().getHyperplane().toSubSpace((Point) point);
|
||||
final BoundaryAttribute attribute = (BoundaryAttribute) node.getAttribute();
|
||||
if ((attribute.getPlusOutside() != null) &&
|
||||
(attribute.getPlusOutside().getRemainingRegion().checkPoint(point2D) == Location.INSIDE)) {
|
||||
return attribute.getPlusOutside();
|
||||
}
|
||||
if ((attribute.getPlusInside() != null) &&
|
||||
(attribute.getPlusInside().getRemainingRegion().checkPoint(point2D) == Location.INSIDE)) {
|
||||
return attribute.getPlusInside();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Rotate the region around the specified point.
|
||||
* <p>The instance is not modified, a new instance is created.</p>
|
||||
* @param center rotation center
|
||||
* @param rotation vectorial rotation operator
|
||||
* @return a new instance representing the rotated region
|
||||
*/
|
||||
public PolyhedronsSet rotate(final Vector3D center, final Rotation rotation) {
|
||||
return (PolyhedronsSet) applyTransform(new RotationTransform(center, rotation));
|
||||
}
|
||||
|
||||
/** 3D rotation as a Transform. */
|
||||
private static class RotationTransform implements Transform {
|
||||
|
||||
/** Center point of the rotation. */
|
||||
private Vector3D center;
|
||||
|
||||
/** Vectorial rotation. */
|
||||
private Rotation rotation;
|
||||
|
||||
/** Cached original hyperplane. */
|
||||
private Hyperplane cachedOriginal;
|
||||
|
||||
/** Cached 2D transform valid inside the cached original hyperplane. */
|
||||
private Transform cachedTransform;
|
||||
|
||||
/** Build a rotation transform.
|
||||
* @param center center point of the rotation
|
||||
* @param rotation vectorial rotation
|
||||
*/
|
||||
public RotationTransform(final Vector3D center, final Rotation rotation) {
|
||||
this.center = center;
|
||||
this.rotation = rotation;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public Point apply(final Point point) {
|
||||
final Vector3D delta = ((Vector3D) point).subtract(center);
|
||||
return new Point3D(1.0, center, 1.0, rotation.applyTo(delta));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public Hyperplane apply(final Hyperplane hyperplane) {
|
||||
return ((Plane) hyperplane).rotate(center, rotation);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public SubHyperplane apply(final SubHyperplane sub,
|
||||
final Hyperplane original, final Hyperplane transformed) {
|
||||
if (original != cachedOriginal) {
|
||||
// we have changed hyperplane, reset the in-hyperplane transform
|
||||
|
||||
final Plane oPlane = (Plane) original;
|
||||
final Plane tPlane = (Plane) transformed;
|
||||
final Vector3D p00 = oPlane.getOrigin();
|
||||
final Vector3D p10 = (Vector3D) oPlane.toSpace(new Point2D(1.0, 0.0));
|
||||
final Vector3D p01 = (Vector3D) oPlane.toSpace(new Point2D(0.0, 1.0));
|
||||
final Point2D tP00 = (Point2D) tPlane.toSubSpace(apply((Point) p00));
|
||||
final Point2D tP10 = (Point2D) tPlane.toSubSpace(apply((Point) p10));
|
||||
final Point2D tP01 = (Point2D) tPlane.toSubSpace(apply((Point) p01));
|
||||
final AffineTransform at =
|
||||
new AffineTransform(tP10.getX() - tP00.getX(), tP10.getY() - tP00.getY(),
|
||||
tP01.getX() - tP00.getX(), tP01.getY() - tP00.getY(),
|
||||
tP00.getX(), tP00.getY());
|
||||
|
||||
cachedOriginal = original;
|
||||
cachedTransform = org.apache.commons.math.geometry.euclidean.twoD.Line.getTransform(at);
|
||||
|
||||
}
|
||||
return sub.applyTransform(cachedTransform);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** Translate the region by the specified amount.
|
||||
* <p>The instance is not modified, a new instance is created.</p>
|
||||
* @param translation translation to apply
|
||||
* @return a new instance representing the translated region
|
||||
*/
|
||||
public PolyhedronsSet translate(final Vector3D translation) {
|
||||
return (PolyhedronsSet) applyTransform(new TranslationTransform(translation));
|
||||
}
|
||||
|
||||
/** 3D translation as a transform. */
|
||||
private static class TranslationTransform implements Transform {
|
||||
|
||||
/** Translation vector. */
|
||||
private Vector3D translation;
|
||||
|
||||
/** Cached original hyperplane. */
|
||||
private Hyperplane cachedOriginal;
|
||||
|
||||
/** Cached 2D transform valid inside the cached original hyperplane. */
|
||||
private Transform cachedTransform;
|
||||
|
||||
/** Build a translation transform.
|
||||
* @param translation translation vector
|
||||
*/
|
||||
public TranslationTransform(final Vector3D translation) {
|
||||
this.translation = translation;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public Point apply(final Point point) {
|
||||
return new Point3D(1.0, (Vector3D) point, 1.0, translation);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public Hyperplane apply(final Hyperplane hyperplane) {
|
||||
return ((Plane) hyperplane).translate(translation);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public SubHyperplane apply(final SubHyperplane sub,
|
||||
final Hyperplane original, final Hyperplane transformed) {
|
||||
if (original != cachedOriginal) {
|
||||
// we have changed hyperplane, reset the in-hyperplane transform
|
||||
|
||||
final Plane oPlane = (Plane) original;
|
||||
final Plane tPlane = (Plane) transformed;
|
||||
final Point2D shift = (Point2D) tPlane.toSubSpace(apply((Point) oPlane.getOrigin()));
|
||||
final AffineTransform at =
|
||||
AffineTransform.getTranslateInstance(shift.getX(), shift.getY());
|
||||
|
||||
cachedOriginal = original;
|
||||
cachedTransform =
|
||||
org.apache.commons.math.geometry.euclidean.twoD.Line.getTransform(at);
|
||||
|
||||
}
|
||||
|
||||
return sub.applyTransform(cachedTransform);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -15,7 +15,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math.geometry;
|
||||
package org.apache.commons.math.geometry.euclidean.threeD;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
|
@ -15,7 +15,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math.geometry;
|
||||
package org.apache.commons.math.geometry.euclidean.threeD;
|
||||
|
||||
/**
|
||||
* This class is a utility representing a rotation order specification
|
|
@ -15,7 +15,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math.geometry;
|
||||
package org.apache.commons.math.geometry.euclidean.threeD;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
|
@ -15,7 +15,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math.geometry;
|
||||
package org.apache.commons.math.geometry.euclidean.threeD;
|
||||
|
||||
import java.text.FieldPosition;
|
||||
import java.text.NumberFormat;
|
|
@ -0,0 +1,512 @@
|
|||
/*
|
||||
* 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.math.geometry.euclidean.twoD;
|
||||
|
||||
import java.awt.geom.AffineTransform;
|
||||
|
||||
import org.apache.commons.math.exception.MathIllegalArgumentException;
|
||||
import org.apache.commons.math.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math.geometry.euclidean.oneD.IntervalsSet;
|
||||
import org.apache.commons.math.geometry.euclidean.oneD.OrientedPoint;
|
||||
import org.apache.commons.math.geometry.euclidean.oneD.Point1D;
|
||||
import org.apache.commons.math.geometry.partitioning.BSPTree;
|
||||
import org.apache.commons.math.geometry.partitioning.Hyperplane;
|
||||
import org.apache.commons.math.geometry.partitioning.Point;
|
||||
import org.apache.commons.math.geometry.partitioning.Region;
|
||||
import org.apache.commons.math.geometry.partitioning.SubHyperplane;
|
||||
import org.apache.commons.math.geometry.partitioning.SubSpace;
|
||||
import org.apache.commons.math.geometry.partitioning.Transform;
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
import org.apache.commons.math.util.MathUtils;
|
||||
|
||||
/** This class represents an oriented line in the 2D plane.
|
||||
|
||||
* <p>An oriented line can be defined either by prolongating a line
|
||||
* segment between two points past these points, or by one point and
|
||||
* an angular direction (in trigonometric orientation).</p>
|
||||
|
||||
* <p>Since it is oriented the two half planes at its two sides are
|
||||
* unambiguously identified as a left half plane and a right half
|
||||
* plane. This can be used to identify the interior and the exterior
|
||||
* in a simple way by local properties only when part of a line is
|
||||
* used to define part of a polygon boundary.</p>
|
||||
|
||||
* <p>A line can also be used to completely define a reference frame
|
||||
* in the plane. It is sufficient to select one specific point in the
|
||||
* line (the orthogonal projection of the original reference frame on
|
||||
* the line) and to use the unit vector in the line direction and the
|
||||
* orthogonal vector oriented from left half plane to right half
|
||||
* plane. We define two coordinates by the process, the
|
||||
* <em>abscissa</em> along the line, and the <em>offset</em> across
|
||||
* the line. All points of the plane are uniquely identified by these
|
||||
* two coordinates. The line is the set of points at zero offset, the
|
||||
* left half plane is the set of points with negative offsets and the
|
||||
* right half plane is the set of points with positive offsets.</p>
|
||||
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public class Line implements Hyperplane {
|
||||
|
||||
/** Angle with respect to the abscissa axis. */
|
||||
private double angle;
|
||||
|
||||
/** Cosine of the line angle. */
|
||||
private double cos;
|
||||
|
||||
/** Sine of the line angle. */
|
||||
private double sin;
|
||||
|
||||
/** Offset of the frame origin. */
|
||||
private double originOffset;
|
||||
|
||||
/** Build a line from two points.
|
||||
* <p>The line is oriented from p1 to p2</p>
|
||||
* @param p1 first point
|
||||
* @param p2 second point
|
||||
*/
|
||||
public Line(final Point2D p1, final Point2D p2) {
|
||||
reset(p1, p2);
|
||||
}
|
||||
|
||||
/** Build a line from a point and an angle.
|
||||
* @param p point belonging to the line
|
||||
* @param angle angle of the line with respect to abscissa axis
|
||||
*/
|
||||
public Line(final Point2D p, final double angle) {
|
||||
reset(p, angle);
|
||||
}
|
||||
|
||||
/** Build a line from its internal characteristics.
|
||||
* @param angle angle of the line with respect to abscissa axis
|
||||
* @param cos cosine of the angle
|
||||
* @param sin sine of the angle
|
||||
* @param originOffset offset of the origin
|
||||
*/
|
||||
private Line(final double angle, final double cos, final double sin, final double originOffset) {
|
||||
this.angle = angle;
|
||||
this.cos = cos;
|
||||
this.sin = sin;
|
||||
this.originOffset = originOffset;
|
||||
}
|
||||
|
||||
/** Copy constructor.
|
||||
* <p>The created instance is completely independant from the
|
||||
* original instance, it is a deep copy.</p>
|
||||
* @param line line to copy
|
||||
*/
|
||||
public Line(final Line line) {
|
||||
angle = MathUtils.normalizeAngle(line.angle, FastMath.PI);
|
||||
cos = FastMath.cos(angle);
|
||||
sin = FastMath.sin(angle);
|
||||
originOffset = line.originOffset;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public Hyperplane copySelf() {
|
||||
return new Line(this);
|
||||
}
|
||||
|
||||
/** Reset the instance as if built from two points.
|
||||
* <p>The line is oriented from p1 to p2</p>
|
||||
* @param p1 first point
|
||||
* @param p2 second point
|
||||
*/
|
||||
public void reset(final Point2D p1, final Point2D p2) {
|
||||
final double dx = p2.x - p1.x;
|
||||
final double dy = p2.y - p1.y;
|
||||
final double d = FastMath.hypot(dx, dy);
|
||||
if (d == 0.0) {
|
||||
angle = 0.0;
|
||||
cos = 1.0;
|
||||
sin = 0.0;
|
||||
originOffset = p1.y;
|
||||
} else {
|
||||
angle = FastMath.PI + FastMath.atan2(-dy, -dx);
|
||||
cos = FastMath.cos(angle);
|
||||
sin = FastMath.sin(angle);
|
||||
originOffset = (p2.x * p1.y - p1.x * p2.y) / d;
|
||||
}
|
||||
}
|
||||
|
||||
/** Reset the instance as if built from a line and an angle.
|
||||
* @param p point belonging to the line
|
||||
* @param alpha angle of the line with respect to abscissa axis
|
||||
*/
|
||||
public void reset(final Point2D p, final double alpha) {
|
||||
this.angle = MathUtils.normalizeAngle(alpha, FastMath.PI);
|
||||
cos = FastMath.cos(this.angle);
|
||||
sin = FastMath.sin(this.angle);
|
||||
originOffset = cos * p.y - sin * p.x;
|
||||
}
|
||||
|
||||
/** Revert the instance.
|
||||
*/
|
||||
public void revertSelf() {
|
||||
if (angle < FastMath.PI) {
|
||||
angle += FastMath.PI;
|
||||
} else {
|
||||
angle -= FastMath.PI;
|
||||
}
|
||||
cos = -cos;
|
||||
sin = -sin;
|
||||
originOffset = -originOffset;
|
||||
}
|
||||
|
||||
/** Get the reverse of the instance.
|
||||
* <p>Get a line with reversed orientation with respect to the
|
||||
* instance. A new object is built, the instance is untouched.</p>
|
||||
* @return a new line, with orientation opposite to the instance orientation
|
||||
*/
|
||||
public Line getReverse() {
|
||||
return new Line((angle < FastMath.PI) ? (angle + FastMath.PI) : (angle - FastMath.PI),
|
||||
-cos, -sin, -originOffset);
|
||||
}
|
||||
|
||||
/** Transform a 2D space point into a line point.
|
||||
* @param point 2D point (must be a {@link Point2D Point2D}
|
||||
* instance)
|
||||
* @return line point corresponding to the 2D point (really a {@link
|
||||
* org.apache.commons.math.geometry.euclidean.oneD.Point1D Point1D} instance)
|
||||
* @see #toSpace
|
||||
*/
|
||||
public Point toSubSpace(final Point point) {
|
||||
final Point2D p2D = (Point2D) point;
|
||||
return new Point1D(cos * p2D.x + sin * p2D.y);
|
||||
}
|
||||
|
||||
/** Get one point from the line.
|
||||
* @param point desired abscissa for the point (must be a {@link
|
||||
* org.apache.commons.math.geometry.euclidean.oneD.Point1D Point1D} instance)
|
||||
* @return line point at specified abscissa (really a {@link Point2D
|
||||
* Point2D} instance)
|
||||
*/
|
||||
public Point toSpace(final Point point) {
|
||||
final double abscissa = ((Point1D) point).getAbscissa();
|
||||
return new Point2D(abscissa * cos - originOffset * sin,
|
||||
abscissa * sin + originOffset * cos);
|
||||
}
|
||||
|
||||
/** Get the intersection point of the instance and another line.
|
||||
* @param other other line
|
||||
* @return intersection point of the instance and the other line
|
||||
* (really a {@link Point2D Point2D} instance)
|
||||
*/
|
||||
public SubSpace intersection(final Hyperplane other) {
|
||||
final Line otherL = (Line) other;
|
||||
final double d = sin * otherL.cos - otherL.sin * cos;
|
||||
if (FastMath.abs(d) < 1.0e-10) {
|
||||
return null;
|
||||
}
|
||||
return new Point2D((cos * otherL.originOffset - otherL.cos * originOffset) / d,
|
||||
(sin * otherL.originOffset - otherL.sin * originOffset) / d);
|
||||
}
|
||||
|
||||
/** Build a region covering the whole hyperplane.
|
||||
* @return a region covering the whole hyperplane
|
||||
*/
|
||||
public Region wholeHyperplane() {
|
||||
return new IntervalsSet();
|
||||
}
|
||||
|
||||
/** Build a region covering the whole space.
|
||||
* @return a region containing the instance (really a {@link
|
||||
* PolygonsSet PolygonsSet} instance)
|
||||
*/
|
||||
public Region wholeSpace() {
|
||||
return new PolygonsSet();
|
||||
}
|
||||
|
||||
/** Get the offset (oriented distance) of a parallel line.
|
||||
* <p>This method should be called only for parallel lines otherwise
|
||||
* the result is not meaningful.</p>
|
||||
* <p>The offset is 0 if both lines are the same, it is
|
||||
* positive if the line is on the right side of the instance and
|
||||
* negative if it is on the left side, according to its natural
|
||||
* orientation.</p>
|
||||
* @param line line to check
|
||||
* @return offset of the line
|
||||
*/
|
||||
public double getOffset(final Line line) {
|
||||
return originOffset +
|
||||
((cos * line.cos + sin * line.sin > 0) ? -line.originOffset : line.originOffset);
|
||||
}
|
||||
|
||||
/** Get the offset (oriented distance) of a point to the line.
|
||||
* <p>The offset is 0 if the point belongs to the line, it is
|
||||
* positive if the point is on the right side of the line and
|
||||
* negative if it is on the left side, according to its natural
|
||||
* orientation.</p>
|
||||
* @param point point to check (must be a {@link Point2D Point2D} instance)
|
||||
* @return offset of the point
|
||||
*/
|
||||
public double getOffset(final Point point) {
|
||||
final Point2D p2D = (Point2D) point;
|
||||
return sin * p2D.x - cos * p2D.y + originOffset;
|
||||
}
|
||||
|
||||
/** Check if the instance has the same orientation as another hyperplane.
|
||||
* <p>This method is expected to be called on parallel hyperplanes
|
||||
* (i.e. when the {@link #side side} method would return {@link
|
||||
* org.apache.commons.math.geometry.partitioning.Hyperplane.Side#HYPER HYPER}
|
||||
* for some sub-hyperplane having the specified hyperplane
|
||||
* as its underlying hyperplane). The method should <em>not</em>
|
||||
* re-check for parallelism, only for orientation, typically by
|
||||
* testing something like the sign of the dot-products of
|
||||
* normals.</p>
|
||||
* @param other other hyperplane to check against the instance
|
||||
* @return true if the instance and the other hyperplane have
|
||||
* the same orientation
|
||||
*/
|
||||
public boolean sameOrientationAs(final Hyperplane other) {
|
||||
final Line otherL = (Line) other;
|
||||
return (sin * otherL.sin + cos * otherL.cos) >= 0.0;
|
||||
}
|
||||
|
||||
/** Get one point from the plane.
|
||||
* @param abscissa desired abscissa for the point
|
||||
* @param offset desired offset for the point
|
||||
* @return one point in the plane, with given abscissa and offset
|
||||
* relative to the line
|
||||
*/
|
||||
public Point2D getPointAt(final Point1D abscissa, final double offset) {
|
||||
final double x = abscissa.getAbscissa();
|
||||
final double dOffset = offset - originOffset;
|
||||
return new Point2D(x * cos + dOffset * sin, x * sin - dOffset * cos);
|
||||
}
|
||||
|
||||
/** Check if the line contains a point.
|
||||
* @param p point to check
|
||||
* @return true if p belongs to the line
|
||||
*/
|
||||
public boolean contains(final Point2D p) {
|
||||
return FastMath.abs(getOffset(p)) < 1.0e-10;
|
||||
}
|
||||
|
||||
/** Check the instance is parallel to another line.
|
||||
* @param line other line to check
|
||||
* @return true if the instance is parallel to the other line
|
||||
* (they can have either the same or opposite orientations)
|
||||
*/
|
||||
public boolean isParallelTo(final Line line) {
|
||||
return FastMath.abs(sin * line.cos - cos * line.sin) < 1.0e-10;
|
||||
}
|
||||
|
||||
/** Translate the line to force it passing by a point.
|
||||
* @param p point by which the line should pass
|
||||
*/
|
||||
public void translateToPoint(final Point2D p) {
|
||||
originOffset = cos * p.y - sin * p.x;
|
||||
}
|
||||
|
||||
/** Get the angle of the line.
|
||||
* @return the angle of the line with respect to the abscissa axis
|
||||
*/
|
||||
public double getAngle() {
|
||||
return MathUtils.normalizeAngle(angle, FastMath.PI);
|
||||
}
|
||||
|
||||
/** Set the angle of the line.
|
||||
* @param angle new angle of the line with respect to the abscissa axis
|
||||
*/
|
||||
public void setAngle(final double angle) {
|
||||
this.angle = MathUtils.normalizeAngle(angle, FastMath.PI);
|
||||
cos = FastMath.cos(this.angle);
|
||||
sin = FastMath.sin(this.angle);
|
||||
}
|
||||
|
||||
/** Get the offset of the origin.
|
||||
* @return the offset of the origin
|
||||
*/
|
||||
public double getOriginOffset() {
|
||||
return originOffset;
|
||||
}
|
||||
|
||||
/** Set the offset of the origin.
|
||||
* @param offset offset of the origin
|
||||
*/
|
||||
public void setOriginOffset(final double offset) {
|
||||
originOffset = offset;
|
||||
}
|
||||
|
||||
/** Compute the relative position of a sub-hyperplane with respect
|
||||
* to the instance.
|
||||
* @param sub sub-hyperplane to check
|
||||
* @return one of {@link org.apache.commons.math.geometry.partitioning.Hyperplane.Side#PLUS PLUS},
|
||||
* {@link org.apache.commons.math.geometry.partitioning.Hyperplane.Side#MINUS MINUS},
|
||||
* {@link org.apache.commons.math.geometry.partitioning.Hyperplane.Side#BOTH BOTH},
|
||||
* {@link org.apache.commons.math.geometry.partitioning.Hyperplane.Side#HYPER HYPER}
|
||||
*/
|
||||
public Side side(final SubHyperplane sub) {
|
||||
|
||||
final Hyperplane otherHyp = sub.getHyperplane();
|
||||
final Point2D crossing = (Point2D) intersection(otherHyp);
|
||||
|
||||
if (crossing == null) {
|
||||
// the lines are parallel,
|
||||
final double global = getOffset((Line) otherHyp);
|
||||
return (global < -1.0e-10) ? Side.MINUS : ((global > 1.0e-10) ? Side.PLUS : Side.HYPER);
|
||||
}
|
||||
|
||||
// the lines do intersect
|
||||
final boolean direct = FastMath.sin(((Line) otherHyp).angle - angle) < 0;
|
||||
final Point1D x = (Point1D) otherHyp.toSubSpace(crossing);
|
||||
return sub.getRemainingRegion().side(new OrientedPoint(x, direct));
|
||||
|
||||
}
|
||||
|
||||
/** Split a sub-hyperplane in two parts by the instance.
|
||||
* @param sub sub-hyperplane to split
|
||||
* @return an object containing both the part of the sub-hyperplane
|
||||
* on the plus side of the instance and the part of the
|
||||
* sub-hyperplane on the minus side of the instance
|
||||
*/
|
||||
public SplitSubHyperplane split(final SubHyperplane sub) {
|
||||
|
||||
final Line otherLine = (Line) sub.getHyperplane();
|
||||
final Point2D crossing = (Point2D) intersection(otherLine);
|
||||
|
||||
if (crossing == null) {
|
||||
// the lines are parallel
|
||||
final double global = getOffset(otherLine);
|
||||
return (global < -1.0e-10) ?
|
||||
new SplitSubHyperplane(null, sub) :
|
||||
new SplitSubHyperplane(sub, null);
|
||||
}
|
||||
|
||||
// the lines do intersect
|
||||
final boolean direct = FastMath.sin(otherLine.angle - angle) < 0;
|
||||
final Point1D x = (Point1D) otherLine.toSubSpace(crossing);
|
||||
final SubHyperplane subPlus = new SubHyperplane(new OrientedPoint(x, !direct));
|
||||
final SubHyperplane subMinus = new SubHyperplane(new OrientedPoint(x, direct));
|
||||
|
||||
final BSPTree splitTree =
|
||||
sub.getRemainingRegion().getTree(false).split(subMinus);
|
||||
final BSPTree plusTree = Region.isEmpty(splitTree.getPlus()) ?
|
||||
new BSPTree(Boolean.FALSE) :
|
||||
new BSPTree(subPlus, new BSPTree(Boolean.FALSE),
|
||||
splitTree.getPlus(), null);
|
||||
final BSPTree minusTree = Region.isEmpty(splitTree.getMinus()) ?
|
||||
new BSPTree(Boolean.FALSE) :
|
||||
new BSPTree(subMinus, new BSPTree(Boolean.FALSE),
|
||||
splitTree.getMinus(), null);
|
||||
|
||||
return new SplitSubHyperplane(new SubHyperplane(otherLine.copySelf(),
|
||||
new IntervalsSet(plusTree)),
|
||||
new SubHyperplane(otherLine.copySelf(),
|
||||
new IntervalsSet(minusTree)));
|
||||
|
||||
}
|
||||
|
||||
/** Get a {@link org.apache.commons.math.geometry.partitioning.Transform
|
||||
* Transform} embedding an affine transform.
|
||||
* @param transform affine transform to embed (must be inversible
|
||||
* otherwise the {@link
|
||||
* org.apache.commons.math.geometry.partitioning.Transform#apply(Hyperplane)
|
||||
* apply(Hyperplane)} method would work only for some lines, and
|
||||
* fail for other ones)
|
||||
* @return a new transform that can be applied to either {@link
|
||||
* Point2D Point2D}, {@link Line Line} or {@link
|
||||
* org.apache.commons.math.geometry.partitioning.SubHyperplane
|
||||
* SubHyperplane} instances
|
||||
* @exception MathIllegalArgumentException if the transform is non invertible
|
||||
*/
|
||||
public static Transform getTransform(final AffineTransform transform) throws MathIllegalArgumentException {
|
||||
return new LineTransform(transform);
|
||||
}
|
||||
|
||||
/** Class embedding an affine transform.
|
||||
* <p>This class is used in order to apply an affine transform to a
|
||||
* line. Using a specific object allow to perform some computations
|
||||
* on the transform only once even if the same transform is to be
|
||||
* applied to a large number of lines (for example to a large
|
||||
* polygon)./<p>
|
||||
*/
|
||||
private static class LineTransform implements Transform {
|
||||
|
||||
// CHECKSTYLE: stop JavadocVariable check
|
||||
private double cXX;
|
||||
private double cXY;
|
||||
private double cX1;
|
||||
private double cYX;
|
||||
private double cYY;
|
||||
private double cY1;
|
||||
|
||||
private double c1Y;
|
||||
private double c1X;
|
||||
private double c11;
|
||||
// CHECKSTYLE: resume JavadocVariable check
|
||||
|
||||
/** Build an affine line transform from a n {@code AffineTransform}.
|
||||
* @param transform transform to use (must be invertible otherwise
|
||||
* the {@link LineTransform#apply(Hyperplane)} method would work
|
||||
* only for some lines, and fail for other ones)
|
||||
* @exception MathIllegalArgumentException if the transform is non invertible
|
||||
*/
|
||||
public LineTransform(final AffineTransform transform) throws MathIllegalArgumentException {
|
||||
|
||||
final double[] m = new double[6];
|
||||
transform.getMatrix(m);
|
||||
cXX = m[0];
|
||||
cXY = m[2];
|
||||
cX1 = m[4];
|
||||
cYX = m[1];
|
||||
cYY = m[3];
|
||||
cY1 = m[5];
|
||||
|
||||
c1Y = cXY * cY1 - cYY * cX1;
|
||||
c1X = cXX * cY1 - cYX * cX1;
|
||||
c11 = cXX * cYY - cYX * cXY;
|
||||
|
||||
if (FastMath.abs(c11) < 1.0e-20) {
|
||||
throw new MathIllegalArgumentException(LocalizedFormats.NON_INVERTIBLE_TRANSFORM);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public Point apply(final Point point) {
|
||||
final Point2D p2D = (Point2D) point;
|
||||
final double x = p2D.getX();
|
||||
final double y = p2D.getY();
|
||||
return new Point2D(cXX * x + cXY * y + cX1,
|
||||
cYX * x + cYY * y + cY1);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public Hyperplane apply(final Hyperplane hyperplane) {
|
||||
final Line line = (Line) hyperplane;
|
||||
final double rOffset = c1X * line.cos + c1Y * line.sin + c11 * line.originOffset;
|
||||
final double rCos = cXX * line.cos + cXY * line.sin;
|
||||
final double rSin = cYX * line.cos + cYY * line.sin;
|
||||
final double inv = 1.0 / FastMath.sqrt(rSin * rSin + rCos * rCos);
|
||||
return new Line(FastMath.PI + FastMath.atan2(-rSin, -rCos),
|
||||
inv * rCos, inv * rSin,
|
||||
inv * rOffset);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public SubHyperplane apply(final SubHyperplane sub,
|
||||
final Hyperplane original, final Hyperplane transformed) {
|
||||
final OrientedPoint op = (OrientedPoint) sub.getHyperplane();
|
||||
final Point1D newLoc =
|
||||
(Point1D) transformed.toSubSpace(apply(original.toSpace(op.getLocation())));
|
||||
return new SubHyperplane(new OrientedPoint(newLoc, op.isDirect()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,190 @@
|
|||
/*
|
||||
* 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.math.geometry.euclidean.twoD;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.apache.commons.math.exception.MathIllegalArgumentException;
|
||||
import org.apache.commons.math.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math.geometry.euclidean.oneD.OrientedPoint;
|
||||
import org.apache.commons.math.geometry.euclidean.oneD.Point1D;
|
||||
import org.apache.commons.math.geometry.partitioning.Hyperplane;
|
||||
import org.apache.commons.math.geometry.partitioning.Region;
|
||||
import org.apache.commons.math.geometry.partitioning.SubHyperplane;
|
||||
|
||||
/** This class represent a tree of nested 2D boundary loops.
|
||||
|
||||
* <p>This class is used during Piece instances construction.
|
||||
* Beams are built using the outline edges as
|
||||
* representative of facets, the orientation of these facets are
|
||||
* meaningful. However, we want to allow the user to specify its
|
||||
* outline loops without having to take care of this orientation. This
|
||||
* class is devoted to correct mis-oriented loops.<p>
|
||||
|
||||
* <p>Orientation is computed assuming the piece is finite, i.e. the
|
||||
* outermost loops have their exterior side facing points at infinity,
|
||||
* and hence are oriented counter-clockwise. The orientation of
|
||||
* internal loops is computed as the reverse of the orientation of
|
||||
* their immediate surrounding loop.</p>
|
||||
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
class NestedLoops {
|
||||
|
||||
/** Boundary loop. */
|
||||
private Point2D[] loop;
|
||||
|
||||
/** Surrounded loops. */
|
||||
private ArrayList<NestedLoops> surrounded;
|
||||
|
||||
/** Polygon enclosing a finite region. */
|
||||
private Region polygon;
|
||||
|
||||
/** Indicator for original loop orientation. */
|
||||
private boolean originalIsClockwise;
|
||||
|
||||
/** Simple Constructor.
|
||||
* <p>Build an empty tree of nested loops. This instance will become
|
||||
* the root node of a complete tree, it is not associated with any
|
||||
* loop by itself, the outermost loops are in the root tree child
|
||||
* nodes.</p>
|
||||
*/
|
||||
public NestedLoops() {
|
||||
surrounded = new ArrayList<NestedLoops>();
|
||||
}
|
||||
|
||||
/** Constructor.
|
||||
* <p>Build a tree node with neither parent nor children</p>
|
||||
* @param loop boundary loop (will be reversed in place if needed)
|
||||
* @exception MathIllegalArgumentException if an outline has an open boundary loop
|
||||
*/
|
||||
private NestedLoops(final Point2D[] loop) throws MathIllegalArgumentException {
|
||||
|
||||
if (loop[0] == null) {
|
||||
throw new MathIllegalArgumentException(LocalizedFormats.OUTLINE_BOUNDARY_LOOP_OPEN);
|
||||
}
|
||||
|
||||
this.loop = loop;
|
||||
surrounded = new ArrayList<NestedLoops>();
|
||||
|
||||
// build the polygon defined by the loop
|
||||
final ArrayList<SubHyperplane> edges = new ArrayList<SubHyperplane>();
|
||||
Point2D current = loop[loop.length - 1];
|
||||
for (int i = 0; i < loop.length; ++i) {
|
||||
final Point2D previous = current;
|
||||
current = loop[i];
|
||||
final Line line = new Line(previous, current);
|
||||
final Region region = Region.buildConvex(Arrays.asList(new Hyperplane[] {
|
||||
new OrientedPoint((Point1D) line.toSubSpace(previous), false),
|
||||
new OrientedPoint((Point1D) line.toSubSpace(current), true)
|
||||
}));
|
||||
edges.add(new SubHyperplane(line, region));
|
||||
}
|
||||
polygon = new PolygonsSet(edges);
|
||||
|
||||
// ensure the polygon encloses a finite region of the plane
|
||||
if (Double.isInfinite(polygon.getSize())) {
|
||||
polygon = polygon.getComplement();
|
||||
originalIsClockwise = false;
|
||||
} else {
|
||||
originalIsClockwise = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** Add a loop in a tree.
|
||||
* @param bLoop boundary loop (will be reversed in place if needed)
|
||||
* @exception MathIllegalArgumentException if an outline has crossing
|
||||
* boundary loops or open boundary loops
|
||||
*/
|
||||
public void add(final Point2D[] bLoop) throws MathIllegalArgumentException {
|
||||
add(new NestedLoops(bLoop));
|
||||
}
|
||||
|
||||
/** Add a loop in a tree.
|
||||
* @param node boundary loop (will be reversed in place if needed)
|
||||
* @exception MathIllegalArgumentException if an outline has boundary
|
||||
* loops that cross each other
|
||||
*/
|
||||
private void add(final NestedLoops node) throws MathIllegalArgumentException {
|
||||
|
||||
// check if we can go deeper in the tree
|
||||
for (final NestedLoops child : surrounded) {
|
||||
if (child.polygon.contains(node.polygon)) {
|
||||
child.add(node);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// check if we can absorb some of the instance children
|
||||
for (final Iterator<NestedLoops> iterator = surrounded.iterator(); iterator.hasNext();) {
|
||||
final NestedLoops child = iterator.next();
|
||||
if (node.polygon.contains(child.polygon)) {
|
||||
node.surrounded.add(child);
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
|
||||
// we should be separate from the remaining children
|
||||
for (final NestedLoops child : surrounded) {
|
||||
if (!Region.intersection(node.polygon, child.polygon).isEmpty()) {
|
||||
throw new MathIllegalArgumentException(LocalizedFormats.CROSSING_BOUNDARY_LOOPS);
|
||||
}
|
||||
}
|
||||
|
||||
surrounded.add(node);
|
||||
|
||||
}
|
||||
|
||||
/** Correct the orientation of the loops contained in the tree.
|
||||
* <p>This is this method that really inverts the loops that where
|
||||
* provided through the {@link #add(Point2D[]) add} method if
|
||||
* they are mis-oriented</p>
|
||||
*/
|
||||
public void correctOrientation() {
|
||||
for (NestedLoops child : surrounded) {
|
||||
child.setClockWise(true);
|
||||
}
|
||||
}
|
||||
|
||||
/** Set the loop orientation.
|
||||
* @param clockwise if true, the loop should be set to clockwise
|
||||
* orientation
|
||||
*/
|
||||
private void setClockWise(final boolean clockwise) {
|
||||
|
||||
if (originalIsClockwise ^ clockwise) {
|
||||
// we need to inverse the original loop
|
||||
int min = -1;
|
||||
int max = loop.length;
|
||||
while (++min < --max) {
|
||||
final Point2D tmp = loop[min];
|
||||
loop[min] = loop[max];
|
||||
loop[max] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
// go deeper in the tree
|
||||
for (final NestedLoops child : surrounded) {
|
||||
child.setClockWise(!clockwise);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
* 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.math.geometry.euclidean.twoD;
|
||||
|
||||
import org.apache.commons.math.geometry.partitioning.Point;
|
||||
import org.apache.commons.math.geometry.partitioning.SubSpace;
|
||||
|
||||
/** This class represents a 2D point.
|
||||
* <p>Instances of this class are guaranteed to be immutable.</p>
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public class Point2D extends java.awt.geom.Point2D.Double implements Point, SubSpace {
|
||||
|
||||
/** Point at undefined (NaN) coordinates. */
|
||||
public static final Point2D UNDEFINED = new Point2D(java.lang.Double.NaN, java.lang.Double.NaN);
|
||||
|
||||
/** Serializable UID. */
|
||||
private static final long serialVersionUID = 8883702098988517151L;
|
||||
|
||||
/** Build a point with default coordinates.
|
||||
*/
|
||||
public Point2D() {
|
||||
}
|
||||
|
||||
/** Build a point from its coordinates.
|
||||
* @param x abscissa
|
||||
* @param y ordinate
|
||||
*/
|
||||
public Point2D(final double x, final double y) {
|
||||
super(x, y);
|
||||
}
|
||||
|
||||
/** Build a point from a java awt point.
|
||||
* @param point java awt point
|
||||
*/
|
||||
public Point2D(final java.awt.geom.Point2D.Double point) {
|
||||
super(point.x, point.y);
|
||||
}
|
||||
|
||||
/** Transform a 2D space point into a sub-space point.
|
||||
* @param point 2D point of the space
|
||||
* @return always return null
|
||||
* @see #toSpace
|
||||
*/
|
||||
public Point toSubSpace(final Point point) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Transform a sub-space point into a space point.
|
||||
* @param point ignored parameter
|
||||
* @return always return the instance
|
||||
* @see #toSubSpace
|
||||
*/
|
||||
public Point toSpace(final Point point) {
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,343 @@
|
|||
/*
|
||||
* 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.math.geometry.euclidean.twoD;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.math.geometry.euclidean.oneD.Point1D;
|
||||
import org.apache.commons.math.geometry.partitioning.BSPTree;
|
||||
import org.apache.commons.math.geometry.partitioning.Hyperplane;
|
||||
import org.apache.commons.math.geometry.partitioning.Region;
|
||||
import org.apache.commons.math.geometry.partitioning.SubHyperplane;
|
||||
import org.apache.commons.math.geometry.partitioning.utilities.AVLTree;
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
|
||||
/** This class represents a 2D region: a set of polygons.
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public class PolygonsSet extends Region {
|
||||
|
||||
/** Vertices organized as boundary loops. */
|
||||
private Point2D[][] vertices;
|
||||
|
||||
/** Build a polygons set representing the whole real line.
|
||||
*/
|
||||
public PolygonsSet() {
|
||||
super();
|
||||
}
|
||||
|
||||
/** Build a polygons set from a BSP tree.
|
||||
* <p>The leaf nodes of the BSP tree <em>must</em> have a
|
||||
* {@code Boolean} attribute representing the inside status of
|
||||
* the corresponding cell (true for inside cells, false for outside
|
||||
* cells). In order to avoid building too many small objects, it is
|
||||
* recommended to use the predefined constants
|
||||
* {@code Boolean.TRUE} and {@code Boolean.FALSE}</p>
|
||||
* @param tree inside/outside BSP tree representing the region
|
||||
*/
|
||||
public PolygonsSet(final BSPTree tree) {
|
||||
super(tree);
|
||||
}
|
||||
|
||||
/** Build a polygons set from a Boundary REPresentation (B-rep).
|
||||
* <p>The boundary is provided as a collection of {@link
|
||||
* SubHyperplane sub-hyperplanes}. Each sub-hyperplane has the
|
||||
* interior part of the region on its minus side and the exterior on
|
||||
* its plus side.</p>
|
||||
* <p>The boundary elements can be in any order, and can form
|
||||
* several non-connected sets (like for example polygons with holes
|
||||
* or a set of disjoints polyhedrons considered as a whole). In
|
||||
* fact, the elements do not even need to be connected together
|
||||
* (their topological connections are not used here). However, if the
|
||||
* boundary does not really separate an inside open from an outside
|
||||
* open (open having here its topological meaning), then subsequent
|
||||
* calls to the {@link
|
||||
* Region#checkPoint(org.apache.commons.math.geometry.partitioning.Point)
|
||||
* checkPoint} method will not be meaningful anymore.</p>
|
||||
* <p>If the boundary is empty, the region will represent the whole
|
||||
* space.</p>
|
||||
* @param boundary collection of boundary elements, as a
|
||||
* collection of {@link SubHyperplane SubHyperplane} objects
|
||||
*/
|
||||
public PolygonsSet(final Collection<SubHyperplane> boundary) {
|
||||
super(boundary);
|
||||
}
|
||||
|
||||
/** Build a parallellepipedic box.
|
||||
* @param xMin low bound along the x direction
|
||||
* @param xMax high bound along the x direction
|
||||
* @param yMin low bound along the y direction
|
||||
* @param yMax high bound along the y direction
|
||||
*/
|
||||
public PolygonsSet(final double xMin, final double xMax,
|
||||
final double yMin, final double yMax) {
|
||||
this(buildConvex(boxBoundary(xMin, xMax, yMin, yMax)).getTree(false));
|
||||
}
|
||||
|
||||
/** Create a list of hyperplanes representing the boundary of a box.
|
||||
* @param xMin low bound along the x direction
|
||||
* @param xMax high bound along the x direction
|
||||
* @param yMin low bound along the y direction
|
||||
* @param yMax high bound along the y direction
|
||||
* @return boundary of the box
|
||||
*/
|
||||
private static List<Hyperplane> boxBoundary(final double xMin, final double xMax,
|
||||
final double yMin, final double yMax) {
|
||||
final Point2D minMin = new Point2D(xMin, yMin);
|
||||
final Point2D minMax = new Point2D(xMin, yMax);
|
||||
final Point2D maxMin = new Point2D(xMax, yMin);
|
||||
final Point2D maxMax = new Point2D(xMax, yMax);
|
||||
return Arrays.asList(new Hyperplane[] {
|
||||
new Line(minMin, maxMin),
|
||||
new Line(maxMin, maxMax),
|
||||
new Line(maxMax, minMax),
|
||||
new Line(minMax, minMin)
|
||||
});
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public Region buildNew(final BSPTree tree) {
|
||||
return new PolygonsSet(tree);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
protected void computeGeometricalProperties() {
|
||||
|
||||
final Point2D[][] v = getVertices();
|
||||
|
||||
if (v.length == 0) {
|
||||
if ((Boolean) getTree(false).getAttribute()) {
|
||||
setSize(Double.POSITIVE_INFINITY);
|
||||
setBarycenter(Point2D.UNDEFINED);
|
||||
} else {
|
||||
setSize(0);
|
||||
setBarycenter(new Point2D(0, 0));
|
||||
}
|
||||
} else if (v[0][0] == null) {
|
||||
// there is at least one open-loop: the polygon is infinite
|
||||
setSize(Double.POSITIVE_INFINITY);
|
||||
setBarycenter(Point2D.UNDEFINED);
|
||||
} else {
|
||||
// all loops are closed, we compute some integrals around the shape
|
||||
|
||||
double sum = 0;
|
||||
double sumX = 0;
|
||||
double sumY = 0;
|
||||
|
||||
for (Point2D[] loop : v) {
|
||||
double x1 = loop[loop.length - 1].x;
|
||||
double y1 = loop[loop.length - 1].y;
|
||||
for (final Point2D point : loop) {
|
||||
final double x0 = x1;
|
||||
final double y0 = y1;
|
||||
x1 = point.x;
|
||||
y1 = point.y;
|
||||
final double factor = x0 * y1 - y0 * x1;
|
||||
sum += factor;
|
||||
sumX += factor * (x0 + x1);
|
||||
sumY += factor * (y0 + y1);
|
||||
}
|
||||
}
|
||||
|
||||
if (sum < 0) {
|
||||
// the polygon as a finite outside surrounded by an infinite inside
|
||||
setSize(Double.POSITIVE_INFINITY);
|
||||
setBarycenter(Point2D.UNDEFINED);
|
||||
} else {
|
||||
setSize(sum / 2);
|
||||
setBarycenter(new Point2D(sumX / (3 * sum), sumY / (3 * sum)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** Get the vertices of the polygon.
|
||||
* <p>The polygon boundary can be represented as an array of loops,
|
||||
* each loop being itself an array of vertices.</p>
|
||||
* <p>In order to identify open loops which start and end by
|
||||
* infinite edges, the open loops arrays start with a null point. In
|
||||
* this case, the first non null point and the last point of the
|
||||
* array do not represent real vertices, they are dummy points
|
||||
* intended only to get the direction of the first and last edge. An
|
||||
* open loop consisting of a single infinite line will therefore be
|
||||
* represented by a three elements array with one null point
|
||||
* followed by two dummy points. The open loops are always the first
|
||||
* ones in the loops array.</p>
|
||||
* <p>If the polygon has no boundary at all, a zero length loop
|
||||
* array will be returned.</p>
|
||||
* <p>All line segments in the various loops have the inside of the
|
||||
* region on their left side and the outside on their right side
|
||||
* when moving in the underlying line direction. This means that
|
||||
* closed loops surrounding finite areas obey the direct
|
||||
* trigonometric orientation.</p>
|
||||
* @return vertices of the polygon, organized as oriented boundary
|
||||
* loops with the open loops first (the returned value is guaranteed
|
||||
* to be non-null)
|
||||
*/
|
||||
public Point2D[][] getVertices() {
|
||||
if (vertices == null) {
|
||||
if (getTree(false).getCut() == null) {
|
||||
vertices = new Point2D[0][];
|
||||
} else {
|
||||
|
||||
// sort the segmfinal ents according to their start point
|
||||
final SegmentsBuilder visitor = new SegmentsBuilder();
|
||||
getTree(true).visit(visitor);
|
||||
final AVLTree<Segment> sorted = visitor.getSorted();
|
||||
|
||||
// identify the loops, starting from the open ones
|
||||
// (their start segments final are naturally at the sorted set beginning)
|
||||
final ArrayList<List<Segment>> loops = new ArrayList<List<Segment>>();
|
||||
while (!sorted.isEmpty()) {
|
||||
final AVLTree<Segment>.Node node = sorted.getSmallest();
|
||||
final List<Segment> loop = followLoop(node, sorted);
|
||||
if (loop != null) {
|
||||
loops.add(loop);
|
||||
}
|
||||
}
|
||||
|
||||
// tranform the loops in an array of arrays of points
|
||||
vertices = new Point2D[loops.size()][];
|
||||
int i = 0;
|
||||
|
||||
for (final List<Segment> loop : loops) {
|
||||
if (loop.size() < 2) {
|
||||
// sifinal ngle infinite line
|
||||
final Line line = ((Segment) loop.get(0)).getLine();
|
||||
vertices[i++] = new Point2D[] {
|
||||
null,
|
||||
(Point2D) line.toSpace(new Point1D(-Float.MAX_VALUE)),
|
||||
(Point2D) line.toSpace(new Point1D(+Float.MAX_VALUE))
|
||||
};
|
||||
} else if (((Segment) loop.get(0)).getStart() == null) {
|
||||
// open lofinal op with at least one real point
|
||||
final Point2D[] array = new Point2D[loop.size() + 2];
|
||||
int j = 0;
|
||||
for (Segment segment : loop) {
|
||||
|
||||
if (j == 0) {
|
||||
// null point and first dummy point
|
||||
double x =
|
||||
((Point1D) segment.getLine().toSubSpace(segment.getEnd())).getAbscissa();
|
||||
x -= FastMath.max(1.0, FastMath.abs(x / 2));
|
||||
array[j++] = null;
|
||||
array[j++] = (Point2D) segment.getLine().toSpace(new Point1D(x));
|
||||
}
|
||||
|
||||
if (j < (array.length - 1)) {
|
||||
// current point
|
||||
array[j++] = segment.getEnd();
|
||||
}
|
||||
|
||||
if (j == (array.length - 1)) {
|
||||
// last dummy point
|
||||
double x =
|
||||
((Point1D) segment.getLine().toSubSpace(segment.getStart())).getAbscissa();
|
||||
x += FastMath.max(1.0, FastMath.abs(x / 2));
|
||||
array[j++] = (Point2D) segment.getLine().toSpace(new Point1D(x));
|
||||
}
|
||||
|
||||
}
|
||||
vertices[i++] = array;
|
||||
} else {
|
||||
final Point2D[] array = new Point2D[loop.size()];
|
||||
int j = 0;
|
||||
for (Segment segment : loop) {
|
||||
array[j++] = segment.getStart();
|
||||
}
|
||||
vertices[i++] = array;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return vertices.clone();
|
||||
|
||||
}
|
||||
|
||||
/** Follow a boundary loop.
|
||||
* @param node node containing the segment starting the loop
|
||||
* @param sorted set of segments belonging to the boundary, sorted by
|
||||
* start points (contains {@code node})
|
||||
* @return a list of connected sub-hyperplanes starting at
|
||||
* {@code node}
|
||||
*/
|
||||
private List<Segment> followLoop(final AVLTree<Segment>.Node node,
|
||||
final AVLTree<Segment> sorted) {
|
||||
|
||||
final ArrayList<Segment> loop = new ArrayList<Segment>();
|
||||
Segment segment = (Segment) node.getElement();
|
||||
loop.add(segment);
|
||||
final Point2D globalStart = segment.getStart();
|
||||
Point2D end = segment.getEnd();
|
||||
node.delete();
|
||||
|
||||
// is this an open or a closed loop ?
|
||||
final boolean open = segment.getStart() == null;
|
||||
|
||||
while ((end != null) && (open || (globalStart.distance(end) > 1.0e-10))) {
|
||||
|
||||
// search the sub-hyperplane starting where the previous one ended
|
||||
AVLTree<Segment>.Node selectedNode = null;
|
||||
Segment selectedSegment = null;
|
||||
double selectedDistance = Double.POSITIVE_INFINITY;
|
||||
final Segment lowerLeft = new Segment(end, -1.0e-10, -1.0e-10);
|
||||
final Segment upperRight = new Segment(end, +1.0e-10, +1.0e-10);
|
||||
for (AVLTree<Segment>.Node n = sorted.getNotSmaller(lowerLeft);
|
||||
(n != null) && (n.getElement().compareTo(upperRight) <= 0);
|
||||
n = n.getNext()) {
|
||||
segment = (Segment) n.getElement();
|
||||
final double distance = end.distance(segment.getStart());
|
||||
if (distance < selectedDistance) {
|
||||
selectedNode = n;
|
||||
selectedSegment = segment;
|
||||
selectedDistance = distance;
|
||||
}
|
||||
}
|
||||
|
||||
if (selectedDistance > 1.0e-10) {
|
||||
// this is a degenerated loop, it probably comes from a very
|
||||
// tiny region with some segments smaller than the threshold, we
|
||||
// simply ignore it
|
||||
return null;
|
||||
}
|
||||
|
||||
end = selectedSegment.getEnd();
|
||||
loop.add(selectedSegment);
|
||||
selectedNode.delete();
|
||||
|
||||
}
|
||||
|
||||
if ((loop.size() == 2) && !open) {
|
||||
// this is a degenerated infinitely thin loop, we simply ignore it
|
||||
return null;
|
||||
}
|
||||
|
||||
if ((end == null) && !open) {
|
||||
throw new RuntimeException("internal error");
|
||||
}
|
||||
|
||||
return loop;
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,112 @@
|
|||
/*
|
||||
* 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.math.geometry.euclidean.twoD;
|
||||
|
||||
import org.apache.commons.math.geometry.partitioning.utilities.OrderedTuple;
|
||||
|
||||
/** This class holds segments information before they are connected.
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
class Segment implements Comparable<Segment> {
|
||||
|
||||
/** Start point of the segment. */
|
||||
private final Point2D start;
|
||||
|
||||
/** End point of the segments. */
|
||||
private final Point2D end;
|
||||
|
||||
/** Line containing the segment. */
|
||||
private final Line line;
|
||||
|
||||
/** Sorting key. */
|
||||
private OrderedTuple sortingKey;
|
||||
|
||||
/** Build a segment.
|
||||
* @param start start point of the segment
|
||||
* @param end end point of the segment
|
||||
* @param line line containing the segment
|
||||
*/
|
||||
public Segment(final Point2D start, final Point2D end, final Line line) {
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
this.line = line;
|
||||
sortingKey = (start == null) ?
|
||||
new OrderedTuple(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY) :
|
||||
new OrderedTuple(start.x, start.y);
|
||||
}
|
||||
|
||||
/** Build a dummy segment.
|
||||
* <p>
|
||||
* The object built is not a real segment, only the sorting key is used to
|
||||
* allow searching in the neighborhood of a point. This is an horrible hack ...
|
||||
* </p>
|
||||
* @param start start point of the segment
|
||||
* @param dx abscissa offset from the start point
|
||||
* @param dy ordinate offset from the start point
|
||||
*/
|
||||
public Segment(final Point2D start, final double dx, final double dy) {
|
||||
this.start = null;
|
||||
this.end = null;
|
||||
this.line = null;
|
||||
sortingKey = new OrderedTuple(start.x + dx, start.y + dy);
|
||||
}
|
||||
|
||||
/** Get the start point of the segment.
|
||||
* @return start point of the segment
|
||||
*/
|
||||
public Point2D getStart() {
|
||||
return start;
|
||||
}
|
||||
|
||||
/** Get the end point of the segment.
|
||||
* @return end point of the segment
|
||||
*/
|
||||
public Point2D getEnd() {
|
||||
return end;
|
||||
}
|
||||
|
||||
/** Get the line containing the segment.
|
||||
* @return line containing the segment
|
||||
*/
|
||||
public Line getLine() {
|
||||
return line;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public int compareTo(final Segment o) {
|
||||
return sortingKey.compareTo(o.sortingKey);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public boolean equals(final Object other) {
|
||||
if (this == other) {
|
||||
return true;
|
||||
} else if (other instanceof Segment) {
|
||||
return compareTo((Segment) other) == 0;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return start.hashCode() ^ end.hashCode() ^ line.hashCode() ^ sortingKey.hashCode();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
* 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.math.geometry.euclidean.twoD;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.math.geometry.euclidean.oneD.Interval;
|
||||
import org.apache.commons.math.geometry.euclidean.oneD.IntervalsSet;
|
||||
import org.apache.commons.math.geometry.euclidean.oneD.Point1D;
|
||||
import org.apache.commons.math.geometry.partitioning.BSPTree;
|
||||
import org.apache.commons.math.geometry.partitioning.BSPTreeVisitor;
|
||||
import org.apache.commons.math.geometry.partitioning.Region.BoundaryAttribute;
|
||||
import org.apache.commons.math.geometry.partitioning.SubHyperplane;
|
||||
import org.apache.commons.math.geometry.partitioning.utilities.AVLTree;
|
||||
|
||||
/** Visitor building segments.
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
class SegmentsBuilder implements BSPTreeVisitor {
|
||||
|
||||
/** Sorted segments. */
|
||||
private AVLTree<Segment> sorted;
|
||||
|
||||
/** Simple constructor. */
|
||||
public SegmentsBuilder() {
|
||||
sorted = new AVLTree<Segment>();
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public Order visitOrder(final BSPTree node) {
|
||||
return Order.MINUS_SUB_PLUS;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void visitInternalNode(final BSPTree node) {
|
||||
final BoundaryAttribute attribute = (BoundaryAttribute) node.getAttribute();
|
||||
if (attribute.getPlusOutside() != null) {
|
||||
addContribution(attribute.getPlusOutside(), false);
|
||||
}
|
||||
if (attribute.getPlusInside() != null) {
|
||||
addContribution(attribute.getPlusInside(), true);
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void visitLeafNode(final BSPTree node) {
|
||||
}
|
||||
|
||||
/** Add he contribution of a boundary facet.
|
||||
* @param sub boundary facet
|
||||
* @param reversed if true, the facet has the inside on its plus side
|
||||
*/
|
||||
private void addContribution(final SubHyperplane sub, final boolean reversed) {
|
||||
final Line line = (Line) sub.getHyperplane();
|
||||
final List<Interval> intervals = ((IntervalsSet) sub.getRemainingRegion()).asList();
|
||||
for (final Interval i : intervals) {
|
||||
final Point2D start = Double.isInfinite(i.getLower()) ?
|
||||
null : (Point2D) line.toSpace(new Point1D(i.getLower()));
|
||||
final Point2D end = Double.isInfinite(i.getUpper()) ?
|
||||
null : (Point2D) line.toSpace(new Point1D(i.getUpper()));
|
||||
if (reversed) {
|
||||
sorted.insert(new Segment(end, start, line.getReverse()));
|
||||
} else {
|
||||
sorted.insert(new Segment(start, end, line));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Get the sorted segments.
|
||||
* @return sorted segments
|
||||
*/
|
||||
public AVLTree<Segment> getSorted() {
|
||||
return sorted;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
<html>
|
||||
<!--
|
||||
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.
|
||||
-->
|
||||
<!-- $Revision$ -->
|
||||
<body>
|
||||
<p>
|
||||
This package provides basic 2D geometry components.
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,631 @@
|
|||
/*
|
||||
* 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.math.geometry.partitioning;
|
||||
|
||||
import org.apache.commons.math.geometry.partitioning.Hyperplane.Side;
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
|
||||
/** This class represent a Binary Space Partition tree.
|
||||
|
||||
* <p>BSP trees are an efficient way to represent space partitions and
|
||||
* to associate attributes with each cell. Each node in a BSP tree
|
||||
* represents a convex region which is partitioned in two convex
|
||||
* sub-regions at each side of a cut hyperplane. The root tree
|
||||
* contains the complete space.</p>
|
||||
|
||||
* <p>The main use of such partitions is to use a boolean attribute to
|
||||
* define an inside/outside property, hence representing arbitrary
|
||||
* polytopes (line segments in 1D, polygons in 2D and polyhedrons in
|
||||
* 3D) and to operate on them.</p>
|
||||
|
||||
* <p>Another example would be to represent Voronoi tesselations, the
|
||||
* attribute of each cell holding the defining point of the cell.</p>
|
||||
|
||||
* <p>The application-defined attributes are shared among copied
|
||||
* instances and propagated to split parts. These attributes are not
|
||||
* used by the BSP-tree algorithms themselves, so the application can
|
||||
* use them for any purpose. Since the tree visiting method holds
|
||||
* internal and leaf nodes differently, it is possible to use
|
||||
* different classes for internal nodes attributes and leaf nodes
|
||||
* attributes. This should be used with care, though, because if the
|
||||
* tree is modified in any way after attributes have been set, some
|
||||
* internal nodes may become leaf nodes and some leaf nodes may become
|
||||
* internal nodes.</p>
|
||||
|
||||
* <p>One of the main sources for the development of this package was
|
||||
* Bruce Naylor, John Amanatides and William Thibault paper <a
|
||||
* href="http://www.cs.yorku.ca/~amana/research/bsptSetOp.pdf">Merging
|
||||
* BSP Trees Yields Polyhedral Set Operations</a> Proc. Siggraph '90,
|
||||
* Computer Graphics 24(4), August 1990, pp 115-124, published by the
|
||||
* Association for Computing Machinery (ACM).</p>
|
||||
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public class BSPTree {
|
||||
|
||||
/** Cut sub-hyperplane. */
|
||||
private SubHyperplane cut;
|
||||
|
||||
/** Tree at the plus side of the cut hyperplane. */
|
||||
private BSPTree plus;
|
||||
|
||||
/** Tree at the minus side of the cut hyperplane. */
|
||||
private BSPTree minus;
|
||||
|
||||
/** Parent tree. */
|
||||
private BSPTree parent;
|
||||
|
||||
/** Application-defined attribute. */
|
||||
private Object attribute;
|
||||
|
||||
/** Build a tree having only one root cell representing the whole space.
|
||||
*/
|
||||
public BSPTree() {
|
||||
cut = null;
|
||||
plus = null;
|
||||
minus = null;
|
||||
parent = null;
|
||||
attribute = null;
|
||||
}
|
||||
|
||||
/** Build a tree having only one root cell representing the whole space.
|
||||
* @param attribute attribute of the tree (may be null)
|
||||
*/
|
||||
public BSPTree(final Object attribute) {
|
||||
cut = null;
|
||||
plus = null;
|
||||
minus = null;
|
||||
parent = null;
|
||||
this.attribute = attribute;
|
||||
}
|
||||
|
||||
/** Build a BSPTree from its underlying elements.
|
||||
* <p>This method does <em>not</em> perform any verification on
|
||||
* consistency of its arguments, it should therefore be used only
|
||||
* when then caller knows what it is doing.</p>
|
||||
* <p>This method is mainly useful kto build trees
|
||||
* bottom-up. Building trees top-down is realized with the help of
|
||||
* method {@link #insertCut insertCut}.</p>
|
||||
* @param cut cut sub-hyperplane for the tree
|
||||
* @param plus plus side sub-tree
|
||||
* @param minus minus side sub-tree
|
||||
* @param attribute attribute associated with the node (may be null)
|
||||
* @see #insertCut
|
||||
*/
|
||||
public BSPTree(final SubHyperplane cut, final BSPTree plus, final BSPTree minus,
|
||||
final Object attribute) {
|
||||
this.cut = cut;
|
||||
this.plus = plus;
|
||||
this.minus = minus;
|
||||
this.parent = null;
|
||||
this.attribute = attribute;
|
||||
plus.parent = this;
|
||||
minus.parent = this;
|
||||
}
|
||||
|
||||
/** Insert a cut sub-hyperplane in a node.
|
||||
* <p>The sub-tree starting at this node will be completely
|
||||
* overwritten. The new cut sub-hyperplane will be built from the
|
||||
* intersection of the provided hyperplane with the cell. If the
|
||||
* hyperplane does intersect the cell, the cell will have two
|
||||
* children cells with {@code null} attributes on each side of
|
||||
* the inserted cut sub-hyperplane. If the hyperplane does not
|
||||
* intersect the cell then <em>no</em> cut hyperplane will be
|
||||
* inserted and the cell will be changed to a leaf cell. The
|
||||
* attribute of the node is never changed.</p>
|
||||
* <p>This method is mainly useful when called on leaf nodes
|
||||
* (i.e. nodes for which {@link #getCut getCut} returns
|
||||
* {@code null}), in this case it provides a way to build a
|
||||
* tree top-down (whereas the {@link #BSPTree(SubHyperplane,
|
||||
* BSPTree, BSPTree, Object) 4 arguments constructor} is devoted to
|
||||
* build trees bottom-up).</p>
|
||||
* @param hyperplane hyperplane to insert, it will be chopped in
|
||||
* order to fit in the cell defined by the parent nodes of the
|
||||
* instance
|
||||
* @return true if a cut sub-hyperplane has been inserted (i.e. if
|
||||
* the cell now has two leaf child nodes)
|
||||
* @see #BSPTree(SubHyperplane, BSPTree, BSPTree, Object)
|
||||
*/
|
||||
public boolean insertCut(final Hyperplane hyperplane) {
|
||||
|
||||
if (cut != null) {
|
||||
plus.parent = null;
|
||||
minus.parent = null;
|
||||
}
|
||||
|
||||
final SubHyperplane chopped = fitToCell(new SubHyperplane(hyperplane));
|
||||
if (chopped.getRemainingRegion().isEmpty()) {
|
||||
cut = null;
|
||||
plus = null;
|
||||
minus = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
cut = chopped;
|
||||
plus = new BSPTree();
|
||||
plus.parent = this;
|
||||
minus = new BSPTree();
|
||||
minus.parent = this;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
/** Copy the instance.
|
||||
* <p>The instance created is completely independant of the original
|
||||
* one. A deep copy is used, none of the underlying objects are
|
||||
* shared (except for the nodes attributes and immutable
|
||||
* objects).</p>
|
||||
* @return a new tree, copy of the instance
|
||||
*/
|
||||
public BSPTree copySelf() {
|
||||
|
||||
if (cut == null) {
|
||||
return new BSPTree(attribute);
|
||||
}
|
||||
|
||||
return new BSPTree(cut.copySelf(), plus.copySelf(), minus.copySelf(),
|
||||
attribute);
|
||||
|
||||
}
|
||||
|
||||
/** Get the cut sub-hyperplane.
|
||||
* @return cut sub-hyperplane, null if this is a leaf tree
|
||||
*/
|
||||
public SubHyperplane getCut() {
|
||||
return cut;
|
||||
}
|
||||
|
||||
/** Get the tree on the plus side of the cut hyperplane.
|
||||
* @return tree on the plus side of the cut hyperplane, null if this
|
||||
* is a leaf tree
|
||||
*/
|
||||
public BSPTree getPlus() {
|
||||
return plus;
|
||||
}
|
||||
|
||||
/** Get the tree on the minus side of the cut hyperplane.
|
||||
* @return tree on the minus side of the cut hyperplane, null if this
|
||||
* is a leaf tree
|
||||
*/
|
||||
public BSPTree getMinus() {
|
||||
return minus;
|
||||
}
|
||||
|
||||
/** Get the parent node.
|
||||
* @return parent node, null if the node has no parents
|
||||
*/
|
||||
public BSPTree getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
/** Associate an attribute with the instance.
|
||||
* @param attribute attribute to associate with the node
|
||||
* @see #getAttribute
|
||||
*/
|
||||
public void setAttribute(final Object attribute) {
|
||||
this.attribute = attribute;
|
||||
}
|
||||
|
||||
/** Get the attribute associated with the instance.
|
||||
* @return attribute associated with the node or null if no
|
||||
* attribute has been explicitly set using the {@link #setAttribute
|
||||
* setAttribute} method
|
||||
* @see #setAttribute
|
||||
*/
|
||||
public Object getAttribute() {
|
||||
return attribute;
|
||||
}
|
||||
|
||||
/** Visit the BSP tree nodes.
|
||||
* @param visitor object visiting the tree nodes
|
||||
*/
|
||||
public void visit(final BSPTreeVisitor visitor) {
|
||||
if (cut == null) {
|
||||
visitor.visitLeafNode(this);
|
||||
} else {
|
||||
switch (visitor.visitOrder(this)) {
|
||||
case PLUS_MINUS_SUB:
|
||||
plus.visit(visitor);
|
||||
minus.visit(visitor);
|
||||
visitor.visitInternalNode(this);
|
||||
break;
|
||||
case PLUS_SUB_MINUS:
|
||||
plus.visit(visitor);
|
||||
visitor.visitInternalNode(this);
|
||||
minus.visit(visitor);
|
||||
break;
|
||||
case MINUS_PLUS_SUB:
|
||||
minus.visit(visitor);
|
||||
plus.visit(visitor);
|
||||
visitor.visitInternalNode(this);
|
||||
break;
|
||||
case MINUS_SUB_PLUS:
|
||||
minus.visit(visitor);
|
||||
visitor.visitInternalNode(this);
|
||||
plus.visit(visitor);
|
||||
break;
|
||||
case SUB_PLUS_MINUS:
|
||||
visitor.visitInternalNode(this);
|
||||
plus.visit(visitor);
|
||||
minus.visit(visitor);
|
||||
break;
|
||||
case SUB_MINUS_PLUS:
|
||||
visitor.visitInternalNode(this);
|
||||
minus.visit(visitor);
|
||||
plus.visit(visitor);
|
||||
break;
|
||||
default:
|
||||
throw new RuntimeException("internal error");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/** Fit a sub-hyperplane inside the cell defined by the instance.
|
||||
* <p>Fitting is done by chopping off the parts of the
|
||||
* sub-hyperplane that lie outside of the cell using the
|
||||
* cut-hyperplanes of the parent nodes of the instance.</p>
|
||||
* @param sub sub-hyperplane to fit
|
||||
* @return a new sub-hyperplane, gueranteed to have no part outside
|
||||
* of the instance cell
|
||||
*/
|
||||
private SubHyperplane fitToCell(final SubHyperplane sub) {
|
||||
SubHyperplane s = sub;
|
||||
for (BSPTree tree = this; tree.parent != null; tree = tree.parent) {
|
||||
if (tree == tree.parent.plus) {
|
||||
s = tree.parent.cut.getHyperplane().split(s).getPlus();
|
||||
} else {
|
||||
s = tree.parent.cut.getHyperplane().split(s).getMinus();
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/** Get the cell to which a point belongs.
|
||||
* <p>If the returned cell is a leaf node the points belongs to the
|
||||
* interior of the node, if the cell is an internal node the points
|
||||
* belongs to the node cut sub-hyperplane.</p>
|
||||
* @param point point to check
|
||||
* @return the tree cell to which the point belongs (can be
|
||||
*/
|
||||
public BSPTree getCell(final Point point) {
|
||||
|
||||
if (cut == null) {
|
||||
return this;
|
||||
}
|
||||
|
||||
// position of the point with respect to the cut hyperplane
|
||||
final double offset = cut.getHyperplane().getOffset(point);
|
||||
|
||||
if (FastMath.abs(offset) < 1.0e-10) {
|
||||
return this;
|
||||
} else if (offset <= 0) {
|
||||
// point is on the minus side of the cut hyperplane
|
||||
return minus.getCell(point);
|
||||
} else {
|
||||
// point is on the plus side of the cut hyperplane
|
||||
return plus.getCell(point);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** Perform condensation on a tree.
|
||||
* <p>The condensation operation is not recursive, it must be called
|
||||
* explicitely from leaves to root.</p>
|
||||
*/
|
||||
private void condense() {
|
||||
if ((cut != null) && (plus.cut == null) && (minus.cut == null) &&
|
||||
(((plus.attribute == null) && (minus.attribute == null)) ||
|
||||
((plus.attribute != null) && plus.attribute.equals(minus.attribute)))) {
|
||||
attribute = (plus.attribute == null) ? minus.attribute : plus.attribute;
|
||||
cut = null;
|
||||
plus = null;
|
||||
minus = null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Merge a BSP tree with the instance.
|
||||
* <p>All trees are modified (parts of them are reused in the new
|
||||
* tree), it is the responsibility of the caller to ensure a copy
|
||||
* has been done before if any of the former tree should be
|
||||
* preserved, <em>no</em> such copy is done here!</p>
|
||||
* <p>The algorithm used here is directly derived from the one
|
||||
* described in the Naylor, Amanatides and Thibault paper (section
|
||||
* III, Binary Partitioning of a BSP Tree).</p>
|
||||
* @param tree other tree to merge with the instance (will be
|
||||
* <em>unusable</em> after the operation, as well as the
|
||||
* instance itself)
|
||||
* @param leafMerger object implementing the final merging phase
|
||||
* (this is where the semantic of the operation occurs, generally
|
||||
* depending on the attribute of the leaf node)
|
||||
* @return a new tree, result of <code>instance <op>
|
||||
* tree</code>, this value can be ignored if parentTree is not null
|
||||
* since all connections have already been established
|
||||
*/
|
||||
public BSPTree merge(final BSPTree tree, final LeafMerger leafMerger) {
|
||||
return merge(tree, leafMerger, null, false);
|
||||
}
|
||||
|
||||
/** Merge a BSP tree with the instance.
|
||||
* @param tree other tree to merge with the instance (will be
|
||||
* <em>unusable</em> after the operation, as well as the
|
||||
* instance itself)
|
||||
* @param leafMerger object implementing the final merging phase
|
||||
* (this is where the semantic of the operation occurs, generally
|
||||
* depending on the attribute of the leaf node)
|
||||
* @param parentTree parent tree to connect to (may be null)
|
||||
* @param isPlusChild if true and if parentTree is not null, the
|
||||
* resulting tree should be the plus child of its parent, ignored if
|
||||
* parentTree is null
|
||||
* @return a new tree, result of <code>instance <op>
|
||||
* tree</code>, this value can be ignored if parentTree is not null
|
||||
* since all connections have already been established
|
||||
*/
|
||||
private BSPTree merge(final BSPTree tree, final LeafMerger leafMerger,
|
||||
final BSPTree parentTree, final boolean isPlusChild) {
|
||||
if (cut == null) {
|
||||
// cell/tree operation
|
||||
return leafMerger.merge(this, tree, parentTree, isPlusChild, true);
|
||||
} else if (tree.cut == null) {
|
||||
// tree/cell operation
|
||||
return leafMerger.merge(tree, this, parentTree, isPlusChild, false);
|
||||
} else {
|
||||
// tree/tree operation
|
||||
final BSPTree merged = tree.split(cut);
|
||||
if (parentTree != null) {
|
||||
merged.parent = parentTree;
|
||||
if (isPlusChild) {
|
||||
parentTree.plus = merged;
|
||||
} else {
|
||||
parentTree.minus = merged;
|
||||
}
|
||||
}
|
||||
|
||||
// merging phase
|
||||
plus.merge(merged.plus, leafMerger, merged, true);
|
||||
minus.merge(merged.minus, leafMerger, merged, false);
|
||||
merged.condense();
|
||||
if (merged.cut != null) {
|
||||
merged.cut =
|
||||
merged.fitToCell(new SubHyperplane(merged.cut.getHyperplane()));
|
||||
}
|
||||
|
||||
return merged;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/** This interface gather the merging operations between a BSP tree
|
||||
* leaf and another BSP tree.
|
||||
* <p>As explained in Bruce Naylor, John Amanatides and William
|
||||
* Thibault paper <a
|
||||
* href="http://www.cs.yorku.ca/~amana/research/bsptSetOp.pdf">Merging
|
||||
* BSP Trees Yields Polyhedral Set Operations</a>,
|
||||
* the operations on {@link BSPTree BSP trees} can be expressed as a
|
||||
* generic recursive merging operation where only the final part,
|
||||
* when one of the operand is a leaf, is specific to the real
|
||||
* operation semantics. For example, a tree representing a region
|
||||
* using a boolean attribute to identify inside cells and outside
|
||||
* cells would use four different objects to implement the final
|
||||
* merging phase of the four set operations union, intersection,
|
||||
* difference and symmetric difference (exclusive or).</p>
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public static interface LeafMerger {
|
||||
|
||||
/** Merge a leaf node and a tree node.
|
||||
* <p>This method is called at the end of a recursive merging
|
||||
* resulting from a {@code tree1.merge(tree2, leafMerger)}
|
||||
* call, when one of the sub-trees involved is a leaf (i.e. when
|
||||
* its cut-hyperplane is null). This is the only place where the
|
||||
* precise semantics of the operation are required. For all upper
|
||||
* level nodes in the tree, the merging operation is only a
|
||||
* generic partitioning algorithm.</p>
|
||||
* <p>Since the final operation may be non-commutative, it is
|
||||
* important to know if the leaf node comes from the instance tree
|
||||
* ({@code tree1}) or the argument tree
|
||||
* ({@code tree2}). The third argument of the method is
|
||||
* devoted to this. It can be ignored for commutative
|
||||
* operations.</p>
|
||||
* <p>The {@link BSPTree#insertInTree BSPTree.insertInTree} method
|
||||
* may be useful to implement this method.</p>
|
||||
* @param leaf leaf node (its cut hyperplane is guaranteed to be
|
||||
* null)
|
||||
* @param tree tree node (its cut hyperplane may be null or not)
|
||||
* @param parentTree parent tree to connect to (may be null)
|
||||
* @param isPlusChild if true and if parentTree is not null, the
|
||||
* resulting tree should be the plus child of its parent, ignored if
|
||||
* parentTree is null
|
||||
* @param leafFromInstance if true, the leaf node comes from the
|
||||
* instance tree ({@code tree1}) and the tree node comes from
|
||||
* the argument tree ({@code tree2})
|
||||
* @return the BSP tree resulting from the merging (may be one of
|
||||
* the arguments)
|
||||
*/
|
||||
BSPTree merge(BSPTree leaf, BSPTree tree,
|
||||
BSPTree parentTree, boolean isPlusChild,
|
||||
boolean leafFromInstance);
|
||||
|
||||
}
|
||||
|
||||
/** Split a BSP tree by an external sub-hyperplane.
|
||||
* <p>Split a tree in two halves, on each side of the
|
||||
* sub-hyperplane. The instance is not modified.</p>
|
||||
* <p>The tree returned is not upward-consistent: despite all of its
|
||||
* sub-trees cut sub-hyperplanes (including its own cut
|
||||
* sub-hyperplane) are bounded to the current cell, it is <em>not</em>
|
||||
* attached to any parent tree yet. This tree is intended to be
|
||||
* later inserted into an higher level tree.</p>
|
||||
* <p>The algorithm used here is the one given in Naylor, Amanatides
|
||||
* and Thibault paper (section III, Binary Partitioning of a BSP
|
||||
* Tree).</p>
|
||||
* @param sub partitioning sub-hyperplane, must be already clipped
|
||||
* to the convex region represented by the instance, will be used as
|
||||
* the cut sub-hyperplane of the returned tree
|
||||
* @return a tree having the specified sub-hyperplane as its cut
|
||||
* sub-hyperplane, the two parts of the split instance as its two
|
||||
* sub-trees and a null parent
|
||||
*/
|
||||
public BSPTree split(final SubHyperplane sub) {
|
||||
|
||||
if (cut == null) {
|
||||
return new BSPTree(sub, copySelf(), new BSPTree(attribute), null);
|
||||
}
|
||||
|
||||
final Hyperplane cHyperplane = cut.getHyperplane();
|
||||
final Hyperplane sHyperplane = sub.getHyperplane();
|
||||
switch (cHyperplane.side(sub)) {
|
||||
case PLUS :
|
||||
{ // the partitioning sub-hyperplane is entirely in the plus sub-tree
|
||||
final BSPTree split = plus.split(sub);
|
||||
if (sHyperplane.side(cut) == Side.PLUS) {
|
||||
split.plus = new BSPTree(cut.copySelf(),
|
||||
split.plus, minus.copySelf(), attribute);
|
||||
split.plus.condense();
|
||||
split.plus.parent = split;
|
||||
} else {
|
||||
split.minus = new BSPTree(cut.copySelf(),
|
||||
split.minus, minus.copySelf(), attribute);
|
||||
split.minus.condense();
|
||||
split.minus.parent = split;
|
||||
}
|
||||
return split;
|
||||
}
|
||||
case MINUS :
|
||||
{ // the partitioning sub-hyperplane is entirely in the minus sub-tree
|
||||
final BSPTree split = minus.split(sub);
|
||||
if (sHyperplane.side(cut) == Side.PLUS) {
|
||||
split.plus = new BSPTree(cut.copySelf(),
|
||||
plus.copySelf(), split.plus, attribute);
|
||||
split.plus.condense();
|
||||
split.plus.parent = split;
|
||||
} else {
|
||||
split.minus = new BSPTree(cut.copySelf(),
|
||||
plus.copySelf(), split.minus, attribute);
|
||||
split.minus.condense();
|
||||
split.minus.parent = split;
|
||||
}
|
||||
return split;
|
||||
}
|
||||
case BOTH :
|
||||
{
|
||||
final Hyperplane.SplitSubHyperplane cutParts = sHyperplane.split(cut);
|
||||
final Hyperplane.SplitSubHyperplane subParts = cHyperplane.split(sub);
|
||||
final BSPTree split = new BSPTree(sub,
|
||||
plus.split(subParts.getPlus()),
|
||||
minus.split(subParts.getMinus()),
|
||||
null);
|
||||
split.plus.cut = cutParts.getPlus();
|
||||
split.minus.cut = cutParts.getMinus();
|
||||
final BSPTree tmp = split.plus.minus;
|
||||
split.plus.minus = split.minus.plus;
|
||||
split.plus.minus.parent = split.plus;
|
||||
split.minus.plus = tmp;
|
||||
split.minus.plus.parent = split.minus;
|
||||
split.plus.condense();
|
||||
split.minus.condense();
|
||||
return split;
|
||||
}
|
||||
default :
|
||||
return cHyperplane.sameOrientationAs(sHyperplane) ?
|
||||
new BSPTree(sub, plus.copySelf(), minus.copySelf(), attribute) :
|
||||
new BSPTree(sub, minus.copySelf(), plus.copySelf(), attribute);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** Insert the instance into another tree.
|
||||
* <p>The instance itself is modified so its former parent should
|
||||
* not be used anymore.</p>
|
||||
* @param parentTree parent tree to connect to (may be null)
|
||||
* @param isPlusChild if true and if parentTree is not null, the
|
||||
* resulting tree should be the plus child of its parent, ignored if
|
||||
* parentTree is null
|
||||
* @see LeafMerger
|
||||
*/
|
||||
public void insertInTree(final BSPTree parentTree, final boolean isPlusChild) {
|
||||
|
||||
// set up parent/child links
|
||||
parent = parentTree;
|
||||
if (parentTree != null) {
|
||||
if (isPlusChild) {
|
||||
parentTree.plus = this;
|
||||
} else {
|
||||
parentTree.minus = this;
|
||||
}
|
||||
}
|
||||
|
||||
// make sure the inserted tree lies in the cell defined by its parent nodes
|
||||
if (cut != null) {
|
||||
|
||||
// explore the parent nodes from here towards tree root
|
||||
for (BSPTree tree = this; tree.parent != null; tree = tree.parent) {
|
||||
|
||||
// this is an hyperplane of some parent node
|
||||
final Hyperplane hyperplane = tree.parent.cut.getHyperplane();
|
||||
|
||||
// chop off the parts of the inserted tree that extend
|
||||
// on the wrong side of this parent hyperplane
|
||||
if (tree == tree.parent.plus) {
|
||||
cut = hyperplane.split(cut).getPlus();
|
||||
plus.chopOffMinus(hyperplane);
|
||||
minus.chopOffMinus(hyperplane);
|
||||
} else {
|
||||
cut = hyperplane.split(cut).getMinus();
|
||||
plus.chopOffPlus(hyperplane);
|
||||
minus.chopOffPlus(hyperplane);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// since we may have drop some parts of the inserted tree,
|
||||
// perform a condensation pass to keep the tree structure simple
|
||||
condense();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** Chop off parts of the tree.
|
||||
* <p>The instance is modified in place, all the parts that are on
|
||||
* the minus side of the chopping hyperplane are disgarded, only the
|
||||
* parts on the plus side remain.</p>
|
||||
* @param hyperplane chopping hyperplane
|
||||
*/
|
||||
private void chopOffMinus(final Hyperplane hyperplane) {
|
||||
if (cut != null) {
|
||||
cut = hyperplane.split(cut).getPlus();
|
||||
plus.chopOffMinus(hyperplane);
|
||||
minus.chopOffMinus(hyperplane);
|
||||
}
|
||||
}
|
||||
|
||||
/** Chop off parts of the tree.
|
||||
* <p>The instance is modified in place, all the parts that are on
|
||||
* the plus side of the chopping hyperplane are disgarded, only the
|
||||
* parts on the minus side remain.</p>
|
||||
* @param hyperplane chopping hyperplane
|
||||
*/
|
||||
private void chopOffPlus(final Hyperplane hyperplane) {
|
||||
if (cut != null) {
|
||||
cut = hyperplane.split(cut).getMinus();
|
||||
plus.chopOffPlus(hyperplane);
|
||||
minus.chopOffPlus(hyperplane);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,110 @@
|
|||
/*
|
||||
* 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.math.geometry.partitioning;
|
||||
|
||||
/** This interface is used to visit {@link BSPTree BSP tree} nodes.
|
||||
|
||||
* <p>Navigation through {@link BSPTree BSP trees} can be done using
|
||||
* two different point of views:</p>
|
||||
* <ul>
|
||||
* <li>
|
||||
* the first one is in a node-oriented way using the {@link
|
||||
* BSPTree#getPlus}, {@link BSPTree#getMinus} and {@link
|
||||
* BSPTree#getParent} methods. Terminal nodes without associated
|
||||
* {@link SubHyperplane sub-hyperplanes} can be visited this way,
|
||||
* there is no constraint in the visit order, and it is possible
|
||||
* to visit either all nodes or only a subset of the nodes
|
||||
* </li>
|
||||
* <li>
|
||||
* the second one is in a sub-hyperplane-oriented way using
|
||||
* classes implementing this interface which obeys the visitor
|
||||
* design pattern. The visit order is provided by the visitor as
|
||||
* each node is first encountered. Each node is visited exactly
|
||||
* once.
|
||||
* </li>
|
||||
* </ul>
|
||||
|
||||
* @see BSPTree
|
||||
* @see SubHyperplane
|
||||
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public interface BSPTreeVisitor {
|
||||
|
||||
/** Enumerate for visit order with respect to plus sub-tree, minus sub-tree and cut sub-hyperplane. */
|
||||
enum Order {
|
||||
/** Indicator for visit order plus sub-tree, then minus sub-tree,
|
||||
* and last cut sub-hyperplane.
|
||||
*/
|
||||
PLUS_MINUS_SUB,
|
||||
|
||||
/** Indicator for visit order plus sub-tree, then cut sub-hyperplane,
|
||||
* and last minus sub-tree.
|
||||
*/
|
||||
PLUS_SUB_MINUS,
|
||||
|
||||
/** Indicator for visit order minus sub-tree, then plus sub-tree,
|
||||
* and last cut sub-hyperplane.
|
||||
*/
|
||||
MINUS_PLUS_SUB,
|
||||
|
||||
/** Indicator for visit order minus sub-tree, then cut sub-hyperplane,
|
||||
* and last plus sub-tree.
|
||||
*/
|
||||
MINUS_SUB_PLUS,
|
||||
|
||||
/** Indicator for visit order cut sub-hyperplane, then plus sub-tree,
|
||||
* and last minus sub-tree.
|
||||
*/
|
||||
SUB_PLUS_MINUS,
|
||||
|
||||
/** Indicator for visit order cut sub-hyperplane, then minus sub-tree,
|
||||
* and last plus sub-tree.
|
||||
*/
|
||||
SUB_MINUS_PLUS;
|
||||
}
|
||||
|
||||
/** Determine the visit order for this node.
|
||||
* <p>Before attempting to visit an internal node, this method is
|
||||
* called to determine the desired ordering of the visit. It is
|
||||
* guaranteed that this method will be called before {@link
|
||||
* #visitInternalNode visitInternalNode} for a given node, it will be
|
||||
* called exactly once for each internal node.</p>
|
||||
* @param node BSP node guaranteed to have a non null cut sub-hyperplane
|
||||
* @return desired visit order, must be one of
|
||||
* {@link Order#PLUS_MINUS_SUB}, {@link Order#PLUS_SUB_MINUS},
|
||||
* {@link Order#MINUS_PLUS_SUB}, {@link Order#MINUS_SUB_PLUS},
|
||||
* {@link Order#SUB_PLUS_MINUS}, {@link Order#SUB_MINUS_PLUS}
|
||||
*/
|
||||
Order visitOrder(BSPTree node);
|
||||
|
||||
/** Visit a BSP tree node node having a non-null sub-hyperplane.
|
||||
* <p>It is guaranteed that this method will be called after {@link
|
||||
* #visitOrder visitOrder} has been called for a given node,
|
||||
* it wil be called exactly once for each internal node.</p>
|
||||
* @param node BSP node guaranteed to have a non null cut sub-hyperplane
|
||||
* @see #visitLeafNode
|
||||
*/
|
||||
void visitInternalNode(BSPTree node);
|
||||
|
||||
/** Visit a leaf BSP tree node node having a null sub-hyperplane.
|
||||
* @param node leaf BSP node having a null sub-hyperplane
|
||||
* @see #visitInternalNode
|
||||
*/
|
||||
void visitLeafNode(BSPTree node);
|
||||
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
* 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.math.geometry.partitioning;
|
||||
|
||||
/** Characterization of a sub-hyperplane.
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
class Characterization {
|
||||
|
||||
/** Parts of the sub-hyperplane that have inside cells on the tested side. */
|
||||
private SubHyperplane in;
|
||||
|
||||
/** Parts of the sub-hyperplane that have outside cells on the tested side. */
|
||||
private SubHyperplane out;
|
||||
|
||||
/** Create an empty characterization of a sub-hyperplane.
|
||||
*/
|
||||
public Characterization() {
|
||||
in = null;
|
||||
out = null;
|
||||
}
|
||||
|
||||
/** Check if the sub-hyperplane that have inside cells on the tested side.
|
||||
* @return true if the sub-hyperplane that have inside cells on the tested side
|
||||
*/
|
||||
public boolean hasIn() {
|
||||
return (in != null) && (!in.getRemainingRegion().isEmpty());
|
||||
}
|
||||
|
||||
/** Get the parts of the sub-hyperplane that have inside cells on the tested side.
|
||||
* @return parts of the sub-hyperplane that have inside cells on the tested side
|
||||
*/
|
||||
public SubHyperplane getIn() {
|
||||
return in;
|
||||
}
|
||||
|
||||
/** Check if the sub-hyperplane that have outside cells on the tested side.
|
||||
* @return true if the sub-hyperplane that have outside cells on the tested side
|
||||
*/
|
||||
public boolean hasOut() {
|
||||
return (out != null) && (!out.getRemainingRegion().isEmpty());
|
||||
}
|
||||
|
||||
/** Get the parts of the sub-hyperplane that have outside cells on the tested side.
|
||||
* @return parts of the sub-hyperplane that have outside cells on the tested side
|
||||
*/
|
||||
public SubHyperplane getOut() {
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Add a part of the sub-hyperplane known to have inside or outside cell on the tested side.
|
||||
* @param sub part of the sub-hyperplane to add
|
||||
* @param inside if true, the part added as an inside cell on the tested side, otherwise
|
||||
* it has an outside cell on the tested side
|
||||
*/
|
||||
public void add(final SubHyperplane sub, final boolean inside) {
|
||||
if (inside) {
|
||||
if (in == null) {
|
||||
in = sub;
|
||||
} else {
|
||||
in = new SubHyperplane(in.getHyperplane(),
|
||||
Region.union(in.getRemainingRegion(),
|
||||
sub.getRemainingRegion()));
|
||||
}
|
||||
} else {
|
||||
if (out == null) {
|
||||
out = sub;
|
||||
} else {
|
||||
out = new SubHyperplane(out.getHyperplane(),
|
||||
Region.union(out.getRemainingRegion(),
|
||||
sub.getRemainingRegion()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,159 @@
|
|||
/*
|
||||
* 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.math.geometry.partitioning;
|
||||
|
||||
/** This interface represents an hyperplane of a space.
|
||||
|
||||
* <p>The most prominent place where hyperplane appears in space
|
||||
* partitioning is as cutters. Each partitioning node in a {@link
|
||||
* BSPTree BSP tree} has a cut {@link SubHyperplane sub-hyperplane}
|
||||
* which is either an hyperplane or a part of an hyperplane. In an
|
||||
* n-dimensions euclidean space, an hyperplane is an (n-1)-dimensions
|
||||
* hyperplane (for example a traditional plane in the 3D euclidean
|
||||
* space). They can be more exotic objects in specific fields, for
|
||||
* example a circle on the surface of the unit sphere.</p>
|
||||
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public interface Hyperplane extends SubSpace {
|
||||
|
||||
/** Enumerate for specifying sides of the hyperplane. */
|
||||
enum Side {
|
||||
|
||||
/** Code for the plus side of the hyperplane. */
|
||||
PLUS,
|
||||
|
||||
/** Code for the minus side of the hyperplane. */
|
||||
MINUS,
|
||||
|
||||
/** Code for elements crossing the hyperplane from plus to minus side. */
|
||||
BOTH,
|
||||
|
||||
/** Code for the hyperplane itself. */
|
||||
HYPER;
|
||||
|
||||
}
|
||||
|
||||
/** Copy the instance.
|
||||
* <p>The instance created is completely independant of the original
|
||||
* one. A deep copy is used, none of the underlying objects are
|
||||
* shared (except for immutable objects).</p>
|
||||
* @return a new hyperplane, copy of the instance
|
||||
*/
|
||||
Hyperplane copySelf();
|
||||
|
||||
/** Get the offset (oriented distance) of a point.
|
||||
* <p>The offset is 0 if the point is on the underlying hyperplane,
|
||||
* it is positive if the point is on one particular side of the
|
||||
* hyperplane, and it is negative if the point is on the other side,
|
||||
* according to the hyperplane natural orientation.</p>
|
||||
* @param point point to check
|
||||
* @return offset of the point
|
||||
*/
|
||||
double getOffset(Point point);
|
||||
|
||||
/** Check if the instance has the same orientation as another hyperplane.
|
||||
* <p>This method is expected to be called on parallel hyperplanes
|
||||
* (i.e. when the {@link #side side} method would return {@link
|
||||
* Side#HYPER} for some sub-hyperplane having the specified hyperplane
|
||||
* as its underlying hyperplane). The method should <em>not</em>
|
||||
* re-check for parallelism, only for orientation, typically by
|
||||
* testing something like the sign of the dot-products of
|
||||
* normals.</p>
|
||||
* @param other other hyperplane to check against the instance
|
||||
* @return true if the instance and the other hyperplane have
|
||||
* the same orientation
|
||||
*/
|
||||
boolean sameOrientationAs(Hyperplane other);
|
||||
|
||||
/** Build the sub-space shared by the instance and another hyperplane.
|
||||
* @param other other hyperplane
|
||||
* @return a sub-space at the intersection of the instance and the
|
||||
* other sub-space (it has a dimension one unit less than the
|
||||
* instance)
|
||||
*/
|
||||
SubSpace intersection(Hyperplane other);
|
||||
|
||||
/** Build a region covering the whole hyperplane.
|
||||
* <p>The region build is restricted to the sub-space defined by the
|
||||
* hyperplane. This means that the regions points are consistent
|
||||
* with the argument of the {@link SubSpace#toSpace toSpace} method
|
||||
* and with the return value of the {@link SubSpace#toSubSpace
|
||||
* toSubSpace} method.<p>
|
||||
* @return a region covering the whole hyperplane
|
||||
*/
|
||||
Region wholeHyperplane();
|
||||
|
||||
/** Build a region covering the whole space.
|
||||
* @return a region containing the instance
|
||||
*/
|
||||
Region wholeSpace();
|
||||
|
||||
/** Compute the relative position of a sub-hyperplane with respect
|
||||
* to the instance.
|
||||
* @param sub sub-hyperplane to check
|
||||
* @return one of {@link Side#PLUS}, {@link Side#MINUS}, {@link Side#BOTH},
|
||||
* {@link Side#HYPER}
|
||||
*/
|
||||
Side side(SubHyperplane sub);
|
||||
|
||||
/** Split a sub-hyperplane in two parts by the instance.
|
||||
* @param sub sub-hyperplane to split
|
||||
* @return an object containing both the part of the sub-hyperplane
|
||||
* on the plus side of the instance and the part of the
|
||||
* sub-hyperplane on the minus side of the instance
|
||||
*/
|
||||
SplitSubHyperplane split(SubHyperplane sub);
|
||||
|
||||
/** Class holding the results of the {@link Hyperplane#split Hyperplane.split}
|
||||
* method. */
|
||||
class SplitSubHyperplane {
|
||||
|
||||
/** Part of the sub-hyperplane on the plus side of the splitting hyperplane. */
|
||||
private final SubHyperplane plus;
|
||||
|
||||
/** Part of the sub-hyperplane on the minus side of the splitting hyperplane. */
|
||||
private final SubHyperplane minus;
|
||||
|
||||
/** Build a SplitSubHyperplane from its parts.
|
||||
* @param plus part of the sub-hyperplane on the plus side of the
|
||||
* splitting hyperplane
|
||||
* @param minus part of the sub-hyperplane on the minus side of the
|
||||
* splitting hyperplane
|
||||
*/
|
||||
public SplitSubHyperplane(final SubHyperplane plus, final SubHyperplane minus) {
|
||||
this.plus = plus;
|
||||
this.minus = minus;
|
||||
}
|
||||
|
||||
/** Get the part of the sub-hyperplane on the plus side of the splitting hyperplane.
|
||||
* @return part of the sub-hyperplane on the plus side of the splitting hyperplane
|
||||
*/
|
||||
public SubHyperplane getPlus() {
|
||||
return plus;
|
||||
}
|
||||
|
||||
/** Get the part of the sub-hyperplane on the minus side of the splitting hyperplane.
|
||||
* @return part of the sub-hyperplane on the minus side of the splitting hyperplane
|
||||
*/
|
||||
public SubHyperplane getMinus() {
|
||||
return minus;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* 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.math.geometry.partitioning;
|
||||
|
||||
/** This interface represents a generic point to be used in a space partition.
|
||||
* <p>Points are completely virtual entities with no specification at
|
||||
* all, so this class is essentially a marker interface with no
|
||||
* methods. This allows to perform partition in traditional euclidean
|
||||
* n-dimensions spaces, but also in more exotic universes like for
|
||||
* example the surface of the unit sphere.</p>
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public interface Point {
|
||||
// nothing here, this is only a marker interface
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,138 @@
|
|||
/*
|
||||
* 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.math.geometry.partitioning;
|
||||
|
||||
/** This interface represents the remaining parts of an hyperplane after
|
||||
* other parts have been chopped off.
|
||||
|
||||
* <p>sub-hyperplanes are obtained when parts of an {@link
|
||||
* Hyperplane hyperplane} are chopped off by other hyperplanes that
|
||||
* intersect it. The remaining part is a convex region. Such objects
|
||||
* appear in {@link BSPTree BSP trees} as the intersection of a cut
|
||||
* hyperplane with the convex region which it splits, the chopping
|
||||
* hyperplanes are the cut hyperplanes closer to the tree root.</p>
|
||||
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public class SubHyperplane {
|
||||
|
||||
/** Underlying hyperplane. */
|
||||
private final Hyperplane hyperplane;
|
||||
|
||||
/** Remaining region of the hyperplane. */
|
||||
private final Region remainingRegion;
|
||||
|
||||
/** Build a chopped hyperplane that is not chopped at all.
|
||||
* @param hyperplane underlying hyperplane
|
||||
*/
|
||||
public SubHyperplane(final Hyperplane hyperplane) {
|
||||
this.hyperplane = hyperplane;
|
||||
remainingRegion = hyperplane.wholeHyperplane();
|
||||
}
|
||||
|
||||
/** Build a sub-hyperplane from an hyperplane and a region.
|
||||
* @param hyperplane underlying hyperplane
|
||||
* @param remainingRegion remaining region of the hyperplane
|
||||
*/
|
||||
public SubHyperplane(final Hyperplane hyperplane, final Region remainingRegion) {
|
||||
this.hyperplane = hyperplane;
|
||||
this.remainingRegion = remainingRegion;
|
||||
}
|
||||
|
||||
/** Copy the instance.
|
||||
* <p>The instance created is completely independant of the original
|
||||
* one. A deep copy is used, none of the underlying objects are
|
||||
* shared (except for the nodes attributes and immutable
|
||||
* objects).</p>
|
||||
* @return a new sub-hyperplane, copy of the instance
|
||||
*/
|
||||
public SubHyperplane copySelf() {
|
||||
return new SubHyperplane(hyperplane.copySelf(), remainingRegion.copySelf());
|
||||
}
|
||||
|
||||
/** Get the underlying hyperplane.
|
||||
* @return underlying hyperplane
|
||||
*/
|
||||
public Hyperplane getHyperplane() {
|
||||
return hyperplane;
|
||||
}
|
||||
|
||||
/** Get the remaining region of the hyperplane.
|
||||
* <p>The returned region is expressed in the canonical hyperplane
|
||||
* frame and has the hyperplane dimension. For example a chopped
|
||||
* hyperplane in the 3D euclidean is a 2D plane and the
|
||||
* corresponding region is a convex 2D polygon.</p>
|
||||
* @return remaining region of the hyperplane
|
||||
*/
|
||||
public Region getRemainingRegion() {
|
||||
return remainingRegion;
|
||||
}
|
||||
|
||||
/** Apply a transform to the instance.
|
||||
* <p>The instance must be a (D-1)-dimension sub-hyperplane with
|
||||
* respect to the transform <em>not</em> a (D-2)-dimension
|
||||
* sub-hyperplane the transform knows how to transform by
|
||||
* itself. The transform will consist in transforming first the
|
||||
* hyperplane and then the all region using the various methods
|
||||
* provided by the transform.</p>
|
||||
* @param transform D-dimension transform to apply
|
||||
* @return the transformed instance
|
||||
*/
|
||||
public SubHyperplane applyTransform(final Transform transform) {
|
||||
final Hyperplane tHyperplane = transform.apply(hyperplane);
|
||||
final BSPTree tTree =
|
||||
recurseTransform(remainingRegion.getTree(false), tHyperplane, transform);
|
||||
return new SubHyperplane(tHyperplane, remainingRegion.buildNew(tTree));
|
||||
}
|
||||
|
||||
/** Recursively transform a BSP-tree from a sub-hyperplane.
|
||||
* @param node current BSP tree node
|
||||
* @param transformed image of the instance hyperplane by the transform
|
||||
* @param transform transform to apply
|
||||
* @return a new tree
|
||||
*/
|
||||
private BSPTree recurseTransform(final BSPTree node, final Hyperplane transformed,
|
||||
final Transform transform) {
|
||||
if (node.getCut() == null) {
|
||||
return new BSPTree(node.getAttribute());
|
||||
}
|
||||
|
||||
Region.BoundaryAttribute attribute =
|
||||
(Region.BoundaryAttribute) node.getAttribute();
|
||||
if (attribute != null) {
|
||||
final SubHyperplane tPO = (attribute.getPlusOutside() == null) ?
|
||||
null :
|
||||
transform.apply(attribute.getPlusOutside(),
|
||||
hyperplane, transformed);
|
||||
final SubHyperplane tPI = (attribute.getPlusInside() == null) ?
|
||||
null :
|
||||
transform.apply(attribute.getPlusInside(),
|
||||
hyperplane, transformed);
|
||||
attribute = new Region.BoundaryAttribute(tPO, tPI);
|
||||
}
|
||||
|
||||
return new BSPTree(transform.apply(node.getCut(),
|
||||
hyperplane, transformed),
|
||||
recurseTransform(node.getPlus(), transformed,
|
||||
transform),
|
||||
recurseTransform(node.getMinus(), transformed,
|
||||
transform),
|
||||
attribute);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* 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.math.geometry.partitioning;
|
||||
|
||||
|
||||
/** This interface represents a sub-space of a space.
|
||||
|
||||
* <p>Sub-spaces are the lower dimensions subsets of a n-dimensions
|
||||
* space. The (n-1)-dimension sub-spaces are specific sub-spaces known
|
||||
* as {@link Hyperplane hyperplanes}.</p>
|
||||
|
||||
* <p>In the 3D euclidean space, hyperplanes are 2D planes, and the 1D
|
||||
* sub-spaces are lines.</p>
|
||||
|
||||
* @see Hyperplane
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public interface SubSpace {
|
||||
|
||||
/** Transform a space point into a sub-space point.
|
||||
* @param point n-dimension point of the space
|
||||
* @return (n-1)-dimension point of the sub-space corresponding to
|
||||
* the specified space point
|
||||
* @see #toSpace
|
||||
*/
|
||||
Point toSubSpace(Point point);
|
||||
|
||||
/** Transform a sub-space point into a space point.
|
||||
* @param point (n-1)-dimension point of the sub-space
|
||||
* @return n-dimension point of the space corresponding to the
|
||||
* specified sub-space point
|
||||
* @see #toSubSpace
|
||||
*/
|
||||
Point toSpace(Point point);
|
||||
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* 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.math.geometry.partitioning;
|
||||
|
||||
|
||||
/** This interface represents an inversible affine transform in a space.
|
||||
* <p>Inversible affine transform include for example scalings,
|
||||
* translations, rotations.</p>
|
||||
|
||||
* <p>Transforms are dimension-specific. The consistency rules between
|
||||
* the three {@code apply} methods are the following ones for a
|
||||
* transformed defined for dimension D:</p>
|
||||
* <ul>
|
||||
* <li>
|
||||
* the transform can be applied to a point in the
|
||||
* D-dimension space using its {@link #apply(Point)}
|
||||
* method
|
||||
* </li>
|
||||
* <li>
|
||||
* the transform can be applied to a (D-1)-dimension
|
||||
* hyperplane in the D-dimension space using its
|
||||
* {@link #apply(Hyperplane)} method
|
||||
* </li>
|
||||
* <li>
|
||||
* the transform can be applied to a (D-2)-dimension
|
||||
* sub-hyperplane in a (D-1)-dimension hyperplane using
|
||||
* its {@link #apply(SubHyperplane, Hyperplane, Hyperplane)}
|
||||
* method
|
||||
* </li>
|
||||
* </ul>
|
||||
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public interface Transform {
|
||||
|
||||
/** Transform a point of a space.
|
||||
* @param point point to transform
|
||||
* @return a new object representing the transformed point
|
||||
*/
|
||||
Point apply(Point point);
|
||||
|
||||
/** Transform an hyperplane of a space.
|
||||
* @param hyperplane hyperplane to transform
|
||||
* @return a new object representing the transformed hyperplane
|
||||
*/
|
||||
Hyperplane apply(Hyperplane hyperplane);
|
||||
|
||||
/** Transform a sub-hyperplane embedded in an hyperplane.
|
||||
* @param sub sub-hyperplane to transform
|
||||
* @param original hyperplane in which the sub-hyperplane is
|
||||
* defined (this is the original hyperplane, the transform has
|
||||
* <em>not</em> been applied to it)
|
||||
* @param transformed hyperplane in which the sub-hyperplane is
|
||||
* defined (this is the transformed hyperplane, the transform
|
||||
* <em>has</em> been applied to it)
|
||||
* @return a new object representing the transformed sub-hyperplane
|
||||
*/
|
||||
SubHyperplane apply(SubHyperplane sub, Hyperplane original, Hyperplane transformed);
|
||||
|
||||
}
|
|
@ -0,0 +1,107 @@
|
|||
<!--
|
||||
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.
|
||||
-->
|
||||
<html>
|
||||
<body>
|
||||
This package provides classes to implement Binary Space Partition trees.
|
||||
|
||||
<p>
|
||||
{@link org.apache.commons.math.geometry.partitioning.BSPTree BSP trees}
|
||||
are an efficient way to represent parts of space and in particular
|
||||
polytopes (line segments in 1D, polygons in 2D and polyhedrons in 3D)
|
||||
and to operate on them. The main principle is to recursively subdivide
|
||||
the space using simple hyperplanes (points in 1D, lines in 2D, planes
|
||||
in 3D).
|
||||
</p>
|
||||
|
||||
<p>
|
||||
We start with a tree composed of a single node without any cut
|
||||
hyperplane: it represents the complete space, which is a convex
|
||||
part. If we add a cut hyperplane to this node, this represents a
|
||||
partition with the hyperplane at the node level and two half spaces at
|
||||
each side of the cut hyperplane. These half-spaces are represented by
|
||||
two child nodes without any cut hyperplanes associated, the plus child
|
||||
which represents the half space on the plus side of the cut hyperplane
|
||||
and the minus child on the other side. Continuing the subdivisions, we
|
||||
end up with a tree having internal nodes that are associated with a
|
||||
cut hyperplane and leaf nodes without any hyperplane which correspond
|
||||
to convex parts.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
When BSP trees are used to represent polytopes, the convex parts are
|
||||
known to be completely inside or outside the polytope as long as there
|
||||
is no facet in the part (which is obviously the case if the cut
|
||||
hyperplanes have been chosen as the underlying hyperplanes of the
|
||||
facets (this is called an autopartition) and if the subdivision
|
||||
process has been continued until all facets have been processed. It is
|
||||
important to note that the polytope is <em>not</em> defined by a
|
||||
single part, but by several convex ones. This is the property that
|
||||
allows BSP-trees to represent non-convex polytopes despites all parts
|
||||
are convex. The {@link
|
||||
org.apache.commons.math.geometry.partitioning.Region Region} class is
|
||||
devoted to this representation, it is build on top of the {@link
|
||||
org.apache.commons.math.geometry.partitioning.BSPTree BSPTree} class using
|
||||
boolean objects as the leaf nodes attributes to represent the
|
||||
inside/outside property of each leaf part, and also adds various
|
||||
methods dealing with boundaries (i.e. the separation between the
|
||||
inside and the outside parts).
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Rather than simply associating the internal nodes with an hyperplane,
|
||||
we consider <em>sub-hyperplanes</em> which correspond to the part of
|
||||
the hyperplane that is inside the convex part defined by all the
|
||||
parent nodes (this implies that the sub-hyperplane at root node is in
|
||||
fact a complete hyperplane, because there is no parent to bound
|
||||
it). Since the parts are convex, the sub-hyperplanes are convex, in
|
||||
3D the convex parts are convex polyhedrons, and the sub-hyperplanes
|
||||
are convex polygons that cut these polyhedrons in two
|
||||
sub-polyhedrons. Using this definition, a BSP tree completely
|
||||
partitions the space. Each point either belongs to one of the
|
||||
sub-hyperplanes in an internal node or belongs to one of the leaf
|
||||
convex parts.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
In order to determine where a point is, it is sufficient to check its
|
||||
position with respect to the root cut hyperplane, to select the
|
||||
corresponding child tree and to repeat the procedure recursively,
|
||||
until either the point appears to be exactly on one of the hyperplanes
|
||||
in the middle of the tree or to be in one of the leaf parts. For
|
||||
this operation, it is sufficient to consider the complete hyperplanes,
|
||||
there is no need to check the points with the boundary of the
|
||||
sub-hyperplanes, because this check has in fact already been realized
|
||||
by the recursive descent in the tree. This is very easy to do and very
|
||||
efficient, especially if the tree is well balanced (the cost is
|
||||
<code>O(log(n))</code> where <code>n</code> is the number of facets)
|
||||
or if the first tree levels close to the root discriminate large parts
|
||||
of the total space.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
One of the main sources for the development of this package was Bruce
|
||||
Naylor, John Amanatides and William Thibault paper <a
|
||||
href="http://www.cs.yorku.ca/~amana/research/bsptSetOp.pdf">Merging
|
||||
BSP Trees Yields Polyhedral Set Operations</a> Proc. Siggraph '90,
|
||||
Computer Graphics 24(4), August 1990, pp 115-124, published by the
|
||||
Association for Computing Machinery (ACM). The same paper can also be
|
||||
found <a
|
||||
href="http://www.cs.utexas.edu/users/fussell/courses/cs384g/bsp_treemerge.pdf">here</a>.
|
||||
</p>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,631 @@
|
|||
/*
|
||||
* 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.math.geometry.partitioning.utilities;
|
||||
|
||||
/** This class implements AVL trees.
|
||||
|
||||
* <p>The purpose of this class is to sort elements while allowing
|
||||
* duplicate elements (i.e. such that {@code a.equals(b)} is
|
||||
* true). The {@code SortedSet} interface does not allow this, so
|
||||
* a specific class is needed. Null elements are not allowed.</p>
|
||||
|
||||
* <p>Since the {@code equals} method is not sufficient to
|
||||
* differentiate elements, the {@link #delete delete} method is
|
||||
* implemented using the equality operator.</p>
|
||||
|
||||
* <p>In order to clearly mark the methods provided here do not have
|
||||
* the same semantics as the ones specified in the
|
||||
* {@code SortedSet} interface, different names are used
|
||||
* ({@code add} has been replaced by {@link #insert insert} and
|
||||
* {@code remove} has been replaced by {@link #delete
|
||||
* delete}).</p>
|
||||
|
||||
* <p>This class is based on the C implementation Georg Kraml has put
|
||||
* in the public domain. Unfortunately, his <a
|
||||
* href="www.purists.org/georg/avltree/index.html">page</a> seems not
|
||||
* to exist any more.</p>
|
||||
|
||||
* @param <T> the type of the elements
|
||||
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public class AVLTree<T extends Comparable<T>> {
|
||||
|
||||
/** Top level node. */
|
||||
private Node top;
|
||||
|
||||
/** Build an empty tree.
|
||||
*/
|
||||
public AVLTree() {
|
||||
top = null;
|
||||
}
|
||||
|
||||
/** Insert an element in the tree.
|
||||
* @param element element to insert (silently ignored if null)
|
||||
*/
|
||||
public void insert(final T element) {
|
||||
if (element != null) {
|
||||
if (top == null) {
|
||||
top = new Node(element, null);
|
||||
} else {
|
||||
top.insert(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Delete an element from the tree.
|
||||
* <p>The element is deleted only if there is a node {@code n}
|
||||
* containing exactly the element instance specified, i.e. for which
|
||||
* {@code n.getElement() == element}. This is purposely
|
||||
* <em>different</em> from the specification of the
|
||||
* {@code java.util.Set} {@code remove} method (in fact,
|
||||
* this is the reason why a specific class has been developed).</p>
|
||||
* @param element element to delete (silently ignored if null)
|
||||
* @return true if the element was deleted from the tree
|
||||
*/
|
||||
public boolean delete(final T element) {
|
||||
if (element != null) {
|
||||
for (Node node = getNotSmaller(element); node != null; node = node.getNext()) {
|
||||
// loop over all elements neither smaller nor larger
|
||||
// than the specified one
|
||||
if (node.element == element) {
|
||||
node.delete();
|
||||
return true;
|
||||
} else if (node.element.compareTo(element) > 0) {
|
||||
// all the remaining elements are known to be larger,
|
||||
// the element is not in the tree
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Check if the tree is empty.
|
||||
* @return true if the tree is empty
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return top == null;
|
||||
}
|
||||
|
||||
|
||||
/** Get the number of elements of the tree.
|
||||
* @return number of elements contained in the tree
|
||||
*/
|
||||
public int size() {
|
||||
return (top == null) ? 0 : top.size();
|
||||
}
|
||||
|
||||
/** Get the node whose element is the smallest one in the tree.
|
||||
* @return the tree node containing the smallest element in the tree
|
||||
* or null if the tree is empty
|
||||
* @see #getLargest
|
||||
* @see #getNotSmaller
|
||||
* @see #getNotLarger
|
||||
* @see Node#getPrevious
|
||||
* @see Node#getNext
|
||||
*/
|
||||
public Node getSmallest() {
|
||||
return (top == null) ? null : top.getSmallest();
|
||||
}
|
||||
|
||||
/** Get the node whose element is the largest one in the tree.
|
||||
* @return the tree node containing the largest element in the tree
|
||||
* or null if the tree is empty
|
||||
* @see #getSmallest
|
||||
* @see #getNotSmaller
|
||||
* @see #getNotLarger
|
||||
* @see Node#getPrevious
|
||||
* @see Node#getNext
|
||||
*/
|
||||
public Node getLargest() {
|
||||
return (top == null) ? null : top.getLargest();
|
||||
}
|
||||
|
||||
/** Get the node whose element is not smaller than the reference object.
|
||||
* @param reference reference object (may not be in the tree)
|
||||
* @return the tree node containing the smallest element not smaller
|
||||
* than the reference object or null if either the tree is empty or
|
||||
* all its elements are smaller than the reference object
|
||||
* @see #getSmallest
|
||||
* @see #getLargest
|
||||
* @see #getNotLarger
|
||||
* @see Node#getPrevious
|
||||
* @see Node#getNext
|
||||
*/
|
||||
public Node getNotSmaller(final T reference) {
|
||||
Node candidate = null;
|
||||
for (Node node = top; node != null;) {
|
||||
if (node.element.compareTo(reference) < 0) {
|
||||
if (node.right == null) {
|
||||
return candidate;
|
||||
}
|
||||
node = node.right;
|
||||
} else {
|
||||
candidate = node;
|
||||
if (node.left == null) {
|
||||
return candidate;
|
||||
}
|
||||
node = node.left;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Get the node whose element is not larger than the reference object.
|
||||
* @param reference reference object (may not be in the tree)
|
||||
* @return the tree node containing the largest element not larger
|
||||
* than the reference object (in which case the node is guaranteed
|
||||
* not to be empty) or null if either the tree is empty or all its
|
||||
* elements are larger than the reference object
|
||||
* @see #getSmallest
|
||||
* @see #getLargest
|
||||
* @see #getNotSmaller
|
||||
* @see Node#getPrevious
|
||||
* @see Node#getNext
|
||||
*/
|
||||
public Node getNotLarger(final T reference) {
|
||||
Node candidate = null;
|
||||
for (Node node = top; node != null;) {
|
||||
if (node.element.compareTo(reference) > 0) {
|
||||
if (node.left == null) {
|
||||
return candidate;
|
||||
}
|
||||
node = node.left;
|
||||
} else {
|
||||
candidate = node;
|
||||
if (node.right == null) {
|
||||
return candidate;
|
||||
}
|
||||
node = node.right;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Enum for tree skew factor. */
|
||||
private static enum Skew {
|
||||
/** Code for left high trees. */
|
||||
LEFT_HIGH,
|
||||
|
||||
/** Code for right high trees. */
|
||||
RIGHT_HIGH,
|
||||
|
||||
/** Code for Skew.BALANCED trees. */
|
||||
BALANCED;
|
||||
}
|
||||
|
||||
/** This class implements AVL trees nodes.
|
||||
* <p>AVL tree nodes implement all the logical structure of the
|
||||
* tree. Nodes are created by the {@link AVLTree AVLTree} class.</p>
|
||||
* <p>The nodes are not independant from each other but must obey
|
||||
* specific balancing constraints and the tree structure is
|
||||
* rearranged as elements are inserted or deleted from the tree. The
|
||||
* creation, modification and tree-related navigation methods have
|
||||
* therefore restricted access. Only the order-related navigation,
|
||||
* reading and delete methods are public.</p>
|
||||
* @see AVLTree
|
||||
*/
|
||||
public class Node {
|
||||
|
||||
/** Element contained in the current node. */
|
||||
private T element;
|
||||
|
||||
/** Left sub-tree. */
|
||||
private Node left;
|
||||
|
||||
/** Right sub-tree. */
|
||||
private Node right;
|
||||
|
||||
/** Parent tree. */
|
||||
private Node parent;
|
||||
|
||||
/** Skew factor. */
|
||||
private Skew skew;
|
||||
|
||||
/** Build a node for a specified element.
|
||||
* @param element element
|
||||
* @param parent parent node
|
||||
*/
|
||||
Node(final T element, final Node parent) {
|
||||
this.element = element;
|
||||
left = null;
|
||||
right = null;
|
||||
this.parent = parent;
|
||||
skew = Skew.BALANCED;
|
||||
}
|
||||
|
||||
/** Get the contained element.
|
||||
* @return element contained in the node
|
||||
*/
|
||||
public T getElement() {
|
||||
return element;
|
||||
}
|
||||
|
||||
/** Get the number of elements of the tree rooted at this node.
|
||||
* @return number of elements contained in the tree rooted at this node
|
||||
*/
|
||||
int size() {
|
||||
return 1 + ((left == null) ? 0 : left.size()) + ((right == null) ? 0 : right.size());
|
||||
}
|
||||
|
||||
/** Get the node whose element is the smallest one in the tree
|
||||
* rooted at this node.
|
||||
* @return the tree node containing the smallest element in the
|
||||
* tree rooted at this node or null if the tree is empty
|
||||
* @see #getLargest
|
||||
*/
|
||||
Node getSmallest() {
|
||||
Node node = this;
|
||||
while (node.left != null) {
|
||||
node = node.left;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
/** Get the node whose element is the largest one in the tree
|
||||
* rooted at this node.
|
||||
* @return the tree node containing the largest element in the
|
||||
* tree rooted at this node or null if the tree is empty
|
||||
* @see #getSmallest
|
||||
*/
|
||||
Node getLargest() {
|
||||
Node node = this;
|
||||
while (node.right != null) {
|
||||
node = node.right;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
/** Get the node containing the next smaller or equal element.
|
||||
* @return node containing the next smaller or equal element or
|
||||
* null if there is no smaller or equal element in the tree
|
||||
* @see #getNext
|
||||
*/
|
||||
public Node getPrevious() {
|
||||
|
||||
if (left != null) {
|
||||
final Node node = left.getLargest();
|
||||
if (node != null) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
for (Node node = this; node.parent != null; node = node.parent) {
|
||||
if (node != node.parent.left) {
|
||||
return node.parent;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
/** Get the node containing the next larger or equal element.
|
||||
* @return node containing the next larger or equal element (in
|
||||
* which case the node is guaranteed not to be empty) or null if
|
||||
* there is no larger or equal element in the tree
|
||||
* @see #getPrevious
|
||||
*/
|
||||
public Node getNext() {
|
||||
|
||||
if (right != null) {
|
||||
final Node node = right.getSmallest();
|
||||
if (node != null) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
for (Node node = this; node.parent != null; node = node.parent) {
|
||||
if (node != node.parent.right) {
|
||||
return node.parent;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
/** Insert an element in a sub-tree.
|
||||
* @param newElement element to insert
|
||||
* @return true if the parent tree should be re-Skew.BALANCED
|
||||
*/
|
||||
boolean insert(final T newElement) {
|
||||
if (newElement.compareTo(this.element) < 0) {
|
||||
// the inserted element is smaller than the node
|
||||
if (left == null) {
|
||||
left = new Node(newElement, this);
|
||||
return rebalanceLeftGrown();
|
||||
}
|
||||
return left.insert(newElement) ? rebalanceLeftGrown() : false;
|
||||
}
|
||||
|
||||
// the inserted element is equal to or greater than the node
|
||||
if (right == null) {
|
||||
right = new Node(newElement, this);
|
||||
return rebalanceRightGrown();
|
||||
}
|
||||
return right.insert(newElement) ? rebalanceRightGrown() : false;
|
||||
|
||||
}
|
||||
|
||||
/** Delete the node from the tree.
|
||||
*/
|
||||
public void delete() {
|
||||
if ((parent == null) && (left == null) && (right == null)) {
|
||||
// this was the last node, the tree is now empty
|
||||
element = null;
|
||||
top = null;
|
||||
} else {
|
||||
|
||||
Node node;
|
||||
Node child;
|
||||
boolean leftShrunk;
|
||||
if ((left == null) && (right == null)) {
|
||||
node = this;
|
||||
element = null;
|
||||
leftShrunk = node == node.parent.left;
|
||||
child = null;
|
||||
} else {
|
||||
node = (left != null) ? left.getLargest() : right.getSmallest();
|
||||
element = node.element;
|
||||
leftShrunk = node == node.parent.left;
|
||||
child = (node.left != null) ? node.left : node.right;
|
||||
}
|
||||
|
||||
node = node.parent;
|
||||
if (leftShrunk) {
|
||||
node.left = child;
|
||||
} else {
|
||||
node.right = child;
|
||||
}
|
||||
if (child != null) {
|
||||
child.parent = node;
|
||||
}
|
||||
|
||||
while (leftShrunk ? node.rebalanceLeftShrunk() : node.rebalanceRightShrunk()) {
|
||||
if (node.parent == null) {
|
||||
return;
|
||||
}
|
||||
leftShrunk = node == node.parent.left;
|
||||
node = node.parent;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/** Re-balance the instance as left sub-tree has grown.
|
||||
* @return true if the parent tree should be reSkew.BALANCED too
|
||||
*/
|
||||
private boolean rebalanceLeftGrown() {
|
||||
switch (skew) {
|
||||
case LEFT_HIGH:
|
||||
if (left.skew == Skew.LEFT_HIGH) {
|
||||
rotateCW();
|
||||
skew = Skew.BALANCED;
|
||||
right.skew = Skew.BALANCED;
|
||||
} else {
|
||||
final Skew s = left.right.skew;
|
||||
left.rotateCCW();
|
||||
rotateCW();
|
||||
switch(s) {
|
||||
case LEFT_HIGH:
|
||||
left.skew = Skew.BALANCED;
|
||||
right.skew = Skew.RIGHT_HIGH;
|
||||
break;
|
||||
case RIGHT_HIGH:
|
||||
left.skew = Skew.LEFT_HIGH;
|
||||
right.skew = Skew.BALANCED;
|
||||
break;
|
||||
default:
|
||||
left.skew = Skew.BALANCED;
|
||||
right.skew = Skew.BALANCED;
|
||||
}
|
||||
skew = Skew.BALANCED;
|
||||
}
|
||||
return false;
|
||||
case RIGHT_HIGH:
|
||||
skew = Skew.BALANCED;
|
||||
return false;
|
||||
default:
|
||||
skew = Skew.LEFT_HIGH;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/** Re-balance the instance as right sub-tree has grown.
|
||||
* @return true if the parent tree should be reSkew.BALANCED too
|
||||
*/
|
||||
private boolean rebalanceRightGrown() {
|
||||
switch (skew) {
|
||||
case LEFT_HIGH:
|
||||
skew = Skew.BALANCED;
|
||||
return false;
|
||||
case RIGHT_HIGH:
|
||||
if (right.skew == Skew.RIGHT_HIGH) {
|
||||
rotateCCW();
|
||||
skew = Skew.BALANCED;
|
||||
left.skew = Skew.BALANCED;
|
||||
} else {
|
||||
final Skew s = right.left.skew;
|
||||
right.rotateCW();
|
||||
rotateCCW();
|
||||
switch (s) {
|
||||
case LEFT_HIGH:
|
||||
left.skew = Skew.BALANCED;
|
||||
right.skew = Skew.RIGHT_HIGH;
|
||||
break;
|
||||
case RIGHT_HIGH:
|
||||
left.skew = Skew.LEFT_HIGH;
|
||||
right.skew = Skew.BALANCED;
|
||||
break;
|
||||
default:
|
||||
left.skew = Skew.BALANCED;
|
||||
right.skew = Skew.BALANCED;
|
||||
}
|
||||
skew = Skew.BALANCED;
|
||||
}
|
||||
return false;
|
||||
default:
|
||||
skew = Skew.RIGHT_HIGH;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/** Re-balance the instance as left sub-tree has shrunk.
|
||||
* @return true if the parent tree should be reSkew.BALANCED too
|
||||
*/
|
||||
private boolean rebalanceLeftShrunk() {
|
||||
switch (skew) {
|
||||
case LEFT_HIGH:
|
||||
skew = Skew.BALANCED;
|
||||
return true;
|
||||
case RIGHT_HIGH:
|
||||
if (right.skew == Skew.RIGHT_HIGH) {
|
||||
rotateCCW();
|
||||
skew = Skew.BALANCED;
|
||||
left.skew = Skew.BALANCED;
|
||||
return true;
|
||||
} else if (right.skew == Skew.BALANCED) {
|
||||
rotateCCW();
|
||||
skew = Skew.LEFT_HIGH;
|
||||
left.skew = Skew.RIGHT_HIGH;
|
||||
return false;
|
||||
} else {
|
||||
final Skew s = right.left.skew;
|
||||
right.rotateCW();
|
||||
rotateCCW();
|
||||
switch (s) {
|
||||
case LEFT_HIGH:
|
||||
left.skew = Skew.BALANCED;
|
||||
right.skew = Skew.RIGHT_HIGH;
|
||||
break;
|
||||
case RIGHT_HIGH:
|
||||
left.skew = Skew.LEFT_HIGH;
|
||||
right.skew = Skew.BALANCED;
|
||||
break;
|
||||
default:
|
||||
left.skew = Skew.BALANCED;
|
||||
right.skew = Skew.BALANCED;
|
||||
}
|
||||
skew = Skew.BALANCED;
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
skew = Skew.RIGHT_HIGH;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Re-balance the instance as right sub-tree has shrunk.
|
||||
* @return true if the parent tree should be reSkew.BALANCED too
|
||||
*/
|
||||
private boolean rebalanceRightShrunk() {
|
||||
switch (skew) {
|
||||
case RIGHT_HIGH:
|
||||
skew = Skew.BALANCED;
|
||||
return true;
|
||||
case LEFT_HIGH:
|
||||
if (left.skew == Skew.LEFT_HIGH) {
|
||||
rotateCW();
|
||||
skew = Skew.BALANCED;
|
||||
right.skew = Skew.BALANCED;
|
||||
return true;
|
||||
} else if (left.skew == Skew.BALANCED) {
|
||||
rotateCW();
|
||||
skew = Skew.RIGHT_HIGH;
|
||||
right.skew = Skew.LEFT_HIGH;
|
||||
return false;
|
||||
} else {
|
||||
final Skew s = left.right.skew;
|
||||
left.rotateCCW();
|
||||
rotateCW();
|
||||
switch (s) {
|
||||
case LEFT_HIGH:
|
||||
left.skew = Skew.BALANCED;
|
||||
right.skew = Skew.RIGHT_HIGH;
|
||||
break;
|
||||
case RIGHT_HIGH:
|
||||
left.skew = Skew.LEFT_HIGH;
|
||||
right.skew = Skew.BALANCED;
|
||||
break;
|
||||
default:
|
||||
left.skew = Skew.BALANCED;
|
||||
right.skew = Skew.BALANCED;
|
||||
}
|
||||
skew = Skew.BALANCED;
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
skew = Skew.LEFT_HIGH;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Perform a clockwise rotation rooted at the instance.
|
||||
* <p>The skew factor are not updated by this method, they
|
||||
* <em>must</em> be updated by the caller</p>
|
||||
*/
|
||||
private void rotateCW() {
|
||||
|
||||
final T tmpElt = element;
|
||||
element = left.element;
|
||||
left.element = tmpElt;
|
||||
|
||||
final Node tmpNode = left;
|
||||
left = tmpNode.left;
|
||||
tmpNode.left = tmpNode.right;
|
||||
tmpNode.right = right;
|
||||
right = tmpNode;
|
||||
|
||||
if (left != null) {
|
||||
left.parent = this;
|
||||
}
|
||||
if (right.right != null) {
|
||||
right.right.parent = right;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** Perform a counter-clockwise rotation rooted at the instance.
|
||||
* <p>The skew factor are not updated by this method, they
|
||||
* <em>must</em> be updated by the caller</p>
|
||||
*/
|
||||
private void rotateCCW() {
|
||||
|
||||
final T tmpElt = element;
|
||||
element = right.element;
|
||||
right.element = tmpElt;
|
||||
|
||||
final Node tmpNode = right;
|
||||
right = tmpNode.right;
|
||||
tmpNode.right = tmpNode.left;
|
||||
tmpNode.left = left;
|
||||
left = tmpNode;
|
||||
|
||||
if (right != null) {
|
||||
right.parent = this;
|
||||
}
|
||||
if (left.left != null) {
|
||||
left.left.parent = left;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,417 @@
|
|||
/*
|
||||
* 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.math.geometry.partitioning.utilities;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
|
||||
/** This class implements an ordering operation for T-uples.
|
||||
|
||||
* <p>Ordering is done by encoding all components of the T-uple into a
|
||||
* single scalar value and using this value as the sorting
|
||||
* key. Encoding is performed using the method invented by Georg
|
||||
* Cantor in 1877 when he proved it was possible to establish a
|
||||
* bijection between a line and a plane. The binary representations of
|
||||
* the components of the T-uple are mixed together to form a single
|
||||
* scalar. This means that the 2<sup>k</sup> bit of component 0 is
|
||||
* followed by the 2<sup>k</sup> bit of component 1, then by the
|
||||
* 2<sup>k</sup> bit of component 2 up to the 2<sup>k</sup> bit of
|
||||
* component {@code t}, which is followed by the 2<sup>k-1</sup>
|
||||
* bit of component 0, followed by the 2<sup>k-1</sup> bit of
|
||||
* component 1 ... The binary representations are extended as needed
|
||||
* to handle numbers with different scales and a suitable
|
||||
* 2<sup>p</sup> offset is added to the components in order to avoid
|
||||
* negative numbers (this offset is adjusted as needed during the
|
||||
* comparison operations).</p>
|
||||
|
||||
* <p>The more interesting property of the encoding method for our
|
||||
* purpose is that it allows to select all the points that are in a
|
||||
* given range. This is depicted in dimension 2 by the following
|
||||
* picure:</p>
|
||||
|
||||
* <img src="doc-files/OrderedTuple.png" />
|
||||
|
||||
* <p>This picture shows a set of 100000 random 2-D pairs having their
|
||||
* first component between -50 and +150 and their second component
|
||||
* between -350 and +50. We wanted to extract all pairs having their
|
||||
* first component between +30 and +70 and their second component
|
||||
* between -120 and -30. We built the lower left point at coordinates
|
||||
* (30, -120) and the upper right point at coordinates (70, -30). All
|
||||
* points smaller than the lower left point are drawn in red and all
|
||||
* points larger than the upper right point are drawn in blue. The
|
||||
* green points are between the two limits. This picture shows that
|
||||
* all the desired points are selected, along with spurious points. In
|
||||
* this case, we get 15790 points, 4420 of which really belonging to
|
||||
* the desired rectangle. It is possible to extract very small
|
||||
* subsets. As an example extracting from the same 100000 points set
|
||||
* the points having their first component between +30 and +31 and
|
||||
* their second component between -91 and -90, we get a subset of 11
|
||||
* points, 2 of which really belonging to the desired rectangle.</p>
|
||||
|
||||
* <p>the previous selection technique can be applied in all
|
||||
* dimensions, still using two points to define the interval. The
|
||||
* first point will have all its components set to their lower bounds
|
||||
* while the second point will have all its components set to their
|
||||
* upper bounds.</p>
|
||||
|
||||
* <p>T-uples with negative infinite or positive infinite components
|
||||
* are sorted logically.</p>
|
||||
|
||||
* <p>Since the specification of the {@code Comparator} interface
|
||||
* allows only {@code ClassCastException} errors, some arbitrary
|
||||
* choices have been made to handle specific cases. The rationale for
|
||||
* these choices is to keep <em>regular</em> and consistent T-uples
|
||||
* together.</p>
|
||||
* <ul>
|
||||
* <li>instances with different dimensions are sorted according to
|
||||
* their dimension regardless of their components values</li>
|
||||
* <li>instances with {@code Double.NaN} components are sorted
|
||||
* after all other ones (even after instances with positive infinite
|
||||
* components</li>
|
||||
* <li>instances with both positive and negative infinite components
|
||||
* are considered as if they had {@code Double.NaN}
|
||||
* components</li>
|
||||
* </ul>
|
||||
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public class OrderedTuple implements Comparable<OrderedTuple> {
|
||||
|
||||
/** Sign bit mask. */
|
||||
private static final long SIGN_MASK = 0x8000000000000000L;
|
||||
|
||||
/** Exponent bits mask. */
|
||||
private static final long EXPONENT_MASK = 0x7ff0000000000000L;
|
||||
|
||||
/** Mantissa bits mask. */
|
||||
private static final long MANTISSA_MASK = 0x000fffffffffffffL;
|
||||
|
||||
/** Implicit MSB for normalized numbers. */
|
||||
private static final long IMPLICIT_ONE = 0x0010000000000000L;
|
||||
|
||||
/** Double components of the T-uple. */
|
||||
private double[] components;
|
||||
|
||||
/** Offset scale. */
|
||||
private int offset;
|
||||
|
||||
/** Least Significant Bit scale. */
|
||||
private int lsb;
|
||||
|
||||
/** Ordering encoding of the double components. */
|
||||
private long[] encoding;
|
||||
|
||||
/** Positive infinity marker. */
|
||||
private boolean posInf;
|
||||
|
||||
/** Negative infinity marker. */
|
||||
private boolean negInf;
|
||||
|
||||
/** Not A Number marker. */
|
||||
private boolean nan;
|
||||
|
||||
/** Build an ordered T-uple from its components.
|
||||
* @param components double components of the T-uple
|
||||
*/
|
||||
public OrderedTuple(final double ... components) {
|
||||
this.components = components.clone();
|
||||
int msb = Integer.MIN_VALUE;
|
||||
lsb = Integer.MAX_VALUE;
|
||||
posInf = false;
|
||||
negInf = false;
|
||||
nan = false;
|
||||
for (int i = 0; i < components.length; ++i) {
|
||||
if (Double.isInfinite(components[i])) {
|
||||
if (components[i] < 0) {
|
||||
negInf = true;
|
||||
} else {
|
||||
posInf = true;
|
||||
}
|
||||
} else if (Double.isNaN(components[i])) {
|
||||
nan = true;
|
||||
} else {
|
||||
final long b = Double.doubleToLongBits(components[i]);
|
||||
final long m = mantissa(b);
|
||||
if (m != 0) {
|
||||
final int e = exponent(b);
|
||||
msb = FastMath.max(msb, e + computeMSB(m));
|
||||
lsb = FastMath.min(lsb, e + computeLSB(m));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (posInf && negInf) {
|
||||
// instance cannot be sorted logically
|
||||
posInf = false;
|
||||
negInf = false;
|
||||
nan = true;
|
||||
}
|
||||
|
||||
if (lsb <= msb) {
|
||||
// encode the T-upple with the specified offset
|
||||
encode(msb + 16);
|
||||
} else {
|
||||
encoding = new long[] {
|
||||
0x0L
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** Encode the T-uple with a given offset.
|
||||
* @param minOffset minimal scale of the offset to add to all
|
||||
* components (must be greater than the MSBs of all components)
|
||||
*/
|
||||
private void encode(final int minOffset) {
|
||||
|
||||
// choose an offset with some margins
|
||||
offset = minOffset + 31;
|
||||
offset -= offset % 32;
|
||||
|
||||
if ((encoding != null) && (encoding.length == 1) && (encoding[0] == 0x0L)) {
|
||||
// the components are all zeroes
|
||||
return;
|
||||
}
|
||||
|
||||
// allocate an integer array to encode the components (we use only
|
||||
// 63 bits per element because there is no unsigned long in Java)
|
||||
final int neededBits = offset + 1 - lsb;
|
||||
final int neededLongs = (neededBits + 62) / 63;
|
||||
encoding = new long[components.length * neededLongs];
|
||||
|
||||
// mix the bits from all components
|
||||
int eIndex = 0;
|
||||
int shift = 62;
|
||||
long word = 0x0L;
|
||||
for (int k = offset; eIndex < encoding.length; --k) {
|
||||
for (int vIndex = 0; vIndex < components.length; ++vIndex) {
|
||||
if (getBit(vIndex, k) != 0) {
|
||||
word |= 0x1L << shift;
|
||||
}
|
||||
if (shift-- == 0) {
|
||||
encoding[eIndex++] = word;
|
||||
word = 0x0L;
|
||||
shift = 62;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** Compares this ordered T-uple with the specified object.
|
||||
|
||||
* <p>The ordering method is detailed in the general description of
|
||||
* the class. Its main property is to be consistent with distance:
|
||||
* geometrically close T-uples stay close to each other when stored
|
||||
* in a sorted collection using this comparison method.</p>
|
||||
|
||||
* <p>T-uples with negative infinite, positive infinite are sorted
|
||||
* logically.</p>
|
||||
|
||||
* <p>Some arbitrary choices have been made to handle specific
|
||||
* cases. The rationale for these choices is to keep
|
||||
* <em>normal</em> and consistent T-uples together.</p>
|
||||
* <ul>
|
||||
* <li>instances with different dimensions are sorted according to
|
||||
* their dimension regardless of their components values</li>
|
||||
* <li>instances with {@code Double.NaN} components are sorted
|
||||
* after all other ones (evan after instances with positive infinite
|
||||
* components</li>
|
||||
* <li>instances with both positive and negative infinite components
|
||||
* are considered as if they had {@code Double.NaN}
|
||||
* components</li>
|
||||
* </ul>
|
||||
|
||||
* @param ot T-uple to compare instance with
|
||||
* @return a negative integer if the instance is less than the
|
||||
* object, zero if they are equal, or a positive integer if the
|
||||
* instance is greater than the object
|
||||
|
||||
*/
|
||||
public int compareTo(final OrderedTuple ot) {
|
||||
if (components.length == ot.components.length) {
|
||||
if (nan) {
|
||||
return +1;
|
||||
} else if (ot.nan) {
|
||||
return -1;
|
||||
} else if (negInf || ot.posInf) {
|
||||
return -1;
|
||||
} else if (posInf || ot.negInf) {
|
||||
return +1;
|
||||
} else {
|
||||
|
||||
if (offset < ot.offset) {
|
||||
encode(ot.offset);
|
||||
} else if (offset > ot.offset) {
|
||||
ot.encode(offset);
|
||||
}
|
||||
|
||||
final int limit = FastMath.min(encoding.length, ot.encoding.length);
|
||||
for (int i = 0; i < limit; ++i) {
|
||||
if (encoding[i] < ot.encoding[i]) {
|
||||
return -1;
|
||||
} else if (encoding[i] > ot.encoding[i]) {
|
||||
return +1;
|
||||
}
|
||||
}
|
||||
|
||||
if (encoding.length < ot.encoding.length) {
|
||||
return -1;
|
||||
} else if (encoding.length > ot.encoding.length) {
|
||||
return +1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return components.length - ot.components.length;
|
||||
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public boolean equals(final Object other) {
|
||||
if (this == other) {
|
||||
return true;
|
||||
} else if (other instanceof OrderedTuple) {
|
||||
return compareTo((OrderedTuple) other) == 0;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(components) ^
|
||||
((Integer) offset).hashCode() ^
|
||||
((Integer) lsb).hashCode() ^
|
||||
((Boolean) posInf).hashCode() ^
|
||||
((Boolean) negInf).hashCode() ^
|
||||
((Boolean) nan).hashCode();
|
||||
}
|
||||
|
||||
/** Get the components array.
|
||||
* @return array containing the T-uple components
|
||||
*/
|
||||
public double[] getComponents() {
|
||||
return components.clone();
|
||||
}
|
||||
|
||||
/** Extract the sign from the bits of a double.
|
||||
* @param bits binary representation of the double
|
||||
* @return sign bit (zero if positive, non zero if negative)
|
||||
*/
|
||||
private static long sign(final long bits) {
|
||||
return bits & SIGN_MASK;
|
||||
}
|
||||
|
||||
/** Extract the exponent from the bits of a double.
|
||||
* @param bits binary representation of the double
|
||||
* @return exponent
|
||||
*/
|
||||
private static int exponent(final long bits) {
|
||||
return ((int) ((bits & EXPONENT_MASK) >> 52)) - 1075;
|
||||
}
|
||||
|
||||
/** Extract the mantissa from the bits of a double.
|
||||
* @param bits binary representation of the double
|
||||
* @return mantissa
|
||||
*/
|
||||
private static long mantissa(final long bits) {
|
||||
return ((bits & EXPONENT_MASK) == 0) ?
|
||||
((bits & MANTISSA_MASK) << 1) : // subnormal number
|
||||
(IMPLICIT_ONE | (bits & MANTISSA_MASK)); // normal number
|
||||
}
|
||||
|
||||
/** Compute the most significant bit of a long.
|
||||
* @param l long from which the most significant bit is requested
|
||||
* @return scale of the most significant bit of {@code l},
|
||||
* or 0 if {@code l} is zero
|
||||
* @see #computeLSB
|
||||
*/
|
||||
private static int computeMSB(final long l) {
|
||||
|
||||
long ll = l;
|
||||
long mask = 0xffffffffL;
|
||||
int scale = 32;
|
||||
int msb = 0;
|
||||
|
||||
while (scale != 0) {
|
||||
if ((ll & mask) != ll) {
|
||||
msb |= scale;
|
||||
ll = ll >> scale;
|
||||
}
|
||||
scale = scale >> 1;
|
||||
mask = mask >> scale;
|
||||
}
|
||||
|
||||
return msb;
|
||||
|
||||
}
|
||||
|
||||
/** Compute the least significant bit of a long.
|
||||
* @param l long from which the least significant bit is requested
|
||||
* @return scale of the least significant bit of {@code l},
|
||||
* or 63 if {@code l} is zero
|
||||
* @see #computeMSB
|
||||
*/
|
||||
private static int computeLSB(final long l) {
|
||||
|
||||
long ll = l;
|
||||
long mask = 0xffffffff00000000L;
|
||||
int scale = 32;
|
||||
int lsb = 0;
|
||||
|
||||
while (scale != 0) {
|
||||
if ((ll & mask) == ll) {
|
||||
lsb |= scale;
|
||||
ll = ll >> scale;
|
||||
}
|
||||
scale = scale >> 1;
|
||||
mask = mask >> scale;
|
||||
}
|
||||
|
||||
return lsb;
|
||||
|
||||
}
|
||||
|
||||
/** Get a bit from the mantissa of a double.
|
||||
* @param i index of the component
|
||||
* @param k scale of the requested bit
|
||||
* @return the specified bit (either 0 or 1), after the offset has
|
||||
* been added to the double
|
||||
*/
|
||||
private int getBit(final int i, final int k) {
|
||||
final long bits = Double.doubleToLongBits(components[i]);
|
||||
final int e = exponent(bits);
|
||||
if ((k < e) || (k > offset)) {
|
||||
return 0;
|
||||
} else if (k == offset) {
|
||||
return (sign(bits) == 0L) ? 1 : 0;
|
||||
} else if (k > (e + 52)) {
|
||||
return (sign(bits) == 0L) ? 0 : 1;
|
||||
} else {
|
||||
final long m = (sign(bits) == 0L) ? mantissa(bits) : -mantissa(bits);
|
||||
return (int) ((m >> (k - e)) & 0x1L);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 28 KiB |
|
@ -0,0 +1,24 @@
|
|||
<html>
|
||||
<!--
|
||||
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.
|
||||
-->
|
||||
<!-- $Revision$ -->
|
||||
<body>
|
||||
<p>
|
||||
This package provides multidimensional ordering features for partitioning.
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
|
@ -52,6 +52,16 @@ The <action> type attribute can be add,update,fix,remove.
|
|||
If the output is not quite correct, check for invisible trailing spaces!
|
||||
-->
|
||||
<release version="3.0" date="TBD" description="TBD">
|
||||
<action dev="luc" type="add" issue="MATH-576">
|
||||
A complete generic implementation of Binary Space Partitioning Trees (BSP trees)
|
||||
has been added. A few specializations of this implementation are also provided
|
||||
for 1D, 2D and 3D Euclidean geometry. This allows support for arbitrary
|
||||
intervals sets (1D), polygons sets (2D) and polyhedrons sets (3D) with all
|
||||
sets operations (union, intersection, symmetric difference, difference, complement),
|
||||
with predicates (point inside/outside/on boundary, emptiness, other region contained),
|
||||
with geometrical computation (barycenter, size, boundary size) and with conversions
|
||||
from and to boundary representation.
|
||||
</action>
|
||||
<action dev="luc" type="fix" issue="MATH-573">
|
||||
Avoid some array copying in add and subtract ArrayFieldVector.
|
||||
</action>
|
||||
|
|
|
@ -30,12 +30,41 @@
|
|||
<subsection name="11.1 Overview" href="overview">
|
||||
<p>
|
||||
The geometry package provides classes useful for many physical simulations
|
||||
in the real 3D space, namely vectors and rotations.
|
||||
in Euclidean spaces, like vectors and rotations in 3D, as well as a general
|
||||
implentation of Binary Space Partitioning Trees (BSP trees).
|
||||
</p>
|
||||
</subsection>
|
||||
<subsection name="11.2 Vectors" href="vectors">
|
||||
<subsection name="11.2 Euclidean spaces" href="euclidean">
|
||||
<p>
|
||||
<a href="../apidocs/org/apache/commons/math/geometry/euclidean/oneD/Interval.html">
|
||||
Interval</a> and <a href="../apidocs/org/apache/commons/math/geometry/euclidean/oneD/IntervalsSet.html">
|
||||
IntervalsSet</a> represent one dimensional regions. All classical set operations are available
|
||||
for intervals sets: union, intersection, symmetric difference (exclusive or), difference, complement,
|
||||
as well as region predicates (point inside/outside/on boundary, emptiness, other region contained).
|
||||
It is also possible to compute geometrical properties like size, barycenter or boundary size.
|
||||
Intervals sets can be built by constructive geometry (union, intersection ...) or from a boundary
|
||||
representation.
|
||||
</p>
|
||||
<p>
|
||||
<a href="../apidocs/org/apache/commons/math/geometry/euclidean/twoD/PolygonsSet.html">
|
||||
PolygonsSet</a> represent two dimensional regions. All classical set operations are available
|
||||
for polygons sets: union, intersection, symmetric difference (exclusive or), difference, complement,
|
||||
as well as region predicates (point inside/outside/on boundary, emptiness, other region contained).
|
||||
It is also possible to compute geometrical properties like size, barycenter or boundary size and
|
||||
to extract the vertices. Polygons sets can be built by constructive geometry (union, intersection ...)
|
||||
or from a boundary representation.
|
||||
</p>
|
||||
<p>
|
||||
<a href="../apidocs/org/apache/commons/math/geometry/euclidean/threeD/PolyhedronsSet.html">
|
||||
PolyhedronsSet</a> represent three dimensional regions. All classical set operations are available
|
||||
for polyhedrons sets: union, intersection, symmetric difference (exclusive or), difference, complement,
|
||||
as well as region predicates (point inside/outside/on boundary, emptiness, other region contained).
|
||||
It is also possible to compute geometrical properties like size, barycenter or boundary size and
|
||||
to extract the vertices. Polyhedrons sets can be built by constructive geometry (union, intersection ...)
|
||||
or from a boundary representation.
|
||||
</p>
|
||||
<p>
|
||||
<a href="../apidocs/org/apache/commons/math/geometry/Vector3D.html">
|
||||
<a href="../apidocs/org/apache/commons/math/geometry/euclidean/threeD/Vector3D.html">
|
||||
Vector3D</a> provides a simple vector type. One important feature is
|
||||
that instances of this class are guaranteed
|
||||
to be immutable, this greatly simplifies modelling dynamical systems
|
||||
|
@ -57,14 +86,12 @@
|
|||
is of course also implemented.
|
||||
</p>
|
||||
<p>
|
||||
<a href="../apidocs/org/apache/commons/math/geometry/Vector3DFormat.html">
|
||||
<a href="../apidocs/org/apache/commons/math/geometry/euclidean/threeD/Vector3DFormat.html">
|
||||
Vector3DFormat</a> is a specialized format for formatting output or parsing
|
||||
input with text representation of 3D vectors.
|
||||
</p>
|
||||
</subsection>
|
||||
<subsection name="11.3 Rotations" href="rotations">
|
||||
<p>
|
||||
<a href="../apidocs/org/apache/commons/math/geometry/Rotation.html">
|
||||
<a href="../apidocs/org/apache/commons/math/geometry/euclidean/threeD/Rotation.html">
|
||||
Rotation</a> represents 3D rotations.
|
||||
Rotation instances are also immutable objects, as Vector3D instances.
|
||||
</p>
|
||||
|
@ -136,6 +163,41 @@
|
|||
<code>applyInverseTo(Rotation)</code>.
|
||||
</p>
|
||||
</subsection>
|
||||
<subsection name="11.3 Binary Space Partitioning" href="partitioning">
|
||||
<p>
|
||||
<a href="../apidocs/org/apache/commons/math/geometry/partitioning/BSPTree.html">
|
||||
BSP trees</a> are an efficient way to represent space partitions and
|
||||
to associate attributes with each cell. Each node in a BSP tree
|
||||
represents a convex region which is partitioned in two convex
|
||||
sub-regions at each side of a cut hyperplane. The root tree
|
||||
contains the complete space.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
The main use of such partitions is to use a boolean attribute to
|
||||
define an inside/outside property, hence representing arbitrary
|
||||
polytopes (line segments in 1D, polygons in 2D and polyhedrons in
|
||||
3D) and to operate on them.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Another example would be to represent Voronoi tesselations, the
|
||||
attribute of each cell holding the defining point of the cell.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
The application-defined attributes are shared among copied
|
||||
instances and propagated to split parts. These attributes are not
|
||||
used by the BSP-tree algorithms themselves, so the application can
|
||||
use them for any purpose. Since the tree visiting method holds
|
||||
internal and leaf nodes differently, it is possible to use
|
||||
different classes for internal nodes attributes and leaf nodes
|
||||
attributes. This should be used with care, though, because if the
|
||||
tree is modified in any way after attributes have been set, some
|
||||
internal nodes may become leaf nodes and some leaf nodes may become
|
||||
internal nodes.
|
||||
</p>
|
||||
</subsection>
|
||||
</section>
|
||||
</body>
|
||||
</document>
|
||||
|
|
|
@ -83,7 +83,7 @@
|
|||
<li><a href="distribution.html">org.apache.commons.math.distribution</a> - probability distributions</li>
|
||||
<li><a href="fraction.html">org.apache.commons.math.fraction</a> - rational numbers</li>
|
||||
<li><a href="transform.html">org.apache.commons.math.transform</a> - transform methods (Fast Fourier)</li>
|
||||
<li><a href="geometry.html">org.apache.commons.math.geometry</a> - 3D geometry (vectors and rotations)</li>
|
||||
<li><a href="geometry.html">org.apache.commons.math.geometry</a> - geometry (Euclidean spaces and Binary Space Partitioning)</li>
|
||||
<li><a href="optimization.html">org.apache.commons.math.optimization</a> - function maximization or minimization</li>
|
||||
<li><a href="ode.html">org.apache.commons.math.ode</a> - Ordinary Differential Equations integration</li>
|
||||
<li><a href="genetics.html">org.apache.commons.math.genetics</a> - Genetic Algorithms</li>
|
||||
|
|
|
@ -0,0 +1,96 @@
|
|||
/*
|
||||
* 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.math.geometry.euclidean.oneD;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.math.geometry.euclidean.oneD.Interval;
|
||||
import org.apache.commons.math.geometry.euclidean.oneD.IntervalsSet;
|
||||
import org.apache.commons.math.geometry.euclidean.oneD.Point1D;
|
||||
import org.apache.commons.math.geometry.partitioning.Region;
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class IntervalsSetTest {
|
||||
|
||||
@Test
|
||||
public void testInterval() {
|
||||
IntervalsSet set = new IntervalsSet(2.3, 5.7);
|
||||
Assert.assertEquals(3.4, set.getSize(), 1.0e-10);
|
||||
Assert.assertEquals(4.0, ((Point1D) set.getBarycenter()).getAbscissa(), 1.0e-10);
|
||||
Assert.assertEquals(Region.Location.BOUNDARY, set.checkPoint(new Point1D(2.3)));
|
||||
Assert.assertEquals(Region.Location.BOUNDARY, set.checkPoint(new Point1D(5.7)));
|
||||
Assert.assertEquals(Region.Location.OUTSIDE, set.checkPoint(new Point1D(1.2)));
|
||||
Assert.assertEquals(Region.Location.OUTSIDE, set.checkPoint(new Point1D(8.7)));
|
||||
Assert.assertEquals(Region.Location.INSIDE, set.checkPoint(new Point1D(3.0)));
|
||||
Assert.assertEquals(2.3, set.getInf(), 1.0e-10);
|
||||
Assert.assertEquals(5.7, set.getSup(), 1.0e-10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInfinite() {
|
||||
IntervalsSet set = new IntervalsSet(9.0, Double.POSITIVE_INFINITY);
|
||||
Assert.assertEquals(Region.Location.BOUNDARY, set.checkPoint(new Point1D(9.0)));
|
||||
Assert.assertEquals(Region.Location.OUTSIDE, set.checkPoint(new Point1D(8.4)));
|
||||
for (double e = 1.0; e <= 6.0; e += 1.0) {
|
||||
Assert.assertEquals(Region.Location.INSIDE,
|
||||
set.checkPoint(new Point1D(FastMath.pow(10.0, e))));
|
||||
}
|
||||
Assert.assertTrue(Double.isInfinite(set.getSize()));
|
||||
Assert.assertEquals(9.0, set.getInf(), 1.0e-10);
|
||||
Assert.assertTrue(Double.isInfinite(set.getSup()));
|
||||
|
||||
set = (IntervalsSet) set.getComplement();
|
||||
Assert.assertEquals(9.0, set.getSup(), 1.0e-10);
|
||||
Assert.assertTrue(Double.isInfinite(set.getInf()));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiple() {
|
||||
IntervalsSet set = (IntervalsSet)
|
||||
Region.intersection(Region.union(Region.difference(new IntervalsSet(1.0, 6.0),
|
||||
new IntervalsSet(3.0, 5.0)),
|
||||
new IntervalsSet(9.0, Double.POSITIVE_INFINITY)),
|
||||
new IntervalsSet(Double.NEGATIVE_INFINITY, 11.0));
|
||||
Assert.assertEquals(5.0, set.getSize(), 1.0e-10);
|
||||
Assert.assertEquals(5.9, ((Point1D) set.getBarycenter()).getAbscissa(), 1.0e-10);
|
||||
Assert.assertEquals(Region.Location.OUTSIDE, set.checkPoint(new Point1D(0.0)));
|
||||
Assert.assertEquals(Region.Location.OUTSIDE, set.checkPoint(new Point1D(4.0)));
|
||||
Assert.assertEquals(Region.Location.OUTSIDE, set.checkPoint(new Point1D(8.0)));
|
||||
Assert.assertEquals(Region.Location.OUTSIDE, set.checkPoint(new Point1D(12.0)));
|
||||
Assert.assertEquals(Region.Location.INSIDE, set.checkPoint(new Point1D(1.2)));
|
||||
Assert.assertEquals(Region.Location.INSIDE, set.checkPoint(new Point1D(5.9)));
|
||||
Assert.assertEquals(Region.Location.INSIDE, set.checkPoint(new Point1D(9.01)));
|
||||
Assert.assertEquals(Region.Location.BOUNDARY, set.checkPoint(new Point1D(5.0)));
|
||||
Assert.assertEquals(Region.Location.BOUNDARY, set.checkPoint(new Point1D(11.0)));
|
||||
Assert.assertEquals( 1.0, set.getInf(), 1.0e-10);
|
||||
Assert.assertEquals(11.0, set.getSup(), 1.0e-10);
|
||||
|
||||
List<Interval> list = set.asList();
|
||||
Assert.assertEquals(3, list.size());
|
||||
Assert.assertEquals( 1.0, list.get(0).getLower(), 1.0e-10);
|
||||
Assert.assertEquals( 3.0, list.get(0).getUpper(), 1.0e-10);
|
||||
Assert.assertEquals( 5.0, list.get(1).getLower(), 1.0e-10);
|
||||
Assert.assertEquals( 6.0, list.get(1).getUpper(), 1.0e-10);
|
||||
Assert.assertEquals( 9.0, list.get(2).getLower(), 1.0e-10);
|
||||
Assert.assertEquals(11.0, list.get(2).getUpper(), 1.0e-10);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -15,7 +15,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math.geometry;
|
||||
package org.apache.commons.math.geometry.euclidean.threeD;
|
||||
|
||||
import java.util.Locale;
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
* 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.math.geometry.euclidean.threeD;
|
||||
|
||||
import org.apache.commons.math.geometry.euclidean.threeD.Line;
|
||||
import org.apache.commons.math.geometry.euclidean.threeD.Vector3D;
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class LineTest {
|
||||
|
||||
@Test
|
||||
public void testContains() {
|
||||
Vector3D p1 = new Vector3D(0, 0, 1);
|
||||
Line l = new Line(p1, new Vector3D(0, 0, 1));
|
||||
Assert.assertTrue(l.contains(p1));
|
||||
Assert.assertTrue(l.contains(new Vector3D(1.0, p1, 0.3, l.getDirection())));
|
||||
Vector3D u = l.getDirection().orthogonal();
|
||||
Vector3D v = Vector3D.crossProduct(l.getDirection(), u);
|
||||
for (double alpha = 0; alpha < 2 * FastMath.PI; alpha += 0.3) {
|
||||
Assert.assertTrue(! l.contains(p1.add(new Vector3D(FastMath.cos(alpha), u,
|
||||
FastMath.sin(alpha), v))));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimilar() {
|
||||
Vector3D p1 = new Vector3D (1.2, 3.4, -5.8);
|
||||
Vector3D p2 = new Vector3D (3.4, -5.8, 1.2);
|
||||
Line lA = new Line(p1, p2.subtract(p1));
|
||||
Line lB = new Line(p2, p1.subtract(p2));
|
||||
Assert.assertTrue(lA.isSimilarTo(lB));
|
||||
Assert.assertTrue(! lA.isSimilarTo(new Line(p1, lA.getDirection().orthogonal())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPointDistance() {
|
||||
Line l = new Line(new Vector3D(0, 1, 1), new Vector3D(0, 1, 1));
|
||||
Assert.assertEquals(FastMath.sqrt(3.0 / 2.0), l.distance(new Vector3D(1, 0, 1)), 1.0e-10);
|
||||
Assert.assertEquals(0, l.distance(new Vector3D(0, -4, -4)), 1.0e-10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLineDistance() {
|
||||
Line l = new Line(new Vector3D(0, 1, 1), new Vector3D(0, 1, 1));
|
||||
Assert.assertEquals(1.0,
|
||||
l.distance(new Line(new Vector3D(1, 0, 1), Vector3D.PLUS_K)),
|
||||
1.0e-10);
|
||||
Assert.assertEquals(0.5,
|
||||
l.distance(new Line(new Vector3D(-0.5, 0, 0), new Vector3D(0, -1, -1))),
|
||||
1.0e-10);
|
||||
Assert.assertEquals(0.0,
|
||||
l.distance(l),
|
||||
1.0e-10);
|
||||
Assert.assertEquals(0.0,
|
||||
l.distance(new Line(new Vector3D(0, -4, -4), new Vector3D(0, -1, -1))),
|
||||
1.0e-10);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,168 @@
|
|||
/*
|
||||
* 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.math.geometry.euclidean.threeD;
|
||||
|
||||
import org.apache.commons.math.geometry.euclidean.threeD.Line;
|
||||
import org.apache.commons.math.geometry.euclidean.threeD.Plane;
|
||||
import org.apache.commons.math.geometry.euclidean.threeD.Point3D;
|
||||
import org.apache.commons.math.geometry.euclidean.threeD.Rotation;
|
||||
import org.apache.commons.math.geometry.euclidean.threeD.Vector3D;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PlaneTest {
|
||||
|
||||
@Test
|
||||
public void testContains() {
|
||||
Plane p = new Plane(new Vector3D(0, 0, 1), new Vector3D(0, 0, 1));
|
||||
Assert.assertTrue(p.contains(new Point3D(0, 0, 1)));
|
||||
Assert.assertTrue(p.contains(new Point3D(17, -32, 1)));
|
||||
Assert.assertTrue(! p.contains(new Point3D(17, -32, 1.001)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOffset() {
|
||||
Vector3D p1 = new Vector3D(1, 1, 1);
|
||||
Plane p = new Plane(p1, new Vector3D(0.2, 0, 0));
|
||||
Assert.assertEquals(-5.0, p.getOffset(new Point3D(-4, 0, 0)), 1.0e-10);
|
||||
Assert.assertEquals(+5.0, p.getOffset(new Point3D(6, 10, -12)), 1.0e-10);
|
||||
Assert.assertEquals(0.3,
|
||||
p.getOffset(new Point3D(1.0, p1, 0.3, p.getNormal())),
|
||||
1.0e-10);
|
||||
Assert.assertEquals(-0.3,
|
||||
p.getOffset(new Point3D(1.0, p1, -0.3, p.getNormal())),
|
||||
1.0e-10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPoint() {
|
||||
Plane p = new Plane(new Vector3D(2, -3, 1), new Vector3D(1, 4, 9));
|
||||
Assert.assertTrue(p.contains(p.getOrigin()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThreePoints() {
|
||||
Point3D p1 = new Point3D(1.2, 3.4, -5.8);
|
||||
Point3D p2 = new Point3D(3.4, -5.8, 1.2);
|
||||
Point3D p3 = new Point3D(-2.0, 4.3, 0.7);
|
||||
Plane p = new Plane(p1, p2, p3);
|
||||
Assert.assertTrue(p.contains(p1));
|
||||
Assert.assertTrue(p.contains(p2));
|
||||
Assert.assertTrue(p.contains(p3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRotate() {
|
||||
Point3D p1 = new Point3D(1.2, 3.4, -5.8);
|
||||
Point3D p2 = new Point3D(3.4, -5.8, 1.2);
|
||||
Point3D p3 = new Point3D(-2.0, 4.3, 0.7);
|
||||
Plane p = new Plane(p1, p2, p3);
|
||||
Vector3D oldNormal = p.getNormal();
|
||||
|
||||
p = p.rotate(p2, new Rotation(p2.subtract(p1), 1.7));
|
||||
Assert.assertTrue(p.contains(p1));
|
||||
Assert.assertTrue(p.contains(p2));
|
||||
Assert.assertTrue(! p.contains(p3));
|
||||
|
||||
p = p.rotate(p2, new Rotation(oldNormal, 0.1));
|
||||
Assert.assertTrue(! p.contains(p1));
|
||||
Assert.assertTrue(p.contains(p2));
|
||||
Assert.assertTrue(! p.contains(p3));
|
||||
|
||||
p = p.rotate(p1, new Rotation(oldNormal, 0.1));
|
||||
Assert.assertTrue(! p.contains(p1));
|
||||
Assert.assertTrue(! p.contains(p2));
|
||||
Assert.assertTrue(! p.contains(p3));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTranslate() {
|
||||
Point3D p1 = new Point3D(1.2, 3.4, -5.8);
|
||||
Point3D p2 = new Point3D(3.4, -5.8, 1.2);
|
||||
Point3D p3 = new Point3D(-2.0, 4.3, 0.7);
|
||||
Plane p = new Plane(p1, p2, p3);
|
||||
|
||||
p = p.translate(new Vector3D(2.0, p.getU(), -1.5, p.getV()));
|
||||
Assert.assertTrue(p.contains(p1));
|
||||
Assert.assertTrue(p.contains(p2));
|
||||
Assert.assertTrue(p.contains(p3));
|
||||
|
||||
p = p.translate(new Vector3D(-1.2, p.getNormal()));
|
||||
Assert.assertTrue(! p.contains(p1));
|
||||
Assert.assertTrue(! p.contains(p2));
|
||||
Assert.assertTrue(! p.contains(p3));
|
||||
|
||||
p = p.translate(new Vector3D(+1.2, p.getNormal()));
|
||||
Assert.assertTrue(p.contains(p1));
|
||||
Assert.assertTrue(p.contains(p2));
|
||||
Assert.assertTrue(p.contains(p3));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntersection() {
|
||||
Plane p = new Plane(new Vector3D(1, 2, 3), new Vector3D(-4, 1, -5));
|
||||
Line l = new Line(new Vector3D(0.2, -3.5, 0.7), new Vector3D(1, 1, -1));
|
||||
Point3D point = p.intersection(l);
|
||||
Assert.assertTrue(p.contains(point));
|
||||
Assert.assertTrue(l.contains(point));
|
||||
Assert.assertNull(p.intersection(new Line(new Vector3D(10, 10, 10),
|
||||
p.getNormal().orthogonal())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntersection2() {
|
||||
Vector3D p1 = new Vector3D (1.2, 3.4, -5.8);
|
||||
Vector3D p2 = new Vector3D (3.4, -5.8, 1.2);
|
||||
Plane pA = new Plane(p1, p2, new Vector3D (-2.0, 4.3, 0.7));
|
||||
Plane pB = new Plane(p1, new Vector3D (11.4, -3.8, 5.1), p2);
|
||||
Line l = (Line) pA.intersection(pB);
|
||||
Assert.assertTrue(l.contains(p1));
|
||||
Assert.assertTrue(l.contains(p2));
|
||||
Assert.assertNull(pA.intersection(pA));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntersection3() {
|
||||
Vector3D reference = new Vector3D (1.2, 3.4, -5.8);
|
||||
Plane p1 = new Plane(reference, new Vector3D(1, 3, 3));
|
||||
Plane p2 = new Plane(reference, new Vector3D(-2, 4, 0));
|
||||
Plane p3 = new Plane(reference, new Vector3D(7, 0, -4));
|
||||
Vector3D p = Plane.intersection(p1, p2, p3);
|
||||
Assert.assertEquals(reference.getX(), p.getX(), 1.0e-10);
|
||||
Assert.assertEquals(reference.getY(), p.getY(), 1.0e-10);
|
||||
Assert.assertEquals(reference.getZ(), p.getZ(), 1.0e-10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimilar() {
|
||||
Vector3D p1 = new Vector3D (1.2, 3.4, -5.8);
|
||||
Vector3D p2 = new Vector3D (3.4, -5.8, 1.2);
|
||||
Vector3D p3 = new Vector3D (-2.0, 4.3, 0.7);
|
||||
Plane pA = new Plane(p1, p2, p3);
|
||||
Plane pB = new Plane(p1, new Vector3D (11.4, -3.8, 5.1), p2);
|
||||
Assert.assertTrue(! pA.isSimilarTo(pB));
|
||||
Assert.assertTrue(pA.isSimilarTo(pA));
|
||||
Assert.assertTrue(pA.isSimilarTo(new Plane(p1, p3, p2)));
|
||||
Vector3D shift = new Vector3D(0.3, pA.getNormal());
|
||||
Assert.assertTrue(! pA.isSimilarTo(new Plane(p1.add(shift),
|
||||
p3.add(shift),
|
||||
p2.add(shift))));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,243 @@
|
|||
/*
|
||||
* 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.math.geometry.euclidean.threeD;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.commons.math.geometry.euclidean.threeD.Plane;
|
||||
import org.apache.commons.math.geometry.euclidean.threeD.Point3D;
|
||||
import org.apache.commons.math.geometry.euclidean.threeD.PolyhedronsSet;
|
||||
import org.apache.commons.math.geometry.euclidean.threeD.Rotation;
|
||||
import org.apache.commons.math.geometry.euclidean.threeD.Vector3D;
|
||||
import org.apache.commons.math.geometry.euclidean.twoD.Point2D;
|
||||
import org.apache.commons.math.geometry.euclidean.twoD.PolygonsSet;
|
||||
import org.apache.commons.math.geometry.partitioning.BSPTree;
|
||||
import org.apache.commons.math.geometry.partitioning.BSPTreeVisitor;
|
||||
import org.apache.commons.math.geometry.partitioning.Hyperplane;
|
||||
import org.apache.commons.math.geometry.partitioning.Region;
|
||||
import org.apache.commons.math.geometry.partitioning.SubHyperplane;
|
||||
import org.apache.commons.math.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);
|
||||
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 Point3D(x, y, z)));
|
||||
}
|
||||
}
|
||||
}
|
||||
checkPoints(Region.Location.BOUNDARY, tree, new Point3D[] {
|
||||
new Point3D(0.0, 0.5, 0.5),
|
||||
new Point3D(1.0, 0.5, 0.5),
|
||||
new Point3D(0.5, 0.0, 0.5),
|
||||
new Point3D(0.5, 1.0, 0.5),
|
||||
new Point3D(0.5, 0.5, 0.0),
|
||||
new Point3D(0.5, 0.5, 1.0)
|
||||
});
|
||||
checkPoints(Region.Location.OUTSIDE, tree, new Point3D[] {
|
||||
new Point3D(0.0, 1.2, 1.2),
|
||||
new Point3D(1.0, 1.2, 1.2),
|
||||
new Point3D(1.2, 0.0, 1.2),
|
||||
new Point3D(1.2, 1.0, 1.2),
|
||||
new Point3D(1.2, 1.2, 0.0),
|
||||
new Point3D(1.2, 1.2, 1.0)
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTetrahedron() {
|
||||
Point3D vertex1 = new Point3D(1, 2, 3);
|
||||
Point3D vertex2 = new Point3D(2, 2, 4);
|
||||
Point3D vertex3 = new Point3D(2, 3, 3);
|
||||
Point3D vertex4 = new Point3D(1, 3, 4);
|
||||
PolyhedronsSet tree =
|
||||
(PolyhedronsSet) Region.buildConvex(Arrays.asList(new Hyperplane[] {
|
||||
new Plane(vertex3, vertex2, vertex1),
|
||||
new Plane(vertex2, vertex3, vertex4),
|
||||
new Plane(vertex4, vertex3, vertex1),
|
||||
new Plane(vertex1, vertex2, vertex4)
|
||||
}));
|
||||
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 Point3D[] {
|
||||
vertex1, vertex2, vertex3, vertex4,
|
||||
new Point3D(third, vertex1, third, vertex2, third, vertex3),
|
||||
new Point3D(third, vertex2, third, vertex3, third, vertex4),
|
||||
new Point3D(third, vertex3, third, vertex4, third, vertex1),
|
||||
new Point3D(third, vertex4, third, vertex1, third, vertex2)
|
||||
});
|
||||
checkPoints(Region.Location.OUTSIDE, tree, new Point3D[] {
|
||||
new Point3D(1, 2, 4),
|
||||
new Point3D(2, 2, 3),
|
||||
new Point3D(2, 3, 4),
|
||||
new Point3D(1, 3, 3)
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsometry() {
|
||||
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);
|
||||
PolyhedronsSet tree =
|
||||
(PolyhedronsSet) Region.buildConvex(Arrays.asList(new Hyperplane[] {
|
||||
new Plane(vertex3, vertex2, vertex1),
|
||||
new Plane(vertex2, vertex3, vertex4),
|
||||
new Plane(vertex4, vertex3, vertex1),
|
||||
new Plane(vertex1, vertex2, vertex4)
|
||||
}));
|
||||
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);
|
||||
|
||||
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((Vector3D) 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() {
|
||||
|
||||
public Order visitOrder(BSPTree node) {
|
||||
return Order.MINUS_SUB_PLUS;
|
||||
}
|
||||
|
||||
public void visitInternalNode(BSPTree node) {
|
||||
Region.BoundaryAttribute attribute =
|
||||
(Region.BoundaryAttribute) node.getAttribute();
|
||||
if (attribute.getPlusOutside() != null) {
|
||||
checkFacet(attribute.getPlusOutside());
|
||||
}
|
||||
if (attribute.getPlusInside() != null) {
|
||||
checkFacet(attribute.getPlusInside());
|
||||
}
|
||||
}
|
||||
|
||||
public void visitLeafNode(BSPTree node) {
|
||||
}
|
||||
|
||||
private void checkFacet(SubHyperplane facet) {
|
||||
Plane plane = (Plane) facet.getHyperplane();
|
||||
Point2D[][] vertices =
|
||||
((PolygonsSet) facet.getRemainingRegion()).getVertices();
|
||||
Assert.assertEquals(1, vertices.length);
|
||||
for (int i = 0; i < vertices[0].length; ++i) {
|
||||
Vector3D v = (Vector3D) 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);
|
||||
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);
|
||||
PolyhedronsSet yBeam =
|
||||
new PolyhedronsSet(x - w, x + w, y - l, y + l, z - w, z + w);
|
||||
PolyhedronsSet zBeam =
|
||||
new PolyhedronsSet(x - w, x + w, y - w, y + w, z - l, z + l);
|
||||
PolyhedronsSet tree =
|
||||
(PolyhedronsSet) Region.union(xBeam, Region.union(yBeam, zBeam));
|
||||
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 * w * w * (3 * l - 2 * w), tree.getSize(), 1.0e-10);
|
||||
Assert.assertEquals(24 * w * (2 * l - w), tree.getBoundarySize(), 1.0e-10);
|
||||
|
||||
}
|
||||
|
||||
private void checkPoints(Region.Location expected, PolyhedronsSet tree, Point3D[] points) {
|
||||
for (int i = 0; i < points.length; ++i) {
|
||||
Assert.assertEquals(expected, tree.checkPoint(points[i]));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -15,11 +15,11 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math.geometry;
|
||||
package org.apache.commons.math.geometry.euclidean.threeD;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.apache.commons.math.geometry.RotationOrder;
|
||||
import org.apache.commons.math.geometry.euclidean.threeD.RotationOrder;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
|
@ -15,13 +15,13 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math.geometry;
|
||||
package org.apache.commons.math.geometry.euclidean.threeD;
|
||||
|
||||
import org.apache.commons.math.geometry.CardanEulerSingularityException;
|
||||
import org.apache.commons.math.geometry.NotARotationMatrixException;
|
||||
import org.apache.commons.math.geometry.Rotation;
|
||||
import org.apache.commons.math.geometry.RotationOrder;
|
||||
import org.apache.commons.math.geometry.Vector3D;
|
||||
import org.apache.commons.math.geometry.euclidean.threeD.CardanEulerSingularityException;
|
||||
import org.apache.commons.math.geometry.euclidean.threeD.NotARotationMatrixException;
|
||||
import org.apache.commons.math.geometry.euclidean.threeD.Rotation;
|
||||
import org.apache.commons.math.geometry.euclidean.threeD.RotationOrder;
|
||||
import org.apache.commons.math.geometry.euclidean.threeD.Vector3D;
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
import org.apache.commons.math.util.MathUtils;
|
||||
import org.junit.Assert;
|
|
@ -15,12 +15,14 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math.geometry;
|
||||
package org.apache.commons.math.geometry.euclidean.threeD;
|
||||
|
||||
import java.text.NumberFormat;
|
||||
import java.text.ParsePosition;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.apache.commons.math.geometry.euclidean.threeD.Vector3D;
|
||||
import org.apache.commons.math.geometry.euclidean.threeD.Vector3DFormat;
|
||||
import org.junit.Test;
|
||||
import org.junit.Assert;
|
||||
|
|
@ -15,7 +15,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math.geometry;
|
||||
package org.apache.commons.math.geometry.euclidean.threeD;
|
||||
|
||||
import java.util.Locale;
|
||||
|
|
@ -15,9 +15,9 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math.geometry;
|
||||
package org.apache.commons.math.geometry.euclidean.threeD;
|
||||
|
||||
import org.apache.commons.math.geometry.Vector3D;
|
||||
import org.apache.commons.math.geometry.euclidean.threeD.Vector3D;
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
import org.apache.commons.math.exception.MathArithmeticException;
|
||||
|
|
@ -0,0 +1,129 @@
|
|||
/*
|
||||
* 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.math.geometry.euclidean.twoD;
|
||||
|
||||
import org.apache.commons.math.geometry.euclidean.oneD.Point1D;
|
||||
import org.apache.commons.math.geometry.euclidean.twoD.Line;
|
||||
import org.apache.commons.math.geometry.euclidean.twoD.Point2D;
|
||||
import org.apache.commons.math.geometry.partitioning.Transform;
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.awt.geom.AffineTransform;
|
||||
|
||||
public class LineTest {
|
||||
|
||||
@Test
|
||||
public void testContains() {
|
||||
Line l = new Line(new Point2D(0, 1), new Point2D(1, 2));
|
||||
Assert.assertTrue(l.contains(new Point2D(0, 1)));
|
||||
Assert.assertTrue(l.contains(new Point2D(1, 2)));
|
||||
Assert.assertTrue(l.contains(new Point2D(7, 8)));
|
||||
Assert.assertTrue(! l.contains(new Point2D(8, 7)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAbscissa() {
|
||||
Line l = new Line(new Point2D(2, 1), new Point2D(-2, -2));
|
||||
Assert.assertEquals(0.0,
|
||||
((Point1D) l.toSubSpace(new Point2D(-3, 4))).getAbscissa(),
|
||||
1.0e-10);
|
||||
Assert.assertEquals(0.0,
|
||||
((Point1D) l.toSubSpace(new Point2D( 3, -4))).getAbscissa(),
|
||||
1.0e-10);
|
||||
Assert.assertEquals(-5.0,
|
||||
((Point1D) l.toSubSpace(new Point2D( 7, -1))).getAbscissa(),
|
||||
1.0e-10);
|
||||
Assert.assertEquals( 5.0,
|
||||
((Point1D) l.toSubSpace(new Point2D(-1, -7))).getAbscissa(),
|
||||
1.0e-10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOffset() {
|
||||
Line l = new Line(new Point2D(2, 1), new Point2D(-2, -2));
|
||||
Assert.assertEquals(-5.0, l.getOffset(new Point2D(5, -3)), 1.0e-10);
|
||||
Assert.assertEquals(+5.0, l.getOffset(new Point2D(-5, 2)), 1.0e-10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPointAt() {
|
||||
Line l = new Line(new Point2D(2, 1), new Point2D(-2, -2));
|
||||
for (double a = -2.0; a < 2.0; a += 0.2) {
|
||||
Point1D pA = new Point1D(a);
|
||||
Point2D point = (Point2D) l.toSpace(pA);
|
||||
Assert.assertEquals(a, ((Point1D) l.toSubSpace(point)).getAbscissa(), 1.0e-10);
|
||||
Assert.assertEquals(0.0, l.getOffset(point), 1.0e-10);
|
||||
for (double o = -2.0; o < 2.0; o += 0.2) {
|
||||
point = l.getPointAt(pA, o);
|
||||
Assert.assertEquals(a, ((Point1D) l.toSubSpace(point)).getAbscissa(), 1.0e-10);
|
||||
Assert.assertEquals(o, l.getOffset(point), 1.0e-10);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOriginOffset() {
|
||||
Line l1 = new Line(new Point2D(0, 1), new Point2D(1, 2));
|
||||
Assert.assertEquals(FastMath.sqrt(0.5), l1.getOriginOffset(), 1.0e-10);
|
||||
Line l2 = new Line(new Point2D(1, 2), new Point2D(0, 1));
|
||||
Assert.assertEquals(-FastMath.sqrt(0.5), l2.getOriginOffset(), 1.0e-10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParallel() {
|
||||
Line l1 = new Line(new Point2D(0, 1), new Point2D(1, 2));
|
||||
Line l2 = new Line(new Point2D(2, 2), new Point2D(3, 3));
|
||||
Assert.assertTrue(l1.isParallelTo(l2));
|
||||
Line l3 = new Line(new Point2D(1, 0), new Point2D(0.5, -0.5));
|
||||
Assert.assertTrue(l1.isParallelTo(l3));
|
||||
Line l4 = new Line(new Point2D(1, 0), new Point2D(0.5, -0.51));
|
||||
Assert.assertTrue(! l1.isParallelTo(l4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransform() {
|
||||
|
||||
Line l1 = new Line(new Point2D(1.0 ,1.0), new Point2D(4.0 ,1.0));
|
||||
Transform t1 = Line.getTransform(new AffineTransform(0.0, 0.5,
|
||||
-1.0, 0.0,
|
||||
1.0, 1.5));
|
||||
Assert.assertEquals(0.5 * FastMath.PI,
|
||||
((Line) t1.apply(l1)).getAngle(),
|
||||
1.0e-10);
|
||||
|
||||
Line l2 = new Line(new Point2D(0.0, 0.0), new Point2D(1.0, 1.0));
|
||||
Transform t2 = Line.getTransform(new AffineTransform(0.0, 0.5,
|
||||
-1.0, 0.0,
|
||||
1.0, 1.5));
|
||||
Assert.assertEquals(FastMath.atan2(1.0, -2.0),
|
||||
((Line) t2.apply(l2)).getAngle(),
|
||||
1.0e-10);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntersection() {
|
||||
Line l1 = new Line(new Point2D( 0, 1), new Point2D(1, 2));
|
||||
Line l2 = new Line(new Point2D(-1, 2), new Point2D(2, 1));
|
||||
Point2D p = (Point2D) l1.intersection(l2);
|
||||
Assert.assertEquals(0.5, p.x, 1.0e-10);
|
||||
Assert.assertEquals(1.5, p.y, 1.0e-10);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,883 @@
|
|||
/*
|
||||
* 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.math.geometry.euclidean.twoD;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.math.geometry.euclidean.oneD.Interval;
|
||||
import org.apache.commons.math.geometry.euclidean.oneD.IntervalsSet;
|
||||
import org.apache.commons.math.geometry.euclidean.oneD.Point1D;
|
||||
import org.apache.commons.math.geometry.euclidean.twoD.Line;
|
||||
import org.apache.commons.math.geometry.euclidean.twoD.Point2D;
|
||||
import org.apache.commons.math.geometry.euclidean.twoD.PolygonsSet;
|
||||
import org.apache.commons.math.geometry.partitioning.BSPTree;
|
||||
import org.apache.commons.math.geometry.partitioning.Region;
|
||||
import org.apache.commons.math.geometry.partitioning.SubHyperplane;
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PolygonsSetTest {
|
||||
|
||||
@Test
|
||||
public void testSimplyConnected() {
|
||||
Point2D[][] vertices = new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D(36.0, 22.0),
|
||||
new Point2D(39.0, 32.0),
|
||||
new Point2D(19.0, 32.0),
|
||||
new Point2D( 6.0, 16.0),
|
||||
new Point2D(31.0, 10.0),
|
||||
new Point2D(42.0, 16.0),
|
||||
new Point2D(34.0, 20.0),
|
||||
new Point2D(29.0, 19.0),
|
||||
new Point2D(23.0, 22.0),
|
||||
new Point2D(33.0, 25.0)
|
||||
}
|
||||
};
|
||||
PolygonsSet set = buildSet(vertices);
|
||||
Assert.assertEquals(Region.Location.OUTSIDE, set.checkPoint(new Point2D(50.0, 30.0)));
|
||||
checkPoints(Region.Location.INSIDE, set, new Point2D[] {
|
||||
new Point2D(30.0, 15.0),
|
||||
new Point2D(15.0, 20.0),
|
||||
new Point2D(24.0, 25.0),
|
||||
new Point2D(35.0, 30.0),
|
||||
new Point2D(19.0, 17.0)
|
||||
});
|
||||
checkPoints(Region.Location.OUTSIDE, set, new Point2D[] {
|
||||
new Point2D(50.0, 30.0),
|
||||
new Point2D(30.0, 35.0),
|
||||
new Point2D(10.0, 25.0),
|
||||
new Point2D(10.0, 10.0),
|
||||
new Point2D(40.0, 10.0),
|
||||
new Point2D(50.0, 15.0),
|
||||
new Point2D(30.0, 22.0)
|
||||
});
|
||||
checkPoints(Region.Location.BOUNDARY, set, new Point2D[] {
|
||||
new Point2D(30.0, 32.0),
|
||||
new Point2D(34.0, 20.0)
|
||||
});
|
||||
checkVertices(set.getVertices(), vertices);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStair() {
|
||||
Point2D[][] vertices = new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D( 0.0, 0.0),
|
||||
new Point2D( 0.0, 2.0),
|
||||
new Point2D(-0.1, 2.0),
|
||||
new Point2D(-0.1, 1.0),
|
||||
new Point2D(-0.3, 1.0),
|
||||
new Point2D(-0.3, 1.5),
|
||||
new Point2D(-1.3, 1.5),
|
||||
new Point2D(-1.3, 2.0),
|
||||
new Point2D(-1.8, 2.0),
|
||||
new Point2D(-1.8 - 1.0 / FastMath.sqrt(2.0),
|
||||
2.0 - 1.0 / FastMath.sqrt(2.0))
|
||||
}
|
||||
};
|
||||
|
||||
PolygonsSet set = buildSet(vertices);
|
||||
checkVertices(set.getVertices(), vertices);
|
||||
|
||||
Assert.assertEquals(1.1 + 0.95 * FastMath.sqrt(2.0), set.getSize(), 1.0e-10);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHole() {
|
||||
Point2D[][] vertices = new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D(0.0, 0.0),
|
||||
new Point2D(3.0, 0.0),
|
||||
new Point2D(3.0, 3.0),
|
||||
new Point2D(0.0, 3.0)
|
||||
}, new Point2D[] {
|
||||
new Point2D(1.0, 2.0),
|
||||
new Point2D(2.0, 2.0),
|
||||
new Point2D(2.0, 1.0),
|
||||
new Point2D(1.0, 1.0)
|
||||
}
|
||||
};
|
||||
PolygonsSet set = buildSet(vertices);
|
||||
checkPoints(Region.Location.INSIDE, set, new Point2D[] {
|
||||
new Point2D(0.5, 0.5),
|
||||
new Point2D(1.5, 0.5),
|
||||
new Point2D(2.5, 0.5),
|
||||
new Point2D(0.5, 1.5),
|
||||
new Point2D(2.5, 1.5),
|
||||
new Point2D(0.5, 2.5),
|
||||
new Point2D(1.5, 2.5),
|
||||
new Point2D(2.5, 2.5),
|
||||
new Point2D(0.5, 1.0)
|
||||
});
|
||||
checkPoints(Region.Location.OUTSIDE, set, new Point2D[] {
|
||||
new Point2D(1.5, 1.5),
|
||||
new Point2D(3.5, 1.0),
|
||||
new Point2D(4.0, 1.5),
|
||||
new Point2D(6.0, 6.0)
|
||||
});
|
||||
checkPoints(Region.Location.BOUNDARY, set, new Point2D[] {
|
||||
new Point2D(1.0, 1.0),
|
||||
new Point2D(1.5, 0.0),
|
||||
new Point2D(1.5, 1.0),
|
||||
new Point2D(1.5, 2.0),
|
||||
new Point2D(1.5, 3.0),
|
||||
new Point2D(3.0, 3.0)
|
||||
});
|
||||
checkVertices(set.getVertices(), vertices);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDisjointPolygons() {
|
||||
Point2D[][] vertices = new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D(0.0, 1.0),
|
||||
new Point2D(2.0, 1.0),
|
||||
new Point2D(1.0, 2.0)
|
||||
}, new Point2D[] {
|
||||
new Point2D(4.0, 0.0),
|
||||
new Point2D(5.0, 1.0),
|
||||
new Point2D(3.0, 1.0)
|
||||
}
|
||||
};
|
||||
PolygonsSet set = buildSet(vertices);
|
||||
Assert.assertEquals(Region.Location.INSIDE, set.checkPoint(new Point2D(1.0, 1.5)));
|
||||
checkPoints(Region.Location.INSIDE, set, new Point2D[] {
|
||||
new Point2D(1.0, 1.5),
|
||||
new Point2D(4.5, 0.8)
|
||||
});
|
||||
checkPoints(Region.Location.OUTSIDE, set, new Point2D[] {
|
||||
new Point2D(1.0, 0.0),
|
||||
new Point2D(3.5, 1.2),
|
||||
new Point2D(2.5, 1.0),
|
||||
new Point2D(3.0, 4.0)
|
||||
});
|
||||
checkPoints(Region.Location.BOUNDARY, set, new Point2D[] {
|
||||
new Point2D(1.0, 1.0),
|
||||
new Point2D(3.5, 0.5),
|
||||
new Point2D(0.0, 1.0)
|
||||
});
|
||||
checkVertices(set.getVertices(), vertices);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOppositeHyperplanes() {
|
||||
Point2D[][] vertices = new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D(1.0, 0.0),
|
||||
new Point2D(2.0, 1.0),
|
||||
new Point2D(3.0, 1.0),
|
||||
new Point2D(2.0, 2.0),
|
||||
new Point2D(1.0, 1.0),
|
||||
new Point2D(0.0, 1.0)
|
||||
}
|
||||
};
|
||||
PolygonsSet set = buildSet(vertices);
|
||||
checkVertices(set.getVertices(), vertices);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingularPoint() {
|
||||
Point2D[][] vertices = new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D( 0.0, 0.0),
|
||||
new Point2D( 1.0, 0.0),
|
||||
new Point2D( 1.0, 1.0),
|
||||
new Point2D( 0.0, 1.0),
|
||||
new Point2D( 0.0, 0.0),
|
||||
new Point2D(-1.0, 0.0),
|
||||
new Point2D(-1.0, -1.0),
|
||||
new Point2D( 0.0, -1.0)
|
||||
}
|
||||
};
|
||||
PolygonsSet set = buildSet(vertices);
|
||||
checkVertices(set.getVertices(), vertices);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLineIntersection() {
|
||||
Point2D[][] vertices = new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D( 0.0, 0.0),
|
||||
new Point2D( 2.0, 0.0),
|
||||
new Point2D( 2.0, 1.0),
|
||||
new Point2D( 3.0, 1.0),
|
||||
new Point2D( 3.0, 3.0),
|
||||
new Point2D( 1.0, 3.0),
|
||||
new Point2D( 1.0, 2.0),
|
||||
new Point2D( 0.0, 2.0)
|
||||
}
|
||||
};
|
||||
PolygonsSet set = buildSet(vertices);
|
||||
|
||||
Line l1 = new Line(new Point2D(-1.5, 0.0), FastMath.PI / 4);
|
||||
SubHyperplane s1 = set.intersection(new SubHyperplane(l1));
|
||||
List<Interval> i1 = ((IntervalsSet) s1.getRemainingRegion()).asList();
|
||||
Assert.assertEquals(2, i1.size());
|
||||
Interval v10 = (Interval) i1.get(0);
|
||||
Point2D p10Lower = (Point2D) l1.toSpace(new Point1D(v10.getLower()));
|
||||
Assert.assertEquals(0.0, p10Lower.getX(), 1.0e-10);
|
||||
Assert.assertEquals(1.5, p10Lower.getY(), 1.0e-10);
|
||||
Point2D p10Upper = (Point2D) l1.toSpace(new Point1D(v10.getUpper()));
|
||||
Assert.assertEquals(0.5, p10Upper.getX(), 1.0e-10);
|
||||
Assert.assertEquals(2.0, p10Upper.getY(), 1.0e-10);
|
||||
Interval v11 = (Interval) i1.get(1);
|
||||
Point2D p11Lower = (Point2D) l1.toSpace(new Point1D(v11.getLower()));
|
||||
Assert.assertEquals(1.0, p11Lower.getX(), 1.0e-10);
|
||||
Assert.assertEquals(2.5, p11Lower.getY(), 1.0e-10);
|
||||
Point2D p11Upper = (Point2D) l1.toSpace(new Point1D(v11.getUpper()));
|
||||
Assert.assertEquals(1.5, p11Upper.getX(), 1.0e-10);
|
||||
Assert.assertEquals(3.0, p11Upper.getY(), 1.0e-10);
|
||||
|
||||
Line l2 = new Line(new Point2D(-1.0, 2.0), 0);
|
||||
SubHyperplane s2 = set.intersection(new SubHyperplane(l2));
|
||||
List<Interval> i2 = ((IntervalsSet) s2.getRemainingRegion()).asList();
|
||||
Assert.assertEquals(1, i2.size());
|
||||
Interval v20 = (Interval) i2.get(0);
|
||||
Point2D p20Lower = (Point2D) l2.toSpace(new Point1D(v20.getLower()));
|
||||
Assert.assertEquals(1.0, p20Lower.getX(), 1.0e-10);
|
||||
Assert.assertEquals(2.0, p20Lower.getY(), 1.0e-10);
|
||||
Point2D p20Upper = (Point2D) l2.toSpace(new Point1D(v20.getUpper()));
|
||||
Assert.assertEquals(3.0, p20Upper.getX(), 1.0e-10);
|
||||
Assert.assertEquals(2.0, p20Upper.getY(), 1.0e-10);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnlimitedSubHyperplane() {
|
||||
Point2D[][] vertices1 = new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D(0.0, 0.0),
|
||||
new Point2D(4.0, 0.0),
|
||||
new Point2D(1.4, 1.5),
|
||||
new Point2D(0.0, 3.5)
|
||||
}
|
||||
};
|
||||
PolygonsSet set1 = buildSet(vertices1);
|
||||
Point2D[][] vertices2 = new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D(1.4, 0.2),
|
||||
new Point2D(2.8, -1.2),
|
||||
new Point2D(2.5, 0.6)
|
||||
}
|
||||
};
|
||||
PolygonsSet set2 = buildSet(vertices2);
|
||||
|
||||
PolygonsSet set = (PolygonsSet) Region.union(set1.copySelf(),
|
||||
set2.copySelf());
|
||||
checkVertices(set1.getVertices(), vertices1);
|
||||
checkVertices(set2.getVertices(), vertices2);
|
||||
checkVertices(set.getVertices(), new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D(0.0, 0.0),
|
||||
new Point2D(1.6, 0.0),
|
||||
new Point2D(2.8, -1.2),
|
||||
new Point2D(2.6, 0.0),
|
||||
new Point2D(4.0, 0.0),
|
||||
new Point2D(1.4, 1.5),
|
||||
new Point2D(0.0, 3.5)
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnion() {
|
||||
Point2D[][] vertices1 = new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D( 0.0, 0.0),
|
||||
new Point2D( 2.0, 0.0),
|
||||
new Point2D( 2.0, 2.0),
|
||||
new Point2D( 0.0, 2.0)
|
||||
}
|
||||
};
|
||||
PolygonsSet set1 = buildSet(vertices1);
|
||||
Point2D[][] vertices2 = new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D( 1.0, 1.0),
|
||||
new Point2D( 3.0, 1.0),
|
||||
new Point2D( 3.0, 3.0),
|
||||
new Point2D( 1.0, 3.0)
|
||||
}
|
||||
};
|
||||
PolygonsSet set2 = buildSet(vertices2);
|
||||
PolygonsSet set = (PolygonsSet) Region.union(set1.copySelf(),
|
||||
set2.copySelf());
|
||||
checkVertices(set1.getVertices(), vertices1);
|
||||
checkVertices(set2.getVertices(), vertices2);
|
||||
checkVertices(set.getVertices(), new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D( 0.0, 0.0),
|
||||
new Point2D( 2.0, 0.0),
|
||||
new Point2D( 2.0, 1.0),
|
||||
new Point2D( 3.0, 1.0),
|
||||
new Point2D( 3.0, 3.0),
|
||||
new Point2D( 1.0, 3.0),
|
||||
new Point2D( 1.0, 2.0),
|
||||
new Point2D( 0.0, 2.0)
|
||||
}
|
||||
});
|
||||
checkPoints(Region.Location.INSIDE, set, new Point2D[] {
|
||||
new Point2D(1.0, 1.0),
|
||||
new Point2D(0.5, 0.5),
|
||||
new Point2D(2.0, 2.0),
|
||||
new Point2D(2.5, 2.5),
|
||||
new Point2D(0.5, 1.5),
|
||||
new Point2D(1.5, 1.5),
|
||||
new Point2D(1.5, 0.5),
|
||||
new Point2D(1.5, 2.5),
|
||||
new Point2D(2.5, 1.5),
|
||||
new Point2D(2.5, 2.5)
|
||||
});
|
||||
checkPoints(Region.Location.OUTSIDE, set, new Point2D[] {
|
||||
new Point2D(-0.5, 0.5),
|
||||
new Point2D( 0.5, 2.5),
|
||||
new Point2D( 2.5, 0.5),
|
||||
new Point2D( 3.5, 2.5)
|
||||
});
|
||||
checkPoints(Region.Location.BOUNDARY, set, new Point2D[] {
|
||||
new Point2D(0.0, 0.0),
|
||||
new Point2D(0.5, 2.0),
|
||||
new Point2D(2.0, 0.5),
|
||||
new Point2D(2.5, 1.0),
|
||||
new Point2D(3.0, 2.5)
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntersection() {
|
||||
Point2D[][] vertices1 = new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D( 0.0, 0.0),
|
||||
new Point2D( 2.0, 0.0),
|
||||
new Point2D( 2.0, 2.0),
|
||||
new Point2D( 0.0, 2.0)
|
||||
}
|
||||
};
|
||||
PolygonsSet set1 = buildSet(vertices1);
|
||||
Point2D[][] vertices2 = new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D( 1.0, 1.0),
|
||||
new Point2D( 3.0, 1.0),
|
||||
new Point2D( 3.0, 3.0),
|
||||
new Point2D( 1.0, 3.0)
|
||||
}
|
||||
};
|
||||
PolygonsSet set2 = buildSet(vertices2);
|
||||
PolygonsSet set = (PolygonsSet) Region.intersection(set1.copySelf(),
|
||||
set2.copySelf());
|
||||
checkVertices(set1.getVertices(), vertices1);
|
||||
checkVertices(set2.getVertices(), vertices2);
|
||||
checkVertices(set.getVertices(), new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D( 1.0, 1.0),
|
||||
new Point2D( 2.0, 1.0),
|
||||
new Point2D( 2.0, 2.0),
|
||||
new Point2D( 1.0, 2.0)
|
||||
}
|
||||
});
|
||||
checkPoints(Region.Location.INSIDE, set, new Point2D[] {
|
||||
new Point2D(1.5, 1.5)
|
||||
});
|
||||
checkPoints(Region.Location.OUTSIDE, set, new Point2D[] {
|
||||
new Point2D(0.5, 1.5),
|
||||
new Point2D(2.5, 1.5),
|
||||
new Point2D(1.5, 0.5),
|
||||
new Point2D(0.5, 0.5)
|
||||
});
|
||||
checkPoints(Region.Location.BOUNDARY, set, new Point2D[] {
|
||||
new Point2D(1.0, 1.0),
|
||||
new Point2D(2.0, 2.0),
|
||||
new Point2D(1.0, 1.5),
|
||||
new Point2D(1.5, 2.0)
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXor() {
|
||||
Point2D[][] vertices1 = new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D( 0.0, 0.0),
|
||||
new Point2D( 2.0, 0.0),
|
||||
new Point2D( 2.0, 2.0),
|
||||
new Point2D( 0.0, 2.0)
|
||||
}
|
||||
};
|
||||
PolygonsSet set1 = buildSet(vertices1);
|
||||
Point2D[][] vertices2 = new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D( 1.0, 1.0),
|
||||
new Point2D( 3.0, 1.0),
|
||||
new Point2D( 3.0, 3.0),
|
||||
new Point2D( 1.0, 3.0)
|
||||
}
|
||||
};
|
||||
PolygonsSet set2 = buildSet(vertices2);
|
||||
PolygonsSet set = (PolygonsSet) Region.xor(set1.copySelf(),
|
||||
set2.copySelf());
|
||||
checkVertices(set1.getVertices(), vertices1);
|
||||
checkVertices(set2.getVertices(), vertices2);
|
||||
checkVertices(set.getVertices(), new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D( 0.0, 0.0),
|
||||
new Point2D( 2.0, 0.0),
|
||||
new Point2D( 2.0, 1.0),
|
||||
new Point2D( 3.0, 1.0),
|
||||
new Point2D( 3.0, 3.0),
|
||||
new Point2D( 1.0, 3.0),
|
||||
new Point2D( 1.0, 2.0),
|
||||
new Point2D( 0.0, 2.0)
|
||||
},
|
||||
new Point2D[] {
|
||||
new Point2D( 1.0, 1.0),
|
||||
new Point2D( 1.0, 2.0),
|
||||
new Point2D( 2.0, 2.0),
|
||||
new Point2D( 2.0, 1.0)
|
||||
}
|
||||
});
|
||||
checkPoints(Region.Location.INSIDE, set, new Point2D[] {
|
||||
new Point2D(0.5, 0.5),
|
||||
new Point2D(2.5, 2.5),
|
||||
new Point2D(0.5, 1.5),
|
||||
new Point2D(1.5, 0.5),
|
||||
new Point2D(1.5, 2.5),
|
||||
new Point2D(2.5, 1.5),
|
||||
new Point2D(2.5, 2.5)
|
||||
});
|
||||
checkPoints(Region.Location.OUTSIDE, set, new Point2D[] {
|
||||
new Point2D(-0.5, 0.5),
|
||||
new Point2D( 0.5, 2.5),
|
||||
new Point2D( 2.5, 0.5),
|
||||
new Point2D( 1.5, 1.5),
|
||||
new Point2D( 3.5, 2.5)
|
||||
});
|
||||
checkPoints(Region.Location.BOUNDARY, set, new Point2D[] {
|
||||
new Point2D(1.0, 1.0),
|
||||
new Point2D(2.0, 2.0),
|
||||
new Point2D(1.5, 1.0),
|
||||
new Point2D(2.0, 1.5),
|
||||
new Point2D(0.0, 0.0),
|
||||
new Point2D(0.5, 2.0),
|
||||
new Point2D(2.0, 0.5),
|
||||
new Point2D(2.5, 1.0),
|
||||
new Point2D(3.0, 2.5)
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDifference() {
|
||||
Point2D[][] vertices1 = new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D( 0.0, 0.0),
|
||||
new Point2D( 2.0, 0.0),
|
||||
new Point2D( 2.0, 2.0),
|
||||
new Point2D( 0.0, 2.0)
|
||||
}
|
||||
};
|
||||
PolygonsSet set1 = buildSet(vertices1);
|
||||
Point2D[][] vertices2 = new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D( 1.0, 1.0),
|
||||
new Point2D( 3.0, 1.0),
|
||||
new Point2D( 3.0, 3.0),
|
||||
new Point2D( 1.0, 3.0)
|
||||
}
|
||||
};
|
||||
PolygonsSet set2 = buildSet(vertices2);
|
||||
PolygonsSet set = (PolygonsSet) Region.difference(set1.copySelf(),
|
||||
set2.copySelf());
|
||||
checkVertices(set1.getVertices(), vertices1);
|
||||
checkVertices(set2.getVertices(), vertices2);
|
||||
checkVertices(set.getVertices(), new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D( 0.0, 0.0),
|
||||
new Point2D( 2.0, 0.0),
|
||||
new Point2D( 2.0, 1.0),
|
||||
new Point2D( 1.0, 1.0),
|
||||
new Point2D( 1.0, 2.0),
|
||||
new Point2D( 0.0, 2.0)
|
||||
}
|
||||
});
|
||||
checkPoints(Region.Location.INSIDE, set, new Point2D[] {
|
||||
new Point2D(0.5, 0.5),
|
||||
new Point2D(0.5, 1.5),
|
||||
new Point2D(1.5, 0.5)
|
||||
});
|
||||
checkPoints(Region.Location.OUTSIDE, set, new Point2D[] {
|
||||
new Point2D( 2.5, 2.5),
|
||||
new Point2D(-0.5, 0.5),
|
||||
new Point2D( 0.5, 2.5),
|
||||
new Point2D( 2.5, 0.5),
|
||||
new Point2D( 1.5, 1.5),
|
||||
new Point2D( 3.5, 2.5),
|
||||
new Point2D( 1.5, 2.5),
|
||||
new Point2D( 2.5, 1.5),
|
||||
new Point2D( 2.0, 1.5),
|
||||
new Point2D( 2.0, 2.0),
|
||||
new Point2D( 2.5, 1.0),
|
||||
new Point2D( 2.5, 2.5),
|
||||
new Point2D( 3.0, 2.5)
|
||||
});
|
||||
checkPoints(Region.Location.BOUNDARY, set, new Point2D[] {
|
||||
new Point2D(1.0, 1.0),
|
||||
new Point2D(1.5, 1.0),
|
||||
new Point2D(0.0, 0.0),
|
||||
new Point2D(0.5, 2.0),
|
||||
new Point2D(2.0, 0.5)
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyDifference() {
|
||||
Point2D[][] vertices1 = new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D( 0.5, 3.5),
|
||||
new Point2D( 0.5, 4.5),
|
||||
new Point2D(-0.5, 4.5),
|
||||
new Point2D(-0.5, 3.5)
|
||||
}
|
||||
};
|
||||
PolygonsSet set1 = buildSet(vertices1);
|
||||
Point2D[][] vertices2 = new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D( 1.0, 2.0),
|
||||
new Point2D( 1.0, 8.0),
|
||||
new Point2D(-1.0, 8.0),
|
||||
new Point2D(-1.0, 2.0)
|
||||
}
|
||||
};
|
||||
PolygonsSet set2 = buildSet(vertices2);
|
||||
Assert.assertTrue(Region.difference(set1.copySelf(), set2.copySelf()).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChoppedHexagon() {
|
||||
double pi6 = FastMath.PI / 6.0;
|
||||
double sqrt3 = FastMath.sqrt(3.0);
|
||||
SubHyperplane[] hyp = {
|
||||
new SubHyperplane(new Line(new Point2D( 0.0, 1.0), 5 * pi6)),
|
||||
new SubHyperplane(new Line(new Point2D(-sqrt3, 1.0), 7 * pi6)),
|
||||
new SubHyperplane(new Line(new Point2D(-sqrt3, 1.0), 9 * pi6)),
|
||||
new SubHyperplane(new Line(new Point2D(-sqrt3, 0.0), 11 * pi6)),
|
||||
new SubHyperplane(new Line(new Point2D( 0.0, 0.0), 13 * pi6)),
|
||||
new SubHyperplane(new Line(new Point2D( 0.0, 1.0), 3 * pi6)),
|
||||
new SubHyperplane(new Line(new Point2D(-5.0 * sqrt3 / 6.0, 0.0), 9 * pi6))
|
||||
};
|
||||
hyp[1] = hyp[0].getHyperplane().split(hyp[1]).getMinus();
|
||||
hyp[2] = hyp[1].getHyperplane().split(hyp[2]).getMinus();
|
||||
hyp[3] = hyp[2].getHyperplane().split(hyp[3]).getMinus();
|
||||
hyp[4] = hyp[0].getHyperplane().split(hyp[3].getHyperplane().split(hyp[4]).getMinus()).getMinus();
|
||||
hyp[5] = hyp[0].getHyperplane().split(hyp[4].getHyperplane().split(hyp[5]).getMinus()).getMinus();
|
||||
hyp[6] = hyp[1].getHyperplane().split(hyp[3].getHyperplane().split(hyp[6]).getMinus()).getMinus();
|
||||
BSPTree tree = new BSPTree(Boolean.TRUE);
|
||||
for (int i = hyp.length - 1; i >= 0; --i) {
|
||||
tree = new BSPTree(hyp[i], new BSPTree(Boolean.FALSE), tree, null);
|
||||
}
|
||||
PolygonsSet set = new PolygonsSet(tree);
|
||||
SubHyperplane splitter =
|
||||
new SubHyperplane(new Line(new Point2D(-2.0 * sqrt3 / 3.0, 0.0), 9 * pi6));
|
||||
PolygonsSet slice =
|
||||
new PolygonsSet(new BSPTree(splitter,
|
||||
set.getTree(false).split(splitter).getPlus(),
|
||||
new BSPTree(Boolean.FALSE), null));
|
||||
Assert.assertEquals(Region.Location.OUTSIDE,
|
||||
slice.checkPoint(new Point2D(0.1, 0.5)));
|
||||
Assert.assertEquals(11.0 / 3.0, slice.getBoundarySize(), 1.0e-10);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConcentric() {
|
||||
double h = FastMath.sqrt(3.0) / 2.0;
|
||||
Point2D[][] vertices1 = new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D( 0.00, 0.1 * h),
|
||||
new Point2D( 0.05, 0.1 * h),
|
||||
new Point2D( 0.10, 0.2 * h),
|
||||
new Point2D( 0.05, 0.3 * h),
|
||||
new Point2D(-0.05, 0.3 * h),
|
||||
new Point2D(-0.10, 0.2 * h),
|
||||
new Point2D(-0.05, 0.1 * h)
|
||||
}
|
||||
};
|
||||
PolygonsSet set1 = buildSet(vertices1);
|
||||
Point2D[][] vertices2 = new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D( 0.00, 0.0 * h),
|
||||
new Point2D( 0.10, 0.0 * h),
|
||||
new Point2D( 0.20, 0.2 * h),
|
||||
new Point2D( 0.10, 0.4 * h),
|
||||
new Point2D(-0.10, 0.4 * h),
|
||||
new Point2D(-0.20, 0.2 * h),
|
||||
new Point2D(-0.10, 0.0 * h)
|
||||
}
|
||||
};
|
||||
PolygonsSet set2 = buildSet(vertices2);
|
||||
Assert.assertTrue(set2.contains(set1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBug20040520() {
|
||||
BSPTree a0 = new BSPTree(buildSegment(new Point2D(0.85, -0.05),
|
||||
new Point2D(0.90, -0.10)),
|
||||
new BSPTree(Boolean.FALSE),
|
||||
new BSPTree(Boolean.TRUE),
|
||||
null);
|
||||
BSPTree a1 = new BSPTree(buildSegment(new Point2D(0.85, -0.10),
|
||||
new Point2D(0.90, -0.10)),
|
||||
new BSPTree(Boolean.FALSE), a0, null);
|
||||
BSPTree a2 = new BSPTree(buildSegment(new Point2D(0.90, -0.05),
|
||||
new Point2D(0.85, -0.05)),
|
||||
new BSPTree(Boolean.FALSE), a1, null);
|
||||
BSPTree a3 = new BSPTree(buildSegment(new Point2D(0.82, -0.05),
|
||||
new Point2D(0.82, -0.08)),
|
||||
new BSPTree(Boolean.FALSE),
|
||||
new BSPTree(Boolean.TRUE),
|
||||
null);
|
||||
BSPTree a4 = new BSPTree(buildHalfLine(new Point2D(0.85, -0.05),
|
||||
new Point2D(0.80, -0.05),
|
||||
false),
|
||||
new BSPTree(Boolean.FALSE), a3, null);
|
||||
BSPTree a5 = new BSPTree(buildSegment(new Point2D(0.82, -0.08),
|
||||
new Point2D(0.82, -0.18)),
|
||||
new BSPTree(Boolean.FALSE),
|
||||
new BSPTree(Boolean.TRUE),
|
||||
null);
|
||||
BSPTree a6 = new BSPTree(buildHalfLine(new Point2D(0.82, -0.18),
|
||||
new Point2D(0.85, -0.15),
|
||||
true),
|
||||
new BSPTree(Boolean.FALSE), a5, null);
|
||||
BSPTree a7 = new BSPTree(buildHalfLine(new Point2D(0.85, -0.05),
|
||||
new Point2D(0.82, -0.08),
|
||||
false),
|
||||
a4, a6, null);
|
||||
BSPTree a8 = new BSPTree(buildLine(new Point2D(0.85, -0.25),
|
||||
new Point2D(0.85, 0.05)),
|
||||
a2, a7, null);
|
||||
BSPTree a9 = new BSPTree(buildLine(new Point2D(0.90, 0.05),
|
||||
new Point2D(0.90, -0.50)),
|
||||
a8, new BSPTree(Boolean.FALSE), null);
|
||||
|
||||
BSPTree b0 = new BSPTree(buildSegment(new Point2D(0.92, -0.12),
|
||||
new Point2D(0.92, -0.08)),
|
||||
new BSPTree(Boolean.FALSE), new BSPTree(Boolean.TRUE),
|
||||
null);
|
||||
BSPTree b1 = new BSPTree(buildHalfLine(new Point2D(0.92, -0.08),
|
||||
new Point2D(0.90, -0.10),
|
||||
true),
|
||||
new BSPTree(Boolean.FALSE), b0, null);
|
||||
BSPTree b2 = new BSPTree(buildSegment(new Point2D(0.92, -0.18),
|
||||
new Point2D(0.92, -0.12)),
|
||||
new BSPTree(Boolean.FALSE), new BSPTree(Boolean.TRUE),
|
||||
null);
|
||||
BSPTree b3 = new BSPTree(buildSegment(new Point2D(0.85, -0.15),
|
||||
new Point2D(0.90, -0.20)),
|
||||
new BSPTree(Boolean.FALSE), b2, null);
|
||||
BSPTree b4 = new BSPTree(buildSegment(new Point2D(0.95, -0.15),
|
||||
new Point2D(0.85, -0.05)),
|
||||
b1, b3, null);
|
||||
BSPTree b5 = new BSPTree(buildHalfLine(new Point2D(0.85, -0.05),
|
||||
new Point2D(0.85, -0.25),
|
||||
true),
|
||||
new BSPTree(Boolean.FALSE), b4, null);
|
||||
BSPTree b6 = new BSPTree(buildLine(new Point2D(0.0, -1.10),
|
||||
new Point2D(1.0, -0.10)),
|
||||
new BSPTree(Boolean.FALSE), b5, null);
|
||||
|
||||
PolygonsSet c = (PolygonsSet) Region.union(new PolygonsSet(a9),
|
||||
new PolygonsSet(b6));
|
||||
|
||||
checkPoints(Region.Location.INSIDE, c, new Point2D[] {
|
||||
new Point2D(0.83, -0.06),
|
||||
new Point2D(0.83, -0.15),
|
||||
new Point2D(0.88, -0.15),
|
||||
new Point2D(0.88, -0.09),
|
||||
new Point2D(0.88, -0.07),
|
||||
new Point2D(0.91, -0.18),
|
||||
new Point2D(0.91, -0.10)
|
||||
});
|
||||
|
||||
checkPoints(Region.Location.OUTSIDE, c, new Point2D[] {
|
||||
new Point2D(0.80, -0.10),
|
||||
new Point2D(0.83, -0.50),
|
||||
new Point2D(0.83, -0.20),
|
||||
new Point2D(0.83, -0.02),
|
||||
new Point2D(0.87, -0.50),
|
||||
new Point2D(0.87, -0.20),
|
||||
new Point2D(0.87, -0.02),
|
||||
new Point2D(0.91, -0.20),
|
||||
new Point2D(0.91, -0.08),
|
||||
new Point2D(0.93, -0.15)
|
||||
});
|
||||
|
||||
checkVertices(c.getVertices(),
|
||||
new Point2D[][] {
|
||||
new Point2D[] {
|
||||
new Point2D(0.85, -0.15),
|
||||
new Point2D(0.90, -0.20),
|
||||
new Point2D(0.92, -0.18),
|
||||
new Point2D(0.92, -0.08),
|
||||
new Point2D(0.90, -0.10),
|
||||
new Point2D(0.90, -0.05),
|
||||
new Point2D(0.82, -0.05),
|
||||
new Point2D(0.82, -0.18),
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBug20041003() {
|
||||
|
||||
Line[] l = {
|
||||
new Line(new Point2D(0.0, 0.625000007541172),
|
||||
new Point2D(1.0, 0.625000007541172)),
|
||||
new Line(new Point2D(-0.19204433621902645, 0.0),
|
||||
new Point2D(-0.19204433621902645, 1.0)),
|
||||
new Line(new Point2D(-0.40303524786887, 0.4248364535319128),
|
||||
new Point2D(-1.12851149797877, -0.2634107480798909)),
|
||||
new Line(new Point2D(0.0, 2.0),
|
||||
new Point2D(1.0, 2.0))
|
||||
};
|
||||
|
||||
BSPTree node1 =
|
||||
new BSPTree(new SubHyperplane(l[0],
|
||||
new IntervalsSet(intersectionAbscissa(l[0], l[1]),
|
||||
intersectionAbscissa(l[0], l[2]))),
|
||||
new BSPTree(Boolean.TRUE), new BSPTree(Boolean.FALSE),
|
||||
null);
|
||||
BSPTree node2 =
|
||||
new BSPTree(new SubHyperplane(l[1],
|
||||
new IntervalsSet(intersectionAbscissa(l[1], l[2]),
|
||||
intersectionAbscissa(l[1], l[3]))),
|
||||
node1, new BSPTree(Boolean.FALSE), null);
|
||||
BSPTree node3 =
|
||||
new BSPTree(new SubHyperplane(l[2],
|
||||
new IntervalsSet(intersectionAbscissa(l[2], l[3]),
|
||||
Double.POSITIVE_INFINITY)),
|
||||
node2, new BSPTree(Boolean.FALSE), null);
|
||||
BSPTree node4 =
|
||||
new BSPTree(new SubHyperplane(l[3]),
|
||||
node3, new BSPTree(Boolean.FALSE), null);
|
||||
|
||||
PolygonsSet set = new PolygonsSet(node4);
|
||||
Assert.assertEquals(0, set.getVertices().length);
|
||||
|
||||
}
|
||||
|
||||
private PolygonsSet buildSet(Point2D[][] vertices) {
|
||||
ArrayList<SubHyperplane> edges = new ArrayList<SubHyperplane>();
|
||||
for (int i = 0; i < vertices.length; ++i) {
|
||||
int l = vertices[i].length;
|
||||
for (int j = 0; j < l; ++j) {
|
||||
edges.add(buildSegment(vertices[i][j], vertices[i][(j + 1) % l]));
|
||||
}
|
||||
}
|
||||
return new PolygonsSet(edges);
|
||||
}
|
||||
|
||||
private SubHyperplane buildLine(Point2D start, Point2D end) {
|
||||
return new SubHyperplane(new Line(start, end));
|
||||
}
|
||||
|
||||
private double intersectionAbscissa(Line l0, Line l1) {
|
||||
Point2D p = (Point2D) l0.intersection(l1);
|
||||
return ((Point1D) l0.toSubSpace(p)).getAbscissa();
|
||||
}
|
||||
|
||||
private SubHyperplane buildHalfLine(Point2D start, Point2D end,
|
||||
boolean startIsVirtual) {
|
||||
Line line = new Line(start, end);
|
||||
double lower = startIsVirtual
|
||||
? Double.NEGATIVE_INFINITY
|
||||
: ((Point1D) line.toSubSpace(start)).getAbscissa();
|
||||
double upper = startIsVirtual
|
||||
? ((Point1D) line.toSubSpace(end)).getAbscissa()
|
||||
: Double.POSITIVE_INFINITY;
|
||||
return new SubHyperplane(line, new IntervalsSet(lower, upper));
|
||||
}
|
||||
|
||||
private SubHyperplane buildSegment(Point2D start, Point2D end) {
|
||||
Line line = new Line(start, end);
|
||||
double lower = ((Point1D) line.toSubSpace(start)).getAbscissa();
|
||||
double upper = ((Point1D) line.toSubSpace(end)).getAbscissa();
|
||||
return new SubHyperplane(line, new IntervalsSet(lower, upper));
|
||||
}
|
||||
|
||||
private void checkPoints(Region.Location expected, PolygonsSet set,
|
||||
Point2D[] points) {
|
||||
for (int i = 0; i < points.length; ++i) {
|
||||
Assert.assertEquals(expected, set.checkPoint(points[i]));
|
||||
}
|
||||
}
|
||||
|
||||
private boolean checkInSegment(Point2D p,
|
||||
Point2D p1, Point2D p2,
|
||||
double tolerance) {
|
||||
Line line = new Line(p1, p2);
|
||||
if (line.getOffset(p) < tolerance) {
|
||||
double x = ((Point1D) line.toSubSpace(p)).getAbscissa();
|
||||
double x1 = ((Point1D) line.toSubSpace(p1)).getAbscissa();
|
||||
double x2 = ((Point1D) line.toSubSpace(p2)).getAbscissa();
|
||||
return (((x - x1) * (x - x2) <= 0.0)
|
||||
|| (p1.distance(p) < tolerance)
|
||||
|| (p2.distance(p) < tolerance));
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void checkVertices(Point2D[][] rebuiltVertices,
|
||||
Point2D[][] vertices) {
|
||||
|
||||
// each rebuilt vertex should be in a segment joining two original vertices
|
||||
for (int i = 0; i < rebuiltVertices.length; ++i) {
|
||||
for (int j = 0; j < rebuiltVertices[i].length; ++j) {
|
||||
boolean inSegment = false;
|
||||
Point2D p = rebuiltVertices[i][j];
|
||||
for (int k = 0; k < vertices.length; ++k) {
|
||||
Point2D[] loop = vertices[k];
|
||||
int length = loop.length;
|
||||
for (int l = 0; (! inSegment) && (l < length); ++l) {
|
||||
inSegment = checkInSegment(p, loop[l], loop[(l + 1) % length], 1.0e-10);
|
||||
}
|
||||
}
|
||||
Assert.assertTrue(inSegment);
|
||||
}
|
||||
}
|
||||
|
||||
// each original vertex should have a corresponding rebuilt vertex
|
||||
for (int k = 0; k < vertices.length; ++k) {
|
||||
for (int l = 0; l < vertices[k].length; ++l) {
|
||||
double min = Double.POSITIVE_INFINITY;
|
||||
for (int i = 0; i < rebuiltVertices.length; ++i) {
|
||||
for (int j = 0; j < rebuiltVertices[i].length; ++j) {
|
||||
min = FastMath.min(vertices[k][l].distance(rebuiltVertices[i][j]),
|
||||
min);
|
||||
}
|
||||
}
|
||||
Assert.assertEquals(0.0, min, 1.0e-10);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,175 @@
|
|||
/*
|
||||
* 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.math.geometry.partitioning.utilities;
|
||||
|
||||
import org.apache.commons.math.geometry.partitioning.utilities.AVLTree;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AVLTreeTest {
|
||||
|
||||
@Test
|
||||
public void testInsert() {
|
||||
// this array in this order allows to pass in all branches
|
||||
// of the insertion algorithm
|
||||
int[] array = { 16, 13, 15, 14, 2, 0, 12, 9, 8, 5,
|
||||
11, 18, 19, 17, 4, 7, 1, 3, 6, 10 };
|
||||
AVLTree<Integer> tree = buildTree(array);
|
||||
|
||||
Assert.assertEquals(array.length, tree.size());
|
||||
|
||||
for (int i = 0; i < array.length; ++i) {
|
||||
Assert.assertEquals(array[i], value(tree.getNotSmaller(new Integer(array[i]))));
|
||||
}
|
||||
|
||||
checkOrder(tree);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelete1() {
|
||||
int[][][] arrays = {
|
||||
{ { 16, 13, 15, 14, 2, 0, 12, 9, 8, 5, 11, 18, 19, 17, 4, 7, 1, 3, 6, 10 },
|
||||
{ 11, 10, 9, 12, 16, 15, 13, 18, 5, 0, 3, 2, 14, 6, 19, 17, 8, 4, 7, 1 } },
|
||||
{ { 16, 13, 15, 14, 2, 0, 12, 9, 8, 5, 11, 18, 19, 17, 4, 7, 1, 3, 6, 10 },
|
||||
{ 0, 17, 14, 15, 16, 18, 6 } },
|
||||
{ { 6, 2, 7, 8, 1, 4, 3, 5 }, { 8 } },
|
||||
{ { 6, 2, 7, 8, 1, 4, 5 }, { 8 } },
|
||||
{ { 3, 7, 2, 1, 5, 8, 4 }, { 1 } },
|
||||
{ { 3, 7, 2, 1, 5, 8, 6 }, { 1 } }
|
||||
};
|
||||
for (int i = 0; i < arrays.length; ++i) {
|
||||
AVLTree<Integer> tree = buildTree(arrays[i][0]);
|
||||
Assert.assertTrue(! tree.delete(new Integer(-2000)));
|
||||
for (int j = 0; j < arrays[i][1].length; ++j) {
|
||||
Assert.assertTrue(tree.delete(tree.getNotSmaller(new Integer(arrays[i][1][j])).getElement()));
|
||||
Assert.assertEquals(arrays[i][0].length - j - 1, tree.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNavigation() {
|
||||
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
|
||||
AVLTree<Integer> tree = buildTree(array);
|
||||
|
||||
AVLTree<Integer>.Node node = tree.getSmallest();
|
||||
Assert.assertEquals(array[0], value(node));
|
||||
for (int i = 0; i < array.length; ++i) {
|
||||
Assert.assertEquals(array[i], value(node));
|
||||
node = node.getNext();
|
||||
}
|
||||
Assert.assertNull(node);
|
||||
|
||||
node = tree.getLargest();
|
||||
Assert.assertEquals(array[array.length - 1], value(node));
|
||||
for (int i = array.length - 1; i >= 0; --i) {
|
||||
Assert.assertEquals(array[i], value(node));
|
||||
node = node.getPrevious();
|
||||
}
|
||||
Assert.assertNull(node);
|
||||
|
||||
checkOrder(tree);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSearch() {
|
||||
int[] array = { 2, 4, 6, 8, 10, 12, 14 };
|
||||
AVLTree<Integer> tree = buildTree(array);
|
||||
|
||||
Assert.assertNull(tree.getNotLarger(new Integer(array[0] - 1)));
|
||||
Assert.assertNull(tree.getNotSmaller(new Integer(array[array.length - 1] + 1)));
|
||||
|
||||
for (int i = 0; i < array.length; ++i) {
|
||||
Assert.assertEquals(array[i],
|
||||
value(tree.getNotSmaller(new Integer(array[i] - 1))));
|
||||
Assert.assertEquals(array[i],
|
||||
value(tree.getNotLarger(new Integer(array[i] + 1))));
|
||||
}
|
||||
|
||||
checkOrder(tree);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRepetition() {
|
||||
int[] array = { 1, 1, 3, 3, 4, 5, 6, 7, 7, 7, 7, 7 };
|
||||
AVLTree<Integer> tree = buildTree(array);
|
||||
Assert.assertEquals(array.length, tree.size());
|
||||
|
||||
AVLTree<Integer>.Node node = tree.getNotSmaller(new Integer(3));
|
||||
Assert.assertEquals(3, value(node));
|
||||
Assert.assertEquals(1, value(node.getPrevious()));
|
||||
Assert.assertEquals(3, value(node.getNext()));
|
||||
Assert.assertEquals(4, value(node.getNext().getNext()));
|
||||
|
||||
node = tree.getNotLarger(new Integer(2));
|
||||
Assert.assertEquals(1, value(node));
|
||||
Assert.assertEquals(1, value(node.getPrevious()));
|
||||
Assert.assertEquals(3, value(node.getNext()));
|
||||
Assert.assertNull(node.getPrevious().getPrevious());
|
||||
|
||||
AVLTree<Integer>.Node otherNode = tree.getNotSmaller(new Integer(1));
|
||||
Assert.assertTrue(node != otherNode);
|
||||
Assert.assertEquals(1, value(otherNode));
|
||||
Assert.assertNull(otherNode.getPrevious());
|
||||
|
||||
node = tree.getNotLarger(new Integer(10));
|
||||
Assert.assertEquals(7, value(node));
|
||||
Assert.assertNull(node.getNext());
|
||||
node = node.getPrevious();
|
||||
Assert.assertEquals(7, value(node));
|
||||
node = node.getPrevious();
|
||||
Assert.assertEquals(7, value(node));
|
||||
node = node.getPrevious();
|
||||
Assert.assertEquals(7, value(node));
|
||||
node = node.getPrevious();
|
||||
Assert.assertEquals(7, value(node));
|
||||
node = node.getPrevious();
|
||||
Assert.assertEquals(6, value(node));
|
||||
|
||||
checkOrder(tree);
|
||||
|
||||
}
|
||||
|
||||
private AVLTree<Integer> buildTree(int[] array) {
|
||||
AVLTree<Integer> tree = new AVLTree<Integer>();
|
||||
for (int i = 0; i < array.length; ++i) {
|
||||
tree.insert(new Integer(array[i]));
|
||||
tree.insert(null);
|
||||
}
|
||||
return tree;
|
||||
}
|
||||
|
||||
private int value(AVLTree<Integer>.Node node) {
|
||||
return ((Integer) node.getElement()).intValue();
|
||||
}
|
||||
|
||||
private void checkOrder(AVLTree<Integer> tree) {
|
||||
AVLTree<Integer>.Node next = null;
|
||||
for (AVLTree<Integer>.Node node = tree.getSmallest();
|
||||
node != null;
|
||||
node = next) {
|
||||
next = node.getNext();
|
||||
if (next != null) {
|
||||
Assert.assertTrue(node.getElement().compareTo(next.getElement()) <= 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue