1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4 
5   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
6   All rights reserved.
7   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
8 
9      This software is distributed WITHOUT ANY WARRANTY; without even
10      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11      PURPOSE.  See the above copyright notice for more information.
12 
13 =========================================================================*/
14 
15 #include "vtkRegressionTestImage.h"
16 #include "vtkTestUtilities.h"
17 
18 #include "vtkAbstractElectronicData.h"
19 #include "vtkActor.h"
20 #include "vtkCamera.h"
21 #include "vtkColorTransferFunction.h"
22 #include "vtkImageData.h"
23 #include "vtkImageShiftScale.h"
24 #include "vtkMolecule.h"
25 #include "vtkMoleculeMapper.h"
26 #include "vtkNew.h"
27 #include "vtkOpenQubeMoleculeSource.h"
28 #include "vtkPiecewiseFunction.h"
29 #include "vtkRenderWindow.h"
30 #include "vtkRenderWindowInteractor.h"
31 #include "vtkRenderer.h"
32 #include "vtkSimpleBondPerceiver.h"
33 #include "vtkSmartPointer.h"
34 #include "vtkSmartVolumeMapper.h"
35 #include "vtkVolume.h"
36 #include "vtkVolumeProperty.h"
37 
38 #include <openqube/basisset.h>
39 #include <openqube/basissetloader.h>
40 
TestOpenQubeMOPACDensity(int argc,char * argv[])41 int TestOpenQubeMOPACDensity(int argc, char* argv[])
42 {
43   char* fname = vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/2h2o.aux");
44 
45   vtkNew<vtkOpenQubeMoleculeSource> oq;
46   oq->SetFileName(fname);
47   oq->Update();
48 
49   delete[] fname;
50 
51   vtkSmartPointer<vtkMolecule> mol = vtkSmartPointer<vtkMolecule>::New();
52   mol = oq->GetOutput();
53 
54   // If there aren't any bonds, attempt to perceive them
55   if (mol->GetNumberOfBonds() == 0)
56   {
57     cout << "No bonds found. Running simple bond perception...\n";
58     vtkNew<vtkSimpleBondPerceiver> bonder;
59     bonder->SetInputData(mol);
60     bonder->Update();
61     mol = bonder->GetOutput();
62     cout << "Bonds found: " << mol->GetNumberOfBonds() << "\n";
63   }
64 
65   vtkNew<vtkMoleculeMapper> molMapper;
66   molMapper->SetInputData(mol);
67   molMapper->UseLiquoriceStickSettings();
68   molMapper->SetBondRadius(0.1);
69   molMapper->SetAtomicRadiusScaleFactor(0.1);
70 
71   vtkNew<vtkActor> molActor;
72   molActor->SetMapper(molMapper);
73 
74   vtkAbstractElectronicData* edata = oq->GetOutput()->GetElectronicData();
75   if (!edata)
76   {
77     cout << "null vtkAbstractElectronicData returned from "
78             "vtkOpenQubeElectronicData.\n";
79     return EXIT_FAILURE;
80   }
81 
82   cout << "Num electrons: " << edata->GetNumberOfElectrons() << "\n";
83 
84   vtkSmartPointer<vtkImageData> data = vtkSmartPointer<vtkImageData>::New();
85   data = edata->GetElectronDensity();
86   if (!data)
87   {
88     cout << "null vtkImageData returned from vtkOpenQubeElectronicData.\n";
89     return EXIT_FAILURE;
90   }
91 
92   double range[2];
93   data->GetScalarRange(range);
94   cout << "ImageData range: " << range[0] << " " << range[1] << "\n";
95 
96   vtkNew<vtkImageShiftScale> t;
97   t->SetInputData(data);
98   t->SetShift(0.0);
99   double magnitude = range[1];
100   if (fabs(magnitude) < 1e-10)
101     magnitude = 1.0;
102   t->SetScale(255.0 / magnitude);
103   t->SetOutputScalarTypeToDouble();
104 
105   cout << "magnitude: " << magnitude << "\n";
106 
107   t->Update();
108   t->GetOutput()->GetScalarRange(range);
109   cout << "Shifted min/max: " << range[0] << " " << range[1] << "\n";
110 
111   vtkNew<vtkPiecewiseFunction> compositeOpacity;
112   compositeOpacity->AddPoint(0.000, 0.00);
113   compositeOpacity->AddPoint(0.001, 0.00);
114   compositeOpacity->AddPoint(5.000, 0.45);
115   //  compositeOpacity->AddPoint( 10.000, 0.45);
116   compositeOpacity->AddPoint(255.000, 0.90);
117 
118   vtkNew<vtkColorTransferFunction> color;
119   color->AddRGBPoint(0.000, 0.0, 0.0, 0.00);
120   color->AddRGBPoint(0.001, 0.0, 0.0, 0.20);
121   color->AddRGBPoint(5.000, 0.0, 0.0, 0.50);
122   color->AddRGBPoint(255.000, 0.0, 0.0, 1.00);
123 
124   vtkNew<vtkSmartVolumeMapper> volumeMapper;
125   volumeMapper->SetInputConnection(t->GetOutputPort());
126   volumeMapper->SetBlendModeToComposite();
127   volumeMapper->SetInterpolationModeToLinear();
128 
129   vtkNew<vtkVolumeProperty> volumeProperty;
130   volumeProperty->ShadeOff();
131   volumeProperty->SetInterpolationTypeToLinear();
132   volumeProperty->SetScalarOpacity(compositeOpacity);
133   volumeProperty->SetColor(color);
134 
135   vtkNew<vtkVolume> volume;
136   volume->SetMapper(volumeMapper);
137   volume->SetProperty(volumeProperty);
138 
139   vtkNew<vtkRenderer> ren;
140   vtkNew<vtkRenderWindow> win;
141   win->AddRenderer(ren);
142   vtkNew<vtkRenderWindowInteractor> iren;
143   iren->SetRenderWindow(win);
144 
145   ren->AddActor(volume);
146   ren->AddActor(molActor);
147 
148   ren->SetBackground(0.0, 0.0, 0.0);
149   win->SetSize(450, 450);
150   win->Render();
151   ren->GetActiveCamera()->Zoom(2.4);
152 
153   // Finally render the scene and compare the image to a reference image
154   win->SetMultiSamples(0);
155   win->GetInteractor()->Initialize();
156   win->GetInteractor()->Start();
157   cout << volumeMapper->GetLastUsedRenderMode() << "\n";
158   return EXIT_SUCCESS;
159 }
160