Ejemplo n.º 1
0
 def shapeFromVTKPolyData(self, pdata, min_cell_area=1.e-8):
     """
     Create a shape from a VTK PolyData object
     @param pdata vtkPolyData instance
     @param min_cell_area tolerance for cell areas
     @return shape
     @note field data will get lost
     """
     # Store the cell connectivity as CSG polygons.
     numCells = pdata.GetNumberOfPolys()
     cells = pdata.GetPolys()
     cells.InitTraversal()
     ptIds = vtk.vtkIdList()
     polygons = []
     for i in range(numCells):
         cells.GetNextCell(ptIds)
         npts = ptIds.GetNumberOfIds()
         verts = []
         for j in range(npts):
             pointIndex = ptIds.GetId(j)
             pt = pdata.GetPoint(pointIndex)
             v = Vertex(Vector(pt[0], pt[1], pt[2]))
             verts.append(v)
         self.cleanPolygon(verts, min_cell_area)
         if len(verts) >= 3:
             polygons.append(Polygon(verts))
     # Instantiate the shape.
     return CSG.fromPolygons(polygons)
Ejemplo n.º 2
0
    def to_csg(self):
        points = self.points
        polygons = [
            CSGPolygon([CSGVertex(pos=points[:, i]) for i in face])
            for face in self.faces_idxs
        ]

        return CSG.fromPolygons(polygons)
Ejemplo n.º 3
0
def sequence_union(fov_polyhedrons):

    n = len(fov_polyhedrons)
    if n == 0: return CSG.fromPolygons([])
    if n == 1: return fov_polyhedrons[0]

    unified_fov_polyhedron = fov_polyhedrons[0]
    for i in range(1, n):
        unified_fov_polyhedron = unified_fov_polyhedron.union(fov_polyhedrons[i])
    return unified_fov_polyhedron
Ejemplo n.º 4
0
def form_mesh(vertices, faces):
    polygons = []
    for face in faces:
        newvertices = []
        for ind in face:
            newvertices.append(
                Vertex(
                    Vector(vertices[ind][0], vertices[ind][1],
                           vertices[ind][2])))
        polygons.append(Polygon(newvertices))
    return CSG.fromPolygons(polygons)
Ejemplo n.º 5
0
def get_fov_polyhedron_from_pts(pts):

    pts = [Vertex(p) for p in pts]

    face_index = [[0, 1, 2], [3, 0, 2], [4, 3, 2], [1, 4, 2], [1, 0, 4], [4, 0, 3]] # order is changed by pycsg
    faces = []
    for face in face_index:
        faces.append(Polygon([pts[i].clone() for i in face]))
    polyhedron = CSG.fromPolygons(faces)

    return polyhedron
Ejemplo n.º 6
0
def cascaded_union(fov_polyhedrons):

    # using divide and conquer to get union

    n = len(fov_polyhedrons)
    if n == 0: return CSG.fromPolygons([])
    if n == 1: return fov_polyhedrons[0]
    left = cascaded_union(fov_polyhedrons[:n // 2])
    right = cascaded_union(fov_polyhedrons[n // 2:])
    unified_fov_polyhedron = left.union(right)

    return unified_fov_polyhedron
Ejemplo n.º 7
0
 def getBoundarySurfaceInsideShape(self, shape, other):
     """
     Return the portion of the surface that is inside another shape
     @param shape
     @param other other shape
     @return shape
     """
     a = BSPNode(shape.clone().polygons)
     b = BSPNode(other.clone().polygons)
     b.invert()
     a.clipTo(b)
     return CSG.fromPolygons(a.allPolygons())
Ejemplo n.º 8
0
from __future__ import print_function
from icqsol.shapes.icqShapeManager import ShapeManager
from csg.core import CSG
from icqsol import util

"""
Test conversion from a shape to a list of polygons
@author [email protected]
"""
shape_mgr = ShapeManager(file_format=util.VTK_FORMAT, vtk_dataset_type=util.POLYDATA)
shp = shape_mgr.createShape('box', origin=[0., 0., 0.], lengths=[1., 1., 1.],)

# check whether one can convert to a list of polygons
polys = shape_mgr.shapeToPolygons(shp)

# check whether each polygon can be cloned
map(lambda p: p.clone(), polys)

# check that we can load the polygons
a = CSG.fromPolygons(polys)

shp2 = shape_mgr.createShape('sphere', radius=1.0, origin=(0., 0., 0.), n_theta=5, n_phi=2)
polys2 = shape_mgr.shapeToPolygons(shp2)
a2 = CSG.fromPolygons(polys2)
Ejemplo n.º 9
0
def get_fov_polyhedron(pos, theta, alpha=45, R=0.1):

    # get polyhedron from pos and theta

    pts = [Vertex([0, 0, 0])]
    dx = [-1, 1]
    dz = [-1, 1]

    for i in range(2):
        for j in range(2):
            pts.append(Vertex([
                dz[j] * R * np.sin(alpha / 2 * np.pi / 180),

                dx[i] * R * np.sin(alpha / 2 * np.pi / 180),

                               R * np.cos(alpha / 2 * np.pi / 180),
                               ]))

    face_index = [[2, 1, 0],
             [4, 2,  0],
             [3, 4, 0],
             [1, 3, 0],
             [1, 2, 3],
             [3, 2, 4]]
    faces = []
    for face in face_index:
        faces.append(Polygon([pts[i].clone() for i in face]))
    polyhedron = CSG.fromPolygons(faces)

    # finish creating a polyhedron before rotation and translation

    r = Rotation.from_euler('ZYX', theta, degrees=False)
    rotvec = r.as_rotvec()
    angle = np.linalg.norm(rotvec)
    if angle == 0:
        rotvec = [0, 0, 1]
    else:
        rotvec /= angle

    # rotation and translation
    polyhedron.rotate(rotvec, angle * 180 / np.pi)
    polyhedron.rotate([0, 0, 1], 90)
    polyhedron.rotate([0, 1, 0], 180)

    polyhedron.translate(pos)



    # f = open("fovs.csv", "a")
    # polygons = polygon.toPolygons()
    # f.write(str(polygons[0].vertices[2].pos[0]) + ','
    #         + str(polygons[0].vertices[2].pos[1]) + ','
    #         + str(polygons[0].vertices[2].pos[2]) + ',')
    # f.write(str(polygons[0].vertices[1].pos[0]) + ','
    #         + str(polygons[0].vertices[1].pos[1]) + ','
    #         + str(polygons[0].vertices[1].pos[2]) + ',')
    # f.write(str(polygons[0].vertices[0].pos[0]) + ','
    #         + str(polygons[0].vertices[0].pos[1]) + ','
    #         + str(polygons[0].vertices[0].pos[2]) + ',')
    # f.write(str(polygons[2].vertices[0].pos[0]) + ','
    #         + str(polygons[2].vertices[0].pos[1]) + ','
    #         + str(polygons[2].vertices[0].pos[2]) + ',')
    # f.write(str(polygons[2].vertices[1].pos[0]) + ','
    #         + str(polygons[2].vertices[1].pos[1]) + ','
    #         + str(polygons[2].vertices[1].pos[2]) + '\n')

    return polyhedron
Ejemplo n.º 10
0
 def shapeFromPolygons(self, polys):
     return CSG.fromPolygons(polys)