summaryrefslogtreecommitdiff
path: root/trunk/users/metalab/AoI/scripts/Gear.bsh
blob: 7f0bebc9e03714bb01a06c85899a4ffb25056da3 (plain)
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
/*
<?xml version='1.0' standalone='yes' ?>

<script>
	<name>Gear</name>
	<author>Metalab</author>
	<version>0.1</version>
	<date>20081008</date>
	<description>This script creates a simple gear.</description>
</script>
*/

scene = window.getScene();

invertGearCheckbox = new BCheckBox( "", false );
toothSizeField = new ValueField(1, ValueField.NONNEGATIVE);
numTeethField = new ValueField(8, ValueField.NONNEGATIVE);

dlg = new ComponentsDialog(window, "Gear" ,
			   new Widget [] { toothSizeField, numTeethField, invertGearCheckbox },
			   new String [] { "Tooth size:", "# teeth:", "inverted gear" });

if (!dlg.clickedOk()) return;

toothSize = toothSizeField.getValue();
numTeeth = (int)Math.round(numTeethField.getValue());
boolean invertedGear = invertGearCheckbox.getState();

float innerradius = numTeeth * toothSize / 2;
float pitchradius = innerradius + toothSize * 1.25;
float outerradius = innerradius + toothSize * 2.25;
float toothangle = 2*Math.PI/numTeeth;




int PROTOSIZE = 7;
Vec3[] protoTooth = new Vec3[PROTOSIZE];

/* // standard teeth (but in practice they are too small

protoTooth[0] = new Vec3(-Math.PI/2, (double)innerradius, 0.0);
protoTooth[1] = new Vec3(-Math.PI/4 -1.25*Math.tan(Math.PI*20/180), innerradius, 0.0);
protoTooth[2] = new Vec3(-Math.PI/4, pitchradius, 0.0);
protoTooth[3] = new Vec3(-Math.PI/4 + Math.tan(Math.PI*20/180), outerradius, 0.0);
protoTooth[4] = new Vec3(Math.PI/4 - Math.tan(Math.PI*20/180), outerradius, 0.0);
protoTooth[5] = new Vec3(Math.PI/4, pitchradius, 0.0);
protoTooth[6] = new Vec3(Math.PI/4 + 1.25*Math.tan(Math.PI*20/180), innerradius, 0.0);
*/ 

if (invertedGear) {
	outerradius = (float)(numTeeth * toothSize / 2);
	pitchradius = (float)(innerradius + toothSize * 1.25);
	innerradius = (float)(innerradius + toothSize * 2.25);
	
	protoTooth[0] = new Vec3(-Math.PI/2, innerradius, 0.0);
	protoTooth[1] = new Vec3(-Math.PI/4 - 0.2, innerradius, 0.0);
	protoTooth[2] = new Vec3(-Math.PI/4 + 0.3, pitchradius, 0.0);
	protoTooth[3] = new Vec3(-Math.PI/4 + 0.6, outerradius, 0.0);
	protoTooth[4] = new Vec3(Math.PI/4 - 0.6, outerradius, 0.0);
	protoTooth[5] = new Vec3(Math.PI/4 - 0.3, pitchradius, 0.0);
	protoTooth[6] = new Vec3(Math.PI/4 + 0.2, innerradius, 0.0);

}
else
{
	protoTooth[0] = new Vec3(-Math.PI/2, innerradius, 0.0);
	protoTooth[1] = new Vec3(-Math.PI/4 - 0.2, innerradius, 0.0);
	protoTooth[2] = new Vec3(-Math.PI/4 + 0.3, pitchradius, 0.0);
	protoTooth[3] = new Vec3(-Math.PI/4 + 0.6, outerradius, 0.0);
	protoTooth[4] = new Vec3(Math.PI/4 - 0.6, outerradius, 0.0);
	protoTooth[5] = new Vec3(Math.PI/4 - 0.3, pitchradius, 0.0);
	protoTooth[6] = new Vec3(Math.PI/4 + 0.2, innerradius, 0.0);
}

Vec3[] polaroutline = new Vec3[numTeeth * PROTOSIZE];
float startangle = 0;
for (int i=0;i<numTeeth;i++) {
    for (int j=0;j<PROTOSIZE;j++) {
	polaroutline[PROTOSIZE*i+j] = new Vec3((i + protoTooth[j].x/Math.PI) * toothangle,
					       protoTooth[j].y,
					       0);
    }
}

Vec3[] cartesianoutline = new Vec3[numTeeth * PROTOSIZE];
	float[] smoothness = new float[numTeeth * PROTOSIZE];


for (int i=0;i<numTeeth*PROTOSIZE;i++) {
    cartesianoutline[i] = new Vec3(polaroutline[i].y * Math.cos(polaroutline[i].x),
				   polaroutline[i].y * Math.sin(polaroutline[i].x),
				   0.0);
    smoothness[i] = 0;
}


curve = new Curve(cartesianoutline, smoothness, Mesh.INTERPOLATING, true);
window.addObject(curve, new CoordinateSystem(), "Gear", null);