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.threed;
18  
19  import java.util.Arrays;
20  import java.util.List;
21  
22  import org.apache.commons.geometry.core.Transform;
23  
24  /** Simple implementation of {@link Triangle3D}.
25   *
26   * <p>Instances of this class are guaranteed to be immutable.</p>
27   */
28  final class SimpleTriangle3D extends AbstractConvexPolygon3D implements Triangle3D {
29  
30      /** First point in the triangle. */
31      private final Vector3D p1;
32  
33      /** Second point in the triangle. */
34      private final Vector3D p2;
35  
36      /** Third point in the triangle. */
37      private final Vector3D p3;
38  
39      /** Construct a new instance from a plane and 3 points. Callers are responsible for ensuring that
40       * the points lie on the plane and define a triangle. No validation is performed.
41       * @param plane the plane containing the triangle
42       * @param p1 first point in the triangle
43       * @param p2 second point in the triangle
44       * @param p3 third point in the triangle
45       */
46      SimpleTriangle3D(final Plane plane, final Vector3D p1, final Vector3D p2, final Vector3D p3) {
47          super(plane);
48  
49          this.p1 = p1;
50          this.p2 = p2;
51          this.p3 = p3;
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      public Vector3D getPoint1() {
57          return p1;
58      }
59  
60      /** {@inheritDoc} */
61      @Override
62      public Vector3D getPoint2() {
63          return p2;
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public Vector3D getPoint3() {
69          return p3;
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      public List<Vector3D> getVertices() {
75          return Arrays.asList(p1, p2, p3);
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public double getSize() {
81          final Vector3D v1 = p1.vectorTo(p2);
82          final Vector3D v2 = p1.vectorTo(p3);
83          return 0.5 * v1.cross(v2).norm();
84      }
85  
86      /** {@inheritDoc} */
87      @Override
88      public Vector3D getCentroid() {
89          return Vector3D.centroid(p1, p2, p3);
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      public SimpleTriangle3D reverse() {
95          final Plane rPlane = getPlane().reverse();
96  
97          return new SimpleTriangle3D(rPlane, p1, p3, p2); // reverse point ordering
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     public SimpleTriangle3D transform(final Transform<Vector3D> transform) {
103         final Plane tPlane = getPlane().transform(transform);
104         final Vector3D t1 = transform.apply(p1);
105         final Vector3D t2 = transform.apply(p2);
106         final Vector3D t3 = transform.apply(p3);
107 
108         return new SimpleTriangle3D(tPlane, t1, t2, t3);
109     }
110 }