morphogen 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- morphogen/__init__.py +60 -0
- morphogen/__main__.py +7 -0
- morphogen/_platform.py +55 -0
- morphogen/_public.py +153 -0
- morphogen/_utils.py +80 -0
- morphogen/_version.py +1 -0
- morphogen/analysis/__init__.py +174 -0
- morphogen/analysis/anisotropy.py +135 -0
- morphogen/analysis/basic.py +38 -0
- morphogen/analysis/cells.py +54 -0
- morphogen/analysis/connectivity.py +156 -0
- morphogen/analysis/correlation.py +195 -0
- morphogen/analysis/covariance.py +80 -0
- morphogen/analysis/fractal.py +54 -0
- morphogen/analysis/measures.py +188 -0
- morphogen/analysis/morphology.py +265 -0
- morphogen/analysis/particles.py +160 -0
- morphogen/analysis/percolation.py +2034 -0
- morphogen/analysis/power_laws.py +143 -0
- morphogen/analysis/random_sets.py +351 -0
- morphogen/analysis/stereology.py +289 -0
- morphogen/analysis/surface.py +107 -0
- morphogen/analysis/topology.py +92 -0
- morphogen/automata/__init__.py +74 -0
- morphogen/automata/engine.py +211 -0
- morphogen/automata/growth.py +686 -0
- morphogen/automata/lattice.py +143 -0
- morphogen/automata/reaction.py +255 -0
- morphogen/automata/rules.py +353 -0
- morphogen/automata/tilings.py +1715 -0
- morphogen/cli.py +3526 -0
- morphogen/config.py +2167 -0
- morphogen/contact/__init__.py +113 -0
- morphogen/contact/experiments.py +368 -0
- morphogen/contact/hertz.py +104 -0
- morphogen/contact/kernels.py +827 -0
- morphogen/contact/periodic.py +278 -0
- morphogen/contact/problem.py +578 -0
- morphogen/contact/surfaces.py +310 -0
- morphogen/coupling/__init__.py +43 -0
- morphogen/coupling/ageing.py +143 -0
- morphogen/coupling/diffusion.py +226 -0
- morphogen/coupling/fenicsx.py +484 -0
- morphogen/coupling/oxidation.py +447 -0
- morphogen/coupling/plates.py +247 -0
- morphogen/design/__init__.py +51 -0
- morphogen/design/csp.py +616 -0
- morphogen/design/frames.py +277 -0
- morphogen/design/search.py +287 -0
- morphogen/design/tiles.py +336 -0
- morphogen/distributions.py +312 -0
- morphogen/domain.py +203 -0
- morphogen/dynamics/__init__.py +179 -0
- morphogen/dynamics/model.py +638 -0
- morphogen/dynamics/openradioss.py +643 -0
- morphogen/dynamics/results.py +515 -0
- morphogen/fracture/__init__.py +170 -0
- morphogen/fracture/bulk.py +349 -0
- morphogen/fracture/cohesive.py +1614 -0
- morphogen/fracture/ductile.py +557 -0
- morphogen/fracture/lefm.py +1168 -0
- morphogen/fracture/mixed.py +1178 -0
- morphogen/fracture/periodic.py +733 -0
- morphogen/fracture/phasefield.py +714 -0
- morphogen/fracture/phasefield_finite.py +368 -0
- morphogen/fracture/surface.py +572 -0
- morphogen/fracture/toughness.py +566 -0
- morphogen/gpu.py +108 -0
- morphogen/gui/__init__.py +8 -0
- morphogen/gui/api.py +1927 -0
- morphogen/gui/desktop.py +387 -0
- morphogen/gui/icon.png +0 -0
- morphogen/gui/index.html +1909 -0
- morphogen/gui/server.py +229 -0
- morphogen/gui/tools/__init__.py +629 -0
- morphogen/gui/tools/compare.py +149 -0
- morphogen/gui/tools/contact.py +391 -0
- morphogen/gui/tools/couplings.py +1085 -0
- morphogen/gui/tools/crackfront.py +245 -0
- morphogen/gui/tools/design.py +224 -0
- morphogen/gui/tools/dynamics.py +824 -0
- morphogen/gui/tools/fracture.py +2586 -0
- morphogen/gui/tools/interop.py +1654 -0
- morphogen/gui/tools/learning.py +595 -0
- morphogen/gui/tools/mechanics.py +3881 -0
- morphogen/gui/tools/nonlocal_damage.py +1321 -0
- morphogen/gui/tools/phasefield.py +215 -0
- morphogen/gui/tools/physics.py +597 -0
- morphogen/gui/tools/stability.py +1756 -0
- morphogen/gui/tools/statistics.py +1408 -0
- morphogen/gui/viewer.js +1100 -0
- morphogen/homogenization/__init__.py +210 -0
- morphogen/homogenization/acoustics.py +566 -0
- morphogen/homogenization/amg.py +1005 -0
- morphogen/homogenization/apparent.py +1046 -0
- morphogen/homogenization/behaviours.cu +2285 -0
- morphogen/homogenization/behaviours.py +944 -0
- morphogen/homogenization/beremin.py +263 -0
- morphogen/homogenization/bifurcation.py +1085 -0
- morphogen/homogenization/bloch.py +507 -0
- morphogen/homogenization/bounds.py +226 -0
- morphogen/homogenization/core.py +458 -0
- morphogen/homogenization/coupled.py +1135 -0
- morphogen/homogenization/crystal.py +2967 -0
- morphogen/homogenization/dilatation.py +454 -0
- morphogen/homogenization/elements.py +157 -0
- morphogen/homogenization/fatigue.py +768 -0
- morphogen/homogenization/fe.py +1739 -0
- morphogen/homogenization/fenicsx.py +307 -0
- morphogen/homogenization/fft.py +499 -0
- morphogen/homogenization/fft_nonlinear.py +391 -0
- morphogen/homogenization/finite.py +613 -0
- morphogen/homogenization/flow.py +361 -0
- morphogen/homogenization/frames.py +1125 -0
- morphogen/homogenization/gpu.py +1241 -0
- morphogen/homogenization/harmonic.py +427 -0
- morphogen/homogenization/identification.py +579 -0
- morphogen/homogenization/kernels.py +760 -0
- morphogen/homogenization/laws.py +2061 -0
- morphogen/homogenization/materials.py +1080 -0
- morphogen/homogenization/mfront.py +589 -0
- morphogen/homogenization/networks.py +880 -0
- morphogen/homogenization/nonlinear.py +2612 -0
- morphogen/homogenization/ntfa.py +577 -0
- morphogen/homogenization/paths.py +476 -0
- morphogen/homogenization/photonic.py +467 -0
- morphogen/homogenization/poroelastic.py +212 -0
- morphogen/homogenization/porous.py +1120 -0
- morphogen/homogenization/postprocess.py +290 -0
- morphogen/homogenization/solvers.py +1140 -0
- morphogen/homogenization/tensors.py +240 -0
- morphogen/homogenization/voxel_fe.py +457 -0
- morphogen/homogenization/xfem.py +1016 -0
- morphogen/homogenization/zmat.py +1258 -0
- morphogen/iga/__init__.py +48 -0
- morphogen/iga/analysis.py +508 -0
- morphogen/iga/basis.py +214 -0
- morphogen/iga/geometry.py +501 -0
- morphogen/iga/homogenization.py +219 -0
- morphogen/iga/optimization.py +700 -0
- morphogen/iga/shapes.py +331 -0
- morphogen/image.py +128 -0
- morphogen/io/__init__.py +246 -0
- morphogen/io/amitex.py +741 -0
- morphogen/io/ebsd.py +881 -0
- morphogen/io/export.py +196 -0
- morphogen/io/geometry.py +158 -0
- morphogen/io/medit.py +147 -0
- morphogen/io/meshes.py +844 -0
- morphogen/io/metaimage.py +97 -0
- morphogen/io/mfront.py +1104 -0
- morphogen/io/native.py +57 -0
- morphogen/io/radioss.py +764 -0
- morphogen/io/solvers.py +371 -0
- morphogen/io/tables.py +278 -0
- morphogen/io/tiff.py +66 -0
- morphogen/io/vtk.py +701 -0
- morphogen/io/xdmf.py +200 -0
- morphogen/kernels/__init__.py +5 -0
- morphogen/kernels/annealing.py +192 -0
- morphogen/kernels/cells.py +69 -0
- morphogen/kernels/collective.py +87 -0
- morphogen/kernels/force_biased.py +595 -0
- morphogen/kernels/implicit.py +145 -0
- morphogen/kernels/leaves.py +132 -0
- morphogen/kernels/packing.py +304 -0
- morphogen/kernels/raster.py +279 -0
- morphogen/kernels/rsa.py +371 -0
- morphogen/kernels/union.py +135 -0
- morphogen/kernels/watershed.py +190 -0
- morphogen/laser/__init__.py +234 -0
- morphogen/laser/architectures.py +375 -0
- morphogen/laser/bending.py +216 -0
- morphogen/laser/cells.py +1694 -0
- morphogen/laser/design.py +940 -0
- morphogen/laser/figures.py +227 -0
- morphogen/laser/heat.py +804 -0
- morphogen/laser/materials.py +691 -0
- morphogen/laser/mechanics.py +1670 -0
- morphogen/laser/metallurgy.py +615 -0
- morphogen/laser/patterns.py +828 -0
- morphogen/laser/periodic.py +392 -0
- morphogen/laser/process.py +362 -0
- morphogen/laser/run.py +534 -0
- morphogen/laser/solid.py +216 -0
- morphogen/laser/specimen.py +200 -0
- morphogen/laser/structures.py +708 -0
- morphogen/learning/__init__.py +81 -0
- morphogen/learning/behaviour.py +398 -0
- morphogen/learning/data.py +328 -0
- morphogen/learning/learned.cpp +668 -0
- morphogen/learning/logarithmic.py +202 -0
- morphogen/learning/mfront.py +409 -0
- morphogen/learning/networks.py +767 -0
- morphogen/learning/run.py +212 -0
- morphogen/learning/standard.py +814 -0
- morphogen/learning/training.py +235 -0
- morphogen/mesh/__init__.py +29 -0
- morphogen/mesh/conforming.py +1423 -0
- morphogen/mesh/curved.py +600 -0
- morphogen/mesh/exact.py +1717 -0
- morphogen/mesh/octree.py +350 -0
- morphogen/mesh/polycrystal.py +1041 -0
- morphogen/mesh/polyhedra.py +677 -0
- morphogen/mesh/regularize.py +570 -0
- morphogen/mesh/surface.py +97 -0
- morphogen/mesh/tetra.py +512 -0
- morphogen/mesh/voxel.py +124 -0
- morphogen/micromorphic/__init__.py +31 -0
- morphogen/micromorphic/damage.py +359 -0
- morphogen/micromorphic/ductile.py +534 -0
- morphogen/micromorphic/finite.py +220 -0
- morphogen/micromorphic/microfoam.py +787 -0
- morphogen/micromorphic/space.py +341 -0
- morphogen/models/__init__.py +195 -0
- morphogen/models/affine.py +384 -0
- morphogen/models/automata.py +592 -0
- morphogen/models/base.py +500 -0
- morphogen/models/boolean.py +310 -0
- morphogen/models/bouligand.py +331 -0
- morphogen/models/branching.py +241 -0
- morphogen/models/combination.py +430 -0
- morphogen/models/deadleaves.py +766 -0
- morphogen/models/design.py +289 -0
- morphogen/models/ebsd.py +206 -0
- morphogen/models/fibers.py +1476 -0
- morphogen/models/foam.py +594 -0
- morphogen/models/gaussian.py +314 -0
- morphogen/models/growth.py +459 -0
- morphogen/models/hardcore.py +317 -0
- morphogen/models/hierarchical.py +269 -0
- morphogen/models/honeycomb.py +749 -0
- morphogen/models/implicit.py +177 -0
- morphogen/models/laminate.py +470 -0
- morphogen/models/lattice.py +351 -0
- morphogen/models/lattice_field.py +345 -0
- morphogen/models/levelset_foam.py +563 -0
- morphogen/models/nacre.py +411 -0
- morphogen/models/nested.py +924 -0
- morphogen/models/osteons.py +374 -0
- morphogen/models/pieces.py +167 -0
- morphogen/models/planar.py +126 -0
- morphogen/models/reconstruction.py +203 -0
- morphogen/models/scale_rich.py +1409 -0
- morphogen/models/scale_rich_3d.py +1616 -0
- morphogen/models/tessellation.py +735 -0
- morphogen/models/tiling_field.py +302 -0
- morphogen/models/tpms.py +197 -0
- morphogen/models/transparent.py +406 -0
- morphogen/models/union.py +141 -0
- morphogen/optimization/__init__.py +43 -0
- morphogen/optimization/cell.py +295 -0
- morphogen/optimization/filters.py +115 -0
- morphogen/optimization/mma.py +226 -0
- morphogen/optimization/run.py +219 -0
- morphogen/optimization/topology.py +632 -0
- morphogen/orientations.py +568 -0
- morphogen/parallel.py +599 -0
- morphogen/pipeline.py +978 -0
- morphogen/primitives.py +188 -0
- morphogen/results.py +564 -0
- morphogen/rve/__init__.py +73 -0
- morphogen/rve/report.py +627 -0
- morphogen/rve/scaling.py +363 -0
- morphogen/rve/study.py +1054 -0
- morphogen/rve/windows.py +345 -0
- morphogen/seeding.py +51 -0
- morphogen/settings.py +118 -0
- morphogen/stopping.py +48 -0
- morphogen/symmetry.py +384 -0
- morphogen/templates/activator_inhibitor.yaml +22 -0
- morphogen/templates/aggregation.yaml +23 -0
- morphogen/templates/boolean_cylinders.yaml +20 -0
- morphogen/templates/boolean_spheres.yaml +22 -0
- morphogen/templates/bouligand.yaml +28 -0
- morphogen/templates/cellular_automaton.yaml +25 -0
- morphogen/templates/coated_spheres.yaml +26 -0
- morphogen/templates/contact_fibers.yaml +27 -0
- morphogen/templates/cox_nanocomposite.yaml +22 -0
- morphogen/templates/cross_linked_fibers.yaml +26 -0
- morphogen/templates/curved_arcs.yaml +28 -0
- morphogen/templates/curved_fibers.yaml +24 -0
- morphogen/templates/dead_leaves.yaml +22 -0
- morphogen/templates/delaunay_network.yaml +22 -0
- morphogen/templates/dense_packing.yaml +22 -0
- morphogen/templates/diffusion.yaml +33 -0
- morphogen/templates/ebsd.yaml +34 -0
- morphogen/templates/ebsd_map.ctf +3087 -0
- morphogen/templates/fracture_composite.yaml +42 -0
- morphogen/templates/fracture_rubber.yaml +47 -0
- morphogen/templates/gaussian_excursion.yaml +17 -0
- morphogen/templates/graded_lattice.yaml +24 -0
- morphogen/templates/hardcore_fibers.yaml +22 -0
- morphogen/templates/hardcore_spheres.yaml +22 -0
- morphogen/templates/hierarchical_lattice.yaml +22 -0
- morphogen/templates/homogenization_acoustic_foam.yaml +36 -0
- morphogen/templates/homogenization_beremin.yaml +50 -0
- morphogen/templates/homogenization_cleavage.yaml +51 -0
- morphogen/templates/homogenization_cleavage_band.yaml +55 -0
- morphogen/templates/homogenization_contact.yaml +43 -0
- morphogen/templates/homogenization_crack_closure.yaml +49 -0
- morphogen/templates/homogenization_crystal_plasticity.yaml +50 -0
- morphogen/templates/homogenization_crystal_plasticity_fft.yaml +52 -0
- morphogen/templates/homogenization_crystallization.yaml +30 -0
- morphogen/templates/homogenization_debonding.yaml +43 -0
- morphogen/templates/homogenization_detwinning.yaml +58 -0
- morphogen/templates/homogenization_ductile_damage.yaml +47 -0
- morphogen/templates/homogenization_fatigue.yaml +40 -0
- morphogen/templates/homogenization_grain_fracture.yaml +56 -0
- morphogen/templates/homogenization_gyroid.yaml +30 -0
- morphogen/templates/homogenization_intergranular.yaml +43 -0
- morphogen/templates/homogenization_interphase.yaml +42 -0
- morphogen/templates/homogenization_mixed_loading.yaml +33 -0
- morphogen/templates/homogenization_ntfa.yaml +56 -0
- morphogen/templates/homogenization_piezoelectric.yaml +27 -0
- morphogen/templates/homogenization_polycrystal.yaml +30 -0
- morphogen/templates/homogenization_polycrystal_inclusions.yaml +30 -0
- morphogen/templates/homogenization_poroelastic.yaml +26 -0
- morphogen/templates/homogenization_rubber.yaml +40 -0
- morphogen/templates/homogenization_slip_cleavage.yaml +50 -0
- morphogen/templates/homogenization_spheres.yaml +31 -0
- morphogen/templates/homogenization_transport.yaml +29 -0
- morphogen/templates/homogenization_twinning.yaml +54 -0
- morphogen/templates/homogenization_viscoelastic.yaml +34 -0
- morphogen/templates/homogenization_viscoplastic.yaml +42 -0
- morphogen/templates/homogenization_voronoi.yaml +26 -0
- morphogen/templates/homogenization_xfem.yaml +23 -0
- morphogen/templates/hyperuniform_cells.yaml +24 -0
- morphogen/templates/laguerre.yaml +18 -0
- morphogen/templates/laser_bending.yaml +35 -0
- morphogen/templates/laser_cells.yaml +38 -0
- morphogen/templates/laser_corrugated.yaml +36 -0
- morphogen/templates/level_set_foam.yaml +29 -0
- morphogen/templates/menger_sponge.yaml +17 -0
- morphogen/templates/nacre.yaml +25 -0
- morphogen/templates/nucleation_growth.yaml +25 -0
- morphogen/templates/optimize_auxetic.yaml +29 -0
- morphogen/templates/optimize_auxetic_3d.yaml +27 -0
- morphogen/templates/optimize_bulk.yaml +23 -0
- morphogen/templates/optimize_conductivity.yaml +20 -0
- morphogen/templates/osteons.yaml +28 -0
- morphogen/templates/penetrable_spheres.yaml +21 -0
- morphogen/templates/poisson_fibers.yaml +21 -0
- morphogen/templates/random_laminate.yaml +26 -0
- morphogen/templates/random_lattice.yaml +27 -0
- morphogen/templates/random_walk_fibers.yaml +26 -0
- morphogen/templates/reaction_diffusion.yaml +22 -0
- morphogen/templates/reconstruction.yaml +22 -0
- morphogen/templates/rve_cellular_automaton.yaml +23 -0
- morphogen/templates/rve_crystal_plasticity_fft.yaml +51 -0
- morphogen/templates/rve_multiphysics.yaml +43 -0
- morphogen/templates/rve_poisson_fibers.yaml +22 -0
- morphogen/templates/rve_poisson_fibers_fe.yaml +33 -0
- morphogen/templates/rve_voronoi_windows.yaml +20 -0
- morphogen/templates/scale_rich_network.yaml +29 -0
- morphogen/templates/scale_rich_plates.yaml +30 -0
- morphogen/templates/sequential_dead_leaves.yaml +25 -0
- morphogen/templates/strut_lattice.yaml +21 -0
- morphogen/templates/tile_assembly.yaml +28 -0
- morphogen/templates/tpms.yaml +19 -0
- morphogen/templates/trabecular_bone.yaml +29 -0
- morphogen/templates/transparent_dead_leaves.yaml +23 -0
- morphogen/templates/vascular_tree.yaml +23 -0
- morphogen/templates/voronoi.yaml +20 -0
- morphogen/templates/wood.yaml +34 -0
- morphogen/templates/workflow.yaml +56 -0
- morphogen/texture.py +665 -0
- morphogen/theory.py +162 -0
- morphogen/viz/__init__.py +12 -0
- morphogen/viz/export.py +244 -0
- morphogen/viz/figures.py +707 -0
- morphogen/viz/png.py +148 -0
- morphogen/viz/sections.py +696 -0
- morphogen/viz/surfaces.py +1352 -0
- morphogen/viz/video.py +904 -0
- morphogen/viz/workflow.py +488 -0
- morphogen-1.0.0.dist-info/METADATA +2316 -0
- morphogen-1.0.0.dist-info/RECORD +381 -0
- morphogen-1.0.0.dist-info/WHEEL +4 -0
- morphogen-1.0.0.dist-info/entry_points.txt +5 -0
- morphogen-1.0.0.dist-info/licenses/LICENSE +674 -0
|
@@ -0,0 +1,828 @@
|
|
|
1
|
+
"""Laser paths: the patterns of tracks that architecture a sheet.
|
|
2
|
+
|
|
3
|
+
A :class:`LaserPath` is a sequence of :class:`Track` s, polylines (mm) that the beams
|
|
4
|
+
follow in order, each with its beams (the upper one, and the lower one of a double-sided
|
|
5
|
+
treatment) and the pause that follows it (the cooling before the next track, at least
|
|
6
|
+
30 s between neighbouring tracks in Wang's experiments). Coordinates are those of the
|
|
7
|
+
sheet: ``x`` along the tensile axis (the rolling direction), ``y`` across.
|
|
8
|
+
|
|
9
|
+
Generators (:data:`PATTERNS`, :func:`make_pattern`), for a band ``[x0, x1] x [y0, y1]``
|
|
10
|
+
(the tracks overhang the band along x by ``margin``, the specimen being cut afterwards,
|
|
11
|
+
so that no track starts or stops in it):
|
|
12
|
+
|
|
13
|
+
``straight``
|
|
14
|
+
``count`` parallel tracks at ``angle`` (degrees from x), at ``spacing`` (default:
|
|
15
|
+
the band's width over ``count``), centred;
|
|
16
|
+
``corrugated``
|
|
17
|
+
``count`` tracks ``y = y_i + A sin(2π x/P + φ_i)`` (``amplitude`` ``A``, the height
|
|
18
|
+
of Wang et al., ``period`` ``P``), ``phase`` the shift between neighbours (0: in
|
|
19
|
+
phase, 180: mirrored), ``shape`` ``sine`` or ``triangle`` (zigzag);
|
|
20
|
+
``fourier``
|
|
21
|
+
the same with the waveform ``Σ a_k sin(2πkx/P) + b_k cos(2πkx/P)`` of
|
|
22
|
+
``coefficients`` ``[[a1, b1], [a2, b2], ...]``: the free shapes of an optimization;
|
|
23
|
+
``crossed``
|
|
24
|
+
two families of straight tracks at ``±angle`` (a rhombic or square grid), the
|
|
25
|
+
second family after the first, at ``spacing`` or at its own ``spacing2`` (a
|
|
26
|
+
rectangular grid: more tracks one way than the other);
|
|
27
|
+
``honeycomb``
|
|
28
|
+
the edges of hexagons of side ``cell``, drawn as zigzag rows and the rungs between
|
|
29
|
+
them;
|
|
30
|
+
``spiral``
|
|
31
|
+
an Archimedean spiral ``r = radius + pitch θ/2π`` about ``center`` (the centre of
|
|
32
|
+
the band by default), ``turns`` turns from the direction ``angle``, counter-clockwise
|
|
33
|
+
or ``clockwise``: one track (the path of a crack to guide);
|
|
34
|
+
``polylines``
|
|
35
|
+
given ``tracks`` (lists of points).
|
|
36
|
+
|
|
37
|
+
Measures: length, time and energy of the path, the smallest radius of curvature
|
|
38
|
+
(:meth:`LaserPath.min_radius`) and the smallest gap between distinct tracks
|
|
39
|
+
(:meth:`LaserPath.min_gap`); :meth:`LaserPath.to_gcode` writes it for a machine.
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
from __future__ import annotations
|
|
43
|
+
|
|
44
|
+
import itertools
|
|
45
|
+
import math
|
|
46
|
+
from dataclasses import dataclass, field, replace
|
|
47
|
+
|
|
48
|
+
import numpy as np
|
|
49
|
+
from numba import njit, prange
|
|
50
|
+
|
|
51
|
+
from morphogen.laser.heat import Beam
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@dataclass
|
|
55
|
+
class Track:
|
|
56
|
+
"""A polyline (n x 2, mm) followed by ``beam`` (upper face) and ``bottom`` (lower
|
|
57
|
+
face, or None), then ``pause`` seconds lasers off."""
|
|
58
|
+
|
|
59
|
+
points: np.ndarray
|
|
60
|
+
beam: Beam
|
|
61
|
+
bottom: Beam | None = None
|
|
62
|
+
pause: float = 30.0
|
|
63
|
+
|
|
64
|
+
def __post_init__(self):
|
|
65
|
+
self.points = np.ascontiguousarray(np.asarray(self.points, dtype=float).reshape(-1, 2))
|
|
66
|
+
if len(self.points) < 2:
|
|
67
|
+
raise ValueError("a track needs at least two points")
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def length(self) -> float:
|
|
71
|
+
d = np.diff(self.points, axis=0)
|
|
72
|
+
return float(np.hypot(d[:, 0], d[:, 1]).sum())
|
|
73
|
+
|
|
74
|
+
@property
|
|
75
|
+
def time(self) -> float:
|
|
76
|
+
"""Time with the lasers on (s)."""
|
|
77
|
+
return self.length / self.beam.speed
|
|
78
|
+
|
|
79
|
+
@property
|
|
80
|
+
def energy(self) -> float:
|
|
81
|
+
"""Incident energy of both beams (J)."""
|
|
82
|
+
power = self.beam.power + (self.bottom.power if self.bottom is not None else 0.0)
|
|
83
|
+
return power * self.time
|
|
84
|
+
|
|
85
|
+
def shifted(self, dx: float = 0.0, dy: float = 0.0) -> Track:
|
|
86
|
+
return replace(self, points=self.points + np.array([dx, dy]))
|
|
87
|
+
|
|
88
|
+
def reversed(self) -> Track:
|
|
89
|
+
return replace(self, points=self.points[::-1].copy())
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@dataclass
|
|
93
|
+
class LaserPath:
|
|
94
|
+
"""Tracks in the order of the treatment."""
|
|
95
|
+
|
|
96
|
+
tracks: list = field(default_factory=list)
|
|
97
|
+
|
|
98
|
+
def __len__(self) -> int:
|
|
99
|
+
return len(self.tracks)
|
|
100
|
+
|
|
101
|
+
def __iter__(self):
|
|
102
|
+
return iter(self.tracks)
|
|
103
|
+
|
|
104
|
+
@property
|
|
105
|
+
def length(self) -> float:
|
|
106
|
+
return sum(t.length for t in self.tracks)
|
|
107
|
+
|
|
108
|
+
@property
|
|
109
|
+
def time(self) -> float:
|
|
110
|
+
"""Treatment time (s): lasers on, and the pauses between tracks."""
|
|
111
|
+
return sum(t.time for t in self.tracks) + sum(t.pause for t in self.tracks[:-1])
|
|
112
|
+
|
|
113
|
+
@property
|
|
114
|
+
def energy(self) -> float:
|
|
115
|
+
return sum(t.energy for t in self.tracks)
|
|
116
|
+
|
|
117
|
+
def segments(self):
|
|
118
|
+
"""``(points, beam, bottom, pause)`` of each track, for
|
|
119
|
+
:meth:`~morphogen.laser.heat.TransientHeat.run`."""
|
|
120
|
+
return [(t.points, t.beam, t.bottom, t.pause) for t in self.tracks]
|
|
121
|
+
|
|
122
|
+
def bounds(self) -> tuple[np.ndarray, np.ndarray]:
|
|
123
|
+
pts = np.vstack([t.points for t in self.tracks])
|
|
124
|
+
return pts.min(axis=0), pts.max(axis=0)
|
|
125
|
+
|
|
126
|
+
def clipped_length(self, lo, hi) -> float:
|
|
127
|
+
"""Length of the tracks inside the box ``[lo, hi]`` (the specimen)."""
|
|
128
|
+
lo, hi = np.asarray(lo, float), np.asarray(hi, float)
|
|
129
|
+
total = 0.0
|
|
130
|
+
for t in self.tracks:
|
|
131
|
+
p = t.points
|
|
132
|
+
for a, b in itertools.pairwise(p):
|
|
133
|
+
total += _clip_segment(a, b, lo, hi)
|
|
134
|
+
return total
|
|
135
|
+
|
|
136
|
+
def min_radius(self) -> float:
|
|
137
|
+
"""Smallest radius of curvature of the tracks (the circumradius of consecutive
|
|
138
|
+
triplets of points; inf for straight tracks)."""
|
|
139
|
+
best = math.inf
|
|
140
|
+
for t in self.tracks:
|
|
141
|
+
p = t.points
|
|
142
|
+
if len(p) < 3:
|
|
143
|
+
continue
|
|
144
|
+
a, b, c = p[:-2], p[1:-1], p[2:]
|
|
145
|
+
ab = np.hypot(*(b - a).T)
|
|
146
|
+
bc = np.hypot(*(c - b).T)
|
|
147
|
+
ca = np.hypot(*(a - c).T)
|
|
148
|
+
cross = np.abs((b - a)[:, 0] * (c - a)[:, 1] - (b - a)[:, 1] * (c - a)[:, 0])
|
|
149
|
+
with np.errstate(divide="ignore", invalid="ignore"):
|
|
150
|
+
r = ab * bc * ca / (2.0 * cross)
|
|
151
|
+
r = r[np.isfinite(r)]
|
|
152
|
+
if r.size:
|
|
153
|
+
best = min(best, float(r.min()))
|
|
154
|
+
return best
|
|
155
|
+
|
|
156
|
+
def min_gap(self, lo=None, hi=None) -> float:
|
|
157
|
+
"""Smallest distance between two distinct tracks (within the box ``[lo, hi]``
|
|
158
|
+
if given): the tracks closer than their width overlap and temper each other."""
|
|
159
|
+
best = math.inf
|
|
160
|
+
pts = [t.points for t in self.tracks]
|
|
161
|
+
for i in range(len(pts)):
|
|
162
|
+
sample = _densify(pts[i], 0.1)
|
|
163
|
+
if lo is not None:
|
|
164
|
+
inside = np.all((sample >= np.asarray(lo)) & (sample <= np.asarray(hi)), axis=1)
|
|
165
|
+
sample = sample[inside]
|
|
166
|
+
if not len(sample):
|
|
167
|
+
continue
|
|
168
|
+
for j in range(len(pts)):
|
|
169
|
+
if j == i:
|
|
170
|
+
continue
|
|
171
|
+
d = distance_to_polyline(sample, pts[j])
|
|
172
|
+
best = min(best, float(d.min()))
|
|
173
|
+
return best
|
|
174
|
+
|
|
175
|
+
def to_gcode(self, *, laser_on: str = "M3", laser_off: str = "M5") -> str:
|
|
176
|
+
"""The path as G-code (mm, mm/min): rapid moves to the starts of the tracks, the
|
|
177
|
+
lasers on (``S``: the power of the upper beam, W; a comment gives the lower one),
|
|
178
|
+
linear moves at the speed of the track, the lasers off, a dwell for the pause."""
|
|
179
|
+
lines = ["; morphoGen laser path", "G21 ; mm", "G90 ; absolute"]
|
|
180
|
+
for k, t in enumerate(self.tracks):
|
|
181
|
+
p = t.points
|
|
182
|
+
lines.append(
|
|
183
|
+
f"; track {k}: {t.length:.3f} mm"
|
|
184
|
+
+ (f", lower beam {t.bottom.power:g} W" if t.bottom is not None else "")
|
|
185
|
+
)
|
|
186
|
+
lines.append(f"G0 X{p[0, 0]:.4f} Y{p[0, 1]:.4f}")
|
|
187
|
+
lines.append(f"{laser_on} S{t.beam.power:g}")
|
|
188
|
+
feed = t.beam.speed * 60.0
|
|
189
|
+
for x, y in p[1:]:
|
|
190
|
+
lines.append(f"G1 X{x:.4f} Y{y:.4f} F{feed:.1f}")
|
|
191
|
+
lines.append(laser_off)
|
|
192
|
+
if t.pause > 0 and k < len(self.tracks) - 1:
|
|
193
|
+
lines.append(f"G4 P{t.pause:g}")
|
|
194
|
+
lines.append("M2")
|
|
195
|
+
return "\n".join(lines) + "\n"
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def _clip_segment(a, b, lo, hi) -> float:
|
|
199
|
+
"""Length of the segment ``ab`` inside the box (Liang–Barsky)."""
|
|
200
|
+
d = b - a
|
|
201
|
+
t0, t1 = 0.0, 1.0
|
|
202
|
+
for p, q in (
|
|
203
|
+
(-d[0], a[0] - lo[0]),
|
|
204
|
+
(d[0], hi[0] - a[0]),
|
|
205
|
+
(-d[1], a[1] - lo[1]),
|
|
206
|
+
(d[1], hi[1] - a[1]),
|
|
207
|
+
):
|
|
208
|
+
if p == 0:
|
|
209
|
+
if q < 0:
|
|
210
|
+
return 0.0
|
|
211
|
+
continue
|
|
212
|
+
r = q / p
|
|
213
|
+
if p < 0:
|
|
214
|
+
t0 = max(t0, r)
|
|
215
|
+
else:
|
|
216
|
+
t1 = min(t1, r)
|
|
217
|
+
if t0 > t1:
|
|
218
|
+
return 0.0
|
|
219
|
+
return float((t1 - t0) * math.hypot(d[0], d[1]))
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def _densify(points, step: float) -> np.ndarray:
|
|
223
|
+
out = [points[:1]]
|
|
224
|
+
for a, b in itertools.pairwise(points):
|
|
225
|
+
n = max(1, math.ceil(math.hypot(*(b - a)) / step))
|
|
226
|
+
t = (np.arange(1, n + 1) / n)[:, None]
|
|
227
|
+
out.append(a + t * (b - a))
|
|
228
|
+
return np.vstack(out)
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
@njit(parallel=True, cache=True)
|
|
232
|
+
def _distances(points, poly, out): # pragma: no cover - compiled
|
|
233
|
+
n, m = points.shape[0], poly.shape[0]
|
|
234
|
+
for i in prange(n):
|
|
235
|
+
px, py = points[i, 0], points[i, 1]
|
|
236
|
+
best = 1e300
|
|
237
|
+
for k in range(m - 1):
|
|
238
|
+
ax, ay = poly[k, 0], poly[k, 1]
|
|
239
|
+
bx, by = poly[k + 1, 0], poly[k + 1, 1]
|
|
240
|
+
dx, dy = bx - ax, by - ay
|
|
241
|
+
ll = dx * dx + dy * dy
|
|
242
|
+
if ll > 0:
|
|
243
|
+
t = ((px - ax) * dx + (py - ay) * dy) / ll
|
|
244
|
+
t = min(1.0, max(0.0, t))
|
|
245
|
+
else:
|
|
246
|
+
t = 0.0
|
|
247
|
+
ex, ey = ax + t * dx - px, ay + t * dy - py
|
|
248
|
+
d2 = ex * ex + ey * ey
|
|
249
|
+
if d2 < best:
|
|
250
|
+
best = d2
|
|
251
|
+
out[i] = math.sqrt(best)
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def distance_to_polyline(points, polyline) -> np.ndarray:
|
|
255
|
+
"""Distance (mm) of points (n x 2) to a polyline (m x 2)."""
|
|
256
|
+
p = np.ascontiguousarray(np.asarray(points, dtype=float).reshape(-1, 2))
|
|
257
|
+
poly = np.ascontiguousarray(np.asarray(polyline, dtype=float).reshape(-1, 2))
|
|
258
|
+
out = np.empty(len(p))
|
|
259
|
+
if len(poly) == 1:
|
|
260
|
+
return np.hypot(p[:, 0] - poly[0, 0], p[:, 1] - poly[0, 1])
|
|
261
|
+
_distances(p, poly, out)
|
|
262
|
+
return out
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
# -- generators ----------------------------------------------------------------------------
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def _positions(count: int, y0: float, y1: float, spacing: float | None, offset: float):
|
|
269
|
+
if count < 1:
|
|
270
|
+
raise ValueError("count: at least one track")
|
|
271
|
+
centre = 0.5 * (y0 + y1) + offset
|
|
272
|
+
s = (y1 - y0) / count if spacing is None else float(spacing)
|
|
273
|
+
return centre + (np.arange(count) - 0.5 * (count - 1)) * s
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def _waveform(x, period: float, amplitude: float, phase: float, shape: str, coefficients):
|
|
277
|
+
u = 2.0 * math.pi * x / period + phase
|
|
278
|
+
if coefficients is not None:
|
|
279
|
+
out = np.zeros_like(x)
|
|
280
|
+
for k, (a, b) in enumerate(np.asarray(coefficients, dtype=float).reshape(-1, 2), start=1):
|
|
281
|
+
out += a * np.sin(k * u) + b * np.cos(k * u)
|
|
282
|
+
return out
|
|
283
|
+
if shape == "sine":
|
|
284
|
+
return amplitude * np.sin(u)
|
|
285
|
+
if shape == "triangle":
|
|
286
|
+
return amplitude * (2.0 / math.pi) * np.arcsin(np.sin(u))
|
|
287
|
+
raise ValueError("shape: sine or triangle")
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def _wavy(
|
|
291
|
+
x0,
|
|
292
|
+
x1,
|
|
293
|
+
y0,
|
|
294
|
+
y1,
|
|
295
|
+
*,
|
|
296
|
+
count,
|
|
297
|
+
period,
|
|
298
|
+
amplitude=0.0,
|
|
299
|
+
phase=0.0,
|
|
300
|
+
shape="sine",
|
|
301
|
+
coefficients=None,
|
|
302
|
+
spacing=None,
|
|
303
|
+
offset=0.0,
|
|
304
|
+
margin=5.0,
|
|
305
|
+
resolution=24,
|
|
306
|
+
alternate=True,
|
|
307
|
+
beam,
|
|
308
|
+
bottom,
|
|
309
|
+
pause,
|
|
310
|
+
):
|
|
311
|
+
if not period > 0:
|
|
312
|
+
raise ValueError("period must be positive")
|
|
313
|
+
xs = np.linspace(
|
|
314
|
+
x0 - margin,
|
|
315
|
+
x1 + margin,
|
|
316
|
+
max(2, math.ceil((x1 - x0 + 2 * margin) / period * resolution) + 1),
|
|
317
|
+
)
|
|
318
|
+
if shape == "triangle" and coefficients is None: # exact corners
|
|
319
|
+
u0 = (x0 - margin) / period
|
|
320
|
+
corners = (
|
|
321
|
+
np.arange(
|
|
322
|
+
math.floor(4 * u0 - 4 * phase / (2 * math.pi)) - 1,
|
|
323
|
+
math.ceil(4 * (x1 + margin) / period) + 2,
|
|
324
|
+
)
|
|
325
|
+
+ 1
|
|
326
|
+
)
|
|
327
|
+
xc = (corners / 4.0 - phase / (2 * math.pi)) * period
|
|
328
|
+
xs = np.unique(np.concatenate([xs, xc[(xc > xs[0]) & (xc < xs[-1])]]))
|
|
329
|
+
tracks = []
|
|
330
|
+
for i, yc in enumerate(_positions(count, y0, y1, spacing, offset)):
|
|
331
|
+
y = yc + _waveform(xs, period, amplitude, math.radians(phase) * i, shape, coefficients)
|
|
332
|
+
t = Track(np.column_stack([xs, y]), beam, bottom, pause)
|
|
333
|
+
tracks.append(t.reversed() if alternate and i % 2 else t)
|
|
334
|
+
return LaserPath(tracks)
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
def _lines(
|
|
338
|
+
x0,
|
|
339
|
+
x1,
|
|
340
|
+
y0,
|
|
341
|
+
y1,
|
|
342
|
+
*,
|
|
343
|
+
angle,
|
|
344
|
+
spacing,
|
|
345
|
+
count=None,
|
|
346
|
+
offset=0.0,
|
|
347
|
+
margin=5.0,
|
|
348
|
+
alternate=True,
|
|
349
|
+
beam,
|
|
350
|
+
bottom,
|
|
351
|
+
pause,
|
|
352
|
+
):
|
|
353
|
+
"""Parallel straight tracks at ``angle`` covering the band (plus margin)."""
|
|
354
|
+
th = math.radians(angle)
|
|
355
|
+
u = np.array([math.cos(th), math.sin(th)]) # along the tracks
|
|
356
|
+
nrm = np.array([-u[1], u[0]])
|
|
357
|
+
cx, cy = 0.5 * (x0 + x1), 0.5 * (y0 + y1)
|
|
358
|
+
corners = np.array([[x0 - margin, y0], [x1 + margin, y0], [x0 - margin, y1], [x1 + margin, y1]])
|
|
359
|
+
proj_n = (corners - [cx, cy]) @ nrm
|
|
360
|
+
proj_u = (corners - [cx, cy]) @ u
|
|
361
|
+
if count is not None and spacing is None:
|
|
362
|
+
width = proj_n.max() - proj_n.min() if abs(math.sin(th)) > 1e-12 else (y1 - y0)
|
|
363
|
+
spacing = width / count
|
|
364
|
+
if spacing is None or not spacing > 0:
|
|
365
|
+
raise ValueError("spacing (or count) of the tracks")
|
|
366
|
+
if count is None:
|
|
367
|
+
k0 = math.ceil((proj_n.min() - offset) / spacing - 0.5)
|
|
368
|
+
k1 = math.floor((proj_n.max() - offset) / spacing - 0.5)
|
|
369
|
+
offsets = offset + (np.arange(k0, k1 + 1) + 0.5) * spacing
|
|
370
|
+
else:
|
|
371
|
+
offsets = offset + (np.arange(count) - 0.5 * (count - 1)) * spacing
|
|
372
|
+
half = 0.5 * (proj_u.max() - proj_u.min()) + margin
|
|
373
|
+
lo = np.array([x0 - margin, y0 - margin])
|
|
374
|
+
hi = np.array([x1 + margin, y1 + margin])
|
|
375
|
+
tracks = []
|
|
376
|
+
for o in offsets:
|
|
377
|
+
c = np.array([cx, cy]) + o * nrm
|
|
378
|
+
a, b = c - half * u, c + half * u
|
|
379
|
+
seg = _clip_points(a, b, lo, hi)
|
|
380
|
+
if seg is not None:
|
|
381
|
+
tracks.append(Track(np.array(seg), beam, bottom, pause))
|
|
382
|
+
if alternate:
|
|
383
|
+
tracks = [t.reversed() if i % 2 else t for i, t in enumerate(tracks)]
|
|
384
|
+
return LaserPath(tracks)
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
def _clip_points(a, b, lo, hi):
|
|
388
|
+
"""The part of the segment ``ab`` inside the box, or None (Liang–Barsky)."""
|
|
389
|
+
d = b - a
|
|
390
|
+
t0, t1 = 0.0, 1.0
|
|
391
|
+
for p, q in (
|
|
392
|
+
(-d[0], a[0] - lo[0]),
|
|
393
|
+
(d[0], hi[0] - a[0]),
|
|
394
|
+
(-d[1], a[1] - lo[1]),
|
|
395
|
+
(d[1], hi[1] - a[1]),
|
|
396
|
+
):
|
|
397
|
+
if p == 0:
|
|
398
|
+
if q < 0:
|
|
399
|
+
return None
|
|
400
|
+
continue
|
|
401
|
+
r = q / p
|
|
402
|
+
if p < 0:
|
|
403
|
+
t0 = max(t0, r)
|
|
404
|
+
else:
|
|
405
|
+
t1 = min(t1, r)
|
|
406
|
+
if t0 >= t1:
|
|
407
|
+
return None
|
|
408
|
+
return a + t0 * d, a + t1 * d
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
def straight(
|
|
412
|
+
x0,
|
|
413
|
+
x1,
|
|
414
|
+
y0,
|
|
415
|
+
y1,
|
|
416
|
+
*,
|
|
417
|
+
count=1,
|
|
418
|
+
angle=0.0,
|
|
419
|
+
spacing=None,
|
|
420
|
+
offset=0.0,
|
|
421
|
+
margin=5.0,
|
|
422
|
+
alternate=True,
|
|
423
|
+
beam,
|
|
424
|
+
bottom=None,
|
|
425
|
+
pause=30.0,
|
|
426
|
+
) -> LaserPath:
|
|
427
|
+
"""Parallel straight tracks (see the module)."""
|
|
428
|
+
if abs(math.sin(math.radians(angle))) < 1e-12:
|
|
429
|
+
if count is None: # a family at its spacing over the band
|
|
430
|
+
if not spacing:
|
|
431
|
+
raise ValueError("straight: a count or a spacing")
|
|
432
|
+
count = max(1, math.floor((y1 - y0) / float(spacing) + 1e-9))
|
|
433
|
+
return _wavy(
|
|
434
|
+
x0,
|
|
435
|
+
x1,
|
|
436
|
+
y0,
|
|
437
|
+
y1,
|
|
438
|
+
count=count,
|
|
439
|
+
period=x1 - x0 + 2 * margin,
|
|
440
|
+
spacing=spacing,
|
|
441
|
+
offset=offset,
|
|
442
|
+
margin=margin,
|
|
443
|
+
resolution=1,
|
|
444
|
+
alternate=alternate,
|
|
445
|
+
beam=beam,
|
|
446
|
+
bottom=bottom,
|
|
447
|
+
pause=pause,
|
|
448
|
+
)
|
|
449
|
+
return _lines(
|
|
450
|
+
x0,
|
|
451
|
+
x1,
|
|
452
|
+
y0,
|
|
453
|
+
y1,
|
|
454
|
+
angle=angle,
|
|
455
|
+
spacing=spacing,
|
|
456
|
+
count=count,
|
|
457
|
+
offset=offset,
|
|
458
|
+
margin=margin,
|
|
459
|
+
alternate=alternate,
|
|
460
|
+
beam=beam,
|
|
461
|
+
bottom=bottom,
|
|
462
|
+
pause=pause,
|
|
463
|
+
)
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
def corrugated(
|
|
467
|
+
x0,
|
|
468
|
+
x1,
|
|
469
|
+
y0,
|
|
470
|
+
y1,
|
|
471
|
+
*,
|
|
472
|
+
count=1,
|
|
473
|
+
amplitude=1.0,
|
|
474
|
+
period=10.0,
|
|
475
|
+
phase=0.0,
|
|
476
|
+
shape="sine",
|
|
477
|
+
spacing=None,
|
|
478
|
+
offset=0.0,
|
|
479
|
+
margin=5.0,
|
|
480
|
+
resolution=24,
|
|
481
|
+
alternate=True,
|
|
482
|
+
beam,
|
|
483
|
+
bottom=None,
|
|
484
|
+
pause=30.0,
|
|
485
|
+
) -> LaserPath:
|
|
486
|
+
"""Corrugated tracks (see the module)."""
|
|
487
|
+
return _wavy(
|
|
488
|
+
x0,
|
|
489
|
+
x1,
|
|
490
|
+
y0,
|
|
491
|
+
y1,
|
|
492
|
+
count=count,
|
|
493
|
+
period=period,
|
|
494
|
+
amplitude=amplitude,
|
|
495
|
+
phase=phase,
|
|
496
|
+
shape=shape,
|
|
497
|
+
spacing=spacing,
|
|
498
|
+
offset=offset,
|
|
499
|
+
margin=margin,
|
|
500
|
+
resolution=resolution,
|
|
501
|
+
alternate=alternate,
|
|
502
|
+
beam=beam,
|
|
503
|
+
bottom=bottom,
|
|
504
|
+
pause=pause,
|
|
505
|
+
)
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
def fourier(
|
|
509
|
+
x0,
|
|
510
|
+
x1,
|
|
511
|
+
y0,
|
|
512
|
+
y1,
|
|
513
|
+
*,
|
|
514
|
+
count=1,
|
|
515
|
+
period=10.0,
|
|
516
|
+
coefficients=((1.0, 0.0),),
|
|
517
|
+
phase=0.0,
|
|
518
|
+
spacing=None,
|
|
519
|
+
offset=0.0,
|
|
520
|
+
margin=5.0,
|
|
521
|
+
resolution=32,
|
|
522
|
+
alternate=True,
|
|
523
|
+
beam,
|
|
524
|
+
bottom=None,
|
|
525
|
+
pause=30.0,
|
|
526
|
+
) -> LaserPath:
|
|
527
|
+
"""Tracks of a Fourier waveform (see the module)."""
|
|
528
|
+
return _wavy(
|
|
529
|
+
x0,
|
|
530
|
+
x1,
|
|
531
|
+
y0,
|
|
532
|
+
y1,
|
|
533
|
+
count=count,
|
|
534
|
+
period=period,
|
|
535
|
+
phase=phase,
|
|
536
|
+
coefficients=coefficients,
|
|
537
|
+
spacing=spacing,
|
|
538
|
+
offset=offset,
|
|
539
|
+
margin=margin,
|
|
540
|
+
resolution=resolution,
|
|
541
|
+
alternate=alternate,
|
|
542
|
+
beam=beam,
|
|
543
|
+
bottom=bottom,
|
|
544
|
+
pause=pause,
|
|
545
|
+
)
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
def crossed(
|
|
549
|
+
x0,
|
|
550
|
+
x1,
|
|
551
|
+
y0,
|
|
552
|
+
y1,
|
|
553
|
+
*,
|
|
554
|
+
angle=45.0,
|
|
555
|
+
spacing=3.0,
|
|
556
|
+
spacing2=None,
|
|
557
|
+
offset=0.0,
|
|
558
|
+
margin=5.0,
|
|
559
|
+
alternate=True,
|
|
560
|
+
beam,
|
|
561
|
+
bottom=None,
|
|
562
|
+
pause=30.0,
|
|
563
|
+
) -> LaserPath:
|
|
564
|
+
"""Two families of straight tracks at ``±angle``, the second at ``spacing2`` if
|
|
565
|
+
given (see the module)."""
|
|
566
|
+
a = _lines(
|
|
567
|
+
x0,
|
|
568
|
+
x1,
|
|
569
|
+
y0,
|
|
570
|
+
y1,
|
|
571
|
+
angle=angle,
|
|
572
|
+
spacing=spacing,
|
|
573
|
+
offset=offset,
|
|
574
|
+
margin=margin,
|
|
575
|
+
alternate=alternate,
|
|
576
|
+
beam=beam,
|
|
577
|
+
bottom=bottom,
|
|
578
|
+
pause=pause,
|
|
579
|
+
)
|
|
580
|
+
b = _lines(
|
|
581
|
+
x0,
|
|
582
|
+
x1,
|
|
583
|
+
y0,
|
|
584
|
+
y1,
|
|
585
|
+
angle=-angle,
|
|
586
|
+
spacing=spacing if spacing2 is None else spacing2,
|
|
587
|
+
offset=offset,
|
|
588
|
+
margin=margin,
|
|
589
|
+
alternate=alternate,
|
|
590
|
+
beam=beam,
|
|
591
|
+
bottom=bottom,
|
|
592
|
+
pause=pause,
|
|
593
|
+
)
|
|
594
|
+
return LaserPath(a.tracks + b.tracks)
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
def chain_edges(vertices, edges) -> list[np.ndarray]:
|
|
598
|
+
"""Polylines that cover the edges of a network once each: from an unused edge, a
|
|
599
|
+
walk continues along the unused edge that turns the least, both ways, until none
|
|
600
|
+
is left at its ends (few laser starts and stops)."""
|
|
601
|
+
v = np.asarray(vertices, dtype=float).reshape(-1, 2)
|
|
602
|
+
e = [tuple(sorted(map(int, ed))) for ed in edges]
|
|
603
|
+
e = sorted(set(ed for ed in e if ed[0] != ed[1]))
|
|
604
|
+
adj: dict[int, list[int]] = {}
|
|
605
|
+
for k, (a, b) in enumerate(e):
|
|
606
|
+
adj.setdefault(a, []).append(k)
|
|
607
|
+
adj.setdefault(b, []).append(k)
|
|
608
|
+
used = np.zeros(len(e), dtype=bool)
|
|
609
|
+
|
|
610
|
+
def other(k, a):
|
|
611
|
+
return e[k][1] if e[k][0] == a else e[k][0]
|
|
612
|
+
|
|
613
|
+
def walk(prev, cur):
|
|
614
|
+
out = []
|
|
615
|
+
while True:
|
|
616
|
+
d0 = v[cur] - v[prev]
|
|
617
|
+
best, best_cos = None, -2.0
|
|
618
|
+
for k in adj[cur]:
|
|
619
|
+
if used[k]:
|
|
620
|
+
continue
|
|
621
|
+
d1 = v[other(k, cur)] - v[cur]
|
|
622
|
+
c = float(d0 @ d1) / max(np.linalg.norm(d0) * np.linalg.norm(d1), 1e-300)
|
|
623
|
+
if c > best_cos:
|
|
624
|
+
best, best_cos = k, c
|
|
625
|
+
if best is None:
|
|
626
|
+
return out
|
|
627
|
+
used[best] = True
|
|
628
|
+
prev, cur = cur, other(best, cur)
|
|
629
|
+
out.append(cur)
|
|
630
|
+
|
|
631
|
+
lines = []
|
|
632
|
+
for k in range(len(e)):
|
|
633
|
+
if used[k]:
|
|
634
|
+
continue
|
|
635
|
+
used[k] = True
|
|
636
|
+
a, b = e[k]
|
|
637
|
+
forward = walk(a, b)
|
|
638
|
+
backward = walk(b, a)
|
|
639
|
+
idx = [*backward[::-1], a, b, *forward]
|
|
640
|
+
lines.append(v[idx])
|
|
641
|
+
return lines
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
def honeycomb(
|
|
645
|
+
x0,
|
|
646
|
+
x1,
|
|
647
|
+
y0,
|
|
648
|
+
y1,
|
|
649
|
+
*,
|
|
650
|
+
cell=3.0,
|
|
651
|
+
angle=0.0,
|
|
652
|
+
offset=0.0,
|
|
653
|
+
margin=5.0,
|
|
654
|
+
alternate=True,
|
|
655
|
+
beam,
|
|
656
|
+
bottom=None,
|
|
657
|
+
pause=30.0,
|
|
658
|
+
) -> LaserPath:
|
|
659
|
+
"""The edges of regular hexagons of side ``cell``, two of their sides along x (turned
|
|
660
|
+
by ``angle`` degrees), chained into continuous tracks (:func:`chain_edges`)."""
|
|
661
|
+
a = float(cell)
|
|
662
|
+
if not a > 0:
|
|
663
|
+
raise ValueError("cell must be positive")
|
|
664
|
+
h = math.sqrt(3.0) * a
|
|
665
|
+
cx, cy = 0.5 * (x0 + x1), 0.5 * (y0 + y1) + offset
|
|
666
|
+
th = math.radians(angle)
|
|
667
|
+
rot = np.array([[math.cos(th), -math.sin(th)], [math.sin(th), math.cos(th)]])
|
|
668
|
+
reach = math.hypot(x1 - x0 + 2 * margin, y1 - y0 + 2 * margin) / 2 + 2 * a
|
|
669
|
+
ni, nj = math.ceil(reach / (1.5 * a)) + 1, math.ceil(reach / h) + 1
|
|
670
|
+
corners = a * np.array(
|
|
671
|
+
[[math.cos(k * math.pi / 3), math.sin(k * math.pi / 3)] for k in range(6)]
|
|
672
|
+
)
|
|
673
|
+
keys: dict[tuple, int] = {}
|
|
674
|
+
verts, edges = [], []
|
|
675
|
+
for i in range(-ni, ni + 1):
|
|
676
|
+
for j in range(-nj, nj + 1):
|
|
677
|
+
c = np.array([1.5 * a * i, h * (j + 0.5 * (i % 2))])
|
|
678
|
+
ids = []
|
|
679
|
+
for q in corners + c:
|
|
680
|
+
key = (round(q[0] / a * 1e6), round(q[1] / a * 1e6))
|
|
681
|
+
if key not in keys:
|
|
682
|
+
keys[key] = len(verts)
|
|
683
|
+
verts.append(q)
|
|
684
|
+
ids.append(keys[key])
|
|
685
|
+
edges.extend((ids[k], ids[(k + 1) % 6]) for k in range(6))
|
|
686
|
+
verts = np.array(verts) @ rot.T + [cx, cy]
|
|
687
|
+
lo = np.array([x0 - margin, y0 - margin])
|
|
688
|
+
hi = np.array([x1 + margin, y1 + margin])
|
|
689
|
+
inside = np.all((verts >= lo - a) & (verts <= hi + a), axis=1)
|
|
690
|
+
edges = [ed for ed in edges if inside[ed[0]] and inside[ed[1]]]
|
|
691
|
+
lines = chain_edges(verts, edges)
|
|
692
|
+
tracks = []
|
|
693
|
+
for i, pts in enumerate(lines):
|
|
694
|
+
t = Track(pts, beam, bottom, pause)
|
|
695
|
+
tracks.append(t.reversed() if alternate and i % 2 else t)
|
|
696
|
+
return LaserPath(tracks)
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
def spiral(
|
|
700
|
+
x0,
|
|
701
|
+
x1,
|
|
702
|
+
y0,
|
|
703
|
+
y1,
|
|
704
|
+
*,
|
|
705
|
+
center=None,
|
|
706
|
+
radius=0.0,
|
|
707
|
+
pitch=3.0,
|
|
708
|
+
turns=2.0,
|
|
709
|
+
angle=0.0,
|
|
710
|
+
clockwise=False,
|
|
711
|
+
resolution=0.2,
|
|
712
|
+
margin=5.0,
|
|
713
|
+
beam,
|
|
714
|
+
bottom=None,
|
|
715
|
+
pause=30.0,
|
|
716
|
+
) -> LaserPath:
|
|
717
|
+
"""An Archimedean spiral track (see the module), its points about ``resolution``
|
|
718
|
+
mm apart along it."""
|
|
719
|
+
if not pitch > 0 or not turns > 0:
|
|
720
|
+
raise ValueError("spiral: positive pitch and turns")
|
|
721
|
+
cx, cy = (0.5 * (x0 + x1), 0.5 * (y0 + y1)) if center is None else map(float, center)
|
|
722
|
+
th_end = 2.0 * math.pi * float(turns)
|
|
723
|
+
ths = [0.0]
|
|
724
|
+
while ths[-1] < th_end:
|
|
725
|
+
r = float(radius) + float(pitch) * ths[-1] / (2.0 * math.pi)
|
|
726
|
+
ths.append(min(th_end, ths[-1] + float(resolution) / max(r, float(resolution))))
|
|
727
|
+
th = np.array(ths)
|
|
728
|
+
r = float(radius) + float(pitch) * th / (2.0 * math.pi)
|
|
729
|
+
phi = math.radians(angle) + (-th if clockwise else th)
|
|
730
|
+
pts = np.column_stack([cx + r * np.cos(phi), cy + r * np.sin(phi)])
|
|
731
|
+
return LaserPath([Track(pts, beam, bottom, pause)])
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
def polylines(tracks, *, beam, bottom=None, pause=30.0) -> LaserPath:
|
|
735
|
+
"""Tracks given as lists of points (see the module)."""
|
|
736
|
+
return LaserPath([Track(np.asarray(t, dtype=float), beam, bottom, pause) for t in tracks])
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
#: Pattern generators of :func:`make_pattern`.
|
|
740
|
+
PATTERNS = {
|
|
741
|
+
"straight": straight,
|
|
742
|
+
"corrugated": corrugated,
|
|
743
|
+
"fourier": fourier,
|
|
744
|
+
"crossed": crossed,
|
|
745
|
+
"honeycomb": honeycomb,
|
|
746
|
+
"spiral": spiral,
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
def clip_path(path: LaserPath, lo, hi) -> LaserPath:
|
|
751
|
+
"""The parts of the tracks inside the box ``[lo, hi]`` (a track that leaves and
|
|
752
|
+
comes back splits into several)."""
|
|
753
|
+
lo, hi = np.asarray(lo, float), np.asarray(hi, float)
|
|
754
|
+
out = []
|
|
755
|
+
for t in path.tracks:
|
|
756
|
+
piece: list = []
|
|
757
|
+
for a, b in itertools.pairwise(t.points):
|
|
758
|
+
seg = _clip_points(a, b, lo, hi)
|
|
759
|
+
if seg is None:
|
|
760
|
+
if len(piece) > 1:
|
|
761
|
+
out.append(replace(t, points=np.array(piece)))
|
|
762
|
+
piece = []
|
|
763
|
+
continue
|
|
764
|
+
p, q = seg
|
|
765
|
+
if piece and np.allclose(piece[-1], p):
|
|
766
|
+
piece.append(q)
|
|
767
|
+
else:
|
|
768
|
+
if len(piece) > 1:
|
|
769
|
+
out.append(replace(t, points=np.array(piece)))
|
|
770
|
+
piece = [p, q]
|
|
771
|
+
if not np.allclose(q, b): # it leaves the box here
|
|
772
|
+
out.append(replace(t, points=np.array(piece)))
|
|
773
|
+
piece = []
|
|
774
|
+
if len(piece) > 1:
|
|
775
|
+
out.append(replace(t, points=np.array(piece)))
|
|
776
|
+
return LaserPath(out)
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
def make_pattern(
|
|
780
|
+
kind: str,
|
|
781
|
+
band,
|
|
782
|
+
*,
|
|
783
|
+
beam,
|
|
784
|
+
bottom=None,
|
|
785
|
+
pause=30.0,
|
|
786
|
+
rotation: float = 0.0,
|
|
787
|
+
cover: bool = False,
|
|
788
|
+
**params,
|
|
789
|
+
) -> LaserPath:
|
|
790
|
+
"""A pattern of :data:`PATTERNS` (or ``polylines``) over the band ``(x0, x1, y0, y1)``
|
|
791
|
+
(mm), its tracks followed by ``beam`` and ``bottom``. ``rotation`` (degrees) turns
|
|
792
|
+
the pattern about the centre of the band: the specimen is then cut at ``-rotation``
|
|
793
|
+
from the axes of the pattern (the anisotropy of an architectured sheet, tested at
|
|
794
|
+
0°, 45° and 90°); the pattern is drawn on a square that covers the turned band, its
|
|
795
|
+
families of tracks repeated at their spacing over it (the treated sheet), then
|
|
796
|
+
clipped to the band and its margin — so is it unturned with ``cover`` (the periodic
|
|
797
|
+
sheet around a cell, :mod:`~morphogen.laser.periodic`)."""
|
|
798
|
+
if kind == "polylines":
|
|
799
|
+
path = polylines(params.pop("tracks"), beam=beam, bottom=bottom, pause=pause)
|
|
800
|
+
if rotation:
|
|
801
|
+
raise ValueError("rotation: give the polylines turned")
|
|
802
|
+
return path
|
|
803
|
+
if kind not in PATTERNS:
|
|
804
|
+
raise ValueError(f"pattern: one of {[*sorted(PATTERNS), 'polylines']}, got {kind!r}")
|
|
805
|
+
x0, x1, y0, y1 = (float(v) for v in band)
|
|
806
|
+
rotation = float(rotation) % 360.0
|
|
807
|
+
if rotation == 0.0 and not cover:
|
|
808
|
+
return PATTERNS[kind](x0, x1, y0, y1, beam=beam, bottom=bottom, pause=pause, **params)
|
|
809
|
+
margin = float(params.get("margin", 5.0))
|
|
810
|
+
cx, cy = 0.5 * (x0 + x1), 0.5 * (y0 + y1)
|
|
811
|
+
half = 0.5 * math.hypot(x1 - x0, y1 - y0) + margin
|
|
812
|
+
if kind in ("straight", "corrugated", "fourier") and params.get("count", 1) is not None:
|
|
813
|
+
# the tracks of the sheet, at their spacing, over the whole square: a parallel
|
|
814
|
+
# family (same parity of the count, so that the tracks keep their positions)
|
|
815
|
+
count = int(params.get("count", 1))
|
|
816
|
+
spacing = params.get("spacing")
|
|
817
|
+
spacing = (y1 - y0) / count if spacing is None else float(spacing)
|
|
818
|
+
params["spacing"] = spacing
|
|
819
|
+
params["count"] = count + 2 * max(0, math.ceil((2.0 * half / spacing - count) / 2.0))
|
|
820
|
+
path = PATTERNS[kind](
|
|
821
|
+
cx - half, cx + half, cy - half, cy + half, beam=beam, bottom=bottom, pause=pause, **params
|
|
822
|
+
)
|
|
823
|
+
th = math.radians(rotation)
|
|
824
|
+
rot = np.array([[math.cos(th), -math.sin(th)], [math.sin(th), math.cos(th)]])
|
|
825
|
+
turned = LaserPath(
|
|
826
|
+
[replace(t, points=(t.points - [cx, cy]) @ rot.T + [cx, cy]) for t in path.tracks]
|
|
827
|
+
)
|
|
828
|
+
return clip_path(turned, (x0 - margin, y0 - margin), (x1 + margin, y1 + margin))
|