Commit 2f7110c7 authored by David C. Lonie's avatar David C. Lonie

Add support for reading/writing vtkMolecules in IO/Legacy.

parent 375be371
Pipeline #20148 passed with stage
......@@ -7,6 +7,7 @@ vtk_add_test_cxx(${vtk-module}CxxTests tests
TestFastRender.cxx
TestLiquoriceSticks.cxx
TestMolecule.cxx,NO_VALID
TestMoleculeIOLegacy.cxx
TestMoleculeSelection.cxx,NO_VALID
TestMoleculeMapperPropertyUpdate.cxx
TestMultiCylinderOn.cxx
......
/*=========================================================================
Program: Visualization Toolkit
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkTestUtilities.h"
#include "vtkRegressionTestImage.h"
#include "vtkActor.h"
#include "vtkCamera.h"
#include "vtkGenericDataObjectReader.h"
#include "vtkGenericDataObjectWriter.h"
#include "vtkLight.h"
#include "vtkMolecule.h"
#include "vtkMoleculeMapper.h"
#include "vtkNew.h"
#include "vtkProperty.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
int TestMoleculeIOLegacy(int, char *[])
{
vtkNew<vtkMolecule> mol;
mol->Initialize();
vtkAtom O1 = mol->AppendAtom(8, 3.0088731969, 1.1344098673, 0.9985902874);
vtkAtom O2 = mol->AppendAtom(8, -0.2616286966, 2.7806709534, 0.7027800226);
vtkAtom C1 = mol->AppendAtom(6, -2.0738607910, 1.2298524695, 0.3421802228);
vtkAtom C2 = mol->AppendAtom(6, -1.4140240045, 0.1045928523, 0.0352265378);
vtkAtom C3 = mol->AppendAtom(6, 0.0000000000, 0.0000000000, 0.0000000000);
vtkAtom C4 = mol->AppendAtom(6, 1.2001889412, 0.0000000000, 0.0000000000);
vtkAtom C5 = mol->AppendAtom(6, -1.4612030913, 2.5403617582, 0.6885503164);
vtkAtom C6 = mol->AppendAtom(6, 2.6528126498, 0.1432895796, 0.0427014196);
vtkAtom H1 = mol->AppendAtom(1, -3.1589178142, 1.2268537165, 0.3536340040);
vtkAtom H2 = mol->AppendAtom(1, -1.9782163251, -0.7930325394, -0.1986937306);
vtkAtom H3 = mol->AppendAtom(1, 3.0459155564, 0.4511167867, -0.9307386568);
vtkAtom H4 = mol->AppendAtom(1, 3.1371551056, -0.7952192984, 0.3266426961);
vtkAtom H5 = mol->AppendAtom(1, 2.3344947615, 1.8381683043, 0.9310726537);
vtkAtom H6 = mol->AppendAtom(1, -2.1991803919, 3.3206134015, 0.9413825084);
mol->AppendBond( C1, C5, 1);
mol->AppendBond( C1, C2, 2);
mol->AppendBond( C2, C3, 1);
mol->AppendBond( C3, C4, 3);
mol->AppendBond( C4, C6, 1);
mol->AppendBond( C5, O2, 2);
mol->AppendBond( C6, O1, 1);
mol->AppendBond( C5, H6, 1);
mol->AppendBond( C1, H1, 1);
mol->AppendBond( C2, H2, 1);
mol->AppendBond( C6, H3, 1);
mol->AppendBond( C6, H4, 1);
mol->AppendBond( O1, H5, 1);
// Test passing the molecule through the IO reader/writer:
vtkNew<vtkGenericDataObjectWriter> writer;
writer->SetInputData(mol.Get());
writer->WriteToOutputStringOn();
writer->Write();
vtkNew<vtkGenericDataObjectReader> reader;
reader->ReadFromInputStringOn();
reader->SetInputString(writer->GetOutputStdString());
reader->Update();
vtkNew<vtkMoleculeMapper> molmapper;
molmapper->SetInputConnection(reader->GetOutputPort());
molmapper->UseBallAndStickSettings();
vtkNew<vtkActor> actor;
actor->SetMapper(molmapper.GetPointer());
actor->GetProperty()->SetAmbient(0.0);
actor->GetProperty()->SetDiffuse(0.0);
actor->GetProperty()->SetSpecular(0.0);
actor->GetProperty()->SetSpecularPower(40);
vtkNew<vtkLight> light;
light->SetLightTypeToCameraLight();
light->SetPosition(1.0, 1.0, 1.0);
vtkNew<vtkRenderer> ren;
vtkNew<vtkRenderWindow> win;
win->AddRenderer(ren.GetPointer());
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(win.GetPointer());
ren->AddActor(actor.GetPointer());
ren->SetBackground(0.0, 0.0, 0.0);
win->SetSize(450, 450);
win->SetMultiSamples(0);
win->Render();
// Finally render the scene and compare the image to a reference image
win->GetInteractor()->Initialize();
win->GetInteractor()->Start();
return EXIT_SUCCESS;
}
......@@ -12,6 +12,7 @@ vtk_module(vtkDomainsChemistry
vtkIOXML
vtkFiltersSources
TEST_DEPENDS
vtkIOLegacy
vtkTestingCore
vtkTestingRendering
vtkInteractionStyle
......
......@@ -22,6 +22,7 @@
#include "vtkImageData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMolecule.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkMultiPieceDataSet.h"
#include "vtkNonOverlappingAMR.h"
......@@ -128,6 +129,7 @@ int vtkGenericDataObjectReader::RequestDataObject(
case VTK_DIRECTED_GRAPH:
output = vtkDirectedGraph::New();
break;
case VTK_MOLECULE:
case VTK_UNDIRECTED_GRAPH:
output = vtkUndirectedGraph::New();
break;
......@@ -199,6 +201,7 @@ int vtkGenericDataObjectReader::RequestInformation(
int retVal;
switch (this->ReadOutputType())
{
case VTK_MOLECULE:
case VTK_UNDIRECTED_GRAPH:
case VTK_DIRECTED_GRAPH:
reader = vtkGraphReader::New();
......@@ -263,6 +266,11 @@ int vtkGenericDataObjectReader::RequestData(
switch (this->ReadOutputType())
{
case VTK_MOLECULE:
{
this->ReadData<vtkGraphReader, vtkMolecule>("vtkMolecule", output);
return 1;
}
case VTK_DIRECTED_GRAPH:
{
this->ReadData<vtkGraphReader, vtkDirectedGraph>("vtkDirectedGraph", output);
......@@ -381,11 +389,15 @@ int vtkGenericDataObjectReader::ReadOutputType()
this->CloseVTKFile();
if(!strncmp(this->LowerCase(line), "directed_graph", 5))
if(!strncmp(this->LowerCase(line), "molecule", 8))
{
return VTK_MOLECULE;
}
if(!strncmp(this->LowerCase(line), "directed_graph", 14))
{
return VTK_DIRECTED_GRAPH;
}
if(!strncmp(this->LowerCase(line), "undirected_graph", 5))
if(!strncmp(this->LowerCase(line), "undirected_graph", 16))
{
return VTK_UNDIRECTED_GRAPH;
}
......@@ -459,6 +471,11 @@ vtkGraph *vtkGenericDataObjectReader::GetGraphOutput()
return vtkGraph::SafeDownCast(this->GetOutput());
}
vtkMolecule *vtkGenericDataObjectReader::GetMoleculeOutput()
{
return vtkMolecule::SafeDownCast(this->GetOutput());
}
vtkPolyData *vtkGenericDataObjectReader::GetPolyDataOutput()
{
return vtkPolyData::SafeDownCast(this->GetOutput());
......
......@@ -37,6 +37,7 @@
class vtkDataObject;
class vtkGraph;
class vtkMolecule;
class vtkPolyData;
class vtkRectilinearGrid;
class vtkStructuredGrid;
......@@ -64,6 +65,7 @@ public:
// returned. (You must also set the filename of the object prior to
// getting the output.)
vtkGraph *GetGraphOutput();
vtkMolecule *GetMoleculeOutput();
vtkPolyData *GetPolyDataOutput();
vtkRectilinearGrid *GetRectilinearGridOutput();
vtkStructuredGrid *GetStructuredGridOutput();
......
......@@ -79,6 +79,7 @@ void vtkGenericDataObjectWriter::WriteData()
return;
case VTK_DIRECTED_GRAPH:
case VTK_UNDIRECTED_GRAPH:
case VTK_MOLECULE:
writer = CreateWriter<vtkGraphWriter>(input);
break;
case VTK_HIERARCHICAL_DATA_SET:
......
......@@ -20,6 +20,7 @@
#include "vtkGraph.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMolecule.h"
#include "vtkMutableDirectedGraph.h"
#include "vtkMutableUndirectedGraph.h"
#include "vtkObjectFactory.h"
......@@ -36,9 +37,8 @@ vtkStandardNewMacro(vtkGraphReader);
vtkGraphReader::vtkGraphReader()
{
// We don't know the output type yet.
// It could be vtkDirectedGraph or vtkUndirectedGraph.
// It could be vtkDirectedGraph, vtkUndirectedGraph, or vtkMolecule.
// We will set it in RequestInformation().
}
//----------------------------------------------------------------------------
......@@ -103,8 +103,8 @@ int vtkGraphReader::RequestData(
vtkDebugMacro(<<"Reading vtk graph ...");
char line[256];
bool directed = true;
if (!this->ReadGraphDirectedness(directed))
GraphType graphType;
if (!this->ReadGraphType(graphType))
{
this->CloseVTKFile();
return 1;
......@@ -114,14 +114,23 @@ int vtkGraphReader::RequestData(
vtkSmartPointer<vtkMutableDirectedGraph>::New();
vtkSmartPointer<vtkMutableUndirectedGraph> undir_builder =
vtkSmartPointer<vtkMutableUndirectedGraph>::New();
vtkGraph *builder = 0;
if (directed)
{
builder = dir_builder;
}
else
switch (graphType)
{
builder = undir_builder;
case vtkGraphReader::DirectedGraph:
builder = dir_builder;
break;
case vtkGraphReader::UndirectedGraph:
case vtkGraphReader::Molecule: // Extends undirected graph.
builder = undir_builder;
break;
default:
vtkErrorMacro("ReadGraphType gave invalid result.");
this->CloseVTKFile();
return 1;
}
while(true)
......@@ -134,14 +143,21 @@ int vtkGraphReader::RequestData(
if(!strncmp(this->LowerCase(line), "field", 5))
{
vtkFieldData* const field_data = this->ReadFieldData();
if (directed)
{
dir_builder->SetFieldData(field_data);
}
else
switch (graphType)
{
undir_builder->SetFieldData(field_data);
case vtkGraphReader::DirectedGraph:
dir_builder->SetFieldData(field_data);
break;
case vtkGraphReader::UndirectedGraph:
case vtkGraphReader::Molecule:
undir_builder->SetFieldData(field_data);
break;
default: // Can't happen, would return earlier.
break;
}
field_data->Delete();
continue;
}
......@@ -171,13 +187,19 @@ int vtkGraphReader::RequestData(
}
for (vtkIdType v = 0; v < vertex_count; ++v)
{
if (directed)
{
dir_builder->AddVertex();
}
else
switch (graphType)
{
undir_builder->AddVertex();
case vtkGraphReader::DirectedGraph:
dir_builder->AddVertex();
break;
case vtkGraphReader::UndirectedGraph:
case vtkGraphReader::Molecule:
undir_builder->AddVertex();
break;
default: // Can't happen, would return earlier.
break;
}
}
continue;
......@@ -203,13 +225,19 @@ int vtkGraphReader::RequestData(
return 1;
}
if (directed)
switch (graphType)
{
dir_builder->AddEdge(source, target);
}
else
{
undir_builder->AddEdge(source, target);
case vtkGraphReader::DirectedGraph:
dir_builder->AddEdge(source, target);
break;
case vtkGraphReader::UndirectedGraph:
case vtkGraphReader::Molecule:
undir_builder->AddEdge(source, target);
break;
default: // Can't happen, would return earlier.
break;
}
}
continue;
......@@ -258,15 +286,9 @@ int vtkGraphReader::RequestData(
// Copy builder into output.
vtkGraph* const output = vtkGraph::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
bool valid = true;
if (directed)
{
valid = output->CheckedShallowCopy(dir_builder);
}
else
{
valid = output->CheckedShallowCopy(undir_builder);
}
valid = output->CheckedShallowCopy(builder);
if (!valid)
{
......@@ -277,8 +299,10 @@ int vtkGraphReader::RequestData(
}
//----------------------------------------------------------------------------
int vtkGraphReader::ReadGraphDirectedness(bool & directed)
int vtkGraphReader::ReadGraphType(GraphType &type)
{
type = UnknownGraph;
if(!this->OpenVTKFile() || !this->ReadHeader())
{
return 0;
......@@ -309,11 +333,15 @@ int vtkGraphReader::ReadGraphDirectedness(bool & directed)
if(!strncmp(this->LowerCase(line),"directed_graph", 14))
{
directed = true;
type = DirectedGraph;
}
else if(!strncmp(this->LowerCase(line), "undirected_graph", 16))
{
directed = false;
type = UndirectedGraph;
}
else if (!strncmp(this->LowerCase(line), "molecule", 8))
{
type = Molecule;
}
else
{
......@@ -336,8 +364,8 @@ int vtkGraphReader::RequestDataObject(vtkInformation *,
vtkInformationVector **,
vtkInformationVector *)
{
bool directed = true;
if (!this->ReadGraphDirectedness(directed))
GraphType graphType;
if (!this->ReadGraphType(graphType))
{
this->CloseVTKFile();
return 1;
......@@ -345,15 +373,27 @@ int vtkGraphReader::RequestDataObject(vtkInformation *,
this->CloseVTKFile();
vtkGraph *output = 0;
if (directed)
switch (graphType)
{
output = vtkDirectedGraph::New();
}
else
{
output = vtkUndirectedGraph::New();
case vtkGraphReader::DirectedGraph:
output = vtkDirectedGraph::New();
break;
case vtkGraphReader::UndirectedGraph:
output = vtkUndirectedGraph::New();
break;
case vtkGraphReader::Molecule:
output = vtkMolecule::New();
break;
default:
vtkErrorMacro("ReadGraphType returned invalid result.");
return 1;
}
this->SetOutput(output);
// Releasing data for pipeline parallism.
// Filters will know it is empty.
output->ReleaseData();
......
......@@ -50,6 +50,14 @@ protected:
vtkGraphReader();
~vtkGraphReader();
enum GraphType
{
UnknownGraph,
DirectedGraph,
UndirectedGraph,
Molecule
};
virtual int RequestData(vtkInformation *, vtkInformationVector **,
vtkInformationVector *);
......@@ -68,7 +76,7 @@ protected:
vtkInformationVector *);
// Read beginning of file to determine whether the graph is directed.
virtual int ReadGraphDirectedness(bool & directed);
virtual int ReadGraphType(GraphType &type);
virtual int FillOutputPortInformation(int, vtkInformation*);
......
......@@ -20,6 +20,7 @@
#include "vtkEdgeListIterator.h"
#include "vtkGraph.h"
#include "vtkInformation.h"
#include "vtkMolecule.h"
#include "vtkObjectFactory.h"
#if!defined(_WIN32) || defined(__CYGWIN__)
......@@ -57,7 +58,11 @@ void vtkGraphWriter::WriteData()
return;
}
if(vtkDirectedGraph::SafeDownCast(input))
if (vtkMolecule::SafeDownCast(input)) // molecule is most derived, test first
{
*fp << "DATASET MOLECULE\n";
}
else if (vtkDirectedGraph::SafeDownCast(input))
{
*fp << "DATASET DIRECTED_GRAPH\n";
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment