Segment3D.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. import org.apache.commons.numbers.core.Precision;

  22. /** Class representing a line segment in 3D Euclidean space. A line segment is a portion of
  23.  * a line with finite start and end points.
  24.  *
  25.  * <p>Instances of this class are guaranteed to be immutable.</p>
  26.  * @see Lines3D
  27.  * @see <a href="https://en.wikipedia.org/wiki/Line_segment">Line Segment</a>
  28.  */
  29. public final class Segment3D extends LineConvexSubset3D {

  30.     /** Start abscissa for the segment. */
  31.     private final double start;

  32.     /** End abscissa for the segment. */
  33.     private final double end;

  34.     /** Construct a new instance from a line and two points on the line. The points are projected onto
  35.      * the line and must be in order of increasing abscissa. No validation is performed.
  36.      * @param line line for the segment
  37.      * @param startPoint segment start point
  38.      * @param endPoint segment end point
  39.      */
  40.     Segment3D(final Line3D line, final Vector3D startPoint, final Vector3D endPoint) {
  41.         this(line, line.abscissa(startPoint), line.abscissa(endPoint));
  42.     }

  43.     /** Construct a new instance from a line and two abscissa locations on the line.
  44.      * The abscissa locations must be in increasing order. No validation is performed.
  45.      * @param line line for the segment
  46.      * @param start abscissa start location
  47.      * @param end abscissa end location
  48.      */
  49.     Segment3D(final Line3D line, final double start, final double end) {
  50.         super(line);

  51.         this.start = start;
  52.         this.end = end;
  53.     }

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

  62.     /** {@inheritDoc}
  63.      *
  64.      * <p>This method always returns {@code true}.</p>
  65.      */
  66.     @Override
  67.     public boolean isFinite() {
  68.         return true;
  69.     }

  70.     /** {@inheritDoc} */
  71.     @Override
  72.     public Vector3D getStartPoint() {
  73.         return getLine().toSpace(start);
  74.     }

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

  80.     /** {@inheritDoc} */
  81.     @Override
  82.     public Vector3D getEndPoint() {
  83.         return getLine().toSpace(end);
  84.     }

  85.     /** {@inheritDoc} */
  86.     @Override
  87.     public double getSubspaceEnd() {
  88.         return end;
  89.     }

  90.     /** {@inheritDoc} */
  91.     @Override
  92.     public double getSize() {
  93.         return end - start;
  94.     }

  95.     /** {@inheritDoc} */
  96.     @Override
  97.     public Vector3D getCentroid() {
  98.         return getLine().toSpace((0.5 * (end - start)) + start);
  99.     }

  100.     /** {@inheritDoc} */
  101.     @Override
  102.     public Bounds3D getBounds() {
  103.         return Bounds3D.builder()
  104.                 .add(getStartPoint())
  105.                 .add(getEndPoint())
  106.                 .build();
  107.     }

  108.     /** {@inheritDoc} */
  109.     @Override
  110.     public Segment3D transform(final Transform<Vector3D> transform) {
  111.         final Vector3D t1 = transform.apply(getStartPoint());
  112.         final Vector3D t2 = transform.apply(getEndPoint());

  113.         final Line3D tLine = getLine().transform(transform);

  114.         return new Segment3D(tLine, t1, t2);
  115.     }

  116.     /** {@inheritDoc} */
  117.     @Override
  118.     public String toString() {
  119.         final StringBuilder sb = new StringBuilder();
  120.         sb.append(getClass().getSimpleName())
  121.             .append("[startPoint= ")
  122.             .append(getStartPoint())
  123.             .append(", endPoint= ")
  124.             .append(getEndPoint())
  125.             .append(']');

  126.         return sb.toString();
  127.     }

  128.     /** {@inheritDoc} */
  129.     @Override
  130.     boolean containsAbscissa(final double abscissa) {
  131.         final Precision.DoubleEquivalence precision = getLine().getPrecision();
  132.         return precision.gte(abscissa, start) &&
  133.                 precision.lte(abscissa, end);
  134.     }
  135. }