From 55361d44c769e2e632bc0accac6bd054a77bf7b2 Mon Sep 17 00:00:00 2001 From: Andeloth Date: Thu, 12 Nov 2026 16:50:40 -0700 Subject: [PATCH] Use the true trapezoid side faces of slanted prisms (#101) intersect_line_with_prism and normal_to_prism treated each side face of a slanted prism as the parallelogram spanned by the bottom edge and the top offset of the edge's first vertex, vps_top[nv] - vps_bottom[nv]. The real face is the trapezoid vps_bottom[nv], vps_bottom[nvp1], vps_top[nvp1], vps_top[nv]; wherever the edge meets its neighbor at an angle the two top offsets differ, and near the edge's second end the parallelogram overhangs the face (inward taper) or falls short of it (outward taper). intersect_line_segment_with_prism takes the inside/outside state of a segment from the parity of the crossings before it, so a spurious or missed crossing flips every segment after it on the line: * inward taper: a line just outside a side wall crosses the overhang of an end face. If the line has further crossings, e.g. with a part of the prism far away, the segment counts as inside, so subpixel fill fractions next to a sidewall depended on distant geometry. * outward taper: a line just inside any wall of a plain rectangle misses one end face, has a single crossing and gets chord 0. normal_to_prism likewise took points beside an overhang to be at their normal distance from the face, and points over a shortfall to be off it, and could return the normal of a face other than the nearest. Intersect lines with each face as the two triangles sharing the diagonal vps_bottom[nv]-vps_top[nvp1], and measure distances to the actual trapezoid in normal_to_prism (min_distance_to_planar_quadrilateral). Add test_slanted_prism_side_faces to test-prism: a straight ridge and the same ridge with a step far from the probed segments, an outward- tapered rectangle, and normals beside the corners for both tapers. 22 checks fail before this change, none after. Co-Authored-By: Claude Opus 5.5 --- utils/geom.c | 76 +++++++++++++++------ utils/test-prism.c | 164 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 218 insertions(+), 22 deletions(-) diff --git a/utils/geom.c b/utils/geom.c index e3b7f3b..e3d1f5b 100644 --- a/utils/geom.c +++ b/utils/geom.c @@ -3336,26 +3336,31 @@ int intersect_line_with_prism(prism *prsm, vector3 pc, vector3 dc, double *slist int nvp1 = nv + 1; if (nvp1 == num_vertices) nvp1 = 0; - // checks if dp is parallel to the plane of the prism side face under consideration - vector3 v1 = vector3_minus(vps_bottom[nvp1], vps_bottom[nv]); - vector3 v2 = vector3_minus(vps_top[nv], vps_bottom[nv]); - double dot_tolerance = 1e-6; - if (fabs(vector3_dot(dp, vector3_cross(v1, v2))) <= dot_tolerance) continue; - - // to find the intersection point pp + s*dp between the line and the - // prism side face, we will solve the vector equation - // pp + s*dp = o + t*v1 + u*v2 - // where o is vps_bottom[nv], v1 is vps_bottom[nvp1]-vps_bottom[nv], - // v2 is vps_top[nv]-vps_bottom[nv], and 0 <= t <= 1, 0 <= u <= 1. - matrix3x3 M; - M.c0 = v1; - M.c1 = v2; - M.c2 = vector3_scale(-1, dp); - vector3 RHS = vector3_minus(pp, vps_bottom[nv]); - vector3 tus = matrix3x3_vector3_mult(matrix3x3_inverse(M), RHS); - if (tus.x < -tus_tolerance || tus.x > 1+tus_tolerance || tus.y < -tus_tolerance || tus.y > 1+tus_tolerance) continue; - double s = tus.z; - if (num_intersections++ < slist_len) slist[num_intersections-1] = s; + // The side face is the quadrilateral vps_bottom[nv], vps_bottom[nvp1], vps_top[nvp1], + // vps_top[nv]. The parallelogram spanned by the bottom edge and vps_top[nv]-vps_bottom[nv] + // overhangs it wherever vps_top[nvp1]-vps_bottom[nvp1] differs, i.e. at every corner of a + // slanted prism, so test the face as two triangles sharing the diagonal + // vps_bottom[nv]-vps_top[nvp1]. + vector3 o = vps_bottom[nv]; + vector3 corners[2][2] = {{vector3_minus(vps_bottom[nvp1], o), vector3_minus(vps_top[nvp1], o)}, + {vector3_minus(vps_top[nvp1], o), vector3_minus(vps_top[nv], o)}}; + int tri; + for (tri = 0; tri < 2; tri++) { + vector3 v1 = corners[tri][0], v2 = corners[tri][1]; + double dot_tolerance = 1e-6; + if (fabs(vector3_dot(dp, vector3_cross(v1, v2))) <= dot_tolerance) continue; + matrix3x3 M; + M.c0 = v1; + M.c1 = v2; + M.c2 = vector3_scale(-1, dp); + vector3 RHS = vector3_minus(pp, o); + vector3 tus = matrix3x3_vector3_mult(matrix3x3_inverse(M), RHS); + if (tus.x < -tus_tolerance || tus.y < -tus_tolerance || tus.x + tus.y > 1 + tus_tolerance) continue; + // a hit on the shared diagonal (tus.y = 0 in the second triangle) was counted by the first + if (tri == 1 && tus.y <= tus_tolerance) continue; + double s = tus.z; + if (num_intersections++ < slist_len) slist[num_intersections-1] = s; + } } // identify intersections with prism ceiling and floor faces @@ -3499,6 +3504,34 @@ double min_distance_to_quadrilateral(vector3 p, vector3 o, vector3 v1, vector3 v return sqrt(s * s + d * d); } +// like min_distance_to_quadrilateral, but for the planar convex quadrilateral with +// corners o, o+v1, o+v12, o+v2 (in that order) and unit normal v3, which need not be a +// parallelogram: a side face of a slanted prism is a trapezoid, because the two ends of +// its top edge are offset differently wherever the edge meets a neighbor at an angle +static double min_distance_to_planar_quadrilateral(vector3 p, vector3 o, vector3 v1, vector3 v12, + vector3 v2, vector3 v3) { + double s = normal_distance_to_plane(p, o, v1, v2, v3, NULL); + vector3 pPlane = vector3_minus(p, vector3_scale(s, v3)); + vector3 corners[4]; + corners[0] = o; + corners[1] = vector3_plus(o, v1); + corners[2] = vector3_plus(o, v12); + corners[3] = vector3_plus(o, v2); + // pPlane lies in the quadrilateral iff it is on the same side of all four edges + int k, num_positive = 0, num_negative = 0; + double d = HUGE_VAL; + for (k = 0; k < 4; k++) { + vector3 a = corners[k], b = corners[(k + 1) % 4]; + double side = vector3_dot(vector3_cross(vector3_minus(b, a), vector3_minus(pPlane, a)), v3); + if (side > 0) num_positive++; + if (side < 0) num_negative++; + if (vector3_norm(vector3_minus(b, a)) > 0) // the top edge can shrink to a point + d = fmin(d, min_distance_to_line_segment(pPlane, a, b)); + } + if (num_positive == 0 || num_negative == 0) return s; + return sqrt(s * s + d * d); +} + // fc==0/1 for floor/ceiling double min_distance_to_prism_roof_or_ceiling(vector3 pp, prism *prsm, int fc) { int num_vertices = prsm->vertices_p.num_items, i; @@ -3553,8 +3586,9 @@ vector3 normal_to_prism(prism *prsm, vector3 pc) { vector3 v0p = vps_bottom[nv]; vector3 v1p = vector3_minus(vps_bottom[nvp1], vps_bottom[nv]); vector3 v2p = vps_diff_to_top[nv]; + vector3 v12p = vector3_plus(v1p, vps_diff_to_top[nvp1]); // top corner above vps_bottom[nvp1] vector3 v3p = unit_vector3(vector3_cross(v1p, v2p)); - double s = min_distance_to_quadrilateral(pp, v0p, v1p, v2p, v3p); + double s = min_distance_to_planar_quadrilateral(pp, v0p, v1p, v12p, v2p, v3p); if (fabs(s) < min_distance) { min_distance = fabs(s); retval = v3p; diff --git a/utils/test-prism.c b/utils/test-prism.c index 4181ad3..89fbcb5 100644 --- a/utils/test-prism.c +++ b/utils/test-prism.c @@ -1241,6 +1241,167 @@ int test_slanted_prism_cross_sections() { return num_failed; } +/***************************************************************/ +/* Side faces of slanted prisms near corners. */ +/* */ +/* A side face of a slanted prism is the trapezoid between a */ +/* bottom edge and the matching top edge, whose ends are */ +/* offset differently wherever the edge meets its neighbor at */ +/* an angle. intersect_line_with_prism() and normal_to_prism() */ +/* used to treat it as the parallelogram spanned by the bottom */ +/* edge and the top offset of the edge's first vertex. Near */ +/* the edge's other end that parallelogram overhangs the face */ +/* (inward taper) or falls short of it (outward taper): */ +/* */ +/* * a line through an overhang gets a crossing it never */ +/* makes, and a line through a shortfall misses one it does */ +/* make. intersect_line_segment_with_prism() takes the */ +/* inside/outside state of a segment from the parity of the */ +/* crossings before it, so the error shows wherever the */ +/* line has further crossings -- including crossings with */ +/* parts of the prism far away along the same line, which */ +/* made subpixel fill fractions next to a sidewall depend */ +/* on distant geometry; */ +/* * a point beside an overhang was taken to be at its normal */ +/* distance from the face, and a point over a shortfall to */ +/* be off the face, so normal_to_prism() could return the */ +/* normal of a face other than the nearest one. */ +/***************************************************************/ +static int check_segment(geometric_object o, vector3 p, vector3 d, double a, double b, double expected, + const char *label) { + double actual = intersect_line_segment_with_object(p, d, o, a, b); + if (fabs(actual - expected) <= 1.0e-6 * fmax(1.0, fabs(expected))) return 0; + ctl_printf("\t[%s] the segment s in [%g, %g] of the line (%f, %f, %f) + s (%g, %g, %g) was expected\n" + "\t\tto have intersection length %f but instead had %f\n", + label, a, b, p.x, p.y, p.z, d.x, d.y, d.z, expected, actual); + return 1; +} + +static int check_normal(geometric_object o, vector3 p, vector3 expected_direction, const char *label) { + vector3 expected = standardize(expected_direction); + vector3 actual = standardize(normal_to_fixed_object(p, o)); + if (vector3_norm(vector3_minus(actual, expected)) <= 1.0e-6) return 0; + ctl_printf("\t[%s] at (%f, %f, %f) the normal should be (%f, %f, %f) but is (%f, %f, %f)\n", label, p.x, + p.y, p.z, expected.x, expected.y, expected.z, actual.x, actual.y, actual.z); + return 1; +} + +int test_slanted_prism_side_faces() { + printf("slanted prism side face testing:\n"); + void *m = NULL; + vector3 xhat = make_vector3(1, 0, 0); + vector3 yhat = make_vector3(0, 1, 0); + vector3 zhat = make_vector3(0, 0, 1); + double delta = 1.0e-3; // distance from the analytic wall at which we probe + int total_failed = 0, num_tests, num_failed, iz, is; + + // 1. An inward-tapering ridge along x, and the same ridge with a step that widens it on + // the -y side for x > 3; near x = 0 the two are the same solid. A line along x just + // outside the -y wall passes through the overhang of the x = -5 end face, and in the + // stepped ridge it also crosses the step, so the segment near x = 0 counted as inside. + { + double height = 0.3, sidewall_angle = 35.0 * K_PI / 180.0, half_width = 0.81; + double taper = tan(sidewall_angle); + vector3 straight[4] = {{-5, -half_width, 0}, {5, -half_width, 0}, {5, half_width, 0}, {-5, half_width, 0}}; + vector3 stepped[6] = {{-5, -half_width, 0}, {3, -half_width, 0}, {3, -1.5, 0}, + {5, -1.5, 0}, {5, half_width, 0}, {-5, half_width, 0}}; + geometric_object ridges[2]; + const char *labels[2] = {"straight ridge, +35 deg", "ridge with a step, +35 deg"}; + ridges[0] = make_slanted_prism(m, straight, 4, height, zhat, sidewall_angle); + ridges[1] = make_slanted_prism(m, stepped, 6, height, zhat, sidewall_angle); + int ir; + for (ir = 0; ir < 2; ir++) { + geom_fix_object_ptr(&ridges[ir]); + num_tests = num_failed = 0; + for (iz = 0; iz < 3; iz++) { + double z = 0.05 + 0.11 * iz; // 0.05, 0.16, 0.27 + double inset = z * taper; + double wy = half_width - inset; + for (is = 0; is < 4; is++) { + int inside = !(is & 2); + double y = ((is & 1) ? -1 : 1) * (wy + (inside ? -delta : delta)); + vector3 p = make_vector3(0, y, z); + // a short segment around x = 0, as in subpixel averaging + num_failed += check_segment(ridges[ir], p, xhat, -0.1, 0.1, inside ? 0.2 : 0.0, labels[ir]); + // the whole line; just outside the -y wall the step spans [3 + inset, 5 - inset] + double chord = inside ? 10 - 2 * inset : (ir == 1 && y < 0) ? 2 - 2 * inset : 0.0; + num_failed += check_segment(ridges[ir], p, xhat, -10, 10, chord, labels[ir]); + num_tests += 2; + } + } + printf("\t\t%i/%i tests failed with %s\n", num_failed, num_tests, labels[ir]); + total_failed += num_failed; + } + } + + // 2. An outward-tapering 4x2 rectangle. A line along x (y) just inside a wall passes + // through the strip where the parallelogram of one end face falls short, so it had a + // single crossing and the whole chord counted as outside. + { + double height = 0.5, sidewall_angle = -30.0 * K_PI / 180.0; + double taper = tan(sidewall_angle); + vector3 nodes[4] = {{-2, -1, 0}, {2, -1, 0}, {2, 1, 0}, {-2, 1, 0}}; + geometric_object o = make_slanted_prism(m, nodes, 4, height, zhat, sidewall_angle); + geom_fix_object_ptr(&o); + const char *label = "rectangle, -30 deg"; + num_tests = num_failed = 0; + for (iz = 0; iz < 3; iz++) { + double z = 0.05 + 0.2 * iz; // 0.05, 0.25, 0.45 + double wx = 2 - z * taper, wy = 1 - z * taper; + for (is = 0; is < 4; is++) { + double s = (is & 1) ? -1 : 1; + double f = (is & 2) ? 0.5 : 1.0; // just inside the wall, or well inside + vector3 px = make_vector3(0, s * (f * wy - delta), z); + vector3 py = make_vector3(s * (f * wx - delta), 0, z); + num_failed += check_segment(o, px, xhat, -10, 10, 2 * wx, label); + num_failed += check_segment(o, py, yhat, -10, 10, 2 * wy, label); + num_tests += 2; + } + } + printf("\t\t%i/%i tests failed with %s\n", num_failed, num_tests, label); + total_failed += num_failed; + } + + // 3. Normals beside the corners of a 4x2 rectangle, at mid-height. With an inward taper, + // a point just outside a side wall and barely in from an end lies beside the overhang + // of the end face, close to its plane, but the side wall is the nearest face. With an + // outward taper, a point just inside an end face near a side wall projects onto the + // end face where its parallelogram fell short, which made the side wall look nearer. + { + double height = 0.5, z = 0.25; + vector3 nodes[4] = {{-2, -1, 0}, {2, -1, 0}, {2, 1, 0}, {-2, 1, 0}}; + int ia, ic; + num_tests = num_failed = 0; + for (ia = 0; ia < 2; ia++) { + double sidewall_angle = (ia == 0 ? 30.0 : -30.0) * K_PI / 180.0; + double taper = tan(sidewall_angle); + double wx = 2 - z * taper, wy = 1 - z * taper; + const char *label = ia == 0 ? "normals beside corners, +30 deg" : "normals beside corners, -30 deg"; + geometric_object o = make_slanted_prism(m, nodes, 4, height, zhat, sidewall_angle); + geom_fix_object_ptr(&o); + for (ic = 0; ic < 4; ic++) { // the four corners + double sx = (ic & 1) ? -1 : 1, sy = (ic & 2) ? -1 : 1; + vector3 p, wall; + if (ia == 0) { // delta outside the side wall, 0.1*delta in from the end + p = make_vector3(sx * (wx - 0.1 * delta), sy * (wy + delta), z); + wall = make_vector3(0, sy, taper); + } + else { // delta inside the end face, 3*delta in from the side wall + p = make_vector3(sx * (wx - delta), sy * (wy - 3 * delta), z); + wall = make_vector3(sx, 0, taper); + } + num_failed += check_normal(o, p, wall, label); + num_tests++; + } + printf("\t\t%i/%i tests failed with %s\n", num_failed, num_tests, label); + total_failed += num_failed; + num_tests = num_failed = 0; + } + } + + return total_failed; +} + /***************************************************************/ /* unit tests: create the same parallelepiped two ways (as a */ /* block and as a prism) and verify that geometric primitives */ @@ -1313,9 +1474,10 @@ int run_unit_tests() { int num_failed_6 = test_octagon_c_base_sidewall_prisms_to_gnuplot(); int num_failed_7 = test_helper_functions_on_octagonal_c_prism(); int num_failed_8 = test_slanted_prism_cross_sections(); + int num_failed_9 = test_slanted_prism_side_faces(); return num_failed_1 + num_failed_3 + num_failed_4 + num_failed_5 + num_failed_6 + num_failed_7 + - num_failed_8;; + num_failed_8 + num_failed_9; } /***************************************************************/