Fix int64 cross-product overflow and bounds in cga_builder
This commit is contained in:
parent
9798d1b868
commit
6dba18d6a7
1 changed files with 24 additions and 26 deletions
|
|
@ -1311,9 +1311,9 @@ def build_cga_building(
|
||||||
r_sup = all_tier_supports_mask
|
r_sup = all_tier_supports_mask
|
||||||
add_zone(ZONE_ROOF_SEGMENT, pal["roof_class"], ZONE_FLAG_EXTERIOR, r_sup, r_render, r_col)
|
add_zone(ZONE_ROOF_SEGMENT, pal["roof_class"], ZONE_FLAG_EXTERIOR, r_sup, r_render, r_col)
|
||||||
|
|
||||||
all_c_pts = np.vstack([z.col_tris.reshape(-1, 3) for z in zones])
|
all_pts_bounds = np.vstack([z.render_tris.reshape(-1, 3) for z in zones] + [z.col_tris.reshape(-1, 3) for z in zones])
|
||||||
bmin = all_c_pts.min(axis=0)
|
bmin = all_pts_bounds.min(axis=0)
|
||||||
bmax = all_c_pts.max(axis=0)
|
bmax = all_pts_bounds.max(axis=0)
|
||||||
g_mask = 0
|
g_mask = 0
|
||||||
for z in zones:
|
for z in zones:
|
||||||
if z.flags & ZONE_FLAG_GROUND_ANCHORED:
|
if z.flags & ZONE_FLAG_GROUND_ANCHORED:
|
||||||
|
|
@ -1384,6 +1384,7 @@ def _pack_zone_mesh_16b(
|
||||||
pos_scale: np.ndarray,
|
pos_scale: np.ndarray,
|
||||||
zone_index: int,
|
zone_index: int,
|
||||||
bldg_slot: int,
|
bldg_slot: int,
|
||||||
|
allow_fallback: bool = True,
|
||||||
) -> tuple[bytes, bytes, int, int]:
|
) -> tuple[bytes, bytes, int, int]:
|
||||||
"""
|
"""
|
||||||
Pack triangle soup -> 16 B/vert vertex buffer + uint16 index buffer:
|
Pack triangle soup -> 16 B/vert vertex buffer + uint16 index buffer:
|
||||||
|
|
@ -1392,30 +1393,27 @@ def _pack_zone_mesh_16b(
|
||||||
"""
|
"""
|
||||||
qpos_all = np.clip(np.round((tris - pos_origin[None, None, :]) / pos_scale[None, None, :]), -32767, 32767).astype(np.int16)
|
qpos_all = np.clip(np.round((tris - pos_origin[None, None, :]) / pos_scale[None, None, :]), -32767, 32767).astype(np.int16)
|
||||||
|
|
||||||
# Filter out any triangle whose quantized vertices collapse or are collinear
|
# Check if any triangle collapses or is collinear after int16 quantization (avoid int64 squaring overflow!)
|
||||||
v0 = qpos_all[:, 0, :].astype(np.int64)
|
v0 = qpos_all[:, 0, :].astype(np.int64)
|
||||||
v1 = qpos_all[:, 1, :].astype(np.int64)
|
v1 = qpos_all[:, 1, :].astype(np.int64)
|
||||||
v2 = qpos_all[:, 2, :].astype(np.int64)
|
v2 = qpos_all[:, 2, :].astype(np.int64)
|
||||||
e1q = v1 - v0
|
cross_q = np.cross(v1 - v0, v2 - v0)
|
||||||
e2q = v2 - v0
|
valid = np.any(cross_q != 0, axis=1)
|
||||||
cross_q = np.cross(e1q, e2q)
|
if not np.all(valid):
|
||||||
valid = np.sum(cross_q * cross_q, axis=1) > 0
|
if allow_fallback:
|
||||||
if not np.any(valid):
|
bmin = tris.reshape(-1, 3).min(axis=0)
|
||||||
# Fallback to single minimal valid box from raw tris bounds
|
bmax = np.maximum(bmin + pos_scale * 4.0, tris.reshape(-1, 3).max(axis=0))
|
||||||
bmin = tris.reshape(-1, 3).min(axis=0)
|
box_t, box_m, box_ao, box_uv = make_box_prism(
|
||||||
bmax = np.maximum(bmin + pos_scale * 2.0, tris.reshape(-1, 3).max(axis=0))
|
bmin, np.array([1.0, 0.0, 0.0]), np.array([0.0, 0.0, -1.0]), np.array([0.0, 1.0, 0.0]),
|
||||||
box_t, box_m, box_ao, box_uv = make_box_prism(
|
0.0, float(bmax[0] - bmin[0]), -float(bmax[2] - bmin[2]), 0.0, 0.0, float(bmax[1] - bmin[1]),
|
||||||
bmin, np.array([1.0, 0.0, 0.0]), np.array([0.0, 0.0, -1.0]), np.array([0.0, 1.0, 0.0]),
|
int(mats[0]), int(mats[0]), int(mats[0]),
|
||||||
0.0, float(bmax[0] - bmin[0]), -float(bmax[2] - bmin[2]), 0.0, 0.0, float(bmax[1] - bmin[1]),
|
)
|
||||||
int(mats[0]), int(mats[0]), int(mats[0]),
|
return _pack_zone_mesh_16b(box_t, box_m, box_ao, box_uv, pos_origin, pos_scale, zone_index, bldg_slot, allow_fallback=False)
|
||||||
)
|
tris = tris[valid]
|
||||||
return _pack_zone_mesh_16b(box_t, box_m, box_ao, box_uv, pos_origin, pos_scale, zone_index, bldg_slot)
|
qpos_all = qpos_all[valid]
|
||||||
|
mats = mats[valid]
|
||||||
tris = tris[valid]
|
ao = ao[valid]
|
||||||
qpos_all = qpos_all[valid]
|
uvs = uvs[valid]
|
||||||
mats = mats[valid]
|
|
||||||
ao = ao[valid]
|
|
||||||
uvs = uvs[valid]
|
|
||||||
|
|
||||||
e1 = tris[:, 1] - tris[:, 0]
|
e1 = tris[:, 1] - tris[:, 0]
|
||||||
e2 = tris[:, 2] - tris[:, 0]
|
e2 = tris[:, 2] - tris[:, 0]
|
||||||
|
|
@ -1493,10 +1491,10 @@ def check_mesh_watertight(qpos_verts: np.ndarray, indices: np.ndarray) -> tuple[
|
||||||
dir_edges[(u, v)] += 1
|
dir_edges[(u, v)] += 1
|
||||||
undir_edges[(min(u, v), max(u, v))] += 1
|
undir_edges[(min(u, v), max(u, v))] += 1
|
||||||
|
|
||||||
# Vectorized collinearity check in quantized space
|
# Vectorized collinearity check in quantized space (avoid int64 squaring overflow!)
|
||||||
vp = qpos_verts[tri_idx].astype(np.int64)
|
vp = qpos_verts[tri_idx].astype(np.int64)
|
||||||
cr = np.cross(vp[:, 1] - vp[:, 0], vp[:, 2] - vp[:, 0])
|
cr = np.cross(vp[:, 1] - vp[:, 0], vp[:, 2] - vp[:, 0])
|
||||||
degenerate += int(np.sum(np.sum(cr * cr, axis=1) == 0))
|
degenerate += int(np.sum(np.all(cr == 0, axis=1)))
|
||||||
|
|
||||||
open_edges = 0
|
open_edges = 0
|
||||||
non_manifold = 0
|
non_manifold = 0
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue