http://git-wip-us.apache.org/repos/asf/commons-math/blob/a7b4803f/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/Rotation.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/Rotation.java b/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/Rotation.java deleted file mode 100644 index 7945c7c..0000000 --- a/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/Rotation.java +++ /dev/null @@ -1,1053 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.commons.math3.geometry.euclidean.threed; - -import java.io.Serializable; - -import org.apache.commons.math3.exception.MathArithmeticException; -import org.apache.commons.math3.exception.MathIllegalArgumentException; -import org.apache.commons.math3.exception.util.LocalizedFormats; -import org.apache.commons.math3.util.FastMath; -import org.apache.commons.math3.util.MathArrays; - -/** - * This class implements rotations in a three-dimensional space. - * - * <p>Rotations can be represented by several different mathematical - * entities (matrices, axe and angle, Cardan or Euler angles, - * quaternions). This class presents an higher level abstraction, more - * user-oriented and hiding this implementation details. Well, for the - * curious, we use quaternions for the internal representation. The - * user can build a rotation from any of these representations, and - * any of these representations can be retrieved from a - * <code>Rotation</code> instance (see the various constructors and - * getters). In addition, a rotation can also be built implicitly - * from a set of vectors and their image.</p> - * <p>This implies that this class can be used to convert from one - * representation to another one. For example, converting a rotation - * matrix into a set of Cardan angles from can be done using the - * following single line of code:</p> - * <pre> - * double[] angles = new Rotation(matrix, 1.0e-10).getAngles(RotationOrder.XYZ); - * </pre> - * <p>Focus is oriented on what a rotation <em>do</em> rather than on its - * underlying representation. Once it has been built, and regardless of its - * internal representation, a rotation is an <em>operator</em> which basically - * transforms three dimensional {@link Vector3D vectors} into other three - * dimensional {@link Vector3D vectors}. Depending on the application, the - * meaning of these vectors may vary and the semantics of the rotation also.</p> - * <p>For example in an spacecraft attitude simulation tool, users will often - * consider the vectors are fixed (say the Earth direction for example) and the - * frames change. The rotation transforms the coordinates of the vector in inertial - * frame into the coordinates of the same vector in satellite frame. In this - * case, the rotation implicitly defines the relation between the two frames.</p> - * <p>Another example could be a telescope control application, where the rotation - * would transform the sighting direction at rest into the desired observing - * direction when the telescope is pointed towards an object of interest. In this - * case the rotation transforms the direction at rest in a topocentric frame - * into the sighting direction in the same topocentric frame. This implies in this - * case the frame is fixed and the vector moves.</p> - * <p>In many case, both approaches will be combined. In our telescope example, - * we will probably also need to transform the observing direction in the topocentric - * frame into the observing direction in inertial frame taking into account the observatory - * location and the Earth rotation, which would essentially be an application of the - * first approach.</p> - * - * <p>These examples show that a rotation is what the user wants it to be. This - * class does not push the user towards one specific definition and hence does not - * provide methods like <code>projectVectorIntoDestinationFrame</code> or - * <code>computeTransformedDirection</code>. It provides simpler and more generic - * methods: {@link #applyTo(Vector3D) applyTo(Vector3D)} and {@link - * #applyInverseTo(Vector3D) applyInverseTo(Vector3D)}.</p> - * - * <p>Since a rotation is basically a vectorial operator, several rotations can be - * composed together and the composite operation <code>r = r<sub>1</sub> o - * r<sub>2</sub></code> (which means that for each vector <code>u</code>, - * <code>r(u) = r<sub>1</sub>(r<sub>2</sub>(u))</code>) is also a rotation. Hence - * we can consider that in addition to vectors, a rotation can be applied to other - * rotations as well (or to itself). With our previous notations, we would say we - * can apply <code>r<sub>1</sub></code> to <code>r<sub>2</sub></code> and the result - * we get is <code>r = r<sub>1</sub> o r<sub>2</sub></code>. For this purpose, the - * class provides the methods: {@link #applyTo(Rotation) applyTo(Rotation)} and - * {@link #applyInverseTo(Rotation) applyInverseTo(Rotation)}.</p> - * - * <p>Rotations are guaranteed to be immutable objects.</p> - * - * @see Vector3D - * @see RotationOrder - * @since 1.2 - */ - -public class Rotation implements Serializable { - - /** Identity rotation. */ - public static final Rotation IDENTITY = new Rotation(1.0, 0.0, 0.0, 0.0, false); - - /** Serializable version identifier */ - private static final long serialVersionUID = -2153622329907944313L; - - /** Scalar coordinate of the quaternion. */ - private final double q0; - - /** First coordinate of the vectorial part of the quaternion. */ - private final double q1; - - /** Second coordinate of the vectorial part of the quaternion. */ - private final double q2; - - /** Third coordinate of the vectorial part of the quaternion. */ - private final double q3; - - /** Build a rotation from the quaternion coordinates. - * <p>A rotation can be built from a <em>normalized</em> quaternion, - * i.e. a quaternion for which q<sub>0</sub><sup>2</sup> + - * q<sub>1</sub><sup>2</sup> + q<sub>2</sub><sup>2</sup> + - * q<sub>3</sub><sup>2</sup> = 1. If the quaternion is not normalized, - * the constructor can normalize it in a preprocessing step.</p> - * <p>Note that some conventions put the scalar part of the quaternion - * as the 4<sup>th</sup> component and the vector part as the first three - * components. This is <em>not</em> our convention. We put the scalar part - * as the first component.</p> - * @param q0 scalar part of the quaternion - * @param q1 first coordinate of the vectorial part of the quaternion - * @param q2 second coordinate of the vectorial part of the quaternion - * @param q3 third coordinate of the vectorial part of the quaternion - * @param needsNormalization if true, the coordinates are considered - * not to be normalized, a normalization preprocessing step is performed - * before using them - */ - public Rotation(double q0, double q1, double q2, double q3, - boolean needsNormalization) { - - if (needsNormalization) { - // normalization preprocessing - double inv = 1.0 / FastMath.sqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3); - q0 *= inv; - q1 *= inv; - q2 *= inv; - q3 *= inv; - } - - this.q0 = q0; - this.q1 = q1; - this.q2 = q2; - this.q3 = q3; - - } - - /** Build a rotation from an axis and an angle. - * <p>We use the convention that angles are oriented according to - * the effect of the rotation on vectors around the axis. That means - * that if (i, j, k) is a direct frame and if we first provide +k as - * the axis and π/2 as the angle to this constructor, and then - * {@link #applyTo(Vector3D) apply} the instance to +i, we will get - * +j.</p> - * <p>Another way to represent our convention is to say that a rotation - * of angle θ about the unit vector (x, y, z) is the same as the - * rotation build from quaternion components { cos(-θ/2), - * x * sin(-θ/2), y * sin(-θ/2), z * sin(-θ/2) }. - * Note the minus sign on the angle!</p> - * <p>On the one hand this convention is consistent with a vectorial - * perspective (moving vectors in fixed frames), on the other hand it - * is different from conventions with a frame perspective (fixed vectors - * viewed from different frames) like the ones used for example in spacecraft - * attitude community or in the graphics community.</p> - * @param axis axis around which to rotate - * @param angle rotation angle. - * @exception MathIllegalArgumentException if the axis norm is zero - */ - public Rotation(Vector3D axis, double angle) throws MathIllegalArgumentException { - - double norm = axis.getNorm(); - if (norm == 0) { - throw new MathIllegalArgumentException(LocalizedFormats.ZERO_NORM_FOR_ROTATION_AXIS); - } - - double halfAngle = -0.5 * angle; - double coeff = FastMath.sin(halfAngle) / norm; - - q0 = FastMath.cos (halfAngle); - q1 = coeff * axis.getX(); - q2 = coeff * axis.getY(); - q3 = coeff * axis.getZ(); - - } - - /** Build a rotation from a 3X3 matrix. - - * <p>Rotation matrices are orthogonal matrices, i.e. unit matrices - * (which are matrices for which m.m<sup>T</sup> = I) with real - * coefficients. The module of the determinant of unit matrices is - * 1, among the orthogonal 3X3 matrices, only the ones having a - * positive determinant (+1) are rotation matrices.</p> - - * <p>When a rotation is defined by a matrix with truncated values - * (typically when it is extracted from a technical sheet where only - * four to five significant digits are available), the matrix is not - * orthogonal anymore. This constructor handles this case - * transparently by using a copy of the given matrix and applying a - * correction to the copy in order to perfect its orthogonality. If - * the Frobenius norm of the correction needed is above the given - * threshold, then the matrix is considered to be too far from a - * true rotation matrix and an exception is thrown.<p> - - * @param m rotation matrix - * @param threshold convergence threshold for the iterative - * orthogonality correction (convergence is reached when the - * difference between two steps of the Frobenius norm of the - * correction is below this threshold) - - * @exception NotARotationMatrixException if the matrix is not a 3X3 - * matrix, or if it cannot be transformed into an orthogonal matrix - * with the given threshold, or if the determinant of the resulting - * orthogonal matrix is negative - - */ - public Rotation(double[][] m, double threshold) - throws NotARotationMatrixException { - - // dimension check - if ((m.length != 3) || (m[0].length != 3) || - (m[1].length != 3) || (m[2].length != 3)) { - throw new NotARotationMatrixException( - LocalizedFormats.ROTATION_MATRIX_DIMENSIONS, - m.length, m[0].length); - } - - // compute a "close" orthogonal matrix - double[][] ort = orthogonalizeMatrix(m, threshold); - - // check the sign of the determinant - double det = ort[0][0] * (ort[1][1] * ort[2][2] - ort[2][1] * ort[1][2]) - - ort[1][0] * (ort[0][1] * ort[2][2] - ort[2][1] * ort[0][2]) + - ort[2][0] * (ort[0][1] * ort[1][2] - ort[1][1] * ort[0][2]); - if (det < 0.0) { - throw new NotARotationMatrixException( - LocalizedFormats.CLOSEST_ORTHOGONAL_MATRIX_HAS_NEGATIVE_DETERMINANT, - det); - } - - double[] quat = mat2quat(ort); - q0 = quat[0]; - q1 = quat[1]; - q2 = quat[2]; - q3 = quat[3]; - - } - - /** Build the rotation that transforms a pair of vector into another pair. - - * <p>Except for possible scale factors, if the instance were applied to - * the pair (u<sub>1</sub>, u<sub>2</sub>) it will produce the pair - * (v<sub>1</sub>, v<sub>2</sub>).</p> - - * <p>If the angular separation between u<sub>1</sub> and u<sub>2</sub> is - * not the same as the angular separation between v<sub>1</sub> and - * v<sub>2</sub>, then a corrected v'<sub>2</sub> will be used rather than - * v<sub>2</sub>, the corrected vector will be in the (v<sub>1</sub>, - * v<sub>2</sub>) plane.</p> - - * @param u1 first vector of the origin pair - * @param u2 second vector of the origin pair - * @param v1 desired image of u1 by the rotation - * @param v2 desired image of u2 by the rotation - * @exception MathArithmeticException if the norm of one of the vectors is zero, - * or if one of the pair is degenerated (i.e. the vectors of the pair are colinear) - */ - public Rotation(Vector3D u1, Vector3D u2, Vector3D v1, Vector3D v2) - throws MathArithmeticException { - - // build orthonormalized base from u1, u2 - // this fails when vectors are null or colinear, which is forbidden to define a rotation - final Vector3D u3 = u1.crossProduct(u2).normalize(); - u2 = u3.crossProduct(u1).normalize(); - u1 = u1.normalize(); - - // build an orthonormalized base from v1, v2 - // this fails when vectors are null or colinear, which is forbidden to define a rotation - final Vector3D v3 = v1.crossProduct(v2).normalize(); - v2 = v3.crossProduct(v1).normalize(); - v1 = v1.normalize(); - - // buid a matrix transforming the first base into the second one - final double[][] m = new double[][] { - { - MathArrays.linearCombination(u1.getX(), v1.getX(), u2.getX(), v2.getX(), u3.getX(), v3.getX()), - MathArrays.linearCombination(u1.getY(), v1.getX(), u2.getY(), v2.getX(), u3.getY(), v3.getX()), - MathArrays.linearCombination(u1.getZ(), v1.getX(), u2.getZ(), v2.getX(), u3.getZ(), v3.getX()) - }, - { - MathArrays.linearCombination(u1.getX(), v1.getY(), u2.getX(), v2.getY(), u3.getX(), v3.getY()), - MathArrays.linearCombination(u1.getY(), v1.getY(), u2.getY(), v2.getY(), u3.getY(), v3.getY()), - MathArrays.linearCombination(u1.getZ(), v1.getY(), u2.getZ(), v2.getY(), u3.getZ(), v3.getY()) - }, - { - MathArrays.linearCombination(u1.getX(), v1.getZ(), u2.getX(), v2.getZ(), u3.getX(), v3.getZ()), - MathArrays.linearCombination(u1.getY(), v1.getZ(), u2.getY(), v2.getZ(), u3.getY(), v3.getZ()), - MathArrays.linearCombination(u1.getZ(), v1.getZ(), u2.getZ(), v2.getZ(), u3.getZ(), v3.getZ()) - } - }; - - double[] quat = mat2quat(m); - q0 = quat[0]; - q1 = quat[1]; - q2 = quat[2]; - q3 = quat[3]; - - } - - /** Build one of the rotations that transform one vector into another one. - - * <p>Except for a possible scale factor, if the instance were - * applied to the vector u it will produce the vector v. There is an - * infinite number of such rotations, this constructor choose the - * one with the smallest associated angle (i.e. the one whose axis - * is orthogonal to the (u, v) plane). If u and v are colinear, an - * arbitrary rotation axis is chosen.</p> - - * @param u origin vector - * @param v desired image of u by the rotation - * @exception MathArithmeticException if the norm of one of the vectors is zero - */ - public Rotation(Vector3D u, Vector3D v) throws MathArithmeticException { - - double normProduct = u.getNorm() * v.getNorm(); - if (normProduct == 0) { - throw new MathArithmeticException(LocalizedFormats.ZERO_NORM_FOR_ROTATION_DEFINING_VECTOR); - } - - double dot = u.dotProduct(v); - - if (dot < ((2.0e-15 - 1.0) * normProduct)) { - // special case u = -v: we select a PI angle rotation around - // an arbitrary vector orthogonal to u - Vector3D w = u.orthogonal(); - q0 = 0.0; - q1 = -w.getX(); - q2 = -w.getY(); - q3 = -w.getZ(); - } else { - // general case: (u, v) defines a plane, we select - // the shortest possible rotation: axis orthogonal to this plane - q0 = FastMath.sqrt(0.5 * (1.0 + dot / normProduct)); - double coeff = 1.0 / (2.0 * q0 * normProduct); - Vector3D q = v.crossProduct(u); - q1 = coeff * q.getX(); - q2 = coeff * q.getY(); - q3 = coeff * q.getZ(); - } - - } - - /** Build a rotation from three Cardan or Euler elementary rotations. - - * <p>Cardan rotations are three successive rotations around the - * canonical axes X, Y and Z, each axis being used once. There are - * 6 such sets of rotations (XYZ, XZY, YXZ, YZX, ZXY and ZYX). Euler - * rotations are three successive rotations around the canonical - * axes X, Y and Z, the first and last rotations being around the - * same axis. There are 6 such sets of rotations (XYX, XZX, YXY, - * YZY, ZXZ and ZYZ), the most popular one being ZXZ.</p> - * <p>Beware that many people routinely use the term Euler angles even - * for what really are Cardan angles (this confusion is especially - * widespread in the aerospace business where Roll, Pitch and Yaw angles - * are often wrongly tagged as Euler angles).</p> - - * @param order order of rotations to use - * @param alpha1 angle of the first elementary rotation - * @param alpha2 angle of the second elementary rotation - * @param alpha3 angle of the third elementary rotation - */ - public Rotation(RotationOrder order, - double alpha1, double alpha2, double alpha3) { - Rotation r1 = new Rotation(order.getA1(), alpha1); - Rotation r2 = new Rotation(order.getA2(), alpha2); - Rotation r3 = new Rotation(order.getA3(), alpha3); - Rotation composed = r1.applyTo(r2.applyTo(r3)); - q0 = composed.q0; - q1 = composed.q1; - q2 = composed.q2; - q3 = composed.q3; - } - - /** Convert an orthogonal rotation matrix to a quaternion. - * @param ort orthogonal rotation matrix - * @return quaternion corresponding to the matrix - */ - private static double[] mat2quat(final double[][] ort) { - - final double[] quat = new double[4]; - - // There are different ways to compute the quaternions elements - // from the matrix. They all involve computing one element from - // the diagonal of the matrix, and computing the three other ones - // using a formula involving a division by the first element, - // which unfortunately can be zero. Since the norm of the - // quaternion is 1, we know at least one element has an absolute - // value greater or equal to 0.5, so it is always possible to - // select the right formula and avoid division by zero and even - // numerical inaccuracy. Checking the elements in turn and using - // the first one greater than 0.45 is safe (this leads to a simple - // test since qi = 0.45 implies 4 qi^2 - 1 = -0.19) - double s = ort[0][0] + ort[1][1] + ort[2][2]; - if (s > -0.19) { - // compute q0 and deduce q1, q2 and q3 - quat[0] = 0.5 * FastMath.sqrt(s + 1.0); - double inv = 0.25 / quat[0]; - quat[1] = inv * (ort[1][2] - ort[2][1]); - quat[2] = inv * (ort[2][0] - ort[0][2]); - quat[3] = inv * (ort[0][1] - ort[1][0]); - } else { - s = ort[0][0] - ort[1][1] - ort[2][2]; - if (s > -0.19) { - // compute q1 and deduce q0, q2 and q3 - quat[1] = 0.5 * FastMath.sqrt(s + 1.0); - double inv = 0.25 / quat[1]; - quat[0] = inv * (ort[1][2] - ort[2][1]); - quat[2] = inv * (ort[0][1] + ort[1][0]); - quat[3] = inv * (ort[0][2] + ort[2][0]); - } else { - s = ort[1][1] - ort[0][0] - ort[2][2]; - if (s > -0.19) { - // compute q2 and deduce q0, q1 and q3 - quat[2] = 0.5 * FastMath.sqrt(s + 1.0); - double inv = 0.25 / quat[2]; - quat[0] = inv * (ort[2][0] - ort[0][2]); - quat[1] = inv * (ort[0][1] + ort[1][0]); - quat[3] = inv * (ort[2][1] + ort[1][2]); - } else { - // compute q3 and deduce q0, q1 and q2 - s = ort[2][2] - ort[0][0] - ort[1][1]; - quat[3] = 0.5 * FastMath.sqrt(s + 1.0); - double inv = 0.25 / quat[3]; - quat[0] = inv * (ort[0][1] - ort[1][0]); - quat[1] = inv * (ort[0][2] + ort[2][0]); - quat[2] = inv * (ort[2][1] + ort[1][2]); - } - } - } - - return quat; - - } - - /** Revert a rotation. - * Build a rotation which reverse the effect of another - * rotation. This means that if r(u) = v, then r.revert(v) = u. The - * instance is not changed. - * @return a new rotation whose effect is the reverse of the effect - * of the instance - */ - public Rotation revert() { - return new Rotation(-q0, q1, q2, q3, false); - } - - /** Get the scalar coordinate of the quaternion. - * @return scalar coordinate of the quaternion - */ - public double getQ0() { - return q0; - } - - /** Get the first coordinate of the vectorial part of the quaternion. - * @return first coordinate of the vectorial part of the quaternion - */ - public double getQ1() { - return q1; - } - - /** Get the second coordinate of the vectorial part of the quaternion. - * @return second coordinate of the vectorial part of the quaternion - */ - public double getQ2() { - return q2; - } - - /** Get the third coordinate of the vectorial part of the quaternion. - * @return third coordinate of the vectorial part of the quaternion - */ - public double getQ3() { - return q3; - } - - /** Get the normalized axis of the rotation. - * @return normalized axis of the rotation - * @see #Rotation(Vector3D, double) - */ - public Vector3D getAxis() { - double squaredSine = q1 * q1 + q2 * q2 + q3 * q3; - if (squaredSine == 0) { - return new Vector3D(1, 0, 0); - } else if (q0 < 0) { - double inverse = 1 / FastMath.sqrt(squaredSine); - return new Vector3D(q1 * inverse, q2 * inverse, q3 * inverse); - } - double inverse = -1 / FastMath.sqrt(squaredSine); - return new Vector3D(q1 * inverse, q2 * inverse, q3 * inverse); - } - - /** Get the angle of the rotation. - * @return angle of the rotation (between 0 and π) - * @see #Rotation(Vector3D, double) - */ - public double getAngle() { - if ((q0 < -0.1) || (q0 > 0.1)) { - return 2 * FastMath.asin(FastMath.sqrt(q1 * q1 + q2 * q2 + q3 * q3)); - } else if (q0 < 0) { - return 2 * FastMath.acos(-q0); - } - return 2 * FastMath.acos(q0); - } - - /** Get the Cardan or Euler angles corresponding to the instance. - - * <p>The equations show that each rotation can be defined by two - * different values of the Cardan or Euler angles set. For example - * if Cardan angles are used, the rotation defined by the angles - * a<sub>1</sub>, a<sub>2</sub> and a<sub>3</sub> is the same as - * the rotation defined by the angles π + a<sub>1</sub>, π - * - a<sub>2</sub> and π + a<sub>3</sub>. This method implements - * the following arbitrary choices:</p> - * <ul> - * <li>for Cardan angles, the chosen set is the one for which the - * second angle is between -π/2 and π/2 (i.e its cosine is - * positive),</li> - * <li>for Euler angles, the chosen set is the one for which the - * second angle is between 0 and π (i.e its sine is positive).</li> - * </ul> - - * <p>Cardan and Euler angle have a very disappointing drawback: all - * of them have singularities. This means that if the instance is - * too close to the singularities corresponding to the given - * rotation order, it will be impossible to retrieve the angles. For - * Cardan angles, this is often called gimbal lock. There is - * <em>nothing</em> to do to prevent this, it is an intrinsic problem - * with Cardan and Euler representation (but not a problem with the - * rotation itself, which is perfectly well defined). For Cardan - * angles, singularities occur when the second angle is close to - * -π/2 or +π/2, for Euler angle singularities occur when the - * second angle is close to 0 or π, this implies that the identity - * rotation is always singular for Euler angles!</p> - - * @param order rotation order to use - * @return an array of three angles, in the order specified by the set - * @exception CardanEulerSingularityException if the rotation is - * singular with respect to the angles set specified - */ - public double[] getAngles(RotationOrder order) - throws CardanEulerSingularityException { - - if (order == RotationOrder.XYZ) { - - // r (Vector3D.plusK) coordinates are : - // sin (theta), -cos (theta) sin (phi), cos (theta) cos (phi) - // (-r) (Vector3D.plusI) coordinates are : - // cos (psi) cos (theta), -sin (psi) cos (theta), sin (theta) - // and we can choose to have theta in the interval [-PI/2 ; +PI/2] - Vector3D v1 = applyTo(Vector3D.PLUS_K); - Vector3D v2 = applyInverseTo(Vector3D.PLUS_I); - if ((v2.getZ() < -0.9999999999) || (v2.getZ() > 0.9999999999)) { - throw new CardanEulerSingularityException(true); - } - return new double[] { - FastMath.atan2(-(v1.getY()), v1.getZ()), - FastMath.asin(v2.getZ()), - FastMath.atan2(-(v2.getY()), v2.getX()) - }; - - } else if (order == RotationOrder.XZY) { - - // r (Vector3D.plusJ) coordinates are : - // -sin (psi), cos (psi) cos (phi), cos (psi) sin (phi) - // (-r) (Vector3D.plusI) coordinates are : - // cos (theta) cos (psi), -sin (psi), sin (theta) cos (psi) - // and we can choose to have psi in the interval [-PI/2 ; +PI/2] - Vector3D v1 = applyTo(Vector3D.PLUS_J); - Vector3D v2 = applyInverseTo(Vector3D.PLUS_I); - if ((v2.getY() < -0.9999999999) || (v2.getY() > 0.9999999999)) { - throw new CardanEulerSingularityException(true); - } - return new double[] { - FastMath.atan2(v1.getZ(), v1.getY()), - -FastMath.asin(v2.getY()), - FastMath.atan2(v2.getZ(), v2.getX()) - }; - - } else if (order == RotationOrder.YXZ) { - - // r (Vector3D.plusK) coordinates are : - // cos (phi) sin (theta), -sin (phi), cos (phi) cos (theta) - // (-r) (Vector3D.plusJ) coordinates are : - // sin (psi) cos (phi), cos (psi) cos (phi), -sin (phi) - // and we can choose to have phi in the interval [-PI/2 ; +PI/2] - Vector3D v1 = applyTo(Vector3D.PLUS_K); - Vector3D v2 = applyInverseTo(Vector3D.PLUS_J); - if ((v2.getZ() < -0.9999999999) || (v2.getZ() > 0.9999999999)) { - throw new CardanEulerSingularityException(true); - } - return new double[] { - FastMath.atan2(v1.getX(), v1.getZ()), - -FastMath.asin(v2.getZ()), - FastMath.atan2(v2.getX(), v2.getY()) - }; - - } else if (order == RotationOrder.YZX) { - - // r (Vector3D.plusI) coordinates are : - // cos (psi) cos (theta), sin (psi), -cos (psi) sin (theta) - // (-r) (Vector3D.plusJ) coordinates are : - // sin (psi), cos (phi) cos (psi), -sin (phi) cos (psi) - // and we can choose to have psi in the interval [-PI/2 ; +PI/2] - Vector3D v1 = applyTo(Vector3D.PLUS_I); - Vector3D v2 = applyInverseTo(Vector3D.PLUS_J); - if ((v2.getX() < -0.9999999999) || (v2.getX() > 0.9999999999)) { - throw new CardanEulerSingularityException(true); - } - return new double[] { - FastMath.atan2(-(v1.getZ()), v1.getX()), - FastMath.asin(v2.getX()), - FastMath.atan2(-(v2.getZ()), v2.getY()) - }; - - } else if (order == RotationOrder.ZXY) { - - // r (Vector3D.plusJ) coordinates are : - // -cos (phi) sin (psi), cos (phi) cos (psi), sin (phi) - // (-r) (Vector3D.plusK) coordinates are : - // -sin (theta) cos (phi), sin (phi), cos (theta) cos (phi) - // and we can choose to have phi in the interval [-PI/2 ; +PI/2] - Vector3D v1 = applyTo(Vector3D.PLUS_J); - Vector3D v2 = applyInverseTo(Vector3D.PLUS_K); - if ((v2.getY() < -0.9999999999) || (v2.getY() > 0.9999999999)) { - throw new CardanEulerSingularityException(true); - } - return new double[] { - FastMath.atan2(-(v1.getX()), v1.getY()), - FastMath.asin(v2.getY()), - FastMath.atan2(-(v2.getX()), v2.getZ()) - }; - - } else if (order == RotationOrder.ZYX) { - - // r (Vector3D.plusI) coordinates are : - // cos (theta) cos (psi), cos (theta) sin (psi), -sin (theta) - // (-r) (Vector3D.plusK) coordinates are : - // -sin (theta), sin (phi) cos (theta), cos (phi) cos (theta) - // and we can choose to have theta in the interval [-PI/2 ; +PI/2] - Vector3D v1 = applyTo(Vector3D.PLUS_I); - Vector3D v2 = applyInverseTo(Vector3D.PLUS_K); - if ((v2.getX() < -0.9999999999) || (v2.getX() > 0.9999999999)) { - throw new CardanEulerSingularityException(true); - } - return new double[] { - FastMath.atan2(v1.getY(), v1.getX()), - -FastMath.asin(v2.getX()), - FastMath.atan2(v2.getY(), v2.getZ()) - }; - - } else if (order == RotationOrder.XYX) { - - // r (Vector3D.plusI) coordinates are : - // cos (theta), sin (phi1) sin (theta), -cos (phi1) sin (theta) - // (-r) (Vector3D.plusI) coordinates are : - // cos (theta), sin (theta) sin (phi2), sin (theta) cos (phi2) - // and we can choose to have theta in the interval [0 ; PI] - Vector3D v1 = applyTo(Vector3D.PLUS_I); - Vector3D v2 = applyInverseTo(Vector3D.PLUS_I); - if ((v2.getX() < -0.9999999999) || (v2.getX() > 0.9999999999)) { - throw new CardanEulerSingularityException(false); - } - return new double[] { - FastMath.atan2(v1.getY(), -v1.getZ()), - FastMath.acos(v2.getX()), - FastMath.atan2(v2.getY(), v2.getZ()) - }; - - } else if (order == RotationOrder.XZX) { - - // r (Vector3D.plusI) coordinates are : - // cos (psi), cos (phi1) sin (psi), sin (phi1) sin (psi) - // (-r) (Vector3D.plusI) coordinates are : - // cos (psi), -sin (psi) cos (phi2), sin (psi) sin (phi2) - // and we can choose to have psi in the interval [0 ; PI] - Vector3D v1 = applyTo(Vector3D.PLUS_I); - Vector3D v2 = applyInverseTo(Vector3D.PLUS_I); - if ((v2.getX() < -0.9999999999) || (v2.getX() > 0.9999999999)) { - throw new CardanEulerSingularityException(false); - } - return new double[] { - FastMath.atan2(v1.getZ(), v1.getY()), - FastMath.acos(v2.getX()), - FastMath.atan2(v2.getZ(), -v2.getY()) - }; - - } else if (order == RotationOrder.YXY) { - - // r (Vector3D.plusJ) coordinates are : - // sin (theta1) sin (phi), cos (phi), cos (theta1) sin (phi) - // (-r) (Vector3D.plusJ) coordinates are : - // sin (phi) sin (theta2), cos (phi), -sin (phi) cos (theta2) - // and we can choose to have phi in the interval [0 ; PI] - Vector3D v1 = applyTo(Vector3D.PLUS_J); - Vector3D v2 = applyInverseTo(Vector3D.PLUS_J); - if ((v2.getY() < -0.9999999999) || (v2.getY() > 0.9999999999)) { - throw new CardanEulerSingularityException(false); - } - return new double[] { - FastMath.atan2(v1.getX(), v1.getZ()), - FastMath.acos(v2.getY()), - FastMath.atan2(v2.getX(), -v2.getZ()) - }; - - } else if (order == RotationOrder.YZY) { - - // r (Vector3D.plusJ) coordinates are : - // -cos (theta1) sin (psi), cos (psi), sin (theta1) sin (psi) - // (-r) (Vector3D.plusJ) coordinates are : - // sin (psi) cos (theta2), cos (psi), sin (psi) sin (theta2) - // and we can choose to have psi in the interval [0 ; PI] - Vector3D v1 = applyTo(Vector3D.PLUS_J); - Vector3D v2 = applyInverseTo(Vector3D.PLUS_J); - if ((v2.getY() < -0.9999999999) || (v2.getY() > 0.9999999999)) { - throw new CardanEulerSingularityException(false); - } - return new double[] { - FastMath.atan2(v1.getZ(), -v1.getX()), - FastMath.acos(v2.getY()), - FastMath.atan2(v2.getZ(), v2.getX()) - }; - - } else if (order == RotationOrder.ZXZ) { - - // r (Vector3D.plusK) coordinates are : - // sin (psi1) sin (phi), -cos (psi1) sin (phi), cos (phi) - // (-r) (Vector3D.plusK) coordinates are : - // sin (phi) sin (psi2), sin (phi) cos (psi2), cos (phi) - // and we can choose to have phi in the interval [0 ; PI] - Vector3D v1 = applyTo(Vector3D.PLUS_K); - Vector3D v2 = applyInverseTo(Vector3D.PLUS_K); - if ((v2.getZ() < -0.9999999999) || (v2.getZ() > 0.9999999999)) { - throw new CardanEulerSingularityException(false); - } - return new double[] { - FastMath.atan2(v1.getX(), -v1.getY()), - FastMath.acos(v2.getZ()), - FastMath.atan2(v2.getX(), v2.getY()) - }; - - } else { // last possibility is ZYZ - - // r (Vector3D.plusK) coordinates are : - // cos (psi1) sin (theta), sin (psi1) sin (theta), cos (theta) - // (-r) (Vector3D.plusK) coordinates are : - // -sin (theta) cos (psi2), sin (theta) sin (psi2), cos (theta) - // and we can choose to have theta in the interval [0 ; PI] - Vector3D v1 = applyTo(Vector3D.PLUS_K); - Vector3D v2 = applyInverseTo(Vector3D.PLUS_K); - if ((v2.getZ() < -0.9999999999) || (v2.getZ() > 0.9999999999)) { - throw new CardanEulerSingularityException(false); - } - return new double[] { - FastMath.atan2(v1.getY(), v1.getX()), - FastMath.acos(v2.getZ()), - FastMath.atan2(v2.getY(), -v2.getX()) - }; - - } - - } - - /** Get the 3X3 matrix corresponding to the instance - * @return the matrix corresponding to the instance - */ - public double[][] getMatrix() { - - // products - double q0q0 = q0 * q0; - double q0q1 = q0 * q1; - double q0q2 = q0 * q2; - double q0q3 = q0 * q3; - double q1q1 = q1 * q1; - double q1q2 = q1 * q2; - double q1q3 = q1 * q3; - double q2q2 = q2 * q2; - double q2q3 = q2 * q3; - double q3q3 = q3 * q3; - - // create the matrix - double[][] m = new double[3][]; - m[0] = new double[3]; - m[1] = new double[3]; - m[2] = new double[3]; - - m [0][0] = 2.0 * (q0q0 + q1q1) - 1.0; - m [1][0] = 2.0 * (q1q2 - q0q3); - m [2][0] = 2.0 * (q1q3 + q0q2); - - m [0][1] = 2.0 * (q1q2 + q0q3); - m [1][1] = 2.0 * (q0q0 + q2q2) - 1.0; - m [2][1] = 2.0 * (q2q3 - q0q1); - - m [0][2] = 2.0 * (q1q3 - q0q2); - m [1][2] = 2.0 * (q2q3 + q0q1); - m [2][2] = 2.0 * (q0q0 + q3q3) - 1.0; - - return m; - - } - - /** Apply the rotation to a vector. - * @param u vector to apply the rotation to - * @return a new vector which is the image of u by the rotation - */ - public Vector3D applyTo(Vector3D u) { - - double x = u.getX(); - double y = u.getY(); - double z = u.getZ(); - - double s = q1 * x + q2 * y + q3 * z; - - return new Vector3D(2 * (q0 * (x * q0 - (q2 * z - q3 * y)) + s * q1) - x, - 2 * (q0 * (y * q0 - (q3 * x - q1 * z)) + s * q2) - y, - 2 * (q0 * (z * q0 - (q1 * y - q2 * x)) + s * q3) - z); - - } - - /** Apply the rotation to a vector stored in an array. - * @param in an array with three items which stores vector to rotate - * @param out an array with three items to put result to (it can be the same - * array as in) - */ - public void applyTo(final double[] in, final double[] out) { - - final double x = in[0]; - final double y = in[1]; - final double z = in[2]; - - final double s = q1 * x + q2 * y + q3 * z; - - out[0] = 2 * (q0 * (x * q0 - (q2 * z - q3 * y)) + s * q1) - x; - out[1] = 2 * (q0 * (y * q0 - (q3 * x - q1 * z)) + s * q2) - y; - out[2] = 2 * (q0 * (z * q0 - (q1 * y - q2 * x)) + s * q3) - z; - - } - - /** Apply the inverse of the rotation to a vector. - * @param u vector to apply the inverse of the rotation to - * @return a new vector which such that u is its image by the rotation - */ - public Vector3D applyInverseTo(Vector3D u) { - - double x = u.getX(); - double y = u.getY(); - double z = u.getZ(); - - double s = q1 * x + q2 * y + q3 * z; - double m0 = -q0; - - return new Vector3D(2 * (m0 * (x * m0 - (q2 * z - q3 * y)) + s * q1) - x, - 2 * (m0 * (y * m0 - (q3 * x - q1 * z)) + s * q2) - y, - 2 * (m0 * (z * m0 - (q1 * y - q2 * x)) + s * q3) - z); - - } - - /** Apply the inverse of the rotation to a vector stored in an array. - * @param in an array with three items which stores vector to rotate - * @param out an array with three items to put result to (it can be the same - * array as in) - */ - public void applyInverseTo(final double[] in, final double[] out) { - - final double x = in[0]; - final double y = in[1]; - final double z = in[2]; - - final double s = q1 * x + q2 * y + q3 * z; - final double m0 = -q0; - - out[0] = 2 * (m0 * (x * m0 - (q2 * z - q3 * y)) + s * q1) - x; - out[1] = 2 * (m0 * (y * m0 - (q3 * x - q1 * z)) + s * q2) - y; - out[2] = 2 * (m0 * (z * m0 - (q1 * y - q2 * x)) + s * q3) - z; - - } - - /** Apply the instance to another rotation. - * Applying the instance to a rotation is computing the composition - * in an order compliant with the following rule : let u be any - * vector and v its image by r (i.e. r.applyTo(u) = v), let w be the image - * of v by the instance (i.e. applyTo(v) = w), then w = comp.applyTo(u), - * where comp = applyTo(r). - * @param r rotation to apply the rotation to - * @return a new rotation which is the composition of r by the instance - */ - public Rotation applyTo(Rotation r) { - return new Rotation(r.q0 * q0 - (r.q1 * q1 + r.q2 * q2 + r.q3 * q3), - r.q1 * q0 + r.q0 * q1 + (r.q2 * q3 - r.q3 * q2), - r.q2 * q0 + r.q0 * q2 + (r.q3 * q1 - r.q1 * q3), - r.q3 * q0 + r.q0 * q3 + (r.q1 * q2 - r.q2 * q1), - false); - } - - /** Apply the inverse of the instance to another rotation. - * Applying the inverse of the instance to a rotation is computing - * the composition in an order compliant with the following rule : - * let u be any vector and v its image by r (i.e. r.applyTo(u) = v), - * let w be the inverse image of v by the instance - * (i.e. applyInverseTo(v) = w), then w = comp.applyTo(u), where - * comp = applyInverseTo(r). - * @param r rotation to apply the rotation to - * @return a new rotation which is the composition of r by the inverse - * of the instance - */ - public Rotation applyInverseTo(Rotation r) { - return new Rotation(-r.q0 * q0 - (r.q1 * q1 + r.q2 * q2 + r.q3 * q3), - -r.q1 * q0 + r.q0 * q1 + (r.q2 * q3 - r.q3 * q2), - -r.q2 * q0 + r.q0 * q2 + (r.q3 * q1 - r.q1 * q3), - -r.q3 * q0 + r.q0 * q3 + (r.q1 * q2 - r.q2 * q1), - false); - } - - /** Perfect orthogonality on a 3X3 matrix. - * @param m initial matrix (not exactly orthogonal) - * @param threshold convergence threshold for the iterative - * orthogonality correction (convergence is reached when the - * difference between two steps of the Frobenius norm of the - * correction is below this threshold) - * @return an orthogonal matrix close to m - * @exception NotARotationMatrixException if the matrix cannot be - * orthogonalized with the given threshold after 10 iterations - */ - private double[][] orthogonalizeMatrix(double[][] m, double threshold) - throws NotARotationMatrixException { - double[] m0 = m[0]; - double[] m1 = m[1]; - double[] m2 = m[2]; - double x00 = m0[0]; - double x01 = m0[1]; - double x02 = m0[2]; - double x10 = m1[0]; - double x11 = m1[1]; - double x12 = m1[2]; - double x20 = m2[0]; - double x21 = m2[1]; - double x22 = m2[2]; - double fn = 0; - double fn1; - - double[][] o = new double[3][3]; - double[] o0 = o[0]; - double[] o1 = o[1]; - double[] o2 = o[2]; - - // iterative correction: Xn+1 = Xn - 0.5 * (Xn.Mt.Xn - M) - int i = 0; - while (++i < 11) { - - // Mt.Xn - double mx00 = m0[0] * x00 + m1[0] * x10 + m2[0] * x20; - double mx10 = m0[1] * x00 + m1[1] * x10 + m2[1] * x20; - double mx20 = m0[2] * x00 + m1[2] * x10 + m2[2] * x20; - double mx01 = m0[0] * x01 + m1[0] * x11 + m2[0] * x21; - double mx11 = m0[1] * x01 + m1[1] * x11 + m2[1] * x21; - double mx21 = m0[2] * x01 + m1[2] * x11 + m2[2] * x21; - double mx02 = m0[0] * x02 + m1[0] * x12 + m2[0] * x22; - double mx12 = m0[1] * x02 + m1[1] * x12 + m2[1] * x22; - double mx22 = m0[2] * x02 + m1[2] * x12 + m2[2] * x22; - - // Xn+1 - o0[0] = x00 - 0.5 * (x00 * mx00 + x01 * mx10 + x02 * mx20 - m0[0]); - o0[1] = x01 - 0.5 * (x00 * mx01 + x01 * mx11 + x02 * mx21 - m0[1]); - o0[2] = x02 - 0.5 * (x00 * mx02 + x01 * mx12 + x02 * mx22 - m0[2]); - o1[0] = x10 - 0.5 * (x10 * mx00 + x11 * mx10 + x12 * mx20 - m1[0]); - o1[1] = x11 - 0.5 * (x10 * mx01 + x11 * mx11 + x12 * mx21 - m1[1]); - o1[2] = x12 - 0.5 * (x10 * mx02 + x11 * mx12 + x12 * mx22 - m1[2]); - o2[0] = x20 - 0.5 * (x20 * mx00 + x21 * mx10 + x22 * mx20 - m2[0]); - o2[1] = x21 - 0.5 * (x20 * mx01 + x21 * mx11 + x22 * mx21 - m2[1]); - o2[2] = x22 - 0.5 * (x20 * mx02 + x21 * mx12 + x22 * mx22 - m2[2]); - - // correction on each elements - double corr00 = o0[0] - m0[0]; - double corr01 = o0[1] - m0[1]; - double corr02 = o0[2] - m0[2]; - double corr10 = o1[0] - m1[0]; - double corr11 = o1[1] - m1[1]; - double corr12 = o1[2] - m1[2]; - double corr20 = o2[0] - m2[0]; - double corr21 = o2[1] - m2[1]; - double corr22 = o2[2] - m2[2]; - - // Frobenius norm of the correction - fn1 = corr00 * corr00 + corr01 * corr01 + corr02 * corr02 + - corr10 * corr10 + corr11 * corr11 + corr12 * corr12 + - corr20 * corr20 + corr21 * corr21 + corr22 * corr22; - - // convergence test - if (FastMath.abs(fn1 - fn) <= threshold) { - return o; - } - - // prepare next iteration - x00 = o0[0]; - x01 = o0[1]; - x02 = o0[2]; - x10 = o1[0]; - x11 = o1[1]; - x12 = o1[2]; - x20 = o2[0]; - x21 = o2[1]; - x22 = o2[2]; - fn = fn1; - - } - - // the algorithm did not converge after 10 iterations - throw new NotARotationMatrixException( - LocalizedFormats.UNABLE_TO_ORTHOGONOLIZE_MATRIX, - i - 1); - } - - /** Compute the <i>distance</i> between two rotations. - * <p>The <i>distance</i> is intended here as a way to check if two - * rotations are almost similar (i.e. they transform vectors the same way) - * or very different. It is mathematically defined as the angle of - * the rotation r that prepended to one of the rotations gives the other - * one:</p> - * <pre> - * r<sub>1</sub>(r) = r<sub>2</sub> - * </pre> - * <p>This distance is an angle between 0 and π. Its value is the smallest - * possible upper bound of the angle in radians between r<sub>1</sub>(v) - * and r<sub>2</sub>(v) for all possible vectors v. This upper bound is - * reached for some v. The distance is equal to 0 if and only if the two - * rotations are identical.</p> - * <p>Comparing two rotations should always be done using this value rather - * than for example comparing the components of the quaternions. It is much - * more stable, and has a geometric meaning. Also comparing quaternions - * components is error prone since for example quaternions (0.36, 0.48, -0.48, -0.64) - * and (-0.36, -0.48, 0.48, 0.64) represent exactly the same rotation despite - * their components are different (they are exact opposites).</p> - * @param r1 first rotation - * @param r2 second rotation - * @return <i>distance</i> between r1 and r2 - */ - public static double distance(Rotation r1, Rotation r2) { - return r1.applyInverseTo(r2).getAngle(); - } - -}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/a7b4803f/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/RotationOrder.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/RotationOrder.java b/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/RotationOrder.java deleted file mode 100644 index 03bc1c2..0000000 --- a/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/RotationOrder.java +++ /dev/null @@ -1,174 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.commons.math3.geometry.euclidean.threed; - -/** - * This class is a utility representing a rotation order specification - * for Cardan or Euler angles specification. - * - * This class cannot be instanciated by the user. He can only use one - * of the twelve predefined supported orders as an argument to either - * the {@link Rotation#Rotation(RotationOrder,double,double,double)} - * constructor or the {@link Rotation#getAngles} method. - * - * @since 1.2 - */ -public final class RotationOrder { - - /** Set of Cardan angles. - * this ordered set of rotations is around X, then around Y, then - * around Z - */ - public static final RotationOrder XYZ = - new RotationOrder("XYZ", Vector3D.PLUS_I, Vector3D.PLUS_J, Vector3D.PLUS_K); - - /** Set of Cardan angles. - * this ordered set of rotations is around X, then around Z, then - * around Y - */ - public static final RotationOrder XZY = - new RotationOrder("XZY", Vector3D.PLUS_I, Vector3D.PLUS_K, Vector3D.PLUS_J); - - /** Set of Cardan angles. - * this ordered set of rotations is around Y, then around X, then - * around Z - */ - public static final RotationOrder YXZ = - new RotationOrder("YXZ", Vector3D.PLUS_J, Vector3D.PLUS_I, Vector3D.PLUS_K); - - /** Set of Cardan angles. - * this ordered set of rotations is around Y, then around Z, then - * around X - */ - public static final RotationOrder YZX = - new RotationOrder("YZX", Vector3D.PLUS_J, Vector3D.PLUS_K, Vector3D.PLUS_I); - - /** Set of Cardan angles. - * this ordered set of rotations is around Z, then around X, then - * around Y - */ - public static final RotationOrder ZXY = - new RotationOrder("ZXY", Vector3D.PLUS_K, Vector3D.PLUS_I, Vector3D.PLUS_J); - - /** Set of Cardan angles. - * this ordered set of rotations is around Z, then around Y, then - * around X - */ - public static final RotationOrder ZYX = - new RotationOrder("ZYX", Vector3D.PLUS_K, Vector3D.PLUS_J, Vector3D.PLUS_I); - - /** Set of Euler angles. - * this ordered set of rotations is around X, then around Y, then - * around X - */ - public static final RotationOrder XYX = - new RotationOrder("XYX", Vector3D.PLUS_I, Vector3D.PLUS_J, Vector3D.PLUS_I); - - /** Set of Euler angles. - * this ordered set of rotations is around X, then around Z, then - * around X - */ - public static final RotationOrder XZX = - new RotationOrder("XZX", Vector3D.PLUS_I, Vector3D.PLUS_K, Vector3D.PLUS_I); - - /** Set of Euler angles. - * this ordered set of rotations is around Y, then around X, then - * around Y - */ - public static final RotationOrder YXY = - new RotationOrder("YXY", Vector3D.PLUS_J, Vector3D.PLUS_I, Vector3D.PLUS_J); - - /** Set of Euler angles. - * this ordered set of rotations is around Y, then around Z, then - * around Y - */ - public static final RotationOrder YZY = - new RotationOrder("YZY", Vector3D.PLUS_J, Vector3D.PLUS_K, Vector3D.PLUS_J); - - /** Set of Euler angles. - * this ordered set of rotations is around Z, then around X, then - * around Z - */ - public static final RotationOrder ZXZ = - new RotationOrder("ZXZ", Vector3D.PLUS_K, Vector3D.PLUS_I, Vector3D.PLUS_K); - - /** Set of Euler angles. - * this ordered set of rotations is around Z, then around Y, then - * around Z - */ - public static final RotationOrder ZYZ = - new RotationOrder("ZYZ", Vector3D.PLUS_K, Vector3D.PLUS_J, Vector3D.PLUS_K); - - /** Name of the rotations order. */ - private final String name; - - /** Axis of the first rotation. */ - private final Vector3D a1; - - /** Axis of the second rotation. */ - private final Vector3D a2; - - /** Axis of the third rotation. */ - private final Vector3D a3; - - /** Private constructor. - * This is a utility class that cannot be instantiated by the user, - * so its only constructor is private. - * @param name name of the rotation order - * @param a1 axis of the first rotation - * @param a2 axis of the second rotation - * @param a3 axis of the third rotation - */ - private RotationOrder(final String name, - final Vector3D a1, final Vector3D a2, final Vector3D a3) { - this.name = name; - this.a1 = a1; - this.a2 = a2; - this.a3 = a3; - } - - /** Get a string representation of the instance. - * @return a string representation of the instance (in fact, its name) - */ - @Override - public String toString() { - return name; - } - - /** Get the axis of the first rotation. - * @return axis of the first rotation - */ - public Vector3D getA1() { - return a1; - } - - /** Get the axis of the second rotation. - * @return axis of the second rotation - */ - public Vector3D getA2() { - return a2; - } - - /** Get the axis of the second rotation. - * @return axis of the second rotation - */ - public Vector3D getA3() { - return a3; - } - -} http://git-wip-us.apache.org/repos/asf/commons-math/blob/a7b4803f/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/Segment.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/Segment.java b/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/Segment.java deleted file mode 100644 index 200b462..0000000 --- a/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/Segment.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.math3.geometry.euclidean.threed; - - -/** Simple container for a two-points segment. - * @since 3.0 - */ -public class Segment { - - /** Start point of the segment. */ - private final Vector3D start; - - /** End point of the segments. */ - private final Vector3D end; - - /** Line containing the segment. */ - private final Line line; - - /** 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 Vector3D start, final Vector3D end, final Line line) { - this.start = start; - this.end = end; - this.line = line; - } - - /** Get the start point of the segment. - * @return start point of the segment - */ - public Vector3D getStart() { - return start; - } - - /** Get the end point of the segment. - * @return end point of the segment - */ - public Vector3D getEnd() { - return end; - } - - /** Get the line containing the segment. - * @return line containing the segment - */ - public Line getLine() { - return line; - } - -} http://git-wip-us.apache.org/repos/asf/commons-math/blob/a7b4803f/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/SphereGenerator.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/SphereGenerator.java b/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/SphereGenerator.java deleted file mode 100644 index b553510..0000000 --- a/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/SphereGenerator.java +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.math3.geometry.euclidean.threed; - -import java.util.Arrays; -import java.util.List; - -import org.apache.commons.math3.fraction.BigFraction; -import org.apache.commons.math3.geometry.enclosing.EnclosingBall; -import org.apache.commons.math3.geometry.enclosing.SupportBallGenerator; -import org.apache.commons.math3.geometry.euclidean.twod.DiskGenerator; -import org.apache.commons.math3.geometry.euclidean.twod.Euclidean2D; -import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; -import org.apache.commons.math3.util.FastMath; - -/** Class generating an enclosing ball from its support points. - * @since 3.3 - */ -public class SphereGenerator implements SupportBallGenerator<Euclidean3D, Vector3D> { - - /** {@inheritDoc} */ - public EnclosingBall<Euclidean3D, Vector3D> ballOnSupport(final List<Vector3D> support) { - - if (support.size() < 1) { - return new EnclosingBall<Euclidean3D, Vector3D>(Vector3D.ZERO, Double.NEGATIVE_INFINITY); - } else { - final Vector3D vA = support.get(0); - if (support.size() < 2) { - return new EnclosingBall<Euclidean3D, Vector3D>(vA, 0, vA); - } else { - final Vector3D vB = support.get(1); - if (support.size() < 3) { - return new EnclosingBall<Euclidean3D, Vector3D>(new Vector3D(0.5, vA, 0.5, vB), - 0.5 * vA.distance(vB), - vA, vB); - } else { - final Vector3D vC = support.get(2); - if (support.size() < 4) { - - // delegate to 2D disk generator - final Plane p = new Plane(vA, vB, vC, - 1.0e-10 * (vA.getNorm1() + vB.getNorm1() + vC.getNorm1())); - final EnclosingBall<Euclidean2D, Vector2D> disk = - new DiskGenerator().ballOnSupport(Arrays.asList(p.toSubSpace(vA), - p.toSubSpace(vB), - p.toSubSpace(vC))); - - // convert back to 3D - return new EnclosingBall<Euclidean3D, Vector3D>(p.toSpace(disk.getCenter()), - disk.getRadius(), vA, vB, vC); - - } else { - final Vector3D vD = support.get(3); - // a sphere is 3D can be defined as: - // (1) (x - x_0)^2 + (y - y_0)^2 + (z - z_0)^2 = r^2 - // which can be written: - // (2) (x^2 + y^2 + z^2) - 2 x_0 x - 2 y_0 y - 2 z_0 z + (x_0^2 + y_0^2 + z_0^2 - r^2) = 0 - // or simply: - // (3) (x^2 + y^2 + z^2) + a x + b y + c z + d = 0 - // with sphere center coordinates -a/2, -b/2, -c/2 - // If the sphere exists, a b, c and d are a non zero solution to - // [ (x^2 + y^2 + z^2) x y z 1 ] [ 1 ] [ 0 ] - // [ (xA^2 + yA^2 + zA^2) xA yA zA 1 ] [ a ] [ 0 ] - // [ (xB^2 + yB^2 + zB^2) xB yB zB 1 ] * [ b ] = [ 0 ] - // [ (xC^2 + yC^2 + zC^2) xC yC zC 1 ] [ c ] [ 0 ] - // [ (xD^2 + yD^2 + zD^2) xD yD zD 1 ] [ d ] [ 0 ] - // So the determinant of the matrix is zero. Computing this determinant - // by expanding it using the minors m_ij of first row leads to - // (4) m_11 (x^2 + y^2 + z^2) - m_12 x + m_13 y - m_14 z + m_15 = 0 - // So by identifying equations (2) and (4) we get the coordinates - // of center as: - // x_0 = +m_12 / (2 m_11) - // y_0 = -m_13 / (2 m_11) - // z_0 = +m_14 / (2 m_11) - // Note that the minors m_11, m_12, m_13 and m_14 all have the last column - // filled with 1.0, hence simplifying the computation - final BigFraction[] c2 = new BigFraction[] { - new BigFraction(vA.getX()), new BigFraction(vB.getX()), - new BigFraction(vC.getX()), new BigFraction(vD.getX()) - }; - final BigFraction[] c3 = new BigFraction[] { - new BigFraction(vA.getY()), new BigFraction(vB.getY()), - new BigFraction(vC.getY()), new BigFraction(vD.getY()) - }; - final BigFraction[] c4 = new BigFraction[] { - new BigFraction(vA.getZ()), new BigFraction(vB.getZ()), - new BigFraction(vC.getZ()), new BigFraction(vD.getZ()) - }; - final BigFraction[] c1 = new BigFraction[] { - c2[0].multiply(c2[0]).add(c3[0].multiply(c3[0])).add(c4[0].multiply(c4[0])), - c2[1].multiply(c2[1]).add(c3[1].multiply(c3[1])).add(c4[1].multiply(c4[1])), - c2[2].multiply(c2[2]).add(c3[2].multiply(c3[2])).add(c4[2].multiply(c4[2])), - c2[3].multiply(c2[3]).add(c3[3].multiply(c3[3])).add(c4[3].multiply(c4[3])) - }; - final BigFraction twoM11 = minor(c2, c3, c4).multiply(2); - final BigFraction m12 = minor(c1, c3, c4); - final BigFraction m13 = minor(c1, c2, c4); - final BigFraction m14 = minor(c1, c2, c3); - final BigFraction centerX = m12.divide(twoM11); - final BigFraction centerY = m13.divide(twoM11).negate(); - final BigFraction centerZ = m14.divide(twoM11); - final BigFraction dx = c2[0].subtract(centerX); - final BigFraction dy = c3[0].subtract(centerY); - final BigFraction dz = c4[0].subtract(centerZ); - final BigFraction r2 = dx.multiply(dx).add(dy.multiply(dy)).add(dz.multiply(dz)); - return new EnclosingBall<Euclidean3D, Vector3D>(new Vector3D(centerX.doubleValue(), - centerY.doubleValue(), - centerZ.doubleValue()), - FastMath.sqrt(r2.doubleValue()), - vA, vB, vC, vD); - } - } - } - } - } - - /** Compute a dimension 4 minor, when 4<sup>th</sup> column is known to be filled with 1.0. - * @param c1 first column - * @param c2 second column - * @param c3 third column - * @return value of the minor computed has an exact fraction - */ - private BigFraction minor(final BigFraction[] c1, final BigFraction[] c2, final BigFraction[] c3) { - return c2[0].multiply(c3[1]).multiply(c1[2].subtract(c1[3])). - add(c2[0].multiply(c3[2]).multiply(c1[3].subtract(c1[1]))). - add(c2[0].multiply(c3[3]).multiply(c1[1].subtract(c1[2]))). - add(c2[1].multiply(c3[0]).multiply(c1[3].subtract(c1[2]))). - add(c2[1].multiply(c3[2]).multiply(c1[0].subtract(c1[3]))). - add(c2[1].multiply(c3[3]).multiply(c1[2].subtract(c1[0]))). - add(c2[2].multiply(c3[0]).multiply(c1[1].subtract(c1[3]))). - add(c2[2].multiply(c3[1]).multiply(c1[3].subtract(c1[0]))). - add(c2[2].multiply(c3[3]).multiply(c1[0].subtract(c1[1]))). - add(c2[3].multiply(c3[0]).multiply(c1[2].subtract(c1[1]))). - add(c2[3].multiply(c3[1]).multiply(c1[0].subtract(c1[2]))). - add(c2[3].multiply(c3[2]).multiply(c1[1].subtract(c1[0]))); - } - -} http://git-wip-us.apache.org/repos/asf/commons-math/blob/a7b4803f/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/SphericalCoordinates.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/SphericalCoordinates.java b/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/SphericalCoordinates.java deleted file mode 100644 index 23d818e..0000000 --- a/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/SphericalCoordinates.java +++ /dev/null @@ -1,395 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.math3.geometry.euclidean.threed; - - -import java.io.Serializable; - -import org.apache.commons.math3.util.FastMath; - -/** This class provides conversions related to <a - * href="http://mathworld.wolfram.com/SphericalCoordinates.html">spherical coordinates</a>. - * <p> - * The conventions used here are the mathematical ones, i.e. spherical coordinates are - * related to Cartesian coordinates as follows: - * </p> - * <ul> - * <li>x = r cos(θ) sin(Φ)</li> - * <li>y = r sin(θ) sin(Φ)</li> - * <li>z = r cos(Φ)</li> - * </ul> - * <ul> - * <li>r = √(x<sup>2</sup>+y<sup>2</sup>+z<sup>2</sup>)</li> - * <li>θ = atan2(y, x)</li> - * <li>Φ = acos(z/r)</li> - * </ul> - * <p> - * r is the radius, θ is the azimuthal angle in the x-y plane and Φ is the polar - * (co-latitude) angle. These conventions are <em>different</em> from the conventions used - * in physics (and in particular in spherical harmonics) where the meanings of θ and - * Φ are reversed. - * </p> - * <p> - * This class provides conversion of coordinates and also of gradient and Hessian - * between spherical and Cartesian coordinates. - * </p> - * @since 3.2 - */ -public class SphericalCoordinates implements Serializable { - - /** Serializable UID. */ - private static final long serialVersionUID = 20130206L; - - /** Cartesian coordinates. */ - private final Vector3D v; - - /** Radius. */ - private final double r; - - /** Azimuthal angle in the x-y plane θ. */ - private final double theta; - - /** Polar angle (co-latitude) Φ. */ - private final double phi; - - /** Jacobian of (r, θ &Phi). */ - private double[][] jacobian; - - /** Hessian of radius. */ - private double[][] rHessian; - - /** Hessian of azimuthal angle in the x-y plane θ. */ - private double[][] thetaHessian; - - /** Hessian of polar (co-latitude) angle Φ. */ - private double[][] phiHessian; - - /** Build a spherical coordinates transformer from Cartesian coordinates. - * @param v Cartesian coordinates - */ - public SphericalCoordinates(final Vector3D v) { - - // Cartesian coordinates - this.v = v; - - // remaining spherical coordinates - this.r = v.getNorm(); - this.theta = v.getAlpha(); - this.phi = FastMath.acos(v.getZ() / r); - - } - - /** Build a spherical coordinates transformer from spherical coordinates. - * @param r radius - * @param theta azimuthal angle in x-y plane - * @param phi polar (co-latitude) angle - */ - public SphericalCoordinates(final double r, final double theta, final double phi) { - - final double cosTheta = FastMath.cos(theta); - final double sinTheta = FastMath.sin(theta); - final double cosPhi = FastMath.cos(phi); - final double sinPhi = FastMath.sin(phi); - - // spherical coordinates - this.r = r; - this.theta = theta; - this.phi = phi; - - // Cartesian coordinates - this.v = new Vector3D(r * cosTheta * sinPhi, - r * sinTheta * sinPhi, - r * cosPhi); - - } - - /** Get the Cartesian coordinates. - * @return Cartesian coordinates - */ - public Vector3D getCartesian() { - return v; - } - - /** Get the radius. - * @return radius r - * @see #getTheta() - * @see #getPhi() - */ - public double getR() { - return r; - } - - /** Get the azimuthal angle in x-y plane. - * @return azimuthal angle in x-y plane θ - * @see #getR() - * @see #getPhi() - */ - public double getTheta() { - return theta; - } - - /** Get the polar (co-latitude) angle. - * @return polar (co-latitude) angle Φ - * @see #getR() - * @see #getTheta() - */ - public double getPhi() { - return phi; - } - - /** Convert a gradient with respect to spherical coordinates into a gradient - * with respect to Cartesian coordinates. - * @param sGradient gradient with respect to spherical coordinates - * {df/dr, df/dθ, df/dΦ} - * @return gradient with respect to Cartesian coordinates - * {df/dx, df/dy, df/dz} - */ - public double[] toCartesianGradient(final double[] sGradient) { - - // lazy evaluation of Jacobian - computeJacobian(); - - // compose derivatives as gradient^T . J - // the expressions have been simplified since we know jacobian[1][2] = dTheta/dZ = 0 - return new double[] { - sGradient[0] * jacobian[0][0] + sGradient[1] * jacobian[1][0] + sGradient[2] * jacobian[2][0], - sGradient[0] * jacobian[0][1] + sGradient[1] * jacobian[1][1] + sGradient[2] * jacobian[2][1], - sGradient[0] * jacobian[0][2] + sGradient[2] * jacobian[2][2] - }; - - } - - /** Convert a Hessian with respect to spherical coordinates into a Hessian - * with respect to Cartesian coordinates. - * <p> - * As Hessian are always symmetric, we use only the lower left part of the provided - * spherical Hessian, so the upper part may not be initialized. However, we still - * do fill up the complete array we create, with guaranteed symmetry. - * </p> - * @param sHessian Hessian with respect to spherical coordinates - * {{d<sup>2</sup>f/dr<sup>2</sup>, d<sup>2</sup>f/drdθ, d<sup>2</sup>f/drdΦ}, - * {d<sup>2</sup>f/drdθ, d<sup>2</sup>f/dθ<sup>2</sup>, d<sup>2</sup>f/dθdΦ}, - * {d<sup>2</sup>f/drdΦ, d<sup>2</sup>f/dθdΦ, d<sup>2</sup>f/dΦ<sup>2</sup>} - * @param sGradient gradient with respect to spherical coordinates - * {df/dr, df/dθ, df/dΦ} - * @return Hessian with respect to Cartesian coordinates - * {{d<sup>2</sup>f/dx<sup>2</sup>, d<sup>2</sup>f/dxdy, d<sup>2</sup>f/dxdz}, - * {d<sup>2</sup>f/dxdy, d<sup>2</sup>f/dy<sup>2</sup>, d<sup>2</sup>f/dydz}, - * {d<sup>2</sup>f/dxdz, d<sup>2</sup>f/dydz, d<sup>2</sup>f/dz<sup>2</sup>}} - */ - public double[][] toCartesianHessian(final double[][] sHessian, final double[] sGradient) { - - computeJacobian(); - computeHessians(); - - // compose derivative as J^T . H_f . J + df/dr H_r + df/dtheta H_theta + df/dphi H_phi - // the expressions have been simplified since we know jacobian[1][2] = dTheta/dZ = 0 - // and H_theta is only a 2x2 matrix as it does not depend on z - final double[][] hj = new double[3][3]; - final double[][] cHessian = new double[3][3]; - - // compute H_f . J - // beware we use ONLY the lower-left part of sHessian - hj[0][0] = sHessian[0][0] * jacobian[0][0] + sHessian[1][0] * jacobian[1][0] + sHessian[2][0] * jacobian[2][0]; - hj[0][1] = sHessian[0][0] * jacobian[0][1] + sHessian[1][0] * jacobian[1][1] + sHessian[2][0] * jacobian[2][1]; - hj[0][2] = sHessian[0][0] * jacobian[0][2] + sHessian[2][0] * jacobian[2][2]; - hj[1][0] = sHessian[1][0] * jacobian[0][0] + sHessian[1][1] * jacobian[1][0] + sHessian[2][1] * jacobian[2][0]; - hj[1][1] = sHessian[1][0] * jacobian[0][1] + sHessian[1][1] * jacobian[1][1] + sHessian[2][1] * jacobian[2][1]; - // don't compute hj[1][2] as it is not used below - hj[2][0] = sHessian[2][0] * jacobian[0][0] + sHessian[2][1] * jacobian[1][0] + sHessian[2][2] * jacobian[2][0]; - hj[2][1] = sHessian[2][0] * jacobian[0][1] + sHessian[2][1] * jacobian[1][1] + sHessian[2][2] * jacobian[2][1]; - hj[2][2] = sHessian[2][0] * jacobian[0][2] + sHessian[2][2] * jacobian[2][2]; - - // compute lower-left part of J^T . H_f . J - cHessian[0][0] = jacobian[0][0] * hj[0][0] + jacobian[1][0] * hj[1][0] + jacobian[2][0] * hj[2][0]; - cHessian[1][0] = jacobian[0][1] * hj[0][0] + jacobian[1][1] * hj[1][0] + jacobian[2][1] * hj[2][0]; - cHessian[2][0] = jacobian[0][2] * hj[0][0] + jacobian[2][2] * hj[2][0]; - cHessian[1][1] = jacobian[0][1] * hj[0][1] + jacobian[1][1] * hj[1][1] + jacobian[2][1] * hj[2][1]; - cHessian[2][1] = jacobian[0][2] * hj[0][1] + jacobian[2][2] * hj[2][1]; - cHessian[2][2] = jacobian[0][2] * hj[0][2] + jacobian[2][2] * hj[2][2]; - - // add gradient contribution - cHessian[0][0] += sGradient[0] * rHessian[0][0] + sGradient[1] * thetaHessian[0][0] + sGradient[2] * phiHessian[0][0]; - cHessian[1][0] += sGradient[0] * rHessian[1][0] + sGradient[1] * thetaHessian[1][0] + sGradient[2] * phiHessian[1][0]; - cHessian[2][0] += sGradient[0] * rHessian[2][0] + sGradient[2] * phiHessian[2][0]; - cHessian[1][1] += sGradient[0] * rHessian[1][1] + sGradient[1] * thetaHessian[1][1] + sGradient[2] * phiHessian[1][1]; - cHessian[2][1] += sGradient[0] * rHessian[2][1] + sGradient[2] * phiHessian[2][1]; - cHessian[2][2] += sGradient[0] * rHessian[2][2] + sGradient[2] * phiHessian[2][2]; - - // ensure symmetry - cHessian[0][1] = cHessian[1][0]; - cHessian[0][2] = cHessian[2][0]; - cHessian[1][2] = cHessian[2][1]; - - return cHessian; - - } - - /** Lazy evaluation of (r, θ, φ) Jacobian. - */ - private void computeJacobian() { - if (jacobian == null) { - - // intermediate variables - final double x = v.getX(); - final double y = v.getY(); - final double z = v.getZ(); - final double rho2 = x * x + y * y; - final double rho = FastMath.sqrt(rho2); - final double r2 = rho2 + z * z; - - jacobian = new double[3][3]; - - // row representing the gradient of r - jacobian[0][0] = x / r; - jacobian[0][1] = y / r; - jacobian[0][2] = z / r; - - // row representing the gradient of theta - jacobian[1][0] = -y / rho2; - jacobian[1][1] = x / rho2; - // jacobian[1][2] is already set to 0 at allocation time - - // row representing the gradient of phi - jacobian[2][0] = x * z / (rho * r2); - jacobian[2][1] = y * z / (rho * r2); - jacobian[2][2] = -rho / r2; - - } - } - - /** Lazy evaluation of Hessians. - */ - private void computeHessians() { - - if (rHessian == null) { - - // intermediate variables - final double x = v.getX(); - final double y = v.getY(); - final double z = v.getZ(); - final double x2 = x * x; - final double y2 = y * y; - final double z2 = z * z; - final double rho2 = x2 + y2; - final double rho = FastMath.sqrt(rho2); - final double r2 = rho2 + z2; - final double xOr = x / r; - final double yOr = y / r; - final double zOr = z / r; - final double xOrho2 = x / rho2; - final double yOrho2 = y / rho2; - final double xOr3 = xOr / r2; - final double yOr3 = yOr / r2; - final double zOr3 = zOr / r2; - - // lower-left part of Hessian of r - rHessian = new double[3][3]; - rHessian[0][0] = y * yOr3 + z * zOr3; - rHessian[1][0] = -x * yOr3; - rHessian[2][0] = -z * xOr3; - rHessian[1][1] = x * xOr3 + z * zOr3; - rHessian[2][1] = -y * zOr3; - rHessian[2][2] = x * xOr3 + y * yOr3; - - // upper-right part is symmetric - rHessian[0][1] = rHessian[1][0]; - rHessian[0][2] = rHessian[2][0]; - rHessian[1][2] = rHessian[2][1]; - - // lower-left part of Hessian of azimuthal angle theta - thetaHessian = new double[2][2]; - thetaHessian[0][0] = 2 * xOrho2 * yOrho2; - thetaHessian[1][0] = yOrho2 * yOrho2 - xOrho2 * xOrho2; - thetaHessian[1][1] = -2 * xOrho2 * yOrho2; - - // upper-right part is symmetric - thetaHessian[0][1] = thetaHessian[1][0]; - - // lower-left part of Hessian of polar (co-latitude) angle phi - final double rhor2 = rho * r2; - final double rho2r2 = rho * rhor2; - final double rhor4 = rhor2 * r2; - final double rho3r4 = rhor4 * rho2; - final double r2P2rho2 = 3 * rho2 + z2; - phiHessian = new double[3][3]; - phiHessian[0][0] = z * (rho2r2 - x2 * r2P2rho2) / rho3r4; - phiHessian[1][0] = -x * y * z * r2P2rho2 / rho3r4; - phiHessian[2][0] = x * (rho2 - z2) / rhor4; - phiHessian[1][1] = z * (rho2r2 - y2 * r2P2rho2) / rho3r4; - phiHessian[2][1] = y * (rho2 - z2) / rhor4; - phiHessian[2][2] = 2 * rho * zOr3 / r; - - // upper-right part is symmetric - phiHessian[0][1] = phiHessian[1][0]; - phiHessian[0][2] = phiHessian[2][0]; - phiHessian[1][2] = phiHessian[2][1]; - - } - - } - - /** - * Replace the instance with a data transfer object for serialization. - * @return data transfer object that will be serialized - */ - private Object writeReplace() { - return new DataTransferObject(v.getX(), v.getY(), v.getZ()); - } - - /** Internal class used only for serialization. */ - private static class DataTransferObject implements Serializable { - - /** Serializable UID. */ - private static final long serialVersionUID = 20130206L; - - /** Abscissa. - * @serial - */ - private final double x; - - /** Ordinate. - * @serial - */ - private final double y; - - /** Height. - * @serial - */ - private final double z; - - /** Simple constructor. - * @param x abscissa - * @param y ordinate - * @param z height - */ - public DataTransferObject(final double x, final double y, final double z) { - this.x = x; - this.y = y; - this.z = z; - } - - /** Replace the deserialized data transfer object with a {@link SphericalCoordinates}. - * @return replacement {@link SphericalCoordinates} - */ - private Object readResolve() { - return new SphericalCoordinates(new Vector3D(x, y, z)); - } - - } - -} http://git-wip-us.apache.org/repos/asf/commons-math/blob/a7b4803f/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/SubLine.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/SubLine.java b/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/SubLine.java deleted file mode 100644 index 2ac917f..0000000 --- a/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/SubLine.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.math3.geometry.euclidean.threed; - -import java.util.ArrayList; -import java.util.List; - -import org.apache.commons.math3.exception.MathIllegalArgumentException; -import org.apache.commons.math3.geometry.Point; -import org.apache.commons.math3.geometry.euclidean.oned.Euclidean1D; -import org.apache.commons.math3.geometry.euclidean.oned.Interval; -import org.apache.commons.math3.geometry.euclidean.oned.IntervalsSet; -import org.apache.commons.math3.geometry.euclidean.oned.Vector1D; -import org.apache.commons.math3.geometry.partitioning.Region.Location; - -/** This class represents a subset of a {@link Line}. - * @since 3.0 - */ -public class SubLine { - - /** Default value for tolerance. */ - private static final double DEFAULT_TOLERANCE = 1.0e-10; - - /** Underlying line. */ - private final Line line; - - /** Remaining region of the hyperplane. */ - private final IntervalsSet remainingRegion; - - /** Simple constructor. - * @param line underlying line - * @param remainingRegion remaining region of the line - */ - public SubLine(final Line line, final IntervalsSet remainingRegion) { - this.line = line; - this.remainingRegion = remainingRegion; - } - - /** Create a sub-line from two endpoints. - * @param start start point - * @param end end point - * @param tolerance tolerance below which points are considered identical - * @exception MathIllegalArgumentException if the points are equal - * @since 3.3 - */ - public SubLine(final Vector3D start, final Vector3D end, final double tolerance) - throws MathIllegalArgumentException { - this(new Line(start, end, tolerance), buildIntervalSet(start, end, tolerance)); - } - - /** Create a sub-line from two endpoints. - * @param start start point - * @param end end point - * @exception MathIllegalArgumentException if the points are equal - * @deprecated as of 3.3, replaced with {@link #SubLine(Vector3D, Vector3D, double)} - */ - public SubLine(final Vector3D start, final Vector3D end) - throws MathIllegalArgumentException { - this(start, end, DEFAULT_TOLERANCE); - } - - /** Create a sub-line from a segment. - * @param segment single segment forming the sub-line - * @exception MathIllegalArgumentException if the segment endpoints are equal - */ - public SubLine(final Segment segment) throws MathIllegalArgumentException { - this(segment.getLine(), - buildIntervalSet(segment.getStart(), segment.getEnd(), segment.getLine().getTolerance())); - } - - /** Get the endpoints of the sub-line. - * <p> - * A subline may be any arbitrary number of disjoints segments, so the endpoints - * are provided as a list of endpoint pairs. Each element of the list represents - * one segment, and each segment contains a start point at index 0 and an end point - * at index 1. If the sub-line is unbounded in the negative infinity direction, - * the start point of the first segment will have infinite coordinates. If the - * sub-line is unbounded in the positive infinity direction, the end point of the - * last segment will have infinite coordinates. So a sub-line covering the whole - * line will contain just one row and both elements of this row will have infinite - * coordinates. If the sub-line is empty, the returned list will contain 0 segments. - * </p> - * @return list of segments endpoints - */ - public List<Segment> getSegments() { - - final List<Interval> list = remainingRegion.asList(); - final List<Segment> segments = new ArrayList<Segment>(list.size()); - - for (final Interval interval : list) { - final Vector3D start = line.toSpace((Point<Euclidean1D>) new Vector1D(interval.getInf())); - final Vector3D end = line.toSpace((Point<Euclidean1D>) new Vector1D(interval.getSup())); - segments.add(new Segment(start, end, line)); - } - - return segments; - - } - - /** Get the intersection of the instance and another sub-line. - * <p> - * This method is related to the {@link Line#intersection(Line) - * intersection} method in the {@link Line Line} class, but in addition - * to compute the point along infinite lines, it also checks the point - * lies on both sub-line ranges. - * </p> - * @param subLine other sub-line which may intersect instance - * @param includeEndPoints if true, endpoints are considered to belong to - * instance (i.e. they are closed sets) and may be returned, otherwise endpoints - * are considered to not belong to instance (i.e. they are open sets) and intersection - * occurring on endpoints lead to null being returned - * @return the intersection point if there is one, null if the sub-lines don't intersect - */ - public Vector3D intersection(final SubLine subLine, final boolean includeEndPoints) { - - // compute the intersection on infinite line - Vector3D v1D = line.intersection(subLine.line); - if (v1D == null) { - return null; - } - - // check location of point with respect to first sub-line - Location loc1 = remainingRegion.checkPoint((Point<Euclidean1D>) line.toSubSpace((Point<Euclidean3D>) v1D)); - - // check location of point with respect to second sub-line - Location loc2 = subLine.remainingRegion.checkPoint((Point<Euclidean1D>) subLine.line.toSubSpace((Point<Euclidean3D>) v1D)); - - if (includeEndPoints) { - return ((loc1 != Location.OUTSIDE) && (loc2 != Location.OUTSIDE)) ? v1D : null; - } else { - return ((loc1 == Location.INSIDE) && (loc2 == Location.INSIDE)) ? v1D : null; - } - - } - - /** Build an interval set from two points. - * @param start start point - * @param end end point - * @return an interval set - * @param tolerance tolerance below which points are considered identical - * @exception MathIllegalArgumentException if the points are equal - */ - private static IntervalsSet buildIntervalSet(final Vector3D start, final Vector3D end, final double tolerance) - throws MathIllegalArgumentException { - final Line line = new Line(start, end, tolerance); - return new IntervalsSet(line.toSubSpace((Point<Euclidean3D>) start).getX(), - line.toSubSpace((Point<Euclidean3D>) end).getX(), - tolerance); - } - -}