From 5e38ed002fcce0d2c93d1c9e45f2e63a890f3ce4 Mon Sep 17 00:00:00 2001 From: Peter Willendrup Date: Sat, 31 Oct 2026 17:01:28 +0100 Subject: [PATCH] mcdisplay: filled surfaces at --trace=2 for McXtrace mirrors and capillary Replace line-only MCDISPLAY with polygon() facets (cylinder() for Capillary), so --trace=2 shows surfaces; --trace=1 keeps outlines. optics: Capillary, Mirror (flat case), Mirror_curved, Mirror_elliptic, Mirror_parabolic, Mirror_toroid, Multilayer_elliptic. contrib: Mirror_toroid_pothole. Integer loops replace float accumulation: Multilayer_elliptic no longer draws one step past length, toroid mirrors no longer drop the last step. Display only, no change to simulation results. Co-Authored-By: Claude Opus 5.5 --- .../contrib/Mirror_toroid_pothole.comp | 50 ++++--------------- mcxtrace-comps/optics/Capillary.comp | 7 +-- mcxtrace-comps/optics/Mirror.comp | 10 +--- mcxtrace-comps/optics/Mirror_curved.comp | 5 +- mcxtrace-comps/optics/Mirror_elliptic.comp | 33 ++++-------- mcxtrace-comps/optics/Mirror_parabolic.comp | 7 +-- mcxtrace-comps/optics/Mirror_toroid.comp | 50 ++++--------------- .../optics/Multilayer_elliptic.comp | 37 +++++--------- 8 files changed, 48 insertions(+), 151 deletions(-) diff --git a/mcxtrace-comps/contrib/Mirror_toroid_pothole.comp b/mcxtrace-comps/contrib/Mirror_toroid_pothole.comp index b906669732..d41eea550b 100644 --- a/mcxtrace-comps/contrib/Mirror_toroid_pothole.comp +++ b/mcxtrace-comps/contrib/Mirror_toroid_pothole.comp @@ -203,7 +203,7 @@ MCDISPLAY // Because the code above doesn't actually reproduce a torus exactly but uses a cylinder and an ellipsoid to construct the toroidal mirror (to avoid solving // quartic equations), there's a slight mismatch between the 3D view (exact torus) and the actual toroidal mirror coded above. - double x0, y0, z0, x1, y1, z1, theta, phi, phi_total, theta_total, theta_beginning, phi_beginning, theta_end, phi_end; + double theta, phi, phi_total, theta_total, theta_beginning, phi_beginning, theta_end, phi_end; // int N=50; too many points, makes the 3D matlab viewer bug int N = 10; @@ -221,46 +221,16 @@ MCDISPLAY theta_end = theta + (theta_total / 2); phi_end = phi + (phi_total / 2); - theta = theta_beginning; - phi = phi_beginning; - - while (theta <= theta_end) { - phi = 180 - (phi_total / 2); - x0 = radius * sin (DEG2RAD * theta); - y0 = (radius_o + radius * cos (DEG2RAD * theta)) * cos (DEG2RAD * phi) + (radius_o + radius); - z0 = (radius_o + radius * cos (DEG2RAD * theta)) * sin (DEG2RAD * phi); - while (phi <= phi_end) { - x1 = radius * sin (DEG2RAD * theta); - y1 = (radius_o + radius * cos (DEG2RAD * theta)) * cos (DEG2RAD * phi) + (radius_o + radius); - z1 = (radius_o + radius * cos (DEG2RAD * theta)) * sin (DEG2RAD * phi); - line (x0, y0, z0, x1, y1, z1); - x0 = x1; - y0 = y1; - z0 = z1; - phi += phi_total / N; - } - theta += theta_total / N; - } - - theta = theta_beginning; - phi = phi_beginning; - - while (phi <= phi_end) { - theta = -(theta_total / 2); - x0 = radius * sin (DEG2RAD * theta); - y0 = (radius_o + radius * cos (DEG2RAD * theta)) * cos (DEG2RAD * phi) + (radius_o + radius); - z0 = (radius_o + radius * cos (DEG2RAD * theta)) * sin (DEG2RAD * phi); - while (theta <= theta_end) { - x1 = radius * sin (DEG2RAD * theta); - y1 = (radius_o + radius * cos (DEG2RAD * theta)) * cos (DEG2RAD * phi) + (radius_o + radius); - z1 = (radius_o + radius * cos (DEG2RAD * theta)) * sin (DEG2RAD * phi); - line (x0, y0, z0, x1, y1, z1); - x0 = x1; - y0 = y1; - z0 = z1; - theta += theta_total / N; + int i, j; + for (i = 0; i < N; i++) { + for (j = 0; j < N; j++) { + double t0 = DEG2RAD * (theta_beginning + i * theta_total / N), t1 = DEG2RAD * (theta_beginning + (i + 1) * theta_total / N); + double p0 = DEG2RAD * (phi_beginning + j * phi_total / N), p1 = DEG2RAD * (phi_beginning + (j + 1) * phi_total / N); + polygon (4, radius * sin (t0), (radius_o + radius * cos (t0)) * cos (p0) + (radius_o + radius), (radius_o + radius * cos (t0)) * sin (p0), + radius * sin (t1), (radius_o + radius * cos (t1)) * cos (p0) + (radius_o + radius), (radius_o + radius * cos (t1)) * sin (p0), radius * sin (t1), + (radius_o + radius * cos (t1)) * cos (p1) + (radius_o + radius), (radius_o + radius * cos (t1)) * sin (p1), radius * sin (t0), + (radius_o + radius * cos (t0)) * cos (p1) + (radius_o + radius), (radius_o + radius * cos (t0)) * sin (p1)); } - phi += phi_total / N; } %} diff --git a/mcxtrace-comps/optics/Capillary.comp b/mcxtrace-comps/optics/Capillary.comp index c94c780cca..fc7489c7e7 100644 --- a/mcxtrace-comps/optics/Capillary.comp +++ b/mcxtrace-comps/optics/Capillary.comp @@ -234,12 +234,7 @@ TRACE MCDISPLAY %{ - circle ("xy", 0, 0, 0, radius); - circle ("xy", 0, 0, length, radius); - line (radius, 0, 0, radius, 0, length); - line (0, radius, 0, 0, radius, length); - line (-radius, 0, 0, -radius, 0, length); - line (0, -radius, 0, 0, -radius, length); + cylinder (0, 0, length / 2.0, radius, length, 0, 0, 0, 1); %} END diff --git a/mcxtrace-comps/optics/Mirror.comp b/mcxtrace-comps/optics/Mirror.comp index 5637b044cb..970aa983ee 100644 --- a/mcxtrace-comps/optics/Mirror.comp +++ b/mcxtrace-comps/optics/Mirror.comp @@ -164,15 +164,9 @@ MCDISPLAY if (shape == ANY) { /* OFF file */ off_display (offdata); } else if (yheight) { - line (0, -yheight / 2.0, -zdepth / 2.0, 0, yheight / 2.0, -zdepth / 2.0); - line (0, -yheight / 2.0, zdepth / 2.0, 0, yheight / 2.0, zdepth / 2.0); - line (0, -yheight / 2.0, -zdepth / 2.0, 0, -yheight / 2.0, zdepth / 2.0); - line (0, yheight / 2.0, -zdepth / 2.0, 0, yheight / 2.0, zdepth / 2.0); + polygon (4, 0.0, -yheight / 2.0, -zdepth / 2.0, 0.0, yheight / 2.0, -zdepth / 2.0, 0.0, yheight / 2.0, zdepth / 2.0, 0.0, -yheight / 2.0, zdepth / 2.0); } else { - line (-xwidth / 2.0, 0, -zdepth / 2.0, xwidth / 2.0, 0, -zdepth / 2.0); - line (-xwidth / 2.0, 0, zdepth / 2.0, xwidth / 2.0, 0, zdepth / 2.0); - line (-xwidth / 2.0, 0, -zdepth / 2.0, -xwidth / 2.0, 0, zdepth / 2.0); - line (xwidth / 2.0, 0, -zdepth / 2.0, xwidth / 2.0, 0, zdepth / 2.0); + polygon (4, -xwidth / 2.0, 0.0, -zdepth / 2.0, xwidth / 2.0, 0.0, -zdepth / 2.0, xwidth / 2.0, 0.0, zdepth / 2.0, -xwidth / 2.0, 0.0, zdepth / 2.0); } %} diff --git a/mcxtrace-comps/optics/Mirror_curved.comp b/mcxtrace-comps/optics/Mirror_curved.comp index f41b6d4227..6750399408 100644 --- a/mcxtrace-comps/optics/Mirror_curved.comp +++ b/mcxtrace-comps/optics/Mirror_curved.comp @@ -154,16 +154,13 @@ MCDISPLAY alpha = -(zdepth / 2.0) / fabs (radius); x0 = radius * (1 - cos (alpha)); z0 = radius * sin (alpha); - line (x0, y0, z0, x0, y1, z0); while (alpha < (zdepth / 2.0 / fabs (radius))) { alpha += zdepth / N / fabs (radius); x1 = radius * (1 - cos (alpha)); z1 = radius * sin (alpha); - line (x0, y0, z0, x1, y0, z1); - line (x0, y1, z0, x1, y1, z1); + polygon (4, x0, y0, z0, x0, y1, z0, x1, y1, z1, x1, y0, z1); x0 = x1; z0 = z1; - line (x0, y0, z0, x0, y1, z0); } %} diff --git a/mcxtrace-comps/optics/Mirror_elliptic.comp b/mcxtrace-comps/optics/Mirror_elliptic.comp index 27a2bf74ec..bd548e3495 100644 --- a/mcxtrace-comps/optics/Mirror_elliptic.comp +++ b/mcxtrace-comps/optics/Mirror_elliptic.comp @@ -194,34 +194,21 @@ TRACE MCDISPLAY %{ int i, j, N = 12; - double t, xx, yy, zz, xx0, yy0, zz0; double a2, b2, c2; a2 = 1.0 / a2inv; b2 = 1.0 / b2inv; c2 = 1.0 / c2inv; if (xwidth && zdepth) { - for (i = 0; i < N; i++) { - xx0 = -xwidth / 2.0; - zz = i * zdepth / (N - 1) - zdepth / 2.0; - yy0 = -(1) * sqrt (b2 * (1 - xx0 * xx0 / a2 - zz * zz / c2)) + sqrt (b2); - for (j = 1; j < N; j++) { - xx = j * xwidth / (N - 1) - xwidth / 2.0; - yy = -(1) * sqrt (b2 * (1 - xx * xx / a2 - zz * zz / c2)) + sqrt (b2); - line (xx0, yy0, zz, xx, yy, zz); - xx0 = xx; - yy0 = yy; - } - } - for (i = 0; i < N; i++) { - zz0 = -zdepth / 2.0; - xx = i * xwidth / (N - 1) - xwidth / 2.0; - yy0 = -(1) * sqrt (b2 * (1 - xx * xx / a2 - zz0 * zz0 / c2)) + sqrt (b2); - for (j = 1; j < N; j++) { - zz = j * zdepth / (N - 1) - zdepth / 2.0; - yy = -(1) * sqrt (b2 * (1 - xx * xx / a2 - zz * zz / c2)) + sqrt (b2); - line (xx, yy0, zz0, xx, yy, zz); - zz0 = zz; - yy0 = yy; + double dx = xwidth / (N - 1), dz = zdepth / (N - 1); + for (i = 0; i < N - 1; i++) { + for (j = 0; j < N - 1; j++) { + double x0 = i * dx - xwidth / 2.0, x1 = x0 + dx; + double z0 = j * dz - zdepth / 2.0, z1 = z0 + dz; + double y00 = -sqrt (b2 * (1 - x0 * x0 / a2 - z0 * z0 / c2)) + sqrt (b2); + double y10 = -sqrt (b2 * (1 - x1 * x1 / a2 - z0 * z0 / c2)) + sqrt (b2); + double y11 = -sqrt (b2 * (1 - x1 * x1 / a2 - z1 * z1 / c2)) + sqrt (b2); + double y01 = -sqrt (b2 * (1 - x0 * x0 / a2 - z1 * z1 / c2)) + sqrt (b2); + polygon (4, x0, y00, z0, x1, y10, z0, x1, y11, z1, x0, y01, z1); } } } diff --git a/mcxtrace-comps/optics/Mirror_parabolic.comp b/mcxtrace-comps/optics/Mirror_parabolic.comp index 5a2784efdc..ee34a8ebaa 100644 --- a/mcxtrace-comps/optics/Mirror_parabolic.comp +++ b/mcxtrace-comps/optics/Mirror_parabolic.comp @@ -263,12 +263,7 @@ MCDISPLAY y[2] = x0 * x0 * a2inv + z1 * z1 * b2inv; y[3] = x1 * x1 * a2inv + z1 * z1 * b2inv; - line (x0, y[0], z0, x1, y[1], z0); - line (x0, y[0], z0, x0, y[2], z1); - if (i == Ni - 1) - line (x1, y[1], z0, x1, y[3], z1); - if (j == Nj - 1) - line (x0, y[2], z1, x1, y[3], z1); + polygon (4, x0, y[0], z0, x1, y[1], z0, x1, y[3], z1, x0, y[2], z1); } } %} diff --git a/mcxtrace-comps/optics/Mirror_toroid.comp b/mcxtrace-comps/optics/Mirror_toroid.comp index 2bfe93cc1f..3ff7d2a25e 100644 --- a/mcxtrace-comps/optics/Mirror_toroid.comp +++ b/mcxtrace-comps/optics/Mirror_toroid.comp @@ -137,7 +137,7 @@ MCDISPLAY // Because the code above doesn't actually reproduce a torus exactly but uses a cylinder and an ellipsoid to construct the toroidal mirror (to avoid solving // quartic equations), there's a slight mismatch between the 3D view (exact torus) and the actual toroidal mirror coded above. - double x0, y0, z0, x1, y1, z1, theta, phi, phi_total, theta_total, theta_beginning, phi_beginning, theta_end, phi_end; + double theta, phi, phi_total, theta_total, theta_beginning, phi_beginning, theta_end, phi_end; // int N=50; too many points, makes the 3D matlab viewer bug int N = 10; @@ -155,46 +155,16 @@ MCDISPLAY theta_end = theta + (theta_total / 2); phi_end = phi + (phi_total / 2); - theta = theta_beginning; - phi = phi_beginning; - - while (theta <= theta_end) { - phi = 180 - (phi_total / 2); - x0 = radius * sin (DEG2RAD * theta); - y0 = (radius_o + radius * cos (DEG2RAD * theta)) * cos (DEG2RAD * phi) + (radius_o + radius); - z0 = (radius_o + radius * cos (DEG2RAD * theta)) * sin (DEG2RAD * phi); - while (phi <= phi_end) { - x1 = radius * sin (DEG2RAD * theta); - y1 = (radius_o + radius * cos (DEG2RAD * theta)) * cos (DEG2RAD * phi) + (radius_o + radius); - z1 = (radius_o + radius * cos (DEG2RAD * theta)) * sin (DEG2RAD * phi); - line (x0, y0, z0, x1, y1, z1); - x0 = x1; - y0 = y1; - z0 = z1; - phi += phi_total / N; + int i, j; + for (i = 0; i < N; i++) { + for (j = 0; j < N; j++) { + double t0 = DEG2RAD * (theta_beginning + i * theta_total / N), t1 = DEG2RAD * (theta_beginning + (i + 1) * theta_total / N); + double p0 = DEG2RAD * (phi_beginning + j * phi_total / N), p1 = DEG2RAD * (phi_beginning + (j + 1) * phi_total / N); + polygon (4, radius * sin (t0), (radius_o + radius * cos (t0)) * cos (p0) + (radius_o + radius), (radius_o + radius * cos (t0)) * sin (p0), + radius * sin (t1), (radius_o + radius * cos (t1)) * cos (p0) + (radius_o + radius), (radius_o + radius * cos (t1)) * sin (p0), radius * sin (t1), + (radius_o + radius * cos (t1)) * cos (p1) + (radius_o + radius), (radius_o + radius * cos (t1)) * sin (p1), radius * sin (t0), + (radius_o + radius * cos (t0)) * cos (p1) + (radius_o + radius), (radius_o + radius * cos (t0)) * sin (p1)); } - theta += theta_total / N; - } - - theta = theta_beginning; - phi = phi_beginning; - - while (phi <= phi_end) { - theta = -(theta_total / 2); - x0 = radius * sin (DEG2RAD * theta); - y0 = (radius_o + radius * cos (DEG2RAD * theta)) * cos (DEG2RAD * phi) + (radius_o + radius); - z0 = (radius_o + radius * cos (DEG2RAD * theta)) * sin (DEG2RAD * phi); - while (theta <= theta_end) { - x1 = radius * sin (DEG2RAD * theta); - y1 = (radius_o + radius * cos (DEG2RAD * theta)) * cos (DEG2RAD * phi) + (radius_o + radius); - z1 = (radius_o + radius * cos (DEG2RAD * theta)) * sin (DEG2RAD * phi); - line (x0, y0, z0, x1, y1, z1); - x0 = x1; - y0 = y1; - z0 = z1; - theta += theta_total / N; - } - phi += phi_total / N; } line (-xwidth / 2.0, 0, -zdepth / 2.0, xwidth / 2.0, 0, -zdepth / 2.0); line (-xwidth / 2.0, 0, zdepth / 2.0, xwidth / 2.0, 0, zdepth / 2.0); diff --git a/mcxtrace-comps/optics/Multilayer_elliptic.comp b/mcxtrace-comps/optics/Multilayer_elliptic.comp index 750112f9a4..e399e4892c 100644 --- a/mcxtrace-comps/optics/Multilayer_elliptic.comp +++ b/mcxtrace-comps/optics/Multilayer_elliptic.comp @@ -232,31 +232,20 @@ TRACE MCDISPLAY %{ - /* - rectangle("xz",0,0,0,width,length); */ - int i, j, NN = 10; - double x0, y0, z0; - double x1, y1, z1, z_el, y_el; - - x0 = -width / 2.0; - - for (i = 0; i <= NN; i++) { - z0 = -length / 2.0; - z_el = cos (xi) * z0 + Z0; // transformation to EL reference frame + int j, NN = 10; + double z0, y0, z1, y1, z_el, y_el; + z0 = -length / 2.0; + z_el = cos (xi) * z0 + Z0; // transformation to EL reference frame + y_el = b * sqrt (1.0 - ((z_el * z_el) / (a * a))); + y0 = cos (xi) * (y_el - Y0) - sin (xi) * (z_el - Z0); + for (j = 0; j < NN; j++) { + z1 = z0 + length / NN; + z_el = cos (xi) * z1 + Z0; y_el = b * sqrt (1.0 - ((z_el * z_el) / (a * a))); - y0 = cos (xi) * (y_el - Y0) - sin (xi) * (z_el - Z0); - line (-width / 2, y0, z0, width / 2, y0, z0); - for (j = 0; j <= NN; j++) { - z1 = z0 + length / NN; - z_el = cos (xi) * z1 + Z0; - y_el = b * sqrt (1.0 - ((z_el * z_el) / (a * a))); - y1 = cos (xi) * (y_el - Y0) - sin (xi) * (z_el - Z0); - line (x0, y0, z0, x0, y1, z1); - y0 = y1; - z0 = z1; - line (-width / 2, y1, z1, width / 2, y1, z1); - } - x0 = x0 + width / NN; + y1 = cos (xi) * (y_el - Y0) - sin (xi) * (z_el - Z0); + polygon (4, -width / 2.0, y0, z0, width / 2.0, y0, z0, width / 2.0, y1, z1, -width / 2.0, y1, z1); + y0 = y1; + z0 = z1; } %}