-
Notifications
You must be signed in to change notification settings - Fork 25
/
shapes.jl
395 lines (334 loc) · 13.1 KB
/
shapes.jl
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
"""
Shape{T}
Abstract type; Subtypes contain geometric and visual information for a Body.
"""
abstract type Shape{T} end
"""
EmptyShape{T} <: Shape{T}
Contains no geometric or visual information
"""
struct EmptyShape{T} <: Shape{T}
EmptyShape() = new{Float64}()
end
function Base.show(io::IO, mime::MIME{Symbol("text/plain")}, shape::EmptyShape)
summary(io, shape)
println(io, "")
end
#TODO: change to MeshShape
"""
Mesh{T} <: Shape{T}
Contains geometric and visual information based on .obj file
"""
mutable struct Mesh{T} <: Shape{T}
position_offset::SVector{3,T}
orientation_offset::Quaternion{T}
path::String
scale::SVector{3,T}
color::RGBA
function Mesh(path::String;
position_offset::AbstractVector=szeros(3),
orientation_offset::Quaternion=one(Quaternion),
scale::AbstractVector=sones(3),
color=RGBA(0.75, 0.75, 0.75))
T = promote_type(quateltype.((position_offset, orientation_offset))...)
new{T}(position_offset, orientation_offset, path, scale, color)
end
function Mesh(path::String, m::Real, J::AbstractMatrix;
position_offset::AbstractVector=szeros(3),
orientation_offset::Quaternion=one(Quaternion),
scale::AbstractVector=sones(3),
name::Symbol=Symbol("body_" * randstring(4)),
color=RGBA(0.75, 0.75, 0.75))
T = promote_type(quateltype.((m, J, position_offset, orientation_offset))...)
return Body(m, J; name=name, shape=new{T}(position_offset, orientation_offset, path, scale, color))
end
end
"""
Box{T} <: Shape{T}
Cuboid geometry
position_offset: geometry origin offset from center of mass
orientation_offset: orientation offset from body frame
xyz: dimensions (meters)
scale: scaling
color: RGBA
"""
mutable struct Box{T} <: Shape{T}
position_offset::SVector{3,T}
orientation_offset::Quaternion{T}
xyz::SVector{3,T}
scale::SVector{3,T}
color::RGBA
function Box(x::Real, y::Real, z::Real;
position_offset::AbstractVector=szeros(3),
orientation_offset::Quaternion=one(Quaternion),
scale::AbstractVector=sones(3),
color=RGBA(0.75, 0.75, 0.75))
T = promote_type(quateltype.((x, y, z, position_offset, orientation_offset))...)
new{T}(position_offset, orientation_offset, [x; y; z], scale, color)
end
function Box(x::Real, y::Real, z::Real, m::Real;
position_offset::AbstractVector=szeros(3),
orientation_offset::Quaternion=one(Quaternion),
scale::AbstractVector=sones(3),
name::Symbol=Symbol("body_" * randstring(4)),
color=RGBA(0.75, 0.75, 0.75))
T = promote_type(quateltype.((x, y, z, m, position_offset, orientation_offset))...)
J = 1 / 12 * m * diagm([y^2 + z^2; x^2 + z^2; x^2 + y^2])
return Body(m, J; name=name, shape=new{T}(position_offset, orientation_offset, [x;y;z], scale, color))
end
end
"""
Cylinder{T} <: Shape{T}
cylinder geometry
position_offset: geometry origin offset from center of mass
orientation_offset: orientation offset from body frame
rh: radius and height dimensions (meters)
scale: scaling
color: RGBA
"""
mutable struct Cylinder{T} <: Shape{T}
position_offset::SVector{3,T}
orientation_offset::Quaternion{T}
rh::SVector{2,T}
scale::SVector{3,T}
color::RGBA
# Cylinder points in the z direction
function Cylinder(r::Real, h::Real;
position_offset::AbstractVector=szeros(3),
orientation_offset::Quaternion=one(Quaternion),
scale::AbstractVector=sones(3),
color=RGBA(0.75, 0.75, 0.75))
T = promote_type(quateltype.((r, h, position_offset, orientation_offset))...)
new{T}(position_offset, orientation_offset, [r;h], scale, color)
end
function Cylinder(r::Real, h::Real, m::Real;
position_offset::AbstractVector=szeros(3),
orientation_offset::Quaternion=one(Quaternion),
scale::AbstractVector=sones(3),
name::Symbol=Symbol("body_" * randstring(4)),
color=RGBA(0.75, 0.75, 0.75))
T = promote_type(quateltype.((r, h, m, position_offset, orientation_offset))...)
J = 1 / 2 * m * diagm([r^2 + 1 / 6 * h^2; r^2 + 1 / 6 * h^2; r^2])
return Body(m, J; name=name, shape=new{T}(position_offset, orientation_offset, [r;h], scale, color))
end
end
"""
Capsule geometry created as a CombinedShapes
position_offset: geometry origin offset from center of mass
orientation_offset: orientation offset from body frame
rh: radius and height dimensions (meters)
scale: scaling
color: RGBA
"""
function Capsule(r::Real, h::Real;
position_offset::AbstractVector=szeros(3),
orientation_offset::Quaternion=one(Quaternion),
scale::AbstractVector=sones(3),
color=RGBA(0.75, 0.75, 0.75))
T = promote_type(quateltype.((r, h, position_offset, orientation_offset))...)
cylinder = Cylinder(r, h)
cap1 = Sphere(r; position_offset=[0;0;h/2])
cap2 = Sphere(r; position_offset=[0;0;-h/2])
CombinedShapes([cylinder;cap1;cap2]; position_offset, orientation_offset, scale, color)
end
function Capsule(r::Real, h::Real, m::Real;
position_offset::AbstractVector=szeros(3),
orientation_offset::Quaternion=one(Quaternion),
scale::AbstractVector=sones(3),
name::Symbol=Symbol("body_" * randstring(4)),
color=RGBA(0.75, 0.75, 0.75))
T = promote_type(quateltype.((r, h, m, position_offset, orientation_offset))...)
volume_cylinder = π * h * r^2.0
volume_hemisphere = π * 4.0 / 3.0 * r^3.0 / 2.0
volume_total = volume_cylinder + 2 * volume_hemisphere
mass_cylinder = m * volume_cylinder / volume_total
mass_hemisphere = m * volume_hemisphere / volume_total
Ixx_cylinder = mass_cylinder * (1.0 / 12.0 * h^2.0 + 1.0 / 4.0 * r^2.0)
Izz_cylinder = mass_cylinder * 1.0 / 2.0 * r^2.0
Ixx_hemisphere = 83.0 / 320 * mass_hemisphere * r^2
Izz_hemisphere = mass_hemisphere * 2.0 / 5.0 * r^2 / 2.0
d = (3.0 / 8.0 * r + 0.5 * h)
Ixx = Ixx_cylinder + 2.0 * (Ixx_hemisphere + mass_hemisphere * d^2.0)
Izz = Izz_cylinder + Izz_hemisphere * 2.0
J = diagm([Ixx; Ixx; Izz])
return Body(m, J; name=name, shape=Capsule(r, h; position_offset, orientation_offset, scale, color))
end
"""
CombinedShapes{T} <: Shape{T}
composite geometry
position_offset: geometry origin offset from center of mass
orientation_offset: orientation offset from body frame
shape: list of Shape objects
xyz: dimensions (meters)
"""
mutable struct CombinedShapes{T} <: Shape{T}
position_offset::SVector{3,T}
orientation_offset::Quaternion{T}
shapes::Vector{<:Shape{T}}
scale::SVector{3,T}
color::RGBA
function CombinedShapes(shapes::Vector{<:Shape{T}};
position_offset::AbstractVector=szeros(3),
orientation_offset::Quaternion=one(Quaternion),
scale::AbstractVector=sones(3),
color=RGBA(0.75, 0.75, 0.75)) where T
new{T}(position_offset, orientation_offset, shapes, scale, color)
end
function CombinedShapes(shapes::Vector{<:Shape{T}}, m::T, J;
position_offset::AbstractVector=szeros(3),
orientation_offset::Quaternion=one(Quaternion),
name::Symbol=Symbol("body_" * randstring(4)),
scale::AbstractVector=sones(3),
color=RGBA(0.75, 0.75, 0.75)) where T
Body(m, J; name=name, shape=new{T}(position_offset, orientation_offset, shapes, scale, color))
end
end
"""
Sphere{T} <: Shape{T}
sphere geometry
position_offset: geometry origin offset from center of mass
orientation_offset: orientation offset from body frame
r: radius (meters)
scale: scaling
color: RGBA
"""
mutable struct Sphere{T} <: Shape{T}
position_offset::SVector{3,T}
orientation_offset::Quaternion{T}
r::T
scale::SVector{3,T}
color::RGBA
function Sphere(r::Real;
position_offset::AbstractVector=szeros(3),
orientation_offset::Quaternion=one(Quaternion),
scale::AbstractVector=sones(3),
color=RGBA(0.75, 0.75, 0.75))
T = promote_type(quateltype.((r, position_offset, orientation_offset))...)
new{T}(position_offset, orientation_offset, r, scale, color)
end
function Sphere(r::Real, m::Real;
position_offset::AbstractVector=szeros(3),
orientation_offset::Quaternion=one(Quaternion),
scale::AbstractVector=sones(3),
name::Symbol=Symbol("body_" * randstring(4)),
color=RGBA(0.75, 0.75, 0.75))
T = promote_type(quateltype.((r, m, position_offset, orientation_offset))...)
J = 2 / 5 * m * diagm([r^2 for i = 1:3])
return Body(m, J; name=name, shape=new{T}(position_offset, orientation_offset, r, scale, color))
end
end
"""
Pyramid{T} <: Shape{T}
pyramid geometry
position_offset: geometry origin offset from center of mass
orientation_offset: orientation offset from body frame
wh: width and height dimensions (meters)
scale: scaling
color: RGBA
"""
mutable struct Pyramid{T} <: Shape{T}
position_offset::SVector{3,T}
orientation_offset::Quaternion{T}
wh::SVector{2,T}
scale::SVector{3,T}
color::RGBA
# Pyramid points in the z direction, Center of mass at 1/4 h
function Pyramid(w::Real, h::Real;
position_offset::AbstractVector=szeros(3),
orientation_offset::Quaternion=one(Quaternion),
scale::AbstractVector=sones(3),
color=RGBA(0.75, 0.75, 0.75))
T = promote_type(quateltype.((w, h, position_offset, orientation_offset))...)
new{T}(position_offset, orientation_offset, [w;h], scale, color)
end
function Pyramid(w::Real, h::Real, m::Real;
position_offset::AbstractVector=szeros(3),
orientation_offset::Quaternion=one(Quaternion),
scale::AbstractVector=sones(3),
name::Symbol=Symbol("body_" * randstring(4)), color=RGBA(0.75, 0.75, 0.75))
T = promote_type(quateltype.((w, h, m, position_offset, orientation_offset))...)
J = 1/80 * m * diagm([4*w^2+3*h^2;4*w^2+3*h^2;8*w^2])
return Body(m, J; name=name, shape=new{T}(position_offset, orientation_offset, [w;h], scale, color))
end
end
"""
FrameShape{T} <: Shape{T}
coordinate frame geometry
position_offset: geometry origin offset from center of mass
orientation_offset: orientation offset from body frame
scale: scaling
color: not used
"""
mutable struct FrameShape{T} <: Shape{T}
position_offset::SVector{3,T}
orientation_offset::Quaternion{T}
scale::SVector{3,T}
color::RGBA
function FrameShape(;
position_offset::AbstractVector=szeros(3),
orientation_offset::Quaternion=one(Quaternion),
scale::AbstractVector=sones(3),
color=RGBA(0.75, 0.75, 0.75))
T = promote_type(quateltype.((position_offset, orientation_offset))...)
new{T}(position_offset, orientation_offset, scale, color)
end
function FrameShape(m::Real;
position_offset::AbstractVector=szeros(3),
orientation_offset::Quaternion=one(Quaternion),
scale::AbstractVector=sones(3),
name::Symbol=Symbol("body_" * randstring(4)), color=RGBA(0.75, 0.75, 0.75))
T = promote_type(quateltype.((m, position_offset, orientation_offset))...)
J = m * sI(3)
return Body(m, J; name=name, shape=new{T}(position_offset, orientation_offset, scale, color))
end
end
function Base.show(io::IO, mime::MIME{Symbol("text/plain")}, shape::Shape)
summary(io, shape)
println(io,"")
println(io," position_offset: "*string(shape.position_offset))
println(io," orientation_offset: "*string(shape.orientation_offset))
println(io," scale: "*string(shape.scale))
println(io," color: "*string(shape.color))
end
function convert_shape(box::Box)
x, y, z = Tuple(box.xyz)
return GeometryBasics.HyperRectangle(Vec(-x / 2.0, -y / 2.0, -z / 2.0), Vec(x, y, z))
end
function convert_shape(cylinder::Cylinder)
r, h = Tuple(cylinder.rh)
return GeometryBasics.Cylinder(Point(0.0, 0.0, -h / 2.0),Point(0.0, 0.0, h / 2.0), r)
end
function convert_shape(sphere::Sphere)
r = sphere.r
return GeometryBasics.Sphere(Point(0.0, 0.0, 0.0), r)
end
function convert_shape(pyramid::Pyramid)
w, h = Tuple(pyramid.wh)
return GeometryBasics.Pyramid(Point(0.0, 0.0, -h / 4.0), h, w)
end
function convert_shape(frame::FrameShape)
return MeshCat.Triad()
end
function convert_shape(mesh::Mesh)
return MeshFileObject(mesh.path)
end
function convert_shape(::EmptyShape)
return nothing
end
function convert_shape(combinedshapes::CombinedShapes)
geom = []
for shape in combinedshapes.shapes
push!(geom, convert_shape(shape))
end
return geom
end
# color
set_color!(shape::EmptyShape, color) = nothing
function set_color!(shape::Shape, color)
shape.color = color
end
function set_color!(shapes::CombinedShapes, color)
for i in eachindex(shapes)
shapes.shape[i].color = color
end
end