forked from miho/JCSG
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTextExtrude.java
More file actions
292 lines (245 loc) · 8.38 KB
/
TextExtrude.java
File metadata and controls
292 lines (245 loc) · 8.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package eu.mihosoft.vrl.v3d;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import javafx.scene.shape.ClosePath;
import javafx.scene.shape.CubicCurveTo;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.PathElement;
import javafx.scene.shape.QuadCurveTo;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import com.piro.bezier.BezierPath;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.paint.Color;
import javafx.scene.shape.Path;
import javafx.scene.shape.PathElement;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.CubicCurveTo;
import javafx.scene.shape.QuadCurveTo;
import javafx.scene.shape.ClosePath;
import javafx.scene.text.Text;
import eu.mihosoft.vrl.v3d.Vector3d;
import java.util.*;
// Auto-generated Javadoc
/**
* The Class Text.
*/
@SuppressWarnings("restriction")
public class TextExtrude {
private static final String default_font = "FreeSerif";
private final static int POINTS_CURVE = 10;
private static final double CURVE_SEGMENTS = 4; // Number of segments to approximate curves
private final String text;
// private List<Vector3d> points;
// HashSet<Vector3d> unique = new HashSet<Vector3d>();
private Vector3d p0;
private final List<LineSegment> polis = new ArrayList<>();
ArrayList<CSG> sections = new ArrayList<CSG>();
ArrayList<CSG> holes = new ArrayList<CSG>();
private double dir;
class LineSegment {
/*
* Given one single character in terms of Path, LineSegment stores a list of
* points that define the exterior of one of its polygons (!isHole). It can
* contain reference to one or several holes inside this polygon. Or it can
* define the perimeter of a hole (isHole), with no more holes inside.
*/
private boolean hole;
private List<Vector3d> points;
private Path path;
private Vector3d origen;
private List<LineSegment> holes = new ArrayList<>();
private String letter;
public LineSegment(String text) {
letter = text;
}
public String getLetter() {
return letter;
}
public void setLetter(String letter) {
this.letter = letter;
}
public boolean isHole() {
return hole;
}
public void setHole(boolean isHole) {
this.hole = isHole;
}
public List<Vector3d> getPoints() {
return points;
}
public void setPoints(List<Vector3d> points) {
this.points = points;
}
public Path getPath() {
return path;
}
public void setPath(Path path) {
this.path = path;
}
public Vector3d getOrigen() {
return origen;
}
public void setOrigen(Vector3d origen) {
this.origen = origen;
}
public List<LineSegment> getHoles() {
return holes;
}
public void setHoles(List<LineSegment> holes) {
this.holes = holes;
}
public void addHole(LineSegment hole) {
holes.add(hole);
}
@Override
public String toString() {
return "Poly{" + "points=" + points + ", path=" + path + ", origen=" + origen + ", holes=" + holes + '}';
}
}
private TextExtrude(String text, Font font, double dir) {
if (dir <= 0)
throw new NumberFormatException("length can not be negative");
this.dir = dir;
// points = new ArrayList<>();
this.text = text;
Text textNode = new Text(text);
textNode.setFont(font);
// Convert Text to Path
Path subtract = (Path) (Shape.subtract(textNode, new Rectangle(0, 0)));
List<List<Vector3d>> outlines = extractOutlines(subtract,font.getSize());
double zOff = 0;
// boolean b = CSG.isPreventNonManifoldTriangles();
// CSG.setPreventNonManifoldTriangles(false);
for (List<Vector3d> points : outlines) {
try {
boolean hole = Extrude.isCCWv3d(points);
CSG newLetter = Extrude.points(new Vector3d(0, 0, dir), points).movez(zOff);
//newLetter.triangulate();
if (!hole)
sections.add(newLetter);
else {
newLetter.setIsHole(true);
holes.add(newLetter);
}
}catch(ColinearPointsException e) {
e.printStackTrace();
}
}
// CSG.setPreventNonManifoldTriangles(b);
// // Convert Path elements into lists of points defining the perimeter
// // (exterior or interior)
// subtract.getElements().forEach(this::getPoints);
for (int i = 0; i < sections.size(); i++) {
sections.get(i).setIsHole(false);
for (CSG h : holes) {
try {
if (sections.get(i).isBoundsTouching(h)) {
// println "Hole found "
CSG nl = sections.get(i).difference(h).setIsHole(false);
sections.set(i, nl);
}
} catch (Exception e) {
}
}
}
}
/**
* Extrudes the specified path (convex or concave polygon without holes or
* intersections, specified in CCW) into the specified direction.
*
* @param dir direction of extrusion
* @param text text
* @param font font configuration of the text
*
* @return a CSG object that consists of the extruded polygon
*/
@SuppressWarnings("restriction")
public static ArrayList<CSG> text(double dir, String text, Font font) {
TextExtrude te = new TextExtrude(text, font, dir);
return te.sections;
}
public List<LineSegment> getLineSegment() {
return polis;
}
public List<Vector3d> getOffset() {
return polis.stream().sorted((p1, p2) -> (int) (p1.getOrigen().x - p2.getOrigen().x))
.map(LineSegment::getOrigen).collect(Collectors.toList());
}
// Below is AI slop
//private static final double POINT_EPSILON = 0.0001; // Distance threshold for considering points equal
/**
* Converts a JavaFX Text object into a list of cleaned vector lists
* representing the outlines
* @param fontSize
*/
public static List<List<Vector3d>> extractOutlines(Path text, double fontSize) {
List<List<Vector3d>> rawOutlines = extractRawOutlines(text);
// List<List<Vector3d>> cleanedOutlines = new ArrayList<>();
//
// for (List<Vector3d> outline : rawOutlines) {
// List<Vector3d> cleaned = cleanOutline(outline,fontSize);
// if (cleaned.size() >= 3) { // Only keep outlines with at least 3 points
// cleanedOutlines.add(cleaned);
// }
// }
return rawOutlines;
}
/**
* Initial extraction of outlines from text
*/
private static List<List<Vector3d>> extractRawOutlines(Path textPath) {
List<List<Vector3d>> allOutlines = new ArrayList<>();
StringBuilder pathBuilder = new StringBuilder();
//List<Vector3d> currentPath = new ArrayList<>();
for (PathElement element : textPath.getElements()) {
if (element instanceof MoveTo) {
// If we have a current path, process it with BezierPath
if (pathBuilder.length() > 0) {
BezierPath bezierPath = new BezierPath(5);
bezierPath.parsePathString(pathBuilder.toString());
List<Vector3d> pathPoints = bezierPath.evaluate();
if (!pathPoints.isEmpty()) {
allOutlines.add(new ArrayList<>(pathPoints));
}
pathBuilder = new StringBuilder();
}
MoveTo move = (MoveTo) element;
pathBuilder.append("M").append(move.getX()).append(" ").append(move.getY());
} else if (element instanceof LineTo) {
LineTo line = (LineTo) element;
pathBuilder.append("L").append(line.getX()).append(" ").append(line.getY());
} else if (element instanceof CubicCurveTo) {
CubicCurveTo curve = (CubicCurveTo) element;
pathBuilder.append("C")
.append(curve.getControlX1()).append(" ").append(curve.getControlY1()).append(" ")
.append(curve.getControlX2()).append(" ").append(curve.getControlY2()).append(" ")
.append(curve.getX()).append(" ").append(curve.getY());
} else if (element instanceof QuadCurveTo) {
QuadCurveTo curve = (QuadCurveTo) element;
pathBuilder.append("Q")
.append(curve.getControlX()).append(" ").append(curve.getControlY()).append(" ")
.append(curve.getX()).append(" ").append(curve.getY());
}
}
// Process the final path if it exists
if (pathBuilder.length() > 0) {
BezierPath bezierPath = new BezierPath(5);
bezierPath.parsePathString(pathBuilder.toString());
List<Vector3d> pathPoints = bezierPath.evaluate();
if (!pathPoints.isEmpty()) {
allOutlines.add(pathPoints);
}
}
return allOutlines;
}
}