added ruff for code review

This commit is contained in:
ykcheungab
2026-09-27 19:21:03 +08:00
parent f91ac9d3db
commit e7dfd4f90b
28 changed files with 434 additions and 69 deletions
+12 -2
View File
@@ -41,7 +41,12 @@ def clip_halfspace(points, distance):
def clip_rect(points, x0, x1, y0, y1):
for axis, bound, sign in ((0, x0, 1), (0, x1, -1), (1, y0, 1), (1, y1, -1)):
points = clip_halfspace(points, lambda p: sign * (p[axis] - bound))
# Bind the loop variables as defaults: the callback must clip against the
# plane of *this* iteration, not whatever the loop left behind.
points = clip_halfspace(
points,
lambda p, axis=axis, bound=bound, sign=sign: sign * (p[axis] - bound),
)
return points
@@ -56,7 +61,12 @@ def project_triangle(clip_vertices):
for i, v in enumerate(clip_vertices)]
for axis in range(3):
for sign in (-1, 1):
points = clip_halfspace(points, lambda p: p[3] + sign * p[axis])
# Same late-binding hazard as clip_rect: bind axis/sign per iteration so
# the clip plane cannot be read from a later loop state.
points = clip_halfspace(
points,
lambda p, axis=axis, sign=sign: p[3] + sign * p[axis],
)
return [((p[0] / p[3] + 1) / 2, (p[1] / p[3] + 1) / 2,
1 / p[3], *(b / p[3] for b in p[4:]))
for p in points if p[3] > 1e-15]
+9 -5
View File
@@ -33,7 +33,11 @@ def build_patch_geometry(obj, cam, scene, depsgraph, uv_names, nx, ny, edge_samp
output[name]['mesh'].append(uvs[name])
cache = {}
def visible(weights):
# ``cache`` and ``world`` are bound as defaults: ``cache`` is rebound
# for every loop triangle, and a late-bound closure would read the
# next triangle's cache (or the final triangle's, if a probe were
# deferred), yielding a wrong visibility answer per triangle.
def visible(weights, cache=cache, world=world):
nonlocal ray_count
key = tuple(round(w, 12) for w in weights)
if key in cache:
@@ -64,10 +68,10 @@ def build_patch_geometry(obj, cam, scene, depsgraph, uv_names, nx, ny, edge_samp
output[name]['patches'].append((i, j, polygon))
print(f'Evaluated {len(mesh.loop_triangles)} loop triangles; {ray_count} visibility rays; '
f'{len(output[uv_names[0]]["patches"])} local fragments per UV target')
print(f'Visibility sampling: seven probes per fragment; uniformly probed '
f'fragments are accepted or dropped whole, mixed boundary fragments '
f'refine to 1/4 cell or depth 6 and unresolved leaves are rejected. '
f'Sub-sample occluders may be missed; hit tolerance max(1e-7, distance*2e-5).')
print('Visibility sampling: seven probes per fragment; uniformly probed '
'fragments are accepted or dropped whole, mixed boundary fragments '
'refine to 1/4 cell or depth 6 and unresolved leaves are rejected. '
'Sub-sample occluders may be missed; hit tolerance max(1e-7, distance*2e-5).')
return output
finally:
evaluated.to_mesh_clear()
+2 -1
View File
@@ -31,7 +31,8 @@ bpy (the same pattern as ``blender/scripts/patch_geometry.py``).
from __future__ import annotations
from typing import Any, Sequence
from collections.abc import Sequence
from typing import Any
import numpy as np
+1
View File
@@ -38,6 +38,7 @@ sys.path.insert(0, str(Path(__file__).resolve().parent))
from patch_geometry import clip_rect
from patch_projection import build_patch_geometry
# ----------------------------------------------------------------------
# Common Utilities
# ----------------------------------------------------------------------
+10 -2
View File
@@ -1,7 +1,15 @@
"""Run with python -m unittest discover -s blender/scripts -p test_patch_geometry.py."""
import unittest
from patch_geometry import (area, barycentric, cell_fragments, clip_rect,
interpolate_uv, project_triangle, visible_triangles)
from patch_geometry import (
area,
barycentric,
cell_fragments,
clip_rect,
interpolate_uv,
project_triangle,
visible_triangles,
)
class GeometryTests(unittest.TestCase):
-1
View File
@@ -15,7 +15,6 @@ import base64
import numpy as np
import pytest
from probe_falloff import (
FALLOFF_MODES,
affected_bands,