-
Notifications
You must be signed in to change notification settings - Fork 12
/
gear.go
192 lines (158 loc) · 4.25 KB
/
gear.go
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
package toolbox3d
import (
"math"
"github.com/unixpickle/model3d/model2d"
"github.com/unixpickle/model3d/model3d"
)
type SpurGear struct {
P1 model3d.Coord3D
P2 model3d.Coord3D
Profile GearProfile
}
func (s *SpurGear) Min() model3d.Coord3D {
return s.boundingCylinder().Min()
}
func (s *SpurGear) Max() model3d.Coord3D {
return s.boundingCylinder().Max()
}
func (s *SpurGear) Contains(c model3d.Coord3D) bool {
if !model3d.InBounds(s, c) {
return false
}
v1, v2 := s.P2.Sub(s.P1).OrthoBasis()
return s.Profile.Contains(model2d.Coord{
X: v1.Dot(c),
Y: v2.Dot(c),
})
}
func (s *SpurGear) boundingCylinder() *model3d.CylinderSolid {
return &model3d.CylinderSolid{
P1: s.P1,
P2: s.P2,
Radius: s.Profile.Max().X,
}
}
type HelicalGear struct {
P1 model3d.Coord3D
P2 model3d.Coord3D
Profile GearProfile
Angle float64
}
func (h *HelicalGear) Min() model3d.Coord3D {
return h.boundingCylinder().Min()
}
func (h *HelicalGear) Max() model3d.Coord3D {
return h.boundingCylinder().Max()
}
func (h *HelicalGear) Contains(c model3d.Coord3D) bool {
if !model3d.InBounds(h, c) {
return false
}
axis := h.P2.Sub(h.P1)
v1, v2 := axis.OrthoBasis()
c2 := model2d.Coord{
X: v1.Dot(c),
Y: v2.Dot(c),
}
distUp := axis.Normalize().Dot(c.Sub(h.P1))
theta := math.Tan(h.Angle) * distUp / h.Profile.PitchRadius()
c2 = model2d.NewMatrix2Rotation(theta).MulColumn(c2)
return h.Profile.Contains(c2)
}
func (h *HelicalGear) boundingCylinder() *model3d.CylinderSolid {
return &model3d.CylinderSolid{
P1: h.P1,
P2: h.P2,
Radius: h.Profile.Max().X,
}
}
type GearProfile interface {
model2d.Solid
PitchRadius() float64
}
type involuteGearProfile struct {
rootRadius float64
baseRadius float64
outerRadius float64
pitchRadius float64
toothTheta float64
reflectTheta float64
}
// InvoluteGearProfile creates a GearProfile for a
// standard involute gear with the given specs.
func InvoluteGearProfile(pressureAngle, module, clearance float64, numTeeth int) GearProfile {
radius := module * float64(numTeeth) / 2
baseRadius := math.Cos(pressureAngle) * radius
tForR := math.Sqrt(math.Pow(radius/baseRadius, 2) - 1)
x, y := involuteCoords(tForR)
toothTheta := math.Pi * 2 / float64(numTeeth)
reflectTheta := toothTheta/2 + 2*math.Atan2(y, x)
return &involuteGearProfile{
rootRadius: baseRadius - clearance,
baseRadius: baseRadius,
outerRadius: radius*2 - baseRadius,
pitchRadius: radius,
toothTheta: toothTheta,
reflectTheta: reflectTheta,
}
}
// InvoluteGearProfileSizes creates an involute gear
// profile using different parameters than
// InvoluteGearProfile.
func InvoluteGearProfileSizes(pressureAngle, module, addendum, dedendum float64,
numTeeth int) GearProfile {
radius := module * float64(numTeeth) / 2
baseRadius := math.Cos(pressureAngle) * radius
tForR := math.Sqrt(math.Pow(radius/baseRadius, 2) - 1)
x, y := involuteCoords(tForR)
toothTheta := math.Pi * 2 / float64(numTeeth)
reflectTheta := toothTheta/2 + 2*math.Atan2(y, x)
return &involuteGearProfile{
rootRadius: radius - dedendum,
baseRadius: baseRadius,
outerRadius: radius + addendum,
pitchRadius: radius,
toothTheta: toothTheta,
reflectTheta: reflectTheta,
}
}
func (i *involuteGearProfile) PitchRadius() float64 {
return i.pitchRadius
}
func (i *involuteGearProfile) Min() model2d.Coord {
return model2d.Coord{X: -i.outerRadius, Y: -i.outerRadius}
}
func (i *involuteGearProfile) Max() model2d.Coord {
return i.Min().Scale(-1)
}
func (i *involuteGearProfile) Contains(c model2d.Coord) bool {
if !model2d.InBounds(i, c) {
return false
}
r := c.Norm()
if r < i.rootRadius {
return true
} else if r > i.outerRadius {
return false
}
theta := math.Atan2(c.Y, c.X)
// Move theta into first tooth.
if theta < 0 {
theta += math.Pi * 2
}
_, frac := math.Modf(theta / i.toothTheta)
theta = frac * i.toothTheta
if r < i.baseRadius {
return theta < i.reflectTheta
}
tForR := math.Sqrt(math.Pow(r/i.baseRadius, 2) - 1)
x, y := involuteCoords(tForR)
thetaBound := math.Atan2(y, x)
if theta < thetaBound || i.reflectTheta-theta < thetaBound {
return false
}
return true
}
func involuteCoords(t float64) (float64, float64) {
return math.Cos(t) + t*math.Sin(t), math.Sin(t) - t*math.Cos(t)
}