From d6073e130a772f525308b171c2ce76da524cc9d8 Mon Sep 17 00:00:00 2001 From: kshitij-maths Date: Sat, 28 Mar 2026 23:59:51 +0100 Subject: [PATCH] enhance: add fit_to_points method to automatically position FFD box based on input points --- pygem/ffd.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pygem/ffd.py b/pygem/ffd.py index ab98870b..9b40ead1 100644 --- a/pygem/ffd.py +++ b/pygem/ffd.py @@ -235,6 +235,26 @@ def reset_weights(self): self.array_mu_x.fill(0.0) self.array_mu_y.fill(0.0) self.array_mu_z.fill(0.0) + + def fit_to_points(self, points, padding_factor=0.05): + """ + Automatically set the FFD box_origin and box_length based on the input points. + + The method computes the axis-aligned bounding box of the input points, adds a small padding, and sets the FFD lattice to exactly + enclose the points. This provides a convenient default for the morphing setup. + + :param numpy.ndarray points: The original mesh points (N x 3). + :param float padding_factor: Additional space around the object as a fraction of its size. Default is 0.05 (5%). + """ + points = np.asarray(points) + min_coords = np.min(points, axis=0) + max_coords = np.max(points, axis=0) + + side_lengths = max_coords - min_coords + padding = side_lengths * padding_factor + self.box_length = side_lengths + 2 * padding + self.box_origin = min_coords - padding + def read_parameters(self, filename='parameters.prm'): """