--- jupytext: text_representation: extension: .md format_name: myst format_version: 0.13 jupytext_version: 1.16.1 kernelspec: display_name: Python 3 (ipykernel) language: python name: python3 --- (how-to-3d-side)= # Checking the side of 3D curves and surfaces A 3D curve's running direction, and a surface's disk orientation, decide which side of the shape faces +z after registration: for a leaf, which side is its upper surface. Registration keeps the side that the input's parameterization gives, because it cannot read the side from the shape or the pose with certainty (see {ref}`harmonic-registration`). This guide finds specimens traced the other way round, fixes them, and shows the alternative for data whose side carries no meaning. ```{code-cell} ipython3 import numpy as np from ktch.datasets import load_outline_leaf_bending from ktch.harmonic import EllipticFourierAnalysis, flip_distances coords = list(load_outline_leaf_bending().coords) ``` To imitate inconsistent digitization, three leaves are traced the other way round: ```{code-cell} ipython3 def turn_over(curve): """The curve traced the other way round, from the same first point.""" return np.roll(curve[::-1], 1, axis=0) coords_mixed = [turn_over(c) if i in (4, 21, 37) else c for i, c in enumerate(coords)] ``` ## Finding the specimens on the other side Register as usual, then compare every candidate of each specimen with a template: the registered coefficients of a specimen whose side you have checked, here the first leaf. A specimen nearest a candidate that reverses the running direction (its first two signs differ) is on the other side from the template: ```{code-cell} ipython3 efa = EllipticFourierAnalysis(n_harmonics=20, n_dim=3) coef = efa.fit_transform(coords_mixed) result = flip_distances(efa, coef, coef[0]) nearest = result.candidates[result.nearest] turned = np.flatnonzero(nearest[:, 0] != nearest[:, 1]) turned ``` `result.distances` holds the distance of every candidate, one column per row of `result.candidates`. With one specimen as the template, the distances also include how much the shapes differ. Compare the candidates of a specimen with each other, not the distances of different specimens: ```{code-cell} ipython3 result.distances[turned].round(2) ``` Which side is the right one is biological knowledge: the diagnostic only says which specimens disagree with the template. ## Fixing the input Once the side is decided, trace those specimens again the other way round. For a 3D curve, reverse the point list and keep its first point. For a disk-parameterized surface ({class}`~ktch.harmonic.DiskHarmonicAnalysis`), negate the angle of its disk coordinates, $\theta \to -\theta$, in `r_theta`. ```{code-cell} ipython3 coords_fixed = [turn_over(c) if i in turned else c for i, c in enumerate(coords_mixed)] coef_fixed = efa.fit_transform(coords_fixed) np.allclose(coef_fixed, efa.fit_transform(coords)) ``` ## Letting the shape decide the side When the side carries no meaning for the question, `reversal="align"` lets `sign_method` decide it, and the running direction of the input no longer matters. With `sign_method="shape"` the side is the one along which the skewness of the curve is positive: ```{code-cell} ipython3 efa_align = EllipticFourierAnalysis( n_harmonics=20, n_dim=3, sign_method="shape", reversal="align" ) coef_align = efa_align.fit_transform(coords) np.allclose(coef_align, efa_align.fit_transform(coords_mixed)) ``` Two cautions: - For a nearly flat shape the skewness along the normal is small, and the noise of the data can decide the side. Registering a few specimens digitized twice shows whether it does. - The shape can choose, for a whole dataset, the side opposite to the one the inputs give. For these leaves it does: compared with `reversal="never"`, which keeps the digitized side, `reversal="align"` turns every leaf over. The digitized side is consistent across specimens, but it is not the side of positive skewness. ```{code-cell} ipython3 efa_keep = EllipticFourierAnalysis( n_harmonics=20, n_dim=3, sign_method="shape", reversal="never" ) coef_keep = efa_keep.fit_transform(coords) changed = ~np.isclose(coef_keep, coef_align).all(axis=1) int(changed.sum()), len(coords) ``` ```{seealso} - {ref}`how-to-2d-outline-registration` for the running direction of 2D outlines - {doc}`../../explanation/harmonic` for the residual group and `sign_method` - {func}`~ktch.harmonic.flip_distances` and {func}`~ktch.harmonic.canonical_form_report` ```