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
|
/* XLogo4Schools - A Logo Interpreter specialized for use in schools, based on XLogo by Loic Le Coq
* Copyright (C) 2013 Marko Zivkovic
*
* Contact Information: marko88zivkovic at gmail dot com
*
* 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., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
* This Java source code belongs to XLogo4Schools, written by Marko Zivkovic
* during his Bachelor thesis at the computer science department of ETH Zurich,
* in the year 2013 and/or during future work.
*
* It is a reengineered version of XLogo written by Loic Le Coq, published
* under the GPL License at http://xlogo.tuxfamily.org/
*
* Contents of this file were initially written by Loic Le Coq,
* modifications, extensions, refactorings might have been applied by Marko Zivkovic
*/
package xlogo.kernel.perspective;
import org.jogamp.java3d.Appearance;
import org.jogamp.java3d.GeometryArray;
import org.jogamp.java3d.LineStripArray;
import org.jogamp.java3d.Material;
import org.jogamp.java3d.PolygonAttributes;
import org.jogamp.java3d.Shape3D;
import org.jogamp.java3d.LineAttributes;
import org.jogamp.java3d.TransformGroup;
import org.jogamp.java3d.utils.geometry.Sphere;
import org.jogamp.java3d.Transform3D;
import org.jogamp.vecmath.Color3f;
import org.jogamp.vecmath.Point3d;
import org.jogamp.vecmath.AxisAngle4d;
import org.jogamp.java3d.utils.geometry.Cylinder;
import org.jogamp.vecmath.Vector3d;
import xlogo.kernel.LogoError;
/**
*
* @author Marko Zivkovic - I decoupled this from Application
*
*/
public class ElementLine extends Element3D
{
/**
* This float stores the line Width for each 3D line
*
* @uml.property name="lineWidth"
*/
private final float lineWidth;
public ElementLine(final Viewer3D v3d, final float lineWidth)
{
super(v3d);
this.lineWidth = lineWidth; //app.getKernel().getActiveTurtle().getPenWidth();
}
public void addToScene() throws LogoError
{
final int size = vertex.size();
if (size > 1)
{
if (lineWidth == 0.5)
createSimpleLine(size);
else
createComplexLine(size);
}
}
/**
* This method draws a line with width 1
*
* @param size
*/
private void createSimpleLine(final int size)
{
final int[] tab = { size };
final LineStripArray line = new LineStripArray(size, GeometryArray.COORDINATES | GeometryArray.COLOR_3, tab);
for (int i = 0; i < size; i++)
{
line.setCoordinate(i, vertex.get(i));
// System.out.println("sommet "+(2*i-1)+" "+vertex.get(i).x+" "+vertex.get(i).y+" "+vertex.get(i).z+" ");
line.setColor(i, new Color3f(color.get(i).getRGBColorComponents(null)));
}
final Shape3D s = new Shape3D(line);
final Appearance appear = new Appearance();
final Material mat = new Material(new Color3f(1.0f, 1.0f, 1.0f), new Color3f(0.0f, 0f, 0f), new Color3f(1f, 1.0f,
1.0f), new Color3f(1f, 1f, 1f), 64);
appear.setMaterial(mat);
appear.setLineAttributes(new LineAttributes(2 * lineWidth, LineAttributes.PATTERN_SOLID, false));
s.setAppearance(appear);
// DrawPanel.listPoly.add(s);
v3d.add3DObject(s);
/*
* for (int i=0;i<line.getVertexCount();i++){
* double[] d=new double[3];
* line.getCoordinate(i, d);
* for(int j=0;j<d.length;j++){
* System.out.println(i+" "+d[j]);
* }
* }
*/
}
/**
* This method draws a sequence of lines with a width different from 1
* Draws a cylinder around the line
*
* @param size
*/
private void createComplexLine(final int size)
{
for (int i = 0; i < size - 1; i++)
{
createLine(vertex.get(i), vertex.get(i + 1), new Color3f(color.get(i + 1).getRGBColorComponents(null)));
}
}
/**
* This method creates an elementary line with a width different from 1
* It draws a cylinder around the line and two spheres on each extremities
*
* @param p1
* the first point
* @param p2
* the second point
* @param color
* the sphere color
*/
private void createLine(final Point3d p1, final Point3d p2, final Color3f color)
{
// create a new CylinderTransformer between the two atoms
final CylinderTransformer cT = new CylinderTransformer(p1, p2);
// get the length
final double length = cT.getLength();
final float radius = lineWidth / 1000;
// holds the translation
final Transform3D transform1 = new Transform3D();
// ...move the coordinates there
transform1.setTranslation(cT.getTranslation());
// get the axis and angle for rotation
final AxisAngle4d rotation = cT.getAxisAngle();
final Transform3D transform2 = new Transform3D();
transform2.setRotation(rotation);
// combine the translation and rotation into transform1
transform1.mul(transform2);
// create a new Cylinder
final Appearance appear = new Appearance();
final Material mat = new Material(new Color3f(1.0f, 1.0f, 1.0f), color, new Color3f(1f, 1.0f, 1.0f), new Color3f(1f,
1f, 1f), 64);
appear.setMaterial(mat);
final PolygonAttributes pa = new PolygonAttributes();
pa.setCullFace(PolygonAttributes.CULL_NONE);
pa.setBackFaceNormalFlip(true);
appear.setPolygonAttributes(pa);
final Cylinder cyl = new Cylinder(radius, (float) length, appear);
// and add it to the TransformGroup
final TransformGroup tg = new TransformGroup();
tg.setTransform(transform1);
tg.addChild(cyl);
v3d.add2DText(tg);
// Add Sphere to the line extremities.
final Sphere sphere1 = new Sphere(radius, appear);
final TransformGroup tg2 = new TransformGroup();
final Transform3D transform3 = new Transform3D();
transform3.setTranslation(new Vector3d(p1));
tg2.setTransform(transform3);
tg2.addChild(sphere1);
v3d.add2DText(tg2);
final Sphere sphere2 = new Sphere(radius, appear);
final TransformGroup tg3 = new TransformGroup();
final Transform3D transform4 = new Transform3D();
transform4.setTranslation(new Vector3d(p2));
tg3.setTransform(transform4);
tg3.addChild(sphere2);
v3d.add2DText(tg3);
}
public boolean isLine()
{
return true;
}
public boolean isPoint()
{
return false;
}
public boolean isPolygon()
{
return false;
}
public boolean isText()
{
return false;
}
/**
* A class to calculate the length and transformations necessary to produce
* a cylinder to connect two points. Useful for Java3D and VRML where a
* cylinder object is created aligned along the y-axis.
*
* @author Alastair Hill
*/
class CylinderTransformer
{
/** point A */
private final Point3d pointA;
/** point B */
private final Point3d pointB;
/**
* the angle through which to rotate the cylinder
*/
private double angle;
/**
* the axis around which to rotate the cylinder
*
*/
private Vector3d axis;
/**
* The translation required to translate the cylinder to the midpoint of
* the two points
*/
private Vector3d translation;
/**
* The length of the cylinder required to join the two points
*
*/
private double length;
/**
* Creates a new instance of CylinderTransformer
*
* @param a
* point a
* @param b
* point b
*/
CylinderTransformer(final Point3d a, final Point3d b)
{
pointA = a;
pointB = b;
// carry out the calculations
doCalculations();
}
/**
* Carries out the necessary calculations so that values may be returned
*/
private void doCalculations()
{
length = pointA.distance(pointB);
final double[] arrayA = new double[3];
pointA.get(arrayA);
final double[] arrayB = new double[3];
pointB.get(arrayB);
final double[] arrayMid = new double[3];
for (int i = 0; i < arrayA.length; i++)
{
arrayMid[i] = (arrayA[i] + arrayB[i]) / 2f;
}
// the translation needed is
translation = new Vector3d(arrayMid);
// the initial orientation of the bond is in the y axis
final Vector3d init = new Vector3d(0.0f, 1.0f, 0.0f);
// the vector needed is the same as that from a to b
final Vector3d needed = new Vector3d(pointB.x - pointA.x, pointB.y - pointA.y, pointB.z - pointA.z);
// so the angle to rotate the bond by is:
angle = needed.angle(init);
// and the axis to rotate by is the cross product of the initial and
// needed vectors - ie the vector orthogonal to them both
axis = new Vector3d();
axis.cross(init, needed);
}
/**
* Returns the angle (in radians) through which to rotate the cylinder
*
* @return the angle.
*/
double getAngle()
{
return angle;
}
/**
* The axis around which the cylinder must be rotated
*
* @return the axis
*/
Vector3d getAxis()
{
return axis;
}
/**
* The length required for the cylinder to join the two points
*
* @return the length
*/
double getLength()
{
return length;
}
/**
* The translation required to move the cylinder to the midpoint of the
* two points
*
* @return the translation
*/
Vector3d getTranslation()
{
return translation;
}
/**
* Generates a (pretty) string representation of the the
* CylinderTransformer
*
* @return the string representation
*/
public String toString()
{
return "tr: " + translation + ", ax: " + axis + ", an: " + angle + ", le: " + length;
}
/**
* Generates the required axis and angle combined into an AxisAngle4f
* object
*
* @return the axis and angle
*/
public AxisAngle4d getAxisAngle()
{
return new AxisAngle4d(axis.x, axis.y, axis.z, angle);
}
}
}
|