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.io.euclidean.threed;
    18
    19  import java.util.Collections;
    20  import java.util.List;
    21  import java.util.Objects;
    22
    23  import org.apache.commons.geometry.euclidean.internal.EuclideanUtils;
    24  import org.apache.commons.geometry.euclidean.threed.Vector3D;
    25
    26  /** Simple {@link FacetDefinition} implementation that stores a list of vertices and
    27   * optional normal.
    28   */
    29  public class SimpleFacetDefinition implements FacetDefinition {
    30
    31      /** Facet vertices. */
    32      private final List<Vector3D> vertices;
    33
    34      /** Facet normal; may be null. */
    35      private final Vector3D normal;
    36
    37      /** Construct a new instance with the given vertices and no defined normal.
    38       * @param vertices facet vertices
    39       * @throws IllegalArgumentException if {@code vertices} contains fewer than 3 elements
    40       */
    41      public SimpleFacetDefinition(final List<Vector3D> vertices) {
    42          this(vertices, null);
    43      }
    44
    45      /** Construct a new instance with the given vertices and normal.
    46       * @param vertices facet vertices
    47       * @param normal facet normal; may be null
    48       * @throws IllegalArgumentException if {@code vertices} contains fewer than 3 elements
    49       */
    50      public SimpleFacetDefinition(final List<Vector3D> vertices, final Vector3D normal) {
    51          Objects.requireNonNull(vertices, "Facet vertex list cannot be null");
    52          if (vertices.size() < EuclideanUtils.TRIANGLE_VERTEX_COUNT) {
    53              throw new IllegalArgumentException("Facet vertex list must contain at least " +
    54                      EuclideanUtils.TRIANGLE_VERTEX_COUNT + " points; found " + vertices.size());
    55          }
    56
    57          this.vertices = Collections.unmodifiableList(vertices);
    58          this.normal = normal;
    59      }
    60
    61      /** {@inheritDoc} */
    62      @Override
    63      public List<Vector3D> getVertices() {
    64          return vertices;
    65      }
    66
    67      /** {@inheritDoc} */
    68      @Override
    69      public Vector3D getNormal() {
    70          return normal;
    71      }
    72
    73      /** {@inheritDoc} */
    74      @Override
    75      public String toString() {
    76          final StringBuilder sb = new StringBuilder();
    77          sb.append(getClass().getSimpleName())
    78              .append("[vertices= ")
    79              .append(getVertices())
    80              .append(", normal= ")
    81              .append(getNormal())
    82              .append(']');
    83
    84          return sb.toString();
    85      }
    86  }