Example #1
0
def plot_outward_facets(scene, title):
    vertices = []
    for vid in range(scene.GetSceneVertexNumber()):
        v = scene.GetSceneVertex(vid)
        x, y, z = v.x, v.y, v.z
        vertices.append((str2float(x), str2float(y), str2float(z)))
    vertices = np.asarray(vertices)

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    cnt = 0
    for fid in range(scene.GetSceneHalfFacetNumber()):
        f = scene.GetSceneHalfFacet(fid)
        # f.outward is a boolean flag that indicates whether this half facet is facing outward or not.
        # The if statement below is merely for visualization and you can skip it.
        if f.outward:
            # Plot this half facet.
            cnt += 1
            color = np.random.rand(3)
            # Plot normals.
            normal_plotted = False
            for fc in f.cycles:
                vcs = vertices[list(fc)]
                vc_center = np.mean(vcs, 0)
                if not normal_plotted:
                    n = np.asarray([str2float(v) for v in f.normal.split()])
                    n /= np.linalg.norm(n)
                    vc_tail = vc_center + n * 0.2
                    a = Arrow3D([vc_center[0], vc_tail[0]],
                                [vc_center[1], vc_tail[1]],
                                [vc_center[2], vc_tail[2]],
                                mutation_scale=15,
                                lw=3,
                                arrowstyle='-|>',
                                color=color)
                    ax.add_artist(a)
                    normal_plotted = True
                vcs = (vcs - vc_center) * 0.95 + vc_center
                vc_cnt = len(fc)
                for i in range(vc_cnt):
                    j = (i + 1) % vc_cnt
                    vi, vj = vcs[i], vcs[j]
                    # Plot edge vi -> vj.
                    ax.plot([vi[0]], [vi[1]], [vi[2]], 'k')
                    a = Arrow3D([vi[0], vj[0]], [vi[1], vj[1]], [vi[2], vj[2]],
                                mutation_scale=15,
                                lw=3,
                                arrowstyle='-|>',
                                color=color)
                    ax.add_artist(a)
    assert (cnt * 2 == scene.GetSceneHalfFacetNumber())

    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_zlabel('z')
    ax.set_title(title)
    plt.draw()
    plt.show()
Example #2
0
 def __init__(self, x, y=None, z=None):
     if y is None:
         x, y, z = x.x, x.y, x.z
     self.p = np.array([str2float(x), str2float(y), str2float(z)])
     # for bizarre reasons we actually need to represent these as strings
     self.x = str(x)
     self.y = str(y)
     self.z = str(z)
Example #3
0
    def sample(c, union=True):
        if c.empty:
            # initial box
            c = c.extrude([(0, 0, 0), (0, 5, 0), (5, 5, 0), (5, 0, 0)],
                          (0, 0, 3))
            faceID = 0
        else:
            faceID = random.choice(range(c.child.GetSceneHalfFacetNumber()))
        f = c.child.GetSceneHalfFacet(faceID)
        polygon = c.child.GenerateRandomPolygon(faceID, 0.5, 0.5, False)
        vertices = polygon.strip().split()
        vertices = [
            Vertex(vertices[3 * i], vertices[3 * i + 1], vertices[3 * i + 2])
            for i in range(len(vertices) // 3)
        ]

        normal = np.asarray([str2float(v) for v in f.normal.split()])
        if not f.outward: normal = -normal
        L = ((normal * normal).sum()**0.5)
        if L < 0.001:
            print("for some reason the normal is nothing")
            return Extrusion.sample(c, union=union)
        normal = normal / L

        magnitude = random.random() * 3 + 1
        direction = list(normal * magnitude)
        command = Extrusion(direction, union, vertices)
        return command
Example #4
0
def plot_poly(ax, vc, color, label):
    vc = np.asarray(vc)
    vc = np.vstack([vc, vc[0]])
    ax.plot(vc[:, 0], vc[:, 1], vc[:, 2], color=color, label=label)


# Plot this facet.
fig = plt.figure()
ax = p3.Axes3D(fig)
vcs = []
for fc in f.cycles:
    vc = []
    for vid in f.cycles[0]:
        v = s.GetTargetVertex(vid) if use_target else s.GetSceneVertex(vid)
        vc.append([str2float(v.x), str2float(v.y), str2float(v.z)])
    plot_poly(ax, vc, 'b', 'facet')
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_zlabel('z')
    vcs.append(vc)

# Pick a facet.
# 0 is the facet id and True/False means the facet comes from the target/canvas-so-far.
# Plot the polygon.
max_iter = 20
folder = 'rand_poly_gif'
if os.path.isdir(folder):
    shutil.rmtree(folder)
os.makedirs(folder, exist_ok=True)
for i in range(max_iter):
Example #5
0
    def __deal_with_rating(self, rating):

        rating = replace_multi(rating, self.site_config.product_rating_split, '').strip()
        return str2float(rating, 1)