Copied: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Vector3DFormat.java (from r1124619, commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Vector3DFormat.java) URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Vector3DFormat.java?p2=commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Vector3DFormat.java&p1=commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Vector3DFormat.java&r1=1124619&r2=1131123&rev=1131123&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Vector3DFormat.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Vector3DFormat.java Fri Jun 3 18:02:48 2011 @@ -22,8 +22,10 @@ import java.text.NumberFormat; import java.text.ParsePosition; import java.util.Locale; -import org.apache.commons.math.util.CompositeFormat; import org.apache.commons.math.exception.MathParseException; +import org.apache.commons.math.geometry.Vector; +import org.apache.commons.math.geometry.VectorFormat; +import org.apache.commons.math.util.CompositeFormat; /** * Formats a 3D vector in components list format "{x; y; z}". @@ -36,31 +38,9 @@ import org.apache.commons.math.exception * returned. In the second case, however, the parse position after parsing will be * just after the closing curly brace, i.e. just before the trailing space.</p> * - * @version $Revision$ $Date$ + * @version $Id:$ */ -public class Vector3DFormat { - /** Serializable version identifier */ - private static final long serialVersionUID = -5447606608652576301L; - /** The default prefix: "{". */ - private static final String DEFAULT_PREFIX = "{"; - /** The default suffix: "}". */ - private static final String DEFAULT_SUFFIX = "}"; - /** The default separator: ", ". */ - private static final String DEFAULT_SEPARATOR = "; "; - /** Prefix. */ - private final String prefix; - /** Suffix. */ - private final String suffix; - /** Separator. */ - private final String separator; - /** Trimmed prefix. */ - private final String trimmedPrefix; - /** Trimmed suffix. */ - private final String trimmedSuffix; - /** Trimmed separator. */ - private final String trimmedSeparator; - /** The format used for components. */ - private final NumberFormat format; +public class Vector3DFormat extends VectorFormat<Euclidean3D> { /** * Create an instance with default settings. @@ -68,8 +48,8 @@ public class Vector3DFormat { * "{", "}", and "; " and the default number format for components.</p> */ public Vector3DFormat() { - this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, - CompositeFormat.getDefaultNumberFormat()); + super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, + CompositeFormat.getDefaultNumberFormat()); } /** @@ -77,7 +57,7 @@ public class Vector3DFormat { * @param format the custom format for components. */ public Vector3DFormat(final NumberFormat format) { - this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, format); + super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, format); } /** @@ -87,8 +67,8 @@ public class Vector3DFormat { * @param separator separator to use instead of the default "; " */ public Vector3DFormat(final String prefix, final String suffix, - final String separator) { - this(prefix, suffix, separator, CompositeFormat.getDefaultNumberFormat()); + final String separator) { + super(prefix, suffix, separator, CompositeFormat.getDefaultNumberFormat()); } /** @@ -100,55 +80,8 @@ public class Vector3DFormat { * @param format the custom format for components. */ public Vector3DFormat(final String prefix, final String suffix, - final String separator, final NumberFormat format) { - this.prefix = prefix; - this.suffix = suffix; - this.separator = separator; - trimmedPrefix = prefix.trim(); - trimmedSuffix = suffix.trim(); - trimmedSeparator = separator.trim(); - this.format = format; - } - - /** - * Get the set of locales for which 3D vectors formats are available. - * <p>This is the same set as the {@link NumberFormat} set.</p> - * @return available 3D vector format locales. - */ - public static Locale[] getAvailableLocales() { - return NumberFormat.getAvailableLocales(); - } - - /** - * Get the format prefix. - * @return format prefix. - */ - public String getPrefix() { - return prefix; - } - - /** - * Get the format suffix. - * @return format suffix. - */ - public String getSuffix() { - return suffix; - } - - /** - * Get the format separator between components. - * @return format separator. - */ - public String getSeparator() { - return separator; - } - - /** - * Get the components format. - * @return components format. - */ - public NumberFormat getFormat() { - return format; + final String separator, final NumberFormat format) { + super(prefix, suffix, separator, format); } /** @@ -169,16 +102,6 @@ public class Vector3DFormat { } /** - * This method calls {@link #format(Vector3D,StringBuffer,FieldPosition)}. - * - * @param v Vector3D object to format. - * @return a formatted vector. - */ - public String format(Vector3D v) { - return format(v, new StringBuffer(), new FieldPosition(0)).toString(); - } - - /** * Formats a {@link Vector3D} object to produce a string. * @param vector the object to format. * @param toAppendTo where the text is to be appended @@ -186,26 +109,10 @@ public class Vector3DFormat { * offsets of the alignment field * @return the value passed in as toAppendTo. */ - public StringBuffer format(Vector3D vector, StringBuffer toAppendTo, - FieldPosition pos) { - - pos.setBeginIndex(0); - pos.setEndIndex(0); - - // format prefix - toAppendTo.append(prefix); - - // format components - CompositeFormat.formatDouble(vector.getX(), format, toAppendTo, pos); - toAppendTo.append(separator); - CompositeFormat.formatDouble(vector.getY(), format, toAppendTo, pos); - toAppendTo.append(separator); - CompositeFormat.formatDouble(vector.getZ(), format, toAppendTo, pos); - - // format suffix - toAppendTo.append(suffix); - - return toAppendTo; + public StringBuffer format(final Vector<Euclidean3D> vector, final StringBuffer toAppendTo, + final FieldPosition pos) { + final Vector3D v3 = (Vector3D) vector; + return format(toAppendTo, pos, v3.getX(), v3.getY(), v3.getZ()); } /** @@ -215,7 +122,7 @@ public class Vector3DFormat { * @throws MathParseException if the beginning of the specified string * cannot be parsed. */ - public Vector3D parse(String source) { + public Vector3D parse(final String source) { ParsePosition parsePosition = new ParsePosition(0); Vector3D result = parse(source, parsePosition); if (parsePosition.getIndex() == 0) { @@ -232,59 +139,12 @@ public class Vector3DFormat { * @param pos input/ouput parsing parameter. * @return the parsed {@link Vector3D} object. */ - public Vector3D parse(String source, ParsePosition pos) { - int initialIndex = pos.getIndex(); - - // parse prefix - CompositeFormat.parseAndIgnoreWhitespace(source, pos); - if (!CompositeFormat.parseFixedstring(source, trimmedPrefix, pos)) { - return null; - } - - // parse X component - CompositeFormat.parseAndIgnoreWhitespace(source, pos); - Number x = CompositeFormat.parseNumber(source, format, pos); - if (x == null) { - // invalid abscissa - // set index back to initial, error index should already be set - pos.setIndex(initialIndex); - return null; - } - - // parse Y component - CompositeFormat.parseAndIgnoreWhitespace(source, pos); - if (!CompositeFormat.parseFixedstring(source, trimmedSeparator, pos)) { + public Vector3D parse(final String source, final ParsePosition pos) { + final double[] coordinates = parseCoordinates(3, source, pos); + if (coordinates == null) { return null; } - CompositeFormat.parseAndIgnoreWhitespace(source, pos); - Number y = CompositeFormat.parseNumber(source, format, pos); - if (y == null) { - // invalid ordinate - // set index back to initial, error index should already be set - pos.setIndex(initialIndex); - return null; - } - - // parse Z component - CompositeFormat.parseAndIgnoreWhitespace(source, pos); - if (!CompositeFormat.parseFixedstring(source, trimmedSeparator, pos)) { - return null; - } - CompositeFormat.parseAndIgnoreWhitespace(source, pos); - Number z = CompositeFormat.parseNumber(source, format, pos); - if (z == null) { - // invalid height - // set index back to initial, error index should already be set - pos.setIndex(initialIndex); - return null; - } - - // parse suffix - CompositeFormat.parseAndIgnoreWhitespace(source, pos); - if (!CompositeFormat.parseFixedstring(source, trimmedSuffix, pos)) { - return null; - } - - return new Vector3D(x.doubleValue(), y.doubleValue(), z.doubleValue()); + return new Vector3D(coordinates[0], coordinates[1], coordinates[2]); } + }
Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Euclidean2D.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Euclidean2D.java?rev=1131123&view=auto ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Euclidean2D.java (added) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Euclidean2D.java Fri Jun 3 18:02:48 2011 @@ -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.twod; + +import java.io.Serializable; + +import org.apache.commons.math.geometry.Space; +import org.apache.commons.math.geometry.euclidean.oned.Euclidean1D; + +/** + * This class implements a three-dimensional space. + * @version $Id:$ + * @since 3.0 + */ +public class Euclidean2D implements Serializable, Space { + + /** Serializable version identifier. */ + private static final long serialVersionUID = 4793432849757649566L; + + /** Private constructor for the singleton. + */ + private Euclidean2D() { + } + + /** Get the unique instance. + * @return the unique instance + */ + public static Euclidean2D getInstance() { + return LazyHolder.INSTANCE; + } + + /** {@inheritDoc} */ + public int getDimension() { + return 2; + } + + /** {@inheritDoc} */ + public Euclidean1D getSubSpace() { + return Euclidean1D.getInstance(); + } + + // CHECKSTYLE: stop HideUtilityClassConstructor + /** Holder for the instance. + * <p>We use here the Initialization On Demand Holder Idiom.</p> + */ + private static class LazyHolder { + /** Cached field instance. */ + private static final Euclidean2D INSTANCE = new Euclidean2D(); + } + // CHECKSTYLE: resume HideUtilityClassConstructor + + /** Handle deserialization of the singleton. + * @return the singleton instance + */ + private Object readResolve() { + // return the singleton instance + return LazyHolder.INSTANCE; + } + +} Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Euclidean2D.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Euclidean2D.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Copied: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2D.java (from r1128876, commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Point2D.java) URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2D.java?p2=commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2D.java&p1=commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Point2D.java&r1=1128876&r2=1131123&rev=1131123&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Point2D.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2D.java Fri Jun 3 18:02:48 2011 @@ -16,57 +16,331 @@ */ package org.apache.commons.math.geometry.euclidean.twod; -import org.apache.commons.math.geometry.partitioning.Point; -import org.apache.commons.math.geometry.partitioning.SubSpace; +import java.text.NumberFormat; -/** This class represents a 2D point. +import org.apache.commons.math.geometry.Space; +import org.apache.commons.math.geometry.Vector; +import org.apache.commons.math.util.FastMath; +import org.apache.commons.math.util.MathUtils; + +/** This class represents a 2D vector. * <p>Instances of this class are guaranteed to be immutable.</p> - * @version $Revision$ $Date$ + * @version $Id:$ + * @since 3.0 */ -public class Point2D extends java.awt.geom.Point2D.Double implements Point, SubSpace { +public class Vector2D implements Vector<Euclidean2D> { + + /** Origin (coordinates: 0, 0). */ + public static final Vector2D ZERO = new Vector2D(0, 0); + + // CHECKSTYLE: stop ConstantName + /** A vector with all coordinates set to NaN. */ + public static final Vector2D NaN = new Vector2D(Double.NaN, Double.NaN); + // CHECKSTYLE: resume ConstantName - /** Point at undefined (NaN) coordinates. */ - public static final Point2D UNDEFINED = new Point2D(java.lang.Double.NaN, java.lang.Double.NaN); + /** A vector with all coordinates set to positive infinity. */ + public static final Vector2D POSITIVE_INFINITY = + new Vector2D(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); + + /** A vector with all coordinates set to negative infinity. */ + public static final Vector2D NEGATIVE_INFINITY = + new Vector2D(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY); /** Serializable UID. */ - private static final long serialVersionUID = 8883702098988517151L; + private static final long serialVersionUID = 266938651998679754L; - /** Build a point with default coordinates. - */ - public Point2D() { - } + /** Abscissa. */ + private final double x; + + /** Ordinate. */ + private final double y; - /** Build a point from its coordinates. + /** Simple constructor. + * Build a vector from its coordinates * @param x abscissa * @param y ordinate + * @see #getX() + * @see #getY() + */ + public Vector2D(double x, double y) { + this.x = x; + this.y = y; + } + + /** 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 Point2D(final double x, final double y) { - super(x, y); + public Vector2D(double a, Vector2D u) { + this.x = a * u.x; + this.y = a * u.y; } - /** Build a point from a java awt point. - * @param point java awt point + /** 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 Point2D(final java.awt.geom.Point2D.Double point) { - super(point.x, point.y); + public Vector2D(double a1, Vector2D u1, double a2, Vector2D u2) { + this.x = a1 * u1.x + a2 * u2.x; + this.y = a1 * u1.y + a2 * u2.y; } - /** Transform a 2D space point into a sub-space point. - * @param point 2D point of the space - * @return always return null - * @see #toSpace + /** 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 Point toSubSpace(final Point point) { - return null; + public Vector2D(double a1, Vector2D u1, double a2, Vector2D u2, + double a3, Vector2D u3) { + this.x = a1 * u1.x + a2 * u2.x + a3 * u3.x; + this.y = a1 * u1.y + a2 * u2.y + a3 * u3.y; } - /** Transform a sub-space point into a space point. - * @param point ignored parameter - * @return always return the instance - * @see #toSubSpace + /** 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 Point toSpace(final Point point) { - return this; + public Vector2D(double a1, Vector2D u1, double a2, Vector2D u2, + double a3, Vector2D u3, double a4, Vector2D u4) { + this.x = a1 * u1.x + a2 * u2.x + a3 * u3.x + a4 * u4.x; + this.y = a1 * u1.y + a2 * u2.y + a3 * u3.y + a4 * u4.y; + } + + /** Get the abscissa of the vector. + * @return abscissa of the vector + * @see #Vector2D(double, double) + */ + public double getX() { + return x; + } + + /** Get the ordinate of the vector. + * @return ordinate of the vector + * @see #Vector2D(double, double) + */ + public double getY() { + return y; + } + + /** {@inheritDoc} */ + public Space getSpace() { + return Euclidean2D.getInstance(); + } + + /** {@inheritDoc} */ + public Vector2D getZero() { + return ZERO; + } + + /** {@inheritDoc} */ + public Vector2D toVector() { + return new Vector2D(x, y); + } + + /** {@inheritDoc} */ + public double getNorm1() { + return FastMath.abs(x) + FastMath.abs(y); + } + + /** {@inheritDoc} */ + public double getNorm() { + return FastMath.sqrt (x * x + y * y); + } + + /** {@inheritDoc} */ + public double getNormSq() { + return x * x + y * y; + } + + /** {@inheritDoc} */ + public double getNormInf() { + return FastMath.max(FastMath.abs(x), FastMath.abs(y)); + } + + /** {@inheritDoc} */ + public Vector2D add(Vector<Euclidean2D> v) { + Vector2D v2 = (Vector2D) v; + return new Vector2D(x + v2.getX(), y + v2.getY()); + } + + /** {@inheritDoc} */ + public Vector2D add(double factor, Vector<Euclidean2D> v) { + Vector2D v2 = (Vector2D) v; + return new Vector2D(x + factor * v2.getX(), y + factor * v2.getY()); + } + + /** {@inheritDoc} */ + public Vector2D subtract(Vector<Euclidean2D> p) { + Vector2D p3 = (Vector2D) p; + return new Vector2D(x - p3.x, y - p3.y); + } + + /** {@inheritDoc} */ + public Vector2D subtract(double factor, Vector<Euclidean2D> v) { + Vector2D v2 = (Vector2D) v; + return new Vector2D(x - factor * v2.getX(), y - factor * v2.getY()); + } + + /** {@inheritDoc} */ + public boolean isNaN() { + return Double.isNaN(x) || Double.isNaN(y); + } + + /** {@inheritDoc} */ + public boolean isInfinite() { + return !isNaN() && (Double.isInfinite(x) || Double.isInfinite(y)); + } + + /** {@inheritDoc} */ + public double distance1(Vector<Euclidean2D> p) { + Vector2D p3 = (Vector2D) p; + final double dx = FastMath.abs(p3.x - x); + final double dy = FastMath.abs(p3.y - y); + return dx + dy; + } + + /** {@inheritDoc} */ + public double distance(Vector<Euclidean2D> p) { + Vector2D p3 = (Vector2D) p; + final double dx = p3.x - x; + final double dy = p3.y - y; + return FastMath.sqrt(dx * dx + dy * dy); + } + + /** {@inheritDoc} */ + public double distanceInf(Vector<Euclidean2D> p) { + Vector2D p3 = (Vector2D) p; + final double dx = FastMath.abs(p3.x - x); + final double dy = FastMath.abs(p3.y - y); + return FastMath.max(dx, dy); + } + + /** {@inheritDoc} */ + public double distanceSq(Vector<Euclidean2D> p) { + Vector2D p3 = (Vector2D) p; + final double dx = p3.x - x; + final double dy = p3.y - y; + return dx * dx + dy * dy; + } + + /** Compute the distance between two vectors according to the L<sub>2</sub> norm. + * <p>Calling this method is equivalent to calling: + * <code>p1.subtract(p2).getNorm()</code> except that no intermediate + * vector is built</p> + * @param p1 first vector + * @param p2 second vector + * @return the distance between p1 and p2 according to the L<sub>2</sub> norm + */ + public static double distance(Vector2D p1, Vector2D p2) { + return p1.distance(p2); + } + + /** Compute the distance between two vectors according to the L<sub>∞</sub> norm. + * <p>Calling this method is equivalent to calling: + * <code>p1.subtract(p2).getNormInf()</code> except that no intermediate + * vector is built</p> + * @param p1 first vector + * @param p2 second vector + * @return the distance between p1 and p2 according to the L<sub>∞</sub> norm + */ + public static double distanceInf(Vector2D p1, Vector2D p2) { + return p1.distanceInf(p2); + } + + /** Compute the square of the distance between two vectors. + * <p>Calling this method is equivalent to calling: + * <code>p1.subtract(p2).getNormSq()</code> except that no intermediate + * vector is built</p> + * @param p1 first vector + * @param p2 second vector + * @return the square of the distance between p1 and p2 + */ + public static double distanceSq(Vector2D p1, Vector2D p2) { + return p1.distanceSq(p2); + } + + /** + * Test for the equality of two 2D vectors. + * <p> + * If all coordinates of two 2D vectors are exactly the same, and none are + * <code>Double.NaN</code>, the two 2D vectors are considered to be equal. + * </p> + * <p> + * <code>NaN</code> coordinates are considered to affect globally the vector + * and be equals to each other - i.e, if either (or all) coordinates of the + * 2D vector are equal to <code>Double.NaN</code>, the 2D vector is equal to + * {@link #NaN}. + * </p> + * + * @param other Object to test for equality to this + * @return true if two 2D vector objects are equal, false if + * object is null, not an instance of Vector2D, or + * not equal to this Vector2D instance + * + */ + @Override + public boolean equals(Object other) { + + if (this == other) { + return true; + } + + if (other instanceof Vector2D) { + final Vector2D rhs = (Vector2D)other; + if (rhs.isNaN()) { + return this.isNaN(); + } + + return (x == rhs.x) && (y == rhs.y); + } + return false; + } + + /** + * Get a hashCode for the 2D vector. + * <p> + * All NaN values have the same hash code.</p> + * + * @return a hash code value for this object + */ + @Override + public int hashCode() { + if (isNaN()) { + return 542; + } + return 122 * (76 * MathUtils.hash(x) + MathUtils.hash(y)); + } + + /** Get a string representation of this vector. + * @return a string representation of this vector + */ + @Override + public String toString() { + return Vector2DFormat.getInstance().format(this); + } + + /** {@inheritDoc} */ + public String toString(final NumberFormat format) { + return new Vector2DFormat(format).format(this); } } Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2DFormat.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2DFormat.java?rev=1131123&view=auto ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2DFormat.java (added) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2DFormat.java Fri Jun 3 18:02:48 2011 @@ -0,0 +1,133 @@ +/* + * 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.text.FieldPosition; +import java.text.NumberFormat; +import java.text.ParsePosition; +import java.util.Locale; + +import org.apache.commons.math.exception.MathParseException; +import org.apache.commons.math.geometry.Vector; +import org.apache.commons.math.geometry.VectorFormat; +import org.apache.commons.math.util.CompositeFormat; + +/** + * Formats a 2D vector in components list format "{x; y}". + * <p>The prefix and suffix "{" and "}" and the separator "; " can be replaced by + * any user-defined strings. The number format for components can be configured.</p> + * <p>White space is ignored at parse time, even if it is in the prefix, suffix + * or separator specifications. So even if the default separator does include a space + * character that is used at format time, both input string "{1;1}" and + * " { 1 ; 1 } " will be parsed without error and the same vector will be + * returned. In the second case, however, the parse position after parsing will be + * just after the closing curly brace, i.e. just before the trailing space.</p> + * + * @version $Id:$ + * @since 3.0 + */ +public class Vector2DFormat extends VectorFormat<Euclidean2D> { + + /** + * Create an instance with default settings. + * <p>The instance uses the default prefix, suffix and separator: + * "{", "}", and "; " and the default number format for components.</p> + */ + public Vector2DFormat() { + super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, + CompositeFormat.getDefaultNumberFormat()); + } + + /** + * Create an instance with a custom number format for components. + * @param format the custom format for components. + */ + public Vector2DFormat(final NumberFormat format) { + super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, format); + } + + /** + * Create an instance with custom prefix, suffix and separator. + * @param prefix prefix to use instead of the default "{" + * @param suffix suffix to use instead of the default "}" + * @param separator separator to use instead of the default "; " + */ + public Vector2DFormat(final String prefix, final String suffix, + final String separator) { + super(prefix, suffix, separator, CompositeFormat.getDefaultNumberFormat()); + } + + /** + * Create an instance with custom prefix, suffix, separator and format + * for components. + * @param prefix prefix to use instead of the default "{" + * @param suffix suffix to use instead of the default "}" + * @param separator separator to use instead of the default "; " + * @param format the custom format for components. + */ + public Vector2DFormat(final String prefix, final String suffix, + final String separator, final NumberFormat format) { + super(prefix, suffix, separator, format); + } + + /** + * Returns the default 2D vector format for the current locale. + * @return the default 2D vector format. + */ + public static Vector2DFormat getInstance() { + return getInstance(Locale.getDefault()); + } + + /** + * Returns the default 2D vector format for the given locale. + * @param locale the specific locale used by the format. + * @return the 2D vector format specific to the given locale. + */ + public static Vector2DFormat getInstance(final Locale locale) { + return new Vector2DFormat(CompositeFormat.getDefaultNumberFormat(locale)); + } + + /** {@inheritDoc} */ + public StringBuffer format(final Vector<Euclidean2D> vector, final StringBuffer toAppendTo, + final FieldPosition pos) { + final Vector2D p2 = (Vector2D) vector; + return format(toAppendTo, pos, p2.getX(), p2.getY()); + } + + /** {@inheritDoc} */ + public Vector2D parse(final String source) { + ParsePosition parsePosition = new ParsePosition(0); + Vector2D result = parse(source, parsePosition); + if (parsePosition.getIndex() == 0) { + throw new MathParseException(source, + parsePosition.getErrorIndex(), + Vector2D.class); + } + return result; + } + + /** {@inheritDoc} */ + public Vector2D parse(final String source, final ParsePosition pos) { + final double[] coordinates = parseCoordinates(2, source, pos); + if (coordinates == null) { + return null; + } + return new Vector2D(coordinates[0], coordinates[1]); + } + +} Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2DFormat.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2DFormat.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/package.html URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/package.html?rev=1131123&view=auto ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/package.html (added) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/package.html Fri Jun 3 18:02:48 2011 @@ -0,0 +1,25 @@ +<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 is the top level package for geometry. It provides only a few interfaces +related to vectorial/affine spaces that are implemented in sub-packages. +</p> +</body> +</html> Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/package.html ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/package.html ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Copied: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/partitioning/Embedding.java (from r1124588, commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/partitioning/SubSpace.java) URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/partitioning/Embedding.java?p2=commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/partitioning/Embedding.java&p1=commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/partitioning/SubSpace.java&r1=1124588&r2=1131123&rev=1131123&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/partitioning/SubSpace.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/partitioning/Embedding.java Fri Jun 3 18:02:48 2011 @@ -16,20 +16,32 @@ */ package org.apache.commons.math.geometry.partitioning; +import org.apache.commons.math.geometry.Vector; +import org.apache.commons.math.geometry.Space; -/** This interface represents a sub-space of a space. +/** This interface defines mappers between a space and one of its sub-spaces. * <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> + * as {@link Hyperplane hyperplanes}. This interface can be used regardless + * of the dimensions differences. As an example, {@link + * org.apache.commons.math.geometry.euclidean.threed.Line Line} in 3D + * implements Embedding<{@link + * org.apache.commons.math.geometry.euclidean.threed.Vector3D Vector3D}, {link + * org.apache.commons.math.geometry.euclidean.oned.Vector1D Vector1D>, i.e. it + * maps directly dimensions 3 and 1.</p> * <p>In the 3D euclidean space, hyperplanes are 2D planes, and the 1D * sub-spaces are lines.</p> + * @param <S> Type of the embedding space. + * @param <T> Type of the embedded sub-space. + * @see Hyperplane - * @version $Revision$ $Date$ + * @version $Id:$ + * @since 3.0 */ -public interface SubSpace { +public interface Embedding<S extends Space, T extends Space> { /** Transform a space point into a sub-space point. * @param point n-dimension point of the space @@ -37,7 +49,7 @@ public interface SubSpace { * the specified space point * @see #toSpace */ - Point toSubSpace(Point point); + Vector<T> toSubSpace(Vector<S> point); /** Transform a sub-space point into a space point. * @param point (n-1)-dimension point of the sub-space @@ -45,6 +57,6 @@ public interface SubSpace { * specified sub-space point * @see #toSubSpace */ - Point toSpace(Point point); + Vector<S> toSpace(Vector<T> point); } Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=1131123&r1=1131122&r2=1131123&view=diff ============================================================================== --- commons/proper/math/trunk/src/site/xdoc/changes.xml (original) +++ commons/proper/math/trunk/src/site/xdoc/changes.xml Fri Jun 3 18:02:48 2011 @@ -52,6 +52,9 @@ The <action> type attribute can be add,u 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"> + Added a consistent classes hierarchy for Euclidean spaces in dimension 1, 2 and 3. + </action> <action dev="luc" type="fix" issue="MATH-579"> Improved javadoc for FastMath explaining the overhead at class loading and the targeted use cases.