Ray3D.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 ray in 3D Euclidean space. A ray is a portion of a line consisting of
  22.  * a single start point and extending to infinity along the direction of the line.
  23.  *
  24.  * <p>Instances of this class are guaranteed to be immutable.</p>
  25.  * @see ReverseRay3D
  26.  * @see Lines3D
  27.  * @see <a href="https://en.wikipedia.org/wiki/Line_(geometry)#Ray">Ray</a>
  28.  */
  29. public final class Ray3D extends LineConvexSubset3D {

  30.     /** The start abscissa value for the ray. */
  31.     private final double start;

  32.     /** Construct a ray from a line and a start point. The start point is projected
  33.      * onto the line. No validation is performed.
  34.      * @param line line for the ray
  35.      * @param startPoint start point for the ray
  36.      */
  37.     Ray3D(final Line3D line, final Vector3D startPoint) {
  38.         this(line, line.abscissa(startPoint));
  39.     }

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

  46.         this.start = start;
  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.     @Override
  73.     public Vector3D getStartPoint() {
  74.         return getLine().toSpace(start);
  75.     }

  76.     /** {@inheritDoc} */
  77.     @Override
  78.     public double getSubspaceStart() {
  79.         return start;
  80.     }

  81.     /** {@inheritDoc}
  82.     *
  83.     * <p>This method always returns {@code null}.</p>
  84.     */
  85.     @Override
  86.     public Vector3D getEndPoint() {
  87.         return null;
  88.     }

  89.     /** {@inheritDoc}
  90.     *
  91.     * <p>This method always returns {@link Double#POSITIVE_INFINITY}.</p>
  92.     */
  93.     @Override
  94.     public double getSubspaceEnd() {
  95.         return Double.POSITIVE_INFINITY;
  96.     }

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

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

  113.     /** Get the direction of the ray. This is a convenience method for {@code ray.getLine().getDirection()}.
  114.      * @return the direction of the ray
  115.      */
  116.     public Vector3D getDirection() {
  117.         return getLine().getDirection();
  118.     }

  119.     /** {@inheritDoc} */
  120.     @Override
  121.     public Ray3D transform(final Transform<Vector3D> transform) {
  122.         final Line3D tLine = getLine().transform(transform);
  123.         final Vector3D tStart = transform.apply(getStartPoint());

  124.         return new Ray3D(tLine, tStart);
  125.     }

  126.     /** {@inheritDoc} */
  127.     @Override
  128.     public String toString() {
  129.         final StringBuilder sb = new StringBuilder();
  130.         sb.append(getClass().getSimpleName())
  131.             .append("[startPoint= ")
  132.             .append(getStartPoint())
  133.             .append(", direction= ")
  134.             .append(getLine().getDirection())
  135.             .append(']');

  136.         return sb.toString();
  137.     }

  138.     /** {@inheritDoc} */
  139.     @Override
  140.     boolean containsAbscissa(final double abscissa) {
  141.         return getLine().getPrecision().gte(abscissa, start);
  142.     }
  143. }