| 1 | from sys import float_info |
| 2 | import numpy as np |
| 3 | |
| 4 | from .cfmm import cFastMarcher |
| 5 | |
| 6 | FAR, NARROW, FROZEN, MASK = 0, 1, 2, 3 |
| 7 | DISTANCE, TRAVEL_TIME, EXTENSION_VELOCITY = 0, 1, 2 |
| 8 | |
| 9 | |
| 10 | def pre_process_args(phi, dx, narrow, periodic, ext_mask=None): |
| 11 | """ |
| 12 | get input data into the correct form for calling the c extension module |
| 13 | This wrapper allows for a little bit of flexibility in the input types |
| 14 | """ |
| 15 | if not isinstance(phi, np.ndarray): |
| 16 | phi = np.array(phi) |
| 17 | |
| 18 | if type(dx) is float or type(dx) is int: |
| 19 | dx = [dx for _ in range(phi.ndim)] |
| 20 | dx = np.array(dx) |
| 21 | |
| 22 | if isinstance(phi, np.ma.MaskedArray): |
| 23 | flag = np.zeros(phi.shape, dtype=np.dtype("longlong")) |
| 24 | flag[phi.mask] = MASK |
| 25 | phi = phi.data |
| 26 | else: |
| 27 | flag = np.zeros(phi.shape, dtype=np.dtype("longlong")) |
| 28 | |
| 29 | if ext_mask is None: |
| 30 | ext_mask = np.zeros(phi.shape, dtype=np.dtype("longlong")) |
| 31 | |
| 32 | periodic_data = 0 |
| 33 | if isinstance(periodic, bool): |
| 34 | if periodic: |
| 35 | periodic_data = int(2 ** phi.ndim - 1) |
| 36 | else: |
| 37 | if hasattr(periodic, "__len__") and len(periodic) == phi.ndim: |
| 38 | for i, value in enumerate(periodic): |
| 39 | if value: |
| 40 | periodic_data |= 1 << i |
| 41 | else: |
| 42 | raise ValueError("parameter \"periodic\" must be of type bool or sequence of type bool of length phi.ndim.") |
| 43 | |
| 44 | if narrow < 0: |
| 45 | raise ValueError("parameter \"narrow\" must be greater than or equal to zero.") |
| 46 | |
| 47 | return phi, dx, flag, ext_mask, periodic_data |
| 48 | |
| 49 | |
| 50 | def post_process_result(result): |
| 51 | """ |
| 52 | post-process results from the c module (add mask) |
| 53 | """ |
| 54 | if (result == float_info.max).any(): |
| 55 | mask = (result == float_info.max) |
| 56 | result[mask] = 0 |
| 57 | result = np.ma.MaskedArray(result, mask) |
| 58 | return result |
| 59 | |
| 60 | |
| 61 | def distance(phi, dx=1.0, self_test=False, order=2, |
| 62 | narrow=0.0, periodic=False): |
| 63 | """Return the signed distance from the zero contour of the array phi. |
| 64 | |
| 65 | Parameters |
| 66 | ---------- |
| 67 | phi : array-like |
| 68 | the zero contour of this array is the boundary location for |
| 69 | the distance calculation. Phi can of 1,2,3 or higher |
| 70 | dimension and can be a masked array. |
| 71 | |
| 72 | dx : float or an array-like of len phi.ndim, optional |
| 73 | the cell length in each dimension. |
| 74 | |
| 75 | self_test : bool, optional |
| 76 | if True consistency checks are made on the binary min |
| 77 | heap during the calculation. This is used in testing and |
| 78 | results in a slower calculation. |
| 79 | |
| 80 | order : int, optional |
| 81 | order of computational stencil to use in updating points during |
| 82 | the fast marching method. Must be 1 or 2, the default is 2. |
| 83 | |
| 84 | narrow : float, optional |
| 85 | narrow band half-width. If this optional argument is |
| 86 | specified the marching algorithm is limited to within a |
| 87 | given narrow band. If far-field points remain when this |
| 88 | condition is met a masked array is returned. The default |
| 89 | value is 0.0 which means no narrow band limit. |
| 90 | |
| 91 | periodic : bool or an array-like of len phi.ndim, optional |
| 92 | specifies whether and in which directions periodic |
| 93 | boundary conditions are used. True sets periodic |
| 94 | boundary conditions in all directions. An array-like |
| 95 | (interpreted as True or False values) specifies the |
| 96 | absence or presence of periodic boundaries in |
| 97 | individual directions. The default value is False, |
| 98 | i.e., no periodic boundaries in any direction. |
| 99 | |
| 100 | Returns |
| 101 | ------- |
| 102 | d : an array the same shape as phi |
| 103 | contains the signed distance from the zero contour (zero level set) |
| 104 | of phi to each point in the array. The sign is specified by the sign |
| 105 | of phi at the given point. |
| 106 | |
| 107 | """ |
| 108 | phi, dx, flag, ext_mask, periodic = \ |
| 109 | pre_process_args(phi, dx, narrow, periodic) |
| 110 | d = cFastMarcher(phi, dx, flag, None, ext_mask, |
| 111 | int(self_test), DISTANCE, order, narrow, periodic) |
| 112 | d = post_process_result(d) |
| 113 | return d |
| 114 | |
| 115 | |
| 116 | def travel_time(phi, speed, dx=1.0, self_test=False, order=2, |
| 117 | narrow=0.0, periodic=False): |
| 118 | """Return the travel from the zero contour of the array phi given the |
| 119 | scalar velocity field speed. |
| 120 | |
| 121 | Parameters |
| 122 | ---------- |
| 123 | phi : array-like |
| 124 | the zero contour of this array is the boundary location for |
| 125 | the travel time calculation. Phi can of 1,2,3 or higher |
| 126 | dimension and can be a masked array. |
| 127 | |
| 128 | speed : array-like, the same shape as phi |
| 129 | contains the speed of interface propagation at each point |
| 130 | in the domain. |
| 131 | |
| 132 | dx : float or an array-like of len phi.ndim, optional |
| 133 | the cell length in each dimension. |
| 134 | |
| 135 | self_test : bool, optional |
| 136 | if True consistency checks are made on the binary min |
| 137 | heap during the calculation. This is used in testing and |
| 138 | results in a slower calculation. |
| 139 | |
| 140 | order : int, optional |
| 141 | order of computational stencil to use in updating points during |
| 142 | the fast marching method. Must be 1 or 2, the default is 2. |
| 143 | |
| 144 | narrow : float, optional |
| 145 | narrow band half-width. If this optional argument is |
| 146 | specified the marching algorithm is limited to travel |
| 147 | times within a given value. If far-field points |
| 148 | remain when this condition is met a masked array is |
| 149 | returned. The default value is 0.0 which means no narrow |
| 150 | band limit. |
| 151 | |
| 152 | periodic : bool or an array-like of len phi.ndim, optional |
| 153 | specifies whether and in which directions periodic |
| 154 | boundary conditions are used. True sets periodic |
| 155 | boundary conditions in all directions. An array-like |
| 156 | (interpreted as True or False values) specifies the |
| 157 | absence or presence of periodic boundaries in |
| 158 | individual directions. The default value is False, |
| 159 | i.e., no periodic boundaries in any direction. |
| 160 | |
| 161 | Returns |
| 162 | ------- |
| 163 | t : an array the same shape as phi |
| 164 | contains the travel time from the zero contour (zero level |
| 165 | set) of phi to each point in the array given the scalar |
| 166 | velocity field speed. If the input array speed has values less |
| 167 | than or equal to zero the return value will be a masked array. |
| 168 | |
| 169 | """ |
| 170 | phi, dx, flag, ext_mask, periodic \ |
| 171 | = pre_process_args(phi, dx, narrow, periodic) |
| 172 | t = cFastMarcher(phi, dx, flag, speed, ext_mask, |
| 173 | int(self_test), TRAVEL_TIME, order, narrow, periodic) |
| 174 | t = post_process_result(t) |
| 175 | return t |
| 176 | |
| 177 | |
| 178 | def extension_velocities(phi, speed, dx=1.0, self_test=False, order=2, |
| 179 | ext_mask=None, narrow=0.0, periodic=False): |
| 180 | """Extend the velocities defined at the zero contour of phi, in the |
| 181 | normal direction, to the rest of the domain. Extend the velocities |
| 182 | such that grad f_ext dot grad d = 0 where where f_ext is the |
| 183 | extension velocity and d is the signed distance function. |
| 184 | |
| 185 | Parameters |
| 186 | ---------- |
| 187 | phi : array-like |
| 188 | the zero contour of this array is the boundary location for |
| 189 | the travel time calculation. Phi can of 1,2,3 or higher |
| 190 | dimension and can be a masked array. |
| 191 | |
| 192 | speed : array-like, the same shape as phi |
| 193 | contains the speed of interface propagation at each point |
| 194 | in the domain. |
| 195 | |
| 196 | dx : float or an array-like of len phi.ndim, optional |
| 197 | the cell length in each dimension. |
| 198 | |
| 199 | self_test : bool, optional |
| 200 | if True consistency checks are made on the binary min |
| 201 | heap during the calculation. This is used in testing and |
| 202 | results in a slower calculation. |
| 203 | |
| 204 | order : int, optional |
| 205 | order of computational stencil to use in updating points during |
| 206 | the fast marching method. Must be 1 or 2, the default is 2. |
| 207 | |
| 208 | ext_mask : array-like, the same shape as phi, optional |
| 209 | enables initial front values to be eliminated when |
| 210 | calculating the value at the interface before the |
| 211 | values are extended away from the interface. |
| 212 | |
| 213 | narrow : float, optional |
| 214 | narrow band half-width. If this optional argument is |
| 215 | specified the marching algorithm is limited to within a |
| 216 | given narrow band. If far-field points remain when this |
| 217 | condition is met a masked arrays are returned. The default |
| 218 | value is 0.0 which means no narrow band limit. |
| 219 | |
| 220 | periodic : bool or an array-like of len phi.ndim, optional |
| 221 | specifies whether and in which directions periodic |
| 222 | boundary conditions are used. True sets periodic |
| 223 | boundary conditions in all directions. An array-like |
| 224 | (interpreted as True or False values) specifies the |
| 225 | absence or presence of periodic boundaries in |
| 226 | individual directions. The default value is False, |
| 227 | i.e., no periodic boundaries in any direction. |
| 228 | |
| 229 | Returns |
| 230 | ------- |
| 231 | (d, f_ext) : tuple |
| 232 | a tuple containing the signed distance function d and the |
| 233 | extension velocities f_ext. |
| 234 | |
| 235 | """ |
| 236 | phi, dx, flag, ext_mask, periodic = \ |
| 237 | pre_process_args(phi, dx, narrow, periodic, ext_mask) |
| 238 | |
| 239 | distance, f_ext = cFastMarcher(phi, dx, flag, speed, ext_mask, |
| 240 | int(self_test), EXTENSION_VELOCITY, |
| 241 | order, narrow, periodic) |
| 242 | distance = post_process_result(distance) |
| 243 | f_ext = post_process_result(f_ext) |
| 244 | |
| 245 | return distance, f_ext |