Create a 3D pedestrian

Create the agent

[1]:
import os
from pathlib import Path

import matplotlib.pyplot as plt
import plotly.graph_objects as go
from IPython.display import Image
from matplotlib.figure import Figure
from shapely.geometry import MultiPolygon

import configuration.utils.constants as cst
from configuration.models.agents import Agent
from configuration.models.measures import AgentMeasures
from streamlit_app.plot import plot

# Define the type of agent to be created (e.g., pedestrian)
agent_type = cst.AgentTypes.pedestrian

# Define a dictionary containing measurements specific to the agent type
measures = {
    "sex": "male",  # Specify the sex of the agent
    "bideltoid_breadth": 45.0,  # Shoulder breadth (cm)
    "chest_depth": 23.0,  # Chest depth (cm)
    "height": 180.0,  # Height of the agent (cm)
    "weight": 75.0,  # Weight of the agent (kg)
}

# Create an instance of AgentMeasures using the defined agent type and measurements
agent_measures = AgentMeasures(agent_type=agent_type, measures=measures)

# Create an instance of Agent using the defined agent type and measures object
current_agent = Agent(agent_type=agent_type, measures=agent_measures)

Plot the 3D pedestrian

Plot the orthogonal projection

[2]:
fig: Figure = plot.display_body3D_orthogonal_projection(current_agent)
plt.close(fig)
cwd = Path(os.path.abspath(""))
image_path = (
    cwd.parent.parent
    / "data"
    / "images"
    / "generated"
    / "orthogonal_projection_of_a_male.png"
)
fig.savefig(image_path, format="png", dpi=50)

Image(filename=str(image_path))
[2]:
_images/create3Dped_5_0.png

Plot the superposition of MultiPolygon (one MultiPolygon per height)

[3]:
# Create a 3D plot of the agent using Plotly
fig_plotly: go.Figure = plot.display_body3D_polygons(current_agent)

# Display the generated Plotly figure in the documentation
cwd = Path(os.path.abspath(""))  # Current working directory
path_data = cwd.parent.parent / "data"
fig_plotly.write_image(
    path_data / "images" / "generated" / "pedestrian3D_multipolygon.png", format="png"
)
Image(path_data / "images" / "generated" / "pedestrian3D_multipolygon.png")
[3]:
_images/create3Dped_7_0.png

Plot the mesh

[4]:
fig_plotly_mesh: go.Figure = plot.display_body3D_mesh(current_agent, precision=60)

# Display the generated Plotly figure in the documentation
fig_plotly_mesh.write_image(
    path_data / "images" / "generated" / "pedestrian3D_mesh.png", format="png"
)
Image(path_data / "images" / "generated" / "pedestrian3D_mesh.png")
[4]:
_images/create3Dped_9_0.png

Display the content of the dictionary defining the 3D pedestrians

[5]:
# Retrieve additional parameters for 2D shapes associated with the current agent (in centimeters)
shapes: dict[float, MultiPolygon] = current_agent.shapes3D.shapes

# Print only the first 9 elements (height and geometry) from the shapes dictionary
for i, (height, geometry) in enumerate(shapes.items()):
    if i >= 9:  # Stop after printing 9 elements
        break
    # print only a few coordinates of the geometry for brevity
    print(f"Height: {height:.3f} cm, Geometry: {geometry.wkt[:60]}...")
Height: 0.000 cm, Geometry: MULTIPOLYGON (((6.721004686269752 19.365775200068718, 6.8995...
Height: 1.166 cm, Geometry: MULTIPOLYGON (((7.431869751071063 -20.83769359091587, 7.5393...
Height: 3.110 cm, Geometry: MULTIPOLYGON (((-0.1380449930211109 -18.60721714249899, 0.15...
Height: 5.054 cm, Geometry: MULTIPOLYGON (((-3.11665670685271 -17.654712776331337, -2.99...
Height: 6.803 cm, Geometry: MULTIPOLYGON (((-7.086453776544069 -15.435951383375166, -6.9...
Height: 8.942 cm, Geometry: MULTIPOLYGON (((-8.926701762226038 -14.621959999738664, -8.8...
Height: 12.052 cm, Geometry: MULTIPOLYGON (((-8.563060836568688 -14.918311329296769, -8.4...
Height: 12.829 cm, Geometry: MULTIPOLYGON (((-8.061332358673212 -15.052349804792964, -7.9...
Height: 14.773 cm, Geometry: MULTIPOLYGON (((-7.464222952902746 -15.589569034843137, -7.3...