-
Notifications
You must be signed in to change notification settings - Fork 6
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
Molecule preview button and fast plotting #620
Open
raunakkbanerjee
wants to merge
22
commits into
protos
Choose a base branch
from
Molecule-preview-button
base: protos
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
75d8bd8
Added molecule preview button
15c1882
Merge branch 'protos' of github.com:ISISNeutronMuon/MDANSE into Molec…
43b33de
Added molecule name to preview
abe307f
Merge branch 'protos' of github.com:ISISNeutronMuon/MDANSE into Molec…
4eca1f2
Black formatting
c9e4f27
removed commented code for x, y, z axes
bb56614
Merge branch 'protos' of github.com:ISISNeutronMuon/MDANSE into Molec…
436394f
Added fast plotting and molecule preview functionality
3387587
Deleted unnecessary files
1234683
Merge branch 'protos' of github.com:ISISNeutronMuon/MDANSE into Molec…
2eb14c4
Merge branch 'protos' of github.com:ISISNeutronMuon/MDANSE into Molec…
58eadf6
created unique_molecule_names
bfaf550
Merge branch 'maciej/simplify-chemical-system' into molecule-preview-…
MBartkowiakSTFC 6250ccf
Connect MoleculeWidget to the new ChemicalSystem
MBartkowiakSTFC 31927b2
Merge branch 'maciej/simplify-chemical-system' into molecule-preview-…
MBartkowiakSTFC 089f977
Merge branch 'maciej/simplify-chemical-system' into molecule-preview-…
MBartkowiakSTFC 64ecfd1
Apply black to MDANSE_GUI
MBartkowiakSTFC 46be2c8
Handle more errors when reading H5MD
MBartkowiakSTFC ba1dbba
Merge branch 'protos' of https://github.com/ISISNeutronMuon/MDANSE in…
MBartkowiakSTFC 4d75783
Improve H5MD loading based on new input files
MBartkowiakSTFC d21f020
Merge branch 'maciej/simplify-chemical-system' into molecule-preview-…
MBartkowiakSTFC 71e303b
Add checks for trajectories without molecules in MoleculeWidget
MBartkowiakSTFC 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 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 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
155 changes: 155 additions & 0 deletions
155
MDANSE_GUI/Src/MDANSE_GUI/InputWidgets/MoleculePreviewWidget.py
This file contains 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,155 @@ | ||
from qtpy.QtWidgets import QWidget, QSizePolicy, QVBoxLayout, QLabel, QDialog | ||
from qtpy.Qt3DExtras import Qt3DWindow | ||
from qtpy.QtGui import QColor | ||
from qtpy.Qt3DRender import QDirectionalLight, QGeometryRenderer | ||
from qtpy.QtGui import QColor, QVector3D, QQuaternion, QFont | ||
from qtpy.Qt3DExtras import ( | ||
QPhongMaterial, | ||
QCylinderMesh, | ||
QCuboidMesh, | ||
QPlaneMesh, | ||
QSphereMesh, | ||
Qt3DWindow, | ||
QOrbitCameraController, | ||
) | ||
from qtpy.QtCore import Qt as _Qt | ||
from qtpy.Qt3DCore import QEntity, QTransform | ||
from MDANSE.Chemistry import ATOMS_DATABASE | ||
import numpy as np | ||
|
||
|
||
class MoleculePreviewWidget(QDialog): | ||
def __init__(self, parent, molecule_information, molecule_name): | ||
super().__init__(parent) | ||
self.setWindowTitle("Molecule Preview") | ||
self.resize(800, 600) | ||
self.view = Qt3DWindow() | ||
self.view.defaultFrameGraph().setClearColor( | ||
QColor(0x4D4D4F) | ||
) # molecular viewer mdansechemistry atoms.json | ||
container = QWidget.createWindowContainer( | ||
self.view | ||
) # from mdanse chemistry atoms database | ||
screenSize = self.view.screen().size() | ||
container.setMinimumSize(200, 100) | ||
container.setMaximumSize(screenSize) | ||
container.setSizePolicy( | ||
QSizePolicy.Policy.MinimumExpanding, QSizePolicy.Policy.MinimumExpanding | ||
) | ||
layout = QVBoxLayout() | ||
layout.addWidget(container) | ||
self.rootEntity = QEntity() | ||
self.cuboidTransform = QTransform() | ||
self.axes = [] | ||
mass = [] | ||
coords = [] | ||
info_text = f"Molecule name: {molecule_name}\n" | ||
for key in molecule_information["atom_number"]: | ||
info_text += ( | ||
f"Number of {key} atoms: {molecule_information['atom_number'][key]}\n" | ||
) | ||
|
||
info_text += f"Number of such molecules in trajectory: {molecule_information['no_of_molecules']}\n" | ||
|
||
for i, (index, atom) in enumerate( | ||
molecule_information["atom_information"].items() | ||
): | ||
x, y, z = atom["coords"] | ||
x, y, z = (20 * x - 10, 20 * y - 10, 20 * z - 10) | ||
symbol = atom["symbol"] | ||
colour = ATOMS_DATABASE.get_atom_property(symbol, "color") | ||
radius = ATOMS_DATABASE.get_atom_property(symbol, "covalent_radius") | ||
mass.append(ATOMS_DATABASE.get_atom_property(symbol, "atomic_weight")) | ||
coords.append(atom["coords"]) | ||
r, g, b = [int(x) for x in colour.split(";")] | ||
colour = QColor(r, g, b) | ||
m_sphereEntity = QEntity(self.rootEntity) | ||
sphereMesh = QSphereMesh() | ||
sphereMesh.setRings(20) | ||
sphereMesh.setSlices(20) | ||
sphereMesh.setRadius(radius * 10) | ||
sphereTransform = QTransform() | ||
sphereTransform.setScale(1.0) | ||
sphereTransform.setTranslation(QVector3D(x, y, z)) | ||
sphereMaterial = QPhongMaterial() | ||
sphereMaterial.setDiffuse(colour) | ||
sphereMaterial.setAmbient(colour) # Set ambient to the same as diffuse. | ||
# sphereMaterial.setSpecular(QColor(0, 0, 0)) # Eliminate specular reflection. | ||
# sphereMaterial.setShininess(0.0) # Eliminate shininess. | ||
m_sphereEntity.addComponent(sphereMesh) | ||
m_sphereEntity.addComponent(sphereMaterial) | ||
m_sphereEntity.addComponent(sphereTransform) | ||
|
||
atom_information = molecule_information["atom_information"] | ||
for bond in molecule_information["bond_info"]: | ||
i, j = bond | ||
coord1, coord2 = ( | ||
atom_information[i]["coords"], | ||
atom_information[j]["coords"], | ||
) | ||
coord1 = (20 * coord1[0] - 10, 20 * coord1[1] - 10, 20 * coord1[2] - 10) | ||
coord2 = (20 * coord2[0] - 10, 20 * coord2[1] - 10, 20 * coord2[2] - 10) | ||
direction = QVector3D( | ||
coord2[0] - coord1[0], coord2[1] - coord1[1], coord2[2] - coord1[2] | ||
) | ||
length = direction.length() | ||
direction.normalize() | ||
# Compute rotation | ||
up_vector = QVector3D(0, 1, 0) | ||
axis = QVector3D.crossProduct(up_vector, direction) | ||
angle = float( | ||
np.degrees(np.arccos(QVector3D.dotProduct(up_vector, direction))) | ||
) | ||
|
||
# Create cylinder mesh | ||
cylinder_mesh = QCylinderMesh() | ||
cylinder_mesh.setRadius(radius) | ||
cylinder_mesh.setLength(length) | ||
|
||
# Create material | ||
material = QPhongMaterial() | ||
# material.setDiffuse(QColor(color)) | ||
|
||
# Set transformation | ||
transform = QTransform() | ||
transform.setTranslation(QVector3D(*coord1) + direction * length / 2) | ||
transform.setRotation(QQuaternion.fromAxisAndAngle(axis, angle)) | ||
|
||
# Create entity | ||
entity = QEntity(self.rootEntity) | ||
entity.addComponent(cylinder_mesh) | ||
entity.addComponent(material) | ||
entity.addComponent(transform) | ||
|
||
info_label = QLabel(info_text) | ||
font = QFont("Arial", 12) | ||
info_label.setFont(font) | ||
info_label.setWordWrap(True) | ||
layout.addWidget(info_label) | ||
self.setLayout(layout) | ||
mass = np.array(mass) | ||
coords = np.array(coords) | ||
com = np.einsum("i,ik->k", mass, coords) / np.sum(mass) | ||
x_com, y_com, z_com = 20 * com - 10 | ||
# Camera | ||
self.camera = self.view.camera() | ||
self.camera.lens().setPerspectiveProjection(45.0, 16.0 / 9.0, 0.1, 1000.0) | ||
self.camera.setPosition(QVector3D(x_com + 5, y_com + 5, z_com + 10)) | ||
self.camera.setViewCenter(QVector3D(x_com, y_com, z_com)) | ||
self.camera.setUpVector(QVector3D(0.0, 0.0, 1.0)) | ||
# add light | ||
lightEntity = QEntity(self.rootEntity) | ||
light = QDirectionalLight(lightEntity) | ||
light.setColor(_Qt.white) | ||
light.setIntensity(1) | ||
lightEntity.addComponent(light) | ||
lightTransform = QTransform(lightEntity) | ||
lightTransform.setTranslation(self.camera.position()) | ||
lightEntity.addComponent(lightTransform) | ||
# For camera controls | ||
camController = QOrbitCameraController(self.rootEntity) | ||
camController.setLinearSpeed(-20) | ||
camController.setLookSpeed(-90) | ||
camController.setCamera(self.camera) | ||
self.view.setRootEntity(self.rootEntity) | ||
# # info_box.setStandardButtons(QMessageBox.Close) |
This file contains 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 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 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 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 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll need to update the pyproject.toml and add pyqt3d as a dependency. @MBartkowiakSTFC I guess we should add PyQt6-3D since PyQt6<=6.7.0 is in the pyproject.toml.