-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added triangulation of bow and stern if needed (instead of added a ve…
…rtex at the center) Added some convenience methods for working on frames (x,y, scaled)
- Loading branch information
1 parent
d365a2b
commit ad10ec8
Showing
6 changed files
with
264 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from vtkmodules.vtkCommonCore import vtkIdList, vtkPoints | ||
from vtkmodules.vtkCommonDataModel import vtkCellArray | ||
from vtkmodules.vtkFiltersGeneral import vtkContourTriangulator | ||
|
||
|
||
def triangulate_poly(vertices): | ||
"""Triangulates a polygon defined by vertices. Polygons can be concave | ||
Returns | ||
""" | ||
out_faces = [] | ||
|
||
out_points = list(vertices) | ||
|
||
# make a vtk polydata object from these points | ||
vtkPts = vtkPoints() | ||
for p in out_points: | ||
vtkPts.InsertNextPoint(*p) | ||
|
||
# add the polygon to the polydata | ||
cellArray = vtkCellArray() | ||
|
||
# use vtkContourTriangulator to triangulate the polygon | ||
idList = vtkIdList() | ||
for i in range(len(vertices)): | ||
idList.InsertNextId(i) | ||
|
||
result = vtkContourTriangulator.TriangulatePolygon(idList, vtkPts,cellArray) | ||
|
||
if result != 1: | ||
raise ValueError("Triangulation failed") | ||
|
||
|
||
# extract the vertices from cellArray | ||
cellArray.InitTraversal() | ||
|
||
while True: | ||
idList = vtkIdList() | ||
if cellArray.GetNextCell(idList): | ||
corners = [] | ||
|
||
for i in range(idList.GetNumberOfIds()): | ||
# corners.append(vtkPts.GetPoint(idList.GetId(i))) | ||
corners.append(idList.GetId(i)) | ||
|
||
out_faces.append(corners) | ||
else: | ||
break | ||
|
||
return out_points, out_faces | ||
|
||
|
||
if __name__ == '__main__': | ||
verts = [(0, 0, 0), (0, 0.1, 0), (0, 0.1, 0), (0, 1, 0), (0, 1, 0), (0, -1, 0), (0, -1, 0), (0, -0.1, 0), (0, -0.1, 0), | ||
(0, 0, 0)] | ||
|
||
verts, faces = triangulate_poly(verts) | ||
|
||
import matplotlib.pyplot as plt | ||
|
||
for tri in faces: | ||
# get vertices | ||
t = [verts[i] for i in tri] | ||
t.append(t[0]) | ||
|
||
plt.plot([p[0] for p in t], [p[1] for p in t], 'r-') | ||
|
||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters