|
| 1 | +""" |
| 2 | +Module focused on the implementation of the Radial Basis Functions interpolation technique. |
| 3 | +This technique is still based on the use of a set of parameters, the so-called control points, |
| 4 | +as for FFD, but RBF is interpolatory. Another important key point of RBF strategy relies in the |
| 5 | +way we can locate the control points: in fact, instead of FFD where control points need to be |
| 6 | +placed inside a regular lattice, with RBF we hano no more limitations. So we have the possibility |
| 7 | +to perform localized control points refiniments. |
| 8 | +The module is analogous to the freeform one. |
| 9 | +
|
| 10 | +:Theoretical Insight: |
| 11 | +
|
| 12 | + As reference please consult M. D. Buhmann. Radial Basis Functions, volume 12 of Cambridge |
| 13 | + monographs on applied and computational mathematics. Cambridge University Press, UK, 2003. |
| 14 | + RBF shape parametrization technique is based on the definition of a map, |
| 15 | + :math:`\\mathcal{M}(\\boldsymbol{x}) : \\mathbb{R}^n \\rightarrow \\mathbb{R}^n`, that allows the |
| 16 | + possibility of transferring data across non-matching grids and facing the dynamic mesh handling. |
| 17 | + The map introduced is defines as follows |
| 18 | +
|
| 19 | + .. math:: |
| 20 | + \\mathcal{M}(\\boldsymbol{x}) = p(\\boldsymbol{x}) + \\sum_{i=1}^{\\mathcal{N}_C} \\gamma_i |
| 21 | + \\varphi(\\| \\boldsymbol{x} - \\boldsymbol{x_{C_i}} \\|) |
| 22 | +
|
| 23 | + where :math:`p(\\boldsymbol{x})` is a low_degree polynomial term, :math:`\\gamma_i` is the weight, |
| 24 | + corresponding to the a-priori selected :math:`\\mathcal{N}_C` control points, associated to the |
| 25 | + :math:`i`-th basis function, and :math:`\\varphi(\\| \\boldsymbol{x} - \\boldsymbol{x_{C_i}} \\|)` |
| 26 | + a radial function based on the Euclidean distance between the control points position |
| 27 | + :math:`\\boldsymbol{x_{C_i}}` and :math:`\\boldsymbol{x}`. A radial basis function, generally, is |
| 28 | + a real-valued function whose value depends only on the distance from the origin, so that |
| 29 | + :math:`\\varphi(\\boldsymbol{x}) = \\tilde{\\varphi}(\\| \\boldsymbol{x} \\|)`. |
| 30 | +
|
| 31 | + The matrix version of the formula above is: |
| 32 | +
|
| 33 | + .. math:: |
| 34 | + \\mathcal{M}(\\boldsymbol{x}) = \\boldsymbol{c} + \\boldsymbol{Q}\\boldsymbol{x} + |
| 35 | + \\boldsymbol{W^T}\\boldsymbol{d}(\\boldsymbol{x}) |
| 36 | +
|
| 37 | + The idea is that after the computation of the weights and the polynomial terms from the coordinates |
| 38 | + of the control points before and after the deformation, we can deform all the points of the mesh |
| 39 | + accordingly. |
| 40 | + Among the most common used radial basis functions for modelling 2D and 3D shapes, we consider |
| 41 | + Gaussian splines, Multi-quadratic biharmonic splines, Inverted multi-quadratic biharmonic splines, |
| 42 | + Thin-plate splines and Beckert and Wendland :math:`C^2` basis all defined and implemented below. |
| 43 | +""" |
| 44 | +import os |
| 45 | +import params as rbfp |
| 46 | +import numpy as np |
| 47 | +from mpl_toolkits.mplot3d import axes3d |
| 48 | +import matplotlib.pyplot as plt |
| 49 | + |
| 50 | + |
| 51 | +class RBF(object): |
| 52 | + """ |
| 53 | + Class that handles the Radial Basis Functions interpolation on the mesh points. |
| 54 | +
|
| 55 | + :param RBFParameters rbf_parameters: parameters of the RBF. |
| 56 | + :param numpy.ndarray original_mesh_points: coordinates of the original points of the mesh. |
| 57 | +
|
| 58 | + :cvar RBFParameters parameters: parameters of the RBF. |
| 59 | + :cvar numpy.ndarray original_mesh_points: coordinates of the original points of the mesh. |
| 60 | + The shape is `n_points`-by-3. |
| 61 | + :cvar numpy.ndarray modified_mesh_points: coordinates of the points of the deformed mesh. |
| 62 | + The shape is `n_points`-by-3. |
| 63 | + :cvar dict bases: a dictionary that associates the names of the basis functions |
| 64 | + implemented to the actual implementation. |
| 65 | + :cvar numpy.matrix weights: the matrix formed by the weights corresponding to the a-priori |
| 66 | + selected N control points, associated to the basis functions and c and Q terms that |
| 67 | + describe the polynomial of order one p(x) = c + Qx. The shape is |
| 68 | + (n_control_points+1+3)-by-3. It is computed internally. |
| 69 | +
|
| 70 | + :Example: |
| 71 | +
|
| 72 | + >>> import pygem.radial as rbf |
| 73 | + >>> import pygem.params as rbfp |
| 74 | + >>> import numpy as np |
| 75 | +
|
| 76 | + >>> rbf_parameters = rbfp.FFDParameters() |
| 77 | + >>> rbf_parameters.read_parameters('tests/test_datasets/parameters_rbf_cube.prm') |
| 78 | +
|
| 79 | + >>> nx, ny, nz = (20, 20, 20) |
| 80 | + >>> mesh = np.zeros((nx * ny * nz, 3)) |
| 81 | + >>> xv = np.linspace(0, 1, nx) |
| 82 | + >>> yv = np.linspace(0, 1, ny) |
| 83 | + >>> zv = np.linspace(0, 1, nz) |
| 84 | + >>> z, y, x = np.meshgrid(zv, yv, xv) |
| 85 | + >>> mesh = np.array([x.ravel(), y.ravel(), z.ravel()]) |
| 86 | + >>> original_mesh_points = mesh.T |
| 87 | + |
| 88 | + >>> radial_trans = rbf.RBF(rbf_parameters, original_mesh_points) |
| 89 | + >>> radial_trans.perform() |
| 90 | + >>> new_mesh_points = radial_trans.modified_mesh_points |
| 91 | + """ |
| 92 | + def __init__(self, rbf_parameters, original_mesh_points): |
| 93 | + self.parameters = rbf_parameters |
| 94 | + self.original_mesh_points = original_mesh_points |
| 95 | + self.modified_mesh_points = None |
| 96 | + |
| 97 | + self.bases = { |
| 98 | + 'gaussian_spline': self.gaussian_spline, |
| 99 | + 'multi_quadratic_biharmonic_spline': self.multi_quadratic_biharmonic_spline, |
| 100 | + 'inv_multi_quadratic_biharmonic_spline': self.inv_multi_quadratic_biharmonic_spline, |
| 101 | + 'thin_plate_spline': self.thin_plate_spline, |
| 102 | + 'beckert_wendland_c2_basis': self.beckert_wendland_c2_basis |
| 103 | + } |
| 104 | + |
| 105 | + # to make the str callable we have to use a dictionary with all the implemented radial basis functions |
| 106 | + if params.basis in self.bases: |
| 107 | + self.basis = self.bases[params.basis] |
| 108 | + else: |
| 109 | + raise NameError('The name of the basis function in the parameters file is not correct ' + \ |
| 110 | + 'or not implemented. Check the documentation for all the available functions.') |
| 111 | + |
| 112 | + self.weights = self._get_weights(self.parameters.original_control_points, \ |
| 113 | + self.parameters.deformed_control_points) |
| 114 | + |
| 115 | + |
| 116 | + @staticmethod |
| 117 | + def gaussian_spline(X, r): |
| 118 | + """ |
| 119 | + It implements the following formula: |
| 120 | +
|
| 121 | + .. math:: |
| 122 | + \\varphi(\\| \\boldsymbol{x} \\|) = e^{-\\frac{\\| \\boldsymbol{x} \\|^2}{r^2}} |
| 123 | +
|
| 124 | + :param numpy.ndarray X: the vector x in the formula above. |
| 125 | + :param float r: the parameter r in the formula above. |
| 126 | +
|
| 127 | + :return: result: the result of the formula above. |
| 128 | + :rtype: float |
| 129 | + """ |
| 130 | + norm = np.linalg.norm(X) |
| 131 | + result = np.exp( -(norm * norm) / (r * r) ) |
| 132 | + return result |
| 133 | + |
| 134 | + |
| 135 | + @staticmethod |
| 136 | + def multi_quadratic_biharmonic_spline(X, r): |
| 137 | + """ |
| 138 | + It implements the following formula: |
| 139 | +
|
| 140 | + .. math:: |
| 141 | + \\varphi(\\| \\boldsymbol{x} \\|) = \\sqrt{\\| \\boldsymbol{x} \\|^2 + r^2} |
| 142 | +
|
| 143 | + :param numpy.ndarray X: the vector x in the formula above. |
| 144 | + :param float r: the parameter r in the formula above. |
| 145 | +
|
| 146 | + :return: result: the result of the formula above. |
| 147 | + :rtype: float |
| 148 | + """ |
| 149 | + norm = np.linalg.norm(X) |
| 150 | + result = np.sqrt( (norm * norm) + (r * r) ) |
| 151 | + return result |
| 152 | + |
| 153 | + |
| 154 | + @staticmethod |
| 155 | + def inv_multi_quadratic_biharmonic_spline(X, r): |
| 156 | + """ |
| 157 | + It implements the following formula: |
| 158 | +
|
| 159 | + .. math:: |
| 160 | + \\varphi(\\| \\boldsymbol{x} \\|) = (\\| \\boldsymbol{x} \\|^2 + r^2 )^{-\\frac{1}{2}} |
| 161 | +
|
| 162 | + :param numpy.ndarray X: the vector x in the formula above. |
| 163 | + :param float r: the parameter r in the formula above. |
| 164 | +
|
| 165 | + :return: result: the result of the formula above. |
| 166 | + :rtype: float |
| 167 | + """ |
| 168 | + result = 1.0/multi_quadratic_biharmonic_spline(X, r) |
| 169 | + return result |
| 170 | + |
| 171 | + |
| 172 | + @staticmethod |
| 173 | + def thin_plate_spline(X, r): |
| 174 | + """ |
| 175 | + It implements the following formula: |
| 176 | +
|
| 177 | + .. math:: |
| 178 | + \\varphi(\\| \\boldsymbol{x} \\|) = \\left\\| \\frac{\\boldsymbol{x} }{r} \\right\\|^2 |
| 179 | + \\ln \\left\\| \\frac{\\boldsymbol{x} }{r} \\right\\| |
| 180 | +
|
| 181 | + :param numpy.ndarray X: the vector x in the formula above. |
| 182 | + :param float r: the parameter r in the formula above. |
| 183 | +
|
| 184 | + :return: result: the result of the formula above. |
| 185 | + :rtype: float |
| 186 | + """ |
| 187 | + arg = X/r |
| 188 | + norm = np.linalg.norm(arg) |
| 189 | + result = norm * norm |
| 190 | + if norm > 0: |
| 191 | + result *= np.log(norm) |
| 192 | + return result |
| 193 | + |
| 194 | + |
| 195 | + @staticmethod |
| 196 | + def beckert_wendland_c2_basis(X, r): |
| 197 | + """ |
| 198 | + It implements the following formula: |
| 199 | +
|
| 200 | + .. math:: |
| 201 | + \\varphi(\\| \\boldsymbol{x} \\|) = \\left( 1 - \\frac{\\| \\boldsymbol{x} \\|}{r} \\right)^4_+ |
| 202 | + \\left( 4 \\frac{\\| \\boldsymbol{x} \\|}{r} + 1 \\right) |
| 203 | +
|
| 204 | + :param numpy.ndarray X: the vector x in the formula above. |
| 205 | + :param float r: the parameter r in the formula above. |
| 206 | +
|
| 207 | + :return: result: the result of the formula above. |
| 208 | + :rtype: float |
| 209 | + """ |
| 210 | + norm = np.linalg.norm(X) |
| 211 | + arg = norm / r |
| 212 | + first = 0 |
| 213 | + if (1 - arg) > 0: |
| 214 | + first = np.power((1 - arg), 4) |
| 215 | + second = (4 * arg) + 1 |
| 216 | + result = first * second |
| 217 | + return result |
| 218 | + |
| 219 | + |
| 220 | + def _distance_matrix(self, X1, X2): |
| 221 | + """ |
| 222 | + This private method returns the following matrix: |
| 223 | + :math:`\\boldsymbol{D_{ij}} = \\varphi(\\| \\boldsymbol{x_i} - \\boldsymbol{y_j} \\|)` |
| 224 | +
|
| 225 | + :param numpy.ndarray X1: the vector x in the formula above. |
| 226 | + :param numpy.ndarray X2: the vector y in the formula above. |
| 227 | +
|
| 228 | + :return: matrix: the matrix D. |
| 229 | + :rtype: numpy.ndarray |
| 230 | + """ |
| 231 | + m, n = X1.shape[0], X2.shape[0] |
| 232 | + matrix = np.zeros(shape=(m, n)) |
| 233 | + for i in range(0, m): |
| 234 | + for j in range(0, n): |
| 235 | + matrix[i][j] = self.basis(X1[i] - X2[j], self.parameters.radius) |
| 236 | + return matrix |
| 237 | + |
| 238 | + |
| 239 | + def _get_weights(self, X, Y): |
| 240 | + """ |
| 241 | + This private method, given the original control points and the deformed ones, returns the matrix |
| 242 | + with the weights and the polynomial terms, that is :math:`W`, :math:`c^T` and :math:`Q^T`. |
| 243 | + The shape is (n_control_points+1+3)-by-3. |
| 244 | +
|
| 245 | + :param numpy.ndarray X: it is an n_control_points-by-3 array with the |
| 246 | + coordinates of the original interpolation control points before the deformation. |
| 247 | + :param numpy.ndarray Y: it is an n_control_points-by-3 array with the |
| 248 | + coordinates of the interpolation control points after the deformation. |
| 249 | +
|
| 250 | + :return: weights: the matrix with the weights and the polynomial terms. |
| 251 | + :rtype: numpy.matrix |
| 252 | + """ |
| 253 | + n_points = X.shape[0] |
| 254 | + dim = X.shape[1] |
| 255 | + identity = np.ones(n_points).reshape(n_points, 1) |
| 256 | + dist = self._distance_matrix(X, X) |
| 257 | + H = np.bmat([[dist, identity, X], [identity.T, np.zeros((1, 1)), np.zeros((1, dim))], \ |
| 258 | + [X.T, np.zeros((dim, 1)), np.zeros((dim, dim))]]) |
| 259 | + rhs = np.bmat([[Y], [np.zeros((1, dim))], [np.zeros((dim, dim))]]) |
| 260 | + inv_H = np.linalg.inv(H) |
| 261 | + weights = np.dot(inv_H, rhs) |
| 262 | + return weights |
| 263 | + |
| 264 | + |
| 265 | + def perform(self): |
| 266 | + """ |
| 267 | + This method performs the deformation of the mesh points. After the execution |
| 268 | + it sets `self.modified_mesh_points`. |
| 269 | + """ |
| 270 | + n_points = self.original_mesh_points.shape[0] |
| 271 | + dim = self.original_mesh_points.shape[1] |
| 272 | + dist = self._distance_matrix(self.original_mesh_points, self.parameters.original_control_points) |
| 273 | + identity = np.ones(n_points).reshape(n_points, 1) |
| 274 | + H = np.bmat([[dist, identity, self.original_mesh_points]]) |
| 275 | + self.modified_mesh_points = np.asarray(np.dot(H, self.weights)) |
| 276 | + |
0 commit comments