Datasets:
File size: 8,643 Bytes
bcb16da | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | """
disaster_simulation.py
Simulates a post-disaster scenario on the candidate building set.
Two independent simulations are applied in order (CRS first, then damage):
1. CRS simulation (global):
All cand buildings are rotated by a random angle around the Z-axis and shifted
by a large random translation, simulating a dataset with no absolute coordinate
reference. The internal geometry of each building is preserved exactly; only
the global frame changes. This forces the model to rely on rotation-invariant
features (aligned BB, volume, area, compactness) rather than axis-aligned ones.
2. Damage simulation (per-building):
A random subset of cand buildings have their height reduced, simulating partial
collapse. Each damaged building keeps its ground footprint but loses height
according to a random damage factor.
Only 'cands' are ever modified. The 'index' (reference dataset) is never touched.
Usage:
from disaster_simulation import DisasterSimulator
import config
simulator = DisasterSimulator(config.DisasterSimulation, seed=1)
object_dict = simulator.apply(object_dict)
# simulator.R_crs, simulator.t_crs — ground-truth transform for evaluation
# simulator.damage_log — per-building damage factors for inspection
"""
import numpy as np
import config as cfg
class DisasterSimulator:
"""
Applies CRS simulation and damage simulation to the candidate set.
Parameters
----------
sim_config : config.DisasterSimulation (class reference)
seed : int
Controls randomness for both simulations. Different seeds per pipeline
run ensure the model trains on varied scenarios.
"""
_Z_EPSILON = 1e-4 # threshold to distinguish above-ground vertices from ground
def __init__(self, sim_config=None, seed=42):
if sim_config is None:
sim_config = cfg.DisasterSimulation
self.enabled = sim_config.enabled
self.crs_simulation = sim_config.crs_simulation
self.damage_probability = sim_config.damage_probability
self.min_damage_factor = sim_config.min_damage_factor
self.max_damage_factor = sim_config.max_damage_factor
self._rng = np.random.default_rng(seed)
# Set after apply() — expose for external evaluation
self.R_crs = None # (3,3) rotation matrix applied to all cands
self.t_crs = None # (3,) translation vector applied to all cands
self.damage_log = {} # {building_id: damage_factor} (1.0 = undamaged)
# ------------------------------------------------------------------ #
# Public API
# ------------------------------------------------------------------ #
def apply(self, object_dict: dict) -> dict:
"""
Apply CRS simulation then damage simulation to cands in-place.
Parameters
----------
object_dict : dict
Full object dict with keys 'cands', 'index', 'mapping_dict', etc.
Returns
-------
dict
Same object_dict with modified cands.
"""
if not self.enabled:
return object_dict
if self.crs_simulation:
object_dict = self._apply_crs_simulation(object_dict)
object_dict = self._apply_damage_simulation(object_dict)
self._print_summary(object_dict)
return object_dict
# ------------------------------------------------------------------ #
# CRS simulation
# ------------------------------------------------------------------ #
def _apply_crs_simulation(self, object_dict: dict) -> dict:
"""
Apply a single random rotation (around Z) + large translation to ALL cands.
The same (R_crs, t_crs) is applied to every building so internal
relative geometry is preserved — only the global frame changes.
"""
# Random rotation angle in [0, 2π)
theta = self._rng.uniform(0.0, 2.0 * np.pi)
cos_t, sin_t = np.cos(theta), np.sin(theta)
self.R_crs = np.array([
[ cos_t, -sin_t, 0.0],
[ sin_t, cos_t, 0.0],
[ 0.0, 0.0, 1.0]
])
# Large random translation — no absolute reference
tx = self._rng.uniform(-100_000.0, 100_000.0)
ty = self._rng.uniform(-100_000.0, 100_000.0)
self.t_crs = np.array([tx, ty, 0.0])
for bid, building in object_dict['cands'].items():
self._transform_building(building, self.R_crs, self.t_crs)
print(f"[DisasterSimulator] CRS simulation applied: "
f"rotation={np.degrees(theta):.1f}°, "
f"translation=({tx:.0f}, {ty:.0f}) m")
return object_dict
@staticmethod
def _transform_building(building: dict, R: np.ndarray, t: np.ndarray) -> None:
"""Apply rigid transform (R, t) to all geometry of one building in-place."""
# vertices: (N, 3)
verts = building['vertices']
building['vertices'] = (R @ verts.T).T + t
# centroid: (3,)
building['centroid'] = R @ np.asarray(building['centroid'], dtype=np.float64) + t
# polygon_mesh: list of surfaces, each surface = list of [x, y, z]
new_mesh = []
for surface in building['polygon_mesh']:
new_surface = []
for coord in surface:
v = np.array(coord, dtype=np.float64)
new_surface.append((R @ v + t).tolist())
new_mesh.append(new_surface)
building['polygon_mesh'] = new_mesh
# ------------------------------------------------------------------ #
# Damage simulation
# ------------------------------------------------------------------ #
def _apply_damage_simulation(self, object_dict: dict) -> dict:
"""
Randomly reduce height of a fraction of cand buildings.
Each damaged building keeps its ground footprint but all vertices
above z_min are scaled: z_new = z_min + (z - z_min) * damage_factor
"""
cands = object_dict['cands']
cand_ids = list(cands.keys())
n_to_damage = int(round(self.damage_probability * len(cand_ids)))
# Select which buildings to damage
damaged_indices = self._rng.choice(len(cand_ids), size=n_to_damage, replace=False)
damaged_ids = [cand_ids[i] for i in damaged_indices]
# One damage factor per building
damage_factors = self._rng.uniform(
self.min_damage_factor, self.max_damage_factor, size=n_to_damage
)
for bid, factor in zip(damaged_ids, damage_factors):
self._damage_building(cands[bid], factor)
self.damage_log[bid] = round(float(factor), 4)
# Undamaged buildings recorded as 1.0
for bid in cand_ids:
if bid not in self.damage_log:
self.damage_log[bid] = 1.0
print(f"[DisasterSimulator] Damage simulation: "
f"{n_to_damage}/{len(cand_ids)} buildings damaged "
f"(factor range [{self.min_damage_factor}, {self.max_damage_factor}])")
return object_dict
def _damage_building(self, building: dict, damage_factor: float) -> None:
"""Reduce height of a single building in-place."""
verts = building['vertices'] # (N, 3)
z_min = float(verts[:, 2].min())
# Update vertices array
mask = verts[:, 2] > (z_min + self._Z_EPSILON)
verts[mask, 2] = z_min + (verts[mask, 2] - z_min) * damage_factor
building['vertices'] = verts
# Update polygon_mesh
new_mesh = []
for surface in building['polygon_mesh']:
new_surface = []
for coord in surface:
x, y, z = coord[0], coord[1], coord[2]
if z > z_min + self._Z_EPSILON:
z = z_min + (z - z_min) * damage_factor
new_surface.append([x, y, z])
new_mesh.append(new_surface)
building['polygon_mesh'] = new_mesh
# Update centroid z
new_z_max = float(verts[:, 2].max())
c = np.asarray(building['centroid'], dtype=np.float64)
c[2] = (z_min + new_z_max) / 2.0
building['centroid'] = c
# ------------------------------------------------------------------ #
# Reporting
# ------------------------------------------------------------------ #
@staticmethod
def _print_summary(object_dict: dict) -> None:
print(f"[DisasterSimulator] Done. "
f"cands: {len(object_dict['cands'])}, "
f"index: {len(object_dict['index'])} (unchanged)")
|