|
| 1 | +# Copyright 2025 Mechanics of Microstructures Group |
| 2 | +# at The University of Manchester |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +from numba import njit |
| 17 | +import numpy as np |
| 18 | + |
| 19 | + |
| 20 | +@njit |
| 21 | +def find_first(arr): |
| 22 | + for i in range(len(arr)): |
| 23 | + if arr[i]: |
| 24 | + return i |
| 25 | + |
| 26 | + |
| 27 | +@njit |
| 28 | +def flood_fill(seed, index, points_remaining, grains, boundary_x, boundary_y, |
| 29 | + added_coords): |
| 30 | + """Flood fill algorithm that uses the x and y boundary arrays to |
| 31 | + fill a connected area around the seed point. The points are inserted |
| 32 | + into a grain object and the grain map array is updated. |
| 33 | +
|
| 34 | + Parameters |
| 35 | + ---------- |
| 36 | + seed : tuple of 2 int |
| 37 | + Seed point x for flood fill |
| 38 | + index : int |
| 39 | + Value to fill in grain map |
| 40 | + points_remaining : numpy.ndarray |
| 41 | + Boolean map of the points that have not been assigned a grain yet |
| 42 | +
|
| 43 | + Returns |
| 44 | + ------- |
| 45 | + grain : defdap.ebsd.Grain |
| 46 | + New grain object with points added |
| 47 | + """ |
| 48 | + x, y = seed |
| 49 | + grains[y, x] = index |
| 50 | + points_remaining[y, x] = False |
| 51 | + edge = [seed] |
| 52 | + added_coords[0] = seed |
| 53 | + npoints = 1 |
| 54 | + |
| 55 | + while edge: |
| 56 | + x, y = edge.pop() |
| 57 | + moves = [(x+1, y), (x-1, y), (x, y+1), (x, y-1)] |
| 58 | + # get rid of any that go out of the map area |
| 59 | + if x <= 0: |
| 60 | + moves.pop(1) |
| 61 | + elif x > grains.shape[1] - 2: |
| 62 | + moves.pop(0) |
| 63 | + if y <= 0: |
| 64 | + moves.pop(-1) |
| 65 | + elif y > grains.shape[0] - 2: |
| 66 | + moves.pop(-2) |
| 67 | + |
| 68 | + for (s, t) in moves: |
| 69 | + if grains[t, s] > 0: |
| 70 | + continue |
| 71 | + |
| 72 | + add_point = False |
| 73 | + |
| 74 | + if t == y: |
| 75 | + # moving horizontally |
| 76 | + if s > x: |
| 77 | + # moving right |
| 78 | + add_point = not boundary_x[y, x] |
| 79 | + else: |
| 80 | + # moving left |
| 81 | + add_point = not boundary_x[t, s] |
| 82 | + else: |
| 83 | + # moving vertically |
| 84 | + if t > y: |
| 85 | + # moving down |
| 86 | + add_point = not boundary_y[y, x] |
| 87 | + else: |
| 88 | + # moving up |
| 89 | + add_point = not boundary_y[t, s] |
| 90 | + |
| 91 | + if add_point: |
| 92 | + added_coords[npoints] = s, t |
| 93 | + grains[t, s] = index |
| 94 | + points_remaining[t, s] = False |
| 95 | + npoints += 1 |
| 96 | + edge.append((s, t)) |
| 97 | + |
| 98 | + return added_coords[:npoints] |
| 99 | + |
| 100 | + |
| 101 | +@njit |
| 102 | +def flood_fill_dic(seed, index, points_remaining, grains, added_coords): |
| 103 | + """Flood fill algorithm that uses the combined x and y boundary array |
| 104 | + to fill a connected area around the seed point. The points are returned and |
| 105 | + the grain map array is updated. |
| 106 | +
|
| 107 | + Parameters |
| 108 | + ---------- |
| 109 | + seed : tuple of 2 int |
| 110 | + Seed point x for flood fill |
| 111 | + index : int |
| 112 | + Value to fill in grain map |
| 113 | + points_remaining : numpy.ndarray |
| 114 | + Boolean map of the points remaining to assign a grain yet |
| 115 | + grains : numpy.ndarray |
| 116 | + added_coords : numpy.ndarray |
| 117 | + Buffer for points in the grain |
| 118 | +
|
| 119 | + Returns |
| 120 | + ------- |
| 121 | + numpy.ndarray |
| 122 | + Flooded points (n, 2) |
| 123 | +
|
| 124 | + """ |
| 125 | + # add first point to the grain |
| 126 | + x, y = seed |
| 127 | + grains[y, x] = index |
| 128 | + points_remaining[y, x] = False |
| 129 | + edge = [seed] |
| 130 | + added_coords[0] = seed |
| 131 | + npoints = 1 |
| 132 | + |
| 133 | + while edge: |
| 134 | + x, y = edge.pop() |
| 135 | + |
| 136 | + moves = [(x+1, y), (x-1, y), (x, y+1), (x, y-1)] |
| 137 | + # get rid of any that go out of the map area |
| 138 | + if x <= 0: |
| 139 | + moves.pop(1) |
| 140 | + elif x >= grains.shape[1] - 1: |
| 141 | + moves.pop(0) |
| 142 | + if y <= 0: |
| 143 | + moves.pop(-1) |
| 144 | + elif y >= grains.shape[0] - 1: |
| 145 | + moves.pop(-2) |
| 146 | + |
| 147 | + for (s, t) in moves: |
| 148 | + add_point = False |
| 149 | + |
| 150 | + if grains[t, s] == 0: |
| 151 | + add_point = True |
| 152 | + edge.append((s, t)) |
| 153 | + |
| 154 | + elif grains[t, s] == -1 and (s > x or t > y): |
| 155 | + add_point = True |
| 156 | + |
| 157 | + if add_point: |
| 158 | + added_coords[npoints] = (s, t) |
| 159 | + grains[t, s] = index |
| 160 | + points_remaining[t, s] = False |
| 161 | + npoints += 1 |
| 162 | + |
| 163 | + return added_coords[:npoints] |
0 commit comments