Ejemplo n.º 1
0
def sampleSolid(n, solid, vertexList=None):
    # Create a bounding box for the shape
    boundingBox = Bnd_Box()
    brepbndlib_Add(solid, boundingBox)
    xMin, yMin, zMin, xMax, yMax, zMax = boundingBox.Get()
    xSideLength = xMax - xMin
    ySideLength = yMax - yMin
    zSideLength = zMax - zMin

    # Create extrema sampler to measure if the point is in the shape. For now,
    # just initialize it with the same shape. We'll load the vertex later.
    brepDistShapeShape = BRepExtrema_DistShapeShape(solid, solid)

    # Create a random number of vertices and check to see which ones are
    # in the shape.
    vertices = createPointsDataFrame(n)
    vertices.inShape = vertices.inShape.astype('int')
    # Loop over the vertices
    for i in range(0, n):
        # Pick a random point
        x = xMin + random() * xSideLength
        y = yMin + random() * ySideLength
        z = zMin + random() * zSideLength
        # Create a vertex from a geometric point
        gpPoint = gp_Pnt(x, y, z)
        vertexBuilder = BRepBuilderAPI_MakeVertex(gpPoint)
        vertex = vertexBuilder.Vertex()
        # Load the vertex into the extrema calculator
        brepDistShapeShape.LoadS2(vertex)
        brepDistShapeShape.Perform()
        # Compute the containment with the box and store the value
        inShape = 1 if brepDistShapeShape.InnerSolution() else 0
        # Store the shape value
        vertices.set_value(i, 'x', x)
        vertices.set_value(i, 'y', y)
        vertices.set_value(i, 'z', z)
        vertices.set_value(i, 'inShape', inShape)
        if inShape != 0:
            vertexList.append(vertex)

    # Slice the data frame so that only the x,y,z variables for points in the box are saved.
    innerVertices = vertices[vertices.inShape == 1]
    innerCoords = innerVertices[['x', 'y', 'z']]

    return innerCoords
def compute_minimal_distance_between_cubes():
    """ compute the minimal distance between 2 cubes

    the line between the 2 points is rendered in cyan

    """
    b1 = BRepPrimAPI_MakeBox(gp_Pnt(100, 0, 0), 10., 10., 10.).Shape()
    b2 = BRepPrimAPI_MakeBox(gp_Pnt(45, 45, 45), 10., 10., 10.).Shape()
    display.DisplayShape([b1, b2])

    dss = BRepExtrema_DistShapeShape()
    dss.LoadS1(b1)
    dss.LoadS2(b2)
    dss.Perform()

    assert dss.IsDone()

    edg = make_edge(dss.PointOnShape1(1), dss.PointOnShape2(1))
    display.DisplayColoredShape([edg], color="CYAN")