SimpleTriangle3D.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.threed;

  18. import java.util.Arrays;
  19. import java.util.List;

  20. import org.apache.commons.geometry.core.Transform;

  21. /** Simple implementation of {@link Triangle3D}.
  22.  *
  23.  * <p>Instances of this class are guaranteed to be immutable.</p>
  24.  */
  25. final class SimpleTriangle3D extends AbstractConvexPolygon3D implements Triangle3D {

  26.     /** First point in the triangle. */
  27.     private final Vector3D p1;

  28.     /** Second point in the triangle. */
  29.     private final Vector3D p2;

  30.     /** Third point in the triangle. */
  31.     private final Vector3D p3;

  32.     /** Construct a new instance from a plane and 3 points. Callers are responsible for ensuring that
  33.      * the points lie on the plane and define a triangle. No validation is performed.
  34.      * @param plane the plane containing the triangle
  35.      * @param p1 first point in the triangle
  36.      * @param p2 second point in the triangle
  37.      * @param p3 third point in the triangle
  38.      */
  39.     SimpleTriangle3D(final Plane plane, final Vector3D p1, final Vector3D p2, final Vector3D p3) {
  40.         super(plane);

  41.         this.p1 = p1;
  42.         this.p2 = p2;
  43.         this.p3 = p3;
  44.     }

  45.     /** {@inheritDoc} */
  46.     @Override
  47.     public Vector3D getPoint1() {
  48.         return p1;
  49.     }

  50.     /** {@inheritDoc} */
  51.     @Override
  52.     public Vector3D getPoint2() {
  53.         return p2;
  54.     }

  55.     /** {@inheritDoc} */
  56.     @Override
  57.     public Vector3D getPoint3() {
  58.         return p3;
  59.     }

  60.     /** {@inheritDoc} */
  61.     @Override
  62.     public List<Vector3D> getVertices() {
  63.         return Arrays.asList(p1, p2, p3);
  64.     }

  65.     /** {@inheritDoc} */
  66.     @Override
  67.     public double getSize() {
  68.         final Vector3D v1 = p1.vectorTo(p2);
  69.         final Vector3D v2 = p1.vectorTo(p3);
  70.         return 0.5 * v1.cross(v2).norm();
  71.     }

  72.     /** {@inheritDoc} */
  73.     @Override
  74.     public Vector3D getCentroid() {
  75.         return Vector3D.centroid(p1, p2, p3);
  76.     }

  77.     /** {@inheritDoc} */
  78.     @Override
  79.     public SimpleTriangle3D reverse() {
  80.         final Plane rPlane = getPlane().reverse();

  81.         return new SimpleTriangle3D(rPlane, p1, p3, p2); // reverse point ordering
  82.     }

  83.     /** {@inheritDoc} */
  84.     @Override
  85.     public SimpleTriangle3D transform(final Transform<Vector3D> transform) {
  86.         final Plane tPlane = getPlane().transform(transform);
  87.         final Vector3D t1 = transform.apply(p1);
  88.         final Vector3D t2 = transform.apply(p2);
  89.         final Vector3D t3 = transform.apply(p3);

  90.         return new SimpleTriangle3D(tPlane, t1, t2, t3);
  91.     }
  92. }