-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
78 lines (56 loc) · 1.79 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from typing import List
import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
import compas
from compas.geometry import Box
from compas.datastructures import Mesh
from compas.itertools import flatten
app = FastAPI()
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)