|
6 | 6 |
|
7 | 7 | /** |
8 | 8 | * A modifier that bends a 3D mesh along the X-axis. |
9 | | - * |
10 | | - * This modifier applies a simple bending deformation to each vertex of the |
11 | | - * given 3D mesh. The degree of bending is controlled by the {@code factor} |
12 | | - * parameter. A higher factor results in a more pronounced bend. Extreme bending |
13 | | - * may distort or cause self-intersection depending on the value of the factor. |
| 9 | + * |
| 10 | + * <p>This modifier applies a simple bending deformation to each vertex of the given 3D mesh. The |
| 11 | + * degree of bending is controlled by the {@code factor} parameter. A higher factor results in a |
| 12 | + * more pronounced bend. Extreme bending may distort or cause self-intersection depending on the |
| 13 | + * value of the factor. |
14 | 14 | */ |
15 | 15 | public class BendModifier implements IMeshModifier { |
16 | 16 |
|
17 | | - /** |
18 | | - * A very small value used to determine if the bending factor is effectively |
19 | | - * zero. |
20 | | - */ |
21 | | - private static final float EPSILON = 1e-7f; |
| 17 | + /** A very small value used to determine if the bending factor is effectively zero. */ |
| 18 | + private static final float EPSILON = 1e-7f; |
22 | 19 |
|
23 | | - /** |
24 | | - * The bending factor determining the degree of bending along the X-axis. |
25 | | - * Default value is 0.5. |
26 | | - */ |
27 | | - private float factor; |
| 20 | + /** |
| 21 | + * The bending factor determining the degree of bending along the X-axis. Default value is 0.5. |
| 22 | + */ |
| 23 | + private float factor; |
28 | 24 |
|
29 | | - /** |
30 | | - * A modifier that bends a mesh along the X-axis. |
31 | | - * |
32 | | - * The bending is controlled by the `factor` parameter. Default is 0.5f, |
33 | | - * introducing a subtle bend. |
34 | | - */ |
35 | | - public BendModifier() { |
36 | | - this(0.5f); |
37 | | - } |
| 25 | + /** |
| 26 | + * A modifier that bends a mesh along the X-axis. |
| 27 | + * |
| 28 | + * <p>The bending is controlled by the `factor` parameter. Default is 0.5f, introducing a subtle |
| 29 | + * bend. |
| 30 | + */ |
| 31 | + public BendModifier() { |
| 32 | + this(0.5f); |
| 33 | + } |
38 | 34 |
|
39 | | - /** |
40 | | - * Constructor to specify a custom bending factor. |
41 | | - * |
42 | | - * @param factor the bending factor controlling the degree of bending. Higher |
43 | | - * values cause more bending, but extreme values can distort the |
44 | | - * mesh. |
45 | | - */ |
46 | | - public BendModifier(float factor) { |
47 | | - this.factor = factor; |
48 | | - } |
| 35 | + /** |
| 36 | + * Constructor to specify a custom bending factor. |
| 37 | + * |
| 38 | + * @param factor the bending factor controlling the degree of bending. Higher values cause more |
| 39 | + * bending, but extreme values can distort the mesh. |
| 40 | + */ |
| 41 | + public BendModifier(float factor) { |
| 42 | + this.factor = factor; |
| 43 | + } |
49 | 44 |
|
50 | | - /** |
51 | | - * Modifies the provided mesh by applying bending to its vertices along the |
52 | | - * X-axis. If the provided mesh contains no vertices, the method safely |
53 | | - * returns the mesh without changes. |
54 | | - * <p> |
55 | | - * The bending is only applied if the {@link #factor} value is valid (greater |
56 | | - * than a small threshold, defined by {@link #EPSILON}). This prevents the |
57 | | - * mesh from being unnecessarily modified when the bending factor is |
58 | | - * negligible and would result in division by zero issues. |
59 | | - * </p> |
60 | | - * |
61 | | - * @param mesh the 3D mesh to bend. Cannot be {@code null}. |
62 | | - * @return the modified mesh after applying bending, or the original mesh if |
63 | | - * no changes are applied. |
64 | | - * @throws IllegalArgumentException if {@code mesh} is null. |
65 | | - */ |
66 | | - @Override |
67 | | - public Mesh3D modify(Mesh3D mesh) { |
68 | | - if (mesh == null) { |
69 | | - throw new IllegalArgumentException("Mesh cannot be null."); |
70 | | - } |
71 | | - if (mesh.vertices.isEmpty()) { |
72 | | - return mesh; |
73 | | - } |
74 | | - if (isFactorValid()) |
75 | | - bend(mesh); |
76 | | - return mesh; |
77 | | - } |
| 45 | + /** |
| 46 | + * Modifies the provided mesh by applying bending to its vertices along the X-axis. If the |
| 47 | + * provided mesh contains no vertices, the method safely returns the mesh without changes. |
| 48 | + * |
| 49 | + * <p>The bending is only applied if the {@link #factor} value is valid (greater than a small |
| 50 | + * threshold, defined by {@link #EPSILON}). This prevents the mesh from being unnecessarily |
| 51 | + * modified when the bending factor is negligible and would result in division by zero issues. |
| 52 | + * |
| 53 | + * @param mesh the 3D mesh to bend. Cannot be {@code null}. |
| 54 | + * @return the modified mesh after applying bending, or the original mesh if no changes are |
| 55 | + * applied. |
| 56 | + * @throws IllegalArgumentException if {@code mesh} is null. |
| 57 | + */ |
| 58 | + @Override |
| 59 | + public Mesh3D modify(Mesh3D mesh) { |
| 60 | + if (mesh == null) { |
| 61 | + throw new IllegalArgumentException("Mesh cannot be null."); |
| 62 | + } |
| 63 | + if (mesh.vertices.isEmpty()) { |
| 64 | + return mesh; |
| 65 | + } |
| 66 | + if (isFactorValid()) bend(mesh); |
| 67 | + return mesh; |
| 68 | + } |
78 | 69 |
|
79 | | - /** |
80 | | - * Performs the bending operation on all vertices of the provided mesh using |
81 | | - * parallel processing. |
82 | | - * |
83 | | - * @param mesh the 3D mesh whose vertices are to be deformed. |
84 | | - */ |
85 | | - private void bend(Mesh3D mesh) { |
86 | | - mesh.vertices.parallelStream().forEach(this::simpleDeformBend); |
87 | | - } |
| 70 | + /** |
| 71 | + * Performs the bending operation on all vertices of the provided mesh using parallel processing. |
| 72 | + * |
| 73 | + * @param mesh the 3D mesh whose vertices are to be deformed. |
| 74 | + */ |
| 75 | + private void bend(Mesh3D mesh) { |
| 76 | + mesh.vertices.parallelStream().forEach(this::simpleDeformBend); |
| 77 | + } |
88 | 78 |
|
89 | | - /** |
90 | | - * Applies the bending transformation to a single vertex based on the bending |
91 | | - * equation. |
92 | | - * |
93 | | - * @param v the vertex to deform. |
94 | | - */ |
95 | | - private void simpleDeformBend(Vector3f v) { |
96 | | - float theta = v.x * factor; |
97 | | - float sinTheta = Mathf.sin(theta); |
98 | | - float cosTheta = Mathf.cos(theta); |
| 79 | + /** |
| 80 | + * Applies the bending transformation to a single vertex based on the bending equation. |
| 81 | + * |
| 82 | + * @param v the vertex to deform. |
| 83 | + */ |
| 84 | + private void simpleDeformBend(Vector3f v) { |
| 85 | + float theta = v.x * factor; |
| 86 | + float sinTheta = Mathf.sin(theta); |
| 87 | + float cosTheta = Mathf.cos(theta); |
99 | 88 |
|
100 | | - float bx = -(v.y - 1.0f / factor) * sinTheta; |
101 | | - float by = (v.y - 1.0f / factor) * cosTheta + 1.0f / factor; |
102 | | - float bz = v.z; |
| 89 | + float bx = -(v.y - 1.0f / factor) * sinTheta; |
| 90 | + float by = (v.y - 1.0f / factor) * cosTheta + 1.0f / factor; |
| 91 | + float bz = v.z; |
103 | 92 |
|
104 | | - v.set(bx, by, bz); |
105 | | - } |
| 93 | + v.set(bx, by, bz); |
| 94 | + } |
106 | 95 |
|
107 | | - /** |
108 | | - * Checks if the bending factor is valid (i.e., not effectively zero). |
109 | | - * |
110 | | - * @return {@code true} if the factor is a valid number for bending, |
111 | | - * {@code false} otherwise. |
112 | | - */ |
113 | | - private boolean isFactorValid() { |
114 | | - return Mathf.abs(factor) > EPSILON; |
115 | | - } |
| 96 | + /** |
| 97 | + * Checks if the bending factor is valid (i.e., not effectively zero). |
| 98 | + * |
| 99 | + * @return {@code true} if the factor is a valid number for bending, {@code false} otherwise. |
| 100 | + */ |
| 101 | + private boolean isFactorValid() { |
| 102 | + return Mathf.abs(factor) > EPSILON; |
| 103 | + } |
116 | 104 |
|
117 | | - /** |
118 | | - * Gets the current bending factor value. |
119 | | - * |
120 | | - * @return the bending factor value. |
121 | | - */ |
122 | | - public float getFactor() { |
123 | | - return factor; |
124 | | - } |
125 | | - |
126 | | - /** |
127 | | - * Sets the bending factor to a new value. |
128 | | - * |
129 | | - * @param factor the new bending factor value. Higher values result in more |
130 | | - * bending. |
131 | | - */ |
132 | | - public void setFactor(float factor) { |
133 | | - this.factor = factor; |
134 | | - } |
| 105 | + /** |
| 106 | + * Gets the current bending factor value. |
| 107 | + * |
| 108 | + * @return the bending factor value. |
| 109 | + */ |
| 110 | + public float getFactor() { |
| 111 | + return factor; |
| 112 | + } |
135 | 113 |
|
| 114 | + /** |
| 115 | + * Sets the bending factor to a new value. |
| 116 | + * |
| 117 | + * @param factor the new bending factor value. Higher values result in more bending. |
| 118 | + */ |
| 119 | + public void setFactor(float factor) { |
| 120 | + this.factor = factor; |
| 121 | + } |
136 | 122 | } |
0 commit comments