Spaces:
Sleeping
Sleeping
File size: 6,669 Bytes
5543795 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
from __future__ import annotations
from dataclasses import dataclass
from typing import Dict, List, Sequence, Set, Tuple
import networkx as nx
import numpy as np
Array = np.ndarray
@dataclass
class Cell:
address: Tuple[int, ...]
vertex_positions: List[Array]
level: int
vertex_ids: Tuple[int, ...] | None = None
def _deduplicate_and_build_graph(
leaf_cells: Sequence[Cell],
dim: int,
) -> Tuple[nx.Graph, Dict[int, Array], Set[int]]:
"""
Aus den Leaf-Zellen einen Graphen mit eindeutigen Knoten, Positionen und Randmarkierung bauen.
"""
pos_to_id: Dict[Tuple[float, ...], int] = {}
positions: Dict[int, Array] = {}
next_id = 0
G = nx.Graph()
for cell in leaf_cells:
vid_list: List[int] = []
for p in cell.vertex_positions:
key = tuple(np.round(p.astype(float), 8))
if key not in pos_to_id:
nid = next_id
next_id += 1
pos_to_id[key] = nid
positions[nid] = p.astype(float)
G.add_node(nid)
else:
nid = pos_to_id[key]
vid_list.append(nid)
cell.vertex_ids = tuple(vid_list)
if len(vid_list) == 3:
edges = [
(vid_list[0], vid_list[1]),
(vid_list[1], vid_list[2]),
(vid_list[2], vid_list[0]),
]
elif len(vid_list) == 4:
e = vid_list
edges = [
(e[0], e[1]),
(e[0], e[2]),
(e[0], e[3]),
(e[1], e[2]),
(e[1], e[3]),
(e[2], e[3]),
]
else:
raise ValueError("Cell muss 3- oder 4-Ecken haben.")
for u, v in edges:
if G.has_edge(u, v):
continue
G.add_edge(u, v, weight=1.0)
boundary_nodes: Set[int] = set()
if dim == 2:
# Basis-Dreieck (SG)
A = np.array([0.0, 0.0, 0.0])
B = np.array([1.0, 0.0, 0.0])
C = np.array([0.5, np.sqrt(3) / 2.0, 0.0])
AB = B - A
BC = C - B
CA = A - C
def on_segment(P: Array, P0: Array, P1: Array, vec: Array) -> bool:
cross = np.cross(vec, P - P0)
# In der x-y-Ebene reicht z-Komponente
if abs(cross[2]) > 1e-6:
return False
dot0 = np.dot(P - P0, P1 - P0)
dot1 = np.dot(P - P1, P0 - P1)
return dot0 >= -1e-6 and dot1 >= -1e-6
for nid, p in positions.items():
if (
on_segment(p, A, B, AB)
or on_segment(p, B, C, BC)
or on_segment(p, C, A, CA)
):
boundary_nodes.add(nid)
elif dim == 3:
# Basis-Tetraeder (ST)
A = np.array([0.0, 0.0, 0.0])
B = np.array([1.0, 0.0, 0.0])
C = np.array([0.5, np.sqrt(3) / 2.0, 0.0])
D = np.array([0.5, np.sqrt(3) / 6.0, np.sqrt(6) / 3.0])
faces = [
(A, B, C),
(A, B, D),
(A, C, D),
(B, C, D),
]
normals = []
for P0, P1, P2 in faces:
n = np.cross(P1 - P0, P2 - P0)
normals.append((P0, n))
for nid, p in positions.items():
for P0, n in normals:
dist = float(np.dot(n, p - P0))
if abs(dist) < 1e-6:
boundary_nodes.add(nid)
break
else:
raise ValueError("dim muss 2 oder 3 sein.")
for nid in G.nodes:
G.nodes[nid]["pos"] = positions[nid]
G.nodes[nid]["is_boundary"] = nid in boundary_nodes
return G, positions, boundary_nodes
def generate_sg(level: int):
"""
Erzeuge einen Sierpinski-Gasket-Approximanten (2D, in 3D eingebettet mit z=0).
Rückgabe: (Graph, Positionen, Randknoten, Leaf-Zellen)
"""
if level < 0:
raise ValueError("level muss >= 0 sein.")
A = np.array([0.0, 0.0, 0.0])
B = np.array([1.0, 0.0, 0.0])
C = np.array([0.5, np.sqrt(3) / 2.0, 0.0])
root = Cell(address=(), vertex_positions=[A, B, C], level=0)
leaf_cells: List[Cell] = [root]
all_cells: List[Cell] = [root]
for _ in range(level):
new_leaf: List[Cell] = []
for cell in leaf_cells:
p0, p1, p2 = cell.vertex_positions
addr = cell.address
m01 = 0.5 * (p0 + p1)
m12 = 0.5 * (p1 + p2)
m20 = 0.5 * (p2 + p0)
c0 = Cell(address=addr + (0,), vertex_positions=[p0, m01, m20], level=cell.level + 1)
c1 = Cell(address=addr + (1,), vertex_positions=[m01, p1, m12], level=cell.level + 1)
c2 = Cell(address=addr + (2,), vertex_positions=[m20, m12, p2], level=cell.level + 1)
new_leaf.extend([c0, c1, c2])
all_cells.extend([c0, c1, c2])
leaf_cells = new_leaf
G, positions, boundary_nodes = _deduplicate_and_build_graph(leaf_cells, dim=2)
return G, positions, boundary_nodes, leaf_cells
def generate_st(level: int):
"""
Erzeuge einen Sierpinski-Tetraeder-Approximanten (3D).
Rückgabe: (Graph, Positionen, Randknoten, Leaf-Zellen)
"""
if level < 0:
raise ValueError("level muss >= 0 sein.")
A = np.array([0.0, 0.0, 0.0])
B = np.array([1.0, 0.0, 0.0])
C = np.array([0.5, np.sqrt(3) / 2.0, 0.0])
D = np.array([0.5, np.sqrt(3) / 6.0, np.sqrt(6) / 3.0])
root = Cell(address=(), vertex_positions=[A, B, C, D], level=0)
leaf_cells: List[Cell] = [root]
all_cells: List[Cell] = [root]
for _ in range(level):
new_leaf: List[Cell] = []
for cell in leaf_cells:
p0, p1, p2, p3 = cell.vertex_positions
addr = cell.address
m01 = 0.5 * (p0 + p1)
m02 = 0.5 * (p0 + p2)
m03 = 0.5 * (p0 + p3)
m12 = 0.5 * (p1 + p2)
m13 = 0.5 * (p1 + p3)
m23 = 0.5 * (p2 + p3)
t0 = Cell(address=addr + (0,), vertex_positions=[p0, m01, m02, m03], level=cell.level + 1)
t1 = Cell(address=addr + (1,), vertex_positions=[m01, p1, m12, m13], level=cell.level + 1)
t2 = Cell(address=addr + (2,), vertex_positions=[m02, m12, p2, m23], level=cell.level + 1)
t3 = Cell(address=addr + (3,), vertex_positions=[m03, m13, m23, p3], level=cell.level + 1)
new_leaf.extend([t0, t1, t2, t3])
all_cells.extend([t0, t1, t2, t3])
leaf_cells = new_leaf
G, positions, boundary_nodes = _deduplicate_and_build_graph(leaf_cells, dim=3)
return G, positions, boundary_nodes, leaf_cells
|