StlUtils.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.stl;

  18. import java.nio.ByteBuffer;

  19. import org.apache.commons.geometry.euclidean.threed.Vector3D;

  20. /** Utility methods for the STL format.
  21.  */
  22. final class StlUtils {

  23.     /** Utility class; no instantiation. */
  24.     private StlUtils() { }

  25.     /** Create a {@link ByteBuffer} with the given size and the byte order
  26.      * appropriate for binary STL content.
  27.      * @param capacity buffer capacity
  28.      * @return byte buffer
  29.      */
  30.     static ByteBuffer byteBuffer(final int capacity) {
  31.         return ByteBuffer.allocate(capacity)
  32.                 .order(StlConstants.BINARY_BYTE_ORDER);
  33.     }

  34.     /** Determine the normal that should be used for the given STL triangle vertices. If {@code normal}
  35.      * is present and can be normalized, it is returned. Otherwise, a normal is attempted to be computed
  36.      * using the given triangle vertices. If normal computation fails, the zero vector is returned.
  37.      * @param p1 first point
  38.      * @param p2 second point
  39.      * @param p3 third point
  40.      * @param normal defined triangle normal; may be null
  41.      * @return STL normal for the triangle
  42.      */
  43.     static Vector3D determineNormal(final Vector3D p1, final Vector3D p2, final Vector3D p3,
  44.             final Vector3D normal) {
  45.         if (normal != null) {
  46.             // try to normalize it
  47.             final Vector3D normalized = normal.normalizeOrNull();
  48.             if (normalized != null) {
  49.                 return normalized;
  50.             }
  51.         }

  52.         // try to compute one from the triangle points
  53.         final Vector3D computed = computeTriangleNormal(p1, p2, p3);
  54.         return computed != null ?
  55.                 computed :
  56.                 Vector3D.ZERO;
  57.     }

  58.     /** Return true if the given points are arranged counter-clockwise relative to the
  59.      * given normal. Returns true if {@code normal} is null.
  60.      * @param p1 first point
  61.      * @param p2 second point
  62.      * @param p3 third point
  63.      * @param normal normal; may be null, in which case the zero vector is used
  64.      * @return true if {@code normal} is null or if the given points are arranged counter-clockwise
  65.      *      relative to {@code normal}
  66.      */
  67.     static boolean pointsAreCounterClockwise(final Vector3D p1, final Vector3D p2, final Vector3D p3,
  68.             final Vector3D normal) {
  69.         if (normal != null) {
  70.             final Vector3D computedNormal = computeTriangleNormal(p1, p2, p3);
  71.             if (computedNormal != null && normal.dot(computedNormal) < 0) {
  72.                 return false;
  73.             }
  74.         }

  75.         return true;
  76.     }

  77.     /** Get the normal using the right-hand rule for the given triangle vertices. Null is returned
  78.      * if the normal could not be computed.
  79.      * @param p1 first point
  80.      * @param p2 second point
  81.      * @param p3 third point
  82.      * @return the normal for the given triangle vertices or null if one could not be computed
  83.      */
  84.     private static Vector3D computeTriangleNormal(final Vector3D p1, final Vector3D p2, final Vector3D p3) {
  85.         final Vector3D normal = p1.vectorTo(p2).cross(p1.vectorTo(p3)).normalizeOrNull();
  86.         return normal != null ?
  87.                 normal :
  88.                 null;
  89.     }
  90. }