|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +from ...config import AvailableBackends |
| 6 | +from ...core.backend_tensor import BackendTensor |
| 7 | +from ...core.data.dual_contouring_data import DualContouringData |
| 8 | + |
| 9 | + |
| 10 | +def _compute_vertices(dc_data_per_stack: DualContouringData, |
| 11 | + debug: bool, |
| 12 | + surface_i: int, |
| 13 | + valid_edges_per_surface) -> tuple[DualContouringData, Any]: |
| 14 | + """Compute vertices for a specific surface.""" |
| 15 | + valid_edges: np.ndarray = valid_edges_per_surface[surface_i] |
| 16 | + next_surface_edge_idx: int = valid_edges_per_surface[:surface_i + 1].sum() |
| 17 | + if surface_i == 0: |
| 18 | + last_surface_edge_idx = 0 |
| 19 | + else: |
| 20 | + last_surface_edge_idx: int = valid_edges_per_surface[:surface_i].sum() |
| 21 | + slice_object: slice = slice(last_surface_edge_idx, next_surface_edge_idx) |
| 22 | + |
| 23 | + dc_data_per_surface = DualContouringData( |
| 24 | + xyz_on_edge=dc_data_per_stack.xyz_on_edge, |
| 25 | + valid_edges=valid_edges, |
| 26 | + xyz_on_centers=dc_data_per_stack.xyz_on_centers, |
| 27 | + dxdydz=dc_data_per_stack.dxdydz, |
| 28 | + exported_fields_on_edges=dc_data_per_stack.exported_fields_on_edges, |
| 29 | + n_surfaces_to_export=dc_data_per_stack.n_surfaces_to_export, |
| 30 | + tree_depth=dc_data_per_stack.tree_depth |
| 31 | + ) |
| 32 | + |
| 33 | + vertices_numpy = _generate_vertices(dc_data_per_surface, debug, slice_object) |
| 34 | + return dc_data_per_surface, vertices_numpy |
| 35 | + |
| 36 | + |
| 37 | +def _generate_vertices(dc_data_per_surface: DualContouringData, debug: bool, slice_object: slice) -> Any: |
| 38 | + vertices: np.ndarray = generate_dual_contouring_vertices( |
| 39 | + dc_data_per_stack=dc_data_per_surface, |
| 40 | + slice_surface=slice_object, |
| 41 | + debug=debug |
| 42 | + ) |
| 43 | + vertices_numpy = BackendTensor.t.to_numpy(vertices) |
| 44 | + return vertices_numpy |
| 45 | + |
| 46 | + |
| 47 | +def generate_dual_contouring_vertices(dc_data_per_stack: DualContouringData, slice_surface: slice, debug: bool = False): |
| 48 | + # @off |
| 49 | + n_edges = dc_data_per_stack.n_edges |
| 50 | + valid_edges = dc_data_per_stack.valid_edges |
| 51 | + valid_voxels = dc_data_per_stack.valid_voxels |
| 52 | + xyz_on_edge = dc_data_per_stack.xyz_on_edge[slice_surface] |
| 53 | + gradients = dc_data_per_stack.gradients[slice_surface] |
| 54 | + # @on |
| 55 | + |
| 56 | + # * Coordinates for all posible edges (12) and 3 dummy edges_normals in the center |
| 57 | + edges_xyz = BackendTensor.tfnp.zeros((n_edges, 15, 3), dtype=BackendTensor.dtype_obj) |
| 58 | + valid_edges = valid_edges > 0 |
| 59 | + edges_xyz[:, :12][valid_edges] = xyz_on_edge |
| 60 | + |
| 61 | + # Normals |
| 62 | + edges_normals = BackendTensor.tfnp.zeros((n_edges, 15, 3), dtype=BackendTensor.dtype_obj) |
| 63 | + edges_normals[:, :12][valid_edges] = gradients |
| 64 | + |
| 65 | + if OLD_METHOD := False: |
| 66 | + # ! Moureze model does not seems to work with the new method |
| 67 | + # ! This branch is all nans at least with ch1_1 model |
| 68 | + bias_xyz = BackendTensor.tfnp.copy(edges_xyz[:, :12]) |
| 69 | + isclose = BackendTensor.tfnp.isclose(bias_xyz, 0) |
| 70 | + bias_xyz[isclose] = BackendTensor.tfnp.nan # zero values to nans |
| 71 | + mass_points = BackendTensor.tfnp.nanmean(bias_xyz, axis=1) # Mean ignoring nans |
| 72 | + else: # ? This is actually doing something |
| 73 | + bias_xyz = BackendTensor.tfnp.copy(edges_xyz[:, :12]) |
| 74 | + if BackendTensor.engine_backend == AvailableBackends.PYTORCH: |
| 75 | + # PyTorch doesn't have masked arrays, so we'll use a different approach |
| 76 | + mask = bias_xyz == 0 |
| 77 | + # Replace zeros with NaN for mean calculation |
| 78 | + bias_xyz_masked = BackendTensor.tfnp.where(mask, float('nan'), bias_xyz) |
| 79 | + mass_points = BackendTensor.tfnp.nanmean(bias_xyz_masked, axis=1) |
| 80 | + else: |
| 81 | + # NumPy approach with masked arrays |
| 82 | + bias_xyz = BackendTensor.tfnp.to_numpy(bias_xyz) |
| 83 | + import numpy as np |
| 84 | + mask = bias_xyz == 0 |
| 85 | + masked_arr = np.ma.masked_array(bias_xyz, mask) |
| 86 | + mass_points = masked_arr.mean(axis=1) |
| 87 | + mass_points = BackendTensor.tfnp.array(mass_points) |
| 88 | + |
| 89 | + edges_xyz[:, 12] = mass_points |
| 90 | + edges_xyz[:, 13] = mass_points |
| 91 | + edges_xyz[:, 14] = mass_points |
| 92 | + |
| 93 | + BIAS_STRENGTH = 1 |
| 94 | + |
| 95 | + bias_x = BackendTensor.tfnp.array([BIAS_STRENGTH, 0, 0], dtype=BackendTensor.dtype_obj) |
| 96 | + bias_y = BackendTensor.tfnp.array([0, BIAS_STRENGTH, 0], dtype=BackendTensor.dtype_obj) |
| 97 | + bias_z = BackendTensor.tfnp.array([0, 0, BIAS_STRENGTH], dtype=BackendTensor.dtype_obj) |
| 98 | + |
| 99 | + edges_normals[:, 12] = bias_x |
| 100 | + edges_normals[:, 13] = bias_y |
| 101 | + edges_normals[:, 14] = bias_z |
| 102 | + |
| 103 | + # Remove unused voxels |
| 104 | + edges_xyz = edges_xyz[valid_voxels] |
| 105 | + edges_normals = edges_normals[valid_voxels] |
| 106 | + |
| 107 | + # Compute LSTSQS in all voxels at the same time |
| 108 | + A = edges_normals |
| 109 | + b = (A * edges_xyz).sum(axis=2) |
| 110 | + |
| 111 | + if BackendTensor.engine_backend == AvailableBackends.PYTORCH: |
| 112 | + transpose_shape = (2, 1, 0) # For PyTorch: (batch, dim2, dim1) |
| 113 | + else: |
| 114 | + transpose_shape = (0, 2, 1) # For NumPy: (batch, dim2, dim1) |
| 115 | + |
| 116 | + term1 = BackendTensor.tfnp.einsum("ijk, ilj->ikl", A, BackendTensor.tfnp.transpose(A, transpose_shape)) |
| 117 | + term2 = BackendTensor.tfnp.linalg.inv(term1) |
| 118 | + term3 = BackendTensor.tfnp.einsum("ijk,ik->ij", BackendTensor.tfnp.transpose(A, transpose_shape), b) |
| 119 | + vertices = BackendTensor.tfnp.einsum("ijk, ij->ik", term2, term3) |
| 120 | + |
| 121 | + if debug: |
| 122 | + dc_data_per_stack.bias_center_mass = edges_xyz[:, 12:].reshape(-1, 3) |
| 123 | + dc_data_per_stack.bias_normals = edges_normals[:, 12:].reshape(-1, 3) |
| 124 | + |
| 125 | + return vertices |
0 commit comments