Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
mesh to raster
Browse files Browse the repository at this point in the history
  • Loading branch information
dwastberg committed May 20, 2024
1 parent d261423 commit 2a8b6cf
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
13 changes: 8 additions & 5 deletions src/cpp/include/MeshBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class MeshBuilder
const GridField &dtm,
double max_mesh_size,
double min_mesh_angle,
size_t smooth_ground = 0)
size_t smooth_ground = 0,
bool sort_triangles = true)
{

// Get bounding box
Expand Down Expand Up @@ -128,7 +129,8 @@ class MeshBuilder
double xmax,
double ymax,
double max_mesh_size,
double min_mesh_angle)
double min_mesh_angle,
bool sort_triangles = true)
{
info("Building ground mesh for city...");
Timer timer("build_ground_mesh");
Expand Down Expand Up @@ -164,7 +166,7 @@ class MeshBuilder
// build 2D mesh
Mesh mesh;
call_triangle(mesh, boundary, triangle_sub_domains, subdomain_triangle_size, max_mesh_size,
min_mesh_angle, true);
min_mesh_angle, sort_triangles);

// Mark subdomains
compute_domain_markers(mesh, subdomains);
Expand Down Expand Up @@ -504,7 +506,8 @@ class MeshBuilder
double max_mesh_size,
double min_mesh_angle,
size_t smooth_ground = 0,
bool merge_meshes = true)
bool merge_meshes = true,
bool sort_triangles = true)
{
auto build_city_surface_t = Timer("build_city_surface_mesh");
auto terrain_time = Timer("build_city_surface_mesh: step 1 terrain");
Expand All @@ -514,7 +517,7 @@ class MeshBuilder
subdomains.push_back(b.to_polygon());
}
Mesh terrain_mesh = build_terrain_mesh(subdomains, subdomain_triangle_size, dtm, max_mesh_size,
min_mesh_angle, smooth_ground);
min_mesh_angle, smooth_ground, sort_triangles);
terrain_time.stop();

std::vector<Mesh> city_mesh;
Expand Down
15 changes: 11 additions & 4 deletions src/dtcc_builder/geometry_builders/meshes.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
fix_building_footprint_clearance,
)

from dtcc_builder.meshing.convert import mesh_to_raster

from dtcc_builder.logging import debug, info, warning, error


Expand All @@ -49,6 +51,7 @@ def build_surface_mesh(
min_mesh_angle: float = 25.0,
merge_meshes: bool = True,
smoothing: int = 0,
sort_triangles: bool = False,
) -> Mesh:
"""
Build a mesh from the surfaces of the buildings in the city.
Expand Down Expand Up @@ -99,11 +102,14 @@ def build_surface_mesh(
subdomain_resolution = [building_mesh_triangle_size] * len(building_footprints)

terrain = city.terrain
if terrain is None:
raise ValueError("City has no terrain data. Please compute terrain first.")
terrain_raster = terrain.raster
if terrain_raster is None:
raise ValueError(
"City has no terrain raster data. Please compute terrain first."
)
terrain_mesh = terrain.mesh
if terrain_raster is None and terrain_mesh is None:
raise ValueError("City terrain has no data. Please compute terrain first.")
if terrain_raster is None and terrain_mesh is not None:
terrain_raster = mesh_to_raster(terrain_mesh, cell_size=max_mesh_size)
builder_dem = raster_to_builder_gridfield(terrain_raster)

builder_surfaces = [
Expand All @@ -117,6 +123,7 @@ def build_surface_mesh(
min_mesh_angle,
smoothing,
merge_meshes,
sort_triangles,
)
if merge_meshes:
result_mesh = builder_mesh_to_mesh(builder_mesh[0])
Expand Down
13 changes: 13 additions & 0 deletions src/dtcc_builder/meshing/convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from dtcc_model import Mesh, PointCloud, Raster
import numpy as np

from dtcc_builder.pointcloud.convert import rasterize


def mesh_to_raster(mesh: Mesh, cell_size: float) -> Raster:
unique_points = np.unique(mesh.faces.flatten())
unique_vertices = mesh.vertices[unique_points]
pc = PointCloud(points=unique_vertices)
pc.calculate_bounds()
raster = rasterize(pc, cell_size, radius=cell_size, ground_only=False)
return raster
6 changes: 5 additions & 1 deletion src/dtcc_builder/pointcloud/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ def rasterize(
bounds = pc.bounds

dem = points2grid(
ground_points, cell_size, bounds.tuple, window_size=window_size, radius=radius
ground_points,
cell_size,
bounds.tuple,
window_size=int(window_size),
radius=radius,
)
dem_raster = model.Raster()
dem_raster.data = dem
Expand Down

0 comments on commit 2a8b6cf

Please sign in to comment.