Skip to content

Commit ade0e8b

Browse files
Enable animation export with Measurements
- Enable animation export with Measurements. - Enable animation export in test PendulumWithDamper_Measurements.jl.
1 parent 07e2e15 commit ade0e8b

File tree

3 files changed

+45
-32
lines changed

3 files changed

+45
-32
lines changed

src/AnimationExport/exportAnimation.jl

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ end
66

77
colorNum(red, green, blue) = ((red*256 + green)*256 + blue)
88

9+
FVal(val) = Modia3D.convertToFloat64(val)
10+
911
const coSysMaterialRed = (; name="coordinateSystem.red", uuid=name2uuid("coordinateSystem.red"), type="MeshPhongMaterial", color=colorNum(255, 0, 0), opacity=1, transparent=false, shininess=0.5)
1012
const coSysMaterialGreen = (; name="coordinateSystem.green", uuid=name2uuid("coordinateSystem.green"), type="MeshPhongMaterial", color=colorNum(0, 255, 0), opacity=1, transparent=false, shininess=0.5)
1113
const coSysMaterialBlue = (; name="coordinateSystem.blue", uuid=name2uuid("coordinateSystem.blue"), type="MeshPhongMaterial", color=colorNum(0, 0, 255), opacity=1, transparent=false, shininess=0.5)
@@ -16,7 +18,7 @@ function exportObject(object, elements, obj::Modia3D.Composition.Object3D, spher
1618
R_obj = Modia3D.NullRotation(Float64)
1719
name = Modia3D.fullName(obj)
1820
geometryName = name * ".geometry"
19-
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="SphereBufferGeometry", radius=sphere.diameter/2, heightSegments=16, widthSegments=32)
21+
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="SphereBufferGeometry", radius=0.5*FVal(sphere.diameter), heightSegments=16, widthSegments=32)
2022
material = printVisuMaterialToJSON(obj, obj.visualMaterial)
2123
objectInfo = getObjectInfo(name, geometry, material, initPos, initRot)
2224
printInfoToFile(object, elements, geometry, material, nothing, objectInfo)
@@ -26,11 +28,12 @@ end
2628
function exportObject(object, elements, obj::Modia3D.Composition.Object3D, ellipsoid::Modia3D.Shapes.Ellipsoid, initPos, initRot)
2729
r_obj = Modia3D.ZeroVector3D(Float64)
2830
R_obj = Modia3D.NullRotation(Float64)
31+
lengthX = FVal(ellipsoid.lengthX)
2932
name = Modia3D.fullName(obj)
3033
geometryName = name * ".geometry"
31-
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="SphereBufferGeometry", radius=ellipsoid.lengthX/2, heightSegments=16, widthSegments=32)
34+
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="SphereBufferGeometry", radius=0.5*lengthX, heightSegments=16, widthSegments=32)
3235
material = printVisuMaterialToJSON(obj, obj.visualMaterial)
33-
objectInfo = getObjectInfo(name, geometry, material, initPos, initRot, scale=[1.0, ellipsoid.lengthY/ellipsoid.lengthX, ellipsoid.lengthZ/ellipsoid.lengthX])
36+
objectInfo = getObjectInfo(name, geometry, material, initPos, initRot, scale=[1.0, FVal(ellipsoid.lengthY)/lengthX, FVal(ellipsoid.lengthZ)/lengthX])
3437
printInfoToFile(object, elements, geometry, material, nothing, objectInfo)
3538
return (r_obj, R_obj)
3639
end
@@ -40,39 +43,42 @@ function exportObject(object, elements, obj::Modia3D.Composition.Object3D, box::
4043
R_obj = Modia3D.NullRotation(Float64)
4144
name = Modia3D.fullName(obj)
4245
geometryName = name * ".geometry"
43-
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="BoxBufferGeometry", width=box.lengthX, height=box.lengthY, depth=box.lengthZ)
46+
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="BoxBufferGeometry", width=FVal(box.lengthX), height=FVal(box.lengthY), depth=FVal(box.lengthZ))
4447
material = printVisuMaterialToJSON(obj, obj.visualMaterial)
4548
objectInfo = getObjectInfo(name, geometry, material, initPos, initRot)
4649
printInfoToFile(object, elements, geometry, material, nothing, objectInfo)
4750
return (r_obj, R_obj)
4851
end
4952

5053
function exportObject(object, elements, obj::Modia3D.Composition.Object3D, cylinder::Modia3D.Shapes.Cylinder, initPos, initRot)
54+
radius = 0.5*FVal(cylinder.diameter)
55+
innerRadius = 0.5*FVal(cylinder.innerDiameter)
56+
length = FVal(cylinder.length)
5157
name = Modia3D.fullName(obj)
5258
geometryName = name * ".geometry"
5359
material = printVisuMaterialToJSON(obj, obj.visualMaterial)
54-
if cylinder.innerDiameter == 0.0
60+
if innerRadius == 0.0
5561
r_obj = Modia3D.ZeroVector3D(Float64)
5662
R_obj = Shapes.rotateAxis2y(cylinder.axis, Modia3D.NullRotation(Float64))
5763
shape = nothing
58-
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="CylinderBufferGeometry", radiusBottom=cylinder.diameter/2, radiusTop=cylinder.diameter/2, height=cylinder.length, radialSegments=32, heightSegments=1)
64+
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="CylinderBufferGeometry", radiusBottom=radius, radiusTop=radius, height=length, radialSegments=32, heightSegments=1)
5965
objectInfo = getObjectInfo(name, geometry, material, initPos, initRot, R_obj=R_obj)
6066
else
6167
if cylinder.axis == 1
62-
r_obj = @SVector[-cylinder.length/2, 0.0, 0.0]
68+
r_obj = @SVector[-0.5*length, 0.0, 0.0]
6369
elseif cylinder.axis == 2
64-
r_obj = @SVector[0.0, -cylinder.length/2, 0.0]
70+
r_obj = @SVector[0.0, -0.5*length, 0.0]
6571
else
66-
r_obj = @SVector[0.0, 0.0, -cylinder.length/2]
72+
r_obj = @SVector[0.0, 0.0, -0.5*length]
6773
end
6874
R_obj = Shapes.rotateAxis2z(cylinder.axis, Modia3D.NullRotation(Float64))
69-
innerCurves = [(; type="EllipseCurve", aX=0, aY=0, xRadius=cylinder.innerDiameter/2, yRadius=cylinder.innerDiameter/2, aStartAngle=0, aEndAngle=2*pi, aClockwise=false, aRotation=0)]
75+
innerCurves = [(; type="EllipseCurve", aX=0, aY=0, xRadius=innerRadius, yRadius=innerRadius, aStartAngle=0, aEndAngle=2*pi, aClockwise=false, aRotation=0)]
7076
holes = [(; type="Path", curves=innerCurves, currentPoint=[0, 0])]
71-
curves = [(; type="EllipseCurve", aX=0, aY=0, xRadius=cylinder.diameter/2, yRadius=cylinder.diameter/2, aStartAngle=0, aEndAngle=2*pi, aClockwise=false, aRotation=0)]
77+
curves = [(; type="EllipseCurve", aX=0, aY=0, xRadius=radius, yRadius=radius, aStartAngle=0, aEndAngle=2*pi, aClockwise=false, aRotation=0)]
7278
shapeName = String(Modia3D.fullName(obj)) * ".shape"
7379
shapeUuid = name2uuid(shapeName)
7480
shape = (; name=shapeName, uuid=shapeUuid, type="Shape", curves=curves, holes=holes, currentPoint=[0, 0])
75-
options = (; depth=cylinder.length, bevelEnabled=false)
81+
options = (; depth=length, bevelEnabled=false)
7682
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="ExtrudeGeometry", shapes=[shapeUuid], options=options)
7783
objectInfo = getObjectInfo(name, geometry, material, initPos, initRot, r_obj=r_obj, R_obj=R_obj)
7884
end
@@ -81,17 +87,18 @@ function exportObject(object, elements, obj::Modia3D.Composition.Object3D, cylin
8187
end
8288

8389
function exportObject(object, elements, obj::Modia3D.Composition.Object3D, cone::Modia3D.Shapes.Cone, initPos, initRot)
90+
length = FVal(cone.length)
8491
if cone.axis == 1
85-
r_obj = @SVector[cone.length/2, 0.0, 0.0]
92+
r_obj = @SVector[0.5*length, 0.0, 0.0]
8693
elseif cone.axis == 2
87-
r_obj = @SVector[0.0, cone.length/2, 0.0]
94+
r_obj = @SVector[0.0, 0.5*length, 0.0]
8895
else
89-
r_obj = @SVector[0.0, 0.0, cone.length/2]
96+
r_obj = @SVector[0.0, 0.0, 0.5*length]
9097
end
9198
R_obj = Shapes.rotateAxis2y(cone.axis, Modia3D.NullRotation(Float64))
9299
name = Modia3D.fullName(obj)
93100
geometryName = name * ".geometry"
94-
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="CylinderBufferGeometry", radiusBottom=cone.diameter/2, radiusTop=cone.topDiameter/2, height=cone.length, radialSegments=32, heightSegments=1)
101+
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="CylinderBufferGeometry", radiusBottom=0.5*FVal(cone.diameter), radiusTop=0.5*FVal(cone.topDiameter), height=length, radialSegments=32, heightSegments=1)
95102
material = printVisuMaterialToJSON(obj, obj.visualMaterial)
96103
objectInfo = getObjectInfo(name, geometry, material, initPos, initRot, r_obj=r_obj, R_obj=R_obj)
97104
printInfoToFile(object, elements, geometry, material, nothing, objectInfo)
@@ -101,17 +108,19 @@ end
101108
function exportObject(object, elements, obj::Modia3D.Composition.Object3D, capsule::Modia3D.Shapes.Capsule, initPos, initRot)
102109
r_obj = Modia3D.ZeroVector3D(Float64)
103110
R_obj = Shapes.rotateAxis2y(capsule.axis, Modia3D.NullRotation(Float64))
111+
radius = 0.5*FVal(capsule.diameter)
112+
length = FVal(capsule.length)
104113
name = Modia3D.fullName(obj)
105114
geometryName = name * ".geometry"
106115
points = []
107116
for i in -9:0
108117
angle = i/9 * pi/2
109-
point = (; x=capsule.diameter/2*cos(angle), y=-capsule.length/2+capsule.diameter/2*sin(angle))
118+
point = (; x=radius*cos(angle), y=-0.5*length+radius*sin(angle))
110119
push!(points, point)
111120
end
112121
for i in 0:9
113122
angle = i/9 * pi/2
114-
point = (; x=capsule.diameter/2*cos(angle), y=capsule.length/2+capsule.diameter/2*sin(angle))
123+
point = (; x=radius*cos(angle), y=0.5*length+radius*sin(angle))
115124
push!(points, point)
116125
end
117126
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="LatheGeometry", points=points, phiStart=0, phiLength=2*pi, segments=32)
@@ -122,26 +131,29 @@ function exportObject(object, elements, obj::Modia3D.Composition.Object3D, capsu
122131
end
123132

124133
function exportObject(object, elements, obj::Modia3D.Composition.Object3D, beam::Modia3D.Shapes.Beam, initPos, initRot)
134+
length = FVal(beam.length)
135+
width = FVal(beam.width)
136+
thickness = FVal(beam.thickness)
125137
if beam.axis == 1
126-
r_obj = @SVector[0.0, 0.0, -beam.thickness/2]
138+
r_obj = @SVector[0.0, 0.0, -0.5*thickness]
127139
elseif beam.axis == 2
128-
r_obj = @SVector[-beam.thickness/2, 0.0, 0.0]
140+
r_obj = @SVector[-0.5*thickness, 0.0, 0.0]
129141
else
130-
r_obj = @SVector[0.0, -beam.thickness/2, 0.0]
142+
r_obj = @SVector[0.0, -0.5*thickness, 0.0]
131143
end
132144
R_obj = Shapes.rotateAxis2x(beam.axis, Modia3D.NullRotation(Float64))
133145
name = Modia3D.fullName(obj)
134146
geometryName = name * ".geometry"
135147
curves = [
136-
(; type="LineCurve", v1=[-beam.length/2, -beam.width/2], v2=[beam.length/2, -beam.width/2]),
137-
(; type="EllipseCurve", aX=beam.length/2, aY=0, xRadius=beam.width/2, yRadius=beam.width/2, aStartAngle=-pi/2, aEndAngle=pi/2, aClockwise=false, aRotation=0),
138-
(; type="LineCurve", v1=[beam.length/2, beam.width/2], v2=[-beam.length/2, beam.width/2]),
139-
(; type="EllipseCurve", aX=-beam.length/2, aY=0, xRadius=beam.width/2, yRadius=beam.width/2, aStartAngle=pi/2, aEndAngle=-pi/2, aClockwise=false, aRotation=0)
148+
(; type="LineCurve", v1=[-0.5*length, -0.5*width], v2=[0.5*length, -0.5*width]),
149+
(; type="EllipseCurve", aX=0.5*length, aY=0, xRadius=0.5*width, yRadius=0.5*width, aStartAngle=-pi/2, aEndAngle=pi/2, aClockwise=false, aRotation=0),
150+
(; type="LineCurve", v1=[0.5*length, 0.5*width], v2=[-0.5*length, 0.5*width]),
151+
(; type="EllipseCurve", aX=-0.5*length, aY=0, xRadius=0.5*width, yRadius=0.5*width, aStartAngle=pi/2, aEndAngle=-pi/2, aClockwise=false, aRotation=0)
140152
]
141153
shapeName = name * ".shape"
142154
shapeUuid = name2uuid(shapeName)
143155
shape = (; name=shapeName, uuid=shapeUuid, type="Shape", curves=curves, holes=[], currentPoint=[0, 0])
144-
options = (; depth=beam.thickness, bevelEnabled=false)
156+
options = (; depth=thickness, bevelEnabled=false)
145157
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="ExtrudeGeometry", shapes=[shapeUuid], options=options)
146158
material = printVisuMaterialToJSON(obj, obj.visualMaterial)
147159
objectInfo = getObjectInfo(name, geometry, material, initPos, initRot, r_obj=r_obj, R_obj=R_obj)

src/Composition/dynamics.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function getJointsAndForceElementsAndObject3DsWithoutParents!(evaluatedParameter
1010
if typeof(value.feature) <: Scene
1111
push!(object3DWithoutParents, value)
1212
else
13-
error("\n", value.path, " is an Object3D that has no parent, but no feature=Scene(..)!\n")
13+
error("\n", value.path, " is an Object3D that has no parent, but no feature=Scene(..)!\n")
1414
end
1515
elseif typeof(value.feature) <: Scene
1616
error("\n", value.path, " is an Object3D that has feature=Scene(..) and has a parent (= ", value.parent.path, ")!\n")
@@ -404,8 +404,8 @@ function multibodyResiduals3!(sim::ModiaLang.SimulationModel{F}, scene, world, t
404404
TimerOutputs.@timeit sim.timer "Modia3D_3 exportAnimation" begin
405405
objectData = []
406406
for obj in scene.allVisuElements
407-
pos = obj.r_abs
408-
ori = Modia3D.from_R(obj.R_abs)
407+
pos = Modia3D.convertToFloat64(obj.r_abs)
408+
ori = Modia3D.from_R(Modia3D.convertToFloat64(obj.R_abs))
409409
dat = animationData(pos, ori)
410410
push!(objectData, dat)
411411
end

test/Basic/PendulumWithDamper_Measurements.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ Pendulum = Model(
1212
g = 9.81,
1313
vmat1 = VisualMaterial(color="Sienna", transparency=0.5),
1414
vmat2 = VisualMaterial(color="Red"),
15-
world = Object3D(feature=Scene(enableContactDetection=false)),
15+
world = Object3D(feature=Scene(enableContactDetection=false,
16+
animationFile="PendulumWithDamper_Measurements.json")),
1617
worldFrame = Object3D(parent=:world,
1718
feature=Visual(shape=CoordinateSystem(length=0.5))),
1819

@@ -28,7 +29,7 @@ Pendulum = Model(
2829
)
2930

3031
PendulumWithDamp = Model(
31-
pendulum = buildModia3D(Pendulum | Map(m=2.0 ± 1.0, rev=Map(phi=Var(init=1.0)))),
32+
pendulum = buildModia3D(Pendulum | Map(m=2.0±1.0, rev=Map(phi=Var(init=1.0)))),
3233

3334
damper = Damper | Map(d=0.5),
3435
fixed = Fixed,
@@ -46,6 +47,6 @@ requiredFinalStates = [-1.578178283450938, 0.061515170100766486]
4647
simulate!(pendulumWithDamper, stopTime=stopTime, log=true, logStates=false, requiredFinalStates=requiredFinalStates)
4748

4849
@usingModiaPlot
49-
plot(pendulumWithDamper, ["pendulum.rev.flange.phi", "pendulum.rev.variables[1]"], figure=1)
50+
plot(pendulumWithDamper, ["pendulum.rev.flange.phi", "pendulum.rev.w"], figure=1)
5051

5152
end

0 commit comments

Comments
 (0)