Skip to content

Python api #4

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Implemented zero-copy integration between Python (NumPy) and C++ (Eigen/ShapeOp).
* Constraints: ClosenessConstraint (with target position variant), EdgeStrainConstraint, ShrinkingEdgeConstraint, CircleConstraint, PlaneConstraint, BendingConstraint, SimilarityConstraint, RegularPolygonConstraint, ShapeConstraint
* Forces: VertexForce, NormalForce, GravityForce
* Added MeshSolver class.

### Changed

Expand Down
14,815 changes: 14,814 additions & 1 deletion data/hex_mesh.json

Large diffs are not rendered by default.

35 changes: 14 additions & 21 deletions docs/api/compas_shapeop.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
compas_shapeop
********************************************************************************

.. currentmodule:: compas_shapeop
.. currentmodule:: compas_shapeop.solvers

Classes
=======
Expand All @@ -11,7 +11,7 @@ Classes
:toctree: generated/
:nosignatures:

Solver
MeshSolver

Functions and Methods
=====================
Expand All @@ -23,9 +23,8 @@ Mesh Integration
:toctree: generated/
:nosignatures:

Solver.from_mesh
Solver.add_mesh_edge_strain_constraint
Solver.add_mesh_vertex_force
MeshSolver.from_obj
MeshSolver.from_grid

Constraints
-----------
Expand All @@ -34,16 +33,13 @@ Constraints
:toctree: generated/
:nosignatures:

Solver.add_closeness_constraint
Solver.add_closeness_constraint_with_position
Solver.add_edge_strain_constraint
Solver.add_shrinking_edge_constraint
Solver.add_circle_constraint
Solver.add_plane_constraint
Solver.add_similarity_constraint
Solver.add_regular_polygon_constraint
Solver.add_bending_constraint
Solver.add_shape_constraint
MeshSolver.constrain_edge_lengths
MeshSolver.constrain_face_diagonals
MeshSolver.constrain_face_planarity
MeshSolver.constrain_face_regularization
MeshSolver.constrain_triface_bending
MeshSolver.fix_vertex
MeshSolver.fix_vertices

Forces
------
Expand All @@ -52,9 +48,8 @@ Forces
:toctree: generated/
:nosignatures:

Solver.add_vertex_force
Solver.add_normal_force_with_faces
Solver.add_gravity_force
MeshSolver.add_gravity
MeshSolver.inflate

Core Methods
------------
Expand All @@ -63,9 +58,7 @@ Core Methods
:toctree: generated/
:nosignatures:

Solver.points
Solver.init
Solver.solve
MeshSolver.solve

.. toctree::
:maxdepth: 1
2 changes: 1 addition & 1 deletion docs/examples/bending_constraint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ This example demonstrates how to use COMPAS ShapeOp to apply bending constraints
:figclass: figure
:class: figure-img img-fluid

.. literalinclude:: ../../examples/bending_constraint.py
.. literalinclude:: ../../examples/meshsolver_bending_constraint.py
:language: python
2 changes: 1 addition & 1 deletion docs/examples/circularization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ This example demonstrates how to use COMPAS ShapeOp to apply circle constraints
:figclass: figure
:class: figure-img img-fluid

.. literalinclude:: ../../examples/circularization.py
.. literalinclude:: ../../examples/meshsolver_circularization.py
:language: python
2 changes: 1 addition & 1 deletion docs/examples/closeness_constraint_with_target.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ This example demonstrates how to use COMPAS ShapeOp to create a tensioned cable
:figclass: figure
:class: figure-img img-fluid

.. literalinclude:: ../../examples/closeness_constraint_with_target.py
.. literalinclude:: ../../examples/meshsolver_closeness_constraint_with_target.py
:language: python
2 changes: 1 addition & 1 deletion docs/examples/face_normal_force.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ Vertex forces are computed by applying a normal force to each face of the mesh.
:figclass: figure
:class: figure-img img-fluid

.. literalinclude:: ../../examples/face_normal_force.py
.. literalinclude:: ../../examples/meshsolver_face_normal_force.py
:language: python
2 changes: 1 addition & 1 deletion docs/examples/mesh_regularization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ This example demonstrates how to use COMPAS ShapeOp to regularize a mesh by appl
:figclass: figure
:class: figure-img img-fluid

.. literalinclude:: ../../examples/mesh_regularization.py
.. literalinclude:: ../../examples/meshsolver_mesh_regularization.py
:language: python
2 changes: 1 addition & 1 deletion docs/examples/plane_projection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ This example demonstrates how to project mesh faces vertices onto a plane. Edges
:figclass: figure
:class: figure-img img-fluid

.. literalinclude:: ../../examples/plane_projection.py
.. literalinclude:: ../../examples/meshsolver_plane_projection.py
:language: python
2 changes: 1 addition & 1 deletion docs/examples/vertex_force_and_closeness_constraint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ First we create a grid mesh and initialize the solver from it. Then we add close
:figclass: figure
:class: figure-img img-fluid

.. literalinclude:: ../../examples/vertex_force_and_closeness_constraint.py
.. literalinclude:: ../../examples/meshsolver_vertex_force_and_closeness_constraint.py
:language: python
46 changes: 23 additions & 23 deletions docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,33 @@ Basic Workflow
The basic workflow for using COMPAS ShapeOp involves:

1. Creating or loading a mesh
2. Initializing the solver with mesh vertices
2. Creating a solver with the mesh
3. Adding constraints and forces
4. Initializing the solver
5. Running the solver
6. Updating the mesh with the optimized vertex positions
4. Running the solver to optimize the mesh

The solver automatically handles initialization and mesh vertex updates.

Here's a simple example:

.. code-block:: python

from compas.datastructures import Mesh
from compas_shapeop import Solver
from compas_shapeop import MeshSolver

# 1. Create a mesh
mesh = Mesh.from_obj("data/m0.obj")

# 2. Initialize the solver
solver = Solver.from_mesh(mesh)
solver = MeshSolver(mesh)

# 3. Add constraints and forces
solver.add_mesh_edge_strain_constraint(mesh, weight=1.0)
solver.add_plane_constraint(mesh.face_vertices(0), weight=10.0)

# 4. Initialize the solver
points_ref = solver.init()
solver.constrain_edge_lengths(weight=1.0)
solver.constrain_face_planarity(weight=10.0)

# 5. Run the solver
# 4. Run the solver
solver.solve(iterations=10)

# 6. Update the mesh
# The mesh vertices are automatically updated
for i, vertex in enumerate(mesh.vertices()):
mesh.vertex_attributes(vertex, "xyz", points_ref[i])

Expand Down Expand Up @@ -95,16 +92,23 @@ Example: Adding Gravity
.. code-block:: python

# Add downward force (gravity) to all vertices
solver.add_mesh_vertex_force(mesh, 0, 0, -0.001)
solver.add_gravity(fz=-0.001)

Mesh Integration
================

COMPAS ShapeOp provides convenience methods for working with COMPAS meshes:

* ``Solver.from_mesh(mesh)``: Initialize a solver with mesh vertices
* ``add_mesh_edge_strain_constraint()``: Add edge constraints to all mesh edges
* ``add_mesh_vertex_force()``: Add forces to all vertices
* ``MeshSolver(mesh)``: Create a solver from a COMPAS mesh
* ``MeshSolver.from_obj(path)``: Create a solver from an OBJ file
* ``MeshSolver.from_grid(dx, nx, dy, ny)``: Create a solver from a grid mesh
* ``constrain_edge_lengths()``: Add edge length constraints
* ``constrain_face_planarity()``: Add face planarity constraints
* ``constrain_face_regularization()``: Add face regularization constraints
* ``constrain_triface_bending()``: Add bending constraints between triangular faces
* ``fix_vertices()``: Fix vertices in place or to target positions
* ``add_gravity()``: Add gravity force
* ``inflate()``: Add inflation force

These methods simplify the process of setting up constraints for complex meshes.

Expand All @@ -115,12 +119,8 @@ One of the key features of COMPAS ShapeOp is its zero-copy integration between P

.. code-block:: python

# Setting points (using the property setter)
solver = Solver()
solver.points = mesh.vertices_attributes('xyz') # Automatically converts to NumPy array
solver.init()

# Getting points (direct reference to solver's memory)
# The solver's points property provides direct access to the C++ solver's memory
solver = MeshSolver(mesh)
points = solver.points # This is a zero-copy view into the C++ solver's memory

# Points are directly modified in the solver's memory
Expand Down
1 change: 0 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ channels:
dependencies:
- python <=3.12
- pip
- cmake
- compas
- pip:
- -r requirements-dev.txt
Expand Down
68 changes: 0 additions & 68 deletions examples/bending_constraint.py

This file was deleted.

Loading
Loading