Use existing points when creating points - feature suggestion
Hello,
When using the Gmsh API to create lots of points and want to ensure connectivity it can be useful to use existing points instead of creating new ones at the same location. For my work, I created a function to return the point tag if a point exists in the area around the desired location (tolerance can be specified), otherwise a new point is created at the location. The Python code from my helper module is shown below (not sure if it's the best, but does the job). My suggestion would be that this behaviour could be implemented into the API functions gmsh/model/occ/addPoint and gmsh/model/geo/addPoint with the use of aditional arguments: boolean argument to switch it on (e.g. useExisting) and a float argument (e.g. tolerance), which could also use the global "Geometry.Tolerance" value by default.
Thank you for creating and curating GMSH. Best, Žiga
def addPoint(coords: np.ndarray(3), search_radius=1e-4, tag=-1) -> int:
"""
Creates a new point in GMSH OpenCascade representation or returns an
index of a previously created point in the same location.
Input
coords: list/array of [x, y, z] coordinates of a point
search_radius: search radius for the point from given coordinates (float)
(note: search for the point is in a square, not circle)
tag: specify the point tag (default=-1; automatically assign tag)
Output
pt_tag: integer tag of the created/existing point
"""
pt = np.reshape(coords, 3)
# Use an existing point or create a new one if it does not exist
# Define bounding box
bbox = {"xmin": pt[0] - search_radius,
"ymin": pt[1] - search_radius,
"zmin": pt[2] - search_radius,
"xmax": pt[0] + search_radius,
"ymax": pt[1] + search_radius,
"zmax": pt[2] + search_radius,
"dim": 0}
# search for points inside the bounding box
# res: [(dimension, tag)]
res = gmsh.model.occ.getEntitiesInBoundingBox(**bbox)
# if the point exists
if res:
# add point to the master list
pt_tag = res[-1][1]
# if the point does not exist
else:
# create a point
pt_tag = gmsh.model.occ.addPoint(pt[0], pt[1], pt[2], tag=tag)
return pt_tag