@@ -34,8 +34,8 @@ class CFFD(FFD):
3434 y, normalized with the box length y.
3535 :cvar numpy.ndarray array_mu_z: collects the displacements (weights) along
3636 z, normalized with the box length z.
37- :cvar callable linconstraint : it defines the F of the constraint F(x)=c.
38- :cvar numpy.ndarray valconstraint : it defines the c of the constraint F(x)=c.
37+ :cvar callable fun : it defines the F of the constraint F(x)=c.
38+ :cvar numpy.ndarray fixval : it defines the c of the constraint F(x)=c.
3939 :cvar list indices: it defines the indices of the control points
4040 that are moved to enforce the constraint. The control index is obtained by doing:
4141 all_indices=np.arange(n_x*n_y*n_z*3).reshape(n_x,n_y,n_z,3).tolist().
@@ -53,31 +53,31 @@ class CFFD(FFD):
5353 >>> x=x.reshape(-1)
5454 >>> return A@x
5555 >>> b=fun(original_mesh_points)
56- >>> cffd.linconstraint =fun
57- >>> cffd.valconstraint =b
56+ >>> cffd.fun =fun
57+ >>> cffd.fixval =b
5858 >>> cffd.indices=np.arange(np.prod(cffd.n_control_points)*3).tolist()
5959 >>> cffd.M=np.eye(len(cffd.indices))
6060 >>> new_mesh_points = cffd(original_mesh_points)
6161 >>> assert np.isclose(np.linalg.norm(fun(new_mesh_points)-b),np.array([0.]))
6262 """
63-
6463 def __init__ (self , n_control_points = None ):
6564 super ().__init__ (n_control_points )
66- self .linconstraint = None
67- self .valconstraint = None
65+ self .fun = None
66+ self .fixval = None
6867 self .indices = None
6968 self .M = None
7069
7170 def __call__ (self , src_pts ):
72- saved_parameters = self ._save_parameters ()
71+ saved_parameters = self ._save_parameters ()
7372 A , b = self ._compute_linear_map (src_pts , saved_parameters .copy ())
7473 d = A @ saved_parameters [self .indices ] + b
75- deltax = np .linalg .inv (self .M ) @ A .T @ np .linalg .inv ((A @ np .linalg .inv (self .M )@ A .T )) @ (self .valconstraint - d )
74+ deltax = np .linalg .inv (self .M ) @ A .T @ np .linalg .inv (
75+ (A @ np .linalg .inv (self .M ) @ A .T )) @ (self .fixval - d )
7676 saved_parameters [self .indices ] = saved_parameters [self .indices ] + deltax
7777 self ._load_parameters (saved_parameters )
7878 return self .ffd (src_pts )
7979
80- def ffd (self ,src_pts ):
80+ def ffd (self , src_pts ):
8181 '''
8282 Performs Classic Free Form Deformation.
8383
@@ -87,35 +87,35 @@ def ffd(self,src_pts):
8787 '''
8888 return super ().__call__ (src_pts )
8989
90-
9190 def _save_parameters (self ):
9291 '''
9392 Saves the FFD control points in an array of shape [n_x,ny,nz,3].
9493
9594 :return: the FFD control points in an array of shape [n_x,ny,nz,3].
9695 :rtype: numpy.ndarray
9796 '''
98- tmp = np .zeros ([* self .n_control_points ,3 ])
97+ tmp = np .zeros ([* self .n_control_points , 3 ])
9998 tmp [:, :, :, 0 ] = self .array_mu_x
10099 tmp [:, :, :, 1 ] = self .array_mu_y
101100 tmp [:, :, :, 2 ] = self .array_mu_z
102101 return tmp .reshape (- 1 )
103-
104- def _load_parameters (self ,tmp ):
102+
103+ def _load_parameters (self , tmp ):
105104 '''
106105 Loads the FFD control points from an array of shape [n_x,ny,nz,3].
107106
108107 :param np.ndarray tmp: the array of FFD control points.
109108 :rtype: None
110109 '''
111- tmp = tmp .reshape (* self .n_control_points ,3 )
110+ tmp = tmp .reshape (* self .n_control_points , 3 )
112111 self .array_mu_x = tmp [:, :, :, 0 ]
113112 self .array_mu_y = tmp [:, :, :, 1 ]
114113 self .array_mu_z = tmp [:, :, :, 2 ]
115114
116115
117116# I see that a similar function already exists in pygem.utils, but it does not work for inputs and outputs of different dimensions
118- def _compute_linear_map (self ,src_pts ,saved_parameters ):
117+
118+ def _compute_linear_map (self , src_pts , saved_parameters ):
119119 '''
120120 Computes the coefficient and the intercept of the linear map from the control points to the output.
121121
@@ -124,21 +124,25 @@ def _compute_linear_map(self,src_pts,saved_parameters):
124124 :return: a tuple containing the coefficient and the intercept.
125125 :rtype: tuple(np.ndarray,np.ndarray)
126126 '''
127- saved_parameters_bak = saved_parameters .copy () #saving ffd parameters
128- n_indices = len (self .indices )
129- inputs = np .zeros ([n_indices + 1 ,n_indices + 1 ])
130- outputs = np .zeros ([n_indices + 1 ,self .valconstraint .shape [0 ]])
127+ n_indices = len (self .indices )
128+ inputs = np .zeros ([n_indices + 1 , n_indices + 1 ])
129+ outputs = np .zeros ([n_indices + 1 , self .fixval .shape [0 ]])
131130 np .random .seed (0 )
132- for i in range (n_indices + 1 ): ##now we generate the interpolation points
133- tmp = np .random .rand (1 ,n_indices )
134- tmp = tmp .reshape (1 ,- 1 )
135- inputs [i ]= np .hstack ([tmp , np .ones ((tmp .shape [0 ], 1 ))]) #dependent variable
136- saved_parameters [self .indices ]= tmp
137- self ._load_parameters (saved_parameters ) #loading the depent variable as a control point
138- def_pts = super ().__call__ (src_pts ) #computing the deformation with the dependent variable
139- outputs [i ]= self .linconstraint (def_pts ) #computing the independent variable
140- sol = np .linalg .lstsq (inputs ,outputs ,rcond = None ) #computation of the linear map
141- A = sol [0 ].T [:,:- 1 ] #coefficient
142- b = sol [0 ].T [:,- 1 ] #intercept
143- self ._load_parameters (saved_parameters_bak ) #restoring the original FFD parameters
144- return A ,b
131+ for i in range (n_indices +
132+ 1 ): ##now we generate the interpolation points
133+ tmp = np .random .rand (1 , n_indices )
134+ tmp = tmp .reshape (1 , - 1 )
135+ inputs [i ] = np .hstack ([tmp , np .ones (
136+ (tmp .shape [0 ], 1 ))]) #dependent variable
137+ saved_parameters [self .indices ] = tmp
138+ self ._load_parameters (
139+ saved_parameters
140+ ) #loading the depent variable as a control point
141+ def_pts = super ().__call__ (
142+ src_pts ) #computing the deformation with the dependent variable
143+ outputs [i ] = self .fun (def_pts ) #computing the independent variable
144+ sol = np .linalg .lstsq (inputs , outputs ,
145+ rcond = None ) #computation of the linear map
146+ A = sol [0 ].T [:, :- 1 ] #coefficient
147+ b = sol [0 ].T [:, - 1 ] #intercept
148+ return A , b
0 commit comments