Skip to content

Commit

Permalink
Merge pull request #89 from ModiaSim/gh_enableAnimationExportWithMeas…
Browse files Browse the repository at this point in the history
…urements

Gh enable animation export with measurements
  • Loading branch information
MartinOtter authored Feb 4, 2022
2 parents e066992 + ade0e8b commit c593074
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 84 deletions.
64 changes: 38 additions & 26 deletions src/AnimationExport/exportAnimation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ end

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

FVal(val) = Modia3D.convertToFloat64(val)

const coSysMaterialRed = (; name="coordinateSystem.red", uuid=name2uuid("coordinateSystem.red"), type="MeshPhongMaterial", color=colorNum(255, 0, 0), opacity=1, transparent=false, shininess=0.5)
const coSysMaterialGreen = (; name="coordinateSystem.green", uuid=name2uuid("coordinateSystem.green"), type="MeshPhongMaterial", color=colorNum(0, 255, 0), opacity=1, transparent=false, shininess=0.5)
const coSysMaterialBlue = (; name="coordinateSystem.blue", uuid=name2uuid("coordinateSystem.blue"), type="MeshPhongMaterial", color=colorNum(0, 0, 255), opacity=1, transparent=false, shininess=0.5)
Expand All @@ -16,7 +18,7 @@ function exportObject(object, elements, obj::Modia3D.Composition.Object3D, spher
R_obj = Modia3D.NullRotation(Float64)
name = Modia3D.fullName(obj)
geometryName = name * ".geometry"
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="SphereBufferGeometry", radius=sphere.diameter/2, heightSegments=16, widthSegments=32)
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="SphereBufferGeometry", radius=0.5*FVal(sphere.diameter), heightSegments=16, widthSegments=32)
material = printVisuMaterialToJSON(obj, obj.visualMaterial)
objectInfo = getObjectInfo(name, geometry, material, initPos, initRot)
printInfoToFile(object, elements, geometry, material, nothing, objectInfo)
Expand All @@ -26,11 +28,12 @@ end
function exportObject(object, elements, obj::Modia3D.Composition.Object3D, ellipsoid::Modia3D.Shapes.Ellipsoid, initPos, initRot)
r_obj = Modia3D.ZeroVector3D(Float64)
R_obj = Modia3D.NullRotation(Float64)
lengthX = FVal(ellipsoid.lengthX)
name = Modia3D.fullName(obj)
geometryName = name * ".geometry"
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="SphereBufferGeometry", radius=ellipsoid.lengthX/2, heightSegments=16, widthSegments=32)
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="SphereBufferGeometry", radius=0.5*lengthX, heightSegments=16, widthSegments=32)
material = printVisuMaterialToJSON(obj, obj.visualMaterial)
objectInfo = getObjectInfo(name, geometry, material, initPos, initRot, scale=[1.0, ellipsoid.lengthY/ellipsoid.lengthX, ellipsoid.lengthZ/ellipsoid.lengthX])
objectInfo = getObjectInfo(name, geometry, material, initPos, initRot, scale=[1.0, FVal(ellipsoid.lengthY)/lengthX, FVal(ellipsoid.lengthZ)/lengthX])
printInfoToFile(object, elements, geometry, material, nothing, objectInfo)
return (r_obj, R_obj)
end
Expand All @@ -40,39 +43,42 @@ function exportObject(object, elements, obj::Modia3D.Composition.Object3D, box::
R_obj = Modia3D.NullRotation(Float64)
name = Modia3D.fullName(obj)
geometryName = name * ".geometry"
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="BoxBufferGeometry", width=box.lengthX, height=box.lengthY, depth=box.lengthZ)
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="BoxBufferGeometry", width=FVal(box.lengthX), height=FVal(box.lengthY), depth=FVal(box.lengthZ))
material = printVisuMaterialToJSON(obj, obj.visualMaterial)
objectInfo = getObjectInfo(name, geometry, material, initPos, initRot)
printInfoToFile(object, elements, geometry, material, nothing, objectInfo)
return (r_obj, R_obj)
end

function exportObject(object, elements, obj::Modia3D.Composition.Object3D, cylinder::Modia3D.Shapes.Cylinder, initPos, initRot)
radius = 0.5*FVal(cylinder.diameter)
innerRadius = 0.5*FVal(cylinder.innerDiameter)
length = FVal(cylinder.length)
name = Modia3D.fullName(obj)
geometryName = name * ".geometry"
material = printVisuMaterialToJSON(obj, obj.visualMaterial)
if cylinder.innerDiameter == 0.0
if innerRadius == 0.0
r_obj = Modia3D.ZeroVector3D(Float64)
R_obj = Shapes.rotateAxis2y(cylinder.axis, Modia3D.NullRotation(Float64))
shape = nothing
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="CylinderBufferGeometry", radiusBottom=cylinder.diameter/2, radiusTop=cylinder.diameter/2, height=cylinder.length, radialSegments=32, heightSegments=1)
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="CylinderBufferGeometry", radiusBottom=radius, radiusTop=radius, height=length, radialSegments=32, heightSegments=1)
objectInfo = getObjectInfo(name, geometry, material, initPos, initRot, R_obj=R_obj)
else
if cylinder.axis == 1
r_obj = @SVector[-cylinder.length/2, 0.0, 0.0]
r_obj = @SVector[-0.5*length, 0.0, 0.0]
elseif cylinder.axis == 2
r_obj = @SVector[0.0, -cylinder.length/2, 0.0]
r_obj = @SVector[0.0, -0.5*length, 0.0]
else
r_obj = @SVector[0.0, 0.0, -cylinder.length/2]
r_obj = @SVector[0.0, 0.0, -0.5*length]
end
R_obj = Shapes.rotateAxis2z(cylinder.axis, Modia3D.NullRotation(Float64))
innerCurves = [(; type="EllipseCurve", aX=0, aY=0, xRadius=cylinder.innerDiameter/2, yRadius=cylinder.innerDiameter/2, aStartAngle=0, aEndAngle=2*pi, aClockwise=false, aRotation=0)]
innerCurves = [(; type="EllipseCurve", aX=0, aY=0, xRadius=innerRadius, yRadius=innerRadius, aStartAngle=0, aEndAngle=2*pi, aClockwise=false, aRotation=0)]
holes = [(; type="Path", curves=innerCurves, currentPoint=[0, 0])]
curves = [(; type="EllipseCurve", aX=0, aY=0, xRadius=cylinder.diameter/2, yRadius=cylinder.diameter/2, aStartAngle=0, aEndAngle=2*pi, aClockwise=false, aRotation=0)]
curves = [(; type="EllipseCurve", aX=0, aY=0, xRadius=radius, yRadius=radius, aStartAngle=0, aEndAngle=2*pi, aClockwise=false, aRotation=0)]
shapeName = String(Modia3D.fullName(obj)) * ".shape"
shapeUuid = name2uuid(shapeName)
shape = (; name=shapeName, uuid=shapeUuid, type="Shape", curves=curves, holes=holes, currentPoint=[0, 0])
options = (; depth=cylinder.length, bevelEnabled=false)
options = (; depth=length, bevelEnabled=false)
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="ExtrudeGeometry", shapes=[shapeUuid], options=options)
objectInfo = getObjectInfo(name, geometry, material, initPos, initRot, r_obj=r_obj, R_obj=R_obj)
end
Expand All @@ -81,17 +87,18 @@ function exportObject(object, elements, obj::Modia3D.Composition.Object3D, cylin
end

function exportObject(object, elements, obj::Modia3D.Composition.Object3D, cone::Modia3D.Shapes.Cone, initPos, initRot)
length = FVal(cone.length)
if cone.axis == 1
r_obj = @SVector[cone.length/2, 0.0, 0.0]
r_obj = @SVector[0.5*length, 0.0, 0.0]
elseif cone.axis == 2
r_obj = @SVector[0.0, cone.length/2, 0.0]
r_obj = @SVector[0.0, 0.5*length, 0.0]
else
r_obj = @SVector[0.0, 0.0, cone.length/2]
r_obj = @SVector[0.0, 0.0, 0.5*length]
end
R_obj = Shapes.rotateAxis2y(cone.axis, Modia3D.NullRotation(Float64))
name = Modia3D.fullName(obj)
geometryName = name * ".geometry"
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="CylinderBufferGeometry", radiusBottom=cone.diameter/2, radiusTop=cone.topDiameter/2, height=cone.length, radialSegments=32, heightSegments=1)
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)
material = printVisuMaterialToJSON(obj, obj.visualMaterial)
objectInfo = getObjectInfo(name, geometry, material, initPos, initRot, r_obj=r_obj, R_obj=R_obj)
printInfoToFile(object, elements, geometry, material, nothing, objectInfo)
Expand All @@ -101,17 +108,19 @@ end
function exportObject(object, elements, obj::Modia3D.Composition.Object3D, capsule::Modia3D.Shapes.Capsule, initPos, initRot)
r_obj = Modia3D.ZeroVector3D(Float64)
R_obj = Shapes.rotateAxis2y(capsule.axis, Modia3D.NullRotation(Float64))
radius = 0.5*FVal(capsule.diameter)
length = FVal(capsule.length)
name = Modia3D.fullName(obj)
geometryName = name * ".geometry"
points = []
for i in -9:0
angle = i/9 * pi/2
point = (; x=capsule.diameter/2*cos(angle), y=-capsule.length/2+capsule.diameter/2*sin(angle))
point = (; x=radius*cos(angle), y=-0.5*length+radius*sin(angle))
push!(points, point)
end
for i in 0:9
angle = i/9 * pi/2
point = (; x=capsule.diameter/2*cos(angle), y=capsule.length/2+capsule.diameter/2*sin(angle))
point = (; x=radius*cos(angle), y=0.5*length+radius*sin(angle))
push!(points, point)
end
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="LatheGeometry", points=points, phiStart=0, phiLength=2*pi, segments=32)
Expand All @@ -122,26 +131,29 @@ function exportObject(object, elements, obj::Modia3D.Composition.Object3D, capsu
end

function exportObject(object, elements, obj::Modia3D.Composition.Object3D, beam::Modia3D.Shapes.Beam, initPos, initRot)
length = FVal(beam.length)
width = FVal(beam.width)
thickness = FVal(beam.thickness)
if beam.axis == 1
r_obj = @SVector[0.0, 0.0, -beam.thickness/2]
r_obj = @SVector[0.0, 0.0, -0.5*thickness]
elseif beam.axis == 2
r_obj = @SVector[-beam.thickness/2, 0.0, 0.0]
r_obj = @SVector[-0.5*thickness, 0.0, 0.0]
else
r_obj = @SVector[0.0, -beam.thickness/2, 0.0]
r_obj = @SVector[0.0, -0.5*thickness, 0.0]
end
R_obj = Shapes.rotateAxis2x(beam.axis, Modia3D.NullRotation(Float64))
name = Modia3D.fullName(obj)
geometryName = name * ".geometry"
curves = [
(; type="LineCurve", v1=[-beam.length/2, -beam.width/2], v2=[beam.length/2, -beam.width/2]),
(; 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),
(; type="LineCurve", v1=[beam.length/2, beam.width/2], v2=[-beam.length/2, beam.width/2]),
(; 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)
(; type="LineCurve", v1=[-0.5*length, -0.5*width], v2=[0.5*length, -0.5*width]),
(; 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),
(; type="LineCurve", v1=[0.5*length, 0.5*width], v2=[-0.5*length, 0.5*width]),
(; 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)
]
shapeName = name * ".shape"
shapeUuid = name2uuid(shapeName)
shape = (; name=shapeName, uuid=shapeUuid, type="Shape", curves=curves, holes=[], currentPoint=[0, 0])
options = (; depth=beam.thickness, bevelEnabled=false)
options = (; depth=thickness, bevelEnabled=false)
geometry = (; name=geometryName, uuid=name2uuid(geometryName), type="ExtrudeGeometry", shapes=[shapeUuid], options=options)
material = printVisuMaterialToJSON(obj, obj.visualMaterial)
objectInfo = getObjectInfo(name, geometry, material, initPos, initRot, r_obj=r_obj, R_obj=R_obj)
Expand Down
4 changes: 3 additions & 1 deletion src/Basics/_module.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export trailingPartOfName
export nepsType, neps, sign_eps, radToDeg
export getAndCheckFullLibraryPath, getEnvironmentVariable

export normalizeVector, BoundingBox
export normalizeVector, BoundingBox, convertToFloat64

export readDictOfStructsFromJSON
export listKeys
Expand All @@ -32,6 +32,8 @@ using StaticArrays
using LinearAlgebra
using JSON
using Unitful
import Measurements
import MonteCarloMeasurements

include("environment.jl")
include("constantsAndFunctions.jl")
Expand Down
22 changes: 22 additions & 0 deletions src/Basics/constantsAndFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ function normalizeVector(n::SVector{3,T}) where {T}
return n/nabs
end

# convert VarFloatType to Float64
function convertToFloat64(value)
if typeof(value[1]) <: Measurements.Measurement
# Plot mean value signal
value_mean = Float64.(Measurements.value.(value))
elseif typeof(value[1]) <: MonteCarloMeasurements.StaticParticles ||
typeof(value[1]) <: MonteCarloMeasurements.Particles
# Plot mean value signal
if isdefined(MonteCarloMeasurements, :pmean)
# MonteCarloMeasurements, version >= 1.0
value_mean = Float64.(MonteCarloMeasurements.pmean.(value))
else
# MonteCarloMeasurements, version < 1.0
value_mean = Float64.(MonteCarloMeasurements.mean.(value))
end
else
value_mean = Float64.(value)
end
return value_mean
end


# Standard constants
const radToDeg = 180.0/pi

Expand Down
6 changes: 3 additions & 3 deletions src/Composition/dynamics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function getJointsAndForceElementsAndObject3DsWithoutParents!(evaluatedParameter
if typeof(value.feature) <: Scene
push!(object3DWithoutParents, value)
else
error("\n", value.path, " is an Object3D that has no parent, but no feature=Scene(..)!\n")
error("\n", value.path, " is an Object3D that has no parent, but no feature=Scene(..)!\n")
end
elseif typeof(value.feature) <: Scene
error("\n", value.path, " is an Object3D that has feature=Scene(..) and has a parent (= ", value.parent.path, ")!\n")
Expand Down Expand Up @@ -404,8 +404,8 @@ function multibodyResiduals3!(sim::ModiaLang.SimulationModel{F}, scene, world, t
TimerOutputs.@timeit sim.timer "Modia3D_3 exportAnimation" begin
objectData = []
for obj in scene.allVisuElements
pos = obj.r_abs
ori = Modia3D.from_R(obj.R_abs)
pos = Modia3D.convertToFloat64(obj.r_abs)
ori = Modia3D.from_R(Modia3D.convertToFloat64(obj.R_abs))
dat = animationData(pos, ori)
push!(objectData, dat)
end
Expand Down
2 changes: 0 additions & 2 deletions src/renderer/DLR_Visualization/_module.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import Modia3D
import Modia3D.Basics
import Modia3D.Shapes
import Modia3D.Composition
import Measurements
import MonteCarloMeasurements

using Libdl
ISWINDOWS() = Sys.iswindows()
Expand Down
Loading

0 comments on commit c593074

Please sign in to comment.