Skip to content

Commit

Permalink
frontend api square mesh creation update
Browse files Browse the repository at this point in the history
  • Loading branch information
ryichsecondary committed Dec 18, 2024
1 parent 1c5fd27 commit b128eee
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 24 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ from frontend import App
# make an app with the label "drape"
app = App("drape", renew=True)

# create a square mesh resolution 128
V, F = app.mesh.square(res=128)
# create a square mesh resolution 128 spanning the xz plane
V, F = app.mesh.square(res=128, ex=[1,0,0], ey=[0,0,1])

# add to the asset and name it "sheet"
app.asset.add.tri("sheet", V, F)
Expand All @@ -77,11 +77,11 @@ for i in range(5):
# add a sheet to the scene
obj = scene.add("sheet")

# pick two vertices max towards directions [1,-1,0] and [-1,-1,0]
corner = obj.grab([1, -1, 0]) + obj.grab([-1, -1, 0])
# pick two vertices max towards directions [1,0,-1] and [-1,0,-1]
corner = obj.grab([1, 0, -1]) + obj.grab([-1, 0, -1])

# place it with a vertical offset, rotate it and pin the corners
obj.at(0, gap * i, 0).rotate(90, "x").pin(corner)
# place it with a vertical offset and pin the corners
obj.at(0, gap * i, 0).pin(corner)

# set fiber directions required for the Baraff-Witkin model
obj.direction([1, 0, 0], [0, 0, 1])
Expand Down
4 changes: 2 additions & 2 deletions examples/curtain.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"\n",
"app = App(\"curtain\", renew=True)\n",
"\n",
"V, F = app.mesh.square(res=64)\n",
"V, F = app.mesh.square(res=64,ex=[0,0,1],ey=[0,1,0])\n",
"app.asset.add.tri(\"sheet\", V, F)\n",
"\n",
"V, F = app.mesh.icosphere(r=0.5, subdiv_count=4)\n",
Expand All @@ -22,7 +22,7 @@
"space = 0.25\n",
"for i in range(15):\n",
" obj = scene.add(\"sheet\")\n",
" obj.at(i * space, 0, 0).rotate(90, \"y\")\n",
" obj.at(i * space, 0, 0)\n",
" obj.direction([0,1,0],[0,0,1])\n",
" obj.pin(obj.grab([0, 1, 0]))\n",
"\n",
Expand Down
10 changes: 5 additions & 5 deletions examples/drape.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"# make an app with the label \"drape\"\n",
"app = App(\"drape\", renew=True)\n",
"\n",
"# create a square mesh resolution 128\n",
"V, F = app.mesh.square(res=128)\n",
"# create a square mesh resolution 128 spanning the xz plane\n",
"V, F = app.mesh.square(res=128, ex=[1,0,0], ey=[0,0,1])\n",
"\n",
"# add to the asset and name it \"sheet\"\n",
"app.asset.add.tri(\"sheet\", V, F)\n",
Expand All @@ -37,10 +37,10 @@
" obj = scene.add(\"sheet\")\n",
"\n",
" # pick two corners\n",
" corner = obj.grab([1, -1, 0]) + obj.grab([-1, -1, 0])\n",
" corner = obj.grab([1, 0, -1]) + obj.grab([-1, 0, -1])\n",
"\n",
" # place it with an vertical offset, rorate it and pin the corners\n",
" obj.at(0, gap * i, 0).rotate(90, \"x\").pin(corner)\n",
" # place it with an vertical offset and pin the corners\n",
" obj.at(0, gap * i, 0).pin(corner)\n",
"\n",
" # set fiber directions required for Baraff-Witkin\n",
" obj.direction([1, 0, 0], [0, 0, 1])\n",
Expand Down
21 changes: 18 additions & 3 deletions examples/frontend/_mesh_.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ def box(self, width: float = 1, height: float = 1, depth: float = 1) -> "TriMesh
)

def rectangle(
self, res_x: int = 32, width: float = 2, height: float = 1
self,
res_x: int = 32,
width: float = 2,
height: float = 1,
ex: list[float] = [1, 0, 0],
ey: list[float] = [0, 1, 0],
) -> "TriMesh":
ratio = height / width
res_y = int(res_x * ratio)
Expand All @@ -38,6 +43,10 @@ def rectangle(
X_flat, Y_flat = X.flatten(), Y.flatten()
Z_flat = np.full_like(X_flat, 0)
vert = np.vstack((X_flat, Y_flat, Z_flat)).T
_ex, _ey = np.array(ex), np.array(ey)
for i, v in enumerate(vert):
x, y, _ = v
vert[i] = _ex * x + _ey * y
n_faces = 2 * (res_x - 1) * (res_y - 1)
tri = np.zeros((n_faces, 3), dtype=np.int32)
tri_idx = 0
Expand All @@ -56,8 +65,14 @@ def rectangle(
tri_idx += 2
return TriMesh.create(vert, tri, self._cache_dir)

def square(self, res: int = 32, size: float = 2) -> "TriMesh":
return self.rectangle(res, size, size)
def square(
self,
res: int = 32,
size: float = 2,
ex: list[float] = [1, 0, 0],
ey: list[float] = [0, 1, 0],
) -> "TriMesh":
return self.rectangle(res, size, size, ex, ey)

def circle(self, n: int = 32, r: float = 1, ntri: int = 1024) -> "TriMesh":
pts = []
Expand Down
2 changes: 1 addition & 1 deletion examples/hang.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"\n",
"app = App(\"hang\", renew=True)\n",
"\n",
"V, F = app.mesh.square(res=128)\n",
"V, F = app.mesh.square(res=128,ex=[1,0,0],ey=[0,1,0])\n",
"app.asset.add.tri(\"sheet\", V, F)\n",
"\n",
"scene = app.scene.create(\"hang-sheet\")\n",
Expand Down
4 changes: 2 additions & 2 deletions examples/headless.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

app = App("headless").clear()

V, F = app.mesh.square(res=64)
V, F = app.mesh.square(res=64,ex=[0,0,1],ey=[0,1,0])
app.asset.add.tri("sheet", V, F)

V, F = app.mesh.icosphere(r=0.5, subdiv_count=4)
Expand All @@ -14,7 +14,7 @@
space = 0.25
for i in range(5):
obj = scene.add("sheet")
obj.at(i * space, 0, 0).rotate(90, "y")
obj.at(i * space, 0, 0)
obj.direction([0, 1, 0], [0, 0, 1])
obj.pin(obj.grab([0, 1, 0]))

Expand Down
6 changes: 3 additions & 3 deletions examples/stack.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"\n",
"app = App(\"stack\", renew=True)\n",
"\n",
"V, F = app.mesh.square(res=128)\n",
"V, F = app.mesh.square(res=128,ex=[1,0,0],ey=[0,0,1])\n",
"app.asset.add.tri(\"sheet\", V, F)\n",
"\n",
"V, F, T = app.mesh.icosphere(r=0.25, subdiv_count=4).tetrahedralize()\n",
Expand All @@ -24,8 +24,8 @@
"for i in range(n):\n",
" y = (i + 1) * space\n",
" deg = i * 90 / n\n",
" obj = scene.add(\"sheet\").direction([1,0,0],[0,1,0])\n",
" obj.at(0, y, 0).rotate(90,\"x\").rotate(deg,\"y\")\n",
" obj = scene.add(\"sheet\").direction([1,0,0],[0,0,1])\n",
" obj.at(0, y, 0).rotate(deg,\"y\")\n",
"\n",
"scene.add(\"sphere\").at(0,1,0).velocity(0,-5,0)\n",
"\n",
Expand Down
4 changes: 2 additions & 2 deletions examples/trampoline.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
"\n",
"app = App(\"trampoline\", renew=True)\n",
"\n",
"V, F = app.mesh.square(res=128)\n",
"V, F = app.mesh.square(res=128,ex=[1,0,0],ey=[0,0,1])\n",
"app.asset.add.tri(\"sheet\", V, F)\n",
"\n",
"V, F, T = app.mesh.preset(\"armadillo\").decimate(30000).tetrahedralize().normalize()\n",
"app.asset.add.tet(\"armadillo\", V, F, T)\n",
"\n",
"scene = app.scene.create(\"drop-vel-5\")\n",
"\n",
"sheet = scene.add(\"sheet\").direction([1,0,0],[0,1,0]).rotate(90,\"x\")\n",
"sheet = scene.add(\"sheet\").direction([1,0,0],[0,0,1])\n",
"sheet.pin(sheet.grab([-1,0,0])+sheet.grab([1,0,0]))\n",
"\n",
"armadillo = scene.add(\"armadillo\").scale(0.75).rotate(180,\"y\")\n",
Expand Down

0 comments on commit b128eee

Please sign in to comment.