|
| 1 | +package mesh.modifier; |
| 2 | + |
| 3 | +import mesh.Mesh3D; |
| 4 | + |
| 5 | +/** |
| 6 | + * Transforms a 3D mesh into a wireframe-like structure by creating holes and |
| 7 | + * solidifying the remaining geometry. This is a pseudo-wireframe modifier, |
| 8 | + * differing from traditional wireframe modifiers. |
| 9 | + * <p> |
| 10 | + * Traditional wireframe modifiers, such as the one in Blender, replace the |
| 11 | + * edges of the mesh with cylindrical or rectangular struts, directly converting |
| 12 | + * the mesh's edges into a visible wireframe. In contrast, this pseudo-wireframe |
| 13 | + * approach combines a hole-creation process with solidification to approximate |
| 14 | + * the effect, maintaining the original surface structure with modifications. |
| 15 | + * </p> |
| 16 | + */ |
| 17 | +public class PseudoWireframeModifier implements IMeshModifier { |
| 18 | + |
| 19 | + private static final float DEFAULT_HOLE_PECENTAGE = 0.9f; |
| 20 | + |
| 21 | + private static final float DEFAULT_THICKNESS = 0.02f; |
| 22 | + |
| 23 | + private float holePercentage; |
| 24 | + |
| 25 | + private float thickness; |
| 26 | + |
| 27 | + private Mesh3D mesh; |
| 28 | + |
| 29 | + /** |
| 30 | + * Constructs a new instance of the PseudoWireframeModifier with default |
| 31 | + * parameters. The default hole percentage is set to 0.9, and the default |
| 32 | + * solidify thickness is 0.02. |
| 33 | + */ |
| 34 | + public PseudoWireframeModifier() { |
| 35 | + holePercentage = DEFAULT_HOLE_PECENTAGE; |
| 36 | + thickness = DEFAULT_THICKNESS; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Modifies the provided mesh to create a pseudo-wireframe-like effect by |
| 41 | + * creating holes and applying solidification. |
| 42 | + * |
| 43 | + * @param mesh The 3D mesh to modify. |
| 44 | + * @return The modified 3D mesh with pseudo-wireframe-like transformation |
| 45 | + * applied. |
| 46 | + */ |
| 47 | + @Override |
| 48 | + public Mesh3D modify(Mesh3D mesh) { |
| 49 | + validateMesh(mesh); |
| 50 | + setMesh(mesh); |
| 51 | + createHoles(); |
| 52 | + solidify(); |
| 53 | + return mesh; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Creates holes in the mesh by applying a custom extrusion modifier configured |
| 58 | + * to act as a "hole-creation" operation. |
| 59 | + */ |
| 60 | + private void createHoles() { |
| 61 | + mesh.apply(createExtrudeModifier()); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Creates a configured instance of an ExtrudeModifier to perform the |
| 66 | + * hole-creation effect. |
| 67 | + * |
| 68 | + * @return An instance of ExtrudeModifier configured with the current hole |
| 69 | + * percentage. |
| 70 | + */ |
| 71 | + private ExtrudeModifier createExtrudeModifier() { |
| 72 | + ExtrudeModifier extrude = new ExtrudeModifier(); |
| 73 | + extrude.setScale(holePercentage); |
| 74 | + extrude.setAmount(0); |
| 75 | + extrude.setRemoveFaces(true); |
| 76 | + return extrude; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Solidifies the remaining geometry after the hole-creation step by applying a |
| 81 | + * solidification modifier with the defined thickness. |
| 82 | + */ |
| 83 | + private void solidify() { |
| 84 | + mesh.apply(new SolidifyModifier(thickness)); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Validates that the provided mesh is not null before processing. |
| 89 | + * |
| 90 | + * @param mesh The mesh to validate. |
| 91 | + * @throws IllegalArgumentException If the provided mesh is null. |
| 92 | + */ |
| 93 | + private void validateMesh(Mesh3D mesh) { |
| 94 | + if (mesh == null) { |
| 95 | + throw new IllegalArgumentException("Mesh cannot be null."); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Sets the current mesh instance for transformation operations. |
| 101 | + * |
| 102 | + * @param mesh The 3D mesh to set. |
| 103 | + */ |
| 104 | + private void setMesh(Mesh3D mesh) { |
| 105 | + this.mesh = mesh; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Retrieves the current hole percentage value used for the pseudo-wireframe |
| 110 | + * effect. |
| 111 | + * |
| 112 | + * @return The current hole percentage value. |
| 113 | + */ |
| 114 | + public float getHolePercentage() { |
| 115 | + return holePercentage; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Sets the percentage of the "holes" to apply on the mesh. Value must be |
| 120 | + * between 0 and 1. |
| 121 | + * |
| 122 | + * @param holePercentage The new hole percentage value. |
| 123 | + * @throws IllegalArgumentException If the value is less than 0 or greater than |
| 124 | + * 1. |
| 125 | + */ |
| 126 | + public void setHolePercentage(float holePercentage) { |
| 127 | + if (holePercentage < 0 || holePercentage > 1) { |
| 128 | + throw new IllegalArgumentException("Hole percentage must be between 0 and 1."); |
| 129 | + } |
| 130 | + this.holePercentage = holePercentage; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Retrieves the current thickness value used for solidification. |
| 135 | + * |
| 136 | + * @return The current solidify thickness value. |
| 137 | + */ |
| 138 | + public float getThickness() { |
| 139 | + return thickness; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Sets the solidify thickness value for the mesh transformation. Thickness must |
| 144 | + * be positive. |
| 145 | + * |
| 146 | + * @param thickness The new thickness value. |
| 147 | + * @throws IllegalArgumentException If the value is less than or equal to 0. |
| 148 | + */ |
| 149 | + public void setThickness(float thickness) { |
| 150 | + if (thickness <= 0) { |
| 151 | + throw new IllegalArgumentException("Thickness must be greater than zero."); |
| 152 | + } |
| 153 | + this.thickness = thickness; |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments