Skip to content

Commit

Permalink
fix: handle the converted weights where the sum isn't 255 (#893)
Browse files Browse the repository at this point in the history
  • Loading branch information
Loyalists committed Apr 29, 2024
1 parent b93aabd commit 81883a4
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions ydr/vertex_buffer_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def _get_weights_indices(self) -> Tuple[NDArray[np.uint32], NDArray[np.uint32]]:
weights_arr, ind_arr = self._sort_weights_inds(weights_arr, ind_arr)

weights_arr = self._convert_to_int_range(weights_arr)
weights_arr = self._renormalize_converted_weights(weights_arr)

# Return on loop domain
return weights_arr[self._vert_inds], ind_arr[self._vert_inds]
Expand Down Expand Up @@ -191,6 +192,18 @@ def _convert_to_int_range(self, arr: NDArray[np.float32]) -> NDArray[np.uint32]:
"""Convert float array from range 0-1 to range 0-255"""
return (np.rint(arr * 255)).astype(np.uint32)

def _renormalize_converted_weights(self, weights_arr: NDArray[np.uint32]) -> NDArray[np.uint32]:
"""Re-normalize converted weights to ensure their sum to be 255."""
row_sums = weights_arr.sum(axis=1, keepdims=True)
to_be_subtracted = np.full_like(row_sums, 255, dtype=np.int32)
deltas = np.subtract(to_be_subtracted, row_sums)
max_indices = weights_arr.argmax(axis=1, keepdims=True)
max_values = weights_arr.max(axis=1, keepdims=True)
normalized_max_values = np.add(max_values, deltas)
result = np.copy(weights_arr)
np.put_along_axis(result, max_indices, normalized_max_values, axis=1)
return result

def _get_colors(self) -> list[NDArray[np.uint32]]:
num_loops = len(self.mesh.loops)

Expand Down

0 comments on commit 81883a4

Please sign in to comment.