Commit 2ca14734 authored by Jerry Clarke's avatar Jerry Clarke
Browse files

first addition

parent fd8f3626
#!/usr/bin/env python
#/*******************************************************************/
#/* XDMF */
#/* eXtensible Data Model and Format */
#/* */
#/* Id : Id */
#/* Date : $Date$ */
#/* Version : $Revision$ */
#/* */
#/* Author: */
#/* Jerry A. Clarke */
#/* clarke@arl.army.mil */
#/* US Army Research Laboratory */
#/* Aberdeen Proving Ground, MD */
#/* */
#/* Copyright @ 2002 US Army Research Laboratory */
#/* All Rights Reserved */
#/* See Copyright.txt or http://www.arl.hpc.mil/ice 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. */
#/* */
#/*******************************************************************/
import getopt
import sys
import string
print 'Loading vtk...'
from libVTKCommonPython import *
from libVTKGraphicsPython import *
from libVTKParallelPython import *
from libVTKPatentedPython import *
from libVTKImagingPython import *
from libVTKLocalPython import *
print 'Loading Xdmf...'
import Xdmf
class FromFacet :
def __init__( self ):
self.Thickness = .5
self.Frequency = 1.0 # in Ghz
self.CellWidth = .015 # l for 1Ghz / 20
self.IsTriangle = 0
self.Flip = 0
self.Verbose = 0
self.Adjust = 1
def SetThickness( self, Thickness ) :
self.Thickness = Thickness
def GetThickness( self ) :
return self.Thickness
def SetCellWidth( self, CellWidth ) :
self.CellWidth = CellWidth
def GetCellWidth( self ) :
return self.CellWidth
def SetFrequency( self, Frequency ) :
self.Frequency = Frequency
WaveLength = 0.3 / float(Frequency) # l = 3x10^8 / hz
self.SetCellWidth( WaveLength / 20.0 ) # 20 cells / l
print 'Cell Width for ' + str( Frequency ) + 'Ghz = ' + str( self.GetCellWidth() )
def GetFrequency( self ) :
return self.Frequency
def SetFlip( self, Flip ) :
self.Flip = Flip
def GetFlip( self ) :
return self.Flip
def CheckCellWidth( self ) :
ideal = self.Thickness / 4.0
if self.CellWidth > ideal :
print 'CellWidth Too Big'
print 'Was ' + str( self.CellWidth )
if self.Adjust == 1 :
print 'Setting to ' + str(ideal)
self.CellWidth = ideal
else :
print 'Suggest ' + str(ideal) + ' but --noadjust was set'
def Parse( self, FileName ) :
self.CheckCellWidth()
print 'Parsing ' + FileName
self.DOM = Xdmf.XdmfDOM()
self.DOM.SetInputFileName( FileName )
self.DOM.Parse()
node = self.DOM.FindElement("Grid")
if node == None :
print 'XML file has no <Grid> Element'
return None
top = self.DOM.FindElement("Topology", 0, node )
type = self.DOM.Get( top, "Type" )
print 'Grid Topology = ' + type
if type == 'Triangle' :
self.IsTriangle = 1
def Execute( self ) :
Ren = vtkRenderer()
self.Ren = Ren
Index = 0
Thickness = self.Thickness
node = self.DOM.FindElement("Grid", Index)
if node != None :
print 'Reading Heavy Data'
Grid = Xdmf.XdmfGrid()
Grid.SetDOM( self.DOM )
Grid.SetGridFromElement( node )
# Grid.AssignAttribute( 0 )
# CurrentAttribute = Grid.GetAssignedAttribute()
# ValueArray = CurrentAttribute.GetValues()
# ValueArray.Generate(Thickness, Thickness)
# CurrentAttribute.SetBaseAttribute( Grid, Grid.GetBaseGrid() )
VtkGrid = vtkDataSet( Grid.GetBaseGrid() )
Scalars = vtkScalars()
Scalars.SetNumberOfScalars( VtkGrid.GetNumberOfPoints() )
for i in range( VtkGrid.GetNumberOfPoints() ) :
Scalars.SetScalar( i, self.Thickness )
VtkGrid.GetPointData().SetScalars( Scalars )
print 'Calculating Normals'
GeometryFilter = vtkGeometryFilter()
GeometryFilter.SetInput( VtkGrid )
GeometryFilter.Update()
Normals = vtkPolyDataNormals()
if self.IsTriangle == 0 :
Tri = vtkTriangleFilter()
Tri.SetInput( GeometryFilter.GetOutput() )
Tri.Update()
Normals.SetInput( Tri.GetOutput() )
else :
Normals.SetInput( GeometryFilter.GetOutput() )
Normals.SplittingOn()
Normals.SetFeatureAngle(0)
Normals.FlipNormalsOff()
Normals.Update()
print 'Building Wedges'
warp = vtkWarpScalar()
warp.SetInput( Normals.GetOutput() )
if self.Flip == 0 :
warp.SetScaleFactor( 1.0 )
else :
warp.SetScaleFactor( -1.0 )
warp.Update()
if self.IsTriangle == 0 :
Wedges = self.BuildWedges( Tri.GetOutput(), warp.GetOutput() )
else :
Wedges = self.BuildWedges( GeometryFilter.GetOutput(), warp.GetOutput() )
Bounds = Wedges.GetBounds()
xmin = Bounds[0]
xmax = Bounds[1]
ymin = Bounds[2]
ymax = Bounds[3]
zmin = Bounds[4]
zmax = Bounds[5]
CellWidth = self.CellWidth
# Add Two Cells Around the Edges
# Grid sizw is 1 + Cell Size
idim = 5 + int(( xmax - xmin ) / CellWidth )
jdim = 5 + int(( ymax - ymin ) / CellWidth )
kdim = 5 + int(( zmax - zmin ) / CellWidth )
print 'X : ' + str( idim ) + ' Cells, ' + str( xmin ) + ' -> ' + str( xmax )
print 'Y : ' + str( jdim ) + ' Cells, ' + str( ymin ) + ' -> ' + str( ymax )
print 'Z : ' + str( kdim ) + ' Cells, ' + str( zmin ) + ' -> ' + str( zmax )
print 'Creating Structured Grid'
self.Modeller = vtkImplicitModeller()
self.Modeller.SetSampleDimensions( idim, jdim, kdim )
self.Modeller.SetModelBounds(xmin, xmax, ymin, ymax, zmin, zmax )
self.Modeller.SetInput( Wedges )
self.Modeller.SetProcessModeToPerVoxel()
self.Modeller.SetNumberOfThreads( 10 )
self.Modeller.SetMaximumDistance( 0.001 )
self.Modeller.SetProgressMethod( self.PercentDone )
self.Modeller.Update()
print 'Convert Node Data To Cell Data'
PointToCell = vtkPointDataToCellData()
PointToCell.SetInput( self.Modeller.GetOutput() )
PointToCell.Update()
ScalarRange = PointToCell.GetOutput().GetScalarRange()
print 'Scalar Range = ' + str( ScalarRange )
print 'Writing Data'
Writer = vtkXdmfDataSetWriter()
Writer.SetInput( PointToCell.GetOutput() )
Writer.SetHeavyDataSetName('Extrude.h5')
Writer.WriteGrid()
Writer.WriteAttributes()
XML = Writer.GetXML()
fd = open('Extrude.xml', "w" )
fd.write( XML )
fd.close()
def Show( self ) :
print 'Thresholding'
SgridThresh = vtkThreshold()
SgridThresh.SetInput( self.Modeller.GetOutput() )
SgridThresh.ThresholdByLower( 1.0 )
# SgridThresh.SetAttributeModeToUseCellData()
SgridThresh.SetProgressMethod( self.PercentDone )
SgridGeo = vtkGeometryFilter()
SgridGeo.SetInput( SgridThresh.GetOutput() )
x = vtkStructuredPointsGeometryFilter()
x.SetInput( self.Modeller.GetOutput() )
x.SetExtent( 0, 10000, 0, 10000, 0, 10000 )
SgridMapper = vtkPolyDataMapper()
SgridMapper.SetInput( SgridGeo.GetOutput() )
self.SgridActor = vtkActor()
self.SgridActor.SetMapper( SgridMapper )
self.SgridActor.GetProperty().SetPointSize( 4 )
self.Ren = vtkRenderer()
self.Ren.AddActor( self.SgridActor )
RenWin = vtkRenderWindow()
RenWin.AddRenderer(self.Ren)
Style = vtkInteractorStyleTrackball()
Style.SetTrackballModeToTrackball()
iRen = vtkRenderWindowInteractor()
iRen.SetInteractorStyle( Style )
iRen.SetRenderWindow(RenWin)
RenWin.SetSize(500,500)
self.Ren.SetBackground(.6, .6, .6 )
iRen.Initialize()
iRen.Start()
def PercentDone( self ) :
pd = self.Modeller.GetProgress()
if self.Verbose == 1 :
print 'Filter ' + str( int( pd * 100.0 ) ) + "% Done"
def BuildWedges( self, OrigTri, WarpTri ) :
NumberOfPolys = OrigTri.GetNumberOfPolys()
NumberOfPoints = OrigTri.GetNumberOfPoints()
Append = vtkAppendPolyData()
Append.AddInput( OrigTri )
Append.AddInput( WarpTri )
Append.Update()
NewGrid = vtkUnstructuredGrid()
NewGrid.SetPoints( Append.GetOutput().GetPoints())
NewWedge = vtkWedge()
Cntr = 0
Total = 0
for i in range( NumberOfPolys ) :
old = OrigTri.GetCell( i )
new = WarpTri.GetCell( i )
NewWedge.GetPointIds().SetId(0, old.GetPointId(0) )
NewWedge.GetPointIds().SetId(1, old.GetPointId(1) )
NewWedge.GetPointIds().SetId(2, old.GetPointId(2) )
NewWedge.GetPointIds().SetId(3, NumberOfPoints + new.GetPointId(0) )
NewWedge.GetPointIds().SetId(4, NumberOfPoints + new.GetPointId(1) )
NewWedge.GetPointIds().SetId(5, NumberOfPoints + new.GetPointId(2) )
NewGrid.InsertNextCell( NewWedge.GetCellType(), NewWedge.GetPointIds())
if Cntr >= 10000 :
Percent = int(100.0 * float(Total) / float(NumberOfPolys))
print str(Percent) + '% Completed ' + str( Total ) + ' of ' + str( NumberOfPolys) +' Cells'
Cntr = 0
Total += 1
Cntr += 1
NewGrid.GetPointData().SetScalars( Append.GetOutput().GetPointData().GetScalars() )
return NewGrid
def usage( self ) :
print 'Usage : ' + sys.argv[0] + ' [--noadjust --thickness=t --reverse --frequency=f --verbose] Input.xml'
def main( self ) :
argc = len( sys.argv )
try :
opts, args = getopt.getopt(sys.argv[1:],
"hotrfvn:",
["help", "output=", "thickness=",
"noadjust", "reverse",
"frequency=", "verbose" ])
except getopt.GetoptError:
self.usage()
sys.exit(2)
output = None
for o, a in opts:
if o in ("-h", "--help"):
self.usage()
sys.exit()
if o in ("-t", "--thickness"):
self.SetThickness( float(a) )
if o in ("-r", "--reverse"):
self.Flip = 1
if o in ("-n", "--noadjust"):
self.adjust = 0
if o in ("-f", "--frequency"):
self.SetFrequency( float(a) )
if o in ("-v", "--verbose"):
self.Verbose = 1
self.Parse( sys.argv[ argc - 1 ] )
gridder.Execute()
if __name__ == '__main__' :
gridder = FromFacet()
gridder.main()
gridder.Show()
#!/usr/bin/env python
#/*******************************************************************/
#/* XDMF */
#/* eXtensible Data Model and Format */
#/* */
#/* Id : Id */
#/* Date : $Date$ */
#/* Version : $Revision$ */
#/* */
#/* Author: */
#/* Jerry A. Clarke */
#/* clarke@arl.army.mil */
#/* US Army Research Laboratory */
#/* Aberdeen Proving Ground, MD */
#/* */
#/* Copyright @ 2002 US Army Research Laboratory */
#/* All Rights Reserved */
#/* See Copyright.txt or http://www.arl.hpc.mil/ice 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. */
#/* */
#/*******************************************************************/
import sys
import string
print 'Loading vtk'
from libVTKCommonPython import *
from libVTKGraphicsPython import *
from libVTKImagingPython import *
from libVTKPatentedPython import *
from libVTKContribPython import *
from libVTKLocalPython import *
print 'Loading Xdmf'
import Xdmf
class FromOBJ :
def __init__( self, FileName ) :
self.Convert = 0
self.FileName = FileName
BaseList = string.split(FileName, '.')
if len( BaseList ) == 1 :
self.BaseName = BaseList[0]
else :
self.BaseName = string.join( BaseList[ : len( BaseList ) - 1 ] )
def CreateXdmf( self ) :
ObjReader = vtkOBJReader()
ObjReader.SetFileName( self.FileName )
print 'Reading ' + self.FileName
ObjReader.Update()
TriFilter = vtkTriangleFilter()
TriFilter.SetInput( ObjReader.GetOutput() )
TriFilter.Update()
if self.Convert == 1 :
Points = TriFilter.GetOutput().GetPoints()
print 'Converting %d Points' % Points.GetNumberOfPoints()
for i in range ( Points.GetNumberOfPoints() ) :
x, y, z = Points.GetPoint( i )
x = x * .0254
y = y * .0254
z = z * .0254
Points.SetPoint( i, x, y, z )
Merge = vtkCleanPolyData()
Merge.SetTolerance(0)
Merge.SetInput( TriFilter.GetOutput() )
Normal = vtkPolyDataNormals()
Normal.SetInput( Merge.GetOutput() )
Normal.SetFeatureAngle(0)
Normal.SplittingOff()
Normal.ConsistencyOn()
# Normal.DebugOn()
Normal.Update()
Writer = vtkXdmfDataSetWriter()
Writer.SetInput( Normal.GetOutput() )
Writer.SetHeavyDataSetName(self.BaseName + '.h5')
Writer.WriteGrid()
Writer.WriteAttributes()
XML = Writer.GetXML()
fd = open(self.BaseName + '.xml', "w" )
fd.write("""<?xml version="1.0" ?>\n""")
fd.write("""<!DOCTYPE Xdmf SYSTEM "Xdmf.dtd" [\n""")
# fd.write("""<!ENTITY HeavyData "Zsu.h5">\n""")
fd.write('<!ENTITY HeavyData "' + self.BaseName + '.h5">\n')
fd.write("""]>\n""" )
fd.write("""<Domain>\n<Grid>\n""")
fd.write( XML )
fd.write("""</Grid>\n</Domain>\n""")
fd.close()
if __name__ == '__main__' :
argc = len( sys.argv )
FileName = sys.argv[ argc - 1 ]
fobj = FromOBJ( FileName )
if argc > 2 :
print 'Converting From inches'
fobj.Convert = 1
fobj.CreateXdmf()
#!/usr/bin/env python
#/*******************************************************************/
#/* XDMF */
#/* eXtensible Data Model and Format */
#/* */
#/* Id : Id */
#/* Date : $Date$ */
#/* Version : $Revision$ */
#/* */
#/* Author: */
#/* Jerry A. Clarke */
#/* clarke@arl.army.mil */
#/* US Army Research Laboratory */
#/* Aberdeen Proving Ground, MD */
#/* */
#/* Copyright @ 2002 US Army Research Laboratory */
#/* All Rights Reserved */
#/* See Copyright.txt or http://www.arl.hpc.mil/ice 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. */
#/* */
#/*******************************************************************/
"""PLY Format Converter"""
import string
import time
import colorsys
from IceParser import *
from Xdmf import *
class PlyParser( IceParser ) :
def __init__( self, Filename='Data.ply' ) :
self.DoConvert = 0
IceParser.__init__( self, Filename )
def ReadElements( self ) :
self.GetNextLine()
self.NodesPerElement = int(self.CurrentWords[0])
self.ElementArray = XdmfArray()
self.ElementArray.SetNumberType( XDMF_INT32_TYPE )
self.ElementArray.SetShapeFromString( self.Elements + ' ' + str(self.NodesPerElement) )
self.ElementArray.SetValues( 0, string.join( self.CurrentWords[1:] ))
Index = self.NodesPerElement
Cntr = 0
# One has already been read
NumberOfElements = int( self.Elements ) - 1
while NumberOfElements > 0 :
self.GetNextLine()
self.ElementArray.SetValues( Index, string.join( self.CurrentWords[1:] ))
if Cntr >= 5000 :
print 'Read ' + str( Index / self.NodesPerElement ) + ' Elements'
Cntr = 0
Index += self.NodesPerElement
Cntr += 1
NumberOfElements -= 1
def ReadNodes ( self ) :
NumberOfNodes = int( self.Nodes )
NumberOfElements = int( self.Elements )
self.NodeArray = XdmfArray()
self.NodeArray.SetNumberType( XDMF_FLOAT32_TYPE )
self.NodeArray.SetShapeFromString( self.Nodes + ' 3' )
self.ExtraArray = None
self.FakeColorArray = None
Extra = len( self.Props[3:] )
if Extra > 0 :
# self.ExtraArray = XdmfArray()
# self.ExtraArray.SetNumberType( XDMF_FLOAT32_TYPE )
# self.ExtraArray.SetShapeFromString( self.Nodes + ' ' + str( Extra ) )
if self.DoConvert == 1 :
self.FakeColorArray = XdmfArray()
self.FakeColorArray.SetNumberType( XDMF_FLOAT32_TYPE )
self.FakeColorArray.SetShapeFromString( self.Nodes )
NodeIndex = 0
ExtraIndex = 0
FakeColorIndex = 0
Cntr = 0
Length = len( self.Props ) - 3
while NumberOfNodes > 0 :
self.GetNextLine()
self.NodeArray.SetValues( NodeIndex, string.join( self.CurrentWords[:3] ))
if Extra > 0 :
# self.ExtraArray.SetValues( ExtraIndex, string.join( self.CurrentWords[3:]))
if self.DoConvert == 1 :
r, g, b = self.CurrentWords[-3:]
# RRRR GGGG BBBB
# red = ( int( r ) & 0xE0 )
# green = ( int( g ) & 0xE0 ) >> 3
# blue = ( int( b ) & 0xC0 ) >> 6
red = ( int( r ) & 0xF0 ) << 4
green = ( int( g ) & 0xF0 )
blue = ( int( b ) & 0xF0 ) >> 4
FakeColor = red | green | blue
self.FakeColorArray.SetValueFromInt64( FakeColorIndex, FakeColor )
FakeColorIndex += 1
if Cntr >= 5000 :
print 'Read ' + str( NodeIndex / 3 ) + ' Nodes'
Cntr = 0
ExtraIndex += Length
NodeIndex += 3
Cntr += 1
NumberOfNodes -= 1;
def ReadHeader( self ) :
Control = ''
self.Props = []
while ( Control != 'end_header' ) and ( self.GetNextLine() > 0 ):
Control = self.CurrentWords[0]
print 'Control = ' + Control
if Control == 'format' :
self.Format = self.CurrentWords[ 1 ]
print 'Format = ' + self.Format
elif Control == 'element' :
Type = self.CurrentWords[ 1 ]
if Type == 'vertex' :
self.Nodes = self.CurrentWords[ 2 ]
elif Type == 'face' :
self.Elements = self.CurrentWords[ 2 ]
else :
pass
elif Control == 'property' :
Prop = self.CurrentWords[ 1 ]
if Prop == 'list' :
pass
else :
if self.CurrentWords[ 2 ] == 'red' :
self.DoConvert = 1
self.Props.append( self.CurrentWords[ 2 ] )
else :
pass
def Parse ( self ) :
print 'Reading Header'
self.ReadHeader()
print 'Format ' + self.Format
print 'Nodes ' + self.Nodes
print 'Elements ' + self.Elements
print 'Scalars ' + str(self.Props)
self.ReadNodes()
self.ReadElements()
h5 = XdmfHDF();
DataSet = self.BaseName + '.h5:/Geometry/XYZ'
print 'Writing ' + DataSet
if h5.Open( DataSet, "rw" ) == XDMF_FAIL :
h5.CopyShape( self.NodeArray )
h5.CopyType( self.NodeArray )
h5.CreateDataset( DataSet )
h5.Write( self.NodeArray )
h5.Close()
if self.FakeColorArray != None :
h5 = XdmfHDF();
DataSet = self.BaseName + '.h5:/FakeColor'
print 'Writing ' + DataSet
if h5.Open( DataSet, "rw" ) == XDMF_FAIL :
h5.CopyShape( self.FakeColorArray )
h5.CopyType( self.FakeColorArray )
h5.CreateDataset( DataSet )
h5.Write( self.FakeColorArray )
h5.Close()
if self.ExtraArray != None :
h5 = XdmfHDF();
DataSet = self.BaseName + '.h5:/Scalars'
print 'Writing ' + DataSet
if h5.Open( DataSet, "rw" ) == XDMF_FAIL :
h5.CopyShape( self.ExtraArray )
h5.CopyType( self.ExtraArray )
h5.CreateDataset( DataSet )
h5.Write( self.ExtraArray )
h5.Close()
DataSet = self.BaseName + '.h5:/Connections'