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

Server Fix (CORS and flatten import) #1

Open
pemmenegger opened this issue Sep 30, 2024 · 3 comments
Open

Server Fix (CORS and flatten import) #1

pemmenegger opened this issue Sep 30, 2024 · 3 comments

Comments

@pemmenegger
Copy link

Hi there,

I followed the tutorial and encountered 2 issues in server.py:

  • from compas.utilities import flatten could not be imported
  • Google Chrome failed to display the objects due to CORS issues

Here is my solution:

from typing import List

import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware

import compas
from compas.geometry import Box
from compas.datastructures import Mesh
from compas.itertools import flatten


app = FastAPI()

origins = [
    "http://localhost:3000",  # Frontend origin
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


class BoxInput(BaseModel):
    xsize: float
    ysize: float
    zsize: float


class MeshOutput(BaseModel):
    vertices: List[float]
    faces: List[int]
    edges: List[int]


def mesh_to_vertices_edges_triangles(mesh: Mesh) -> MeshOutput:
    vertices, faces = mesh.to_vertices_and_faces()
    triangles = []
    for face in faces:
        if len(face) == 3:
            triangles.append(face)
        elif len(face) == 4:
            triangles.append([face[0], face[1], face[2]])
            triangles.append([face[0], face[2], face[3]])
    edges = list(mesh.edges())
    return MeshOutput(
        vertices=list(flatten(vertices)),
        faces=list(flatten(triangles)),
        edges=list(flatten(edges)),
    )


@app.get("/ping")
async def ping():
    return 1


@app.get("/version")
async def version():
    return compas.__version__


@app.get("/load_tubemesh")
async def load_tubemesh() -> MeshOutput:
    mesh = Mesh.from_obj(compas.get("tubemesh.obj"))
    return mesh_to_vertices_edges_triangles(mesh)


@app.get("/load_bunny")
async def load_bunny() -> MeshOutput:
    mesh = Mesh.from_ply(compas.get("bunny.ply"))
    mesh.rotate(3.14159 / 2, [1, 0, 0])
    mesh.scale(30)
    return mesh_to_vertices_edges_triangles(mesh)


@app.post("/box_to_subdivided_mesh")
async def box_to_subdivided_mesh(boxdata: BoxInput) -> MeshOutput:
    box = Box(boxdata.xsize, boxdata.ysize, boxdata.zsize)
    mesh = box.to_mesh()
    ball = mesh.subdivided(k=3)
    return mesh_to_vertices_edges_triangles(ball)


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)
@tomvanmele
Copy link
Member

hi,

thanks for taking the time. do you want to submit a PR so i can merge it into the tutorial?

best,
tom

@pemmenegger
Copy link
Author

sure, but it seems that I'm not allowed to push branches to the repo...

@tomvanmele
Copy link
Member

no, but you're allowed to submit a PR from a fork...

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