ReverseRay3D.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.geometry.euclidean.threed.line;

  18. import org.apache.commons.geometry.core.Transform;
  19. import org.apache.commons.geometry.euclidean.threed.Bounds3D;
  20. import org.apache.commons.geometry.euclidean.threed.Vector3D;

  21. /** Class representing a portion of a line in 3D Euclidean space that starts at infinity and
  22.  * continues in the direction of the line up to a single end point. This is equivalent to taking a
  23.  * {@link Ray3D} and reversing the line direction.
  24.  *
  25.  * <p>Instances of this class are guaranteed to be immutable.</p>
  26.  * @see Ray3D
  27.  * @see Lines3D
  28.  */
  29. public final class ReverseRay3D extends LineConvexSubset3D {

  30.     /** The abscissa of the line subset endpoint. */
  31.     private final double end;

  32.     /** Construct a new instance from the given line and end point. The end point is projected onto
  33.      * the line. No validation is performed.
  34.      * @param line line for the instance
  35.      * @param endPoint end point for the instance
  36.      */
  37.     ReverseRay3D(final Line3D line, final Vector3D endPoint) {
  38.         this(line, line.abscissa(endPoint));
  39.     }

  40.     /** Construct a new instance from the given line and 1D end location. No validation is performed.
  41.      * @param line line for the instance
  42.      * @param end end location for the instance
  43.      */
  44.     ReverseRay3D(final Line3D line, final double end) {
  45.         super(line);

  46.         this.end = end;
  47.     }

  48.     /** {@inheritDoc}
  49.     *
  50.     * <p>This method always returns {@code true}.</p>
  51.     */
  52.     @Override
  53.     public boolean isInfinite() {
  54.         return true;
  55.     }

  56.     /** {@inheritDoc}
  57.     *
  58.     * <p>This method always returns {@code false}.</p>
  59.     */
  60.     @Override
  61.     public boolean isFinite() {
  62.         return false;
  63.     }

  64.     /** {@inheritDoc}
  65.     *
  66.     * <p>This method always returns {@link Double#POSITIVE_INFINITY}.</p>
  67.     */
  68.     @Override
  69.     public double getSize() {
  70.         return Double.POSITIVE_INFINITY;
  71.     }

  72.     /** {@inheritDoc}
  73.     *
  74.     * <p>This method always returns {@code null}.</p>
  75.     */
  76.     @Override
  77.     public Vector3D getStartPoint() {
  78.         return null;
  79.     }

  80.     /** {@inheritDoc}
  81.     *
  82.     * <p>This method always returns {@link Double#NEGATIVE_INFINITY}.</p>
  83.     */
  84.     @Override
  85.     public double getSubspaceStart() {
  86.         return Double.NEGATIVE_INFINITY;
  87.     }

  88.     /** {@inheritDoc} */
  89.     @Override
  90.     public Vector3D getEndPoint() {
  91.         return getLine().toSpace(end);
  92.     }

  93.     /** {@inheritDoc} */
  94.     @Override
  95.     public double getSubspaceEnd() {
  96.         return end;
  97.     }

  98.     /** {@inheritDoc}
  99.      *
  100.      * <p>This method always returns {@code null}.</p>
  101.      */
  102.     @Override
  103.     public Vector3D getCentroid() {
  104.         return null; // infinite; no center
  105.     }

  106.    /** {@inheritDoc}
  107.     *
  108.     * <p>This method always returns {@code null}.</p>
  109.     */
  110.     @Override
  111.     public Bounds3D getBounds() {
  112.         return null; // infinite; no bounds
  113.     }

  114.     /** {@inheritDoc} */
  115.     @Override
  116.     public ReverseRay3D transform(final Transform<Vector3D> transform) {
  117.         final Line3D tLine = getLine().transform(transform);
  118.         final Vector3D tEnd = transform.apply(getEndPoint());

  119.         return new ReverseRay3D(tLine, tEnd);
  120.     }

  121.     /** {@inheritDoc} */
  122.     @Override
  123.     public String toString() {
  124.         final StringBuilder sb = new StringBuilder();
  125.         sb.append(getClass().getSimpleName())
  126.             .append("[direction= ")
  127.             .append(getLine().getDirection())
  128.             .append(", endPoint= ")
  129.             .append(getEndPoint())
  130.             .append(']');

  131.         return sb.toString();
  132.     }

  133.     /** {@inheritDoc} */
  134.     @Override
  135.     boolean containsAbscissa(final double abscissa) {
  136.         return getLine().getPrecision().lte(abscissa, end);
  137.     }
  138. }