SimpleFacetDefinition.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.io.euclidean.threed;

  18. import java.util.Collections;
  19. import java.util.List;
  20. import java.util.Objects;

  21. import org.apache.commons.geometry.euclidean.internal.EuclideanUtils;
  22. import org.apache.commons.geometry.euclidean.threed.Vector3D;

  23. /** Simple {@link FacetDefinition} implementation that stores a list of vertices and
  24.  * optional normal.
  25.  */
  26. public class SimpleFacetDefinition implements FacetDefinition {

  27.     /** Facet vertices. */
  28.     private final List<Vector3D> vertices;

  29.     /** Facet normal; may be null. */
  30.     private final Vector3D normal;

  31.     /** Construct a new instance with the given vertices and no defined normal.
  32.      * @param vertices facet vertices
  33.      * @throws IllegalArgumentException if {@code vertices} contains fewer than 3 elements
  34.      */
  35.     public SimpleFacetDefinition(final List<Vector3D> vertices) {
  36.         this(vertices, null);
  37.     }

  38.     /** Construct a new instance with the given vertices and normal.
  39.      * @param vertices facet vertices
  40.      * @param normal facet normal; may be null
  41.      * @throws IllegalArgumentException if {@code vertices} contains fewer than 3 elements
  42.      */
  43.     public SimpleFacetDefinition(final List<Vector3D> vertices, final Vector3D normal) {
  44.         Objects.requireNonNull(vertices, "Facet vertex list cannot be null");
  45.         if (vertices.size() < EuclideanUtils.TRIANGLE_VERTEX_COUNT) {
  46.             throw new IllegalArgumentException("Facet vertex list must contain at least " +
  47.                     EuclideanUtils.TRIANGLE_VERTEX_COUNT + " points; found " + vertices.size());
  48.         }

  49.         this.vertices = Collections.unmodifiableList(vertices);
  50.         this.normal = normal;
  51.     }

  52.     /** {@inheritDoc} */
  53.     @Override
  54.     public List<Vector3D> getVertices() {
  55.         return vertices;
  56.     }

  57.     /** {@inheritDoc} */
  58.     @Override
  59.     public Vector3D getNormal() {
  60.         return normal;
  61.     }

  62.     /** {@inheritDoc} */
  63.     @Override
  64.     public String toString() {
  65.         final StringBuilder sb = new StringBuilder();
  66.         sb.append(getClass().getSimpleName())
  67.             .append("[vertices= ")
  68.             .append(getVertices())
  69.             .append(", normal= ")
  70.             .append(getNormal())
  71.             .append(']');

  72.         return sb.toString();
  73.     }
  74. }