AbstractLinecastPoint.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;

  18. import java.util.Objects;

  19. import org.apache.commons.geometry.core.Embedding;
  20. import org.apache.commons.geometry.euclidean.oned.Vector1D;

  21. /** Base class for intersections discovered during linecast operations. This class contains
  22.  * the intersection point and the normal of the target boundary at the point of intersection
  23.  * along with the intersecting line and abscissa.
  24.  * @param <P> Euclidean point/vector implementation type
  25.  * @param <U> Unit-length Euclidean vector implementation type
  26.  * @param <L> Line implementation type
  27.  */
  28. public abstract class AbstractLinecastPoint<
  29.     P extends EuclideanVector<P>,
  30.     U extends P,
  31.     L extends Embedding<P, Vector1D>> {

  32.     /** Line intersection point. */
  33.     private final P point;

  34.     /** Normal of the target boundary at the intersection point. */
  35.     private final U normal;

  36.     /** The intersecting line. */
  37.     private final L line;

  38.     /** Abscissa of the intersection point along the intersecting line. */
  39.     private final double abscissa;

  40.     /** Construct a new instance from its components.
  41.      * @param point intersection point
  42.      * @param normal surface normal
  43.      * @param line line that the intersection point belongs to
  44.      */
  45.     protected AbstractLinecastPoint(final P point, final U normal, final L line) {
  46.         this.point = point;
  47.         this.normal = normal;
  48.         this.line = line;

  49.         this.abscissa = line.toSubspace(point).getX();
  50.     }

  51.     /** Get the line intersection point.
  52.      * @return the line intersection point
  53.      */
  54.     public P getPoint() {
  55.         return point;
  56.     }

  57.     /** Get the normal of the target boundary at the intersection point.
  58.      * @return the normal of the target boundary at the intersection point
  59.      */
  60.     public U getNormal() {
  61.         return normal;
  62.     }

  63.     /** Get the intersecting line.
  64.      * @return the intersecting line
  65.      */
  66.     public L getLine() {
  67.         return line;
  68.     }

  69.     /** Get the abscissa (1D position) of the intersection point
  70.      * along the linecast line.
  71.      * @return the abscissa of the intersection point.
  72.      */
  73.     public double getAbscissa() {
  74.         return abscissa;
  75.     }

  76.     /** {@inheritDoc} */
  77.     @Override
  78.     public int hashCode() {
  79.         return Objects.hash(point, normal, line);
  80.     }

  81.     /** {@inheritDoc} */
  82.     @Override
  83.     public boolean equals(final Object obj) {
  84.         if (this == obj) {
  85.             return true;
  86.         }
  87.         if (obj == null || !getClass().equals(obj.getClass())) {
  88.             return false;
  89.         }

  90.         final AbstractLinecastPoint<?, ?, ?> other = (AbstractLinecastPoint<?, ?, ?>) obj;

  91.         return Objects.equals(point, other.point) &&
  92.                 Objects.equals(normal, other.normal) &&
  93.                 Objects.equals(line, other.line);
  94.     }

  95.     /** {@inheritDoc} */
  96.     @Override
  97.     public String toString() {
  98.         final StringBuilder sb = new StringBuilder(50);
  99.         sb.append(getClass().getSimpleName())
  100.             .append("[point= ")
  101.             .append(getPoint())
  102.             .append(", normal= ")
  103.             .append(getNormal())
  104.             .append(", abscissa= ")
  105.             .append(getAbscissa())
  106.             .append(", line= ")
  107.             .append(getLine())
  108.             .append(']');

  109.         return sb.toString();
  110.     }
  111. }