I doubt this whole thing will be of use to anyone unless they have weirdly specific needs.. but anyways..the thing I've been working on for three weeks..
this one takes input from multiple antenna files. If you could use the version that takes only one file as input for the antenna, feel free to email me.
try: paraview.simple
except: from paraview.simple import *
import time
#instructions for getting numpy to work with paraview
#to figure out what you need to append to sys.path, 1.run python in the terminal (not paraview python shell)
#2. import numpy 3. print numpy.__file__ 4. copy and paste that path into sys.path.append(), excluding the numpy directory itself
import sys
sys.path.append('/usr/lib64/python2.6/site-packages/')
try:
import numpy as np
except ImportError:
print "Error importing numpy, read script comment above import sys"
import math
paraview.simple._DisableFirstRenderCameraReset()
RenderView1 = GetRenderView()
#for python paraview documentation check out http://www.paraview.org/ParaView3/Doc/Nightly/www/py-doc/
#for calls to c++ objects/methods http://www.paraview.org/ParaQ/Doc/Nightly/html/annotated.html
#you can also call help() on objects in the python shell or use object.ListProperties()
#BEGIN GLOBAL VARIABLES
#BEGIN load VARIABLES
bodySourceFiles = ["C:\\Users\\Zach\\Desktop\\temp\\Bowtie_mov\\parVTK_vox.pvtu"] #["/pathto/sourceFile1","/pathto/sourceFile2"]
timeStepList = []
i = 1
while (i < 31): #convenient timestep name generator, modify to suit your needs
if (i < 10):
timeStepList.append("C:\\Users\\Zach\\Desktop\\temp\\antenna\\current_out_0" + `i` + ".inp")
else:
timeStepList.append("C:\\Users\\Zach\\Desktop\\temp\\antenna\\current_out_" + `i` + ".inp")
i += 1
antennaSourceFilesListList = [timeStepList]#if you want multiple antennas [list1, list2, list3,...] where list1 = [file1, file2,...]
bodyDataArraysTimesteps = [] #["E1","E2","E3","E4"] If left blank, script will try to fill automatically in autoLoadTimeSteps
antennaDataArraysTimesteps = [] #["E1","E2","E3","E4"], If left blank, script will try to fill automatically in autoLoadTimeSteps
backGroundColor = [1,1,1] #[red, green, blue]
#END load VARIABLES
#BEGIN output VARIABLES
#0,1,2,3 etc. will be appended to the output file names
outputPath = 'C:/Users/Zach/Desktop/'
outputFileName = "output"
outputFileType = ".png"
outputResolution = [4000,2000]#these are both minimum values for the resolution
#END output VARIABLES
#BEGIN camera VARIABLES
cameraCenterOfRotation = [0,0,0] #if you leave this as [0,0,0], the script will try to find the center for you
cameraFocalPoint = cameraCenterOfRotation #Center of rotation can also be thought of as the camera focal point
#spherical coordinate system determines camera position
r = 1.2 #fairly sensitive. 10 would be very far away. scales the camera's angle of view.
theta = 180 #0 to 360, angle that specifies location in x-y plane
phi = 90 #0 to 360, angle between z axis and x-y plane
viewingAngleRoll = 0 #0 to 360, "clockwise neck twist angle", 'twists' camera perspective, at 90 your head is resting on your right shoulder
#cameraPositions = []#[[r,theta,phi,lasttimestep],[r,theta,phi,lasttimestep]]
#so the camera moves from it's initial position to the next camera position
#but what happens when the move is greater than 180 degrees? how do we know which way to go?
#also want the capability to repeat the period/run through all the timesteps more than once per
#camera rotation.
cameraChangePerIteration = [0,12,0] #[r, theta, phi] angles theta and phi are in degrees.
#END camera VARIABLES
#BEGIN color VARIABLES
#Color range,[red,green,blue] change to jet by default
bodyNameOfPropertyToColor = "P" #sets the title of the color bar, just a label
antennaPropertyToColor = " Current" #NOT just a label, used to select the right data array to color the antenna.
#jet color scheme
lowestColor = [0.0, 0.0, .562]
lowLowColor = [0,0,1]
lowHighColor = [0,1,1]
middleColor = [0.5, 1.0, 0.5]
highLowColor = [1,1,0]
highHighColor = [1,0,0]
highestColor = [.5, 0.0, 0.0]
decibelRange = 30
highestBodyDecibelValue = None #if None, script finds the highest decibel value in the time series to color by
highestAntennaDecibelValue = None #if None, script finds the highest decibel value in the time series to color by
#END color VARIABLES
#BEGIN thresholdNClip VARIABLES
#Clips are made by choosing an origin point and a normal vector to specify a clipping plane
clipNormal1 = [1,0,0] #clip the plane normal to the x unit vector
clipNormal2 = [0,1,0] #clip the plane normal to the y unit vector
clipNormals = [clipNormal1, clipNormal2] #[[x,y,z][x,y,z], ...]feel free to add more clips or remove some from the list!
clipOrigins = [cameraCenterOfRotation,cameraCenterOfRotation] #[[x,y,z][x,y,z], ...] if set to cameraCenterOfRotation,
#script will try to set the clip origin at the center of the data.
materialIds = [84,92] #[materialIdToThreshold1, materialIdToThreshold2, etc. etc.]
#allow to set transparency?
#END thresholdNClip VARIABLES
#END GLOBAL VARIABLES
#NON USER DEFINED GLOBALS
#well, I mean you can define them if you want to...but you might mess stuff up.
bodySources = []
bodyDataRepresentations = []
antennaSources = []
antennaDataRepresentations = []
bodySourceRange = [0,0]
antennaSourceRange = [0,0]
thresholds = []
thresholdedDataRepresentations = []
clips = []
clippedDataRepresentations = []
RenderView1 = GetRenderView()
RenderView1.CenterAxesVisibility = 0
RenderView1.OrientationAxesVisibility = 0
RenderView1.Background = backGroundColor
#setting the resolution
magnification0 = outputResolution[0]/GetRenderView().ViewSize[0]
magnification1 = outputResolution[1]/GetRenderView().ViewSize[1]
finalMagnification = 1
if magnification0 > magnification1:
finalMagnification = magnification0
else:
finalMagnification = magnification1
coloringPieceWiseFunction = CreatePiecewiseFunction()
outputCount = 1
#END NON USER DEFINED GLOBALS
def autoLoadTimeSteps():
global bodyDataArraysTimesteps
if (not bodyDataArraysTimesteps): #if bodyDataArraysTimesteps is empty
for key in bodySources[0].GetCellDataInformation().keys():#we assume all body sources have same timesteps
if (key.lower() not in {'epsr', 'materialid', 'sigma','cellNormals'}):
bodyDataArraysTimesteps.append(key)
def load():
global bodySources
global bodyDataRepresentations
#BODIES
i = 0
for bodySourceFile in bodySourceFiles:
bodySources.append(OpenDataFile( bodySourceFile ))
bodyDataRepresentations.append( GetDisplayProperties( bodySources[i] ))
#GetDisplayProperties() = Show(), both return representation of file reader data
i += 1
Render()
def camera():
global cameraFocalPoint
if (cameraCenterOfRotation == [0,0,0]):
SetActiveSource(bodySources[0])
ResetCamera() #paraview sets the view to include the whole scene, "centering" the camera.
#This hack will change behaviour based on what the active source is.
#you probably want that to be the body.
cameraFocalPoint = RenderView1.CenterOfRotation
else:
cameraFocalPoint = cameraCenterOfRotation
RenderView1.CameraFocalPoint = cameraFocalPoint
RenderView1.CenterOfRotation = cameraFocalPoint
angleLR = math.radians(theta)#LR: left right
angleTD = math.radians(phi)#TD: top down
angleRoll = math.radians(viewingAngleRoll)
vectorFromCamToOrigin = [ 3 * math.cos(angleLR) * math.sin(angleTD),
3 * math.sin(angleLR) * math.sin(angleTD),
3 * math.cos(angleTD)]
#we need a vector orthogonal to the camera to focal point vector to orient ourselves by,
#this is that vector: the crossProductCamVector
#LEFT HAND COORDINATE SYSTEM VERSION
crossProductCamVector = np.array([-math.sin(angleLR), math.cos(angleLR), 0])
#RIGHT HAND VERSION
#crossProductCamVector = np.array([math.sin(angleLR), -math.cos(angleLR), 0])
camAndOriginVector = np.array(vectorFromCamToOrigin)
#rotating crossProductCamVector vector around camAndOriginVector according to angle roll
#using Rodrigues' rotation formula
rolledCrossProductCamVector = (crossProductCamVector * math.cos(angleRoll) +
np.cross(camAndOriginVector, crossProductCamVector) * math.sin(angleRoll) +
camAndOriginVector * np.dot(camAndOriginVector, crossProductCamVector) * (1 - math.cos(angleRoll)))
#take cross product of the vector formed by camera position and focal point
#and the vector 0,1,0, suitably transformed by angleLR, angleTD, and viewingAngleRoll
RenderView1.CameraViewUp = np.cross(camAndOriginVector, rolledCrossProductCamVector).tolist()
RenderView1.CameraPosition = [cameraFocalPoint[0] + vectorFromCamToOrigin[0],
cameraFocalPoint[1] + vectorFromCamToOrigin[1],
cameraFocalPoint[2] + vectorFromCamToOrigin[2]]
RenderView1.CameraViewAngle = r * 35
Render()
def colorThresholdNClip(bodyPropertyToColor):
global bodyDataRepresentations
global antennaDataRepresentations
global materialDataRepresentations
global clippedDataRepresentations
#Coloring
for bodyDataRepresentation in bodyDataRepresentations:
bodyDataRepresentation.ColorArrayName = ('CELL_DATA', bodyPropertyToColor)
#THRESHOLDING
i = 0
for id in materialIds:
thresholdedDataRepresentations[i].ColorArrayName = ('CELL_DATA', bodyPropertyToColor)
i += 1
#CLIPPING
i = 0
for clipNormal in clipNormals: #Need to loop over bodySources
clippedDataRepresentations[i].ColorArrayName = ('CELL_DATA', bodyPropertyToColor)
i += 1
Render()
def output():
WriteImage(outputPath + outputFileName + `outputCount` + outputFileType, view=RenderView1, Magnification=finalMagnification)
def main():
global bodySourceRange
global antennaSourceRange
global bodyColorLookupTable
global antennaColorLookupTable
global bodyDataRepresentations
global antennaDataRepresentations
global thresholds
global thresholdedDataRepresentations
global clips
global clippedDataRepresentations
global clipOrigins
global outputCount
global theta
global phi
global r
load()
autoLoadTimeSteps()
camera()
#finding the highest value for coloring
if (highestBodyDecibelValue is None):
for bodySource in bodySources:
for bodyPropertyToColor in bodyDataArraysTimesteps:
if (bodySourceRange[1] < bodySource.GetCellDataInformation()[bodyPropertyToColor].GetRange()[1]):
bodySourceRange[1] = bodySource.GetCellDataInformation()[bodyPropertyToColor].GetRange()[1]
else:
bodySourceRange[1] = highestBodyDecibelValue
if (highestAntennaDecibelValue is None):
if antennaSourceFilesListList: #if antennaSourceFiles is not empty
for antennaSourceFiles in antennaSourceFilesListList:#there must be an antenna, must have same fields as body
for antennaSourceFile in antennaSourceFiles:
antennaSources.append(OpenDataFile(antennaSourceFile))
if (antennaSourceRange[1] < antennaSources[0].GetCellDataInformation()[antennaPropertyToColor].GetRange()[1]):
antennaSourceRange[1] = antennaSources[0].GetCellDataInformation()[antennaPropertyToColor].GetRange()[1]
Delete(antennaSources[0])
antennaSources.pop()
else:
if antennaSourceFilesListList:
antennaSourceRange[1] = highestAntennaDecibelValue
#done finding the highest value for coloring
bodyColorLookupTable = GetLookupTableForArray( bodyDataArraysTimesteps[0], 1,
RGBPoints=[bodySourceRange[1] - decibelRange, lowestColor[0], lowestColor[1], lowestColor[2],
bodySourceRange[1] - 7 * decibelRange / 8, lowLowColor[0], lowLowColor[1], lowLowColor[2],
bodySourceRange[1] - 5 * decibelRange / 8, lowHighColor[0], lowHighColor[1], lowHighColor[2],
bodySourceRange[1] - decibelRange / 2, middleColor[0], middleColor[1], middleColor[2],
bodySourceRange[1] - 3 * decibelRange / 8, highLowColor[0], highLowColor[1], highLowColor[2],
bodySourceRange[1] - decibelRange / 8, highHighColor[0], highHighColor[1], highHighColor[2],
bodySourceRange[1], highestColor[0], highestColor[1], highestColor[2]], LockScalarRange=1)
bodyColorLookupTable.ScalarOpacityFunction = coloringPieceWiseFunction
bodyColorLookupTable.NanColor = [0,0,0] #if Not a Number is encountered, will color black.
if antennaSourceFilesListList:
antennaColorLookupTable = GetLookupTableForArray( antennaPropertyToColor, 1,
RGBPoints=[antennaSourceRange[1] - decibelRange, lowestColor[0], lowestColor[1], lowestColor[2],
antennaSourceRange[1] - 7 * decibelRange / 8, lowLowColor[0], lowLowColor[1], lowLowColor[2],
antennaSourceRange[1] - 5 * decibelRange / 8, lowHighColor[0], lowHighColor[1], lowHighColor[2],
antennaSourceRange[1] - decibelRange / 2, middleColor[0], middleColor[1], middleColor[2],
antennaSourceRange[1] - 3 * decibelRange / 8, highLowColor[0], highLowColor[1], highLowColor[2],
antennaSourceRange[1] - decibelRange / 8, highHighColor[0], highHighColor[1], highHighColor[2],
antennaSourceRange[1], highestColor[0], highestColor[1], highestColor[2]], LockScalarRange=1)
antennaColorLookupTable.ScalarOpacityFunction = coloringPieceWiseFunction
antennaColorLookupTable.NanColor = [0,0,0] #if Not a Number is encountered, will color black.
#SET COLOR TABLES FOR DATAREPRESENTATIONS
for bodyDataRepresentation in bodyDataRepresentations:
bodyDataRepresentation.LookupTable = bodyColorLookupTable
bodyDataRepresentation.ColorAttributeType = 'CELL_DATA'
bodyDataRepresentation.Visibility = 0
#SET CLIP ORIGINS
i = 0
for origin in clipOrigins:
if (origin == [0,0,0]):
clipOrigins[i] = cameraFocalPoint
i += 1
#SET THRESHOLDS AND CLIPS FOR DATAREPRESENTATIONS
for bodySource in bodySources:
i = 0
for id in materialIds: #given a body source, one table for all
thresholds.append(Threshold(bodySource))
thresholds[i].Scalars = ['CELLS', 'MaterialID']
thresholds[i].ThresholdRange = [id, id]
thresholdedDataRepresentations.append(Show(thresholds[i]))
thresholdedDataRepresentations[i].LookupTable = bodyColorLookupTable
i+= 1
i = 0
for clipNormal in clipNormals: #Need to loop over bodySources
clips.append(Clip(bodySource, Clip))
clips[i].ClipType.Normal = clipNormal
clips[i].ClipType.Origin = clipOrigins[i]
clippedDataRepresentations.append(Show(clips[i]))
clippedDataRepresentations[i].LookupTable = bodyColorLookupTable
i += 1
colorThresholdNClip(bodyDataArraysTimesteps[0])
#MAIN LOOP WHERE WE UPDATE THE CAMERA AND COLORING OF THE BODY AND THRESHOLDS AND CLIPS AND WRITE OUTPUT
if antennaSourceFilesListList:
for bodyStepProperty in bodyDataArraysTimesteps:
for antennaSourceFilesList in antennaSourceFilesListList:#for each list of antennas
antennaSources.append(OpenDataFile(antennaSourceFilesList[outputCount]))
antennaDataRepresentations.append(GetDisplayProperties(antennaSources[0]))
antennaDataRepresentations[0].LookupTable = antennaColorLookupTable
antennaDataRepresentations[0].ColorArrayName = ('CELL_DATA', antennaPropertyToColor)
colorThresholdNClip(bodyStepProperty)
camera() #have to call camera at every timestep.
output()
r += cameraChangePerIteration[0]
theta += cameraChangePerIteration[1]
phi += cameraChangePerIteration[2]
outputCount += 1
for antenna in antennaSources:
Delete(antenna)
antennaSources.pop()
antennaDataRepresentations.pop()
else:
for bodyStepProperty in bodyDataArraysTimesteps:
colorThresholdNClip(bodyStepProperty)
camera()
output()
r += cameraChangePerIteration[0]
theta += cameraChangePerIteration[1]
phi += cameraChangePerIteration[2]
outputCount += 1
#Create body color bar image file
bodyScalarBarRepresentation = CreateScalarBar( Title=bodyNameOfPropertyToColor, LabelFontSize=12, LookupTable=bodyColorLookupTable,
LabelColor=[0,0,0], Enabled=0, TitleFontSize=12, TitleColor=[0,0,0] )
GetRenderView().Representations.append(bodyScalarBarRepresentation)
Render()
WriteImage(outputPath + outputFileName + "BodyColorScale.png", Magnification=finalMagnification)
GetRenderView().Representations.remove(bodyScalarBarRepresentation)
if antennaSourceFilesListList:
#This should work fine for one antenna, or if all antennas have the same color scheme.
antennaScalarBarRepresentation = CreateScalarBar( Title=antennaPropertyToColor, LabelFontSize=12, LookupTable=antennaColorLookupTable,
LabelColor=[0,0,0], Enabled=0, TitleFontSize=12, TitleColor=[0,0,0] )
GetRenderView().Representations.append(antennaScalarBarRepresentation)
Render()
WriteImage(outputPath + outputFileName + "AntennaColorScale.png", Magnification=finalMagnification)
GetRenderView().Representations.remove(antennaScalarBarRepresentation)
main()
#LIST OF PROGRAM ASSUMPTIONS
#every input file has the same number of timesteps and they will be run concurrently
#another way of saying: the timesteps for any given body/antenna aren’t broken up between multiple files
#if there are multiple body files we assume they have the same timesteps e.g. E1,E2,E3, though
#the timestep properties may have different values between bodies (different bodies have different electric fields).
#we also assume there are multiple input files for the antennas.
#COMMENTS ON SCRIPT ORGANIZATION
#clips and thresholds are created in main, which then updates the coloring thru
#colorThresholdNClip and adjusts the camera through camera to produce a number of
#image files.
#also there's a hell of alot of global variables running around. Mostly they are updated in main,
#and then those updates are propagated/applied to objects of interest in the functions main calls,
#mainly camera and colorThresholdNClip
Copyright (c) 2014, Brian Zachary Abel
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
this one takes input from multiple antenna files. If you could use the version that takes only one file as input for the antenna, feel free to email me.
try: paraview.simple
except: from paraview.simple import *
import time
#instructions for getting numpy to work with paraview
#to figure out what you need to append to sys.path, 1.run python in the terminal (not paraview python shell)
#2. import numpy 3. print numpy.__file__ 4. copy and paste that path into sys.path.append(), excluding the numpy directory itself
import sys
sys.path.append('/usr/lib64/python2.6/site-packages/')
try:
import numpy as np
except ImportError:
print "Error importing numpy, read script comment above import sys"
import math
paraview.simple._DisableFirstRenderCameraReset()
RenderView1 = GetRenderView()
#for python paraview documentation check out http://www.paraview.org/ParaView3/Doc/Nightly/www/py-doc/
#for calls to c++ objects/methods http://www.paraview.org/ParaQ/Doc/Nightly/html/annotated.html
#you can also call help() on objects in the python shell or use object.ListProperties()
#BEGIN GLOBAL VARIABLES
#BEGIN load VARIABLES
bodySourceFiles = ["C:\\Users\\Zach\\Desktop\\temp\\Bowtie_mov\\parVTK_vox.pvtu"] #["/pathto/sourceFile1","/pathto/sourceFile2"]
timeStepList = []
i = 1
while (i < 31): #convenient timestep name generator, modify to suit your needs
if (i < 10):
timeStepList.append("C:\\Users\\Zach\\Desktop\\temp\\antenna\\current_out_0" + `i` + ".inp")
else:
timeStepList.append("C:\\Users\\Zach\\Desktop\\temp\\antenna\\current_out_" + `i` + ".inp")
i += 1
antennaSourceFilesListList = [timeStepList]#if you want multiple antennas [list1, list2, list3,...] where list1 = [file1, file2,...]
bodyDataArraysTimesteps = [] #["E1","E2","E3","E4"] If left blank, script will try to fill automatically in autoLoadTimeSteps
antennaDataArraysTimesteps = [] #["E1","E2","E3","E4"], If left blank, script will try to fill automatically in autoLoadTimeSteps
backGroundColor = [1,1,1] #[red, green, blue]
#END load VARIABLES
#BEGIN output VARIABLES
#0,1,2,3 etc. will be appended to the output file names
outputPath = 'C:/Users/Zach/Desktop/'
outputFileName = "output"
outputFileType = ".png"
outputResolution = [4000,2000]#these are both minimum values for the resolution
#END output VARIABLES
#BEGIN camera VARIABLES
cameraCenterOfRotation = [0,0,0] #if you leave this as [0,0,0], the script will try to find the center for you
cameraFocalPoint = cameraCenterOfRotation #Center of rotation can also be thought of as the camera focal point
#spherical coordinate system determines camera position
r = 1.2 #fairly sensitive. 10 would be very far away. scales the camera's angle of view.
theta = 180 #0 to 360, angle that specifies location in x-y plane
phi = 90 #0 to 360, angle between z axis and x-y plane
viewingAngleRoll = 0 #0 to 360, "clockwise neck twist angle", 'twists' camera perspective, at 90 your head is resting on your right shoulder
#cameraPositions = []#[[r,theta,phi,lasttimestep],[r,theta,phi,lasttimestep]]
#so the camera moves from it's initial position to the next camera position
#but what happens when the move is greater than 180 degrees? how do we know which way to go?
#also want the capability to repeat the period/run through all the timesteps more than once per
#camera rotation.
cameraChangePerIteration = [0,12,0] #[r, theta, phi] angles theta and phi are in degrees.
#END camera VARIABLES
#BEGIN color VARIABLES
#Color range,[red,green,blue] change to jet by default
bodyNameOfPropertyToColor = "P" #sets the title of the color bar, just a label
antennaPropertyToColor = " Current" #NOT just a label, used to select the right data array to color the antenna.
#jet color scheme
lowestColor = [0.0, 0.0, .562]
lowLowColor = [0,0,1]
lowHighColor = [0,1,1]
middleColor = [0.5, 1.0, 0.5]
highLowColor = [1,1,0]
highHighColor = [1,0,0]
highestColor = [.5, 0.0, 0.0]
decibelRange = 30
highestBodyDecibelValue = None #if None, script finds the highest decibel value in the time series to color by
highestAntennaDecibelValue = None #if None, script finds the highest decibel value in the time series to color by
#END color VARIABLES
#BEGIN thresholdNClip VARIABLES
#Clips are made by choosing an origin point and a normal vector to specify a clipping plane
clipNormal1 = [1,0,0] #clip the plane normal to the x unit vector
clipNormal2 = [0,1,0] #clip the plane normal to the y unit vector
clipNormals = [clipNormal1, clipNormal2] #[[x,y,z][x,y,z], ...]feel free to add more clips or remove some from the list!
clipOrigins = [cameraCenterOfRotation,cameraCenterOfRotation] #[[x,y,z][x,y,z], ...] if set to cameraCenterOfRotation,
#script will try to set the clip origin at the center of the data.
materialIds = [84,92] #[materialIdToThreshold1, materialIdToThreshold2, etc. etc.]
#allow to set transparency?
#END thresholdNClip VARIABLES
#END GLOBAL VARIABLES
#NON USER DEFINED GLOBALS
#well, I mean you can define them if you want to...but you might mess stuff up.
bodySources = []
bodyDataRepresentations = []
antennaSources = []
antennaDataRepresentations = []
bodySourceRange = [0,0]
antennaSourceRange = [0,0]
thresholds = []
thresholdedDataRepresentations = []
clips = []
clippedDataRepresentations = []
RenderView1 = GetRenderView()
RenderView1.CenterAxesVisibility = 0
RenderView1.OrientationAxesVisibility = 0
RenderView1.Background = backGroundColor
#setting the resolution
magnification0 = outputResolution[0]/GetRenderView().ViewSize[0]
magnification1 = outputResolution[1]/GetRenderView().ViewSize[1]
finalMagnification = 1
if magnification0 > magnification1:
finalMagnification = magnification0
else:
finalMagnification = magnification1
coloringPieceWiseFunction = CreatePiecewiseFunction()
outputCount = 1
#END NON USER DEFINED GLOBALS
def autoLoadTimeSteps():
global bodyDataArraysTimesteps
if (not bodyDataArraysTimesteps): #if bodyDataArraysTimesteps is empty
for key in bodySources[0].GetCellDataInformation().keys():#we assume all body sources have same timesteps
if (key.lower() not in {'epsr', 'materialid', 'sigma','cellNormals'}):
bodyDataArraysTimesteps.append(key)
def load():
global bodySources
global bodyDataRepresentations
#BODIES
i = 0
for bodySourceFile in bodySourceFiles:
bodySources.append(OpenDataFile( bodySourceFile ))
bodyDataRepresentations.append( GetDisplayProperties( bodySources[i] ))
#GetDisplayProperties() = Show(), both return representation of file reader data
i += 1
Render()
def camera():
global cameraFocalPoint
if (cameraCenterOfRotation == [0,0,0]):
SetActiveSource(bodySources[0])
ResetCamera() #paraview sets the view to include the whole scene, "centering" the camera.
#This hack will change behaviour based on what the active source is.
#you probably want that to be the body.
cameraFocalPoint = RenderView1.CenterOfRotation
else:
cameraFocalPoint = cameraCenterOfRotation
RenderView1.CameraFocalPoint = cameraFocalPoint
RenderView1.CenterOfRotation = cameraFocalPoint
angleLR = math.radians(theta)#LR: left right
angleTD = math.radians(phi)#TD: top down
angleRoll = math.radians(viewingAngleRoll)
vectorFromCamToOrigin = [ 3 * math.cos(angleLR) * math.sin(angleTD),
3 * math.sin(angleLR) * math.sin(angleTD),
3 * math.cos(angleTD)]
#we need a vector orthogonal to the camera to focal point vector to orient ourselves by,
#this is that vector: the crossProductCamVector
#LEFT HAND COORDINATE SYSTEM VERSION
crossProductCamVector = np.array([-math.sin(angleLR), math.cos(angleLR), 0])
#RIGHT HAND VERSION
#crossProductCamVector = np.array([math.sin(angleLR), -math.cos(angleLR), 0])
camAndOriginVector = np.array(vectorFromCamToOrigin)
#rotating crossProductCamVector vector around camAndOriginVector according to angle roll
#using Rodrigues' rotation formula
rolledCrossProductCamVector = (crossProductCamVector * math.cos(angleRoll) +
np.cross(camAndOriginVector, crossProductCamVector) * math.sin(angleRoll) +
camAndOriginVector * np.dot(camAndOriginVector, crossProductCamVector) * (1 - math.cos(angleRoll)))
#take cross product of the vector formed by camera position and focal point
#and the vector 0,1,0, suitably transformed by angleLR, angleTD, and viewingAngleRoll
RenderView1.CameraViewUp = np.cross(camAndOriginVector, rolledCrossProductCamVector).tolist()
RenderView1.CameraPosition = [cameraFocalPoint[0] + vectorFromCamToOrigin[0],
cameraFocalPoint[1] + vectorFromCamToOrigin[1],
cameraFocalPoint[2] + vectorFromCamToOrigin[2]]
RenderView1.CameraViewAngle = r * 35
Render()
def colorThresholdNClip(bodyPropertyToColor):
global bodyDataRepresentations
global antennaDataRepresentations
global materialDataRepresentations
global clippedDataRepresentations
#Coloring
for bodyDataRepresentation in bodyDataRepresentations:
bodyDataRepresentation.ColorArrayName = ('CELL_DATA', bodyPropertyToColor)
#THRESHOLDING
i = 0
for id in materialIds:
thresholdedDataRepresentations[i].ColorArrayName = ('CELL_DATA', bodyPropertyToColor)
i += 1
#CLIPPING
i = 0
for clipNormal in clipNormals: #Need to loop over bodySources
clippedDataRepresentations[i].ColorArrayName = ('CELL_DATA', bodyPropertyToColor)
i += 1
Render()
def output():
WriteImage(outputPath + outputFileName + `outputCount` + outputFileType, view=RenderView1, Magnification=finalMagnification)
def main():
global bodySourceRange
global antennaSourceRange
global bodyColorLookupTable
global antennaColorLookupTable
global bodyDataRepresentations
global antennaDataRepresentations
global thresholds
global thresholdedDataRepresentations
global clips
global clippedDataRepresentations
global clipOrigins
global outputCount
global theta
global phi
global r
load()
autoLoadTimeSteps()
camera()
#finding the highest value for coloring
if (highestBodyDecibelValue is None):
for bodySource in bodySources:
for bodyPropertyToColor in bodyDataArraysTimesteps:
if (bodySourceRange[1] < bodySource.GetCellDataInformation()[bodyPropertyToColor].GetRange()[1]):
bodySourceRange[1] = bodySource.GetCellDataInformation()[bodyPropertyToColor].GetRange()[1]
else:
bodySourceRange[1] = highestBodyDecibelValue
if (highestAntennaDecibelValue is None):
if antennaSourceFilesListList: #if antennaSourceFiles is not empty
for antennaSourceFiles in antennaSourceFilesListList:#there must be an antenna, must have same fields as body
for antennaSourceFile in antennaSourceFiles:
antennaSources.append(OpenDataFile(antennaSourceFile))
if (antennaSourceRange[1] < antennaSources[0].GetCellDataInformation()[antennaPropertyToColor].GetRange()[1]):
antennaSourceRange[1] = antennaSources[0].GetCellDataInformation()[antennaPropertyToColor].GetRange()[1]
Delete(antennaSources[0])
antennaSources.pop()
else:
if antennaSourceFilesListList:
antennaSourceRange[1] = highestAntennaDecibelValue
#done finding the highest value for coloring
bodyColorLookupTable = GetLookupTableForArray( bodyDataArraysTimesteps[0], 1,
RGBPoints=[bodySourceRange[1] - decibelRange, lowestColor[0], lowestColor[1], lowestColor[2],
bodySourceRange[1] - 7 * decibelRange / 8, lowLowColor[0], lowLowColor[1], lowLowColor[2],
bodySourceRange[1] - 5 * decibelRange / 8, lowHighColor[0], lowHighColor[1], lowHighColor[2],
bodySourceRange[1] - decibelRange / 2, middleColor[0], middleColor[1], middleColor[2],
bodySourceRange[1] - 3 * decibelRange / 8, highLowColor[0], highLowColor[1], highLowColor[2],
bodySourceRange[1] - decibelRange / 8, highHighColor[0], highHighColor[1], highHighColor[2],
bodySourceRange[1], highestColor[0], highestColor[1], highestColor[2]], LockScalarRange=1)
bodyColorLookupTable.ScalarOpacityFunction = coloringPieceWiseFunction
bodyColorLookupTable.NanColor = [0,0,0] #if Not a Number is encountered, will color black.
if antennaSourceFilesListList:
antennaColorLookupTable = GetLookupTableForArray( antennaPropertyToColor, 1,
RGBPoints=[antennaSourceRange[1] - decibelRange, lowestColor[0], lowestColor[1], lowestColor[2],
antennaSourceRange[1] - 7 * decibelRange / 8, lowLowColor[0], lowLowColor[1], lowLowColor[2],
antennaSourceRange[1] - 5 * decibelRange / 8, lowHighColor[0], lowHighColor[1], lowHighColor[2],
antennaSourceRange[1] - decibelRange / 2, middleColor[0], middleColor[1], middleColor[2],
antennaSourceRange[1] - 3 * decibelRange / 8, highLowColor[0], highLowColor[1], highLowColor[2],
antennaSourceRange[1] - decibelRange / 8, highHighColor[0], highHighColor[1], highHighColor[2],
antennaSourceRange[1], highestColor[0], highestColor[1], highestColor[2]], LockScalarRange=1)
antennaColorLookupTable.ScalarOpacityFunction = coloringPieceWiseFunction
antennaColorLookupTable.NanColor = [0,0,0] #if Not a Number is encountered, will color black.
#SET COLOR TABLES FOR DATAREPRESENTATIONS
for bodyDataRepresentation in bodyDataRepresentations:
bodyDataRepresentation.LookupTable = bodyColorLookupTable
bodyDataRepresentation.ColorAttributeType = 'CELL_DATA'
bodyDataRepresentation.Visibility = 0
#SET CLIP ORIGINS
i = 0
for origin in clipOrigins:
if (origin == [0,0,0]):
clipOrigins[i] = cameraFocalPoint
i += 1
#SET THRESHOLDS AND CLIPS FOR DATAREPRESENTATIONS
for bodySource in bodySources:
i = 0
for id in materialIds: #given a body source, one table for all
thresholds.append(Threshold(bodySource))
thresholds[i].Scalars = ['CELLS', 'MaterialID']
thresholds[i].ThresholdRange = [id, id]
thresholdedDataRepresentations.append(Show(thresholds[i]))
thresholdedDataRepresentations[i].LookupTable = bodyColorLookupTable
i+= 1
i = 0
for clipNormal in clipNormals: #Need to loop over bodySources
clips.append(Clip(bodySource, Clip))
clips[i].ClipType.Normal = clipNormal
clips[i].ClipType.Origin = clipOrigins[i]
clippedDataRepresentations.append(Show(clips[i]))
clippedDataRepresentations[i].LookupTable = bodyColorLookupTable
i += 1
colorThresholdNClip(bodyDataArraysTimesteps[0])
#MAIN LOOP WHERE WE UPDATE THE CAMERA AND COLORING OF THE BODY AND THRESHOLDS AND CLIPS AND WRITE OUTPUT
if antennaSourceFilesListList:
for bodyStepProperty in bodyDataArraysTimesteps:
for antennaSourceFilesList in antennaSourceFilesListList:#for each list of antennas
antennaSources.append(OpenDataFile(antennaSourceFilesList[outputCount]))
antennaDataRepresentations.append(GetDisplayProperties(antennaSources[0]))
antennaDataRepresentations[0].LookupTable = antennaColorLookupTable
antennaDataRepresentations[0].ColorArrayName = ('CELL_DATA', antennaPropertyToColor)
colorThresholdNClip(bodyStepProperty)
camera() #have to call camera at every timestep.
output()
r += cameraChangePerIteration[0]
theta += cameraChangePerIteration[1]
phi += cameraChangePerIteration[2]
outputCount += 1
for antenna in antennaSources:
Delete(antenna)
antennaSources.pop()
antennaDataRepresentations.pop()
else:
for bodyStepProperty in bodyDataArraysTimesteps:
colorThresholdNClip(bodyStepProperty)
camera()
output()
r += cameraChangePerIteration[0]
theta += cameraChangePerIteration[1]
phi += cameraChangePerIteration[2]
outputCount += 1
#Create body color bar image file
bodyScalarBarRepresentation = CreateScalarBar( Title=bodyNameOfPropertyToColor, LabelFontSize=12, LookupTable=bodyColorLookupTable,
LabelColor=[0,0,0], Enabled=0, TitleFontSize=12, TitleColor=[0,0,0] )
GetRenderView().Representations.append(bodyScalarBarRepresentation)
Render()
WriteImage(outputPath + outputFileName + "BodyColorScale.png", Magnification=finalMagnification)
GetRenderView().Representations.remove(bodyScalarBarRepresentation)
if antennaSourceFilesListList:
#This should work fine for one antenna, or if all antennas have the same color scheme.
antennaScalarBarRepresentation = CreateScalarBar( Title=antennaPropertyToColor, LabelFontSize=12, LookupTable=antennaColorLookupTable,
LabelColor=[0,0,0], Enabled=0, TitleFontSize=12, TitleColor=[0,0,0] )
GetRenderView().Representations.append(antennaScalarBarRepresentation)
Render()
WriteImage(outputPath + outputFileName + "AntennaColorScale.png", Magnification=finalMagnification)
GetRenderView().Representations.remove(antennaScalarBarRepresentation)
main()
#LIST OF PROGRAM ASSUMPTIONS
#every input file has the same number of timesteps and they will be run concurrently
#another way of saying: the timesteps for any given body/antenna aren’t broken up between multiple files
#if there are multiple body files we assume they have the same timesteps e.g. E1,E2,E3, though
#the timestep properties may have different values between bodies (different bodies have different electric fields).
#we also assume there are multiple input files for the antennas.
#COMMENTS ON SCRIPT ORGANIZATION
#clips and thresholds are created in main, which then updates the coloring thru
#colorThresholdNClip and adjusts the camera through camera to produce a number of
#image files.
#also there's a hell of alot of global variables running around. Mostly they are updated in main,
#and then those updates are propagated/applied to objects of interest in the functions main calls,
#mainly camera and colorThresholdNClip
Copyright (c) 2014, Brian Zachary Abel
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.