3.1. How to Create a Mesh from a numpy ArrayΒΆ
In this example, a Mesh is created from a Numpy array.:
import numpy as np
from ORSModel import ArrayDouble, FaceVertexMesh
# Define the vertices for a triangle (can be expanded to more vertices)
vertices = ArrayDouble.createArrayFromNumpyArray(np.array([0.0, 0.0, 0.0, # Vertex 0
1.0, 0.0, 0.0, # Vertex 1
0.5, 1.0, 0.0])) # Vertex 2
# Define the edges for a triangle (can be expanded to more edges)
edges = ArrayDouble.createArrayFromNumpyArray(np.array([0, 1, 2]))
# Create the mesh object
mesh = FaceVertexMesh()
# Define the time dimension
mesh.setTSize(1)
# Set vertices for the mesh, dynamically set the size based on the numpy array
meshVertices = mesh.getVertices(0) # Get vertex array for the mesh
numVertices = vertices.getSize() # Get number of elements in the vertices array
meshVertices.setSize(numVertices)
for i in range(numVertices):
meshVertices.atPut(i, vertices.at(i))
# Set edges (connect the vertices to form triangles), dynamically set size
meshEdges = mesh.getEdges(0) # Get edges array for the mesh
numEdges = edges.getSize() # Get number of elements in the edges array
meshEdges.setSize(numEdges)
for i in range(numEdges):
meshEdges.atPut(i, edges.at(i))
# Publish the mesh
mesh.publish()