View Javadoc

    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
    19  import java.util.Objects;
    20
    21  import org.apache.commons.geometry.core.Embedding;
    22  import org.apache.commons.geometry.euclidean.oned.Vector1D;
    23
    24  /** Base class for intersections discovered during linecast operations. This class contains
    25   * the intersection point and the normal of the target boundary at the point of intersection
    26   * along with the intersecting line and abscissa.
    27   * @param <P> Euclidean point/vector implementation type
    28   * @param <U> Unit-length Euclidean vector implementation type
    29   * @param <L> Line implementation type
    30   */
    31  public abstract class AbstractLinecastPoint<
    32      P extends EuclideanVector<P>,
    33      U extends P,
    34      L extends Embedding<P, Vector1D>> {
    35
    36      /** Line intersection point. */
    37      private final P point;
    38
    39      /** Normal of the target boundary at the intersection point. */
    40      private final U normal;
    41
    42      /** The intersecting line. */
    43      private final L line;
    44
    45      /** Abscissa of the intersection point along the intersecting line. */
    46      private final double abscissa;
    47
    48      /** Construct a new instance from its components.
    49       * @param point intersection point
    50       * @param normal surface normal
    51       * @param line line that the intersection point belongs to
    52       */
    53      protected AbstractLinecastPoint(final P point, final U normal, final L line) {
    54          this.point = point;
    55          this.normal = normal;
    56          this.line = line;
    57
    58          this.abscissa = line.toSubspace(point).getX();
    59      }
    60
    61      /** Get the line intersection point.
    62       * @return the line intersection point
    63       */
    64      public P getPoint() {
    65          return point;
    66      }
    67
    68      /** Get the normal of the target boundary at the intersection point.
    69       * @return the normal of the target boundary at the intersection point
    70       */
    71      public U getNormal() {
    72          return normal;
    73      }
    74
    75      /** Get the intersecting line.
    76       * @return the intersecting line
    77       */
    78      public L getLine() {
    79          return line;
    80      }
    81
    82      /** Get the abscissa (1D position) of the intersection point
    83       * along the linecast line.
    84       * @return the abscissa of the intersection point.
    85       */
    86      public double getAbscissa() {
    87          return abscissa;
    88      }
    89
    90      /** {@inheritDoc} */
    91      @Override
    92      public int hashCode() {
    93          return Objects.hash(point, normal, line);
    94      }
    95
    96      /** {@inheritDoc} */
    97      @Override
    98      public boolean equals(final Object obj) {
    99          if (this == obj) {
    100             return true;
    101         }
    102         if (obj == null || !getClass().equals(obj.getClass())) {
    103             return false;
    104         }
    105
    106         final AbstractLinecastPoint<?, ?, ?> other = (AbstractLinecastPoint<?, ?, ?>) obj;
    107
    108         return Objects.equals(point, other.point) &&
    109                 Objects.equals(normal, other.normal) &&
    110                 Objects.equals(line, other.line);
    111     }
    112
    113     /** {@inheritDoc} */
    114     @Override
    115     public String toString() {
    116         final StringBuilder sb = new StringBuilder(50);
    117         sb.append(getClass().getSimpleName())
    118             .append("[point= ")
    119             .append(getPoint())
    120             .append(", normal= ")
    121             .append(getNormal())
    122             .append(", abscissa= ")
    123             .append(getAbscissa())
    124             .append(", line= ")
    125             .append(getLine())
    126             .append(']');
    127
    128         return sb.toString();
    129     }
    130 }