Skip to content

Commit f3b26ae

Browse files
authored
Merge pull request #85 from mtezzele/rbf
RBF class and documentation
2 parents f8465da + 743bbc0 commit f3b26ae

File tree

3 files changed

+274
-1
lines changed

3 files changed

+274
-1
lines changed

docs/source/code.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Code Documentation
88

99
affine
1010
freeform
11+
radial
1112
params
1213
filehandler
1314
openfhandler

pygem/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11

2-
__all__ = ['affine', 'filehandler', 'freeform', 'openfhandler', 'params', 'stlhandler', 'unvhandler', 'vtkhandler', 'igeshandler', 'utils', 'gui']
2+
__all__ = ['affine', 'filehandler', 'freeform', 'radial', 'openfhandler', 'params', 'stlhandler', 'unvhandler', 'vtkhandler', 'igeshandler', 'utils', 'gui']
33

44
from . import affine
55
from . import freeform
6+
from . import radial
67
from . import filehandler
78
from . import openfhandler
89
from . import params

pygem/radial.py

Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
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 numpy as np
45+
46+
47+
class RBF(object):
48+
"""
49+
Class that handles the Radial Basis Functions interpolation on the mesh points.
50+
51+
:param RBFParameters rbf_parameters: parameters of the RBF.
52+
:param numpy.ndarray original_mesh_points: coordinates of the original points of the mesh.
53+
54+
:cvar RBFParameters parameters: parameters of the RBF.
55+
:cvar numpy.ndarray original_mesh_points: coordinates of the original points of the mesh.
56+
The shape is `n_points`-by-3.
57+
:cvar numpy.ndarray modified_mesh_points: coordinates of the points of the deformed mesh.
58+
The shape is `n_points`-by-3.
59+
:cvar dict bases: a dictionary that associates the names of the basis functions
60+
implemented to the actual implementation.
61+
:cvar numpy.matrix weights: the matrix formed by the weights corresponding to the a-priori
62+
selected N control points, associated to the basis functions and c and Q terms that
63+
describe the polynomial of order one p(x) = c + Qx. The shape is
64+
(n_control_points+1+3)-by-3. It is computed internally.
65+
66+
:Example:
67+
68+
>>> import pygem.radial as rbf
69+
>>> import pygem.params as rbfp
70+
>>> import numpy as np
71+
72+
>>> rbf_parameters = rbfp.RBFParameters()
73+
>>> rbf_parameters.read_parameters('tests/test_datasets/parameters_rbf_cube.prm')
74+
75+
>>> nx, ny, nz = (20, 20, 20)
76+
>>> mesh = np.zeros((nx * ny * nz, 3))
77+
>>> xv = np.linspace(0, 1, nx)
78+
>>> yv = np.linspace(0, 1, ny)
79+
>>> zv = np.linspace(0, 1, nz)
80+
>>> z, y, x = np.meshgrid(zv, yv, xv)
81+
>>> mesh = np.array([x.ravel(), y.ravel(), z.ravel()])
82+
>>> original_mesh_points = mesh.T
83+
84+
>>> radial_trans = rbf.RBF(rbf_parameters, original_mesh_points)
85+
>>> radial_trans.perform()
86+
>>> new_mesh_points = radial_trans.modified_mesh_points
87+
"""
88+
def __init__(self, rbf_parameters, original_mesh_points):
89+
self.parameters = rbf_parameters
90+
self.original_mesh_points = original_mesh_points
91+
self.modified_mesh_points = None
92+
93+
self.bases = {'gaussian_spline': self.gaussian_spline, \
94+
'multi_quadratic_biharmonic_spline': self.multi_quadratic_biharmonic_spline, \
95+
'inv_multi_quadratic_biharmonic_spline': self.inv_multi_quadratic_biharmonic_spline, \
96+
'thin_plate_spline': self.thin_plate_spline, \
97+
'beckert_wendland_c2_basis': self.beckert_wendland_c2_basis}
98+
99+
# to make the str callable we have to use a dictionary with all the implemented
100+
# radial basis functions
101+
if self.parameters.basis in self.bases:
102+
self.basis = self.bases[self.parameters.basis]
103+
else:
104+
raise NameError('The name of the basis function in the parameters file is not correct ' + \
105+
'or not implemented. Check the documentation for all the available functions.')
106+
107+
self.weights = self._get_weights(self.parameters.original_control_points, \
108+
self.parameters.deformed_control_points)
109+
110+
111+
@staticmethod
112+
def gaussian_spline(X, r):
113+
"""
114+
It implements the following formula:
115+
116+
.. math::
117+
\\varphi(\\| \\boldsymbol{x} \\|) = e^{-\\frac{\\| \\boldsymbol{x} \\|^2}{r^2}}
118+
119+
:param numpy.ndarray X: the vector x in the formula above.
120+
:param float r: the parameter r in the formula above.
121+
122+
:return: result: the result of the formula above.
123+
:rtype: float
124+
"""
125+
norm = np.linalg.norm(X)
126+
result = np.exp(-(norm * norm) / (r * r))
127+
return result
128+
129+
130+
@staticmethod
131+
def multi_quadratic_biharmonic_spline(X, r):
132+
"""
133+
It implements the following formula:
134+
135+
.. math::
136+
\\varphi(\\| \\boldsymbol{x} \\|) = \\sqrt{\\| \\boldsymbol{x} \\|^2 + r^2}
137+
138+
:param numpy.ndarray X: the vector x in the formula above.
139+
:param float r: the parameter r in the formula above.
140+
141+
:return: result: the result of the formula above.
142+
:rtype: float
143+
"""
144+
norm = np.linalg.norm(X)
145+
result = np.sqrt((norm * norm) + (r * r))
146+
return result
147+
148+
149+
@staticmethod
150+
def inv_multi_quadratic_biharmonic_spline(X, r):
151+
"""
152+
It implements the following formula:
153+
154+
.. math::
155+
\\varphi(\\| \\boldsymbol{x} \\|) = (\\| \\boldsymbol{x} \\|^2 + r^2 )^{-\\frac{1}{2}}
156+
157+
:param numpy.ndarray X: the vector x in the formula above.
158+
:param float r: the parameter r in the formula above.
159+
160+
:return: result: the result of the formula above.
161+
:rtype: float
162+
"""
163+
norm = np.linalg.norm(X)
164+
result = 1.0 / (np.sqrt((norm * norm) + (r * r)))
165+
return result
166+
167+
168+
@staticmethod
169+
def thin_plate_spline(X, r):
170+
"""
171+
It implements the following formula:
172+
173+
.. math::
174+
\\varphi(\\| \\boldsymbol{x} \\|) = \\left\\| \\frac{\\boldsymbol{x} }{r} \\right\\|^2
175+
\\ln \\left\\| \\frac{\\boldsymbol{x} }{r} \\right\\|
176+
177+
:param numpy.ndarray X: the vector x in the formula above.
178+
:param float r: the parameter r in the formula above.
179+
180+
:return: result: the result of the formula above.
181+
:rtype: float
182+
"""
183+
arg = X/r
184+
norm = np.linalg.norm(arg)
185+
result = norm * norm
186+
if norm > 0:
187+
result *= np.log(norm)
188+
return result
189+
190+
191+
@staticmethod
192+
def beckert_wendland_c2_basis(X, r):
193+
"""
194+
It implements the following formula:
195+
196+
.. math::
197+
\\varphi(\\| \\boldsymbol{x} \\|) = \\left( 1 - \\frac{\\| \\boldsymbol{x} \\|}{r} \\right)^4_+
198+
\\left( 4 \\frac{\\| \\boldsymbol{x} \\|}{r} + 1 \\right)
199+
200+
:param numpy.ndarray X: the vector x in the formula above.
201+
:param float r: the parameter r in the formula above.
202+
203+
:return: result: the result of the formula above.
204+
:rtype: float
205+
"""
206+
norm = np.linalg.norm(X)
207+
arg = norm / r
208+
first = 0
209+
if (1 - arg) > 0:
210+
first = np.power((1 - arg), 4)
211+
second = (4 * arg) + 1
212+
result = first * second
213+
return result
214+
215+
216+
def _distance_matrix(self, X1, X2):
217+
"""
218+
This private method returns the following matrix:
219+
:math:`\\boldsymbol{D_{ij}} = \\varphi(\\| \\boldsymbol{x_i} - \\boldsymbol{y_j} \\|)`
220+
221+
:param numpy.ndarray X1: the vector x in the formula above.
222+
:param numpy.ndarray X2: the vector y in the formula above.
223+
224+
:return: matrix: the matrix D.
225+
:rtype: numpy.ndarray
226+
"""
227+
m, n = X1.shape[0], X2.shape[0]
228+
matrix = np.zeros(shape=(m, n))
229+
for i in range(0, m):
230+
for j in range(0, n):
231+
matrix[i][j] = self.basis(X1[i] - X2[j], self.parameters.radius)
232+
return matrix
233+
234+
235+
def _get_weights(self, X, Y):
236+
"""
237+
This private method, given the original control points and the deformed ones, returns the matrix
238+
with the weights and the polynomial terms, that is :math:`W`, :math:`c^T` and :math:`Q^T`.
239+
The shape is (n_control_points+1+3)-by-3.
240+
241+
:param numpy.ndarray X: it is an n_control_points-by-3 array with the
242+
coordinates of the original interpolation control points before the deformation.
243+
:param numpy.ndarray Y: it is an n_control_points-by-3 array with the
244+
coordinates of the interpolation control points after the deformation.
245+
246+
:return: weights: the matrix with the weights and the polynomial terms.
247+
:rtype: numpy.matrix
248+
"""
249+
n_points = X.shape[0]
250+
dim = X.shape[1]
251+
identity = np.ones(n_points).reshape(n_points, 1)
252+
dist = self._distance_matrix(X, X)
253+
H = np.bmat([[dist, identity, X], [identity.T, np.zeros((1, 1)), np.zeros((1, dim))], \
254+
[X.T, np.zeros((dim, 1)), np.zeros((dim, dim))]])
255+
rhs = np.bmat([[Y], [np.zeros((1, dim))], [np.zeros((dim, dim))]])
256+
inv_H = np.linalg.inv(H)
257+
weights = np.dot(inv_H, rhs)
258+
return weights
259+
260+
261+
def perform(self):
262+
"""
263+
This method performs the deformation of the mesh points. After the execution
264+
it sets `self.modified_mesh_points`.
265+
"""
266+
n_points = self.original_mesh_points.shape[0]
267+
dist = self._distance_matrix(self.original_mesh_points, self.parameters.original_control_points)
268+
identity = np.ones(n_points).reshape(n_points, 1)
269+
H = np.bmat([[dist, identity, self.original_mesh_points]])
270+
self.modified_mesh_points = np.asarray(np.dot(H, self.weights))
271+

0 commit comments

Comments
 (0)