From febb0a90e4f618ab8a62091aa0a23d76518568d3 Mon Sep 17 00:00:00 2001 From: Yuman Hordijk Date: Sat, 11 Nov 2023 10:09:04 +0100 Subject: [PATCH 1/4] Added separating of isosurface mesh into disconnected components. It does not seem to improve transparency issues yet --- src/tcviewer/screen.py | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/tcviewer/screen.py b/src/tcviewer/screen.py index 120e609..ddf4441 100644 --- a/src/tcviewer/screen.py +++ b/src/tcviewer/screen.py @@ -6,6 +6,8 @@ import skimage from tcviewer import materials from TCutility import geometry +import networkx as nx +import itertools @@ -54,16 +56,25 @@ def draw_isosurface(self, gridd, isovalue=0, color=None, material=None, with_pha with_phase = False if val.min() < isovalue < val.max(): spacing = gridd.spacing if len(gridd.spacing) == 3 else gridd.spacing * 3 - verts, faces, normals, values = skimage.measure.marching_cubes(val, isovalue, spacing=spacing, method='lorensen') - verts = o3d.cpu.pybind.utility.Vector3dVector(verts + gridd.origin) - triangles = o3d.cpu.pybind.utility.Vector3iVector(faces) - - mesh = o3d.geometry.TriangleMesh(verts, triangles) - if color is not None: - mesh.paint_uniform_color(np.atleast_2d(color)[0]) - - mesh.compute_vertex_normals() - self.add_mesh(mesh, material=material) + verts, faces, normals, values = skimage.measure.marching_cubes(val, isovalue, spacing=spacing) + + edges = [] + for face in faces: + edges.extend(list(itertools.combinations(face, 2))) + g = nx.from_edgelist(edges) + + # compute connected components and print results + components = list(nx.algorithms.components.connected_components(g)) + # separate faces by component + for im, component in enumerate(components): + triangles = o3d.cpu.pybind.utility.Vector3iVector([face for face in faces if set(face) <= component]) # <= operator tests for subset relation + verts_ = o3d.cpu.pybind.utility.Vector3dVector(verts + gridd.origin) + mesh = o3d.geometry.TriangleMesh(verts_, triangles) + if color is not None: + mesh.paint_uniform_color(np.atleast_2d(color)[0]) + + mesh.compute_vertex_normals() + self.add_mesh(mesh, material=material) if with_phase: self.draw_isosurface(gridd, isovalue=-isovalue, color=np.atleast_2d(color)[-1], material=material, with_phase=False) @@ -161,5 +172,7 @@ def draw_axes(self, center=[0, 0, 0], length=1, width=.04, **kwargs): # create a new screen with Screen() as scr: scr.draw_molecule(mol) - scr.draw_orbital(mo, material=materials.orbital_shiny) + mat = materials.orbital_shiny + # mat.base_color = [1, 1, 1, .5] + scr.draw_orbital(mo, material=mat) scr.draw_axes() From c2bd653711c7105e550184bff52f97ecbfeeb9b4 Mon Sep 17 00:00:00 2001 From: Yuman Hordijk <42876712+YHordijk@users.noreply.github.com> Date: Mon, 13 Nov 2023 00:18:03 +0100 Subject: [PATCH 2/4] Update README.md --- README.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f620d41..8c38908 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,19 @@ To get new updates, simply run: ``` git pull ``` +## Usage +TCviewer exports the `Screen` object which can be used to quickly build a scene. All the heavy work, such as setting up the camera, building and coloring meshes, will be done in the background. Please see the examples section for some simple example scripts for you to try out. ## Examples -[insert example outputs] +To draw a molecule, simply run: + +```python +from tcviewer import Screen +from scm import plams + +molecule = 'path/to/molecule.xyz' # molecules can be given as paths to xyz files +molecule = plams.Molecule('path/to/molecule.xyz') # or as plams.Molecule objects +molecule.guess_bonds() +with Screen() as scr: + scr.draw_molecule(molecule) +``` From fb236b1df450c5df0594ac9782888fe830e2abd0 Mon Sep 17 00:00:00 2001 From: Yuman Hordijk <42876712+YHordijk@users.noreply.github.com> Date: Mon, 13 Nov 2023 10:22:15 +0100 Subject: [PATCH 3/4] Update README.md --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index 8c38908..3f3f8c6 100644 --- a/README.md +++ b/README.md @@ -40,3 +40,22 @@ molecule.guess_bonds() with Screen() as scr: scr.draw_molecule(molecule) ``` + +To load and draw the HOMO: + +```python +from yutility import orbitals +import tcviewer +import pathlib + +rkf_dir = pathlib.Path(__file__).parents[0]/'data'/'NH3BH3' + +# load orbitals and choose an MO to draw +orbs = orbitals.Orbitals(rkf_dir/'adf.rkf') +homo = orbs.mos['HOMO'] +# generate a cube file +cub = homo.generate_orbital() + +with tcviewer.Screen() as scr: + scr.draw_cub(cub, 0.03, material=tcviewer.materials.orbital_matte) +``` From 695932cb20ad37dd5bb126c35ae0c5f4017d6db0 Mon Sep 17 00:00:00 2001 From: Yuman Hordijk <42876712+YHordijk@users.noreply.github.com> Date: Mon, 13 Nov 2023 10:22:56 +0100 Subject: [PATCH 4/4] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3f3f8c6..aa2cda7 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ To load and draw the HOMO: ```python from yutility import orbitals -import tcviewer +from tcviewer import Screen, materials import pathlib rkf_dir = pathlib.Path(__file__).parents[0]/'data'/'NH3BH3' @@ -56,6 +56,6 @@ homo = orbs.mos['HOMO'] # generate a cube file cub = homo.generate_orbital() -with tcviewer.Screen() as scr: - scr.draw_cub(cub, 0.03, material=tcviewer.materials.orbital_matte) +with Screen() as scr: + scr.draw_cub(cub, 0.03, material=materials.orbital_matte) ```