diff --git a/common/lib/share/geometry-mesh-lib.c b/common/lib/share/geometry-mesh-lib.c index b368b61a43..de42b83922 100644 --- a/common/lib/share/geometry-mesh-lib.c +++ b/common/lib/share/geometry-mesh-lib.c @@ -1722,6 +1722,20 @@ gmesh_load (char* filename, double scale, double xwidth, double yheight, double } else if (edge->dir != 0) { mesh->oriented = 0; } + // folded: the two triangles of the edge turn back onto each other, i.e. + // their normals are antiparallel once oriented alike (dir 0) + if (edge->n == 2) { + double turn = coords_sp (mesh->tri_normal[edge->t0], mesh->tri_normal[edge->t1]); + if (edge->dir != 0) + turn = -turn; + if (turn < -1 + GMESH_EPSILON) { + if (!mesh->nfolded) { + mesh->folded_face[0] = mesh->tri_face[edge->t0]; + mesh->folded_face[1] = mesh->tri_face[edge->t1]; + } + mesh->nfolded++; + } + } } if (mesh->nnonmanifold) mesh->oriented = 0; @@ -1733,7 +1747,7 @@ gmesh_load (char* filename, double scale, double xwidth, double yheight, double } } mesh->euler = (int)(nused - mesh->nedges + mesh->ntris); - mesh->closed = (mesh->nboundary == 0 && mesh->nnonmanifold == 0); + mesh->closed = (mesh->nboundary == 0 && mesh->nnonmanifold == 0 && mesh->nfolded == 0); free (queue); free (visited); free (used); @@ -1946,7 +1960,7 @@ gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, double t_min, *ambiguous = 1; continue; } - hit.t = hit_t; + hit.time = hit_t; hit.normal = mesh->tri_normal[t]; hit.tri = t; hit.face = mesh->tri_face[t]; @@ -1954,7 +1968,7 @@ gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, double t_min, hit.edge = on_edge; // hits on shared edges/vertices: keep once for (k = 0; k < *stored_count; k++) { - if (hits[k].in_out == in_out && fabs (hits[k].t - hit_t) <= t_tolerance) { + if (hits[k].in_out == in_out && fabs (hits[k].time - hit_t) <= t_tolerance) { duplicate = 1; break; } @@ -1966,13 +1980,13 @@ gmesh_trace (gmesh_struct* mesh, Coords origin, Coords direction, double t_min, if (position == max_hits) { if (max_hits == 0) continue; - if (hit_t >= hits[max_hits - 1].t) + if (hit_t >= hits[max_hits - 1].time) continue; position = max_hits - 1; } else { (*stored_count)++; } - while (position > 0 && hits[position - 1].t > hit_t) { + while (position > 0 && hits[position - 1].time > hit_t) { hits[position] = hits[position - 1]; position--; } @@ -2006,6 +2020,274 @@ gmesh_intersect (gmesh_struct* mesh, Coords position, Coords direction, double t return stored; } + // ========================================================================== + // Ray tracing of parabolas (e.g. under gravity) + // ========================================================================== + + // real roots of quadratic*t^2 + linear*t + constant_term = 0 in roots[0..1], + // sorted, solving the linear equation when quadratic is 0. Returns their number. + #pragma acc routine seq +static int +gmesh_quadratic_roots (double quadratic, double linear, double constant_term, double roots[2]) { + double discriminant; + double q; + if (quadratic == 0) { + if (linear == 0) + return 0; + roots[0] = -constant_term / linear; + return 1; + } + discriminant = linear * linear - 4 * quadratic * constant_term; + if (discriminant < 0) + return 0; + // q avoids the cancellation of -linear + sqrt(discriminant) + q = -0.5 * (linear + copysign (sqrt (discriminant), linear)); + if (q == 0) { + roots[0] = 0; + return 1; + } + roots[0] = q / quadratic; + roots[1] = constant_term / q; + if (roots[0] > roots[1]) { + double swap = roots[0]; + roots[0] = roots[1]; + roots[1] = swap; + } + return 2; +} + + // box test of the parabola origin + t*velocity + t^2/2*acceleration, for t in + // [t_start, t_end]. Each axis cuts the time range into pieces at the times + // the coordinate equals a box border; the range is narrowed to the hull of the + // pieces within the box. Returns 0 only when the parabola misses the box; it + // may return 1 for a box it misses, which only costs extra triangle tests. + #pragma acc routine seq +static int +gmesh_slab_gravity (Coords lower, Coords upper, Coords origin, Coords velocity, Coords acceleration, double t_start, double t_end) { + double origin_xyz[3] = { origin.x, origin.y, origin.z }; + double velocity_xyz[3] = { velocity.x, velocity.y, velocity.z }; + double acceleration_xyz[3] = { acceleration.x, acceleration.y, acceleration.z }; + double lower_xyz[3] = { lower.x, lower.y, lower.z }; + double upper_xyz[3] = { upper.x, upper.y, upper.z }; + int axis; + for (axis = 0; axis < 3; axis++) { + double low = lower_xyz[axis]; + double high = upper_xyz[axis]; + double pad = 1e-9 * (high - low) + 1e-12 * (fabs (low) + fabs (high)) + 1e-300; + double half_a = 0.5 * acceleration_xyz[axis]; + double v = velocity_xyz[axis]; + double o = origin_xyz[axis]; + double borders[2]; + double cuts[6]; + double roots[2]; + double inside_start = FLT_MAX; + double inside_end = -FLT_MAX; + int ncuts = 0; + int i_border; + int i_cut; + borders[0] = low - pad; + borders[1] = high + pad; + cuts[ncuts] = t_start; + ncuts++; + for (i_border = 0; i_border < 2; i_border++) { + int nroots = gmesh_quadratic_roots (half_a, v, o - borders[i_border], roots); + int i_root; + for (i_root = 0; i_root < nroots; i_root++) { + if (roots[i_root] > t_start && roots[i_root] < t_end) { + cuts[ncuts] = roots[i_root]; + ncuts++; + } + } + } + cuts[ncuts] = t_end; + ncuts++; + // insertion sort of the few cut times + for (i_cut = 1; i_cut < ncuts; i_cut++) { + double cut = cuts[i_cut]; + int position = i_cut; + while (position > 0 && cuts[position - 1] > cut) { + cuts[position] = cuts[position - 1]; + position--; + } + cuts[position] = cut; + } + // the coordinate stays on one side of each border within a piece, so its + // value in the middle tells whether the whole piece is within the box. + // That value is compared with a rounding margin, as a piece may be only a + // few ulp long, e.g. for a flat box. + for (i_cut = 0; i_cut + 1 < ncuts; i_cut++) { + double middle = 0.5 * cuts[i_cut] + 0.5 * cuts[i_cut + 1]; + double coordinate = o + (v + half_a * middle) * middle; + double margin = 1e-12 * (fabs (o) + fabs (v * middle) + fabs (half_a * middle * middle)); + if (coordinate >= borders[0] - margin && coordinate <= borders[1] + margin) { + if (cuts[i_cut] < inside_start) + inside_start = cuts[i_cut]; + if (cuts[i_cut + 1] > inside_end) + inside_end = cuts[i_cut + 1]; + } + } + if (inside_start > inside_end) + return 0; + t_start = inside_start; + t_end = inside_end; + } + return 1; +} + + // intersections of the parabola origin + t*velocity + t^2/2*acceleration with + // a triangle of mesh: the times it crosses the plane of the triangle, kept + // when the crossing point is within the triangle (same tolerance as + // gmesh_tri_hit). Returns their number (0 to 2), with the times in hit_t and + // on_edge = 1 for a crossing close to an edge. + #pragma acc routine seq +static int +gmesh_tri_hit_gravity (gmesh_struct* mesh, int triangle, Coords origin, Coords velocity, Coords acceleration, double hit_t[2], int on_edge[2]) { + Coords v0 = mesh->vtx[mesh->tri[3 * triangle]]; + Coords e1 = coords_sub (mesh->vtx[mesh->tri[3 * triangle + 1]], v0); + Coords e2 = coords_sub (mesh->vtx[mesh->tri[3 * triangle + 2]], v0); + Coords normal = mesh->tri_normal[triangle]; + double e1_e1 = coords_sp (e1, e1); + double e1_e2 = coords_sp (e1, e2); + double e2_e2 = coords_sp (e2, e2); + double denominator = e1_e1 * e2_e2 - e1_e2 * e1_e2; + double eps = GMESH_EPSILON; + double roots[2]; + int nroots; + int i_root; + int nhits = 0; + if (denominator <= 0) + return 0; + nroots = gmesh_quadratic_roots (0.5 * coords_sp (normal, acceleration), coords_sp (normal, velocity), coords_sp (normal, coords_sub (origin, v0)), roots); + for (i_root = 0; i_root < nroots; i_root++) { + double time = roots[i_root]; + Coords point = coords_add (origin, coords_add (coords_scale (velocity, time), coords_scale (acceleration, 0.5 * time * time))); + Coords relative = coords_sub (point, v0); + double r_e1 = coords_sp (relative, e1); + double r_e2 = coords_sp (relative, e2); + // barycentric coordinates along e1 (u) and e2 (v), as in gmesh_tri_hit + double u = (e2_e2 * r_e1 - e1_e2 * r_e2) / denominator; + double v = (e1_e1 * r_e2 - e1_e2 * r_e1) / denominator; + if (u < -eps || u > 1 + eps || v < -eps || u + v > 1 + eps) + continue; + hit_t[nhits] = time; + on_edge[nhits] = (u < eps || v < eps || u + v > 1 - eps); + nhits++; + } + return nhits; +} + + // BVH traversal for the parabola origin + t*velocity + t^2/2*acceleration, + // t > t_min. Stores the max_hits nearest hits in hits, sorted by t, with hits + // on shared edges and vertices kept once, as gmesh_trace does for a line. + // Returns the number of hits found; above *stored_count, hits were dropped. + #pragma acc routine seq +static int +gmesh_trace_gravity (gmesh_struct* mesh, Coords origin, Coords velocity, Coords acceleration, double t_min, gmesh_hit* hits, int max_hits, int* stored_count) { + int stack[GMESH_BVH_STACK + 2]; + int top = 0; + int total = 0; + // a parabola may start at rest: its speed over the mesh size is then about + // sqrt(|acceleration| * size) + double speed = coords_len (velocity) + sqrt (coords_len (acceleration) * mesh->length_scale); + double t_tolerance; + if (speed <= 0) + return 0; + if (!mesh->nnodes) + return 0; + t_tolerance = 1e-9 * mesh->length_scale / speed; + stack[top] = 0; + top++; + while (top > 0) { + gmesh_node* node; + int triangle; + top--; + node = &mesh->node[stack[top]]; + if (!gmesh_slab_gravity (node->lo, node->hi, origin, velocity, acceleration, t_min, FLT_MAX)) + continue; + if (node->count == 0) { + // cannot happen: depth limited at build + if (top + 2 > GMESH_BVH_STACK) + continue; + stack[top] = node->first; + top++; + stack[top] = node->first + 1; + top++; + continue; + } + for (triangle = node->first; triangle < node->first + node->count; triangle++) { + double crossing_t[2]; + int crossing_edge[2]; + int ncrossings = gmesh_tri_hit_gravity (mesh, triangle, origin, velocity, acceleration, crossing_t, crossing_edge); + int i_crossing; + for (i_crossing = 0; i_crossing < ncrossings; i_crossing++) { + double hit_t = crossing_t[i_crossing]; + Coords hit_velocity = coords_add (velocity, coords_scale (acceleration, hit_t)); + int in_out; + int k; + int position; + int duplicate = 0; + gmesh_hit hit; + if (hit_t <= t_min) + continue; + if (coords_sp (mesh->tri_normal[triangle], hit_velocity) < 0) { + in_out = 1; + } else { + in_out = -1; + } + hit.time = hit_t; + hit.normal = mesh->tri_normal[triangle]; + hit.tri = triangle; + hit.face = mesh->tri_face[triangle]; + hit.in_out = in_out; + hit.edge = crossing_edge[i_crossing]; + // hits on shared edges/vertices: keep once + for (k = 0; k < *stored_count; k++) { + if (hits[k].in_out == in_out && fabs (hits[k].time - hit_t) <= t_tolerance) { + duplicate = 1; + break; + } + } + if (duplicate) + continue; + total++; + position = *stored_count; + if (position == max_hits) { + if (hit_t >= hits[max_hits - 1].time) + continue; + position = max_hits - 1; + } else { + (*stored_count)++; + } + while (position > 0 && hits[position - 1].time > hit_t) { + hits[position] = hits[position - 1]; + position--; + } + hits[position] = hit; + } + } + } + return total; +} + + // intersections of a parabola with mesh, see geometry-mesh-lib.h + #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 stored = 0; + int total = gmesh_trace_gravity (mesh, position, velocity, acceleration, t_min, hits, GMESH_MAX_HITS, &stored); + if (total > stored && !gmesh_overflow_warned) { + gmesh_overflow_warned = 1; + fprintf (stderr, + "Warning: %s: a parabola crossed the mesh %d times, but only the %d nearest hits are kept " + "(geometry-mesh-lib).\n" + " To keep more, add this line after the DEFINE INSTRUMENT line of your .instr file:\n" + " DEPENDENCY \" -DGMESH_MAX_HITS=%d \"\n" + " This warning is shown once.\n", + mesh->filename, total, GMESH_MAX_HITS, total); + } + return stored; +} + // 1 when point is inside the box [lower, upper], borders included #pragma acc routine seq static int @@ -2041,6 +2323,50 @@ gmesh_inside (gmesh_struct* mesh, Coords position) { return votes >= 2; } + // first entry and exit of a line through mesh, see geometry-mesh-lib.h + #pragma acc routine seq +int +gmesh_entry_exit (gmesh_struct* mesh, Coords position, Coords velocity, double* t_in, double* t_out, gmesh_hit* first) { + gmesh_hit hits[GMESH_MAX_HITS]; + int nhits = gmesh_intersect (mesh, position, velocity, 0, hits); + int inside = 0; + if (!nhits) + return 0; + // normals of a closed, oriented mesh point outward: leaving first means inside + if (mesh->closed && mesh->oriented) + inside = hits[0].in_out < 0; + else if (mesh->closed) + inside = gmesh_inside (mesh, position); + if (first) + *first = hits[0]; + if (inside) { + *t_in = 0; + *t_out = hits[0].time + 1e-13; + return 1; + } + *t_in = hits[0].time + 1e-13; + *t_out = (nhits > 1 ? hits[1].time : hits[0].time) + 1e-13; + return 2; +} + + // closed check, see geometry-mesh-lib.h +int +gmesh_check_closed (gmesh_struct* mesh) { + if (mesh->nboundary || mesh->nnonmanifold) { + fprintf (stderr, "Error: %s: %d boundary and %d non-manifold edges: the mesh is not closed (geometry-mesh-lib)\n", mesh->filename, mesh->nboundary, + mesh->nnonmanifold); + return 0; + } + if (mesh->nfolded) { + fprintf (stderr, + "Error: %s: %d edges where the surface folds back onto itself, e.g. between faces %d and %d (0-based): " + "the mesh overlaps itself, so inside and outside are not defined (geometry-mesh-lib)\n", + mesh->filename, mesh->nfolded, mesh->folded_face[0], mesh->folded_face[1]); + return 0; + } + return 1; +} + // per-face property, see geometry-mesh-lib.h #pragma acc routine seq double @@ -2072,9 +2398,14 @@ gmesh_print_info (gmesh_struct* mesh) { printf (", volume %g", mesh->volume); printf ("\n Topology: %d edges, Euler characteristic %d, %s, %s%s\n", mesh->nedges, mesh->euler, mesh->closed ? "closed" : "OPEN", mesh->oriented ? "consistently oriented" : "NOT consistently oriented", mesh->flipped ? " (orientation reversed to outward normals)" : ""); - if (!mesh->closed) { + if (mesh->nboundary || mesh->nnonmanifold) { printf (" Warning: %d boundary and %d non-manifold edges: the mesh does not enclose a volume.\n", mesh->nboundary, mesh->nnonmanifold); } + if (mesh->nfolded) { + printf (" Warning: %d edges where the surface folds back onto itself, e.g. between faces %d and %d (0-based).\n", mesh->nfolded, mesh->folded_face[0], + mesh->folded_face[1]); + } + if (mesh->nfaceprops) printf (" Per-face properties: %d\n", mesh->nfaceprops); } diff --git a/common/lib/share/geometry-mesh-lib.h b/common/lib/share/geometry-mesh-lib.h index 6ad3a11f08..3e0b87a1c1 100644 --- a/common/lib/share/geometry-mesh-lib.h +++ b/common/lib/share/geometry-mesh-lib.h @@ -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. * @@ -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 @@ -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; @@ -166,6 +172,18 @@ 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. @@ -173,6 +191,27 @@ int gmesh_intersect (gmesh_struct* mesh, Coords position, Coords direction, doub #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 diff --git a/mcstas-comps/contrib/Lens.comp b/mcstas-comps/contrib/Lens.comp index 2ecbce3c3f..7c19524f9c 100644 --- a/mcstas-comps/contrib/Lens.comp +++ b/mcstas-comps/contrib/Lens.comp @@ -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 @@ -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) @@ -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) @@ -219,7 +219,7 @@ DECLARE double my_a_v; double mean_n; double events; - off_struct offdata; + gmesh_struct mesh; %} INITIALIZE @@ -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 } @@ -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 */ @@ -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) { @@ -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) @@ -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) @@ -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)); diff --git a/mcstas-comps/contrib/Single_crystal_inelastic.comp b/mcstas-comps/contrib/Single_crystal_inelastic.comp index 7ff2e0813d..6b1487e118 100644 --- a/mcstas-comps/contrib/Single_crystal_inelastic.comp +++ b/mcstas-comps/contrib/Single_crystal_inelastic.comp @@ -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. @@ -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 @@ -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). @@ -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 */ @@ -663,7 +665,7 @@ SHARE DECLARE %{ struct hkl_info_struct hkl_info; - off_struct offdata; + gmesh_struct mesh; %} INITIALIZE @@ -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 */ @@ -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 */ @@ -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) @@ -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 diff --git a/mcstas-comps/data/socket.off b/mcstas-comps/data/socket.off index 512d5ee977..708ea91958 100644 --- a/mcstas-comps/data/socket.off +++ b/mcstas-comps/data/socket.off @@ -1,6 +1,6 @@ OFF # This is an Object File Format (geomview) to describe a mechanical socket -327 678 0 +327 670 0 -100.000000 0.000000 -25.000000 -100.000000 0.000000 25.000000 -99.999170 0.407363 -0.064789 @@ -487,8 +487,92 @@ OFF 3 12 8 6 3 8 5 2 3 6 2 4 -3 1 6 4 -3 5 0 3 +3 12 6 4 +3 12 4 10 +3 12 10 16 +3 12 16 28 +3 28 44 50 +3 28 50 60 +3 28 60 62 +3 28 62 42 +3 28 42 37 +3 28 37 32 +3 28 32 23 +3 28 23 20 +3 12 28 20 +3 12 20 25 +3 12 25 34 +3 12 34 39 +3 39 42 62 +3 12 39 62 +3 12 62 75 +3 12 75 46 +3 12 46 30 +3 30 18 12 +3 315 321 323 +3 315 323 317 +3 315 317 311 +3 315 311 299 +3 299 283 277 +3 299 277 269 +3 299 269 263 +3 299 263 285 +3 299 285 290 +3 299 290 295 +3 299 295 304 +3 299 304 307 +3 315 299 307 +3 315 307 302 +3 315 302 293 +3 315 293 288 +3 288 285 263 +3 315 288 263 +3 315 263 253 +3 315 253 251 +3 315 251 281 +3 315 281 297 +3 297 309 315 +3 9 3 5 +3 9 5 11 +3 9 11 17 +3 9 17 29 +3 29 45 74 +3 29 74 61 +3 29 61 41 +3 29 41 38 +3 29 38 33 +3 29 33 24 +3 29 24 19 +3 9 29 19 +3 9 19 22 +3 9 22 31 +3 9 31 36 +3 36 41 61 +3 9 36 61 +3 9 61 49 +3 9 49 43 +3 9 43 27 +3 27 15 9 +3 316 322 303 +3 316 303 294 +3 316 294 289 +3 306 303 322 +3 301 306 322 +3 301 322 320 +3 292 301 320 +3 292 320 314 +3 287 292 314 +3 287 314 308 +3 287 308 296 +3 284 287 296 +3 284 296 280 +3 284 280 250 +3 284 250 276 +3 289 284 276 +3 316 289 276 +3 316 276 282 +3 316 282 298 +3 298 310 316 3 2 3 7 3 4 7 10 3 7 9 13 @@ -507,7 +591,6 @@ OFF 3 279 250 280 3 318 320 324 3 321 324 323 -3 326 321 323 3 318 315 312 3 314 312 308 3 314 318 312 @@ -523,7 +606,6 @@ OFF 3 297 300 309 3 300 308 312 3 309 312 315 -3 320 325 322 3 324 322 319 3 323 319 317 3 319 316 313 @@ -830,29 +912,6 @@ OFF 3 206 264 278 3 278 213 206 3 231 230 264 -3 25 75 20 -3 25 34 75 -3 34 39 75 -3 75 39 62 -3 62 39 42 -3 62 42 37 -3 60 37 32 -3 10 23 4 -3 10 16 23 -3 23 16 28 -3 23 60 32 -3 60 23 28 -3 62 37 60 -3 23 20 4 -3 4 20 1 -3 1 20 75 -3 6 75 12 -3 6 1 75 -3 46 30 75 -3 75 30 18 -3 12 75 18 -3 28 44 60 -3 60 44 50 3 54 51 56 3 54 52 51 3 51 52 53 @@ -879,79 +938,12 @@ OFF 3 62 60 57 3 64 76 69 3 62 57 59 -3 22 9 19 -3 22 31 9 -3 15 9 27 -3 27 9 43 -3 41 61 36 -3 41 38 61 -3 61 38 29 -3 45 74 29 -3 38 33 29 -3 33 24 29 -3 29 24 11 -3 11 17 29 -3 24 19 11 -3 11 19 0 -3 5 11 0 -3 74 61 29 -3 36 61 9 -3 43 9 49 -3 9 3 0 -3 19 9 0 -3 31 36 9 -3 49 9 61 3 61 74 73 3 61 73 48 3 48 49 61 -3 287 250 284 -3 287 292 250 -3 325 296 280 -3 250 325 280 -3 314 325 320 -3 314 308 325 -3 325 308 296 -3 306 325 301 -3 306 303 325 -3 310 325 298 -3 310 316 325 -3 303 294 325 -3 325 294 282 -3 276 294 289 -3 276 282 294 -3 276 289 284 -3 250 276 284 -3 301 325 292 -3 250 292 325 -3 282 298 325 -3 325 316 322 3 265 275 276 3 265 276 250 3 265 250 249 -3 290 263 285 -3 290 295 263 -3 323 299 283 -3 269 283 277 -3 269 323 283 -3 295 304 323 -3 317 311 323 -3 323 311 299 -3 304 307 323 -3 323 307 326 -3 326 307 302 -3 321 302 293 -3 315 293 288 -3 253 288 263 -3 253 281 288 -3 297 288 281 -3 297 309 288 -3 288 309 315 -3 288 285 263 -3 251 281 253 -3 315 321 293 -3 321 326 302 -3 263 295 323 -3 269 263 323 3 273 278 271 3 273 274 278 3 277 278 274 diff --git a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr index 7de2f08790..f3e6b5ddac 100644 --- a/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr +++ b/mcstas-comps/examples/libtest/Test_lib_geometry_mesh_lib/Test_lib_geometry_mesh_lib.instr @@ -6,6 +6,20 @@ * * Smoke test: builds and runs with the library geometry-mesh-lib in the instrument. * Fails to compile or link if the library does not build with the runtime. +* INITIALIZE also checks gmesh_init_fit, gmesh_inside, gmesh_entry_exit and +* gmesh_intersect_gravity, and exits with an error when a check fails: +* - gmesh_init_fit resizes cube.off about the origin with center=0, and about +* its box centre, moved to the origin, with center=1 +* - gmesh_init counts the folded edges of a folded cube, which is then not +* closed, and gmesh_check_closed fails it and the open Guide.off, and passes +* cube.off and socket.off +* - gmesh_inside on cube.off +* - gmesh_entry_exit on cube.off, and its inside/outside from the first hit +* against gmesh_inside for random points and directions around socket.off +* - with zero acceleration it gives the hits of gmesh_intersect, to rounding +* - parabolas through cube.off give the analytic hit times and in_out +* - random parabolas through socket.off give the hits of a fine polyline of +* straight gmesh_intersect pieces * * %Test: Ncount=10 Detector: PSD_I=10 * @@ -16,11 +30,222 @@ DEFINE INSTRUMENT Test_lib_geometry_mesh_lib(Ncount=10) DECLARE %{ %include "geometry-mesh-lib" + +// counts a failed check and prints what failed +void mesh_check (int ok, const char* what, int* failures) { + if (!ok) { + fprintf (stderr, "Test_lib_geometry_mesh_lib: FAILED: %s\n", what); + (*failures)++; + } +} + +// point of the parabola position + time*velocity + time^2/2*acceleration +Coords gravity_parabola (Coords position, Coords velocity, Coords acceleration, double time) { + return coords_add (position, coords_add (coords_scale (velocity, time), coords_scale (acceleration, 0.5 * time * time))); +} + +// uniform number in [low, high] from a fixed sequence, independent of the seed +double gravity_uniform (unsigned long* state, double low, double high) { + *state = (*state * 6364136223846793005UL + 1442695040888963407UL) & 0xFFFFFFFFFFFFFFFFUL; + return low + (high - low) * (double)(*state >> 11) / 9007199254740992.0; +} + +// checks of gmesh_init_fit, gmesh_inside and gmesh_entry_exit; returns the +// number of failed checks +int inside_tests (void) { + gmesh_struct cube; + gmesh_struct socket; + unsigned long state = 4321; + int failures = 0; + int differ = 0; + int i_point; + if (!gmesh_init ("cube.off", 1, GMESH_PROPS_IGNORE, &cube) || !gmesh_init_fit ("socket.off", 0.1, 0, 0, 1, GMESH_PROPS_IGNORE, &socket)) { + fprintf (stderr, "Test_lib_geometry_mesh_lib: FAILED: can not read cube.off or socket.off\n"); + return 1; + } + mesh_check (gmesh_inside (&cube, coords_set (0.5, 0.5, 0.5)) == 1, "inside the cube", &failures); + // the unit cube [0, 1]^3 resized to a side of 2 + { + gmesh_struct fit; + mesh_check (gmesh_init_fit ("cube.off", 2, 0, 0, 0, GMESH_PROPS_IGNORE, &fit) && fabs (fit.bbmin.x) < 1e-12 && fabs (fit.bbmax.y - 2) < 1e-12 + && fabs (fit.bbmax.z - 2) < 1e-12, + "gmesh_init_fit with center=0 scales about the origin", &failures); + gmesh_free (&fit); + mesh_check (gmesh_init_fit ("cube.off", 2, 0, 0, 1, GMESH_PROPS_IGNORE, &fit) && fabs (fit.bbmin.x + 1) < 1e-12 && fabs (fit.bbmax.y - 1) < 1e-12 + && fabs (fit.bbmax.z - 1) < 1e-12, + "gmesh_init_fit with center=1 moves the box centre to the origin", &failures); + gmesh_free (&fit); + } + // a folded mesh: a unit cube whose bottom reaches past the edge x = 1 to + // (1.5, 0.5, 0), where triangle 2 1 8 folds back. It has no boundary edges + // and is consistently oriented, but overlaps itself at 2 edges. + { + gmesh_struct folded; + // one file per MPI process, as all processes run these checks + char folded_file[64] = "Test_lib_geometry_mesh_lib_folded.off"; + FILE* file; + #ifdef USE_MPI + snprintf (folded_file, sizeof (folded_file), "Test_lib_geometry_mesh_lib_folded_%d.off", mpi_node_rank); + #endif + file = fopen (folded_file, "w"); + if (file) { + fprintf (file, "OFF\n9 9 0\n0 0 0\n1 0 0\n1 1 0\n0 1 0\n0 0 1\n1 0 1\n1 1 1\n0 1 1\n1.5 0.5 0\n" + "4 4 5 6 7\n4 0 1 5 4\n4 1 2 6 5\n4 2 3 7 6\n4 0 4 7 3\n3 1 0 8\n3 0 3 8\n3 3 2 8\n3 2 1 8\n"); + fclose (file); + mesh_check (gmesh_init (folded_file, 1, GMESH_PROPS_IGNORE, &folded) && !folded.nboundary && !folded.nnonmanifold && folded.oriented + && folded.nfolded == 2 && !folded.closed && !gmesh_check_closed (&folded), + "folded mesh: 2 folded edges, not closed, fails gmesh_check_closed", &failures); + gmesh_free (&folded); + remove (folded_file); + } + } + mesh_check (gmesh_inside (&cube, coords_set (0.5, 0.5, 1.5)) == 0, "outside the cube", &failures); + // entry and exit, 1e-13 s past the surfaces + { + double t_in = -1; + double t_out = -1; + gmesh_hit first; + mesh_check (gmesh_entry_exit (&cube, coords_set (0.5, 0.5, -1), coords_set (0, 0, 1), &t_in, &t_out, &first) == 2 && fabs (t_in - 1 - 1e-13) < 1e-15 + && fabs (t_out - 2 - 1e-13) < 1e-15 && first.in_out == 1, + "gmesh_entry_exit from outside cube.off", &failures); + mesh_check (gmesh_entry_exit (&cube, coords_set (0.5, 0.5, 0.5), coords_set (0, 0, 1), &t_in, &t_out, NULL) == 1 && t_in == 0 + && fabs (t_out - 0.5 - 1e-13) < 1e-15, + "gmesh_entry_exit from inside cube.off", &failures); + mesh_check (gmesh_entry_exit (&cube, coords_set (0.5, 0.5, 2), coords_set (0, 0, 1), &t_in, &t_out, NULL) == 0, "gmesh_entry_exit past cube.off", + &failures); + } + // inside from the first hit against the parity of ray crossings + for (i_point = 0; i_point < 2000; i_point++) { + Coords point = coords_set (gravity_uniform (&state, socket.bbmin.x, socket.bbmax.x), gravity_uniform (&state, socket.bbmin.y, socket.bbmax.y), + gravity_uniform (&state, socket.bbmin.z, socket.bbmax.z)); + Coords direction = coords_set (gravity_uniform (&state, -1, 1), gravity_uniform (&state, -1, 1), gravity_uniform (&state, -1, 1)); + double t_in; + double t_out; + int side = gmesh_entry_exit (&socket, point, direction, &t_in, &t_out, NULL); + if (side && (side == 1) != gmesh_inside (&socket, point)) + differ++; + } + printf ("Test_lib_geometry_mesh_lib: socket.off: gmesh_entry_exit and gmesh_inside differ for %d of 2000 points\n", differ); + mesh_check (socket.closed && socket.oriented && differ == 0, "gmesh_entry_exit against gmesh_inside on socket.off", &failures); + mesh_check (gmesh_check_closed (&cube) && gmesh_check_closed (&socket), "gmesh_check_closed on cube.off and socket.off", &failures); + { + gmesh_struct guide; + mesh_check (gmesh_init ("Guide.off", 1, GMESH_PROPS_IGNORE, &guide) && !guide.closed && !gmesh_check_closed (&guide), "gmesh_check_closed on the open Guide.off", + &failures); + gmesh_free (&guide); + } + gmesh_free (&cube); + gmesh_free (&socket); + return failures; +} + +// checks of gmesh_intersect_gravity; returns the number of failed checks +int gravity_tests (void) { + gmesh_struct cube; + gmesh_struct socket; + gmesh_hit hits[GMESH_MAX_HITS]; + gmesh_hit line_hits[GMESH_MAX_HITS]; + unsigned long state = 1234; + int failures = 0; + int nhits; + int i_ray; + int mismatches = 0; + double worst = 0; + + if (!gmesh_init ("cube.off", 1, GMESH_PROPS_IGNORE, &cube) || !gmesh_init_fit ("socket.off", 0.1, 0, 0, 1, GMESH_PROPS_IGNORE, &socket)) { + fprintf (stderr, "Test_lib_geometry_mesh_lib: FAILED: can not read cube.off or socket.off\n"); + return 1; + } + + // falling from rest above the unit cube: top at time sqrt(2), bottom at 2 + nhits = gmesh_intersect_gravity (&cube, coords_set (0.5, 2, 0.5), coords_set (0, 0, 0), coords_set (0, -1, 0), 0, hits); + mesh_check (nhits == 2 && fabs (hits[0].time - sqrt (2)) < 1e-12 && hits[0].in_out == 1 && fabs (hits[1].time - 2) < 1e-12 && hits[1].in_out == -1, + "fall from rest through cube.off", &failures); + + // entering through the front face, leaving through the bottom at time sqrt(2) + nhits = gmesh_intersect_gravity (&cube, coords_set (0.5, 0.5, -1), coords_set (0, 0, 1), coords_set (0, -0.5, 0), 0, hits); + mesh_check (nhits == 2 && fabs (hits[0].time - 1) < 1e-12 && hits[0].in_out == 1 && fabs (hits[1].time - sqrt (2)) < 1e-12 && hits[1].in_out == -1, + "front face in, bottom out of cube.off", &failures); + + // thrown up into the cube from below, falling out through the same face + nhits = gmesh_intersect_gravity (&cube, coords_set (0.5, -1, 0.5), coords_set (0, 2, 0), coords_set (0, -1.2, 0), 0, hits); + mesh_check (nhits == 2 && fabs (hits[0].time - (2 - sqrt (1.6)) / 1.2) < 1e-12 && hits[0].in_out == 1 + && fabs (hits[1].time - (2 + sqrt (1.6)) / 1.2) < 1e-12 && hits[1].in_out == -1 && hits[0].face == hits[1].face, + "in and out through the bottom of cube.off", &failures); + + // t_min skips the hits before it + nhits = gmesh_intersect_gravity (&cube, coords_set (0.5, -1, 0.5), coords_set (0, 2, 0), coords_set (0, -1.2, 0), 1, hits); + mesh_check (nhits == 1 && hits[0].in_out == -1, "t_min", &failures); + + // the arc passes above the cube + nhits = gmesh_intersect_gravity (&cube, coords_set (-1, 1.5, 0.5), coords_set (1, 0, 0), coords_set (0, -0.1, 0), 0, hits); + mesh_check (nhits == 0, "arc above cube.off", &failures); + + for (i_ray = 0; i_ray < 2000; i_ray++) { + Coords position = coords_set (gravity_uniform (&state, -0.1, 0.1), gravity_uniform (&state, -0.1, 0.1), gravity_uniform (&state, -0.1, 0.1)); + Coords velocity = coords_set (gravity_uniform (&state, -1, 1), gravity_uniform (&state, -1, 1), gravity_uniform (&state, -1, 1)); + Coords acceleration = coords_set (gravity_uniform (&state, -5, 5), gravity_uniform (&state, -5, 5), gravity_uniform (&state, -5, 5)); + double time_end = 0.2; + int nsteps = 4000; + double step = time_end / nsteps; + double line_times[GMESH_MAX_HITS]; + int nline = 0; + int nstraight; + int i_step; + int i_hit; + + // zero acceleration: the hits of the straight line, to rounding + nstraight = gmesh_intersect (&socket, position, velocity, -FLT_MAX, line_hits); + nhits = gmesh_intersect_gravity (&socket, position, velocity, coords_set (0, 0, 0), -FLT_MAX, hits); + if (nhits != nstraight) { + mismatches++; + continue; + } + for (i_hit = 0; i_hit < nhits; i_hit++) { + if (fabs (hits[i_hit].time - line_hits[i_hit].time) > 1e-12 * (1 + fabs (line_hits[i_hit].time)) || hits[i_hit].in_out != line_hits[i_hit].in_out) + mismatches++; + } + + // the parabola against a polyline of nsteps straight pieces up to time_end + for (i_step = 0; i_step < nsteps; i_step++) { + Coords start = gravity_parabola (position, velocity, acceleration, i_step * step); + Coords chord = coords_sub (gravity_parabola (position, velocity, acceleration, (i_step + 1) * step), start); + int npiece = gmesh_intersect (&socket, start, chord, 0, line_hits); + int i_piece; + for (i_piece = 0; i_piece < npiece && line_hits[i_piece].time <= 1 && nline < GMESH_MAX_HITS; i_piece++) { + line_times[nline] = (i_step + line_hits[i_piece].time) * step; + nline++; + } + } + nhits = gmesh_intersect_gravity (&socket, position, velocity, acceleration, 0, hits); + while (nhits > 0 && hits[nhits - 1].time > time_end) + nhits--; + if (nhits != nline) { + mismatches++; + continue; + } + for (i_hit = 0; i_hit < nhits; i_hit++) { + if (fabs (hits[i_hit].time - line_times[i_hit]) > worst) + worst = fabs (hits[i_hit].time - line_times[i_hit]); + } + } + printf ("Test_lib_geometry_mesh_lib: socket.off: %d of 2000 random rays differ from the line or polyline, worst time difference %g s\n", mismatches, + worst); + mesh_check (mismatches == 0 && worst < 1e-6, "random parabolas through socket.off", &failures); + + gmesh_free (&cube); + gmesh_free (&socket); + return failures; +} %} INITIALIZE %{ mcset_ncount(Ncount); + if (inside_tests () + gravity_tests ()) { + fprintf (stderr, "Test_lib_geometry_mesh_lib: geometry-mesh-lib checks failed\n"); + exit (-1); + } %} TRACE diff --git a/mcstas-comps/misc/Shape.comp b/mcstas-comps/misc/Shape.comp index 505b4e2170..ade46a17d7 100644 --- a/mcstas-comps/misc/Shape.comp +++ b/mcstas-comps/misc/Shape.comp @@ -25,12 +25,12 @@ * cylinder: radius x yheight (thickness=0) * hollow cylinder: radius x yheight and thickness>0 * sphere: radius (yheight=0 thickness=0) -* any shape: geometry=OFF file +* any shape: geometry=OFF, PLY or STL file * * 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 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/powercrust, and * viewed with geomview * The default size of the object depends of the OFF file data, but its @@ -46,8 +46,8 @@ * yheight: [m] Vert. dimension of sample (bounding box if off file), as a height. A sphere shape is used when 0 and radius is set * zdepth: [m] Depth of sample (bounding box if off file) * thickness: [m] Thickness of hollow box or cylinder (spheres are drawn solid) -* geometry: [str] Name of an Object File Format (OFF) file for complex geometry. The OFF file may be generated from XYZ coordinates using qhull/powercrust -* center: [1] Flag to determine if OFF object is centered on its centre of mass. +* geometry: [str] Name of an OFF, PLY or STL file for complex geometry. The OFF file may be generated from XYZ coordinates using qhull/powercrust +* center: [1] Flag to centre the bounding box of the mesh on the origin. * nx: [1] x-comp of orientation vector * ny: [1] y-comp of orientation vector * nz: [1] z-comp of orientation vector @@ -69,19 +69,22 @@ SETTING PARAMETERS (string geometry=0, radius=0, xwidth=0, yheight=0, zdepth=0, SHARE %{ %include "read_table-lib" -%include "interoff-lib" +%include "geometry-mesh-lib" %} DECLARE %{ - off_struct offdata; + gmesh_struct mesh; %} INITIALIZE %{ if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - if (off_init (geometry, xwidth, yheight, zdepth, !center, &offdata)) { + if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, center, GMESH_PROPS_IGNORE, &mesh)) thickness = 0; + else { + fprintf (stderr, "Shape: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry); + exit (-1); } } %} @@ -93,8 +96,8 @@ TRACE %{ MCDISPLAY %{ - if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { /* OFF file */ - off_display (offdata); + if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { // mesh file + gmesh_display (&mesh); } else if (radius > 0 && yheight) { /* cylinder along y*/ cylinder (0, 0, 0, radius, yheight, thickness, nx, ny, nz); } else if (xwidth && yheight) { /* box/rectangle */ diff --git a/mcstas-comps/misc/Shape_simple.comp b/mcstas-comps/misc/Shape_simple.comp index b216942726..c2ac79fc25 100644 --- a/mcstas-comps/misc/Shape_simple.comp +++ b/mcstas-comps/misc/Shape_simple.comp @@ -26,12 +26,12 @@ * hollow cylinder: radius x yheight and thickness>0 * sphere: radius (yheight=0 thickness=0) * hollow sphere: radius and thickness>0 (yheight=0) -* any shape: geometry=OFF file +* any shape: geometry=OFF, PLY or STL file * * 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 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/powercrust, and * viewed with geomview * The default size of the object depends of the OFF file data, but its @@ -47,8 +47,8 @@ * yheight: [m] Vert. dimension of sample (bounding box if off file), as a height. A sphere shape is used when 0 and radius is set * zdepth: [m] Depth of sample (bounding box if off file) * thickness: [m] Thickness of hollow sample -* geometry: [str] Name of an Object File Format (OFF) file for complex geometry. The OFF file may be generated from XYZ coordinates using qhull/powercrust -* center: [1] Flag to determine if OFF object is centered on its centre of mass. +* geometry: [str] Name of an OFF, PLY or STL file for complex geometry. The OFF file may be generated from XYZ coordinates using qhull/powercrust +* center: [1] Flag to centre the bounding box of the mesh on the origin. * * %L * Geomview and Object File Format (OFF) @@ -67,19 +67,22 @@ SETTING PARAMETERS (string geometry=0, radius=0, xwidth=0, yheight=0, zdepth=0, SHARE %{ %include "read_table-lib" -%include "interoff-lib" +%include "geometry-mesh-lib" %} DECLARE %{ - off_struct offdata; + gmesh_struct mesh; %} INITIALIZE %{ if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - if (off_init (geometry, xwidth, yheight, zdepth, !center, &offdata)) { + if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, center, GMESH_PROPS_IGNORE, &mesh)) thickness = 0; + else { + fprintf (stderr, "Shape_simple: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry); + exit (-1); } } %} @@ -91,8 +94,8 @@ TRACE %{ MCDISPLAY %{ - if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { /* OFF file */ - off_display (offdata); + if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { // mesh file + gmesh_display (&mesh); } else if (radius > 0 && yheight) { /* cylinder */ cylinder (0, 0, 0, radius, yheight, thickness, 0, 1, 0); } else if (xwidth && yheight) { /* box/rectangle XY */ diff --git a/mcstas-comps/monitors/Monitor_nD.comp b/mcstas-comps/monitors/Monitor_nD.comp index 2c0f212e3f..d635499bab 100644 --- a/mcstas-comps/monitors/Monitor_nD.comp +++ b/mcstas-comps/monitors/Monitor_nD.comp @@ -521,11 +521,11 @@ TRACE #ifdef USE_OFF if (geometry && strlen (geometry) && strcmp (geometry, "0") && strcmp (geometry, "NULL")) { /* determine intersections with object */ - int face = 0; - intersect = Monitor_nD_MeshIntersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &face); + gmesh_hit first; + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &first); if (Vars.Flag_mantid) { if (intersect) { - Vars.OFF_polyidx = face; + Vars.OFF_polyidx = first.face; } else { Vars.OFF_polyidx = -1; } diff --git a/mcstas-comps/monitors/Monitor_nD_noacc.comp b/mcstas-comps/monitors/Monitor_nD_noacc.comp index 157eac7c1d..fd9fa13076 100644 --- a/mcstas-comps/monitors/Monitor_nD_noacc.comp +++ b/mcstas-comps/monitors/Monitor_nD_noacc.comp @@ -516,11 +516,11 @@ TRACE */ if (geometry && strlen (geometry) && strcmp (geometry, "0") && strcmp (geometry, "NULL")) { /* determine intersections with object */ - int face = 0; - intersect = Monitor_nD_MeshIntersect (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &face); + gmesh_hit first; + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &first); if (Vars.Flag_mantid) { if (intersect) { - Vars.OFF_polyidx = face; + Vars.OFF_polyidx = first.face; } else { Vars.OFF_polyidx = -1; } diff --git a/mcstas-comps/obsolete/Guide_anyshape_r.comp b/mcstas-comps/obsolete/Guide_anyshape_r.comp index ffcf7aeea3..b2775e6150 100644 --- a/mcstas-comps/obsolete/Guide_anyshape_r.comp +++ b/mcstas-comps/obsolete/Guide_anyshape_r.comp @@ -41,7 +41,7 @@ * values as input (common for all the faces), ignoring those specified in the OFF file * * The complex OFF/PLY geometry option handles any closed non-convex polyhedra. -* It supports the OFF and NOFF file format but not COFF (colored faces). +* It supports the OFF (incl. NOFF, COFF), PLY and STL file formats. * Standard .OFF files may be generated from XYZ data using: * qhull < coordinates.xyz Qx Qv Tv o > geomview.off * or @@ -54,7 +54,7 @@ * %P * INPUT PARAMETERS: * -* geometry: [str] Name of the OFF/PLY file describing the guide geometry +* geometry: [str] Name of the OFF, PLY or STL file describing the guide geometry * xwidth: [m] Redimension the object bounding box on X axis is non-zero * yheight: [m] Redimension the object bounding box on Y axis is non-zero * zdepth: [m] Redimension the object bounding box on Z axis is non-zero @@ -95,28 +95,28 @@ DEPENDENCY " -DUSE_OFF " SHARE %{ %include "read_table-lib" -%include "interoff-lib" +%include "geometry-mesh-lib" %include "ref-lib" %} DECLARE %{ - off_struct offdata; + gmesh_struct mesh; int use_file_coatings; // Flag to specify whether to use (1) or not (0) the mirror parameters given in the .OFF file %} INITIALIZE %{ MPI_MASTER (fprintf (stderr, "Guide_anyshape_r: %s: WARNING: this component is obsolete. Use Guide_anyshape(file_coatings=1) instead.\n", NAME_CURRENT_COMP);); - /* initialize OFF object from the file(s) */ - if (!off_init (geometry, xwidth, yheight, zdepth, !center, &offdata)) + // initialize the mesh from the file, with the per-face coatings if given + if (!gmesh_init_fit (geometry, xwidth, yheight, zdepth, center, GMESH_PROPS_OPTIONAL, &mesh)) exit (-1); if (m == 0 && alpha == 0 && W == 0) { use_file_coatings = 1; - if (offdata.nfaceprops < 3) { - fprintf (stderr, "Guide_anyshape_r: %s: Error: m, alpha, W are all 0, but %s does not define m, alpha and W values for each face.\n", - NAME_CURRENT_COMP, geometry); + if (mesh.nfaceprops < 3) { + fprintf (stderr, "Guide_anyshape_r: %s: Error: m, alpha, W are all 0, but %s does not define m, alpha and W values for each face.\n", NAME_CURRENT_COMP, + geometry); exit (-1); } printf ("Guide_anyshape_r: %s: All of m, alpha, W assigned 0: \n - We are using H. Jacobsen reflectivity model if m-values are given in %s\n", @@ -134,40 +134,28 @@ TRACE int counter = 0; /* main loop for multiple reflections */ #ifdef OPENACC - off_struct thread_offdata = offdata; + gmesh_struct thread_mesh = mesh; #else - #define thread_offdata offdata + #define thread_mesh mesh #endif do { - double t0 = 0, t3 = 0, dt = 0; - unsigned long faceindex0 = 0, faceindex3 = 0, fi = 0; - Coords n0, n3, n = { 0, 0, 0 }; - /* determine intersections with object; PL: get index of the intersected face */ - - double mc_gx = 0.0, mc_gy = 0.0, mc_gz = 0.0; - if (mcgravitation) { - Coords locgrav; - locgrav = rot_apply (_comp->_rotation_absolute, coords_set (0, -GRAVITY, 0)); - coords_get (locgrav, &mc_gx, &mc_gy, &mc_gz); - } - - intersect = off_intersect_idx (&t0, &t3, &n0, &n3, &faceindex0, &faceindex3, x, y, z, vx, vy, vz, mc_gx, mc_gy, mc_gz, thread_offdata); + double dt = 0; + int fi = 0; + Coords n = { 0, 0, 0 }; + gmesh_hit hits[GMESH_MAX_HITS]; + Coords acceleration = coords_set (0, 0, 0); + if (mcgravitation) + acceleration = rot_apply (_comp->_rotation_absolute, coords_set (0, -GRAVITY, 0)); - /* get the smallest positive */ - if (t0 > 0) { - dt = t0; - n = n0; - fi = faceindex0; - } - if (intersect > 1 && dt <= 0 && t3 > dt) { - dt = t3; - n = n3; - fi = faceindex3; - } + // next intersection with the object, along the parabola under gravity; PL: with the index of the face + intersect = gmesh_intersect_gravity (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), acceleration, 0, hits); /* exit loop when no intersection forward */ - if (dt <= 0 || !intersect) + if (!intersect) break; + dt = hits[0].time; + n = hits[0].normal; + fi = hits[0].face; double nx, ny, nz; coords_get (n, &nx, &ny, &nz); @@ -187,9 +175,10 @@ TRACE double W_value = W; if (use_file_coatings == 1) { - m_value = off_face_prop (&thread_offdata, fi, OFF_FACEPROP_M, 0); - alpha_value = off_face_prop (&thread_offdata, fi, OFF_FACEPROP_ALPHA, 0); - W_value = off_face_prop (&thread_offdata, fi, OFF_FACEPROP_W, 0); + // per-face properties m, alpha, W are numbers 0, 1, 2 + m_value = gmesh_face_prop (&thread_mesh, fi, 0, 0); + alpha_value = gmesh_face_prop (&thread_mesh, fi, 1, 0); + W_value = gmesh_face_prop (&thread_mesh, fi, 2, 0); } double par[] = { R0, Qc, alpha_value, m_value, W_value }; StdReflecFunc (q, par, &R); @@ -231,7 +220,7 @@ TRACE MCDISPLAY %{ /* display the object */ - off_display (offdata); + gmesh_display (&mesh); %} END diff --git a/mcstas-comps/optics/Guide_anyshape.comp b/mcstas-comps/optics/Guide_anyshape.comp index 543fadcdc7..64e627372f 100644 --- a/mcstas-comps/optics/Guide_anyshape.comp +++ b/mcstas-comps/optics/Guide_anyshape.comp @@ -41,7 +41,7 @@ * * The complex OFF/PLY geometry option handles any closed non-convex polyhedra, * with polygons of any number of vertices. -* It supports the OFF, NOFF and COFF file formats (colours are ignored, but +* It supports the OFF, NOFF, COFF, PLY and STL file formats (colours are ignored, but * face colours would be interpreted as coatings when file_coatings=1). * Such files may be generated from XYZ data using: * qhull < coordinates.xyz Qx Qv Tv o > geomview.off @@ -55,7 +55,7 @@ * %P * INPUT PARAMETERS: * -* geometry: [str] Name of the OFF/PLY file describing the guide geometry +* geometry: [str] Name of the OFF, PLY or STL file describing the guide geometry * xwidth: [m] Redimension the object bounding box on X axis is non-zero * yheight: [m] Redimension the object bounding box on Y axis is non-zero * zdepth: [m] Redimension the object bounding box on Z axis is non-zero @@ -99,21 +99,23 @@ DEPENDENCY " -DUSE_OFF " SHARE %{ %include "read_table-lib" -%include "interoff-lib" +%include "geometry-mesh-lib" %include "ref-lib" +// per-face coating properties in the geometry file: nv i1 ... inv m alpha W [R0 [Qc]] +enum { GUIDE_ANYSHAPE_M, GUIDE_ANYSHAPE_ALPHA, GUIDE_ANYSHAPE_W, GUIDE_ANYSHAPE_R0, GUIDE_ANYSHAPE_QC }; %} DECLARE %{ - off_struct offdata; + gmesh_struct mesh; t_Table pTable; int use_table; %} INITIALIZE %{ - /* initialize OFF object from the file(s) */ - if (!off_init (geometry, xwidth, yheight, zdepth, !center, &offdata)) + // initialize the mesh from the file, with the per-face coatings when used + if (!gmesh_init_fit (geometry, xwidth, yheight, zdepth, center, file_coatings ? GMESH_PROPS_OPTIONAL : GMESH_PROPS_IGNORE, &mesh)) exit (-1); /* optionally initialize reflectivity curves */ @@ -129,13 +131,15 @@ INITIALIZE if (file_coatings) { if (use_table) exit (fprintf (stderr, "Guide_anyshape: %s: file_coatings=1 and reflect=%s can not be used together. Aborting.\n", NAME_CURRENT_COMP, reflect)); - if (offdata.nfaceprops < 3) - exit (fprintf (stderr, - "Guide_anyshape: %s: file_coatings=1 but %s does not give m, alpha, W for each face\n" - " (expected lines 'nv i1 ... inv m alpha W [R0 [Qc]]'). Aborting.\n", - NAME_CURRENT_COMP, geometry)); - MPI_MASTER (printf ("Guide_anyshape: %s: using per-face m, alpha, W%s%s from %s\n", NAME_CURRENT_COMP, offdata.nfaceprops > OFF_FACEPROP_R0 ? ", R0" : "", - offdata.nfaceprops > OFF_FACEPROP_QC ? ", Qc" : "", geometry);); + if (mesh.nfaceprops <= GUIDE_ANYSHAPE_W) { + fprintf (stderr, + "Guide_anyshape: %s: file_coatings=1 but %s does not give m, alpha, W for each face\n" + " (expected lines 'nv i1 ... inv m alpha W [R0 [Qc]]'). Aborting.\n", + NAME_CURRENT_COMP, geometry); + exit (-1); + } + MPI_MASTER (printf ("Guide_anyshape: %s: using per-face m, alpha, W%s%s from %s\n", NAME_CURRENT_COMP, mesh.nfaceprops > GUIDE_ANYSHAPE_R0 ? ", R0" : "", + mesh.nfaceprops > GUIDE_ANYSHAPE_QC ? ", Qc" : "", geometry);); } %} @@ -145,40 +149,28 @@ TRACE int counter = 0; /* main loop for multiple reflections */ #ifdef OPENACC - off_struct thread_offdata = offdata; + gmesh_struct thread_mesh = mesh; #else - #define thread_offdata offdata + #define thread_mesh mesh #endif do { - double t0 = 0, t3 = 0, dt = 0; - unsigned long fi0 = 0, fi3 = 0, fi = 0; - Coords n0, n3, n = { 0, 0, 0 }; - /* determine intersections with object */ - - double mc_gx = 0.0, mc_gy = 0.0, mc_gz = 0.0; - if (mcgravitation) { - Coords locgrav; - locgrav = rot_apply (_comp->_rotation_absolute, coords_set (0, -GRAVITY, 0)); - coords_get (locgrav, &mc_gx, &mc_gy, &mc_gz); - } - - intersect = off_intersect_idx (&t0, &t3, &n0, &n3, &fi0, &fi3, x, y, z, vx, vy, vz, mc_gx, mc_gy, mc_gz, thread_offdata); + double dt = 0; + int fi = 0; + Coords n = { 0, 0, 0 }; + gmesh_hit hits[GMESH_MAX_HITS]; + Coords acceleration = coords_set (0, 0, 0); + if (mcgravitation) + acceleration = rot_apply (_comp->_rotation_absolute, coords_set (0, -GRAVITY, 0)); - /* get the smallest positive */ - if (t0 > 0) { - dt = t0; - n = n0; - fi = fi0; - } - if (intersect > 1 && dt <= 0 && t3 > dt) { - dt = t3; - n = n3; - fi = fi3; - } + // next intersection with the object, along the parabola under gravity + intersect = gmesh_intersect_gravity (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), acceleration, 0, hits); /* exit loop when no intersection forward */ - if (dt <= 0 || !intersect) + if (!intersect) break; + dt = hits[0].time; + n = hits[0].normal; + fi = hits[0].face; double nx, ny, nz; coords_get (n, &nx, &ny, &nz); @@ -199,11 +191,11 @@ TRACE else { double par[] = { R0, Qc, alpha, m, W }; if (file_coatings) { /* per-face values, parameters as defaults */ - par[0] = off_face_prop (&thread_offdata, fi, OFF_FACEPROP_R0, R0); - par[1] = off_face_prop (&thread_offdata, fi, OFF_FACEPROP_QC, Qc); - par[2] = off_face_prop (&thread_offdata, fi, OFF_FACEPROP_ALPHA, alpha); - par[3] = off_face_prop (&thread_offdata, fi, OFF_FACEPROP_M, m); - par[4] = off_face_prop (&thread_offdata, fi, OFF_FACEPROP_W, W); + par[0] = gmesh_face_prop (&thread_mesh, fi, GUIDE_ANYSHAPE_R0, R0); + par[1] = gmesh_face_prop (&thread_mesh, fi, GUIDE_ANYSHAPE_QC, Qc); + par[2] = gmesh_face_prop (&thread_mesh, fi, GUIDE_ANYSHAPE_ALPHA, alpha); + par[3] = gmesh_face_prop (&thread_mesh, fi, GUIDE_ANYSHAPE_M, m); + par[4] = gmesh_face_prop (&thread_mesh, fi, GUIDE_ANYSHAPE_W, W); } StdReflecFunc (q, par, &R); } @@ -245,7 +237,7 @@ TRACE MCDISPLAY %{ /* display the object */ - off_display (offdata); + gmesh_display (&mesh); %} END diff --git a/mcstas-comps/optics/Refractor.comp b/mcstas-comps/optics/Refractor.comp index 4e9b5c1da9..ebb43ee641 100644 --- a/mcstas-comps/optics/Refractor.comp +++ b/mcstas-comps/optics/Refractor.comp @@ -48,7 +48,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 @@ -154,7 +154,7 @@ SHARE %include "read_table-lib" - %include "interoff-lib" + %include "geometry-mesh-lib" %include "ref-lib" /* wrappers to intersection routines which also return the normal vector */ @@ -319,7 +319,7 @@ DECLARE %{ double rho; double bc; - off_struct offdata; + gmesh_struct mesh; double mean_n; double events; double theta1; @@ -348,8 +348,14 @@ INITIALIZE exit (-1); #else /* off/ply case */ - if (!off_init (geometry, xwidth, yheight, zdepth, 0, &offdata)) - exit (printf ("Refractor: %s: FATAL: invalid OFF/PLY geometry specification for file '%s'.\n", NAME_CURRENT_COMP, geometry)); + if (!gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) { + printf ("Refractor: %s: FATAL: invalid OFF/PLY geometry specification for file '%s'.\n", NAME_CURRENT_COMP, geometry); + exit (-1); + } + if (!gmesh_check_closed (&mesh)) { + fprintf (stderr, "Refractor: %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 } else exit (printf ("Refractor: %s: FATAL: invalid geometry specification.\n" @@ -382,15 +388,16 @@ 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 do { double nx = 0, ny = 0, nz = 0; double t0 = 0, t1 = 0, dt = 0; + int mesh_inside = 0; intersect = 0; iterations++; @@ -409,11 +416,13 @@ TRACE } #ifdef USE_OFF else if (geometry && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - Coords n0, n1; - intersect = off_intersect (&t0, &t1, &n0, &n1, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); - if (intersect) { - coords_get (t0 <= 0 || t0 > t1 ? n1 : n0, &nx, &ny, &nz); - } + gmesh_hit first; + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t1, &first); + mesh_inside = intersect == 1; + // normal of the next hit, oriented as in interoff-lib: opposite to the + // right-hand rule of the vertex order in the file, unless the mesh was flipped + if (intersect) + coords_get (coords_scale (first.normal, thread_mesh.flipped ? 1 : -1), &nx, &ny, &nz); } #endif @@ -441,7 +450,7 @@ TRACE char scatter_me = 0, refract_me = 0; double my_t = 0, d_path = 0; double v = 0, lambda = 0; - char inbulk = t0 < 0 && t1 > 0; + char inbulk = (t0 < 0 && t1 > 0) || mesh_inside; /* when density is given negative, we assume bulk refractive material is 'outside' or 'before' the shape */ @@ -722,7 +731,7 @@ MCDISPLAY } box (0, 0, 0, 4 * radius, 4 * radius, 2 * radius + zdepth, 0, 0, 1, 0); } else if (geometry && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { - off_display (offdata); + gmesh_display (&mesh); } %} END diff --git a/mcstas-comps/samples/Incoherent.comp b/mcstas-comps/samples/Incoherent.comp index 1e61f4bf47..3a7c1ba436 100644 --- a/mcstas-comps/samples/Incoherent.comp +++ b/mcstas-comps/samples/Incoherent.comp @@ -39,12 +39,16 @@ * hollow cylinder: radius x yheight and thickness>0 * sphere: radius (yheight=0 thickness=0) * hollow sphere: radius and thickness>0 (yheight=0) -* any shape: geometry=OFF file +* any shape: geometry=OFF, PLY or STL file * * 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. +* A mesh must be a single, closed object: the neutron path through it is +* taken from the first entry and exit only, so a second part of the mesh +* further along the path (disjoint parts, or a concave shape the path +* re-enters) is not seen. * Such files may be generated from XYZ data using: * qhull < coordinates.xyz Qx Qv Tv o > geomview.off * or @@ -80,7 +84,7 @@ * gamma: [1] Lorentzian width of quasielastic broadening (HWHM) * Etrans: [meV] Global energy-transfer, for use in inelastic settings * deltaE: [meV] Width in energy around Etrans, for use in inelastic settings -* 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 * concentric: [1] Indicate that this component has a hollow geometry and may contain other components. It should then be duplicated after the inside part (only for box, cylinder, sphere) * * order: [1] Limit multiple scattering up to given order 0 means all (default), 1 means single, 2 means double, ... @@ -126,7 +130,8 @@ sigma_abs=5.08, sigma_inc=5.08, Vc=13.827, concentric=0, order=0) SHARE %{ %include "read_table-lib" - %include "interoff-lib" + %include "geometry-mesh-lib" + struct StructVarsInc { double sigma_a; /* Absorption cross section per atom (barns) */ double sigma_i; /* Incoherent scattering cross section per atom (barns) */ @@ -143,7 +148,7 @@ SHARE DECLARE %{ struct StructVarsInc VarsInc; - off_struct offdata; + gmesh_struct mesh; %} INITIALIZE @@ -154,10 +159,17 @@ INITIALIZE fprintf (stderr, "Error: You are attempting to use an OFF geometry without -DUSE_OFF. You will need to recompile with that define set!\n"); exit (-1); #else - if (off_init (geometry, xwidth, yheight, zdepth, 0, &offdata)) { + if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) { VarsInc.shape = 3; thickness = 0; concentric = 0; + } else { + fprintf (stderr, "Incoherent: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry); + exit (-1); + } + if (!gmesh_check_closed (&mesh)) { + fprintf (stderr, "Incoherent: %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 } else if (xwidth && yheight && zdepth) @@ -261,10 +273,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 do { /* Main interaction loop. Ends with intersect=0 */ @@ -278,7 +290,7 @@ TRACE intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF else if (VarsInc.shape == 3) - intersect = off_intersect (&t0, &t3, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL); #endif if (intersect) { @@ -475,7 +487,7 @@ TRACE intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF else if (VarsInc.shape == 3) - intersect = off_intersect (&t0, &t3, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL); #endif flag_ishollow = 0; @@ -526,7 +538,7 @@ TRACE MCDISPLAY %{ if (geometry && strlen (geometry) && strcmp (geometry, "NULL") && strcmp (geometry, "0")) { /* OFF file */ - off_display (offdata); + gmesh_display (&mesh); } else if (radius > 0 && yheight) { /* cylinder along y*/ cylinder (0, 0, 0, radius, yheight, thickness, 0, 1, 0); } else if (xwidth && yheight) { /* box/rectangle */ diff --git a/mcstas-comps/samples/Isotropic_Sqw.comp b/mcstas-comps/samples/Isotropic_Sqw.comp index f5797b5ad5..377441716e 100644 --- a/mcstas-comps/samples/Isotropic_Sqw.comp +++ b/mcstas-comps/samples/Isotropic_Sqw.comp @@ -63,12 +63,16 @@ * hollow cylinder: radius x yheight and thickness>0 * sphere: radius (yheight=0 thickness=0) * hollow sphere: radius and thickness>0 (yheight=0) -* any shape: geometry=OFF file +* any shape: geometry=OFF, PLY or STL file * * 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 OFF, PLY and NOFF file format but not COFF (colored faces). +* It supports the OFF (incl. NOFF, COFF), PLY and STL file formats. +* A mesh must be a single, closed object: the neutron path through it is +* taken from the first entry and exit only, so a second part of the mesh +* further along the path (disjoint parts, or a concave shape the path +* re-enters) is not seen. * Such files may be generated from XYZ data using: * qhull < coordinates.xyz Qx Qv Tv o > geomview.off * or @@ -204,7 +208,7 @@ * * OPTIONAL PARAMETERS: * concentric: [1] Indicate that this component has a hollow geometry and may contain other components. It should then be duplicated after the inside part (only for box, cylinder, sphere) [1] -* 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 * order: [1] Limit multiple scattering up to given order 0:all (default), 1:single, 2:double, ... * verbose: [1] Verbosity level (0:silent, 1:normal, 2:verbose, 3:debug). A verbosity>1 also computes dispersions and S(q,w) analysis. * d_phi: [deg] scattering vertical angular spreading (usually the height of the next component/detector). Use 0 for full space. This is only relevant for single scattering (order=1). @@ -313,7 +317,7 @@ SHARE #define qSq {-1,0,0,0,0,0,1, 0,0 } %include "read_table-lib" - %include "interoff-lib" + %include "geometry-mesh-lib" /* For the density of states S(w) */ struct Sqw_W_struct { @@ -430,8 +434,6 @@ SHARE Sqw_Data->q_min_file = 0; } - off_struct offdata; - /* gaussian distribution to appply around Bragg peaks in a powder */ double Sqw_powder_gauss (double x, double mean, double rms) { @@ -1597,10 +1599,7 @@ SHARE T = 0; /* unrealistic */ if (!T_file && T) { MPI_MASTER ( - if (Sqw->verbose_output > 0) { - printf ("Isotropic_Sqw: %s: Temperature computed from S(q,w) data from %s is T=%g [K].\n", Sqw->compname, file, T); - } - ); + if (Sqw->verbose_output > 0) { printf ("Isotropic_Sqw: %s: Temperature computed from S(q,w) data from %s is T=%g [K].\n", Sqw->compname, file, T); }); } if (Sqw->Temperature == 0) { @@ -2012,7 +2011,7 @@ DECLARE %{ struct Sqw_sample_struct VarSqw; int* columns; - off_struct offdata; + gmesh_struct mesh; %} /*****************************************************************************/ @@ -2031,10 +2030,17 @@ INITIALIZE fprintf (stderr, "Error: You are attempting to use an OFF geometry without -DUSE_OFF. You will need to recompile with that define set!\n"); exit (-1); #else - if (off_init (geometry, xwidth, yheight, zdepth, 0, &offdata)) { + if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) { VarSqw.shape = 3; thickness = 0; concentric = 0; + } else { + fprintf (stderr, "Isotropic_Sqw: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry); + exit (-1); + } + if (!gmesh_check_closed (&mesh)) { + fprintf (stderr, "Isotropic_Sqw: %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 } else if (xwidth && yheight && zdepth) @@ -2239,10 +2245,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 do { /* Main interaction loop. Ends with intersect=0 */ @@ -2258,7 +2264,7 @@ TRACE intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF else if (VarSqw.shape == 3) - intersect = off_intersect (&t0, &t3, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL); #endif } else { if (VarSqw.shape == 0) @@ -2271,7 +2277,7 @@ TRACE intersect = sphere_intersect (&t0, &t3, x, y, z, vx, vy, vz, radius - thickness); #ifdef USE_OFF else if (VarSqw.shape == 3) - intersect = off_intersect (&t0, &t3, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL); #endif } } else @@ -2399,7 +2405,7 @@ TRACE } /* Proba of scattering vs absorption (integrating along the whole trajectory) */ - ws = my_s / my_t; /* (inc+coh)/(inc+coh+abs) */ + ws = my_s / my_t; /* (inc+coh)/(inc+coh+abs) */ d_path = v * (dt0 + dt2); /* total path lenght in sample */ /* Proba of transmission/interaction along length d_path */ p_trans = exp (-my_t * d_path); @@ -2819,27 +2825,22 @@ FINALLY mc_MPI_Sum (&VarSqw.multi, 1); } #endif - MPI_MASTER( - if (VarSqw.neutron_removed) - printf("Isotropic_Sqw: %s: %li neutron events (out of %li) that should have\n" - " scattered were transmitted because scattering conditions\n" - "WARNING could not be satisfied after %i tries.\n", - NAME_CURRENT_COMP, VarSqw.neutron_removed, - VarSqw.neutron_exit+VarSqw.neutron_removed, VarSqw.maxloop); - if (VarSqw.neutron_pmult) - printf("Isotropic_Sqw: %s: %li neutron events (out of %li) reached\n" - "WARNING unrealistic weight. The S(q,w) norm might be too high.\n", - NAME_CURRENT_COMP, VarSqw.neutron_pmult, VarSqw.neutron_exit); - - if (VarSqw.verbose_output >= 1 && VarSqw.psum_scatt > 0) { - printf ("Isotropic_Sqw: %s: Scattering fraction=%g of incoming intensity\n" - " Absorption fraction =%g\n", - NAME_CURRENT_COMP, VarSqw.mean_scatt / VarSqw.psum_scatt, VarSqw.mean_abs / VarSqw.psum_scatt); - printf (" Single scattering intensity =%g (coh=%g inc=%g)\n" - " Multiple scattering intensity =%g\n", - VarSqw.single_coh + VarSqw.single_inc, VarSqw.single_coh, VarSqw.single_inc, VarSqw.multi); - } - ); + MPI_MASTER (if (VarSqw.neutron_removed) printf ("Isotropic_Sqw: %s: %li neutron events (out of %li) that should have\n" + " scattered were transmitted because scattering conditions\n" + "WARNING could not be satisfied after %i tries.\n", + NAME_CURRENT_COMP, VarSqw.neutron_removed, VarSqw.neutron_exit + VarSqw.neutron_removed, VarSqw.maxloop); + if (VarSqw.neutron_pmult) printf ("Isotropic_Sqw: %s: %li neutron events (out of %li) reached\n" + "WARNING unrealistic weight. The S(q,w) norm might be too high.\n", + NAME_CURRENT_COMP, VarSqw.neutron_pmult, VarSqw.neutron_exit); + + if (VarSqw.verbose_output >= 1 && VarSqw.psum_scatt > 0) { + printf ("Isotropic_Sqw: %s: Scattering fraction=%g of incoming intensity\n" + " Absorption fraction =%g\n", + NAME_CURRENT_COMP, VarSqw.mean_scatt / VarSqw.psum_scatt, VarSqw.mean_abs / VarSqw.psum_scatt); + printf (" Single scattering intensity =%g (coh=%g inc=%g)\n" + " Multiple scattering intensity =%g\n", + VarSqw.single_coh + VarSqw.single_inc, VarSqw.single_coh, VarSqw.single_inc, VarSqw.multi); + }); /* end FINALLY */ %} /*****************************************************************************/ @@ -2904,7 +2905,7 @@ MCDISPLAY circle ("xz", 0, 0, 0, radius); circle ("yz", 0, 0, 0, radius); } else if (VarSqw.shape == 3) { /* OFF file */ - off_display (offdata); + gmesh_display (&mesh); } } /* end MCDISPLAY */ diff --git a/mcstas-comps/samples/PowderN.comp b/mcstas-comps/samples/PowderN.comp index 32e9922108..db09eacf4c 100644 --- a/mcstas-comps/samples/PowderN.comp +++ b/mcstas-comps/samples/PowderN.comp @@ -54,7 +54,11 @@ * 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. +* A mesh must be a single, closed object: the neutron path through it is +* taken from the first entry and exit only, so a second part of the mesh +* further along the path (disjoint parts, or a concave shape the path +* re-enters) is not seen. * Such files may be generated from XYZ data using: * qhull < coordinates.xyz Qx Qv Tv o > geomview.off * or @@ -164,7 +168,7 @@ * p_transmit: [1] Fraction of transmitted (only attenuated) neutron rays. * p_interact: [1] Fraction of events interacting coherently with sample. * concentric: [1] Indicate that this component has a hollow geometry and may contain other components. It should then be duplicated after the inside part (only for box, cylinder, sphere). -* 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. * barns: [1] Flag to indicate if |F|^2 from 'reflections' is in barns or fm^2 (barns=1 for laz, barns=0 for lau type files). * sigma_abs: [barns] Absorption cross section per unit cell at 2200 m/s. Use a negative value to inactivate it. * sigma_inc: [barns] Incoherent cross section per unit cell. Use a negative value to inactivate it. @@ -224,7 +228,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 POWDERN_DECL #define POWDERN_DECL @@ -831,7 +836,7 @@ DECLARE %{ struct line_info_struct line_info; double* columns; - off_struct offdata; + gmesh_struct mesh; double tgt_x; double tgt_y; double tgt_z; @@ -875,10 +880,17 @@ INITIALIZE fprintf (stderr, "Error: You are attempting to use an OFF geometry without -DUSE_OFF. You will need to recompile with that define set!\n"); exit (-1); #else - if (off_init (geometry, xwidth, yheight, zdepth, 0, &offdata)) { + if (gmesh_init_fit (geometry, xwidth, yheight, zdepth, 1, GMESH_PROPS_IGNORE, &mesh)) { line_info.shape = 3; thickness = 0; concentric = 0; + } else { + fprintf (stderr, "PowderN: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry); + exit (-1); + } + if (!gmesh_check_closed (&mesh)) { + fprintf (stderr, "PowderN: %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 } else if (xwidth && yheight && zdepth) @@ -1095,10 +1107,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 if (line_info.V_0 > 0 && (line_info.count || line_info.my_inc)) { @@ -1114,7 +1126,7 @@ TRACE } #ifdef USE_OFF else if (line_info.shape == 3) { - intersect = off_intersect (&t0, &t3, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL); intersecti = 0; } #endif @@ -1309,7 +1321,7 @@ TRACE } #ifdef USE_OFF else if (line_info.shape == 3) { - intersect = off_intersect (&t0, &t3, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL); intersecti = 0; } #endif @@ -1383,7 +1395,7 @@ TRACE } #ifdef USE_OFF else if (line_info.shape == 3) { - intersect = off_intersect (&t0, &t3, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t0, &t3, NULL); intersecti = 0; } #endif @@ -1532,7 +1544,7 @@ MCDISPLAY circle ("xz", 0, 0, 0, radius); circle ("yz", 0, 0, 0, radius); } else if (line_info.shape == 3) { /* OFF file */ - off_display (offdata); + gmesh_display (&mesh); } } %} diff --git a/mcstas-comps/samples/Single_crystal.comp b/mcstas-comps/samples/Single_crystal.comp index ac29110677..d9c75ce116 100644 --- a/mcstas-comps/samples/Single_crystal.comp +++ b/mcstas-comps/samples/Single_crystal.comp @@ -79,12 +79,16 @@ * box/plate: xwidth x yheight x zdepth * cylinder: radius x yheight * sphere: radius (yheight=0) -* any shape: geometry=OFF file +* any shape: geometry=OFF, PLY or STL file * * 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. +* A mesh must be a single, closed object: the neutron path through it is +* taken from the first entry and exit only, so a second part of the mesh +* further along the path (disjoint parts, or a concave shape the path +* re-enters) is not seen. * Such files may be generated from XYZ data using: * qhull < coordinates.xyz Qx Qv Tv o > geomview.off * or @@ -172,7 +176,7 @@ * xwidth: [m] Width of crystal * yheight: [m] Height of crystal * zdepth: [m] Depth of crystal (no extinction simulated) -* 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 * delta_d_d: [1] Lattice spacing variance, gaussian RMS * mosaic: [arc minutes] Crystal mosaic (isotropic), gaussian RMS. Puts the crystal in the isotropic mosaic model state, thus disregarding other mosaicity parameters. * mosaic_a: [arc minutes] 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. @@ -309,7 +313,8 @@ SHARE %{ /* used for reading data table from file */ %include "read_table-lib" - %include "interoff-lib" + %include "geometry-mesh-lib" + #ifndef OPENACC %include "opencl-lib" #endif @@ -1327,7 +1332,7 @@ SHARE DECLARE %{ struct hkl_info_struct hkl_info; - off_struct offdata; + gmesh_struct mesh; struct hkl_data* hkl_list; #ifndef OPENACC struct tau_data tau_list[MCSX_REFL_SLIST_SIZE]; @@ -1397,8 +1402,16 @@ INITIALIZE fprintf (stderr, "Error: You are attempting to use an OFF geometry without -DUSE_OFF. You will need to recompile with that define set!\n"); exit (-1); #else - 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: %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: %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 } else if (xwidth && yheight && zdepth) @@ -1505,10 +1518,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 /* Intersection neutron trajectory / sample (sample surface) */ @@ -1520,7 +1533,7 @@ TRACE intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF else if (hkl_info.shape == 3) - intersect = off_intersect (&t1, &t2, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2, NULL); #endif if (t2 < 0) intersect = 0; /* we passed sample volume already */ @@ -1568,7 +1581,7 @@ TRACE intersect = sphere_intersect (&t1, &t2, x, y, z, vx, vy, vz, radius); #ifdef USE_OFF else if (hkl_info.shape == 3) - intersect = off_intersect (&t1, &t2, NULL, NULL, x, y, z, vx, vy, vz, 0, 0, 0, thread_offdata); + intersect = gmesh_entry_exit (&thread_mesh, coords_set (x, y, z), coords_set (vx, vy, vz), &t1, &t2, NULL) == 1; #endif if (!intersect || t2 * v < -1e-9 || t1 * v > 1e-9) { /* neutron is leaving the sample */ @@ -1956,7 +1969,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 diff --git a/mcstas-comps/samples/Single_magnetic_crystal.comp b/mcstas-comps/samples/Single_magnetic_crystal.comp index d6841ed976..ad7277227f 100644 --- a/mcstas-comps/samples/Single_magnetic_crystal.comp +++ b/mcstas-comps/samples/Single_magnetic_crystal.comp @@ -35,12 +35,16 @@ * box/plate: xwidth x yheight x zdepth * cylinder: radius x yheight * sphere: radius (yheight=0) -* any shape: geometry=OFF file +* any shape: geometry=OFF, PLY or STL file * * 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 OFF and NOFF file format but not COFF (colored faces). +* It supports the OFF (incl. NOFF, COFF), PLY and STL file formats. +* A mesh must be a single, closed object: the neutron path through it is +* taken from the first entry and exit only, so a second part of the mesh +* further along the path (disjoint parts, or a concave shape the path +* re-enters) is not seen. * Such files may be generated from XYZ data using qhull/powercrust, and * viewed with geomview * The default size of the object depends of the OFF file data, but its @@ -55,7 +59,7 @@ * xwidth: [m] Width of crystal * yheight: [m] Height of crystal * zdepth: [m] Depth of crystal (no extinction simulated) -* geometry: [str] Name of an Object File Format (OFF) file for complex geometry. The OFF 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 * delta_d_d: [1] Lattice spacing variance, gaussian RMS * mosaic: [arcmin] Crystal mosaic (isotropic), gaussian RMS * mosaic_h: [arcmin] Horizontal (rotation around Y) mosaic (anisotropic), gaussian RMS @@ -110,8 +114,9 @@ SHARE %{ /* used for reading data table from file */ %include "read_table-lib" - %include "interoff-lib" + %include "geometry-mesh-lib" %include "mccode-complex-lib" + // %include "columnfile" /* Declare structures and functions only once in each instrument. */ #ifndef SINGLE_MAGNETIC_CRYSTAL_DECL @@ -499,7 +504,7 @@ SHARE DECLARE %{ struct hkl_info_struct hkl_info; - off_struct offdata; + gmesh_struct mesh; %} INITIALIZE @@ -541,8 +546,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_magnetic_crystal: %s: can not use the geometry file %s. Aborting.\n", NAME_CURRENT_COMP, geometry); + exit (-1); + } + if (!gmesh_check_closed (&mesh)) { + fprintf (stderr, "Single_magnetic_crystal: %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 */ @@ -613,7 +626,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 */ @@ -641,7 +654,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) @@ -910,7 +923,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 diff --git a/mcstas-comps/share/monitor_nd-lib.c b/mcstas-comps/share/monitor_nd-lib.c index f0d0c35c8e..dcab9be0a3 100644 --- a/mcstas-comps/share/monitor_nd-lib.c +++ b/mcstas-comps/share/monitor_nd-lib.c @@ -2216,37 +2216,6 @@ void Monitor_nD_McDisplay(MonitornD_Defines_type *DEFS, /* defined by a mesh file (geometry parameter) */ /* ========================================================================= */ -// Hits of the line position + t*velocity with mesh ahead of the neutron -// (t > 0). Inside a closed mesh, t0 is 0 and t1 the first exit; otherwise -// t0 is the first hit and t1 the next one (t0 when there is a single hit). -// face is the face of the first hit ahead. Returns 1 on a hit, 0 otherwise. -#pragma acc routine seq -int Monitor_nD_MeshIntersect(gmesh_struct* mesh, Coords position, Coords velocity, double* t0, double* t1, int* face) -{ - gmesh_hit hits[GMESH_MAX_HITS]; - double t_min = 0; - int nhits = gmesh_intersect(mesh, position, velocity, t_min, hits); - int inside = 0; - if (!nhits) - return 0; - // normals of a closed, oriented mesh point outward: leaving first means inside - if (mesh->closed && mesh->oriented) - inside = hits[0].in_out < 0; - else if (mesh->closed) - inside = gmesh_inside(mesh, position); - if (inside) { - *t0 = 0; - *t1 = hits[0].t; - } else { - *t0 = hits[0].t; - *t1 = hits[0].t; - if (nhits > 1) - *t1 = hits[1].t; - } - *face = hits[0].face; - return 1; -} - // Warns when mantid is combined with a closed mesh: pixel IDs are panels. void Monitor_nD_CheckMantidMesh(gmesh_struct* mesh, int flag_mantid, char* name) { diff --git a/mcstas-comps/share/monitor_nd-lib.h b/mcstas-comps/share/monitor_nd-lib.h index f4c332ecc6..fc54e7b485 100644 --- a/mcstas-comps/share/monitor_nd-lib.h +++ b/mcstas-comps/share/monitor_nd-lib.h @@ -220,8 +220,6 @@ void Monitor_nD_EnsureCapacity(MonitornD_Defines_type *, MonitornD_Variables_typ MCDETECTOR Monitor_nD_Save(MonitornD_Defines_type *, MonitornD_Variables_type *); void Monitor_nD_Finally(MonitornD_Defines_type *, MonitornD_Variables_type *); void Monitor_nD_McDisplay(MonitornD_Defines_type *, MonitornD_Variables_type *); -#pragma acc routine seq -int Monitor_nD_MeshIntersect(gmesh_struct*, Coords, Coords, double*, double*, int*); void Monitor_nD_MeshMantid(gmesh_struct*, long); void Monitor_nD_CheckMantidMesh(gmesh_struct*, int, char*); diff --git a/mcstas-comps/share/union-lib.c b/mcstas-comps/share/union-lib.c index 7c62432402..301ea96b69 100644 --- a/mcstas-comps/share/union-lib.c +++ b/mcstas-comps/share/union-lib.c @@ -2995,7 +2995,7 @@ int sample_mesh_intersect(double *t, *num_solutions = gmesh_intersect(triangle_mesh, position, velocity, t_min, hits); for (i = 0; i < *num_solutions; i++) { Coords normal = rot_apply(geometry->rotation_matrix, hits[i].normal); - t[i] = hits[i].t; + t[i] = hits[i].time; nx[i] = normal.x; ny[i] = normal.y; nz[i] = normal.z; diff --git a/mcxtrace-comps/share/union-lib.c b/mcxtrace-comps/share/union-lib.c index e8d2ba95bd..48c9d7f504 100644 --- a/mcxtrace-comps/share/union-lib.c +++ b/mcxtrace-comps/share/union-lib.c @@ -2621,7 +2621,7 @@ int sample_mesh_intersect(double *t,int *num_solutions,double *r,double *v,struc // McXtrace intersect functions return lengths, so trace along the unit direction direction = coords_scale(direction, 1.0/coords_len(direction)); *num_solutions = gmesh_intersect(triangle_mesh, position, direction, -1e-9*triangle_mesh->length_scale, hits); - for (i = 0; i < *num_solutions; i++) t[i] = hits[i].t; + for (i = 0; i < *num_solutions; i++) t[i] = hits[i].time; return *num_solutions > 0; }