View Javadoc
1   package org.djutils.draw.surface;
2   
3   import java.util.Arrays;
4   import java.util.Iterator;
5   import java.util.LinkedHashMap;
6   import java.util.Locale;
7   import java.util.Map;
8   import java.util.NoSuchElementException;
9   
10  import org.djutils.draw.Drawable2d;
11  import org.djutils.draw.Drawable3d;
12  import org.djutils.draw.InvalidProjectionException;
13  import org.djutils.draw.bounds.Bounds3d;
14  import org.djutils.draw.point.Point3d;
15  import org.djutils.exceptions.Throw;
16  
17  /**
18   * Triangulated surface in 3D space.
19   * <p>
20   * Copyright (c) 2021-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
21   * BSD-style license. See <a href="https://djutils.org/docs/current/djutils/licenses.html">DJUTILS License</a>.
22   * </p>
23   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
24   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
25   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
26   */
27  public class Surface3d implements Drawable3d
28  {
29      /** X-coordinates of all points used to define the triangulated surface. */
30      private final double[] x;
31  
32      /** Y-coordinates of all points used to define the triangulated surface. */
33      private final double[] y;
34  
35      /** Z-coordinates of all points used to define the triangulated surface. */
36      private final double[] z;
37  
38      /** Indices into the points array for each triangle in succession. */
39      private final int[] indices;
40  
41      /** The bounds of this Surface3d. */
42      private final Bounds3d bounds;
43  
44      /**
45       * Construct a new Surface3d.
46       * @param points two dimensional array of points. The first index iterates over the individual triangles; the second index
47       *            iterates over the points of a single triangle. The range of the second index must be 3. It is expected that
48       *            all points appear multiple times in the points array, but never within the same sub-array.
49       * @throws NullPointerException when <code>points</code> is <code>null</code>, or contains a <code>null</code> value
50       * @throws IllegalArgumentException when points is empty, or any element in <code>points</code> does not contain exactly
51       *             three different points
52       */
53      public Surface3d(final Point3d[][] points) throws NullPointerException, IllegalArgumentException
54      {
55          Throw.whenNull(points, "points");
56          Throw.when(points.length == 0, IllegalArgumentException.class, "points must have at least one element");
57          this.indices = new int[points.length * 3];
58          // Figure out how many points are unique
59          Map<Point3d, Integer> indexMap = new LinkedHashMap<>();
60          for (int triangle = 0; triangle < points.length; triangle++)
61          {
62              Point3d[] trianglePoints = points[triangle];
63              Throw.whenNull(trianglePoints, "Element in trianglePoints may not be null");
64              Throw.when(trianglePoints.length != 3, IllegalArgumentException.class,
65                      "Triangle %d contain wrong number of points (should be 3, got %d", triangle, trianglePoints.length);
66              Point3d prevPoint = trianglePoints[2];
67              for (int i = 0; i < 3; i++)
68              {
69                  Point3d point = trianglePoints[i];
70                  // The next check is not good enough when this constructor can be fed a subclass of Point3d
71                  Throw.when(point.equals(prevPoint), IllegalArgumentException.class, "Triangle %d contains duplicate point",
72                          triangle);
73                  Integer currentIndex = indexMap.get(point);
74                  if (currentIndex == null)
75                  {
76                      indexMap.put(point, indexMap.size());
77                  }
78                  prevPoint = point;
79              }
80          }
81          this.x = new double[indexMap.size()];
82          this.y = new double[indexMap.size()];
83          this.z = new double[indexMap.size()];
84          for (int triangle = 0; triangle < points.length; triangle++)
85          {
86              Point3d[] trianglePoints = points[triangle];
87              for (int i = 0; i < 3; i++)
88              {
89                  Point3d point3d = trianglePoints[i];
90                  Integer index = indexMap.get(point3d);
91                  this.indices[triangle * 3 + i] = index;
92                  this.x[index] = point3d.x;
93                  this.y[index] = point3d.y;
94                  this.z[index] = point3d.z;
95              }
96          }
97          this.bounds = new Bounds3d(iterator());
98      }
99  
100     @Override
101     public Iterator<Point3d> iterator()
102     {
103         return new Iterator<Point3d>()
104         {
105             private int current = 0;
106 
107             @Override
108             public boolean hasNext()
109             {
110                 return this.current < size();
111             }
112 
113             @Override
114             public Point3d next()
115             {
116                 Throw.when(this.current >= size(), NoSuchElementException.class, "Iterator has exhausted the input");
117                 int index = this.current++;
118                 return new Point3d(Surface3d.this.x[Surface3d.this.indices[index]],
119                         Surface3d.this.y[Surface3d.this.indices[index]], Surface3d.this.z[Surface3d.this.indices[index]]);
120             }
121         };
122     }
123 
124     @Override
125     public int size()
126     {
127         return this.indices.length;
128     }
129 
130     @Override
131     public Bounds3d getAbsoluteBounds()
132     {
133         return this.bounds;
134     }
135 
136     @Override
137     public Drawable2d project() throws InvalidProjectionException
138     {
139         throw new InvalidProjectionException(
140                 "Project not implemented because we do not have a class that represents a multitude "
141                         + "of triangles in the 2D plane");
142     }
143 
144     @Override
145     public String toString()
146     {
147         return toString("%f", false);
148     }
149 
150     @Override
151     public String toString(final String doubleFormat, final boolean doNotIncludeClassName)
152     {
153         StringBuilder result = new StringBuilder();
154         if (!doNotIncludeClassName)
155         {
156             result.append("Surface3d ");
157         }
158         String format = String.format("%%sx=%1$s, y=%1$s, z=%1$s", doubleFormat);
159         for (int index = 0; index < this.indices.length; index++)
160         {
161             if (index % 3 == 0)
162             {
163                 result.append("[");
164             }
165             int i = this.indices[index];
166             result.append(String.format(Locale.US, format, index == 0 ? "[" : ", ", this.x[i], this.y[i], this.z[i]));
167             if (index % 3 == 2)
168             {
169                 result.append("]");
170             }
171         }
172         result.append("]");
173         return result.toString();
174     }
175 
176     @SuppressWarnings("checkstyle:designforextension")
177     @Override
178     public int hashCode()
179     {
180         final int prime = 31;
181         int result = 1;
182         result = prime * result + Arrays.hashCode(this.indices);
183         result = prime * result + Arrays.hashCode(this.x);
184         result = prime * result + Arrays.hashCode(this.y);
185         result = prime * result + Arrays.hashCode(this.z);
186         return result;
187     }
188 
189     @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
190     @Override
191     public boolean equals(final Object obj)
192     {
193         if (this == obj)
194             return true;
195         if (obj == null)
196             return false;
197         if (getClass() != obj.getClass())
198             return false;
199         Surface3d other = (Surface3d) obj;
200         if (!Arrays.equals(this.indices, other.indices))
201             return false;
202         if (!Arrays.equals(this.x, other.x))
203             return false;
204         if (!Arrays.equals(this.y, other.y))
205             return false;
206         if (!Arrays.equals(this.z, other.z))
207             return false;
208         return true;
209     }
210 
211 }