Skip to content

Commit

Permalink
refactor boolregion and more pre alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
psauvan committed Jan 5, 2025
1 parent b568e1f commit dd40669
Show file tree
Hide file tree
Showing 27 changed files with 2,518 additions and 1,220 deletions.
32 changes: 17 additions & 15 deletions src/geouned/GEOUNED/conversion/cell_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging

from ..utils import geometry_gu as GU
from ..utils.functions import get_multiplanes
from ..utils.functions import get_multiplanes, get_reverseCan
from ..utils.boolean_function import BoolSequence, BoolRegion
from .cell_definition_functions import (
gen_plane,
Expand Down Expand Up @@ -38,16 +38,22 @@ def simple_solid_definition(solid, Surfaces):
solid_gu = GU.SolidGu(solid.Solids[0], tolerances=Surfaces.tolerances)

# multiplanes,pindex = get_multiplanes(solid_gu,solid.BoundBox) #pindex are all faces index used to produced multiplanes, do not count as standard planes
multiplanes, pindex = get_multiplanes(
solid_gu, None
) # pindex are all faces index used to produced multiplanes, do not count as standard planes
# pindex are all faces index used to produced multiplanes, do not count as standard planes
multiplanes, pindex = get_multiplanes(solid_gu)

for mp in multiplanes:
mp_region = Surfaces.add_multiPlane(mp)
component_definition.append(mp_region)

for iface, face in enumerate(solid_gu.Faces):
revereCan, cindex = get_reverseCan(solid_gu)
for cs in revereCan:
cs_region = Surfaces.add_reverseCan(cs)
component_definition.append(cs_region)

omitFaces = pindex.union(cindex)
for iface, face in enumerate(solid_gu.Faces):
if iface in omitFaces:
continue
if abs(face.Area) < Surfaces.tolerances.min_area:
logger.warning(
f"{str(face.Surface)} surface removed from cell definition. Face area < Min area ({face.Area} < {Surfaces.tolerances.min_area})"
Expand All @@ -63,8 +69,6 @@ def simple_solid_definition(solid, Surfaces):
orient = face.Orientation

if isinstance(face.Surface, GU.PlaneGu):
if iface in pindex:
continue # the face in includes in multiplane
plane = gen_plane(face, orient)
plane_region = Surfaces.add_plane(plane, True)
component_definition.append(plane_region)
Expand All @@ -74,11 +78,10 @@ def simple_solid_definition(solid, Surfaces):
cylinder_region = Surfaces.add_cylinder(cylinder, orient)

if orient == "Reversed":
plane = gen_plane_cylinder(
face, solid, Surfaces.tolerances
) # plane must be correctly oriented toward materials
plane = gen_plane_cylinder(face, solid_gu.Faces, Surfaces.Tolerances) # plane must be correctly oriented toward materials
if plane is not None:
cylinder_region = BoolRegion.mult(cylinder_region, auxillary_plane(plane, Surfaces), label=cylinder_region)

component_definition.append(cylinder_region)

elif isinstance(face.Surface, GU.ConeGu):
Expand All @@ -93,19 +96,18 @@ def simple_solid_definition(solid, Surfaces):
cone_region = BoolRegion.add(cone_region, auxillary_plane(apex_plane, Surfaces), label=cone_region)

if orient == "Reversed":
plane = gen_plane_cone(face, solid, Surfaces.tolerances) # plane must be correctly oriented toward materials
plane = gen_plane_cone(face, solid_gu.Faces, Surfaces.tolerances) # plane must be correctly oriented toward materials
if plane is not None:
cone_region = BoolRegion.mult(cone_region, auxillary_plane(plane, Surfaces), label=cone_region)
component_definition.append(cone_region)

elif isinstance(face.Surface, GU.SphereGu):
sphere = gen_sphere(face)
sphere_region = Surfaces.add_sphere(sphere, orient)

if orient == "Reversed":
plane_region = gen_plane_sphere(face, solid, Surfaces)
if plane_region is not None:
sphere_region = BoolRegion.mult(sphere_region, plane_region, label=sphere_region)
plane = gen_plane_sphere(face, solid_gu.Faces)
if plane is not None:
sphere_region = BoolRegion.mult(sphere_region, auxillary_plane(plane, Surfaces), label=sphere_region)
component_definition.append(sphere_region)

elif isinstance(face.Surface, GU.TorusGu):
Expand Down
70 changes: 34 additions & 36 deletions src/geouned/GEOUNED/conversion/cell_definition_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
is_same_value,
)
from ..utils.boolean_function import BoolRegion
from ..utils.functions import GeounedSurface
from ..utils.geouned_classes import GeounedSurface
from ..utils.geometry_gu import FaceGu,CylinderGu

logger = logging.getLogger("general_logger")

Expand Down Expand Up @@ -218,11 +219,11 @@ def U_torus_planes(face, u_params, Surfaces):
return auxillary_plane(plane1, Surfaces) + auxillary_plane(plane2, Surfaces)


def gen_plane_sphere(face, solid, Surfaces):
def gen_plane_sphere(face, solidFaces):
same_faces = []
same_faces.append(face)

for f in solid.Faces:
for f in solidFaces:
if f.isEqual(face) or str(f.Surface) != "Sphere":
continue
if f.Surface.Center == face.Surface.Center and f.Surface.Radius == face.Surface.Radius:
Expand All @@ -247,26 +248,25 @@ def gen_plane_sphere(face, solid, Surfaces):
if dmin > 1e-6:
center = face.Surface.Center + 0.95 * dmin * normal
plane = GeounedSurface(("Plane", (center, normal, 1, 1)))
return auxillary_plane(plane, Surfaces)
else:
return None


def gen_plane_cylinder(face, solid, Surfaces):
def gen_plane_cylinder(face, solidFaces, tolerances):

surf = face.Surface
rad = surf.Radius

if str(surf) != "<Cylinder object>":
return None

my_index = solid.Faces.index(face)
my_index =face.Index
face_index = [my_index]

for i, face2 in enumerate(solid.Faces):
if face2.Area < Surfaces.tolerances.min_area:
for face2 in solidFaces:
if face2.Area < tolerances.min_area:
logger.warning(
f"surface {str(surf)} removed from cell definition. Face area < Min area ({face2.Area} < {Surfaces.tolerances.min_area})"
f"surface {str(surf)} removed from cell definition. Face area < Min area ({face2.Area} < {tolerances.min_area})"
)
continue
if str(face2.Surface) == "<Cylinder object>" and not (face2.isEqual(face)):
Expand All @@ -276,20 +276,20 @@ def gen_plane_cylinder(face, solid, Surfaces):
and is_in_line(face2.Surface.Center, face.Surface.Axis, face.Surface.Center)
):
# print 'Warning: coincident cylinder faces are the same'
face_index.append(i)
face_index.append(face2.Index)

u_min, u_max = get_u_value_boundary(solid, face_index, my_index)
u_min, u_max = get_u_value_boundary(solidFaces, face_index, my_index)
if u_min is None:
return None

u_1, i1 = u_min
u_2, i2 = u_max

v_1 = solid.Faces[i1].ParameterRange[2]
v_2 = solid.Faces[i2].ParameterRange[2]
v_1 = solidFaces[i1].ParameterRange[2]
v_2 = solidFaces[i2].ParameterRange[2]

p1 = solid.Faces[i1].valueAt(u_1, v_1)
p2 = solid.Faces[i2].valueAt(u_2, v_2)
p1 = solidFaces[i1].valueAt(u_1, v_1)
p2 = solidFaces[i2].valueAt(u_2, v_2)

if p1.isEqual(p2, 1e-5):
logger.error("Error in the additional place definition")
Expand All @@ -300,23 +300,22 @@ def gen_plane_cylinder(face, solid, Surfaces):
if normal.dot(face.CenterOfMass - p1) < 0:
normal = -normal

plane = GeounedSurface(("Plane", (p1, normal, 1, 1)))
return auxillary_plane(plane, Surfaces)
return GeounedSurface(("Plane", (p1, normal, 1, 1)))


def gen_plane_cone(face, solid, Surfaces):
def gen_plane_cone(face, solidFaces, tolerances):

Surf = face.Surface
if str(Surf) != "<Cone object>":
return None

myIndex = solid.Faces.index(face)
myIndex = solidFaces.index(face)
face_index = [myIndex]

for i, face2 in enumerate(solid.Faces):
if face2.Area < Surfaces.tolerances.min_area:
for face2 in solidFaces:
if face2.Area < tolerances.min_area:
logger.warning(
f"{str(Surf)} surface removed from cell definition. Face area < Min area ({face2.Area} < {Surfaces.tolerances.min_area})"
f"{str(Surf)} surface removed from cell definition. Face area < Min area ({face2.Area} < {tolerances.min_area})"
)
continue
if str(face2.Surface) == "<Cone object>" and not (face2.isEqual(face)):
Expand All @@ -325,20 +324,20 @@ def gen_plane_cone(face, solid, Surfaces):
and face2.Surface.Apex.isEqual(face.Surface.Apex, 1e-5)
and (face2.Surface.SemiAngle - face.Surface.SemiAngle) < 1e-6
):
face_index.append(i)
face_index.append(face2.Index)

u_min, u_max = get_u_value_boundary(solid, face_index, myIndex)
u_min, u_max = get_u_value_boundary(solidFaces, face_index, myIndex)
if u_min is None:
return None

u_1, i1 = u_min
u_2, i2 = u_max

v_1 = solid.Faces[i1].ParameterRange[2]
v_2 = solid.Faces[i2].ParameterRange[2]
v_1 = solidFaces[i1].ParameterRange[2]
v_2 = solidFaces[i2].ParameterRange[2]

p1 = solid.Faces[i1].valueAt(u_1, v_1)
p2 = solid.Faces[i2].valueAt(u_2, v_2)
p1 = solidFaces[i1].valueAt(u_1, v_1)
p2 = solidFaces[i2].valueAt(u_2, v_2)

if p1.isEqual(p2, 1e-5):
logger.error("in the additional place definition")
Expand All @@ -351,13 +350,12 @@ def gen_plane_cone(face, solid, Surfaces):
if normal.dot(face.CenterOfMass - face.Surface.Apex) < 0:
normal = -normal

plane = GeounedSurface(("Plane", (face.Surface.Apex, normal, 1, 1)))
return auxillary_plane(plane, Surfaces)

return GeounedSurface(("Plane", (face.Surface.Apex, normal, 1, 1)))


def get_u_value_boundary(solid, face_index, my_index):
def get_u_value_boundary(solidFaces, face_index, my_index):

face_u_ranges, closed_face = get_closed_ranges(solid, face_index)
face_u_ranges, closed_face = get_closed_ranges(solidFaces, face_index)
if closed_face:
return None, None

Expand All @@ -367,11 +365,11 @@ def get_u_value_boundary(solid, face_index, my_index):
return u_min, u_max


def get_closed_ranges(solid, face_index):
def get_closed_ranges(solidFaces, face_index):

u_nodes = []
for index in face_index:
URange = solid.Faces[index].ParameterRange
URange = solidFaces[index].ParameterRange
u_nodes.append((URange[0], index))
u_nodes.append((URange[1], index))
u_nodes.sort()
Expand Down
28 changes: 15 additions & 13 deletions src/geouned/GEOUNED/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
from .code_version import *
from .conversion import cell_definition as Conv
from .cuboid.translate import translate
from .decompose import decom_one as Decom
#from .decompose.decom_one import main_split
from .decompose.decom_one_generators import main_split
from .loadfile import load_step as Load
from .utils import functions as UF
from .utils.geouned_classes import GeounedSolid, SurfacesDict, MetaSurfacesDict
from .utils.functions import get_box
from .utils.boolean_solids import build_c_table_from_solids
from .utils.data_classes import NumericFormat, Options, Settings, Tolerances
from .void import void as void
Expand Down Expand Up @@ -391,7 +393,7 @@ def start(self):
# sets self.geometry_bounding_box with default padding
self._get_geometry_bounding_box()

self.Surfaces = UF.SurfacesDict(
self.Surfaces = SurfacesDict(
offset=self.settings.startSurf - 1,
options=self.options,
tolerances=self.tolerances,
Expand All @@ -415,7 +417,7 @@ def start(self):

# start Building CGS cells phase

self.Surfaces = UF.MetaSurfacesDict(
self.Surfaces = MetaSurfacesDict(
options=self.options, tolerances=self.tolerances, numeric_format=self.numeric_format
)
for j, m in enumerate(tqdm(self.meta_list, desc="Translating solid cells")):
Expand Down Expand Up @@ -497,7 +499,7 @@ def start(self):
if c.Definition.level == 0 or c.IsEnclosure:
continue
logger.info(f"simplify cell {c.__id__}")
Box = UF.get_box(c, self.options.enlargeBox)
Box = get_box(c, self.options.enlargeBox)
CT = build_c_table_from_solids(Box, (c.Surfaces, Surfs), "full", options=self.options)
c.Definition.simplify(CT)
c.Definition.clean()
Expand Down Expand Up @@ -537,7 +539,7 @@ def start(self):
##########################################################
VOID CELLS
##########################################################"""
mc = UF.GeounedSolid(None)
mc = GeounedSolid(None)
mc.Comments = lineComment
self.meta_list.append(mc)

Expand Down Expand Up @@ -586,14 +588,14 @@ def _decompose_solids(self, meta: bool):
else:
m.Solids[0].exportStep(str(debug_output_folder / f"origSolid_{i}.stp"))

comsolid, err = Decom.main_split(
comsolid = main_split(
Part.makeCompound(m.Solids),
self.options,
self.tolerances,
self.numeric_format,
# self.numeric_format, #not used with generators
)

if err != 0:
if False: #todo decomposition error information
sus_output_folder = Path("suspicious_solids")
sus_output_folder.mkdir(parents=True, exist_ok=True)
if m.IsEnclosure:
Expand Down Expand Up @@ -655,7 +657,7 @@ def print_warning_solids(warnSolids, warnEnclosures):
solids_logger.info(lines)


def join_meta_lists(MList) -> typing.List[UF.GeounedSolid]:
def join_meta_lists(MList) -> typing.List[GeounedSolid]:

newMetaList = MList[0]
if MList[0]:
Expand Down Expand Up @@ -703,7 +705,7 @@ def sort_enclosure(MetaList, meta_void, offSet=0):
lineComment = f"""##########################################################
ENCLOSURE {m.EnclosureID}
##########################################################"""
mc = UF.GeounedSolid(None)
mc = GeounedSolid(None)
mc.Comments = lineComment
newMeta.append(mc)
for e in newList[m.EnclosureID]:
Expand All @@ -716,7 +718,7 @@ def sort_enclosure(MetaList, meta_void, offSet=0):
lineComment = f"""##########################################################
END ENCLOSURE {m.EnclosureID}
##########################################################"""
mc = UF.GeounedSolid(None)
mc = GeounedSolid(None)
mc.Comments = lineComment
newMeta.append(mc)

Expand All @@ -730,7 +732,7 @@ def sort_enclosure(MetaList, meta_void, offSet=0):
##########################################################
VOID CELLS
##########################################################"""
mc = UF.GeounedSolid(None)
mc = GeounedSolid(None)
mc.Comments = lineComment
newMeta.append(mc)

Expand Down
Loading

0 comments on commit dd40669

Please sign in to comment.