Skip to content

Commit

Permalink
Fixed AlignVertices. Added Cell.Wedge. Bumped to v0.8.9
Browse files Browse the repository at this point in the history
Bug fixes and new methods.
  • Loading branch information
wassimj committed Jan 11, 2025
1 parent a53c76c commit 2b6a753
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 253 deletions.
233 changes: 9 additions & 224 deletions notebooks/AdjacentVerticesByCompassDirection.ipynb

Large diffs are not rendered by default.

101 changes: 101 additions & 0 deletions notebooks/AlignVertices.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Import TopologicPy modules. This is not needed on other computers\n",
"import sys\n",
"sys.path.append(\"C:/Users/sarwj/OneDrive - Cardiff University/Documents/GitHub/topologicpy/src\")\n",
"\n",
"from topologicpy.Vertex import Vertex\n",
"from topologicpy.Cell import Cell\n",
"from topologicpy.CellComplex import CellComplex\n",
"from topologicpy.Graph import Graph\n",
"from topologicpy.Topology import Topology\n",
"from topologicpy.Dictionary import Dictionary\n",
"from topologicpy.Vector import Vector\n",
"from topologicpy.Helper import Helper\n",
"print(Helper.Version())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"v1 = Vertex.ByCoordinates(10, 5, 1.5)\n",
"v2 = Vertex.ByCoordinates(5, 15, 1.5)\n",
"v3 = Vertex.ByCoordinates(30, 10, 4.5)\n",
"v4 = Vertex.ByCoordinates(20, 25, 4.5)\n",
"v5 = Vertex.ByCoordinates(11.5, 5, 4.5)\n",
"v6 = Vertex.ByCoordinates(5, 15, 4.5)\n",
"v7 = Vertex.ByCoordinates(15.1, 15, 4.5)\n",
"v8 = Vertex.ByCoordinates(11.8, 5, 8)\n",
"\n",
"c1 = Cell.Prism(origin=v1, width=19.8, length=9.8, height=2.8)\n",
"c2 = Cell.Prism(origin=v2, width=9.8, length=9.8, height=2.8)\n",
"c3 = Cell.Prism(origin=v3, width=19.8, length=19.8, height=8.2)\n",
"extension = Cell.Prism(origin=v7, width=10, length=9.8, height=8.2)\n",
"c3 = Topology.Union(c3, extension)\n",
"c3 = Topology.RemoveCoplanarFaces(c3)\n",
"c4 = Cell.Wedge(origin=v4, width=39.8, length=9.8, height=8.8, flipHorizontal=True)\n",
"c5 = Cell.Prism(origin=v5, width=16, length=9.8, height=2.6)\n",
"c6 = Cell.Prism(origin=v6, width=9.8, length=9.8, height=2.8)\n",
"c7 = Cell.Wedge(origin=v8, width=16, length=9.8, height=4, flipHorizontal=True)\n",
"\n",
"spaces = [c1, c2, c3, c4, c5, c6, c7]\n",
"Topology.Show(spaces)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Grids\n",
"x_list = [0, 3.5, 10, 20, 40]\n",
"y_list = [0, 10, 20, 30]\n",
"z_list = [0, 3, 6, 9]\n",
"new_spaces = []\n",
"for space in spaces:\n",
" vertices = Topology.Vertices(space)\n",
" new_vertices = []\n",
" for v in vertices:\n",
" new_v = Vertex.AlignCoordinates(v, xList=x_list, yList=y_list, zList=z_list, xEpsilon=0.81, yEpsilon=0.51, zEpsilon= 0.51)\n",
" new_vertices.append(new_v)\n",
" new_space = Topology.SelfMerge(Topology.ReplaceVertices(space, verticesA = vertices, verticesB = new_vertices))\n",
" new_spaces.append(new_space)\n",
"\n",
"cellComplex = CellComplex.ByCells(new_spaces)\n",
"g = Graph.ByTopology(cellComplex, direct=False, viaSharedTopologies=True)\n",
"Topology.Show(cellComplex, g)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
122 changes: 122 additions & 0 deletions src/topologicpy/Cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -2352,6 +2352,128 @@ def Volume(cell, mantissa: int = 6) -> float:
volume = None
return volume

@staticmethod
def Wedge(origin=None,
width=1,
length=1,
height=1,
flipHorizontal = False,
flipVertical = False,
direction=[0,0,1],
placement="center",
tolerance=0.0001,
silent=False):
"""
Creates a Wedge.
Parameters
----------
origin : topologic_core.Vertex , optional
The location of the origin of the Wedge. The default is None which results in the Wedge being placed at (0, 0, 0).
width : float , optional
The overall width of the Wedge. The default is 1.0.
length : float , optional
The overall length of the Wedge. The default is 1.0.
height : float , optional
The overall height of the Wedge. The default is 1.0.
direction : list , optional
The vector representing the up direction of the Wedge. The default is [0, 0, 1].
placement : str , optional
The description of the placement of the origin of the Wedge. This can be "center", "lowerleft", "upperleft", "lowerright", "upperright". It is case insensitive. The default is "center".
tolerance : float , optional
The desired tolerance. The default is 0.0001.
silent : bool , optional
If set to True, no error and warning messages are printed. Otherwise, they are. The default is False.
Returns
-------
topologic_core.Cell
The created Wedge.
"""
from topologicpy.Vertex import Vertex
from topologicpy.Face import Face
from topologicpy.Topology import Topology

if not isinstance(width, int) and not isinstance(width, float):
if not silent:
print("Cell.Wedge - Error: The width input parameter is not a valid number. Returning None.")
return None
if not isinstance(length, int) and not isinstance(length, float):
if not silent:
print("Cell.Wedge - Error: The length input parameter is not a valid number. Returning None.")
return None
if not isinstance(height, int) and not isinstance(height, float):
if not silent:
print("Cell.Wedge - Error: The height input parameter is not a valid number. Returning None.")
return None
if width <= tolerance:
if not silent:
print("Cell.Wedge - Error: The width input parameter must be a positive number greater than the tolerance input parameter. Returning None.")
return None
if length <= tolerance:
if not silent:
print("Cell.Wedge - Error: The length input parameter must be a positive number greater than the tolerance input parameter. Returning None.")
return None
if height <= tolerance:
if not silent:
print("Cell.Wedge - Error: The a input parameter must be a positive number greater than the tolerance input parameter. Returning None.")
return None
if origin == None:
origin = Vertex.Origin()
if not Topology.IsInstance(origin, "vertex"):
if not silent:
print("Cell.Wedge - Error: The origin input parameter is not a valid topologic vertex. Returning None.")
return None
if not isinstance(direction, list):
if not silent:
print("Cell.Wedge - Error: The direction input parameter is not a valid list. Returning None.")
return None
if not len(direction) == 3:
if not silent:
print("Cell.Wedge - Error: The direction input parameter is not a valid vector. Returning None.")
return None

# Define the vertices of the T-shape (counterclockwise)
v1 = Vertex.ByCoordinates(0,0,0)
v2 = Vertex.ByCoordinates(width, 0, 0)
v3 = Vertex.ByCoordinates(width, length, 0)
v4 = Vertex.ByCoordinates(0, length, 0)
v5 = Vertex.ByCoordinates(0, length, height)
v6 = Vertex.ByCoordinates(0, 0, height)

f1 = Face.ByVertices([v1, v2, v3, v4], tolerance=tolerance)
f2 = Face.ByVertices([v1, v2, v6], tolerance=tolerance)
f3 = Face.ByVertices([v4, v5, v3], tolerance=tolerance)
f4 = Face.ByVertices([v1, v6, v5, v4], tolerance=tolerance)
f5 = Face.ByVertices([v2, v3, v5, v6], tolerance=tolerance)
cell = Cell.ByFaces([f1, f2, f3, f4, f5])
cell = Topology.Translate(cell, -width/2, -length/2, -height/2)
cell = Topology.Translate(cell, Vertex.X(origin), Vertex.Y(origin), Vertex.Z(origin))
if flipHorizontal == True:
xScale = -1
else:
xScale = 1
if flipVertical == True:
zScale = -1
else:
zScale = 1
if xScale == -1 or zScale == -1:
cell = Topology.Scale(cell, origin=origin, x=xScale, y=1, z=zScale)
if placement.lower() == "lowerleft":
cell = Topology.Translate(cell, origin=origin, x=width/2, y=length/2, z=height/2)
elif placement.lower() == "upperright":
cell = Topology.Translate(cell, origin=origin, x=-width/2, y=-length/2, z=-height/2)
elif placement.lower() == "upperleft":
cell = Topology.Translate(cell, origin=origin, x=width/2, y=-length/2, z=-height/2)
elif placement.lower() == "lowerright":
cell = Topology.Translate(cell, origin=origin, x=-width/2, y=length/2, z=height/2)

if direction != [0, 0, 1]:
cell = Topology.Orient(cell, origin=origin, dirA=[0, 0, 1], dirB=direction)

return cell

@staticmethod
def Wires(cell) -> list:
"""
Expand Down
25 changes: 19 additions & 6 deletions src/topologicpy/Vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

class Vertex():
@staticmethod
def AlignCoordinates(vertex, xList=None, yList=None, zList=None, transferDictionary=False, mantissa=6, silent=False):
def AlignCoordinates(vertex, xList: list = None, yList: list = None, zList: list = None, xEpsilon: float = 0.0001, yEpsilon: float = 0.0001, zEpsilon: float = 0.0001, transferDictionary: bool = False, mantissa: int = 6, silent: bool = False):
"""
Aligns the coordinates of the input vertex with the list of x,y, and z coordinates.
Expand All @@ -49,6 +49,12 @@ def AlignCoordinates(vertex, xList=None, yList=None, zList=None, transferDiction
The input numerical list of y-coordinates. The default is None.
zList : list , optional
The input numerical list of z-coordinates. The default is None.
xEpsilon : float , optional
The desired tolerance for the x coordinates. The default is 0.0001.
yEpsilon : float , optional
The desired tolerance for the y coordinates. The default is 0.0001.
zEpsilon : float , optional
The desired tolerance for the z coordinates. The default is 0.0001.
transferDictionary : bool , optional
if set to True, the dictionary of the input vertex is transferred to the new vertex.
mantissa : int , optional
Expand All @@ -70,17 +76,24 @@ def AlignCoordinates(vertex, xList=None, yList=None, zList=None, transferDiction
print("Vertex.AlignCoordinates - Error: The input vertex parameter is not a topologic vertex. Returning None.")
return None

closest_x, closest_y, closest_z = Vertex.Coordinates(vertex, mantissa=mantissa)
x, y, z = Vertex.Coordinates(vertex, mantissa=mantissa)
if isinstance(xList, list):
if len(xList) > 0:
closest_x = xList[Helper.ClosestMatch(closest_x, xList)]
closest_x = round(xList[Helper.ClosestMatch(x, xList)], mantissa)
if isinstance(yList, list):
if len(yList) > 0:
closest_y = yList[Helper.ClosestMatch(closest_y, yList)]
closest_y = round(yList[Helper.ClosestMatch(y, yList)], mantissa)
if isinstance(zList, list):
if len(zList) > 0:
closest_z = zList[Helper.ClosestMatch(closest_z, zList)]
return_vertex = Vertex.ByCoordinates(closest_x, closest_y, closest_z)
closest_z = round(zList[Helper.ClosestMatch(z, zList)], mantissa)

if abs(x - closest_x) < xEpsilon:
x = closest_x
if abs(y - closest_y) < yEpsilon:
y = closest_y
if abs(z - closest_z) < zEpsilon:
z = closest_z
return_vertex = Vertex.ByCoordinates(x, y, z)
if transferDictionary == True:
return_vertex = Topology.SetDictionary(return_vertex, Topology.Dictionary(vertex), silent=silent)
return return_vertex
Expand Down
Loading

0 comments on commit 2b6a753

Please sign in to comment.