[vtkusers] VTK and molecules

Andrew J. Dolgert ajd27 at tc.cornell.edu
Thu Aug 3 15:00:56 EDT 2006


If you are a do-it-yourself sort, here's a bit of python code that might
get you in the right direction. There is some dross in the following
code to let the user dynamically update the pipeline when using wxVTK,
but you'll see the relevant VTK classes used. The first bit just shows
atoms. The second just shows bonds. - Drew


import math, os, sys
from vtkpython import *



sample_atoms = (('C', (0,0,0)), ('C', (0,0,1)),
                ('H', (.5,0,1)), ('HO', (.5,.5,1)),
                ('O', (0,.5,.5)), ('Z',(1,1,1)))

sample_bonds = (('S', (0, 1)), ('S', (0, 3)), ('D', (1,4)))
                
                
carbon_bond = {'radius' : .05, 'color' : (.2,.2,.2) }
unknown_bond = {'radius' : .02, 'color' : (1, 1, 1) }
bondsByType = { 'S' : carbon_bond, 'unknown' : unknown_bond }
            
bondResolution = {'theta' : 20, 'phi' : 20 }


class BondShow:
  def __init__(self):
    self.polyData = None
    self.typeToIdx = None
    self.CreatePipeline()
    self.ConfigureBonds(bondsByType)
    
  def GetActor(self):
    return self.tubeActor
  
  def AddBonds(self, atomPts, bonds):
    """Set atom data
    
    atomSet - [(type, (x, y, z))] where type is C, H, O, HO.
    
    """
    
    self.polyData.SetPoints(atomPts)
    self.polyData.SetLines(bonds["cellArray"])
    self.polyData.GetCellData().SetScalars(bonds["radii"])
    self.polyData.GetCellData().AddArray(bonds["color"])

    self.RegisterPolydataInPipeline(self.polyData)

      
      
  def CreateAtomPoints(self, atomSet):
    createPts = vtkPoints()
    createPts.SetDataTypeToFloat()
    for atom in atomSet:
      ptIdx = createPts.InsertNextPoint(atom[1])
    return createPts

    
    
  def CreateCellArray(self, bondSet):
    createRadii = vtkFloatArray()
    createColor = vtkIdTypeArray()
    createColor.SetName("tubecolor")
    createCellArray = vtkCellArray()
    for (bondType, bondLink) in bondSet:
      if self.typeToIdx.has_key(bondType):
        typeIdx = self.typeToIdx[bondType]
        radius = bondsByType[bondType]["radius"]
      else:
        typeIdx = self.typeToIdx['unknown']
        radius = bondsByType['unknown']["radius"]
      createColor.InsertNextValue(typeIdx)
      createRadii.InsertNextValue(radius)
      createCellArray.InsertNextCell(2)
      createCellArray.InsertCellPoint(bondLink[0])
      createCellArray.InsertCellPoint(bondLink[1])
    
    return { "cellArray" : createCellArray, "color" : createColor,
             "radii" : createRadii}
      
      
      
  def AddLookupTableColor(self, index, color):
    self.lut.SetTableValue(index, color[0], color[1], color[2], 1)

  def CreatePipeline(self):
    self.polyData = vtk.vtkPolyData()
    self.tubeFilter = vtk.vtkTubeFilter()
    self.lut = vtk.vtkLookupTable()
    self.glyphMapper = vtk.vtkPolyDataMapper()
    self.tubeActor = vtk.vtkActor()

  def RegisterPolydataInPipeline(self, newPolyData):
    self.tubeFilter.SetInput(newPolyData)
    if self.polyData and self.polyData is not newPolyData:
      del self.polyData
    self.polyData = newPolyData

  def ConfigureBonds(self, bondTypes):
    self.typeToIdx = {}
    bondIdx = 0
    for bondType in bondTypes.keys():
      self.typeToIdx[bondType] = bondIdx
      bondIdx = bondIdx + 1
    bondCnt = bondIdx
    
    #self.tubeFilter.SetVaryRadiusToVaryRadiusByScalar()
    self.tubeFilter.SetVaryRadius(0)
    self.tubeFilter.SetRadius(0.05)
    self.tubeFilter.CappingOff()
    self.tubeFilter.SetNumberOfSides(12)
    self.tubeFilter.SetGenerateTCoordsToOff()
    
    self.lut.SetNumberOfTableValues(bondCnt)
    for sourceType in bondTypes.keys():
      idx = self.typeToIdx[sourceType]
      bondColor = bondTypes[sourceType]["color"]
      self.AddLookupTableColor(idx, bondColor)
    
    self.glyphMapper.SetInput(self.tubeFilter.GetOutput())
    self.glyphMapper.SetLookupTable(self.lut)
    self.glyphMapper.SetScalarRange(0,bondCnt-1)
    
    self.tubeActor.SetMapper(self.glyphMapper)
    self.tubeActor.GetProperty().SetOpacity(1.0)
    self.tubeActor.GetProperty().SetInterpolationToGouraud()
    
    
if __name__ == "__main__":
  actorCreator = BondShow()
  actorCreator.AddBonds(sample_atoms, sample_bonds)
  actorCreator.AddBonds(sample_atoms, sample_bonds)
  actorCreator.GetActor()
  

And the next file, AtomShow.py


import math, os, sys
from vtkpython import *



sample_atoms = (('C', (0,0,0)), ('C', (0,0,1)),
                ('H', (.5,0,1)), ('HO', (.5,.5,1)),
                ('O', (0,.5,.5)), ('Z',(1,1,1)))

carbon_sphere = {'radius' : .15, 'color' : (.2,.2,.2) }
silicon_sphere = {'radius' : .2, 'color' : (1,.2,.2) }
dopant_sphere = {'radius' : .1, 'color' : (.2, 1, .1) }
unknown_sphere = {'radius' : .1, 'color' : (1, 1, 1) }
spheresByType = { 'C' : carbon_sphere, 'H' : silicon_sphere, 'O' :
dopant_sphere,
            'HO' : silicon_sphere, 'unknown' : unknown_sphere }
            
sphereResolution = {'theta' : 20, 'phi' : 20 }


class AtomShow:
  def __init__(self):
    self.polyData = None
    self.typeToIdx = None
    self.CreatePipeline()
    self.ConfigureSpheres(spheresByType)
    
  def __CreateSphere(self, sphereProps):
    sphere = vtk.vtkSphereSource()
    sphere.SetRadius(sphereProps["radius"])
    sphere.SetThetaResolution(sphereResolution["theta"])
    sphere.SetPhiResolution(sphereResolution["phi"])
    return sphere.GetOutput()
  
  def GetActor(self):
    return self.glyphActor
  
  
  def CreateAtomPolyData(self, atomSet):
    createPts = vtkPoints()
    createPts.SetDataTypeToFloat()
    createIndices = vtkIdTypeArray()
    createCellArray = vtkCellArray()
    
    for atom in atomSet:
      ptIdx = createPts.InsertNextPoint(atom[1])
      if self.typeToIdx.has_key(atom[0]):
        typeIdx = self.typeToIdx[atom[0]]
      else:
        typeIdx = self.typeToIdx['unknown']
      createIndices.InsertNextValue(typeIdx)
      createCellArray.InsertNextCell(1)
      createCellArray.InsertCellPoint(ptIdx)
      
    newPolyData = vtk.vtkPolyData()
    newPolyData.SetPoints(createPts)
    newPolyData.GetPointData().SetScalars(createIndices)
    newPolyData.SetVerts(createCellArray)

    return newPolyData  
  
  def AddAtoms(self, atomPolyData):
    self.RegisterPolydataInPipeline(atomPolyData)
  
  def AddLookupTableColor(self, index, color):
    self.lut.SetTableValue(index, color[0], color[1], color[2], 1)

  def CreatePipeline(self):
    self.glyphs = vtk.vtkGlyph3D()
    self.lut = vtk.vtkLookupTable()
    self.glyphMapper = vtk.vtkPolyDataMapper()
    self.glyphActor = vtk.vtkActor()

  def RegisterPolydataInPipeline(self, newPolyData):
    self.glyphs.SetInput(newPolyData)
    if self.polyData and self.polyData is not newPolyData:
      del self.polyData
    self.polyData = newPolyData

  def ConfigureSpheres(self, sphereTypes):
    self.typeToIdx = {}
    sphereIdx = 0
    for sphereType in sphereTypes.keys():
      self.typeToIdx[sphereType] = sphereIdx
      sphereIdx = sphereIdx + 1
    sphereCnt = sphereIdx
    
    for sourceType in sphereTypes.keys():
      self.glyphs.SetSource(self.typeToIdx[sourceType],
                      self.__CreateSphere(sphereTypes[sourceType]))
    self.glyphs.SetScaleModeToDataScalingOff()
    self.glyphs.SetIndexModeToScalar()
    self.glyphs.SetRange(0,sphereCnt-1)
    
    self.lut.SetNumberOfTableValues(sphereCnt)
    for sourceType in sphereTypes.keys():
      idx = self.typeToIdx[sourceType]
      sphereColor = sphereTypes[sourceType]["color"]
      self.AddLookupTableColor(idx, sphereColor)
    
    self.glyphMapper.SetInput(self.glyphs.GetOutput())
    self.glyphMapper.SetLookupTable(self.lut)
    self.glyphMapper.SetScalarRange(0,sphereCnt-1)
    
    self.glyphActor.SetMapper(self.glyphMapper)
    self.glyphActor.GetProperty().SetOpacity(1.0)
    self.glyphActor.GetProperty().SetInterpolationToGouraud()
    
    
    
if __name__ == "__main__":
  actorCreator = AtomShow()
  actorCreator.AddAtoms(sample_atoms)
  actorCreator.AddAtoms(sample_atoms)
  actorCreator.GetActor()
  

-----Original Message-----
From: vtkusers-bounces+ajd27=tc.cornell.edu at vtk.org
[mailto:vtkusers-bounces+ajd27=tc.cornell.edu at vtk.org] On Behalf Of
Vivek Dwivedi
Sent: Thursday, August 03, 2006 2:16 PM
To: VTK Users
Subject: [vtkusers] VTK and molecules

Hi all:

Do any of you have any sample code for generating ball and stick  
molecules or do you know of any tutorials out thier.


Thanks:

-Vivek


_______________________________________________
This is the private VTK discussion list. 
Please keep messages on-topic. Check the FAQ at:
http://www.vtk.org/Wiki/VTK_FAQ
Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers



More information about the vtkusers mailing list