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
|
/*
* $RCSfile$
*
* Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistribution of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any
* kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
* WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
* EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
* NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
* USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
* ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
* CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
* REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
* INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or
* intended for use in the design, construction, operation or
* maintenance of any nuclear facility.
*
* $Revision$
* $Date$
* $State$
*/
import java.lang.Math.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class Gear extends javax.media.j3d.TransformGroup {
// Specifiers determining whether to generate outward facing normals or
// inward facing normals.
static final int OutwardNormals = 1;
static final int InwardNormals = -1;
// The number of teeth in the gear
int toothCount;
// Gear start differential angle. All gears are constructed with the
// center of a tooth at Z-axis angle = 0.
double gearStartAngle;
// The Z-rotation angle to place the tooth center at theta = 0
float toothTopCenterAngle;
// The Z-rotation angle to place the valley center at theta = 0
float valleyCenterAngle;
// The angle about Z subtended by one tooth and its associated valley
float circularPitchAngle;
// Increment angles
float toothValleyAngleIncrement;
// Front and rear facing normals for the gear's body
final Vector3f frontNormal = new Vector3f(0.0f, 0.0f, -1.0f);
final Vector3f rearNormal = new Vector3f(0.0f, 0.0f, 1.0f);
Gear(int toothCount) {
this.toothCount = toothCount;
}
void addBodyDisks(float shaftRadius, float bodyOuterRadius,
float thickness, Appearance look) {
int gearBodySegmentVertexCount; // #(segments) per tooth-unit
int gearBodyTotalVertexCount; // #(vertices) in a gear face
int gearBodyStripCount[] = new int[1]; // per strip (1) vertex count
// A ray from the gear center, used in normal calculations
float xDirection, yDirection;
// The x and y coordinates at each point of a facet and at each
// point on the gear: at the shaft, the root of the teeth, and
// the outer point of the teeth
float xRoot0, yRoot0, xShaft0, yShaft0;
float xRoot3, yRoot3, xShaft3, yShaft3;
float xRoot4, yRoot4, xShaft4, yShaft4;
// Temporary variables for storing coordinates and vectors
Point3f coordinate = new Point3f(0.0f, 0.0f, 0.0f);
// Gear start differential angle. All gears are constructed with the
// center of a tooth at Z-axis angle = 0.
double gearStartAngle = -1.0 * toothTopCenterAngle;
// Temporaries that store start angle for each portion of tooth facet
double toothStartAngle, toothTopStartAngle,
toothDeclineStartAngle, toothValleyStartAngle,
nextToothStartAngle;
Shape3D newShape;
int index;
// The z coordinates for the body disks
final float frontZ = -0.5f * thickness;
final float rearZ = 0.5f * thickness;
/* Construct the gear's front body (front facing torus disk)
* __2__
* - | - 4
* - /| /-
* / / | /| \
* 0\ / | / / >
* \ / | / | >
* \ / | / / |
* \ / ____|/ | >
* \-- --__/ |
* 1 3 5
*
*/
gearBodySegmentVertexCount = 4;
gearBodyTotalVertexCount = 2 + gearBodySegmentVertexCount * toothCount;
gearBodyStripCount[0] = gearBodyTotalVertexCount;
TriangleStripArray frontGearBody
= new TriangleStripArray(gearBodyTotalVertexCount,
GeometryArray.COORDINATES
| GeometryArray.NORMALS,
gearBodyStripCount);
xDirection = (float)Math.cos(gearStartAngle);
yDirection = (float)Math.sin(gearStartAngle);
xShaft0 = shaftRadius * xDirection;
yShaft0 = shaftRadius * yDirection;
xRoot0 = bodyOuterRadius * xDirection;
yRoot0 = bodyOuterRadius * yDirection;
coordinate.set(xRoot0, yRoot0, frontZ);
frontGearBody.setCoordinate(0, coordinate);
frontGearBody.setNormal(0, frontNormal);
coordinate.set(xShaft0, yShaft0, frontZ);
frontGearBody.setCoordinate(1, coordinate);
frontGearBody.setNormal(1, frontNormal);
for(int count = 0; count < toothCount; count++) {
index = 2 + count * 4;
toothStartAngle
= gearStartAngle + circularPitchAngle * (double)count;
toothValleyStartAngle
= toothStartAngle + toothValleyAngleIncrement;
nextToothStartAngle = toothStartAngle + circularPitchAngle;
xDirection = (float)Math.cos(toothValleyStartAngle);
yDirection = (float)Math.sin(toothValleyStartAngle);
xShaft3 = shaftRadius * xDirection;
yShaft3 = shaftRadius * yDirection;
xRoot3 = bodyOuterRadius * xDirection;
yRoot3 = bodyOuterRadius * yDirection;
xDirection = (float)Math.cos(nextToothStartAngle);
yDirection = (float)Math.sin(nextToothStartAngle);
xShaft4 = shaftRadius * xDirection;
yShaft4 = shaftRadius * yDirection;
xRoot4 = bodyOuterRadius * xDirection;
yRoot4 = bodyOuterRadius * yDirection;
coordinate.set(xRoot3, yRoot3, frontZ);
frontGearBody.setCoordinate(index, coordinate);
frontGearBody.setNormal(index, frontNormal);
coordinate.set(xShaft3, yShaft3, frontZ);
frontGearBody.setCoordinate(index + 1, coordinate);
frontGearBody.setNormal(index + 1, frontNormal);
coordinate.set(xRoot4, yRoot4, frontZ);
frontGearBody.setCoordinate(index + 2, coordinate);
frontGearBody.setNormal(index + 2, frontNormal);
coordinate.set(xShaft4, yShaft4, frontZ);
frontGearBody.setCoordinate(index + 3, coordinate);
frontGearBody.setNormal(index + 3, frontNormal);
}
newShape = new Shape3D(frontGearBody, look);
this.addChild(newShape);
// Construct the gear's rear body (rear facing torus disc)
TriangleStripArray rearGearBody
= new TriangleStripArray(gearBodyTotalVertexCount,
GeometryArray.COORDINATES
| GeometryArray.NORMALS,
gearBodyStripCount);
xDirection = (float)Math.cos(gearStartAngle);
yDirection = (float)Math.sin(gearStartAngle);
xShaft0 = shaftRadius * xDirection;
yShaft0 = shaftRadius * yDirection;
xRoot0 = bodyOuterRadius * xDirection;
yRoot0 = bodyOuterRadius * yDirection;
coordinate.set(xShaft0, yShaft0, rearZ);
rearGearBody.setCoordinate(0, coordinate);
rearGearBody.setNormal(0, rearNormal);
coordinate.set(xRoot0, yRoot0, rearZ);
rearGearBody.setCoordinate(1, coordinate);
rearGearBody.setNormal(1, rearNormal);
for(int count = 0; count < toothCount; count++) {
index = 2 + count * 4;
toothStartAngle
= gearStartAngle + circularPitchAngle * (double)count;
toothValleyStartAngle
= toothStartAngle + toothValleyAngleIncrement;
nextToothStartAngle = toothStartAngle + circularPitchAngle;
xDirection = (float)Math.cos(toothValleyStartAngle);
yDirection = (float)Math.sin(toothValleyStartAngle);
xShaft3 = shaftRadius * xDirection;
yShaft3 = shaftRadius * yDirection;
xRoot3 = bodyOuterRadius * xDirection;
yRoot3 = bodyOuterRadius * yDirection;
xDirection = (float)Math.cos(nextToothStartAngle);
yDirection = (float)Math.sin(nextToothStartAngle);
xShaft4 = shaftRadius * xDirection;
yShaft4 = shaftRadius * yDirection;
xRoot4 = bodyOuterRadius * xDirection;
yRoot4 = bodyOuterRadius * yDirection;
coordinate.set(xShaft3, yShaft3, rearZ);
rearGearBody.setCoordinate(index, coordinate);
rearGearBody.setNormal(index, rearNormal);
coordinate.set(xRoot3, yRoot3, rearZ);
rearGearBody.setCoordinate(index + 1, coordinate);
rearGearBody.setNormal(index + 1, rearNormal);
coordinate.set(xShaft4, yShaft4, rearZ);
rearGearBody.setCoordinate(index + 2, coordinate);
rearGearBody.setNormal(index + 2, rearNormal);
coordinate.set(xRoot4, yRoot4, rearZ);
rearGearBody.setCoordinate(index + 3, coordinate);
rearGearBody.setNormal(index + 3, rearNormal);
}
newShape = new Shape3D(rearGearBody, look);
this.addChild(newShape);
}
void addCylinderSkins(float shaftRadius, float length,
int normalDirection, Appearance look) {
int insideShaftVertexCount; // #(vertices) for shaft
int insideShaftStripCount[] = new int[1]; // #(vertices) in strip/strip
double toothStartAngle, nextToothStartAngle, toothValleyStartAngle;
// A ray from the gear center, used in normal calculations
float xDirection, yDirection;
// The z coordinates for the body disks
final float frontZ = -0.5f * length;
final float rearZ = 0.5f * length;
// Temporary variables for storing coordinates, points, and vectors
float xShaft3, yShaft3, xShaft4, yShaft4;
Point3f coordinate = new Point3f(0.0f, 0.0f, 0.0f);
Vector3f surfaceNormal = new Vector3f();
Shape3D newShape;
int index;
int firstIndex;
int secondIndex;
/*
* Construct gear's inside shaft cylinder
* First the tooth's up, flat outer, and down distances
* Second the tooth's flat inner distance
*
* Outward facing vertex order:
* 0_______2____4
* | /| /|
* | / | / |
* | / | / |
* |/______|/___|
* 1 3 5
*
* Inward facing vertex order:
* 1_______3____5
* |\ |\ |
* | \ | \ |
* | \ | \ |
* |______\|___\|
* 0 2 4
*/
insideShaftVertexCount = 4 * toothCount + 2;
insideShaftStripCount[0] = insideShaftVertexCount;
TriangleStripArray insideShaft
= new TriangleStripArray(insideShaftVertexCount,
GeometryArray.COORDINATES
| GeometryArray.NORMALS,
insideShaftStripCount);
xShaft3 = shaftRadius * (float)Math.cos(gearStartAngle);
yShaft3 = shaftRadius * (float)Math.sin(gearStartAngle);
if (normalDirection == OutwardNormals) {
surfaceNormal.set(1.0f, 0.0f, 0.0f);
firstIndex = 1;
secondIndex = 0;
} else {
surfaceNormal.set(-1.0f, 0.0f, 0.0f);
firstIndex = 0;
secondIndex = 1;
}
// Coordinate labeled 0 in the strip
coordinate.set(shaftRadius, 0.0f, frontZ);
insideShaft.setCoordinate(firstIndex, coordinate);
insideShaft.setNormal(firstIndex, surfaceNormal);
// Coordinate labeled 1 in the strip
coordinate.set(shaftRadius, 0.0f, rearZ);
insideShaft.setCoordinate(secondIndex, coordinate);
insideShaft.setNormal(secondIndex, surfaceNormal);
for(int count = 0; count < toothCount; count++) {
index = 2 + count * 4;
toothStartAngle = circularPitchAngle * (double)count;
toothValleyStartAngle
= toothStartAngle + toothValleyAngleIncrement;
nextToothStartAngle = toothStartAngle + circularPitchAngle;
xDirection = (float)Math.cos(toothValleyStartAngle);
yDirection = (float)Math.sin(toothValleyStartAngle);
xShaft3 = shaftRadius * xDirection;
yShaft3 = shaftRadius * yDirection;
if (normalDirection == OutwardNormals)
surfaceNormal.set(xDirection, yDirection, 0.0f);
else
surfaceNormal.set(-xDirection, -yDirection, 0.0f);
// Coordinate labeled 2 in the strip
coordinate.set(xShaft3, yShaft3, frontZ);
insideShaft.setCoordinate(index + firstIndex, coordinate);
insideShaft.setNormal(index + firstIndex, surfaceNormal);
// Coordinate labeled 3 in the strip
coordinate.set(xShaft3, yShaft3, rearZ);
insideShaft.setCoordinate(index + secondIndex, coordinate);
insideShaft.setNormal(index + secondIndex, surfaceNormal);
xDirection = (float)Math.cos(nextToothStartAngle);
yDirection = (float)Math.sin(nextToothStartAngle);
xShaft4 = shaftRadius * xDirection;
yShaft4 = shaftRadius * yDirection;
if (normalDirection == OutwardNormals)
surfaceNormal.set(xDirection, yDirection, 0.0f);
else
surfaceNormal.set(-xDirection, -yDirection, 0.0f);
// Coordinate labeled 4 in the strip
coordinate.set(xShaft4, yShaft4, frontZ);
insideShaft.setCoordinate(index + 2 + firstIndex, coordinate);
insideShaft.setNormal(index + 2 + firstIndex, surfaceNormal);
// Coordinate labeled 5 in the strip
coordinate.set(xShaft4, yShaft4, rearZ);
insideShaft.setCoordinate(index + 2 + secondIndex, coordinate);
insideShaft.setNormal(index + 2 + secondIndex, surfaceNormal);
}
newShape = new Shape3D(insideShaft, look);
this.addChild(newShape);
}
public float getToothTopCenterAngle() {
return toothTopCenterAngle;
}
public float getValleyCenterAngle() {
return valleyCenterAngle;
}
public float getCircularPitchAngle() {
return circularPitchAngle;
}
}
|