예제 #1
0
    def _create_hole(smoothness):
        # calculate vertices of square
        square_vertices = np.array([
            [0.5, 0.5, 0.],
            [-0.5, 0.5, 0.],
            [-0.5, -0.5, 0.],
            [0.5, -0.5, 0.],
        ])

        # calculate vertices of circular aperture (from +x axis going ccw)
        front_circle_vertices = np.zeros((smoothness, 3))
        for i in range(smoothness):
            angle = 2 * pi * i / smoothness
            radial_line = np.array([cos(angle), sin(angle), 0.]) * 0.5
            front_circle_vertices[i][:] = radial_line

        # The square vertices will be appended to the circular vertices
        front_vertices = np.append(front_circle_vertices, square_vertices, 0)

        # Create front face of aperture
        triangle_indices = []
        for i in range(len(front_circle_vertices)):
            quadrant = int(4 * float(i) / smoothness)
            closest_corner_index = len(front_circle_vertices) + quadrant
            triangle_indices.append(
                [closest_corner_index, i, (i + 1) % len(front_circle_vertices)])

        front_face = GLPolygon(front_vertices, triangle_indices)

        # Create back face
        back_face = front_face.clone()
        back_face.orientation[1] = 180.
        back_face.position[2] = 1.

        # Create cylinder between faces - starting by creating the circular
        # vertices of the back face...
        back_circular_vertices = np.array(front_circle_vertices)
        back_circular_vertices[:, 2] += 1.
        back_circle_start_index = len(front_circle_vertices)
        circle_vertices = np.append(front_circle_vertices,
                                    back_circular_vertices,
                                    0)

        # ...and now create the cylinder triangles
        triangle_indices = []
        for front_vertex in range(len(front_circle_vertices)) + [0]:
            back_vertex = front_vertex + back_circle_start_index
            triangle_indices.extend([front_vertex, back_vertex])
        cylinder = GLPolygon.triangle_strip(circle_vertices, triangle_indices)

        return CompositePolygon({'front_face': front_face,
                                 'back_face': back_face,
                                 'cylinder': cylinder})
예제 #2
0
def create_unit_cube():
    vertices = np.array([
        [0., 0., 0.],
        [1., 0., 0.],
        [0., 1., 0.],
        [1., 1., 0.],
        [0., 0., 1.],
        [1., 0., 1.],
        [0., 1., 1.],
        [1., 1., 1.],
    ])

    polygons = {'top': GLPolygon.triangle_strip(vertices, [0, 2, 1, 3]),
                'bottom': GLPolygon.triangle_strip(vertices, [4, 5, 6, 7]),
                'lower': GLPolygon.triangle_strip(vertices, [0, 1, 4, 5]),
                'upper': GLPolygon.triangle_strip(vertices, [2, 6, 3, 7]),
                'left': GLPolygon.triangle_strip(vertices, [0, 4, 2, 6]),
                'right': GLPolygon.triangle_strip(vertices, [1, 3, 5, 7])}

    return CompositePolygon(polygons)