def test_affine_points_fit_generic(self): p_start = np.array([[1,.5,-.3], [0,2,4], [-1,0.,-1.5], [1,-4,.5]]) p_end = np.array([[0,1,0], [-1,0,0], [0,0,1], [0,0,0]]) v_test = np.array([1., 2., 3.]) v_exact = np.array([-0.68443497, 0.7249467 , -0.34221748]) transformation = at.affine_points_fit(p_start, p_end) v_trans = transformation(v_test) np.testing.assert_array_almost_equal(v_exact, v_trans)
def test_affine_points_fit_identity_2(self): p_start = np.array([[1,.5,-.3], [0,2,4], [-1,0.,-1.5], [1,-4,.5]]) p_end = p_start v_test = np.array([-1., 2.5, .3]) v_exact = v_test transformation = at.affine_points_fit(p_start, p_end) v_trans = transformation(v_test) np.testing.assert_array_almost_equal(v_exact, v_trans)
def test_affine_points_fit_rotation(self): p_start = np.array([[1,0,0], [0,1,0], [0,0,1], [0,0,0]]) p_end = np.array([[0,1,0], [-1,0,0], [0,0,1], [0,0,0]]) v_test = np.array([1., 2., 3.]) v_exact = np.array([-2., 1., 3.]) transformation = at.affine_points_fit(p_start, p_end) v_trans = transformation(v_test) np.testing.assert_array_almost_equal(v_exact, v_trans)
def test_affine_points_fit_rotation(self): p_start = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]]) p_end = np.array([[0, 1, 0], [-1, 0, 0], [0, 0, 1], [0, 0, 0]]) v_test = np.array([1., 2., 3.]) v_exact = np.array([-2., 1., 3.]) transformation = at.affine_points_fit(p_start, p_end) v_trans = transformation(v_test) np.testing.assert_array_almost_equal(v_exact, v_trans)
def test_affine_points_fit_generic(self): p_start = np.array([[1, .5, -.3], [0, 2, 4], [-1, 0., -1.5], [1, -4, .5]]) p_end = np.array([[0, 1, 0], [-1, 0, 0], [0, 0, 1], [0, 0, 0]]) v_test = np.array([1., 2., 3.]) v_exact = np.array([-0.68443497, 0.7249467, -0.34221748]) transformation = at.affine_points_fit(p_start, p_end) v_trans = transformation(v_test) np.testing.assert_array_almost_equal(v_exact, v_trans)
def test_affine_points_fit_identity_2(self): p_start = np.array([[1, .5, -.3], [0, 2, 4], [-1, 0., -1.5], [1, -4, .5]]) p_end = p_start v_test = np.array([-1., 2.5, .3]) v_exact = v_test transformation = at.affine_points_fit(p_start, p_end) v_trans = transformation(v_test) np.testing.assert_array_almost_equal(v_exact, v_trans)
def perform(self): """ This method performs the deformation on the mesh points. After the execution it sets `self.modified_mesh_points`. .. todo:: """ # translation and then affine transformation translation = self.parameters.origin_box physical_frame = np.array([ self.parameters.position_vertex_1 - translation, self.parameters.position_vertex_2 - translation, self.parameters.position_vertex_3 - translation, [0, 0, 0] ]) reference_frame = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]]) transformation = at.affine_points_fit(physical_frame, reference_frame) inverse_transformation = at.affine_points_fit(reference_frame, physical_frame) # apply transformation to original mesh points reference_frame_mesh_points = self._transform_points( self.original_mesh_points - translation, transformation) # select mesh points inside bounding box mesh_points = reference_frame_mesh_points[ (reference_frame_mesh_points[:, 0] >= 0.) & (reference_frame_mesh_points[:, 0] <= 1.) & (reference_frame_mesh_points[:, 1] >= 0.) & (reference_frame_mesh_points[:, 1] <= 1.) & (reference_frame_mesh_points[:, 2] >= 0.) & (reference_frame_mesh_points[:, 2] <= 1.)] (n_rows_mesh, n_cols_mesh) = mesh_points.shape # Initialization. In order to exploit the contiguity in memory the following are transposed (dim_n_mu, dim_m_mu, dim_t_mu) = self.parameters.array_mu_x.shape bernstein_x = np.zeros((dim_n_mu, n_rows_mesh)) bernstein_y = np.zeros((dim_m_mu, n_rows_mesh)) bernstein_z = np.zeros((dim_t_mu, n_rows_mesh)) shift_mesh_points = np.zeros((n_cols_mesh, n_rows_mesh)) for i in range(0, dim_n_mu): aux1 = np.power((1 - mesh_points[:, 0]), dim_n_mu - 1 - i) aux2 = np.power(mesh_points[:, 0], i) bernstein_x[i, :] = special.binom(dim_n_mu - 1, i) * np.multiply( aux1, aux2) for i in range(0, dim_m_mu): aux1 = np.power((1 - mesh_points[:, 1]), dim_m_mu - 1 - i) aux2 = np.power(mesh_points[:, 1], i) bernstein_y[i, :] = special.binom(dim_m_mu - 1, i) * np.multiply( aux1, aux2) for i in range(0, dim_t_mu): aux1 = np.power((1 - mesh_points[:, 2]), dim_t_mu - 1 - i) aux2 = np.power(mesh_points[:, 2], i) bernstein_z[i, :] = special.binom(dim_t_mu - 1, i) * np.multiply( aux1, aux2) aux_x = 0. aux_y = 0. aux_z = 0. for j in range(0, dim_m_mu): for k in range(0, dim_t_mu): bernstein_yz = np.multiply(bernstein_y[j, :], bernstein_z[k, :]) for i in range(0, dim_n_mu): aux = np.multiply(bernstein_x[i, :], bernstein_yz) aux_x += aux * self.parameters.array_mu_x[i, j, k] aux_y += aux * self.parameters.array_mu_y[i, j, k] aux_z += aux * self.parameters.array_mu_z[i, j, k] shift_mesh_points[0, :] += aux_x shift_mesh_points[1, :] += aux_y shift_mesh_points[2, :] += aux_z # shift_mesh_points needs to be transposed to be summed with mesh_points # apply inverse transformation to shifted mesh points new_mesh_points = self._transform_points(np.transpose(shift_mesh_points) + mesh_points, inverse_transformation) + \ translation # merge non-shifted mesh points with shifted ones self.modified_mesh_points = np.copy(self.original_mesh_points) self.modified_mesh_points[(reference_frame_mesh_points[:,0] >= 0.) & (reference_frame_mesh_points[:,0] <= 1.) & (reference_frame_mesh_points[:,1] >= 0.) & (reference_frame_mesh_points[:,1] <= 1.) & (reference_frame_mesh_points[:,2] >= 0.) & (reference_frame_mesh_points[:,2] <= 1.)] \ = new_mesh_points
def test_affine_points_fit_under_determined_system_2(self): p_start = np.array([[1,0,0], [0,1,0]]) p_end = np.array([[0,1,0], [-1,0,0]]) with self.assertRaises(RuntimeError): transformation = at.affine_points_fit(p_start, p_end)
def test_affine_points_fit_right_points_size(self): p_start = np.array([[1,0,0], [0,1,0], [0,0,1], [0,0,0]]) p_end = np.array([[0,1,0], [-1,0,0], [0,0,1]]) with self.assertRaises(RuntimeError): transformation = at.affine_points_fit(p_start, p_end)
def perform(self): """ This method performs the deformation on the mesh points. After the execution it sets `self.modified_mesh_points`. .. todo:: In order to improve the performances, we need to perform the FFD only on the points inside the lattice. """ (n_rows_mesh, n_cols_mesh) = self.original_mesh_points.shape # translation and then affine transformation translation = self.parameters.origin_box fisical_frame = np.array([self.parameters.position_vertex_1 - translation, \ self.parameters.position_vertex_2 - translation, \ self.parameters.position_vertex_3 - translation, \ [0, 0, 0]]) reference_frame = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]]) transformation = at.affine_points_fit(fisical_frame, reference_frame) inverse_transformation = at.affine_points_fit(reference_frame, fisical_frame) # Initialization. In order to exploit the contiguity in memory the following are transposed (dim_n_mu, dim_m_mu, dim_t_mu) = self.parameters.array_mu_x.shape bernstein_x = np.zeros((dim_n_mu, n_rows_mesh)) bernstein_y = np.zeros((dim_m_mu, n_rows_mesh)) bernstein_z = np.zeros((dim_t_mu, n_rows_mesh)) shift_mesh_points = np.zeros((n_cols_mesh, n_rows_mesh)) reference_frame_mesh_points = self._transform_points(self.original_mesh_points - translation, \ transformation) for i in range(0, dim_n_mu): aux1 = np.power((1-reference_frame_mesh_points[:, 0]), dim_n_mu-1-i) aux2 = np.power(reference_frame_mesh_points[:, 0], i) bernstein_x[i, :] = special.binom(dim_n_mu-1, i) * np.multiply(aux1, aux2) for i in range(0, dim_m_mu): aux1 = np.power((1-reference_frame_mesh_points[:, 1]), dim_m_mu-1-i) aux2 = np.power(reference_frame_mesh_points[:, 1], i) bernstein_y[i, :] = special.binom(dim_m_mu-1, i) * np.multiply(aux1, aux2) for i in range(0, dim_t_mu): aux1 = np.power((1-reference_frame_mesh_points[:, 2]), dim_t_mu-1-i) aux2 = np.power(reference_frame_mesh_points[:, 2], i) bernstein_z[i, :] = special.binom(dim_t_mu-1, i) * np.multiply(aux1, aux2) aux_x = 0. aux_y = 0. aux_z = 0. for j in range(0, dim_m_mu): for k in range(0, dim_t_mu): bernstein_yz = np.multiply(bernstein_y[j, :], bernstein_z[k, :]) for i in range(0, dim_n_mu): aux = np.multiply(bernstein_x[i, :], bernstein_yz) aux_x += aux * self.parameters.array_mu_x[i, j, k] aux_y += aux * self.parameters.array_mu_y[i, j, k] aux_z += aux * self.parameters.array_mu_z[i, j, k] shift_mesh_points[0, :] += aux_x shift_mesh_points[1, :] += aux_y shift_mesh_points[2, :] += aux_z # Splitting points inside and outside the lattice: TODO not very efficient for i in range(0, n_rows_mesh): if (reference_frame_mesh_points[i, 0] < 0) or (reference_frame_mesh_points[i, 1] < 0) \ or (reference_frame_mesh_points[i, 2] < 0) or (reference_frame_mesh_points[i, 0] > 1) \ or (reference_frame_mesh_points[i, 1] > 1) or (reference_frame_mesh_points[i, 2] > 1): shift_mesh_points[:, i] = 0 # Here shift_mesh_points needs to be transposed to be summed with reference_frame_mesh_points self.modified_mesh_points = self._transform_points(np.transpose(shift_mesh_points) + \ reference_frame_mesh_points, inverse_transformation) + translation
def test_affine_points_fit_under_determined_system_2(self): p_start = np.array([[1, 0, 0], [0, 1, 0]]) p_end = np.array([[0, 1, 0], [-1, 0, 0]]) with self.assertRaises(RuntimeError): transformation = at.affine_points_fit(p_start, p_end)
def test_affine_points_fit_right_points_size(self): p_start = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]]) p_end = np.array([[0, 1, 0], [-1, 0, 0], [0, 0, 1]]) with self.assertRaises(RuntimeError): transformation = at.affine_points_fit(p_start, p_end)
def perform(self): """ This method performs the deformation on the mesh points. After the execution it sets `self.modified_mesh_points`. """ # translation and then affine transformation translation = self.parameters.box_origin physical_frame = self.parameters.position_vertices - translation reference_frame = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]]) transformation = at.affine_points_fit(physical_frame, reference_frame) inverse_transformation = at.affine_points_fit(reference_frame, physical_frame) # apply transformation to original mesh points reference_frame_mesh_points = self._transform_points( self.original_mesh_points - translation, transformation) # select mesh points inside bounding box mesh_points = reference_frame_mesh_points[ (reference_frame_mesh_points[:, 0] >= 0.) & (reference_frame_mesh_points[:, 0] <= 1.) & (reference_frame_mesh_points[:, 1] >= 0.) & (reference_frame_mesh_points[:, 1] <= 1.) & (reference_frame_mesh_points[:, 2] >= 0.) & (reference_frame_mesh_points[:, 2] <= 1.)] (n_rows_mesh, n_cols_mesh) = mesh_points.shape # Initialization. In order to exploit the contiguity in memory the # following are transposed (dim_n_mu, dim_m_mu, dim_t_mu) = self.parameters.array_mu_x.shape bernstein_x = np.zeros((dim_n_mu, n_rows_mesh)) bernstein_y = np.zeros((dim_m_mu, n_rows_mesh)) bernstein_z = np.zeros((dim_t_mu, n_rows_mesh)) shift_mesh_points = np.zeros((n_cols_mesh, n_rows_mesh)) for i in range(0, dim_n_mu): aux1 = np.power((1 - mesh_points[:, 0]), dim_n_mu - 1 - i) aux2 = np.power(mesh_points[:, 0], i) bernstein_x[i, :] = special.binom(dim_n_mu - 1, i) * np.multiply( aux1, aux2) for i in range(0, dim_m_mu): aux1 = np.power((1 - mesh_points[:, 1]), dim_m_mu - 1 - i) aux2 = np.power(mesh_points[:, 1], i) bernstein_y[i, :] = special.binom(dim_m_mu - 1, i) * np.multiply( aux1, aux2) for i in range(0, dim_t_mu): aux1 = np.power((1 - mesh_points[:, 2]), dim_t_mu - 1 - i) aux2 = np.power(mesh_points[:, 2], i) bernstein_z[i, :] = special.binom(dim_t_mu - 1, i) * np.multiply( aux1, aux2) aux_x = 0. aux_y = 0. aux_z = 0. for j in range(0, dim_m_mu): for k in range(0, dim_t_mu): bernstein_yz = np.multiply(bernstein_y[j, :], bernstein_z[k, :]) for i in range(0, dim_n_mu): aux = np.multiply(bernstein_x[i, :], bernstein_yz) aux_x += aux * self.parameters.array_mu_x[i, j, k] aux_y += aux * self.parameters.array_mu_y[i, j, k] aux_z += aux * self.parameters.array_mu_z[i, j, k] shift_mesh_points[0, :] += aux_x shift_mesh_points[1, :] += aux_y shift_mesh_points[2, :] += aux_z # shift_mesh_points needs to be transposed to be summed with mesh_points # apply inverse transformation to shifted mesh points new_mesh_points = self._transform_points( np.transpose(shift_mesh_points) + mesh_points, inverse_transformation) + translation # merge non-shifted mesh points with shifted ones self.modified_mesh_points = np.copy(self.original_mesh_points) self.modified_mesh_points[(reference_frame_mesh_points[:, 0] >= 0.) & (reference_frame_mesh_points[:, 0] <= 1.) & (reference_frame_mesh_points[:, 1] >= 0.) & (reference_frame_mesh_points[:, 1] <= 1.) & (reference_frame_mesh_points[:, 2] >= 0.) & (reference_frame_mesh_points[:, 2] <= 1.)] = new_mesh_points