-
Notifications
You must be signed in to change notification settings - Fork 1
doc: Add shape optimization demo #45
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5685251
add shape optimization demo
dionhaefner 633e14e
fix doc build
dionhaefner 85ef244
add requirements.txt
dionhaefner 7ceb193
try again
dionhaefner c444bb2
try again
dionhaefner 406e0a2
Merge branch 'main' into dion/shapeopt-demo
dionhaefner 5044de7
:lipstick:
dionhaefner 13d115b
Merge branch 'dion/shapeopt-demo' of github.com:pasteurlabs/tesseract…
dionhaefner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| **/* | ||
| !.gitignore |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dionhaefner marked this conversation as resolved.
Show resolved
Hide resolved
jacanchaplais marked this conversation as resolved.
Show resolved
Hide resolved
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,278 @@ | ||
| from typing import Any | ||
|
|
||
| import numpy as np | ||
| import pyvista as pv | ||
| from pydantic import BaseModel, Field | ||
| from tesseract_core.runtime import Array, Differentiable, Float32, ShapeDType | ||
|
|
||
| # | ||
| # Schemata | ||
| # | ||
|
|
||
|
|
||
| class InputSchema(BaseModel): | ||
| bar_params: Differentiable[ | ||
| Array[ | ||
| (None, None, 3), | ||
| Float32, | ||
| ] | ||
| ] = Field( | ||
| description=( | ||
| "Vertex positions of the bar geometry. " | ||
| "The shape is (num_bars, num_vertices, 3), where num_bars is the number of bars " | ||
| "and num_vertices is the number of vertices per bar. The last dimension represents " | ||
| "the x, y, z coordinates of each vertex." | ||
| ) | ||
| ) | ||
|
|
||
| bar_radius: float = Field( | ||
| default=1.5, | ||
| description=( | ||
| "Radius of the bars in the geometry. " | ||
| "This is a scalar value that defines the thickness of the bars." | ||
| ), | ||
| ) | ||
|
|
||
| Lx: float = Field( | ||
| default=60.0, | ||
| description=( | ||
| "Length of the plane in the x direction. " | ||
| "This is a scalar value that defines the size of the plane along the x-axis." | ||
| ), | ||
| ) | ||
| Ly: float = Field( | ||
| default=30.0, | ||
| description=( | ||
| "Length of the plane in the y direction. " | ||
| "This is a scalar value that defines the size of the plane along the y-axis." | ||
| ), | ||
| ) | ||
| Nx: int = Field( | ||
| default=60, | ||
| description=( | ||
| "Number of points in the x direction. " | ||
| "This is an integer value that defines the resolution of the plane along the x-axis." | ||
| ), | ||
| ) | ||
| Ny: int = Field( | ||
| default=30, | ||
| description=( | ||
| "Number of points in the y direction. " | ||
| "This is an integer value that defines the resolution of the plane along the y-axis." | ||
| ), | ||
| ) | ||
| epsilon: float = Field( | ||
| default=1e-5, | ||
| description=( | ||
| "Epsilon value for finite difference approximation of the Jacobian. " | ||
| "This is a small scalar value used to compute the numerical gradient." | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| class OutputSchema(BaseModel): | ||
| sdf: Differentiable[ | ||
| Array[ | ||
| ( | ||
| None, | ||
| None, | ||
| ), | ||
| Float32, | ||
| ] | ||
| ] = Field(description="SDF field of the geometry") | ||
|
|
||
|
|
||
| # | ||
| # Helper functions | ||
| # | ||
|
|
||
|
|
||
| def build_geometry( | ||
| params: np.ndarray, | ||
| radius: float, | ||
| ) -> list[pv.PolyData]: | ||
| """Build a pyvista geometry from the parameters. | ||
|
|
||
| The parameters are expected to be of shape (n_chains, n_edges_per_chain + 1, 3), | ||
| """ | ||
| n_chains = params.shape[0] | ||
| geometry = [] | ||
|
|
||
| for chain in range(n_chains): | ||
| tube = pv.Spline(points=params[chain]).tube(radius=radius, capping=False) | ||
| geometry.append(tube) | ||
|
|
||
| return geometry | ||
|
|
||
|
|
||
| def compute_sdf( | ||
| params: np.ndarray, | ||
| radius: float, | ||
| Lx: float, | ||
| Ly: float, | ||
| Nx: int, | ||
| Ny: int, | ||
| ) -> pv.PolyData: | ||
| """Create a pyvista plane that has the SDF values stored as a vertex attribute. | ||
|
|
||
| The SDF field is computed based on the geometry defined by the parameters. | ||
| """ | ||
| grid_coords = pv.Plane( | ||
| center=(0, 0, 0), | ||
| direction=(0, 0, 1), | ||
| i_size=Lx, | ||
| j_size=Ly, | ||
| i_resolution=Nx - 1, | ||
| j_resolution=Ny - 1, | ||
| ) | ||
| grid_coords = grid_coords.triangulate() | ||
|
|
||
| geometries = build_geometry( | ||
| params, | ||
| radius=radius, | ||
| ) | ||
|
|
||
| sdf_field = None | ||
|
|
||
| for geometry in geometries: | ||
| # Compute the implicit distance from the geometry to the grid coordinates. | ||
| # The implicit distance is a signed distance field, where positive values | ||
| # are outside the geometry and negative values are inside. | ||
| this_sdf = grid_coords.compute_implicit_distance(geometry.triangulate()) | ||
| if sdf_field is None: | ||
| sdf_field = this_sdf | ||
| else: | ||
| sdf_field["implicit_distance"] = np.minimum( | ||
| sdf_field["implicit_distance"], this_sdf["implicit_distance"] | ||
| ) | ||
|
|
||
| return sdf_field | ||
|
|
||
|
|
||
| def apply_fn( | ||
| params: np.ndarray, | ||
| radius: float, | ||
| Lx: float, | ||
| Ly: float, | ||
| Nx: int, | ||
| Ny: int, | ||
| ) -> np.ndarray: | ||
| """Get the sdf values of a the geometry defined by the parameters as a 2D array.""" | ||
| sdf_geom = compute_sdf( | ||
| params, | ||
| radius=radius, | ||
| Lx=Lx, | ||
| Ly=Ly, | ||
| Nx=Nx, | ||
| Ny=Ny, | ||
| )["implicit_distance"] | ||
|
|
||
| # The implicit distance is a 1D where the indexing is tranposed. | ||
| # We need to reshape it to a 2D array with the shape (Ny, Nx) and then transpose it to get the correct orientation. | ||
| return sdf_geom.reshape((Ny, Nx)).T | ||
|
|
||
|
|
||
| def jac_sdf_wrt_params( | ||
| params: np.ndarray, | ||
| radius: float, | ||
| Lx: float, | ||
| Ly: float, | ||
| Nx: int, | ||
| Ny: int, | ||
| epsilon: float, | ||
| ) -> np.ndarray: | ||
| """Compute the Jacobian of the SDF values with respect to the parameters. | ||
|
|
||
| The Jacobian is computed by finite differences. | ||
| The shape of the Jacobian is (n_chains, n_edges_per_chain + 1, 3, Nx, Ny). | ||
| """ | ||
| n_chains = params.shape[0] | ||
| n_edges_per_chain = params.shape[1] - 1 | ||
|
|
||
| jac = np.zeros( | ||
| ( | ||
| n_chains, | ||
| n_edges_per_chain + 1, | ||
| 3, # number of dimensions (x, y, z) | ||
| Nx, | ||
| Ny, | ||
| ) | ||
| ) | ||
|
|
||
| sdf_base = apply_fn( | ||
| params, | ||
| radius=radius, | ||
| Lx=Lx, | ||
| Ly=Ly, | ||
| Nx=Nx, | ||
| Ny=Ny, | ||
| ) | ||
|
|
||
| for chain in range(n_chains): | ||
| for vertex in range(0, n_edges_per_chain + 1): | ||
| # we only care about the y coordinate | ||
| i = 1 | ||
| params_eps = params.copy() | ||
| params_eps[chain, vertex, i] += epsilon | ||
|
|
||
| sdf_epsilon = apply_fn( | ||
| params_eps, | ||
| radius=radius, | ||
| Lx=Lx, | ||
| Ly=Ly, | ||
| Nx=Nx, | ||
| Ny=Ny, | ||
| ) | ||
| jac[chain, vertex, i] = (sdf_epsilon - sdf_base) / epsilon | ||
|
|
||
| return jac | ||
|
|
||
|
|
||
| # | ||
| # Tesseract endpoints | ||
| # | ||
|
|
||
|
|
||
| def apply(inputs: InputSchema) -> OutputSchema: | ||
| return OutputSchema( | ||
| sdf=apply_fn( | ||
| inputs.bar_params, | ||
| radius=inputs.bar_radius, | ||
| Lx=inputs.Lx, | ||
| Ly=inputs.Ly, | ||
| Nx=inputs.Nx, | ||
| Ny=inputs.Ny, | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| def vector_jacobian_product( | ||
| inputs: InputSchema, | ||
| vjp_inputs: set[str], | ||
| vjp_outputs: set[str], | ||
| cotangent_vector: dict[str, Any], | ||
| ): | ||
| assert vjp_inputs == {"bar_params"} | ||
| assert vjp_outputs == {"sdf"} | ||
|
|
||
| jac = jac_sdf_wrt_params( | ||
| inputs.bar_params, | ||
| radius=inputs.bar_radius, | ||
| Lx=inputs.Lx, | ||
| Ly=inputs.Ly, | ||
| Nx=inputs.Nx, | ||
| Ny=inputs.Ny, | ||
| epsilon=inputs.epsilon, | ||
| ) | ||
| # Reduce the cotangent vector to the shape of the Jacobian, to compute VJP by hand | ||
| vjp = np.einsum("ijklm,lm->ijk", jac, cotangent_vector["sdf"]).astype(np.float32) | ||
| return {"bar_params": vjp} | ||
|
|
||
|
|
||
| def abstract_eval(abstract_inputs): | ||
| """Calculate output shape of apply from the shape of its inputs.""" | ||
| return { | ||
| "sdf": ShapeDType( | ||
| shape=(abstract_inputs.Nx, abstract_inputs.Ny), dtype="float32" | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| name: design-tube-sdf | ||
| version: "0.1.0" | ||
| description: | | ||
| Tesseract that generates a gridded signed distance function (SDF) for a set of shape parameters. | ||
|
|
||
| Parameters are expected to define the control points and radii of piecewise linear tubes in 3D space. | ||
|
|
||
| Has a VJP endpoint defined that uses finite differences under the hood. | ||
|
|
||
| build_config: | ||
| target_platform: "linux/x86_64" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| numpy==1.26.4 | ||
| pyvista==0.45.2 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.