Files
make-cad/test/test_plane_axes_convention.py
T
NiJingzhe 67f78f893d fix(geometry): align planar profile axes with the box convention
_orthonormal_plane_axes built the in-plane basis with x = cross(z, ref),
which for a +z normal yields x=(0,1,0), y=(-1,0,0): every non-square
rectangle made on the z=0 plane was silently rotated 90 degrees relative
to make_box_rsolid (width along global y instead of x).

Build the basis by projecting the reference vector onto the plane
(Gram-Schmidt) instead: for a +z normal the frame is exactly the global
(x, y, z), matching the box convention and the OCC gp_Ax2 default. The
reference-axis branch keeps ref away from z, so the projection never
degenerates. Only make_rectangle_rwire uses the helper (session and
legacy paths); other planar makers are unaffected.

BREAKING CHANGE: non-square rectangles now orient width along global x
for +z normals; both session and legacy paths rotate together. Recorded
replay graphs built with non-square rectangles reproduce rotated
geometry.

test_surface_api's partial-overlap trim test encoded the transposed
convention; its loop dims (4, 2) -> (2, 4) reproduce the exact footprint
(x in [0.5, 2.5], y in [-2, 2]) the test has always exercised.
2026-09-03 17:57:47 +08:00

97 lines
3.4 KiB
Python

"""Plane-axis convention regression tests.
`_orthonormal_plane_axes` must build the in-plane frame by projecting the
reference vector onto the plane, so a +z normal yields exactly the global
(x, y, z) frame. Planar profile makers that use the helper (currently
`make_rectangle_rwire`, session and legacy paths) then agree with
`make_box_rsolid`'s width-along-x convention. The previous cross-product
basis rotated every profile by 90 degrees for +z normals (width landed on
global y).
"""
import unittest
import numpy as np
import simplecadapi as scad
from simplecadapi import ql
from simplecadapi.operators._support import _orthonormal_plane_axes
class TestOrthonormalPlaneAxes(unittest.TestCase):
def test_z_normal_frame_matches_box_convention(self):
z_axis, x_axis, y_axis = _orthonormal_plane_axes((0.0, 0.0, 1.0))
np.testing.assert_allclose(z_axis, (0.0, 0.0, 1.0), atol=1e-12)
np.testing.assert_allclose(x_axis, (1.0, 0.0, 0.0), atol=1e-12)
np.testing.assert_allclose(y_axis, (0.0, 1.0, 0.0), atol=1e-12)
def test_frames_are_unit_right_handed_and_transverse(self):
normals = [
(0.3, -0.4, 0.86),
(1.0, 0.0, 0.0),
(0.0, 1.0, 0.0),
(0.0, 0.0, -1.0),
(0.2, 0.98, 0.05),
]
for normal in normals:
with self.subTest(normal=normal):
z_axis, x_axis, y_axis = _orthonormal_plane_axes(normal)
np.testing.assert_allclose(np.linalg.norm(z_axis), 1.0, atol=1e-12)
np.testing.assert_allclose(np.linalg.norm(x_axis), 1.0, atol=1e-12)
np.testing.assert_allclose(np.linalg.norm(y_axis), 1.0, atol=1e-12)
np.testing.assert_allclose(x_axis @ z_axis, 0.0, atol=1e-12)
np.testing.assert_allclose(y_axis @ z_axis, 0.0, atol=1e-12)
np.testing.assert_allclose(
np.cross(x_axis, y_axis), z_axis, atol=1e-12
)
def test_zero_normal_is_rejected(self):
with self.assertRaises(ValueError):
_orthonormal_plane_axes((0.0, 0.0, 0.0))
class TestRectangleAxisConvention(unittest.TestCase):
@staticmethod
def _edge_inventory(use_session: bool):
def build():
return scad.make_rectangle_rwire(30.0, 10.0, (0.0, 0.0, 0.0), (0.0, 0.0, 1.0))
if use_session:
with scad.GraphSession(graph_id="rect_axes_session"):
wire = build()
else:
wire = build()
inventory = []
for edge in ql.edges().resolve(wire):
center = edge.get_center()
inventory.append(
(round(center.x, 3), round(center.y, 3), round(edge.get_length(), 3))
)
return sorted(inventory)
def test_width_spans_x_on_session_path(self):
self.assertEqual(
self._edge_inventory(use_session=True),
[
(-15.0, -0.0, 10.0),
(0.0, -5.0, 30.0),
(0.0, 5.0, 30.0),
(15.0, 0.0, 10.0),
],
)
def test_width_spans_x_on_legacy_path(self):
self.assertEqual(
self._edge_inventory(use_session=False),
[
(-15.0, -0.0, 10.0),
(0.0, -5.0, 30.0),
(0.0, 5.0, 30.0),
(15.0, 0.0, 10.0),
],
)
if __name__ == "__main__":
unittest.main()