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 "vtkTestUtilities.h"
16 #include "vtkRegressionTestImage.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 "vtkRenderWindowInteractor.h"
30 #include "vtkRenderWindow.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/basissetloader.h>
39 #include <openqube/basisset.h>
40 
TestOpenQubeMOPACDensity(int argc,char * argv[])41 int TestOpenQubeMOPACDensity(int argc, char *argv[])
42 {
43   char* fname = vtkTestUtilities::ExpandDataFileName(
44     argc, argv, "Data/2h2o.out");
45 
46   vtkNew<vtkOpenQubeMoleculeSource> oq;
47   oq->SetFileName(fname);
48   oq->Update();
49 
50   delete [] fname;
51 
52   vtkSmartPointer<vtkMolecule> mol = vtkSmartPointer<vtkMolecule>::New();
53   mol = oq->GetOutput();
54 
55   // If there aren't any bonds, attempt to perceive them
56   if (mol->GetNumberOfBonds() == 0)
57   {
58     cout << "No bonds found. Running simple bond perception...\n";
59     vtkNew<vtkSimpleBondPerceiver> bonder;
60     bonder->SetInputData(mol);
61     bonder->Update();
62     mol = bonder->GetOutput();
63     cout << "Bonds found: " << mol->GetNumberOfBonds() << "\n";
64   }
65 
66   vtkNew<vtkMoleculeMapper> molMapper;
67   molMapper->SetInputData(mol);
68   molMapper->UseLiquoriceStickSettings();
69   molMapper->SetBondRadius(0.1);
70   molMapper->SetAtomicRadiusScaleFactor(0.1);
71 
72   vtkNew<vtkActor> molActor;
73   molActor->SetMapper(molMapper);
74 
75   vtkAbstractElectronicData *edata = oq->GetOutput()->GetElectronicData();
76   if (!edata)
77   {
78     cout << "null vtkAbstractElectronicData returned from "
79             "vtkOpenQubeElectronicData.\n";
80     return EXIT_FAILURE;
81   }
82 
83   cout << "Num electrons: " << edata->GetNumberOfElectrons() << "\n";
84 
85   vtkSmartPointer<vtkImageData> data = vtkSmartPointer<vtkImageData>::New();
86   data = edata->GetElectronDensity();
87   if (!data)
88   {
89     cout << "null vtkImageData returned from vtkOpenQubeElectronicData.\n";
90     return EXIT_FAILURE;
91   }
92 
93   double range[2];
94   data->GetScalarRange(range);
95   cout << "ImageData range: " << range[0] <<" "<< range[1] << "\n";
96 
97   vtkNew<vtkImageShiftScale> t;
98   t->SetInputData(data);
99   t->SetShift(0.0);
100   double magnitude = range[1];
101   if(fabs(magnitude) < 1e-10)
102     magnitude = 1.0;
103   t->SetScale(255.0/magnitude);
104   t->SetOutputScalarTypeToDouble();
105 
106   cout << "magnitude: " << magnitude << "\n";
107 
108   t->Update();
109   t->GetOutput()->GetScalarRange(range);
110   cout << "Shifted min/max: " << range[0] << " " << range[1] << "\n";
111 
112   vtkNew<vtkPiecewiseFunction> compositeOpacity;
113   compositeOpacity->AddPoint(  0.000, 0.00);
114   compositeOpacity->AddPoint(  0.001, 0.00);
115   compositeOpacity->AddPoint(  5.000, 0.45);
116 //  compositeOpacity->AddPoint( 10.000, 0.45);
117   compositeOpacity->AddPoint(255.000, 0.90);
118 
119   vtkNew<vtkColorTransferFunction> color;
120   color->AddRGBPoint(  0.000, 0.0, 0.0, 0.00);
121   color->AddRGBPoint(  0.001, 0.0, 0.0, 0.20);
122   color->AddRGBPoint(  5.000, 0.0, 0.0, 0.50);
123   color->AddRGBPoint(255.000, 0.0, 0.0, 1.00);
124 
125   vtkNew<vtkSmartVolumeMapper> volumeMapper;
126   volumeMapper->SetInputConnection(t->GetOutputPort());
127   volumeMapper->SetBlendModeToComposite();
128   volumeMapper->SetInterpolationModeToLinear();
129 
130   vtkNew<vtkVolumeProperty> volumeProperty;
131   volumeProperty->ShadeOff();
132   volumeProperty->SetInterpolationTypeToLinear();
133   volumeProperty->SetScalarOpacity(compositeOpacity);
134   volumeProperty->SetColor(color);
135 
136   vtkNew<vtkVolume> volume;
137   volume->SetMapper(volumeMapper);
138   volume->SetProperty(volumeProperty);
139 
140   vtkNew<vtkRenderer> ren;
141   vtkNew<vtkRenderWindow> win;
142   win->AddRenderer(ren);
143   vtkNew<vtkRenderWindowInteractor> iren;
144   iren->SetRenderWindow(win);
145 
146   ren->AddActor(volume);
147   ren->AddActor(molActor);
148 
149   ren->SetBackground(0.0, 0.0, 0.0);
150   win->SetSize(450,450);
151   win->Render();
152   ren->GetActiveCamera()->Zoom(2.4);
153 
154   // Finally render the scene and compare the image to a reference image
155   win->SetMultiSamples(0);
156   win->GetInteractor()->Initialize();
157   win->GetInteractor()->Start();
158   cout << volumeMapper->GetLastUsedRenderMode() << "\n";
159   return EXIT_SUCCESS;
160 }
161