Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
c56135b
Shape, Shape_simple: use geometry-mesh-lib for the geometry file
Nov 10, 2026
597324a
geometry-mesh-lib: add gmesh_intersect_gravity for parabolic paths
Nov 10, 2026
4fd172b
McStas components: use geometry-mesh-lib instead of interoff-lib
Nov 10, 2026
2e75bfd
Test_lib_geometry_mesh_lib: check gmesh_intersect_gravity
Nov 10, 2026
f095187
Act on the review of PR #99
Nov 10, 2026
f6a7235
geometry-mesh-lib: rename gmesh_hit.t to time, drop gmesh_hit_time
Nov 10, 2026
ed142b3
geometry-mesh-lib: add gmesh_entry_exit, used by the samples and Refr…
Nov 10, 2026
6ca520d
Lens: use gmesh_entry_exit for the next mesh hit
Nov 10, 2026
43233c1
geometry-mesh-lib: add gmesh_entry_exit_gravity
Nov 10, 2026
96e6cf7
Monitor_nD: use gmesh_entry_exit for mesh geometry
Nov 10, 2026
198343d
geometry-mesh-lib: GMESH_TOL for the surface tolerance of gmesh_entry…
Nov 10, 2026
ef7efd2
socket.off: retriangulate the flat top and bottom faces
Nov 11, 2026
1a4c743
geometry-mesh-lib: fold check and gmesh_inside shortcut; components u…
Nov 11, 2026
37da27f
geometry-mesh-lib: gmesh_inside without box padding; t_min = GMESH_TO…
Nov 11, 2026
9ed95ba
geometry-mesh-lib: optional closed check gmesh_check_closed with the …
Nov 11, 2026
b16525a
Remove unnecessary closed mesh check
Lomholy Nov 11, 2026
8fddf30
geometry-mesh-lib: count folded edges in gmesh_init, reject them in g…
Nov 11, 2026
0407ea7
geometry-mesh-lib: remove GMESH_TOL; components step past the surface…
Nov 11, 2026
408843a
Merge pull request #99 from lomholy-agentic/shape-geometry-mesh-lib
Lomholy Nov 11, 2026
25f6445
geometry-mesh-lib: rename constant parameter; libtest file per MPI pr…
Nov 11, 2026
e6db8fb
Merge pull request #100 from lomholy-agentic/mesh-lib-sasview-constant
Lomholy Nov 11, 2026
2dcd7ad
Format mesh_lib components with mccode-clangformat
Nov 12, 2026
dbbccfe
Merge pull request #103 from lomholy-agentic/mesh-lib-clangformat-comps
Lomholy Nov 12, 2026
fa233cf
Merge branch 'mccode-dev:main' into mesh_lib
Lomholy Nov 12, 2026
0a7e626
geometry-mesh-lib: review changes for mccode-dev/McCode#3090
Nov 13, 2026
72f17fb
Merge branch 'main' into mesh_lib
willend Nov 15, 2026
fb9ee59
geometry-mesh-lib: keep origin scaling and gmesh_hit.time
Nov 16, 2026
fd789d4
Merge pull request #115 from lomholy-agentic/mesh-lib-review-3090
Lomholy Nov 16, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
343 changes: 337 additions & 6 deletions common/lib/share/geometry-mesh-lib.c

Large diffs are not rendered by default.

45 changes: 42 additions & 3 deletions common/lib/share/geometry-mesh-lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@
* - computes unit normals, area, volume and mesh topology: number of edges,
* boundary / non-manifold edges, Euler characteristic, consistent
* orientation. Closed, consistently oriented meshes are oriented with
* outward normals.
* outward normals. Edges where the surface folds back onto itself are
* counted (nfolded); a folded mesh is not closed.
* - builds a bounding volume hierarchy (BVH) so that a ray costs
* O(log N) instead of O(N) triangle tests, without any allocation per ray.
*
* Ray tracing
* gmesh_intersect() the GMESH_MAX_HITS nearest intersections of a
* straight line, sorted in t
* gmesh_intersect_gravity() the same for a parabola (e.g. under gravity)
* gmesh_inside() point-in-mesh test (deterministic, no random numbers)
* gmesh_entry_exit() first entry and exit of a line (only exit from inside)
* gmesh_check_closed() optional check that a mesh is closed and not folded
* Hits exactly on an edge or vertex shared by several triangles are
* reported once.
*
Expand Down Expand Up @@ -82,7 +86,7 @@
#endif

typedef struct gmesh_hit {
double t; // position along the line: pos + t*dir
double time; // position along the line: pos + time*dir
Coords normal; // unit normal of the triangle hit (outward for closed oriented meshes)
int tri; // triangle index (BVH order)
int face; // index of the face (polygon) in the file, 0-based
Expand Down Expand Up @@ -125,8 +129,10 @@ typedef struct gmesh_struct {
double area, volume; // surface area, enclosed volume (closed meshes)
int nedges, nboundary, nnonmanifold, ndegenerate, nwelded;
int nduplicate; // faces given more than once (all copies removed)
int nfolded; // edges where the surface folds back onto itself (see gmesh_check_closed)
int folded_face[2]; // faces of the first folded edge, 0-based
int euler; // V - E + F (2 for a closed genus-0 surface)
int closed; // every edge shared by exactly two triangles
int closed; // every edge shared by exactly two triangles, and not folded
int oriented; // shared edges run in opposite directions
int flipped; // orientation was reversed to make normals point outward
char* filename;
Expand Down Expand Up @@ -166,13 +172,46 @@ int gmesh_init_fit (char* filename, double xwidth, double yheight, double zdepth
#pragma acc routine seq
int gmesh_intersect (gmesh_struct* mesh, Coords position, Coords direction, double t_min, gmesh_hit hits[GMESH_MAX_HITS]);

// int gmesh_intersect_gravity(gmesh_struct *mesh, Coords position,
// Coords velocity, Coords acceleration, double t_min,
// gmesh_hit hits[GMESH_MAX_HITS])
// ACTION: as gmesh_intersect, for the parabola
// position + t*velocity + t^2/2*acceleration (e.g. a neutron under
// gravity, with acceleration in the frame of the mesh). in_out refers to
// the velocity at the hit, velocity + t*acceleration. With a zero
// acceleration the hits are those of gmesh_intersect, to rounding.
// RETURN: number of hits stored
#pragma acc routine seq
int gmesh_intersect_gravity (gmesh_struct* mesh, Coords position, Coords velocity, Coords acceleration, double t_min, gmesh_hit hits[GMESH_MAX_HITS]);

// int gmesh_inside(gmesh_struct *mesh, Coords position)
// ACTION: point in (closed) mesh test, parity of ray crossings along fixed
// directions. Does not use random numbers.
// RETURN: 1 inside, 0 outside
#pragma acc routine seq
int gmesh_inside (gmesh_struct* mesh, Coords position);

// int gmesh_entry_exit(gmesh_struct *mesh, Coords position, Coords velocity,
// double *t_in, double *t_out, gmesh_hit *first)
// ACTION: path of the line position + t*velocity (t > 0) through a closed
// mesh. Inside: t_in = 0 and t_out is the first hit. Outside: t_in is the
// first hit and t_out the next one (the first when it is the only one).
// Hit times are 1e-13 s past the surface, so that a particle moved there is
// on the far side and does not hit that surface again. An open mesh counts
// as outside. first, when not NULL, gets the first hit.
// RETURN: 0 when the mesh is not ahead, 1 inside, 2 outside
#pragma acc routine seq
int gmesh_entry_exit (gmesh_struct* mesh, Coords position, Coords velocity, double* t_in, double* t_out, gmesh_hit* first);

// int gmesh_check_closed(gmesh_struct *mesh)
// ACTION: optional check for components that need a volume: the mesh must be
// closed (every edge shared by two triangles) and must not fold back onto
// itself (no edge whose two triangles turn back onto each other, with
// antiparallel normals), as then inside and outside are not defined.
// Prints the reason when the mesh does not pass.
// RETURN: 1 when the mesh passes, 0 otherwise
int gmesh_check_closed (gmesh_struct* mesh);

// per-face property number 'property' of face 'face', or fallback when it is
// not given in the file
#pragma acc routine seq
Expand Down
111 changes: 39 additions & 72 deletions mcstas-comps/contrib/Lens.comp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* The complex geometry option handles any closed non-convex polyhedra.
* It computes the intersection points of the neutron ray with the object
* transparently, so that it can be used like a regular sample object.
* It supports the PLY, OFF and NOFF file format but not COFF (colored faces).
* It supports the OFF (incl. NOFF, COFF), PLY and STL file formats.
* Such files may be generated from XYZ data using:
* qhull < coordinates.xyz Qx Qv Tv o > geomview.off
* or
Expand Down Expand Up @@ -88,7 +88,7 @@
* sigma_abs: [barn] thermal absorption cross section
* p_interact: [1] MC Probability for scattering the ray; otherwise transmit
* RMS: [AA] root mean square roughness of the surface
* geometry: [str] Name of an Object File Format (OFF) or PLY file for complex geometry. The OFF/PLY file may be generated from XYZ coordinates using qhull/powercrust
* geometry: [str] Name of an OFF, PLY or STL file for complex geometry. An OFF file may be generated from XYZ coordinates using qhull/powercrust
*
* %L
* M. L. Goldberger et al, Phys. Rev. 71, 294 - 310 (1947)
Expand Down Expand Up @@ -123,7 +123,7 @@ SHARE
%{
/* support for OFF/PLY geometry */
%include "read_table-lib"
%include "interoff-lib"
%include "geometry-mesh-lib"

/* Lens_roughness: function to rotate normal vector around axis for roughness
* with specified tilt angle (deg)
Expand Down Expand Up @@ -219,7 +219,7 @@ DECLARE
double my_a_v;
double mean_n;
double events;
off_struct offdata;
gmesh_struct mesh;
%}

INITIALIZE
Expand All @@ -239,8 +239,14 @@ INITIALIZE
exit (-1);
#else
/* init the OFF/PLY without centering and scaling */
if (!off_init (geometry, 0, 0, 0, 1, &offdata))
exit (printf ("Lens: %s: FATAL: could not initialize geometry file %s [OFF/PLY]\n", NAME_CURRENT_COMP, geometry));
if (!gmesh_init_fit (geometry, 0, 0, 0, 0, GMESH_PROPS_IGNORE, &mesh)) {
printf ("Lens: %s: FATAL: could not initialize geometry file %s [OFF/PLY]\n", NAME_CURRENT_COMP, geometry);
exit (-1);
}
if (!gmesh_check_closed (&mesh)) {
fprintf (stderr, "Lens: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n", NAME_CURRENT_COMP, geometry);
exit (-1);
}
#endif
}

Expand All @@ -266,10 +272,10 @@ TRACE

#ifdef OPENACC
#ifdef USE_OFF
off_struct thread_offdata = offdata;
gmesh_struct thread_mesh = mesh;
#endif
#else
#define thread_offdata offdata
#define thread_mesh mesh
#endif

/* iterate two faces intersection until no more is possible */
Expand All @@ -279,28 +285,15 @@ TRACE
nx = ny = nz = 0;
#ifdef USE_OFF
if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) {
double t1 = 0, t0 = 0;
Coords n, n0, n1;
n = n0 = n1 = coords_set (0, 0, 0);
int intersect = off_intersect (&t0, &t1, &n0, &n1, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata);
if (!intersect)
gmesh_hit hits[GMESH_MAX_HITS];
// t_min = 1e-13 s skips the surface the neutron sits on after a refraction or reflection
if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 1e-13, hits)) {
dt = hits[0].time;
// normal oriented as in interoff-lib: opposite to the right-hand rule of
// the vertex order in the file, unless the mesh was flipped
coords_get (coords_scale (hits[0].normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz);
} else
dt = -1;
else if (t0 < 0 && 0 < t1) {
dt = t1;
n = n1;
} else if (t1 < 0 && 0 < t0) {
dt = t0;
n = n0;
} else {
if (t1 < t0) {
dt = t1;
n = n1;
} else {
dt = t0;
n = n0;
}
}
coords_get (n, &nx, &ny, &nz);
} else
#endif
if (r1 && !focus1) {
Expand Down Expand Up @@ -390,28 +383,15 @@ TRACE
nx = ny = nz = 0;
#ifdef USE_OFF
if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) {
double t1 = 0, t0 = 0;
Coords n, n0, n1;
n = n0 = n1 = coords_set (0, 0, 0);
int intersect = off_intersect (&t0, &t1, &n0, &n1, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata);
if (!intersect)
gmesh_hit hits[GMESH_MAX_HITS];
// t_min = 1e-13 s skips the surface the neutron sits on after a refraction or reflection
if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 1e-13, hits)) {
dt = hits[0].time;
// normal oriented as in interoff-lib: opposite to the right-hand rule of
// the vertex order in the file, unless the mesh was flipped
coords_get (coords_scale (hits[0].normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz);
} else
dt = -1;
else if (t0 < 0 && 0 < t1) {
dt = t1;
n = n1;
} else if (t1 < 0 && 0 < t0) {
dt = t0;
n = n0;
} else {
if (t1 < t0) {
dt = t1;
n = n1;
} else {
dt = t0;
n = n0;
}
}
coords_get (n, &nx, &ny, &nz);
} else
#endif
if (r2 && !focus2)
Expand Down Expand Up @@ -514,28 +494,15 @@ TRACE
nx = ny = nz = 0;
#ifdef USE_OFF
if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) {
double t1 = 0, t0 = 0;
Coords n, n0, n1;
n = n0 = n1 = coords_set (0, 0, 0);
int intersect = off_intersect (&t0, &t1, &n0, &n1, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata);
if (!intersect)
gmesh_hit hits[GMESH_MAX_HITS];
// t_min = 1e-13 s skips the surface the neutron sits on after a refraction or reflection
if (gmesh_intersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), 1e-13, hits)) {
dt = hits[0].time;
// normal oriented as in interoff-lib: opposite to the right-hand rule of
// the vertex order in the file, unless the mesh was flipped
coords_get (coords_scale (hits[0].normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz);
} else
dt = -1;
else if (t0 < 0 && 0 < t1) {
dt = t1;
n = n1;
} else if (t1 < 0 && 0 < t0) {
dt = t0;
n = n0;
} else {
if (t1 < t0) {
dt = t1;
n = n1;
} else {
dt = t0;
n = n0;
}
}
coords_get (n, &nx, &ny, &nz);
} else
#endif
if (r2 && !focus2)
Expand Down Expand Up @@ -640,7 +607,7 @@ MCDISPLAY

/* draw circles to describe the 2 surfaces */
if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { /* OFF file */
off_display (offdata);
gmesh_display (&mesh);
} else {
if (r1 && !focus1) { /* sphere1 */
theta00 = asin (fabs ((radius / 2) / r1));
Expand Down
34 changes: 22 additions & 12 deletions mcstas-comps/contrib/Single_crystal_inelastic.comp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
* yheight: [m] Height of crystal.
* zdepth: [m] Depth of crystal (no extinction simulated)
* sqw: [string] Name of a 4d S(q,w) file - example.sqw4 is included in your installation
* geometry: [string] Name of an Object File Format (OFF) or PLY file for complex geometry. The OFF/PLY file may be generated from XYZ coordinates using qhull/powercrust.
* geometry: [string] Name of an OFF, PLY or STL file for complex geometry. An OFF file may be generated from XYZ coordinates using qhull/powercrust. The mesh must be a single, closed object: only the first entry and exit along the neutron path are used, so a second part of the mesh further along the path (disjoint parts, or a concave shape the path re-enters) is not seen.
* delta_d_d: [1] Lattice spacing variance, gaussian RMS.
* mosaic: [arcmin] Isotropic crystal mosaic, gaussian RMS. Puts the crystal in the isotropic mosaic model state, thus disregarding other mosaicity parameters.
* mosaic_a: [arcmin] Horizontal (rotation around lattice vector a) mosaic (anisotropic), gaussian RMS. Put the crystal in the anisotropic crystal vector state. I.e. model mosaicity through rotation around the crystal lattice vectors. Has precedence over in-plane mosaic model.
Expand Down Expand Up @@ -120,7 +120,8 @@ SHARE
%{
/* used for reading data table from file */
%include "read_table-lib"
%include "interoff-lib"
%include "geometry-mesh-lib"

/* Declare structures and functions only once in each instrument. */
#ifndef SINGLE_CRYSTAL_DECL
#define SINGLE_CRYSTAL_DECL
Expand Down Expand Up @@ -163,10 +164,10 @@ SHARE
double* SwCDF; /* Cumulative dist. func. of S(|Q|) for inv. transform sampling */
int nSw; /* Number of points in CDF */
int** SwQi;
int* nQ; /* Number of q-points at each energy */
double* SqwCDF; /* Cumulative dist. func. of S(E) at particular values of |Q| */
int* iSqwCDF; /* Index into CDF of S(E) */
int maxecount; /* Maximum number of E-slice for each |Q| */
int* nQ; /* Number of q-points at each energy */
double* SqwCDF; /* Cumulative dist. func. of S(E) at particular values of |Q| */
int* iSqwCDF; /* Index into CDF of S(E) */
int maxecount; /* Maximum number of E-slice for each |Q| */
};

/* Sum of S(q,w) over the hkl-E points kinematically accessible with (kix,kiy,kiz).
Expand Down Expand Up @@ -239,7 +240,8 @@ SHARE
}

int
read_hkl_data (char* SC_file, struct hkl_info_struct* info, double SC_mosaic, double SC_mosaic_a, double SC_mosaic_b, double SC_mosaic_c, double* SC_mosaic_AB) {
read_hkl_data (char* SC_file, struct hkl_info_struct* info, double SC_mosaic, double SC_mosaic_a, double SC_mosaic_b, double SC_mosaic_c,
double* SC_mosaic_AB) {
struct hkl_data* list = NULL;
int size = 0;
t_Table sTable; /* sample data table structure from SC_file */
Expand Down Expand Up @@ -663,7 +665,7 @@ SHARE
DECLARE
%{
struct hkl_info_struct hkl_info;
off_struct offdata;
gmesh_struct mesh;
%}

INITIALIZE
Expand Down Expand Up @@ -716,8 +718,16 @@ INITIALIZE

hkl_info.shape = -1; /* -1:no shape, 0:cyl, 1:box, 2:sphere, 3:any-shape */
if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) {
if (off_init (geometry, xwidth, yheight, zdepth, 0, &offdata)) {
if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh))
hkl_info.shape = 3;
else {
fprintf (stderr, "Single_crystal_inelastic: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry);
exit (-1);
}
if (!gmesh_check_closed (&mesh)) {
fprintf (stderr, "Single_crystal_inelastic: %s: the mesh %s is not closed or folds back onto itself (see the error above). Aborting.\n",
NAME_CURRENT_COMP, geometry);
exit (-1);
}
} else if (xwidth && yheight && zdepth)
hkl_info.shape = 1; /* box */
Expand Down Expand Up @@ -799,7 +809,7 @@ TRACE
else if (hkl_info.shape == 2)
intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius);
else if (hkl_info.shape == 3)
intersect = off_intersect (&t1, &t2, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, offdata);
intersect = gmesh_entry_exit (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2, NULL);

if (t2 < 0)
intersect = 0; /* we passed sample volume already */
Expand Down Expand Up @@ -833,7 +843,7 @@ TRACE
else if (hkl_info.shape == 2)
intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius);
else if (hkl_info.shape == 3)
intersect = off_intersect (&t1, &t2, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, offdata);
intersect = gmesh_entry_exit (&mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2, NULL) == 1;
if (!intersect || t2 * v < -1e-9 || t1 * v > 1e-9) {
/* neutron is leaving the sample */
if (hkl_info.flag_warning < 100)
Expand Down Expand Up @@ -935,7 +945,7 @@ MCDISPLAY
} else if (hkl_info.shape == 2) { /* sphere */
sphere (0, 0, 0, radius);
} else if (hkl_info.shape == 3) { /* OFF file */
off_display (offdata);
gmesh_display (&mesh);
}
%}
END
Expand Down
Loading
Loading