Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added display of atomic structure.

  • Loading branch information...
commit 803c7af33e8ed6d18e44f4c8eb02885423944fe0 1 parent e886867
Marcus D. Hanwell authored April 23, 2011
6  CMakeLists.txt
@@ -13,6 +13,10 @@ find_package(Avogadro 1.1.0 REQUIRED NO_MODULE)
13 13
 # Find the VTK library too
14 14
 find_package(VTK REQUIRED)
15 15
 
  16
+# Need open babel for now to get the atom colors
  17
+find_package(OpenBabel2 REQUIRED)
  18
+include_directories(${OPENBABEL2_INCLUDE_DIR})
  19
+
16 20
 # Set up the build environment
17 21
 include(${Avogadro_USE_FILE})
18 22
 include(${VTK_USE_FILE})
@@ -22,3 +26,5 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR})
22 26
 avogadro_plugin(vtkextension "vtkextension.cpp;VTKDialog.cpp")
23 27
 
24 28
 target_link_libraries(vtkextension ${QT_LIBRARIES} QVTK vtkVolumeRendering)
  29
+
  30
+set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
110  VTKDialog.cpp
@@ -23,6 +23,8 @@
23 23
 // Avogadro includes
24 24
 #include <avogadro/glwidget.h>
25 25
 #include <avogadro/molecule.h>
  26
+#include <avogadro/atom.h>
  27
+#include <avogadro/bond.h>
26 28
 #include <avogadro/cube.h>
27 29
 
28 30
 // VTK includes
@@ -39,6 +41,18 @@
39 41
 #include <vtkVolumeProperty.h>
40 42
 #include <vtkImageShiftScale.h>
41 43
 #include <vtkImageData.h>
  44
+#include <vtkLookupTable.h>
  45
+#include <vtkPolyData.h>
  46
+#include <vtkIntArray.h>
  47
+#include <vtkFloatArray.h>
  48
+#include <vtkPoints.h>
  49
+#include <vtkPointData.h>
  50
+#include <vtkCellArray.h>
  51
+#include <vtkPolyDataMapper.h>
  52
+#include <vtkProperty.h>
  53
+
  54
+// For the atom colors...
  55
+#include <openbabel/mol.h>
42 56
 
43 57
 namespace Avogadro
44 58
 {
@@ -47,7 +61,6 @@ VTKDialog::VTKDialog(QWidget* parent, Qt::WindowFlags f)
47 61
   : QDialog(parent, f)
48 62
 {
49 63
   m_qvtkWidget = new QVTKWidget(this);
50  
-  m_context = vtkRenderViewBase::New();
51 64
   m_context->SetInteractor(m_qvtkWidget->GetInteractor());
52 65
   m_qvtkWidget->SetRenderWindow(m_context->GetRenderWindow());
53 66
 
@@ -58,12 +71,14 @@ VTKDialog::VTKDialog(QWidget* parent, Qt::WindowFlags f)
58 71
   QVBoxLayout *layout = new QVBoxLayout(this);
59 72
   layout->addWidget(m_qvtkWidget);
60 73
   setLayout(layout);
  74
+
  75
+  // Some initialization
  76
+  lut();
61 77
 }
62 78
 
63 79
 VTKDialog::~VTKDialog()
64 80
 {
65 81
   m_qvtkWidget->deleteLater();
66  
-  m_context->Delete();
67 82
 }
68 83
 
69 84
 void VTKDialog::setMolecule(Molecule *mol)
@@ -71,9 +86,32 @@ void VTKDialog::setMolecule(Molecule *mol)
71 86
   if (!mol || mol->numCubes() == 0) {
72 87
     return;
73 88
   }
  89
+  moleculePolyData(mol);
74 90
 
75 91
   Cube *cube = mol->cube(0);
76 92
 
  93
+  vtkVolume *volume = cubeVolume(cube);
  94
+  m_context->GetRenderer()->AddViewProp(volume);
  95
+
  96
+  vtkNew<vtkPolyDataMapper> m;
  97
+  m->SetInput(m_moleculePolyData.GetPointer());
  98
+  m->SetScalarRange(0, 106);
  99
+  m->SetLookupTable(m_lut.GetPointer());
  100
+  m->SetScalarModeToUsePointFieldData();
  101
+  m->ColorByArrayComponent("z", 0);
  102
+
  103
+  vtkNew<vtkActor> actor;
  104
+  actor->SetMapper(m.GetPointer());
  105
+  actor->GetProperty()->SetLineWidth(3);
  106
+  actor->GetProperty()->SetPointSize(5);
  107
+
  108
+  m_context->GetRenderer()->AddViewProp(actor.GetPointer());
  109
+
  110
+  m_context->GetRenderer()->ResetCamera();
  111
+}
  112
+
  113
+vtkVolume * VTKDialog::cubeVolume(Cube *cube)
  114
+{
77 115
   qDebug() << "Cube dimensions: " << cube->dimensions().x()
78 116
            << cube->dimensions().y() << cube->dimensions().z();
79 117
 
@@ -154,12 +192,74 @@ void VTKDialog::setMolecule(Molecule *mol)
154 192
   volumeProperty->SetColor(color);
155 193
   color->Delete();
156 194
 
157  
-  volume=vtkVolume::New();
  195
+  volume = vtkVolume::New();
158 196
   volume->SetMapper(volumeMapper);
159 197
   volume->SetProperty(volumeProperty);
160 198
 
161  
-  m_context->GetRenderer()->AddViewProp(volume);
162  
-  m_context->GetRenderer()->ResetCamera();
  199
+  return volume;
  200
+}
  201
+
  202
+void VTKDialog::moleculePolyData(Molecule *mol)
  203
+{
  204
+  unsigned int nAtoms = mol->numAtoms();
  205
+  vtkNew<vtkIntArray> aNums;
  206
+  aNums->SetName("z");
  207
+  aNums->SetNumberOfComponents(1);
  208
+  aNums->SetNumberOfTuples(nAtoms);
  209
+  vtkNew<vtkFloatArray> radius;
  210
+  radius->SetName("sizevecs");
  211
+  radius->SetNumberOfComponents(3);
  212
+  radius->SetNumberOfTuples(nAtoms);
  213
+  vtkNew<vtkPoints> pts;
  214
+  pts->SetNumberOfPoints(nAtoms);
  215
+
  216
+  // Atoms
  217
+  std::vector<unsigned int> orphans;
  218
+  for(unsigned int i = 0; i < nAtoms; ++i) {
  219
+    Atom *a = mol->atom(i);
  220
+    const Eigen::Vector3d *p = a->pos();
  221
+    aNums->SetValue(i, a->atomicNumber());
  222
+    double r = OpenBabel::etab.GetVdwRad(a->atomicNumber());
  223
+    radius->SetTuple3(i, r, r, r);
  224
+    pts->SetPoint(i, p->data());
  225
+
  226
+    if (a->bonds().size() == 0)
  227
+      orphans.push_back(i);
  228
+  }
  229
+
  230
+  // Bonds
  231
+  vtkNew<vtkCellArray> bonds;
  232
+  QList<Bond *> bList = mol->bonds();
  233
+  bonds->Allocate(bList.size(), bList.size());
  234
+  foreach(Bond *bond, bList) {
  235
+    bonds->InsertNextCell(2);
  236
+    bonds->InsertCellPoint(bond->beginAtomId());
  237
+    bonds->InsertCellPoint(bond->endAtomId());
  238
+  }
  239
+  vtkNew<vtkCellArray> verts;
  240
+  verts->Allocate(orphans.size(), orphans.size());
  241
+  foreach(unsigned int o, orphans) {
  242
+    verts->InsertNextCell(1);
  243
+    verts->InsertCellPoint(o);
  244
+  }
  245
+
  246
+  m_moleculePolyData->SetPoints(pts.GetPointer());
  247
+  m_moleculePolyData->SetLines(bonds.GetPointer());
  248
+  m_moleculePolyData->SetVerts(verts.GetPointer());
  249
+
  250
+  m_moleculePolyData->GetPointData()->SetScalars(aNums.GetPointer());
  251
+  m_moleculePolyData->GetPointData()->SetVectors(radius.GetPointer());
  252
+}
  253
+
  254
+void VTKDialog::lut()
  255
+{
  256
+  int n = 106;
  257
+  m_lut->SetNumberOfColors(n);
  258
+  std::vector<double> rgb;
  259
+  for(int i = 0; i < n; ++i) {
  260
+    rgb = OpenBabel::etab.GetRGB(i);
  261
+    m_lut->SetTableValue(i, rgb[0], rgb[1], rgb[2]);
  262
+  }
163 263
 }
164 264
 
165 265
 }
16  VTKDialog.h
@@ -19,14 +19,21 @@
19 19
 
20 20
 #include <QDialog>
21 21
 
  22
+#include <vtkSmartPointer.h>
  23
+#include <vtkNew.h>
  24
+
22 25
 class QVTKWidget;
23 26
 class vtkRenderViewBase;
  27
+class vtkVolume;
  28
+class vtkLookupTable;
  29
+class vtkPolyData;
24 30
 
25 31
 namespace Avogadro
26 32
 {
27 33
 
28 34
 class GLWidget;
29 35
 class Molecule;
  36
+class Cube;
30 37
 
31 38
 class VTKDialog : public QDialog
32 39
 {
@@ -38,11 +45,18 @@ class VTKDialog : public QDialog
38 45
 
39 46
   void setMolecule(Molecule *mol);
40 47
 
  48
+protected:
  49
+  vtkVolume * cubeVolume(Cube *cube);
  50
+  void moleculePolyData(Molecule *mol);
  51
+  void lut();
  52
+
41 53
 private:
42 54
   const GLWidget *m_glwidget;
43 55
   const Molecule *m_molecule;
44 56
   QVTKWidget *m_qvtkWidget;
45  
-  vtkRenderViewBase *m_context;
  57
+  vtkNew<vtkRenderViewBase> m_context;
  58
+  vtkNew<vtkLookupTable> m_lut;
  59
+  vtkNew<vtkPolyData> m_moleculePolyData;
46 60
 };
47 61
 
48 62
 }

0 notes on commit 803c7af

Please sign in to comment.
Something went wrong with that request. Please try again.