def matter(cls, m: Matter) -> Matter: assert m.n_color() <= 2 if m.is_mesh: return m elif m.n_color() == 0: return m elif m.n_color() == 1: color_count = m.color_count() for c in range(10): if color_count[c] > 0 and c != m.background_color: base_color = c new_matter: Matter = m.copy() new_values = m.background_color * np.ones(m.shape, dtype=np.int) new_values[m.values == m.background_color] = base_color new_matter.set_values(new_values) return new_matter else: # m.n_color() == 2 color_count = m.color_count() switch = [] for c in range(10): if color_count[c] > 0 and c != m.background_color: switch.append(c) new_matter: Matter = m.copy() new_values = m.background_color * np.ones(m.shape, dtype=np.int) new_values[m.values == switch[0]] = switch[1] new_values[m.values == switch[1]] = switch[0] new_matter.set_values(new_values) return new_matter
def point_cross_cnt_mat(m: Matter, y_arr: np.array) -> Tuple[np.array, int]: assert m.n_color() == 1 color_cnt = m.color_count() color_cnt[m.background_color] = 0 m_color = [c for c in range(10) if color_cnt[c] > 0][0] x_arr = np.zeros(y_arr.shape, dtype=np.int) try: x_arr[m.x0:m.x0 + m.shape[0], m.y0:m.y0 + m.shape[1]] = m.values except: print(x_arr, y_arr, m.x0, m.x0 + m.shape[0], m.y0, m.y0 + m.shape[1]) raise return point_cross_cnt_arr(x_arr, y_arr, m_color), m_color
def point_cross_fit_mat(m: Matter, n_row, n_col, op_arr: np.array) -> Matter: assert m.n_color() == 1 color_cnt = m.color_count() color_cnt[m.background_color] = 0 m_color = [c for c in range(10) if color_cnt[c] > 0][0] x_arr = np.zeros((n_row, n_col), dtype=np.int) x_arr[m.x0:m.x0 + m.shape[0], m.y0:m.y0 + m.shape[1]] = m.values res_arr = point_cross_fit_arr(x_arr, m_color, op_arr[m_color]) new_values = np.zeros(x_arr.shape, dtype=int) new_values[res_arr == 1] = m_color new_values[res_arr == 0] = m.background_color new_matter = m.copy() new_matter.set_values(new_values) new_matter.x0 = 0 new_matter.y0 = 0 return new_matter
def __init__(self, m: Matter, ind: int, hash_count: int): self.a_init = m.a self.n_color = m.n_color() self.n_cell = m.n_cell() self.size = m.shape[0] * m.shape[1] self.is_filled_rectangle = m.is_filled_rectangle() if m.is_filled_rectangle(): if m.is_square(): self.is_rectangle = 2 else: self.is_rectangle = 1 else: if m.a is not None: self.is_rectangle = 0 else: self.is_rectangle = None # assume trimmed x_arr = m.values # row res_row = 0 for i in range(x_arr.shape[0] // 2): res_row += (x_arr[i, :] != x_arr[x_arr.shape[0] - 1 - i, :]).sum() # col res_col = 0 for j in range(x_arr.shape[1] // 2): res_col += (x_arr[:, j] != x_arr[:, x_arr.shape[1] - 1 - j]).sum() res = 0 if res_row == 0: res += 1 if res_col == 0: res += 2 self.is_symmetry = res self.ind = ind self.hash = hash_count self.n_noise = (m.values != m.max_color()).sum() self.a = None self.y = None