我们一般是使用VMD来显示分子结构,分子结构或者是DNA结构,我们是从PDB银行下载的。如果我们使用VTK来显示分子结构行不行呢?查阅了资料,发现是可行的。因为在vtk的example中就有这样一个例子。使用pdbreader来读取pdb格式的文件,然后显示。在这里我就抛一块砖头了。
#include <iostream>
#include <cmath>
#include <string>
#include <vtkNew.h>
#include <vtkGlyph3D.h>
#include <vtkNamedColors.h>
#include <vtkLODActor.h>
#include <vtkPDBReader.h> //reader
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSphereSource.h>
#include <vtkTubeFilter.h>
#include <vtkSmartPointer.h>
#include <vtkInteractorStyleTrackballCamera.h>
//必要的文件头
using namespace std;
int main(int, char**)
{
string filename="../data/1mbs.pdb"; //这个文件我会放在链接处的。
vtkNew<vtkNamedColors> colors;
vtkNew<vtkRenderer> renderer;
renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetSize(1024,1024);
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renderWindow);
vtkNew<vtkPDBReader> reader;
reader->SetFileName(filename.c_str());
reader->SetHBScale(1.0);
reader->SetBScale(1.0);
reader->Update();
cout<<"# of atoms is: "<<reader->GetNumberOfAtoms()<<endl;
double resolution=std::sqrt(300000.0/reader->GetNumberOfAtoms());
if(resolution>20)
resolution=20;
if(resolution<4)
resolution=4;
cout<<"Resolution is: "<<resolution<<endl;
vtkNew<vtkSphereSource> sphere;
sphere->SetCenter(0,0,0);
sphere->SetRadius(1);
sphere->SetThetaResolution(static_cast<int>(resolution));
sphere->SetPhiResolution(static_cast<int>(resolution));
vtkNew<vtkGlyph3D> glyph;
glyph->SetInputConnection(reader->GetOutputPort());
glyph->SetOrient(1);
glyph->SetColorMode(1);
glyph->SetScaleMode(2);
glyph->SetScaleFactor(0.25);
glyph->SetSourceConnection(sphere->GetOutputPort());
vtkNew<vtkPolyDataMapper> atomMapper;
atomMapper->SetInputConnection(glyph->GetOutputPort());
atomMapper->UseLookupTableScalarRangeOff();
atomMapper->ScalarVisibilityOn();
atomMapper->SetScalarModeToDefault();
vtkNew<vtkLODActor> atom;
atom->SetMapper(atomMapper);
atom->GetProperty()->SetRepresentationToSurface();
atom->GetProperty()->SetInterpolationToGouraud();
atom->GetProperty()->SetAmbient(0.1);
atom->GetProperty()->SetDiffuse(0.7);
atom->GetProperty()->SetSpecular(0.5);
atom->GetProperty()->SetSpecularPower(80);
atom->GetProperty()->SetSpecularColor(colors->GetColor3d("White").GetData());
atom->SetNumberOfCloudPoints(30000);
renderer->AddActor(atom);
vtkNew<vtkTubeFilter> tube;
tube->SetInputConnection(reader->GetOutputPort());
tube->SetNumberOfSides(static_cast<int>(resolution));
tube->CappingOff();
tube->SetRadius(0.2);
tube->SetVaryRadius(0);
tube->SetRadiusFactor(10);
vtkNew<vtkPolyDataMapper> bondMapper;
bondMapper->SetInputConnection(tube->GetOutputPort());
bondMapper->UseLookupTableScalarRangeOff();
bondMapper->ScalarVisibilityOff();
bondMapper->SetScalarModeToDefault();
vtkNew<vtkLODActor> bond;
bond->SetMapper(bondMapper);
bond->GetProperty()->SetRepresentationToSurface();
bond->GetProperty()->SetInterpolationToGouraud();
bond->GetProperty()->SetAmbient(0.1);
bond->GetProperty()->SetDiffuse(0.7);
bond->GetProperty()->SetSpecular(0.5);
bond->GetProperty()->SetSpecularPower(80);
bond->GetProperty()->SetSpecularColor(colors->GetColor3d("White").GetData());
vtkNew<vtkInteractorStyleTrackballCamera> style;
iren->SetInteractorStyle(style);
renderer->AddActor(bond);
renderWindow->Render(); //这里必须要渲染一下,数据量太大了。
iren->Initialize();
iren->Start();
return 0;
}
图 显示渲染的3D图像
文件链接:https://download.csdn.net/download/kangdehua/90250402
标签:显示,GetProperty,分子结构,VTK,atom,bond,include,glyph,vtkNew From: https://blog.csdn.net/kangdehua/article/details/145060522