forked from CAIDA/walrus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
H3Axes.java
300 lines (247 loc) · 7.91 KB
/
H3Axes.java
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
//
// The Walrus Graph Visualization Tool.
// Copyright (C) 2000,2001,2002 The Regents of the University of California.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// ######END_HEADER######
//
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.Text2D;
import com.sun.j3d.utils.geometry.*;
public class H3Axes
{
public H3Axes()
{
createAxes();
createAxisLabels();
createUnitSphere();
createGeneralAppearances();
}
public void draw(GraphicsContext3D gc, Transform3D transform)
{
gc.setModelTransform(transform);
gc.setAppearance(m_axisAppearance);
gc.draw(m_axes);
gc.setAppearance(m_circleAppearance);
gc.draw(m_xyCircle);
gc.draw(m_yzCircle);
gc.draw(m_xzCircle);
drawAxisLabels(gc, transform);
}
public void setAxisAppearance(Appearance appearance)
{
m_axisAppearance = appearance;
}
public void setCircleAppearance(Appearance appearance)
{
m_circleAppearance = appearance;
}
public void setAxisColor(float r, float g, float b)
{
ColoringAttributes attributes =
new ColoringAttributes(r, g, b, ColoringAttributes.FASTEST);
m_axisAppearance.setColoringAttributes(attributes);
}
public void setCircleColor(float r, float g, float b)
{
ColoringAttributes attributes =
new ColoringAttributes(r, g, b, ColoringAttributes.FASTEST);
m_circleAppearance.setColoringAttributes(attributes);
}
// ===================================================================
private void createAxes()
{
double radius = 1.0;
LineArray axisGeometry = new LineArray(6, LineArray.COORDINATES);
Point3d[] coordinates = new Point3d[6];
coordinates[0] = new Point3d(-radius, 0.0, 0.0);
coordinates[1] = new Point3d(radius, 0.0, 0.0);
coordinates[2] = new Point3d(0.0, -radius, 0.0);
coordinates[3] = new Point3d(0.0, radius, 0.0);
coordinates[4] = new Point3d(0.0, 0.0, -radius);
coordinates[5] = new Point3d(0.0, 0.0, radius);
axisGeometry.setCoordinates(0, coordinates);
m_axes = axisGeometry;
}
private void createAxisLabels()
{
{
m_xT3D = new Transform3D();
m_xT3D.setScale(0.01);
m_xT3D.setTranslation(new Vector3d(1.0, 0.0, 0.0));
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Text3D xLabel = createText3D("X");
Appearance xAppearance =
createLabelAppearance(new Color3f(0.5f, 0.1f, 0.1f));
m_xLabel = new Shape3D(xLabel, xAppearance);
}
{
m_yT3D = new Transform3D();
m_yT3D.setScale(0.01);
m_yT3D.setTranslation(new Vector3d(0.0, 1.0, 0.0));
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Text3D yLabel = createText3D("Y");
Appearance yAppearance =
createLabelAppearance(new Color3f(0.1f, 0.5f, 0.1f));
m_yLabel = new Shape3D(yLabel, yAppearance);
}
{
Matrix4d rot = new Matrix4d();
rot.rotX(-Math.PI / 2.0);
rot.rotY(Math.PI / 2.0);
Matrix4d m = new Matrix4d();
m.set(0.01, new Vector3d(0.0, 0.0, 1.0)); // (scale, translation)
m.mul(rot);
m_zT3D = new Transform3D(m);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Text3D zLabel = createText3D("Z");
zLabel.setAlignment(Text3D.ALIGN_LAST);
Appearance zAppearance =
createLabelAppearance(new Color3f(0.1f, 0.1f, 0.5f));
m_zLabel = new Shape3D(zLabel, zAppearance);
}
}
private void drawAxisLabels(GraphicsContext3D gc, Transform3D transform)
{
{
Transform3D xT3D = new Transform3D(transform);
xT3D.mul(m_xT3D);
gc.setModelTransform(xT3D);
gc.draw(m_xLabel);
}
{
Transform3D yT3D = new Transform3D(transform);
yT3D.mul(m_yT3D);
gc.setModelTransform(yT3D);
gc.draw(m_yLabel);
}
{
Transform3D zT3D = new Transform3D(transform);
zT3D.mul(m_zT3D);
gc.setModelTransform(zT3D);
gc.draw(m_zLabel);
}
}
private void createUnitSphere()
{
int numSegments = 72;
int[] stripLengths = { numSegments + 1 };
{
Point3f[] xyPoints = createXYCircleCoordinates(1.0f, numSegments);
LineStripArray xyLines = new LineStripArray(xyPoints.length,
GeometryArray.COORDINATES,
stripLengths);
xyLines.setCoordinates(0, xyPoints);
m_xyCircle = xyLines;
}
{
Point3f[] yzPoints = createXYCircleCoordinates(1.0f, numSegments);
for (int i = yzPoints.length - 1; i >= 0; i--)
{
Point3f p = yzPoints[i];
p.z = p.y;
p.y = p.x;
p.x = 0.0f;
}
LineStripArray yzLines = new LineStripArray(yzPoints.length,
GeometryArray.COORDINATES,
stripLengths);
yzLines.setCoordinates(0, yzPoints);
m_yzCircle = yzLines;
}
{
Point3f[] xzPoints = createXYCircleCoordinates(1.0f, numSegments);
for (int i = xzPoints.length - 1; i >= 0; i--)
{
Point3f p = xzPoints[i];
p.z = p.x;
p.x = p.y;
p.y = 0.0f;
}
LineStripArray xzLines = new LineStripArray(xzPoints.length,
GeometryArray.COORDINATES,
stripLengths);
xzLines.setCoordinates(0, xzPoints);
m_xzCircle = xzLines;
}
}
private Point3f[] createXYCircleCoordinates(float radius, int numSegments)
{
Point3f[] retval = new Point3f[numSegments + 1];
Matrix4d rot = new Matrix4d();
for (int i = 0; i <= numSegments; i++)
{
retval[i] = new Point3f(radius, 0.0f, 0.0f);
double angle = 2.0 * Math.PI * i / numSegments;
rot.rotZ(angle);
rot.transform(retval[i]);
}
return retval;
}
private Text3D createText3D(String text)
{
Font font = new Font("SansSerif", Font.PLAIN, 14);
Font3D font3D = new Font3D(font, new FontExtrusion());
return new Text3D(font3D, text);
}
private Appearance createLabelAppearance(Color3f color)
{
Appearance retval = new Appearance();
ColoringAttributes coloring = new ColoringAttributes(color,
ColoringAttributes.FASTEST);
retval.setColoringAttributes(coloring);
return retval;
}
private void createGeneralAppearances()
{
{
LineAttributes lineAttributes = new LineAttributes();
lineAttributes.setLineAntialiasingEnable(ANTIALIASING);
ColoringAttributes coloringAttributes =
new ColoringAttributes(0.3f, 0.6f, 0.3f,
ColoringAttributes.FASTEST);
m_axisAppearance = new Appearance();
m_axisAppearance.setLineAttributes(lineAttributes);
m_axisAppearance.setColoringAttributes(coloringAttributes);
}
{
LineAttributes lineAttributes = new LineAttributes();
lineAttributes.setLineAntialiasingEnable(ANTIALIASING);
ColoringAttributes coloringAttributes =
new ColoringAttributes(0.3f, 0.6f, 0.3f,
ColoringAttributes.FASTEST);
m_circleAppearance = new Appearance();
m_circleAppearance.setLineAttributes(lineAttributes);
m_circleAppearance.setColoringAttributes(coloringAttributes);
}
}
// =====================================================================
private static final boolean ANTIALIASING = false;
private Geometry m_axes;
private Geometry m_xyCircle;
private Geometry m_yzCircle;
private Geometry m_xzCircle;
private Shape3D m_xLabel;
private Shape3D m_yLabel;
private Shape3D m_zLabel;
private Transform3D m_xT3D;
private Transform3D m_yT3D;
private Transform3D m_zT3D;
private Appearance m_axisAppearance;
private Appearance m_circleAppearance;
}