squarenet.coreο
Functions
|
Supported: :2: (WARNING/2) Title underline too short. Supported: ---- method [fast, robust, ultimate] backend [numpy, torch] |
- squarenet.core.cartesian_sort(gridmap, points, max_iter=100, method='fast', backend='numpy', loop=None, loopseq='decreasing', verbose=2)[source]ο
Supported:ο
method [fast, robust, ultimate] backend [numpy, torch]
Goalο
Given an unordered point cloud:
points.shape = (N, D)
rearrange point indices into a structured cartesian grid:
grid.shape = (N1, N2, β¦, ND)
such that neighbouring cells of the grid contain spatially coherent points.
GRID INTERPRETATIONο
A grid index map bijectively to a point index:
point index: n <=> grid index: f(n) = (n1, n2, β¦, nD)
Each grid axis defines a local neighbour relation.
For axis d, let define:
next(n, d) = f-1(n1,β¦, nd+1,β¦,nD)
such that P(next(n,d)) is the neighbour obtained by incrementing the d-th grid coordinate.
The objective is therefore:
nearby euclidean points -> nearby grid cells
Note that the reciprocal property
nearby grid cells -> nearby euclidean points
is NOT guaranteed.
Indeed, datasets may contain cracks, holes, disconnected clusters, folds, or other topological singularities.
Gridification will naturally tend to close cracks, stitch nearby boundaries together and overlap the folds.
This algorithm is primarily designed for speed and scalability on large datasets. It is NOT an optimal transport solver.
Therefore, users should always inspect the resulting grid, especially near boundaries where geometric distortions are more likely to appear.
HEURISTIC ORDERING PRINCIPLEο
For each spatial dimension d, define d spatial heuristics:
H_d(point) -> scalar
Heuristics must be orthogonal and monotonic Typical choice: cartesian heuristics
H_0 = x H_1 = y H_2 = z β¦
One could think on an improved version of the algorithm which would learn heuristics that best fit the dataset but cartesian are already pretty good
The desired property is:
H_d(P(n)) <= H_d(P(next(n, d)))
for every point index n and every axis d.
In words:
values of heuristic d should increase along grid axis d.
DISORDER METRICο
A local inversion occurs when:
H_d(P(n)) > H_d(P(next(n, d)))
The total disorder is simply the number of such violations.
Pseudo-definition:
- disorder =
sum over all axes d sum over all point pairs (current, next) of inversion count
FAST METHODο
The simplest strategy consists in repeatedly sorting each grid axis independently.
Pseudo-codeο
initialize gridmap
for iteration in range(max_iter):
for axis d in range(D):
sort grid indexes along axis d using heuristic H_d
compute disorder
- if disorder == 0:
stop
Propertiesο
- Advantages:
extremely fast
fully vectorized
memory efficient
surprisingly effective
- Limitations:
for loop on the axes
-> early axes dominate later ones -> result in weak axes
only adjacent comparison is a weak accuracy criterion
-> disorder is blind on what happens on diagonals -> may converge toward local minima
ROBUST METHODο
To reduce axis domination and improve stability, sorting is performed only on random independent subgrids at each step, thus learning is much more progressiv
At every iteration:
each axis is randomly partitioned
only selected cartesian sub-blocks are sorted
different blocks are used for different axes
Result:
smoother convergence
better axis symmetry
fewer local minima
improved isotropy
Pseudo-codeο
for iteration:
generate random cartesian subgrids
for axis d:
for subgrid in selected_subgrids[d]:
sort only inside this subgrid
compute disorder
ULTIMATE METHODο
Even robust cartesian sorting still treats grid lines as parallel and therefore almost independent.
This can produce:
stratification
layered artifacts
weak coupling between parallel slices
To solve this, a second refinement stage is introduced.
The grid is embedded into a βhash tableβ containing shifted/sheared copies of the grid.
Repeated cyclic shears produce strong cross-line coupling.
This allows information to propagate between previously independent cartesian lines.
High-level ideaο
repeat:
shear grid into staggered hash table
sort along one axis
project back to grid
Effectο
The repeated shearing progressively destroys artificial parallel structures and greatly reduces stratification. ============================================================ SUMMARY ============================================================
- FAST:
deterministic global line sorting (cartesian sort)
- ROBUST:
stochastic partial sorting preserving axis symmetry
- ULTIMATE:
sheared multi-pass relaxation reducing stratification artifacts
COMPLEXITYο
A few hundreds iterations will be largely enough for convergence in most of the cases.
Let:
N = total number of points
Each iteration performs approximately:
O(N (log N +D)) operations
with highly vectorized NumPy operations.