Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to calculate polygon intersections? #3

Open
DBraun opened this issue Dec 11, 2020 · 1 comment
Open

How to calculate polygon intersections? #3

DBraun opened this issue Dec 11, 2020 · 1 comment

Comments

@DBraun
Copy link

DBraun commented Dec 11, 2020

Suppose I have a box and another box that has been shrunk in two dimensions and elongated in the third. The stretched box then intersects the first box, requiring some additional consideration for rendering.

image

It would be cool to support intersections like these if it's not already possible.

import numpy, svg3d, pyrr, math

def get_box_faces(sx, sy, sz):

    verts = numpy.float32(
        [(-1, -1, -1), (-1, -1, 1), (1, -1, 1), (1, -1, -1),
         (-1,  1, -1), (-1,  1, 1), (1,  1, 1), (1,  1, -1),
        ])

    faces = numpy.int32([
        (0, 1, 2, 3),
        (4, 5, 6, 7),
        (0, 1, 5, 4),
        (1, 2, 6, 5),
        (2, 3, 7, 6),
        (3, 0, 4, 7)
        ])

    return 0.5*numpy.float32([sx, sy, sz])*(verts[faces])


def generate_svg(filename):
    view = pyrr.matrix44.create_look_at(
        eye=[50, 40, 120], target=[0, 0, 0], up=[0, 1, 0]
    )
    projection = pyrr.matrix44.create_perspective_projection(
        fovy=15, aspect=1, near=10, far=200
    )
    camera = svg3d.Camera(view, projection)

    style = dict(
        fill="white",
        fill_opacity="0.75",
        stroke="black",
        stroke_linejoin="round",
        stroke_width="0.005",
    )

    faces = numpy.concatenate(
        [get_box_faces(4, 4, 60), get_box_faces(14, 14, 14)]
        )
    print(faces.shape)
    mesh = svg3d.Mesh(faces, style=style)
    view = svg3d.View(camera, svg3d.Scene([mesh]))
    svg3d.Engine([view]).render(filename)

generate_svg("boxes.svg")
@villares
Copy link

Cheers @DBraun! I have half a solution, maybe... you could try union with the trimesh library (https://trimsh.org/index.html), I just wouldn't know how to convert the trimesh meshes into the arrays necessary to use svg3D... I'd love to see an example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants