I/O Functions
Loading Shape Models
AsteroidShapeModels.load_shape_obj
— Functionload_shape_obj(shapepath; scale=1.0, with_face_visibility=false) -> ShapeModel
Load a shape model from a Wavefront OBJ file.
Arguments
shapepath::String
: Path to a Wavefront OBJ file
Keyword Arguments
scale::Real=1.0
: Scale factor of the shape modelwith_face_visibility::Bool=false
: Whether to compute face-to-face visibility
Returns
ShapeModel
: Loaded shape model with computed geometric properties
Examples
# Load a shape model
shape = load_shape_obj("asteroid.obj")
# Load with scaling and visibility computation
shape = load_shape_obj("asteroid_km.obj", scale=1000, with_face_visibility=true)
See also: load_shape_grid
, load_obj
AsteroidShapeModels.load_obj
— Functionload_obj(shapepath::String; scale=1) -> nodes, faces
Load a 3D shape model from an OBJ file.
Arguments
shapepath::String
: Path to the OBJ file
Keyword Arguments
scale::Real=1
: Scale factor to apply to all vertex coordinates. For example, usescale=1000
to convert from kilometers to meters
Returns
nodes::Vector{SVector{3,Float64}}
: Array of vertex positionsfaces::Vector{SVector{3,Int64}}
: Array of triangular face definitions (1-indexed vertex indices)
Examples
# Load shape model in meters
nodes, faces = load_obj("asteroid.obj")
# Load shape model and convert from km to m
nodes, faces = load_obj("asteroid_km.obj", scale=1000)
# Get the number of nodes and faces
println("Number of nodes: ", length(nodes))
println("Number of faces: ", length(faces))
# Access individual nodes and faces
first_node = nodes[1] # SVector{3, Float64}
first_face = faces[1] # SVector{3, Int} with node indices
Notes
This function uses the FileIO/MeshIO packages to load OBJ files. Only triangular faces are supported.
AsteroidShapeModels.isobj
— Functionisobj(filepath::String) -> Bool
Check if a file has the OBJ file extension.
Arguments
filepath::String
: Path to the file to check
Returns
Bool
:true
if the file has.obj
extension,false
otherwise
Examples
isobj("model.obj") # Returns true
isobj("model.stl") # Returns false
isobj("model.OBJ") # Returns false (case-sensitive)
Grid Loading
AsteroidShapeModels.load_shape_grid
— Functionload_shape_grid(xs, ys, zs; scale=1.0, with_face_visibility=false) -> ShapeModel
Convert a regular grid (x, y) with z-values to a shape model.
Arguments
xs::AbstractVector
: x-coordinates of grid pointsys::AbstractVector
: y-coordinates of grid pointszs::AbstractMatrix
: z-coordinates of grid points wherezs[i,j]
corresponds to(xs[i], ys[j])
Keyword Arguments
scale::Real=1.0
: Scale factor to apply to all coordinateswith_face_visibility::Bool=false
: Whether to compute face-to-face visibility
Returns
ShapeModel
: Shape model with computed geometric properties
Examples
# Create a shape from elevation data
xs = range(-10, 10, length=50)
ys = range(-10, 10, length=50)
zs = [exp(-(x^2 + y^2)/10) for x in xs, y in ys] # Gaussian surface
shape = load_shape_grid(xs, ys, zs)
# With scaling and visibility
shape = load_shape_grid(xs, ys, zs, scale=1000, with_face_visibility=true)
See also: load_shape_obj
, grid_to_faces
AsteroidShapeModels.grid_to_faces
— Functiongrid_to_faces(xs::AbstractVector, ys::AbstractVector, zs::AbstractMatrix) -> nodes, faces
Convert a regular grid (x, y) and corresponding z-coordinates to triangular facets.
| ⧹| ⧹| ⧹|
j+1 ・–C–D–・ |⧹ |⧹ |⧹ | | ⧹| ⧹| ⧹| j ・–A–B–・ |⧹ |⧹ |⧹ | i i+1
Arguments
xs::AbstractVector
: x-coordinates of grid points (should be sorted)ys::AbstractVector
: y-coordinates of grid points (should be sorted)zs::AbstractMatrix
: z-coordinates of grid points wherezs[i,j]
corresponds to(xs[i], ys[j])
Returns
nodes::Vector{SVector{3,Float64}}
: Array of 3D vertex positionsfaces::Vector{SVector{3,Int}}
: Array of triangular face definitions (1-indexed)
Notes
Each grid cell is divided into two triangles. The vertices are numbered sequentially row by row (j varies slowest).
Examples
# Create a simple 3x3 grid
xs = [0.0, 1.0, 2.0]
ys = [0.0, 1.0, 2.0]
zs = [i + j for i in 1:3, j in 1:3] # z = x + y
nodes, faces = grid_to_faces(xs, ys, zs)
See also: load_shape_grid