Skip to content

Commit

Permalink
Included center in minimization for parametrization Ellipsoid and Cyl…
Browse files Browse the repository at this point in the history
…iner
  • Loading branch information
maurerv committed Nov 1, 2023
1 parent 9df2a60 commit 62feada
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions colabseg/parametrization.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from abc import ABC, abstractmethod
from typing import Tuple

import numpy as np
from scipy import optimize
Expand All @@ -14,7 +13,7 @@ def __init__(self, *args, **kwargs):
pass

@abstractmethod
def fit(self, positions: np.ndarray, *args, **kwargs):
def fit(self, positions: np.ndarray, *args, **kwargs) -> "Parametrization":
"""
Fit a parametrization to a point cloud.
Expand All @@ -29,8 +28,8 @@ def fit(self, positions: np.ndarray, *args, **kwargs):
Returns
-------
Tuple
Parametrization parameters
Parametrization
Parametrization instance.
"""

@abstractmethod
Expand Down Expand Up @@ -198,7 +197,8 @@ def fit(cls, positions) -> "Ellipsoid":
"Only three-dimensional point clouds are supported."
)

def ellipsoid_loss(radii, data_points, center, orientations):
def ellipsoid_loss(params, data_points, orientations):
radii, center = params[0:3], params[3:]
transformed_points = np.dot(data_points - center, orientations)

normalized_points = transformed_points / radii
Expand All @@ -220,13 +220,15 @@ def ellipsoid_loss(radii, data_points, center, orientations):

initial_radii = 2 * np.sqrt(evals)

print(initial_radii, center)
result = optimize.minimize(
ellipsoid_loss,
initial_radii,
args=(positions, center, evecs),
(initial_radii, center),
args=(positions, evecs),
method="Nelder-Mead",
)
radii = result.x
radii, center = result.x[0:3], result.x[3:]
print(radii, center)

return cls(radii=radii, center=center, orientations=evecs)

Expand Down Expand Up @@ -345,7 +347,8 @@ def fit(cls, positions: np.ndarray) -> "Cylinder":

initial_radii = 2 * np.sqrt(evals)

def cylinder_loss(radii, data_points, center, orientations):
def cylinder_loss(params, data_points, orientations):
radii, center = params[0], params[1:]
transformed_points = np.dot(data_points - center, orientations)

normalized_points = transformed_points / radii
Expand All @@ -357,16 +360,15 @@ def cylinder_loss(radii, data_points, center, orientations):

result = optimize.minimize(
cylinder_loss,
np.max(initial_radii),
args=(positions, center, evecs),
np.array([np.max(initial_radii), *center]),
args=(positions, evecs),
method="Nelder-Mead",
)

radius, center = result.x[0], result.x[1:]
rotated_points = positions_centered.dot(evecs)
heights = rotated_points.max(axis = 0) - rotated_points.min(axis = 0)
height = heights[np.argmax(np.abs(np.diff(heights))) + 1]

return cls(radius=result.x, centers=center, orientations=evecs, height = height)
return cls(radius=radius, centers=center, orientations=evecs, height = height)

def sample(
self,
Expand Down

0 comments on commit 62feada

Please sign in to comment.