Skip to content

Commit 1381e13

Browse files
committed
init with empty, and use for loop
1 parent 1a04790 commit 1381e13

File tree

1 file changed

+15
-51
lines changed

1 file changed

+15
-51
lines changed

gustaf/utils/connec.py

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ def hexa_to_quad(volumes):
112112
raise ValueError("Given volumes are not `hexa` volumes")
113113

114114
fpe = 6 # faces per element
115-
faces = (
116-
np.ones(((volumes.shape[0] * fpe), 4), dtype=settings.INT_DTYPE) * -1
117-
) # -1 for safety check
115+
faces = np.empty(((volumes.shape[0] * fpe), 4), dtype=settings.INT_DTYPE)
118116

119117
faces[::fpe] = volumes[:, [1, 0, 3, 2]]
120118
faces[1::fpe] = volumes[:, [0, 1, 5, 4]]
@@ -123,9 +121,6 @@ def hexa_to_quad(volumes):
123121
faces[4::fpe] = volumes[:, [3, 0, 4, 7]]
124122
faces[5::fpe] = volumes[:, [4, 5, 6, 7]]
125123

126-
if (faces == -1).any():
127-
raise ValueError("Something went wrong while computing faces")
128-
129124
return faces
130125

131126

@@ -188,9 +183,8 @@ def faces_to_edges(faces):
188183
vertices_per_face = faces.shape[1]
189184

190185
num_edges = int(num_faces * vertices_per_face)
191-
edges = (
192-
np.ones((num_edges, 2), dtype=settings.INT_DTYPE) * -1
193-
) # -1 for safety
186+
edges = np.empty((num_edges, 2), dtype=settings.INT_DTYPE)
187+
194188
edges[:, 0] = faces.ravel()
195189

196190
for i in range(vertices_per_face):
@@ -199,10 +193,6 @@ def faces_to_edges(faces):
199193

200194
edges[i::vertices_per_face, 1] = faces[:, v_ind]
201195

202-
# Quick sanity check - No entries are left untouched.
203-
if (edges == -1).any():
204-
raise ValueError("There was an error while computing edges.")
205-
206196
return edges
207197

208198

@@ -296,16 +286,13 @@ def make_quad_faces(resolutions):
296286
except ValueError as e:
297287
raise ValueError(f"Problem with generating node indices. {e}")
298288

299-
faces = np.ones((total_faces, 4), dtype=settings.INT_DTYPE) * -1
289+
faces = np.empty((total_faces, 4), dtype=settings.INT_DTYPE)
300290

301291
faces[:, 0] = node_indices[: (nnpd[1] - 1), : (nnpd[0] - 1)].ravel()
302292
faces[:, 1] = node_indices[: (nnpd[1] - 1), 1 : nnpd[0]].ravel()
303293
faces[:, 2] = node_indices[1 : nnpd[1], 1 : nnpd[0]].ravel()
304294
faces[:, 3] = node_indices[1 : nnpd[1], : (nnpd[0] - 1)].ravel()
305295

306-
if faces.all() == -1:
307-
raise ValueError("Something went wrong during `make_quad_faces`.")
308-
309296
return faces
310297

311298

@@ -343,7 +330,7 @@ def make_hexa_volumes(resolutions):
343330
nnpd[::-1]
344331
)
345332

346-
volumes = np.ones((total_volumes, 8), dtype=settings.INT_DTYPE) * int(-1)
333+
volumes = np.empty((total_volumes, 8), dtype=settings.INT_DTYPE)
347334

348335
volumes[:, 0] = node_indices[
349336
: (nnpd[2] - 1), : (nnpd[1] - 1), : (nnpd[0] - 1)
@@ -368,9 +355,6 @@ def make_hexa_volumes(resolutions):
368355
1 : nnpd[2], 1 : nnpd[1], : (nnpd[0] - 1)
369356
].ravel()
370357

371-
if (volumes == -1).any():
372-
raise ValueError("Something went wrong during `make_hexa_volumes`.")
373-
374358
return volumes
375359

376360

@@ -407,10 +391,7 @@ def subdivide_edges(edges):
407391
raise NotImplementedError
408392

409393

410-
def subdivide_tri(
411-
mesh,
412-
return_dict=False,
413-
):
394+
def subdivide_tri(mesh, return_dict=False):
414395
"""Subdivide triangles. Each triangle is divided into 4 meshes.
415396
416397
``Subdivided Faces``
@@ -581,7 +562,7 @@ def sorted_unique(connectivity, sorted_=False):
581562
)
582563

583564

584-
def edges_to_polygons(outline_edges, return_edges=False, max_polygons=100):
565+
def edges_to_polygons(outline_edges, return_edges=False):
585566
"""
586567
Organize outline edges, so that it describes a polygon.
587568
Edges are expected to be extracted using `gus.Mesh.edges`, from
@@ -593,10 +574,6 @@ def edges_to_polygons(outline_edges, return_edges=False, max_polygons=100):
593574
outline_edges: (n, 2) list-like
594575
return_edges: bool
595576
(Optional) Default is False. If set True, returns polygon as edges.
596-
max_polygons: int
597-
(Optional) Default is 100. Maximum expected polygons.
598-
Used as an exit criterium during polygon search.
599-
600577
601578
Returns
602579
--------
@@ -608,19 +585,19 @@ def edges_to_polygons(outline_edges, return_edges=False, max_polygons=100):
608585
>>> m = gus.load_mesh("path_to_mesh_2d.stl")
609586
>>> polygon_edges = edges_to_polygon(m.edges()[m.single_edges()])
610587
"""
588+
# we want to have an np array
589+
outline_edges = np.asanyarray(outline_edges)
590+
611591
# Build a lookup_array
612592
lookup_array = np.empty(outline_edges.max() + 1, dtype=settings.INT_DTYPE)
613593
lookup_array[outline_edges[:, 0]] = outline_edges[:, 1]
614-
# create some status variables
615-
watch_dog_max = abs(int(max_polygons))
616-
watch_dog = 0
617594

618595
# select starting point - lowest index
619596
starting_point = int(outline_edges.min())
620597
# initialize a set to keep track of processes vertices
621598
next_candidates = set(outline_edges[:, 0])
622599
polygons = []
623-
while True:
600+
for _ in range(len(outline_edges)):
624601
# Get polygon - start with first two points
625602
polygon = [starting_point]
626603
polygon.append(lookup_array[polygon[-1]])
@@ -638,25 +615,12 @@ def edges_to_polygons(outline_edges, return_edges=False, max_polygons=100):
638615

639616
# check if we counted all the edges
640617
# if so, exit
641-
lens = [len(p) for p in polygons]
642-
if sum(lens) == len(outline_edges):
618+
next_candidates.difference_update(polygons[-1])
619+
if len(next_candidates) == 0:
643620
break
644621

645-
else:
646-
# let's try to find the next starting point
647-
next_candidates.difference_update(polygons[-1])
648-
# Assign new
649-
starting_point = min(next_candidates)
650-
651-
watch_dog += 1
652-
# Hope there're no more than number of max polygons.
653-
if watch_dog >= watch_dog_max:
654-
log.warning(
655-
f"Stopping after given max number of polygons {watch_dog_max}."
656-
" If you want to continue searching, please set "
657-
"`max_polygons` to a bigger number"
658-
)
659-
break
622+
# let's try to find the next starting point
623+
starting_point = min(next_candidates)
660624

661625
if not return_edges:
662626
return polygons

0 commit comments

Comments
 (0)