Skip to content

Commit fb9d157

Browse files
authored
Merge pull request #167 from ndem0/control_points_dynamically
Mu arrays dynamically created
2 parents d6a4298 + 6452691 commit fb9d157

File tree

1 file changed

+52
-27
lines changed

1 file changed

+52
-27
lines changed

pygem/params/ffdparams.py

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,25 @@ def __init__(self, n_control_points=None):
7979

8080
if n_control_points is None:
8181
n_control_points = [2, 2, 2]
82-
self.n_control_points = np.array(n_control_points)
82+
self.n_control_points = n_control_points
8383

84+
85+
@property
86+
def n_control_points(self):
87+
"""
88+
The number of control points in X, Y and Z directions
89+
90+
:rtype: numpy.ndarray
91+
"""
92+
return self._n_control_points
93+
94+
@n_control_points.setter
95+
def n_control_points(self, npts):
96+
self._n_control_points = np.array(npts)
8497
self.array_mu_x = np.zeros(self.n_control_points)
8598
self.array_mu_y = np.zeros(self.n_control_points)
8699
self.array_mu_z = np.zeros(self.n_control_points)
100+
87101

88102
@property
89103
def psi_mapping(self):
@@ -391,6 +405,42 @@ def __str__(self):
391405
string += '\nposition_vertices = {}\n'.format(self.position_vertices)
392406
return string
393407

408+
409+
def control_points(self, deformed=True):
410+
"""
411+
Method that returns the FFD control points. If the `deformed` flag is
412+
set to True the method returns the deformed lattice, otherwise it
413+
returns the original undeformed lattice.
414+
415+
:param bool deformed: flag to select the original or modified FFD
416+
control lattice. The default is True.
417+
:return: the FFD control points (by row).
418+
:rtype: numpy.ndarray
419+
"""
420+
x = np.linspace(0, self.box_length[0], self.n_control_points[0])
421+
y = np.linspace(0, self.box_length[1], self.n_control_points[1])
422+
z = np.linspace(0, self.box_length[2], self.n_control_points[2])
423+
424+
y_coords, x_coords, z_coords = np.meshgrid(y, x, z)
425+
426+
box_points = np.array([
427+
x_coords.ravel(), y_coords.ravel(), z_coords.ravel()])
428+
429+
if deformed:
430+
box_points += np.array([
431+
self.array_mu_x.ravel() * self.box_length[0],
432+
self.array_mu_y.ravel() * self.box_length[1],
433+
self.array_mu_z.ravel() * self.box_length[2]
434+
])
435+
436+
n_rows = box_points.shape[1]
437+
438+
box_points = np.dot(
439+
self.rotation_matrix,
440+
box_points) + np.transpose(np.tile(self.box_origin, (n_rows, 1)))
441+
442+
return box_points.T
443+
394444
def save_points(self, filename, write_deformed=True):
395445
"""
396446
Method that writes a vtk file containing the FFD lattice. This method
@@ -417,32 +467,7 @@ def save_points(self, filename, write_deformed=True):
417467
**Point Gaussian** representation.
418468
419469
"""
420-
x = np.linspace(0, self.box_length[0], self.n_control_points[0])
421-
y = np.linspace(0, self.box_length[1], self.n_control_points[1])
422-
z = np.linspace(0, self.box_length[2], self.n_control_points[2])
423-
424-
lattice_y_coords, lattice_x_coords, lattice_z_coords = np.meshgrid(y, x,
425-
z)
426-
427-
if write_deformed:
428-
box_points = np.array([
429-
lattice_x_coords.ravel() + self.array_mu_x.ravel() *
430-
self.box_length[0], lattice_y_coords.ravel() +
431-
self.array_mu_y.ravel() * self.box_length[1],
432-
lattice_z_coords.ravel() + self.array_mu_z.ravel() *
433-
self.box_length[2]
434-
])
435-
else:
436-
box_points = np.array([
437-
lattice_x_coords.ravel(), lattice_y_coords.ravel(),
438-
lattice_z_coords.ravel()
439-
])
440-
441-
n_rows = box_points.shape[1]
442-
443-
box_points = np.dot(
444-
self.rotation_matrix,
445-
box_points) + np.transpose(np.tile(self.box_origin, (n_rows, 1)))
470+
box_points = self.control_points(write_deformed).T
446471

447472
points = vtk.vtkPoints()
448473

0 commit comments

Comments
 (0)