Skip to content

Commit

Permalink
Merge pull request #36 from SPARC-FAIR-Codeathon/mesh_generation
Browse files Browse the repository at this point in the history
Mesh generation
  • Loading branch information
savindi-wijenayaka authored Aug 13, 2024
2 parents b42a096 + 9c9b6fd commit 98fc1d4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
scaffold.update_mesh_label(original_name="unnamed", new_name="whole_stomach")

scaffold.plot()

scaffold.export("output.stl")
2 changes: 1 addition & 1 deletion src/sparc_spy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from sparc_spy.mesh import Mesh
from sparc_spy.meshes import Meshes
from sparc_spy.scaffold import Scaffold
23 changes: 14 additions & 9 deletions src/sparc_spy/mesh.py → src/sparc_spy/meshes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@
import shutil
import pyvista as pv
from io import StringIO


class Meshes(object):
class Meshes(pv.MultiBlock):
def __init__(self) -> None:
self.meshes = pv.MultiBlock()
super().__init__()

def add_mesh(self, label: str, mesh: pv.PolyData):
self.meshes.append(label, mesh)
self.append(label, mesh)

def export(self, output_filepath: str = "output.stl", base_path='.'):
save_multiblock_stl(self.meshes, output_filepath, base_path)
save_multiblock_stl(self, output_filepath, base_path)

def items(self):
items = []
for key in self.meshes.keys():
items.append((key, self.meshes[key]))
return items
def items(self):
'''
This function should return the label and meshes (pv.PolyData) in the MultiBlock object.
'''
blocks = []
for name in self.keys():
blocks.append((name, self[name]))

return blocks


def save_multiblock_stl(multiblock, filename, base_path='.'):
Expand Down
18 changes: 10 additions & 8 deletions src/sparc_spy/scaffold.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np
import pyvista as pv

from sparc_spy import Mesh
from sparc_spy import Meshes


def populate_metadata(paths):
Expand Down Expand Up @@ -83,11 +83,11 @@ def populate_metadata(paths):


class Scaffold(object):
meshes: Dict
meshes: Meshes

def __init__(self, name, derivative_dir: str):
self.name = name
self.meshes = {}
self.meshes = Meshes()
self.metadata = populate_metadata(self.__read_jsons(derivative_dir))
self.geometry = self.build_scaffold(derivative_dir)

Expand Down Expand Up @@ -118,7 +118,7 @@ def build_scaffold(self, derivative_dir: str):
vertices = vertices.reshape(-1, 3)

mesh = pv.PolyData(vertices, faces)
self.meshes[label] = mesh
self.meshes[label] = mesh

def plot(self):
pv.global_theme.color_cycler = ["#DA627D", "#33658A", "#86BBD8", "#06969A", "#9A348E"]
Expand All @@ -128,19 +128,21 @@ def plot(self):
pl.add_legend()
pl.show()

def export(self, output_filepath: str = "output.vtk"):
"""Export the scaffold to a .vtk file
def export(self, output_filepath: str = "output.stl", base_path="."):
"""Export the scaffold to a .stl file
Args:
output_filepath (str, optional): output_filepath (str): Output file
path to save .vtk file. Defaults to "output.vtk".
"""
"""
self.meshes.export(output_filepath, base_path)


def get_metadata(self):
"""Show a tabular view of metadata that is important to the user"""
return

def add_mesh(self, mesh_name: str, mesh: Mesh):
def add_mesh(self, mesh_name: str, mesh: pv.PolyData):
"""Modify self.meshes and add a new mesh to the list.
Args:
Expand Down

0 comments on commit 98fc1d4

Please sign in to comment.