Skip to content

Commit c5b1f72

Browse files
committed
Refactor UpdateFaceNormalsModifier for improved performance and
robustness - Added validation to ensure the mesh is not null, improving error handling. - Introduced a `setMesh` method to cleanly manage the current mesh instance. - Replaced sequential processing with parallelStream for better performance on large meshes. - Optimized normal updates by directly modifying existing vectors to reduce object creation and memory overhead. - Enhanced code readability and maintainability with a structured approach. This update ensures the modifier is faster and more robust, making it suitable for large-scale mesh operations.
1 parent da08978 commit c5b1f72

File tree

1 file changed

+66
-7
lines changed

1 file changed

+66
-7
lines changed
Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,77 @@
11
package mesh.modifier;
22

3+
import math.Vector3f;
34
import mesh.Face3D;
45
import mesh.Mesh3D;
56

7+
/**
8+
* The `UpdateFaceNormalsModifier` recalculates and updates the normals of all
9+
* faces in a 3D mesh. This ensures that face normals are consistent with the
10+
* current vertex positions and geometry.
11+
*
12+
* <p>
13+
* This modifier is useful when the geometry of the mesh has been modified, and
14+
* accurate face normals are needed for rendering, physics calculations, or
15+
* other operations.
16+
* </p>
17+
*/
618
public class UpdateFaceNormalsModifier implements IMeshModifier {
719

8-
@Override
9-
public Mesh3D modify(Mesh3D mesh) {
20+
/** The mesh being modified. */
21+
private Mesh3D mesh;
1022

11-
for (Face3D face : mesh.faces) {
12-
face.normal = mesh.calculateFaceNormal(face);
13-
}
23+
/**
24+
* Recalculates and updates the normals of all faces in the given 3D mesh.
25+
*
26+
* @param mesh The 3D mesh to be modified.
27+
* @return The modified 3D mesh with updated face normals.
28+
* @throws IllegalArgumentException If the provided mesh is null.
29+
*/
30+
@Override
31+
public Mesh3D modify(Mesh3D mesh) {
32+
validateMesh(mesh);
33+
setMesh(mesh);
34+
updateFaceNormals();
35+
return mesh;
36+
}
1437

15-
return mesh;
16-
}
38+
/**
39+
* Iterates over all faces in the mesh and updates their normals in parallel.
40+
* Parallel processing is used to enhance performance for large meshes.
41+
*/
42+
private void updateFaceNormals() {
43+
mesh.faces.parallelStream().forEach(this::updateFaceNormal);
44+
}
45+
46+
/**
47+
* Calculates and updates the normal for a single face.
48+
*
49+
* @param face The face whose normal is to be updated.
50+
*/
51+
private void updateFaceNormal(Face3D face) {
52+
Vector3f normal = mesh.calculateFaceNormal(face);
53+
face.normal.set(normal);
54+
}
55+
56+
/**
57+
* Validates that the provided mesh is not null.
58+
*
59+
* @param mesh The mesh to validate.
60+
* @throws IllegalArgumentException If the mesh is null.
61+
*/
62+
private void validateMesh(Mesh3D mesh) {
63+
if (mesh == null) {
64+
throw new IllegalArgumentException("Mesh cannot be null.");
65+
}
66+
}
67+
68+
/**
69+
* Sets the current mesh for processing.
70+
*
71+
* @param mesh The 3D mesh to set.
72+
*/
73+
private void setMesh(Mesh3D mesh) {
74+
this.mesh = mesh;
75+
}
1776

1877
}

0 commit comments

Comments
 (0)