Skip to content
Merged
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
2 changes: 2 additions & 0 deletions examples/core/export_arrows.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from os.path import join
from numpy import load, zeros, mean, array
from vedo import Mesh
import webbrowser

from SimExporter.core import Exporter

Expand Down Expand Up @@ -34,3 +35,4 @@
exporter.set_camera(factor=0.8, yaw=-80, pitch=60)
exporter.to_html(filename=join('html', 'arrows.html'), background_color='#0D1117', grid_visible=False,
menu_visible=True, frame_visible=False)
webbrowser.open(join('html', 'arrows.html'))
2 changes: 2 additions & 0 deletions examples/core/export_meshes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from numpy import load, mean
from numpy.linalg import norm
from vedo import Mesh
import webbrowser

from SimExporter.core import Exporter

Expand Down Expand Up @@ -40,3 +41,4 @@
exporter.set_camera(factor=0.8, yaw=-80, pitch=60)
exporter.to_html(filename=join('html', 'meshes.html'), background_color='#0D1117', grid_visible=False,
menu_visible=True, frame_visible=True)
webbrowser.open(join('html', 'meshes.html'))
2 changes: 2 additions & 0 deletions examples/core/export_others.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from numpy import array, float32
import k3d
from vedo import Mesh
import webbrowser

from SimExporter.core import Exporter

Expand Down Expand Up @@ -31,3 +32,4 @@

# Export to HTML
exporter.to_html(filename=join('html', 'others.html'))
webbrowser.open(join('html', 'others.html'))
2 changes: 2 additions & 0 deletions examples/core/export_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from numpy import load, mean
from numpy.linalg import norm
from vedo import Mesh
import webbrowser

from SimExporter.core import Exporter

Expand Down Expand Up @@ -33,3 +34,4 @@
exporter.set_camera(factor=0.8, yaw=-80, pitch=60)
exporter.to_html(filename=join('html', 'points.html'), background_color='#0D1117', grid_visible=False,
menu_visible=True, frame_visible=True)
webbrowser.open(join('html', 'points.html'))
2 changes: 2 additions & 0 deletions examples/core/export_tetra_mesh.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from os.path import join
from vedo import Mesh
import webbrowser

from SimExporter.core import Exporter

Expand Down Expand Up @@ -29,3 +30,4 @@
exporter.set_camera(factor=0.8, yaw=-80, pitch=60)
exporter.to_html(filename=join('html', 'tetra.html'), background_color='grey', grid_visible=False, menu_visible=True,
frame_visible=True)
webbrowser.open(join('html', 'tetra.html'))
2 changes: 2 additions & 0 deletions examples/core/github_gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from numpy import load, mean, tile
from numpy.linalg import norm
from vedo import Mesh
import webbrowser

from SimExporter.core import Exporter

Expand Down Expand Up @@ -70,3 +71,4 @@
exporter.set_camera(factor=0.55, yaw=-90, pitch=60)
exporter.to_html(filename=join('html', 'gallery.html'), background_color='#0D1117', grid_visible=False,
menu_visible=False, frame_visible=False)
webbrowser.open(join('html', 'gallery.html'))
2 changes: 2 additions & 0 deletions examples/core/mimesis_website.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from os.path import join
from numpy import load
from vedo import Mesh
import webbrowser

from SimExporter.core import Exporter

Expand Down Expand Up @@ -39,3 +40,4 @@
exporter.set_camera(factor=0.8, yaw=-75, pitch=65)
exporter.to_html(filename=join('html', 'website.html'), background_color="#030929", grid_visible=False,
menu_visible=False, frame_visible=False)
webbrowser.open(join('html', 'website.html'))
17 changes: 10 additions & 7 deletions examples/sofa/data/plugins.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
Sofa.Component.AnimationLoop
Sofa.Component.Collision.Detection.Algorithm
Sofa.Component.Collision.Detection.Intersection
Sofa.Component.ODESolver.Backward
Sofa.Component.LinearSolver.Iterative
Sofa.Component.Constraint.Lagrangian.Solver
Sofa.Component.Constraint.Projective
Sofa.Component.IO.Mesh
Sofa.Component.SolidMechanics.FEM.Elastic
Sofa.Component.LinearSolver.Iterative
Sofa.Component.Mapping.Linear
Sofa.Component.Mass
Sofa.GL.Component.Rendering3D
Sofa.Component.MechanicalLoad
Sofa.Component.Constraint.Projective
Sofa.Component.ODESolver.Backward
Sofa.Component.SolidMechanics.FEM.Elastic
Sofa.Component.StateContainer
Sofa.Component.Topology.Container.Dynamic
Sofa.Component.Topology.Mapping
Sofa.Component.Visual
Sofa.GL.Component.Rendering3D
4 changes: 3 additions & 1 deletion examples/sofa/export_logo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from os.path import join
import numpy as np
import webbrowser

import Sofa.Gui

Expand Down Expand Up @@ -38,7 +39,7 @@ def displacement_field():
colormap_function=displacement_field)

# Init the SOFA simulation AFTER creating the exporter (otherwise, callbacks will not work)
Sofa.Simulation.init(node)
Sofa.Simulation.initRoot(node)

# Launch the SOFA Gui, run a few time steps
Sofa.Gui.GUIManager.Init(program_name="main", gui_name="qglviewer")
Expand All @@ -51,3 +52,4 @@ def displacement_field():
exporter.set_camera(factor=0.8, yaw=0, pitch=0)
exporter.to_html(filename=join('html', 'sofa.html'), background_color='black', grid_visible=False,
menu_visible=True, frame_visible=True)
webbrowser.open(join('html', 'sofa.html'))
8 changes: 1 addition & 7 deletions examples/sofa/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ def __init__(self, root: Sofa.Core.Node, *args, **kwargs):
root.addObject('RequiredPlugin', pluginName=required_plugins)
root.addObject('VisualStyle', displayFlags='showVisualModels showBehaviorModels showForceFields')
root.addObject('DefaultAnimationLoop')
root.addObject('GenericConstraintSolver', maxIterations=10, tolerance=1e-3)
root.addObject('CollisionPipeline')
root.addObject('BruteForceBroadPhase')
root.addObject('BVHNarrowPhase')
root.addObject('DiscreteIntersection')
root.addObject('DefaultContactManager')

# Create the logo object node
root.addChild('logo')
Expand Down Expand Up @@ -56,7 +50,7 @@ def __init__(self, root: Sofa.Core.Node, *args, **kwargs):
node.addObject(Simulation(root=node))

# Init the SOFA simulation
Sofa.Simulation.init(node)
Sofa.Simulation.initRoot(node)

# Launch the SOFA Gui, run a few time steps
Sofa.Gui.GUIManager.Init(program_name="main", gui_name="qglviewer")
Expand Down
15 changes: 14 additions & 1 deletion src/SimExporter/core/exporter.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from typing import List, Union
from os import makedirs
from os.path import join, dirname, exists
import numpy as np
from k3d import Plot
from base64 import b64encode
from zlib import compress
from msgpack import packb

from SimExporter.core.factory import Factory, convert_color
from SimExporter.core.utils import bounding_box


class Exporter:
Expand Down Expand Up @@ -38,7 +40,18 @@ def set_camera(self,
:param pitch: Pitch to apply on the objects.
"""

self._plt.camera = self._plt.get_auto_camera(factor=factor, yaw=yaw, pitch=pitch)
# Compute bounds
boxes = []
for o in self._plt.objects:
try:
bbox = o.get_bounding_box()
except AttributeError:
bbox = bounding_box(o)
boxes.append(bbox)
boxes = np.stack(boxes)
bounds = np.dstack([np.min(boxes[:, 0::2], axis=0), np.max(boxes[:, 1::2], axis=0)]).flatten()

self._plt.camera = self._plt.get_auto_camera(factor=factor, yaw=yaw, pitch=pitch, bounds=bounds)

def to_html(self,
filename: str,
Expand Down
3 changes: 3 additions & 0 deletions src/SimExporter/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def add_points(self,
color: Union[str, List] = 'salmon',
alpha: float = 1.,
point_size: int = 0.1,
dot_shading: bool = False,
colormap_name: str = 'jet',
colormap_range: Optional[List[int]] = None,
colormap_values: Optional[ndarray] = None,
Expand All @@ -112,6 +113,7 @@ def add_points(self,
:param color: Color of the points, either the 'color name' or the [R, G, B] values.
:param alpha: Opacity of the points.
:param point_size: Size of the points.
:param dot_shading: If True, the points are rendered as 2D dots.
:param colormap_name: Color map scheme name, see
https://matplotlib.org/stable/users/explain/colors/colormaps.html#classes-of-colormaps.
:param colormap_range: Range of the color map.
Expand All @@ -125,6 +127,7 @@ def add_points(self,
color=convert_color(color),
opacity=alpha,
point_size=point_size,
dot_shading=dot_shading,
colormap_name=colormap_name,
colormap_range=colormap_range,
colormap_values=colormap_values,
Expand Down
8 changes: 7 additions & 1 deletion src/SimExporter/core/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def points(positions: ndarray,
color: int,
opacity: float,
point_size: float,
dot_shading: bool,
colormap_name: str,
colormap_range: Optional[List[int]],
colormap_values: Optional[ndarray],
Expand All @@ -87,6 +88,7 @@ def points(positions: ndarray,
:param color: Color of the points.
:param opacity: Opacity of the points.
:param point_size: Size of the points.
:param dot_shading: Dot or spheres points.
:param colormap_name: Color map scheme name.
:param colormap_range: Color map range.
:param colormap_values: Color map scalar values.
Expand Down Expand Up @@ -121,12 +123,16 @@ def points(positions: ndarray,
else:
color_range += [colormap_values.min(), colormap_values.max()]

# Adapt shader
shader = 'dot' if dot_shading else '3D'
point_size = 20 * point_size if dot_shading else point_size

# Actually create the k3d points
return k3d.points(positions=positions,
color=color,
opacity=opacity,
point_size=point_size,
shader='3D',
shader=shader,
color_map=color_map,
attribute=color_attribute,
color_range=color_range)
Expand Down
14 changes: 14 additions & 0 deletions src/SimExporter/core/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from numpy import stack
from k3d.helpers import get_bounding_box_point, get_bounding_box_points


def bounding_box(o):

if hasattr(o, 'vertices'):
return get_bounding_box_points(o.vertices['0'], o.model_matrix)
elif hasattr(o, 'positions'):
return get_bounding_box_points(o.positions['0'], o.model_matrix)
elif hasattr(o, 'position'):
return get_bounding_box_point(o.position['0'])
elif hasattr(o, 'origins') and hasattr(o, 'vectors'):
return get_bounding_box_points(stack([o.origins['0'], o.vectors['0']]), o.model_matrix)
2 changes: 2 additions & 0 deletions src/SimExporter/sofa/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def add_sofa_points(self,
color: Union[str, List] = 'salmon',
alpha: float = 1.,
point_size: int = 0.1,
dot_shading: bool = False,
colormap_name: str = 'jet',
colormap_range: Optional[List[int]] = None,
colormap_function: Optional[Callable] = None) -> None:
Expand All @@ -76,6 +77,7 @@ def add_sofa_points(self,
:param color: Color of the points, either the 'color name' or the [R, G, B] values.
:param alpha: Opacity of the points.
:param point_size: Size of the points.
:param dot_shading: If True, the points are rendered as 2D dots.
:param colormap_name: Color map scheme name, see
https://matplotlib.org/stable/users/explain/colors/colormaps.html#classes-of-colormaps.
:param colormap_range: Range of the color map.
Expand Down
Loading