-
Notifications
You must be signed in to change notification settings - Fork 7
/
SkyscrapAR.pde
544 lines (457 loc) · 13.1 KB
/
SkyscrapAR.pde
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
//String INPUT_FILENAME = "awatility.xml";
//String INPUT_FILENAME = "prefuse.xml";
String INPUT_FILENAME = "junit.xml";
/*
TODO list
=========
- no XML, atributo lastVersion pode estar errado
- no tweening, algumas classes vem de baixo.
- busca textual
- regua mostra versoes em que classes highlighted foram modificadas
- ver se nexttext possui texto com outline, que é melhor que texto com shadow que estou usando
DONE
====
- Use churn as height
- height = sum(churns) from v[i+1] to v[j] minus churn(v[i])
- Highlight classes that were changed in the last version
- Also: classes changed between first and last version
- Use tweening to animate version change
- Take greater height as a reference height
- Write name of hover class on some kind of title bar
- Speak class name (split words with hyphen)
- Maybe try mrbola speech (Didn't work, only noise is produced).
*/
////////////////////////////////////////////////////////
/////////// Configuration Variables ////////////////////
////////////////////////////////////////////////////////
int THRESHOLD = 80; //45; //85; //110;
double CONFIDENCE_THRESHOLD = 0.51; // default: 0.51
boolean DEBUG = false;
boolean USE_OPENGL = false;
boolean USE_CAM = true;
int WINDOW_WIDTH = 1000; //640;
int WINDOW_HEIGHT = 750; //480;
boolean PERSISTENT_TREEMAP = false;
boolean DRAW_MODELS = true;
boolean FLIPPED_CAM = false;
int TREEMAP_WIDTH = 100;
int TREEMAP_HEIGHT = TREEMAP_WIDTH;
boolean HIDE_NON_SELECTED = false;
color PACKAGE_MIN_COLOR = #000000;
color PACKAGE_MAX_COLOR = #FFFFFF;
color CLASS_FLOOR_COLOR = #009900;
color CLASS_CHANGED_COLOR = #990000;
color CLASS_DEFAULT_COLOR = #CCCCCC;
color CLASS_HIGHLIGHT_COLOR = #FFFF99;
color FLOOR_COLOR = #000000;
color TEXT_BG_COLOR = #000000;
color TEXT_FG_COLOR = #FFFFFF;
double PACKAGE_HEIGHT = 2.0;
double PACKAGE_BASE_RATIO = 0.90;
double CLASS_BASE_RATIO = 0.70;
double CLASS_MIN_HEIGHT = 10.0;
double CLASS_MAX_HEIGHT = (TREEMAP_WIDTH + TREEMAP_HEIGHT) * 0.6;
boolean HIGHLIGHT_CHANGES_IS_CUMULATIVE = false;
double TWEENING_TIME_INTERVAL = 1000; // milliseconds
float zoomFactor = 1.2;
////////////////////////////////////////////////////////
///////////////////// Imports //////////////////////////
////////////////////////////////////////////////////////
import guru.ttslib.*;
import processing.video.*;
import jp.nyatla.nyar4psg.*;
import treemap.*;
import picking.*;
//if (USE_OPENGL) {
import processing.opengl.*;
//}
////////////////////////////////////////////////////////
/////////// Global Variables ///////////////////////////
////////////////////////////////////////////////////////
PMatrix3D lastMatrix = new PMatrix3D(0.03271547,-0.9987524,0.037727464,7.3349524,0.9948697,0.028926386,-0.09694087,6.203373,0.0957286,0.040705375,0.99457484,-279.99384,0.0,0.0,0.0,1.0);
// NyAR4Psg
Capture cam;
MultiMarker nya;
PFont font=createFont("FFScala", 16);
NyAR4PsgConfig nyarConf = NyAR4PsgConfig.CONFIG_PSG;
//NyAR4PsgConfig nyarConf = new NyAR4PsgConfig(NyAR4PsgConfig.CS_RIGHT_HAND, NyAR4PsgConfig.TM_NYARTK);
PImage myframe;
// Treemap
Treemap map;
PackageItem mapModel;
int globalIndex = 0;
LinkedList<ClassItem> g_treemapItems = new LinkedList<ClassItem>();
int g_nSelectedItems = 0;
int g_currentVersion = 1;
int g_firstVersion = 1;
double g_tweeningVersion = g_currentVersion;
double g_maxChurn = 0;
int maxVersion = -1;
CommitLog commitLog;
String projectName;
int previousVisitedVersion = g_firstVersion;
// misc
TTS tts;
Announcer announcer = null;
String titleString = "";
Picker picker;
////////////////////////////////////////////////////////
////////////////// Functions ///////////////////////////
////////////////////////////////////////////////////////
void loadTreemap() {
// different choices for the layout method
//MapLayout algorithm = new SliceLayout(); // linhas finas
//MapLayout algorithm = new StripTreemap(); // linhas finas subdivididas
MapLayout algorithm = new PivotBySplitSize(); // default
//MapLayout algorithm = new SquarifiedLayout();
XMLElement elem = new XMLElement(this, INPUT_FILENAME);
XMLElement elemCode = elem.getChild("CodeInfo");
XMLElement elemLog = elem.getChild("LogInfo");
projectName = elem.getString("name");
mapModel = new PackageItem(null, elemCode, 0);
maxVersion = elem.getInt("lastVersion");
commitLog = new CommitLog(elemLog);
map = new Treemap(mapModel, 0, 0, width, height);
map.setLayout(algorithm);
map.updateLayout(-TREEMAP_WIDTH/2, -TREEMAP_HEIGHT/2, TREEMAP_WIDTH, TREEMAP_HEIGHT);
}
void setup() {
size(WINDOW_WIDTH, WINDOW_HEIGHT, USE_OPENGL ? OPENGL : P3D);
println(MultiMarker.VERSION);
if (USE_CAM)
cam=new Capture(this, WINDOW_WIDTH, WINDOW_HEIGHT);
nya=new MultiMarker(this,width,height,"camera_para.dat",nyarConf);
nya.addARMarker("patt.sample1",80);
nya.addARMarker("patt.sample2",80);
nya.setThreshold(THRESHOLD);
nya.setConfidenceThreshold(CONFIDENCE_THRESHOLD);
myframe = new PImage(width, height, RGB);
println("default confidence: " + MultiMarker.DEFAULT_CF_THRESHOLD);
loadTreemap();
picker = new Picker(this);
tts = new TTS();
textFont(font);
textMode(SCREEN);
}
// Rodrigo, 2011-06-06
// should be the last method call on draw()
// based on http://forum.processing.org/topic/nyartoolkit-when-i-flip-videocapture-mirror-effect-the-overlayed-objects-don%C2%B4t-flip
void flipScreen() {
loadPixels();
for (int x=0; x<width; x++) {
for (int y=0; y<height; y++) {
int loc = (width- x- 1) + y*width;
myframe.pixels[x+y*width] = color(red(pixels[loc]), green(pixels[loc]), blue(pixels[loc]));
}
}
myframe.updatePixels();
hint (DISABLE_DEPTH_TEST);
image(myframe, 0, 0);
hint (ENABLE_DEPTH_TEST);
}
//*******************************************************/
// Drawing
//*******************************************************/
void drawXmlTreemap3D() {
picker.start(32767);
lights();
noStroke();
pushMatrix();
translate(0, 0, -6.0f);
fill(FLOOR_COLOR);
noStroke();
strokeWeight(0);
box((float)TREEMAP_WIDTH, (float)TREEMAP_HEIGHT, 12.0f);
popMatrix();
stroke(0x33000000);
map.draw();
noLights();
}
void drawModelCube() {
fill(0,0,255);
//translate(0,0,40);
//box(80, 80, 80);
textMode(MODEL);
fill(255);
box(90, 90, -0.2);
textAlign(CENTER);
fill(0);
rotateZ(PI);
text(projectName, 0, 0);
textAlign(LEFT);
textMode(SCREEN);
}
void drawModel() {
applyZoom(zoomFactor);
// drawModelCube();
drawXmlTreemap3D();
}
//////////////// tweening //////////////////////////
int startTime = 0;
double startTweeningVersion = g_tweeningVersion;
void tweenVersion() {
int time = millis();
double progress = (time - startTime) / TWEENING_TIME_INTERVAL;
if (progress > 1.0)
progress = 1.0;
g_tweeningVersion = progress*(g_currentVersion) + (1 - progress)*(startTweeningVersion);
}
void setCurrentVersion(int v) {
if (v < 1)
v = 1;
else if (v > maxVersion)
v = maxVersion;
if (g_currentVersion != v) {
previousVisitedVersion = g_currentVersion;
g_currentVersion = v;
startTime = millis();
startTweeningVersion = g_tweeningVersion;
}
}
////////////////////////////////////////////////////
void applyZoom(float s) {
applyMatrix(
s, 0, 0, 0,
0, s, 0, 0,
0, 0, s, 0,
0, 0, 0, 1);
}
void drawOnLastMarker() {
pushMatrix();
resetMatrix();
applyMatrix(lastMatrix.m00, lastMatrix.m01, lastMatrix.m02, lastMatrix.m03,
lastMatrix.m10, lastMatrix.m11, lastMatrix.m12, lastMatrix.m13,
lastMatrix.m20, lastMatrix.m21, lastMatrix.m22, lastMatrix.m23,
lastMatrix.m30, lastMatrix.m31, lastMatrix.m32, lastMatrix.m33
);
applyZoom(1.75);
if (DRAW_MODELS)
drawModel();
popMatrix();
}
void shadowedText(String str, float x, float y, color fg, color bg) {
fill(bg);
text(str, x+2, y+2);
fill(fg);
text(str, x, y);
}
void shadowedText(String str, float x, float y) {
shadowedText(str, x, y, TEXT_FG_COLOR, TEXT_BG_COLOR);
}
void drawText() {
shadowedText(titleString, 10, 32);
shadowedText(commitLog.getAuthor() + "\nv" + g_currentVersion + " of " + maxVersion + ": " + commitLog.getMessage(), 10, height - 30);
}
void draw()
{
tweenVersion();
if (!USE_CAM) {
background(255);
drawOnLastMarker();
drawText();
return;
}
else if (cam.available() !=true) {
return;
}
cam.read();
nya.detect(cam);
background(0);
if (DEBUG) {
loadPixels();
for (int i = 0; i < width*height; i++) {
color c = cam.pixels[i];
if (brightness(c) > THRESHOLD)
pixels[i] = #FFFFFF;
else
pixels[i] = #000000;
}
updatePixels();
}
else {
nya.drawBackground(cam);
}
if((nya.isExistMarker(0))){
lastMatrix = nya.getMarkerMatrix(0);
nya.beginTransform(0);
if (DRAW_MODELS)
drawModel();
else if (DEBUG) {
int len = 80;
strokeWeight(4);
stroke(#CCCC00);
noFill();
rect(-len/2, -len/2, len, len);
strokeWeight(8);
stroke(#FF0000);
line(0, 0, 0, len, 0, 0);
stroke(#00FF00);
line(0, 0, 0, 0, len, 0);
stroke(#0000FF);
line(0, 0, 0, 0, 0, len);
}
nya.endTransform();
}
else if (PERSISTENT_TREEMAP) {
drawOnLastMarker();
}
if((nya.isExistMarker(1))){
nya.beginTransform(1);
if (DRAW_MODELS)
drawModelCube();
nya.endTransform();
}
if (FLIPPED_CAM)
flipScreen();
drawText();
}
// interaction
void mouseMoved() {
int x = mouseX;
int y = mouseY;
if (FLIPPED_CAM)
x = width - x;
int id = picker.get(x, y);
if (id > -1 && id < g_treemapItems.size()) {
ClassItem item = g_treemapItems.get(id);
titleString = "[" + item.type + "] " + item.fullName;
if (!(item instanceof PackageItem)) // Δ
titleString += "\nLOC:" + item.getIntForCurrentVersion("curr_loc") + " churn: " + (item.getIntForCurrentVersion("churn") - item.firstChurn);
}
else {
titleString = "";
}
}
void mouseClicked() {
int x = mouseX;
int y = mouseY;
if (FLIPPED_CAM)
x = width - x;
int id = picker.get(x, y);
if (id > -1 && id < g_treemapItems.size()) {
ClassItem item = g_treemapItems.get(id);
if (!(item instanceof PackageItem)) {
item.toggleSelect();
println("" + id + ": " + item.name + " level=" + item.level);
if (item.isSelected())
speak(item.name);
}
}
}
void speak(String name) {
String hyphenatedName = "";
int i = 0;
for (char c : name.toCharArray()) {
if (i > 0 && c >= 'A' && c <= 'Z')
hyphenatedName += "-";
hyphenatedName += c;
i++;
}
println("Speak " + hyphenatedName);
announcer = new Announcer(hyphenatedName);
announcer.start();
}
void updateThreshold(int newThreshold) {
if (newThreshold > 255)
THRESHOLD = 255;
else if (newThreshold < 0)
THRESHOLD = 0;
else
THRESHOLD = newThreshold;
nya.setThreshold(THRESHOLD);
println("New THRESHOLD = " + THRESHOLD);
}
void setZoomFactor(float factor) {
if (factor < 0.1)
factor = 0.1;
else if (factor > 30.0)
factor = 5.0;
zoomFactor = factor;
println("zoom = " + zoomFactor);
}
void incZoomFactor(float amount) {
setZoomFactor(zoomFactor + amount);
}
Set<ClassItem> getSelectedItems() {
Set<ClassItem> selected = new HashSet<ClassItem>();
for (ClassItem item : g_treemapItems) {
if (item.isSelected())
selected.add(item);
}
return selected;
}
void nextVersionThatChangedAnySelectedItem(int step) {
int v = g_currentVersion;
Set<ClassItem> selected = getSelectedItems();
if (selected.size() == 0)
return;
boolean found = false;
do {
v += step;
if (v > maxVersion)
v = maxVersion;
if (v < 1)
v = 1;
for (ClassItem item : selected) {
if (item.hasChangedInVersion(v)) {
found = true;
break;
}
}
} while(!found && v < maxVersion && v > 1);
if (found)
setCurrentVersion(v);
}
void keyPressed() {
if (key == 'd')
DEBUG = !DEBUG;
else if (key == 'm')
DRAW_MODELS = !DRAW_MODELS;
else if (key == 'h') {
HIDE_NON_SELECTED = !HIDE_NON_SELECTED;
}
else if (key == '+' || key == '=') {
updateThreshold(THRESHOLD + 5);
}
else if (key == '-') {
updateThreshold(THRESHOLD - 5);
}
else if (key == 'z') {
incZoomFactor(0.1);
}
else if (key == 'Z') {
incZoomFactor(-0.1);
}
else if (key == 'e') {
setCurrentVersion(maxVersion);
}
else if (key == 8) {
setCurrentVersion(previousVisitedVersion);
}
else if (key == 'p') {
PERSISTENT_TREEMAP = !PERSISTENT_TREEMAP;
}
else if (key == 'n') {
for (ClassItem item : getSelectedItems())
item.toggleSelect();
}
// else if (key == 'c') {
// HIGHLIGHT_CHANGES_IS_CUMULATIVE = !HIGHLIGHT_CHANGES_IS_CUMULATIVE;
// }
if (g_nSelectedItems == 0) {
if (key == CODED && keyCode == RIGHT) {
setCurrentVersion(g_currentVersion + 1);
}
else if (key == CODED && keyCode == LEFT) {
setCurrentVersion(g_currentVersion - 1);
}
}
else {
if (key == CODED && keyCode == RIGHT) {
nextVersionThatChangedAnySelectedItem(1);
}
else if (key == CODED && keyCode == LEFT) {
nextVersionThatChangedAnySelectedItem(-1);
}
}
}