-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextCurves.java
More file actions
683 lines (609 loc) · 21.7 KB
/
TextCurves.java
File metadata and controls
683 lines (609 loc) · 21.7 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
package apps;
import algebra.Vec2;
import algebra.Vec3;
import algebra.Vector;
import apps.utils.MyFrame;
import apps.utils.TextFrame;
import nlp.lowbow.simpleLowBow.*;
import nlp.symbolSampler.SymbolSampler;
import nlp.symbolSampler.TopKSymbol;
import nlp.textSplitter.SpaceSplitter;
import numeric.Camera3D;
import numeric.MyMath;
import twoDimEngine.TwoDimEngine;
import twoDimEngine.elements.Line2D;
import twoDimEngine.elements.String2D;
import twoDimEngine.shaders.PaintMethod2D;
import twoDimEngine.shaders.ThickLineShader;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* @author pedro
*/
public class TextCurves extends MyFrame implements MouseWheelListener {
private static final TextFrame HELP_FRAME = TextFrame.builder()
.addLine("<left mouse> to change camera / move camera")
.addLine("<right mouse> zoom in/out (pca mode)")
.buildWithTitle("Help");
private TwoDimEngine engine;
private PaintMethod2D shader;
/**
* mouse coordinates
*/
private int mx, my, newMx, newMy;
/**
* User Interface crap
*/
private TextArea inOut;
private Button button;
private JFrame input;
private TextField sigmaText;
private JSlider samplesSlider;
private Label samplesSliderTxt;
private double samplesIn;
private double samplesInMin;
private double samplesInMax;
private Checkbox txtVisibleCheckBox;
private Label frameState;
private Button simplexOnPolyButton;
private JSlider heatLambdaSlider;
private double lambdaIn;
private double lambdaInMin;
private double lambdaInMax;
private Label lambdaSliderTxt;
private Choice flowSolverChoice;
/**
* locally weighted bag of wordsIndex
*/
private LowBowManagerPca lowbowM;
/**
* read a text
*/
private boolean isReady;
/**
* processed the text: lowbow representation and pca computation done
*/
private boolean isProcess;
/**
* Camera
*/
private Camera3D camera;
/**
* pca curve projected in the plane
*/
private ArrayList<Vec2[]> projCurves;
private ArrayList<Line2D[]> projLines;
/**
* pca curve statistics
*/
private Vec3 centerMass;
private double stdev;
/**
* text curve
*/
private ArrayList<String2D[]> strCurves;
/**
* state that determines when to draw simplex on polygon
* state = 0 : pca
* state = 1 : drawBarycentric
* state = 2 : dont draw
*/
private int simplexOnPolyState = 1;
/**
* polygon where the curve will be drawn
*/
private Vec2[] polygon;
private HeatMethod heatMethod = new MatrixHeatFlow();
private SymbolSampler symbolSampler = new TopKSymbol(1);
enum FlowsEnum {
SPARSE("Sparse", new SparseHeatFlow()),
MATRIX("Matrix", new MatrixHeatFlow()),
KERNEL("Kernel", new HeatKernelFlow()),
GRAPH_LAPLACIAN("GraphLaplacian", new GraphLaplacianFlow()),
HEAT_EXPONENTIAL("HeatExponential", new HeatFlow()),
EIGEN_HEAT("EigenHeat", new LaplacianEigenFlow());
private HeatMethod heatFlow;
private String flowName;
private static Map<String, FlowsEnum> flowsByNameMap = new HashMap<>(FlowsEnum.values().length);
static {
for (FlowsEnum flowsEnum : FlowsEnum.values()) {
flowsByNameMap.put(flowsEnum.flowName, flowsEnum);
}
}
FlowsEnum(String flowName, HeatMethod heatFlow) {
this.flowName = flowName;
this.heatFlow = heatFlow;
}
public static HeatMethod getHeatMethodFromName(String name) {
return flowsByNameMap.get(name).heatFlow;
}
}
public TextCurves(String title, int width, int height) {
super(title, width, height);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
engine = new TwoDimEngine(width, height);
shader = new ThickLineShader(engine);
engine.setBackGroundColor(Color.white);
engine.setCamera(-1, 1, -1, 1);
/**
* GUI crap
*/
input = new JFrame("Input text File");
input.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
input.setLayout(new GridLayout(1, 2));
Panel p1 = new Panel(new GridLayout(7, 1));
input.setResizable(true);
input.setSize((int) Math.floor(0.6 * width), (int) Math.floor(0.3 * width));
input.setLocation(width, 0);
/**
* Text area
*/
inOut = new TextArea();
inOut.setText("Hello , please input some text here.\n For example a c c c b b a c c");
input.add(inOut);
/**
* buttons
*/
Panel pbuttons = new Panel(new GridLayout(1, 2));
button = new Button("Load");
button.addActionListener(new myActionListener(this));
Button addButton = new Button("Reset");
addButton.addActionListener(e -> reset());
pbuttons.add(button);
pbuttons.add(addButton);
p1.add(pbuttons);
/**
* samples text
*/
Panel p2 = new Panel(new GridLayout(1, 2));
samplesInMin = 1.0;
samplesInMax = 10.0;
samplesIn = samplesInMin;
samplesSliderTxt = new Label("samples/txtLength:" + samplesIn);
p2.add(samplesSliderTxt);
samplesSlider = new JSlider(0, 9, 0);
samplesSlider.addChangeListener(e -> {
JSlider aux = (JSlider) e.getSource();
samplesIn = samplesInMin + ((samplesInMax - samplesInMin) / (aux.getMaximum())) * (aux.getValue());
samplesSliderTxt.setText("samples/txtLength:" + samplesIn);
});
p2.add(samplesSlider);
p1.add(p2);
/**
* sigma text
*/
Panel p3 = new Panel(new GridLayout(1, 2));
p3.add(new Label("Sigma[> 0]: "));
sigmaText = new TextField();
sigmaText.setText("auto");
p3.add(sigmaText);
p1.add(p3);
/**
* check box
*/
Panel p4 = new Panel(new GridLayout(1, 2));
frameState = new Label("");
p4.add(frameState);
txtVisibleCheckBox = new Checkbox("Text visible");
p4.add(txtVisibleCheckBox);
p1.add(p4);
/**
* generate text
*/
Panel p5 = new Panel(new GridLayout(1, 2));
Button generateButton = new Button("Generate");
generateButton.addActionListener(e -> generateText());
/**
* Poly AbstractSimplex
*/
simplexOnPolyButton = new Button("PCA");
simplexOnPolyButton.addActionListener(e -> {
if (simplexOnPolyButton.getLabel().equals("PCA")) {
simplexOnPolyState = 0;
buildPca(true);
} else {
simplexOnPolyState = 1;
}
if (simplexOnPolyState == 1 || simplexOnPolyState == 2) {
simplexOnPolyButton.setLabel("PCA");
engine.setCamera(-1, 1, -1, 1);
} else {
simplexOnPolyButton.setLabel("Barycentric");
}
});
p5.add(simplexOnPolyButton);
p5.add(generateButton);
p1.add(p5);
/**
* heat flow button
*/
Panel p6 = new Panel(new GridLayout(1, 3));
lambdaInMin = 0.0001;
lambdaInMax = 0.9999;
lambdaIn = lambdaInMin;
lambdaSliderTxt = new Label("" + lambdaIn);
heatLambdaSlider = new JSlider(0, 99, 0);
heatLambdaSlider.addChangeListener(e -> {
JSlider aux = (JSlider) e.getSource();
lambdaIn = lambdaInMin + ((lambdaInMax - lambdaInMin) / (aux.getMaximum())) * (aux.getValue());
lambdaSliderTxt.setText("" + lambdaIn);
});
Button heatFlowButton = new Button("Heat Flow");
heatFlowButton.addActionListener(e -> {
HeatMethod heat = heatMethod;
heatFlow(lambdaIn, heat);
});
p6.add(lambdaSliderTxt);
p6.add(heatLambdaSlider);
p6.add(heatFlowButton);
p1.add(p6);
Panel p7 = new Panel(new GridLayout(1, 3));
p7.add(new Label(""));
p7.add(new Label(""));
flowSolverChoice = new Choice();
for (FlowsEnum flowsEnum : FlowsEnum.values()) {
flowSolverChoice.add(flowsEnum.flowName);
}
flowSolverChoice.addItemListener(e -> this.heatMethod = FlowsEnum.getHeatMethodFromName(flowSolverChoice.getSelectedItem()));
flowSolverChoice.select("Matrix");
p7.add(flowSolverChoice);
p1.add(p7);
input.add(p1);
isReady = false;
isProcess = false;
input.setVisible(true);
camera = new Camera3D();
projCurves = new ArrayList<>();
projLines = new ArrayList<>();
strCurves = new ArrayList<>();
lowbowM = new LowBowManagerPca();
centerMass = new Vec3();
stdev = 0;
this.addMouseWheelListener(this);
}
public static void main(String[] args) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
new TextCurves("Teste", (int) (screenSize.getWidth() * 0.60), (int) (screenSize.getHeight() * 0.75));
}
private void buildPca(boolean isUpdate) {
frameState.setText("build pca");
lowbowM.buildPca();
if (isUpdate) {
updateCurveStats();
}
frameState.setText("");
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_H:
HELP_FRAME.setVisible(true);
break;
default:
break;
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
mx = e.getX();
my = e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
Vec3 thrust = camera.getThrust();
thrust.setX(0);
thrust.setY(0);
thrust.setZ(0);
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent e) {
newMx = e.getX();
newMy = e.getY();
double dx = newMx - mx;
double dy = newMy - my;
Vec3 raw = camera.getRaw();
Vec3 thrust = camera.getThrust();
double power = 40 * stdev;
if (SwingUtilities.isLeftMouseButton(e)) {
raw.setY(raw.getY() + 2 * Math.PI * (dx / widthChanged));
raw.setZ(raw.getZ() + 2 * Math.PI * (dy / heightChanged));
thrust.setY(power * 2 * Math.PI * (dx / widthChanged));
thrust.setZ(power * 2 * Math.PI * (dy / heightChanged));
} else {
raw.setX(raw.getX() + (dx / widthChanged) + (dy / heightChanged));
thrust.setX(power * ((dx / widthChanged) + (dy / heightChanged)));
}
if (simplexOnPolyState > 0) {
double h = -2 * (dx / widthChanged);
double k = 2 * (dy / heightChanged);
engine.setCamera(engine.getXmin() + h, engine.getXmax() + h, engine.getYmin() + k, engine.getYmax() + k);
}
mx = newMx;
my = newMy;
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void reShape() {
engine.setImageSize(widthChanged, heightChanged);
}
/**
* update projected curve
*/
private void updateCurvePca() {
/**
* distance to drawing plane
*/
double d = 1;
ArrayList<LowBowPca> l = lowbowM.getDocModels();
int n = l.size();
for (int i = 0; i < n; i++) {
Vec3[] pcaCurve = l.get(i).getPcaCurve();
Vec2[] projCurve = projCurves.get(i);
Line2D[] projLine = projLines.get(i);
/**
* Projective projection
*/
for (int j = 0; j < pcaCurve.length; j++) {
Vec3 u = new Vec3(pcaCurve[j].getX(), pcaCurve[j].getY(), pcaCurve[j].getZ());
Vec3 v = Vec3.diff(u, camera.getEye());
v = Vec3.matrixProd(camera.getInverseCamBasis(), v);
int boundj = Math.min(j, projLine.length - 1);
if (v.getZ() < (d / 1000)) {
projLine[boundj].setVisible(false);
} else {
projLine[boundj].setVisible(true);
projCurve[j].setX(v.getX() * (d / v.getZ()));
projCurve[j].setY(v.getY() * (d / v.getZ()));
}
}
}
}
private void updateCurveOnPoly() {
ArrayList<LowBowPca> l = lowbowM.getDocModels();
int n = l.size();
int wordL = l.get(0).getNumWords();
if ((polygon == null) || (polygon.length != wordL)) {
buildPoly(wordL);
}
for (int i = 0; i < n; i++) {
Vector[] curve = l.get(i).getCurve();
Vec2[] projCurve = projCurves.get(i);
Line2D[] projLine = projLines.get(i);
/**
* Barycentric coordinates to world coordinates calculation
*/
for (int j = 0; j < projCurve.length; j++) {
Vec2 acm = new Vec2();
for (int k = 0; k < wordL; k++) {
acm = Vec2.add(acm, Vec2.scalarProd(curve[j].getX(k + 1), polygon[k]));
}
int boundj = Math.min(j, projLine.length - 1);
projLine[boundj].setVisible(true);
projCurve[j].setX(acm.getX());
projCurve[j].setY(acm.getY());
}
}
simplexOnPolyState = 2;
}
private void buildPoly(int wordL) {
polygon = new Vec2[wordL];
double t = 0;
double step = (2 * Math.PI) / wordL;
for (int i = 0; i < wordL; i++) {
polygon[i] = new Vec2(Math.cos(t), Math.sin(t));
t += step;
}
}
private void updateStringCurve() {
ArrayList<LowBowPca> l = lowbowM.getDocModels();
int n = l.size();
for (int i = 0; i < n; i++) {
String2D[] strCurve = strCurves.get(i);
LowBow lowbow = l.get(i);
Vec2[] projCurve = projCurves.get(i);
Line2D[] projLine = projLines.get(i);
for (int j = 0; j < strCurve.length; j++) {
int tLength = lowbow.getTextLength();
int samples = lowbow.getSamples();
double s = (samples - 1.0) / (tLength - 1.0);
int k = (int) Math.floor(MyMath.clamp(s * j, 0, samples - 1));
if (txtVisibleCheckBox.getState() && projLine[i].isVisible()) {
strCurve[j].setVisible(true);
strCurve[j].setVertex(projCurve[k], 0);
strCurve[j].setString(symbolSampler.nextSymbol(lowbow.getCurve()[k], lowbow.getSimplex()));
} else {
strCurve[j].setVisible(false);
}
}
}
}
/**
* update pca curve statistics and update camera.
*/
private void updateCurveStats() {
ArrayList<LowBowPca> lowbows = lowbowM.getDocModels();
LowBowPca lowbow = lowbows.get(lowbows.size() - 1);
int n = lowbow.getPcaCurve().length;
Vec3[] pcaCurve = lowbow.getPcaCurve();
for (int i = 0; i < n; i++) {
centerMass = Vec3.add(centerMass, pcaCurve[i]);
}
centerMass = Vec3.scalarProd(1.0 / n, centerMass);
double stdevAcm = 0;
for (int i = 0; i < n; i++) {
Vec3 aux = Vec3.diff(pcaCurve[i], centerMass);
stdevAcm += aux.squareNorm();
}
stdev = Math.max(Math.sqrt(stdevAcm), stdev);
camera.setFocalPoint(centerMass);
Vec3 raw = camera.getRaw();
double percent = 1.10;
camera.setRaw(new Vec3(percent * stdev, raw.getY(), raw.getZ()));
}
@Override
public void updateDraw() {
engine.clearImageWithBackground();
camera.update(dt);
if (simplexOnPolyState == 1) {
updateCurveOnPoly();
} else if (simplexOnPolyState == 0) {
updateCurvePca();
} else {
}
updateStringCurve();
engine.drawElements();
this.setTitle("Fps : " + this.getFps());
drawImage(engine.getImage());
}
public void reset() {
this.setVisible(false);
lowbowM.removeAll();
engine.removeAllElements();
projCurves.removeAll(projCurves);
projLines.removeAll(projLines);
strCurves.removeAll(strCurves);
centerMass = new Vec3();
stdev = 0;
}
public void addCurveToEngine(LowBow lowbow) {
Color color = Color.getHSBColor((float) Math.random(), 1.0f, 1.0f);
int randSamples = (int) Math.round(Math.random() * 5);
for (int i = 0; i < randSamples; i++) {
color = Color.getHSBColor((float) Math.random(), 1.0f, 1.0f);
}
int n = lowbow.getSamples();
Vec2[] projCurve = new Vec2[n];
Line2D[] projLine = new Line2D[n - 1];
for (int i = 0; i < n - 1; i++) {
projCurve[i] = (projCurve[i] == null) ? new Vec2() : projCurve[i];
projCurve[i + 1] = new Vec2();
projLine[i] = new Line2D(projCurve[i], projCurve[i + 1]);
projLine[i].setColor(color);
engine.addtoList(projLine[i], shader);
}
projLines.add(projLine);
projCurves.add(projCurve);
String[] text = lowbow.getText();
String2D[] strCurve = new String2D[text.length];
for (int i = 0; i < text.length; i++) {
strCurve[i] = new String2D(new Vec2(), text[i]);
strCurve[i].setColor(Color.blue);
strCurve[i].setVisible(false);
engine.addtoList(strCurve[i]);
}
strCurves.add(strCurve);
}
public void generateText() {
ArrayList<LowBowPca> lowList = lowbowM.getDocModels();
if (lowList == null) {
return;
}
LowBow low = lowList.get(lowList.size() - 1);
new TextFrame("Generated Text", low.generateText(symbolSampler)).setVisible(true);
}
public void pcaCoords() {
ArrayList<LowBowPca> low = lowbowM.getDocModels();
LowBowPca curve = low.get(low.size() - 1);
Vec3[] pca = curve.getPcaCurve();
String[] text = curve.getText();
for (int i = 0; i < text.length; i++) {
String string = text[i];
int k = (int) Math.floor(MyMath.clamp(curve.getSamplesPerTextLength() * i, 0, curve.getSamples()));
System.out.println(string + "\t" + pca[k].getX() + "\t" + pca[k].getY() + "\t" + pca[k].getZ());
}
}
public void heatFlow(double lambda, HeatMethod heat) {
ArrayList<LowBowPca> low = lowbowM.getDocModels();
LowBow curve = low.get(low.size() - 1);
frameState.setText("Computing heat flow");
curve.heatFlow(lambda, heat);
frameState.setText("");
if (simplexOnPolyState == 0) {
buildPca(false);
}
if (simplexOnPolyState == 2) {
simplexOnPolyState = 1;
}
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
int mRotation = e.getWheelRotation();
if (simplexOnPolyState > 0) {
double percent = 0.1;
double sizeX = engine.getXmax() - engine.getXmin();
double sizeY = engine.getYmax() - engine.getYmin();
engine.setCamera(engine.getXmin() - mRotation * percent * 0.5 * sizeX, engine.getXmax() + mRotation * percent * 0.5 * sizeX, engine.getYmin() - mRotation * percent * 0.5 * sizeY, engine.getYmax() + mRotation * percent * 0.5 * sizeY);
}
}
public class myActionListener implements ActionListener {
JFrame frame;
public myActionListener(JFrame frame) {
this.frame = frame;
}
@Override
public void actionPerformed(ActionEvent e) {
boolean sigmaAuto = false;
frameState.setText("Loading");
if (frame.isVisible()) {
frame.setVisible(false);
}
String inString = inOut.getText();
// LowBowPca lowbow = new LowBowPca(inString, new MyTextSplitter());
LowBowPca lowbow = new LowBowPca(inString, new SpaceSplitter());
isReady = true;
double sigma = 0.02;
try {
sigma = Double.parseDouble(sigmaText.getText());
} catch (NumberFormatException e1) {
if (sigmaText.getText().toLowerCase().equals("auto")) {
sigmaAuto = true;
}
}
lowbow.setSamplesPerTextLength(samplesIn);
if (sigmaAuto) {
sigma = 1.0 / (2 * lowbow.getTextLength());
}
lowbow.setSigma(sigma);
lowbow.setSmoothingCoeff(0.01);
lowbowM.add(lowbow);
frameState.setText("Processing");
lowbowM.build();
isReady = false;
isProcess = true;
ArrayList<LowBowPca> lLowBow = lowbowM.getDocModels();
addCurveToEngine(lLowBow.get(lLowBow.size() - 1));
frameState.setText("");
frame.setVisible(true);
if (simplexOnPolyState == 2) {
simplexOnPolyState = 1;
}
}
}
}