-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLegoBuilder.java
More file actions
485 lines (413 loc) · 16.5 KB
/
Copy pathLegoBuilder.java
File metadata and controls
485 lines (413 loc) · 16.5 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
package org.freehep.j3d.plot;
import javax.media.j3d.Node;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;
/**
* @author Joy Kyriakopulos (joyk@fnal.gov)
* @version $Id: LegoBuilder.java 8584 2006-08-10 23:06:37Z duns $
*/
public class LegoBuilder extends AbstractPlotBuilder
{
private static final Color3b grey = new Color3b((byte) 50, (byte) 50, (byte) 50);
private static final Rainbow rainbow = new Rainbow();
private static final Vector3f xnormal = new Vector3f(1f,0,0);
private static final Vector3f xabnormal = new Vector3f(-1f,0,0);
private static final Vector3f ynormal = new Vector3f(0,1f,0);
private static final Vector3f yabnormal = new Vector3f(0,-1f,0);
private static final Vector3f znormal = new Vector3f(0,0,1f);
private static final Vector3f zabnormal = new Vector3f(0,0,-1f);
private static final int fullPlotChild = 0;
private static final int sparsePlotChild = 1;
private TimeStamp timeStamp = TimeStamp.sharedInstance();
private boolean drawBlocks = true; // default: draw Lego blocks
// Default: draw wire frame while animating if # populated bins > numWireLines/2
private boolean linesWhileAnim = true;
private int numWireLines = 600;
private int bcur;
private QuadArray quad;
private LineArray line;
private Shape3D shape;
private Shape3D lineShape;
private Switch targetSwitch;
private int popBins = 0; // number of populated bins in lego
private boolean shapeIsLego = false;
/**
* @param data Bin Contents
*/
public Node buildContent(NormalizedBinned2DData data)
{
// Build switch node to switch between full plot and sparse plot
targetSwitch = new Switch();
targetSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);
// Create shape and geometry for full plot
if (drawBlocks) {
shape = createShape();
shapeIsLego = true;
shape.setCapability(shape.ALLOW_GEOMETRY_WRITE);
shape.setCapability(shape.ALLOW_APPEARANCE_WRITE);
shape.setGeometry(buildGeometry(data)); // draw lego as full plot
}
else {
shape = createLineShape();
shapeIsLego = false;
shape.setCapability(shape.ALLOW_GEOMETRY_WRITE);
popBins = calcPopBins(data); // buildWireGeometry won't calculate # populated bins
int[] binInc = {1,1};
shape.setGeometry(buildWireGeometry(data, binInc)); // draw full "wire frame" plot
}
lineShape = createLineShape();
lineShape.setCapability(shape.ALLOW_GEOMETRY_WRITE);
targetSwitch.addChild(shape); // full plot should be added first
targetSwitch.addChild(lineShape); // sparse "wire-frame" added second
//If necessary, create geometry for a sparsified plot
if (linesWhileAnim && (shapeIsLego || popBins > numWireLines/2)) { // if too many bins populated
lineShape.setGeometry(buildWireGeometry(data)); // build alternate view
targetSwitch.setUserData(Boolean.TRUE);
}
else
targetSwitch.setUserData(Boolean.FALSE);
targetSwitch.setWhichChild(fullPlotChild); // full plot displayed by default
// System.out.println("buildContent: linesWhileAnim = "+linesWhileAnim+", popBins = "+popBins+", numWireLines = "+numWireLines+", shapeIsLego = "+shapeIsLego);
return targetSwitch;
}
public void updatePlot(NormalizedBinned2DData data)
{
// Create geometry for full plot
if (drawBlocks) {
if (!shapeIsLego) {
shape.setAppearance(createMaterialAppearance());
shapeIsLego = true;
}
shape.setGeometry(buildGeometry(data));
}
else {
popBins = calcPopBins(data); // buildWireGeometry won't calculate # populated bins
int[] binInc = {1,1};
if (shapeIsLego) {
shape.setAppearance(createWireFrameAppearance());
shapeIsLego = false;
}
shape.setGeometry(buildWireGeometry(data,binInc));
}
//If necessary, create geometry for a sparsified plot
// System.out.println("in updatePlot: linesWhileAnim = "+linesWhileAnim+", popBins = "+popBins+", numWireLines = "+numWireLines+", shapeIsLego = "+shapeIsLego);
if (linesWhileAnim && (shapeIsLego || popBins > numWireLines/2)) { // if too many bins populated
lineShape.setGeometry(buildWireGeometry(data)); // build alternate view
targetSwitch.setUserData(Boolean.TRUE); // flags whether there is line content
}
else
targetSwitch.setUserData(Boolean.FALSE); // no line content to display
}
public int getNumWireLines()
{
return numWireLines;
}
public void setNumWireLines(int val)
{
numWireLines = val;
}
public boolean getDrawBlocks()
{
return drawBlocks;
}
public void setDrawBlocks(boolean b)
{
drawBlocks = b;
}
public boolean getLinesWhileAnim()
{
return linesWhileAnim;
}
public void setLinesWhileAnim(boolean b)
{
linesWhileAnim = b;
}
private Geometry buildGeometry(NormalizedBinned2DData data)
{
timeStamp.print("Starting LegoBuilder.buildContent()");
int nXbins = data.xBins();
int nYbins = data.yBins();
// Create the coordinate and color arrays
bcur = 0;
int maxpoints = (nXbins+1) * (nYbins+1) * 4 * 3 * 4; // An over estimate
quad = new QuadArray(maxpoints,QuadArray.COORDINATES+QuadArray.NORMALS+QuadArray.COLOR_3);
// Create the floor
drawXYrect(-.5f,-.5f,0,.5f,.5f,0,grey,znormal);
float xBinWidth = 1.f/nXbins;
float yBinWidth = 1.f/nYbins;
float x = -xBinWidth - .5f;
float xBinWAdj = 1.f/nXbins/100.f;
float yBinWAdj = 1.f/nYbins/100.f;
popBins = 0;
// Note, we start a bin -1 on the X and Y axis. The getDataAt method always
// returns 0 for elements outside of the legal bin range, so this, coupled with
// the fact that we never draw the tops of bins which have z=0, takes care of all
// the edge effects.
for (int k=-1; k < nXbins; k++, x += xBinWidth)
{
float y = -yBinWidth - .5f;
for(int l=-1; l < nYbins; l++, y += yBinWidth)
{
float z = data.zAt(k,l);
Color3b curColor = data.colorAt(k,l);
// Construct colored, horizontal/top side of lego on the data point
// (X-Y plane at constant z)
if (z != 0) // skip drawing top if no height
{
++popBins; // keep track of how many bins > 0
drawXYrect(x+xBinWidth-xBinWAdj, y+yBinWidth-yBinWAdj, z,
x, y, z,
curColor, znormal);
// color bottom of bin (visible when top is scaled off & clipped)
drawXYrect(x+xBinWidth-xBinWAdj, y+yBinWidth-yBinWAdj, .001f,
x, y, .001f,
curColor, znormal);
}
// Construct sides between this and next Y bin
float nextZ = data.zAt(k,l+1);
Color3b nextColor = data.colorAt(k,l+1);
if (z != 0) { // side of current bin - skip drawing if no height
drawXZrect(x, y+yBinWidth-yBinWAdj/2.f, z,
x+xBinWidth-xBinWAdj, y+yBinWidth-yBinWAdj/2.f, 0,
curColor, ynormal);
drawXZrect(x, y+yBinWidth-yBinWAdj, z,
x+xBinWidth-xBinWAdj, y+yBinWidth-yBinWAdj, 0,
curColor, yabnormal); // inside - seen only if top clipped
}
if (nextZ != 0) { // side of next bin - skip drawing if no height
drawXZrect(x, y+yBinWidth, 0,
x+xBinWidth, y+yBinWidth, nextZ,
nextColor, yabnormal);
drawXZrect(x, y+yBinWidth+yBinWAdj, 0,
x+xBinWidth, y+yBinWidth+yBinWAdj, nextZ,
nextColor, ynormal); // inside - seen only if top clipped
}
// Construct sides between this and next X bin
nextZ = data.zAt(k+1,l);
nextColor = data.colorAt(k+1,l);
if (z != 0) { // side of current bin - skip drawing if no height
drawYZrect(x+xBinWidth-xBinWAdj/2.f, y, z,
x+xBinWidth-xBinWAdj/2.f, y+yBinWidth-yBinWAdj, 0,
curColor, xnormal);
drawYZrect(x+xBinWidth-xBinWAdj, y, z,
x+xBinWidth-xBinWAdj, y+yBinWidth-yBinWAdj, 0,
curColor, xabnormal); // inside - seen only if top clipped
}
if (nextZ != 0) { // side of next bin - skip drawing if no height
drawYZrect(x+xBinWidth, y, 0,
x+xBinWidth, y+yBinWidth, nextZ,
nextColor, xabnormal);
drawYZrect(x+xBinWidth+xBinWAdj, y, 0,
x+xBinWidth+xBinWAdj, y+yBinWidth, nextZ,
nextColor, xnormal); // inside - seen only if top clipped
}
}
}
timeStamp.print("finished, now finalizing, point count = "+bcur);
return quad;
}
private Geometry buildWireGeometry(NormalizedBinned2DData data)
{
// Compute x,y factors: will be > 1 if data is too large and
// plot needs to be sparsified (i.e. made more sparse)
int[] wireBinInc = calcXYfactors(data.xBins(), data.yBins());
return buildWireGeometry(data, wireBinInc);
}
private Geometry buildWireGeometry(NormalizedBinned2DData data, int[] wireBinInc)
{
timeStamp.print("Starting LegoBuilder.buildWireGeometry()");
int nXbins = data.xBins();
int nYbins = data.yBins();
// Create the coordinate and color arrays
bcur = 0;
int maxpoints = ((nXbins/wireBinInc[0] + 1) * (nYbins/wireBinInc[1] + 1)) * 3 * 2 + 8; // An over estimate
line = new LineArray(maxpoints,LineArray.COORDINATES+LineArray.NORMALS+LineArray.COLOR_3);
float xBinWidth = (float)wireBinInc[0]/(float)(nXbins);
float yBinWidth = (float)wireBinInc[1]/(float)(nYbins);;
float x = - .5f;
// Go through data, drawing a vertical line instead of a bin
for (int k=0; k < nXbins; k+=wireBinInc[0], x += xBinWidth)
{
float y = - .5f;
for (int l=0; l < nYbins; l+=wireBinInc[1], y += yBinWidth)
{
float z = data.zAt(k,l);
Color3b curColor = data.colorAt(k,l);
if (z != 0) { // skip drawing if no height
drawVLine(x+xBinWidth/2.f, y+yBinWidth/2.f, 0.f, z, curColor, xnormal);
}
}
}
timeStamp.print("finished, now finalizing, point count = "+bcur);
return line;
}
Shape3D createLineShape()
{
Shape3D lineShape = new Shape3D();
timeStamp.print("line geometry set");
lineShape.setAppearance(createWireFrameAppearance());
return lineShape;
}
Appearance createWireFrameAppearance()
{
Appearance materialAppear = new Appearance();
LineAttributes lineAttrib = new LineAttributes();
materialAppear.setLineAttributes(lineAttrib);
ColoringAttributes redColoring = new ColoringAttributes();
redColoring.setColor(1.0f, 0.0f, 0.0f);
materialAppear.setColoringAttributes(redColoring);
return materialAppear;
}
Shape3D createShape()
{
Shape3D surface = new Shape3D();
timeStamp.print("geometry set");
surface.setAppearance(createMaterialAppearance());
return surface;
}
private Appearance createMaterialAppearance()
{
Appearance materialAppear = new Appearance();
PolygonAttributes polyAttrib = new PolygonAttributes();
polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
materialAppear.setPolygonAttributes(polyAttrib);
Material material = new Material();
// set diffuse color to red (this color will only be used
// if lighting disabled - per-vertex color overrides)
material.setDiffuseColor(new Color3f(1.0f, 0.0f, 0.0f));
materialAppear.setMaterial(material);
return materialAppear;
}
// Construct colored line
private void drawLine(float x1, float y1, float z1,
float x2, float y2, float z2,
Color3b lineColor, Vector3f normal)
{
line.setCoordinate(bcur,new Point3f(x1,y1,z1));
line.setColor(bcur,lineColor);
line.setNormal(bcur,normal);
bcur++;
line.setCoordinate(bcur,new Point3f(x2,y2,z2));
line.setColor(bcur,lineColor);
line.setNormal(bcur,normal);
bcur++;
}
// Construct colored, vertical line at constant X,Y
private void drawVLine(float x, float y, float z1, float z2,
Color3b lineColor, Vector3f normal)
{
line.setCoordinate(bcur,new Point3f(x,y,z1));
line.setColor(bcur,lineColor);
line.setNormal(bcur,normal);
bcur++;
line.setCoordinate(bcur,new Point3f(x,y,z2));
line.setColor(bcur,lineColor);
line.setNormal(bcur,normal);
bcur++;
}
// Construct colored, horizontal rectangle in the X-Y plane at constant Z
private void drawXYrect(float x1, float y1, float z1,
float x2, float y2, float z2,
Color3b rectColor, Vector3f normal)
{
quad.setCoordinate(bcur,new Point3f(x1,y1,z1));
quad.setColor(bcur,rectColor);
quad.setNormal(bcur,normal);
bcur++;
quad.setCoordinate(bcur,new Point3f(x1,y2,z1));
quad.setColor(bcur,rectColor);
quad.setNormal(bcur,normal);
bcur++;
quad.setCoordinate(bcur,new Point3f(x2,y2,z1));
quad.setColor(bcur,rectColor);
quad.setNormal(bcur,normal);
bcur++;
quad.setCoordinate(bcur,new Point3f(x2,y1,z1));
quad.setColor(bcur,rectColor);
quad.setNormal(bcur,normal);
bcur++;
}
// Construct colored, vertical rectangle in the X-Z plane at constant Y
private void drawXZrect(float x1, float y1, float z1,
float x2, float y2, float z2,
Color3b rectColor, Vector3f normal)
{
quad.setCoordinate(bcur,new Point3f(x1,y1,z1));
quad.setColor(bcur,rectColor);
quad.setNormal(bcur,normal);
bcur++;
quad.setCoordinate(bcur,new Point3f(x2,y1,z1));
quad.setColor(bcur,rectColor);
quad.setNormal(bcur,normal);
bcur++;
quad.setCoordinate(bcur,new Point3f(x2,y1,z2));
quad.setColor(bcur,rectColor);
quad.setNormal(bcur,normal);
bcur++;
quad.setCoordinate(bcur,new Point3f(x1,y1,z2));
quad.setColor(bcur,rectColor);
quad.setNormal(bcur,normal);
bcur++;
}
// Construct colored, vertical rectangle in the Y-Z plane at constant X
private void drawYZrect(float x1, float y1, float z1,
float x2, float y2, float z2,
Color3b rectColor, Vector3f normal)
{
quad.setCoordinate(bcur,new Point3f(x1,y1,z1));
quad.setColor(bcur,rectColor);
quad.setNormal(bcur,normal);
bcur++;
quad.setCoordinate(bcur,new Point3f(x1,y2,z1));
quad.setColor(bcur,rectColor);
quad.setNormal(bcur,normal);
bcur++;
quad.setCoordinate(bcur,new Point3f(x1,y2,z2));
quad.setColor(bcur,rectColor);
quad.setNormal(bcur,normal);
bcur++;
quad.setCoordinate(bcur,new Point3f(x1,y1,z2));
quad.setColor(bcur,rectColor);
quad.setNormal(bcur,normal);
bcur++;
}
// Heuristic to calculate sparsification factors (i.e. to make plot
// more sparse) for both X and Y in cases where there's too much
// lego data to render in a short time during rotations, panning,
// zooming, etc.
private int[] calcXYfactors(int nXbins, int nYbins)
{
int[] binInc = {1,1};
if (nXbins * nYbins > numWireLines) {
double xyBinRatio = (double) nXbins / (double)nYbins;
binInc[0] = (int) (nXbins / (Math.sqrt((double)numWireLines) / xyBinRatio));
binInc[1] = (int) (nYbins / (Math.sqrt((double)numWireLines) * xyBinRatio));
if (binInc[0] < 1) binInc[0] = 1;
if (binInc[1] < 1) binInc[1] = 1;
}
// System.out.println("in calcXYfactors: " + binInc[0] + " " + binInc[1]);
return binInc;
}
// calcPopBins - method to calculate the number of bins that are != zero in the
// normalized data. (This routine is called when the lego will not be built.
// Otherwise the lego sets popBins while it's building its geometry.)
private int calcPopBins(NormalizedBinned2DData data)
{
int nXbins = data.xBins();
int nYbins = data.yBins();
int numPopBins = 0;
float x = -1.f/nXbins - .5f;
for (int k=-1; k < nXbins; k++, x += 1.f/nXbins)
{
float y = -1.f/nYbins - .5f;
for (int l=-1; l < nYbins; l++, y += 1.f/nYbins)
{
float z = data.zAt(k,l);
if (z != 0)
++numPopBins; // keep track of how many bins != 0
}
}
return numPopBins;
}
}