data_writer.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import os
  2. from paraview import simple
  3. from vtkmodules.vtkIOImage import vtkPNGReader
  4. from vtkmodules.vtkCommonCore import vtkFloatArray, vtkUnsignedCharArray
  5. VTK_DATA_TYPES = [
  6. "void", # 0
  7. "bit", # 1
  8. "char", # 2
  9. "unsigned_char", # 3
  10. "short", # 4
  11. "unsigned_short", # 5
  12. "int", # 6
  13. "unsigned_int", # 7
  14. "long", # 8
  15. "unsigned_long", # 9
  16. "float", # 10
  17. "double", # 11
  18. "id_type", # 12
  19. "unspecified", # 13
  20. "unspecified", # 14
  21. "signed_char",
  22. ]
  23. # -----------------------------------------------------------------------------
  24. # Scalar Value to File
  25. # -----------------------------------------------------------------------------
  26. class ScalarRenderer(object):
  27. def __init__(self, isWriter=True, removePNG=True):
  28. self.view = simple.CreateView("RenderView")
  29. self.view.Background = [0.0, 0.0, 0.0]
  30. self.view.CenterAxesVisibility = 0
  31. self.view.OrientationAxesVisibility = 0
  32. self.reader = vtkPNGReader()
  33. self.cleanAfterMe = removePNG
  34. self.canWrite = isWriter
  35. def getView(self):
  36. return self.view
  37. def writeLightArray(self, path, source):
  38. rep = simple.Show(source, self.view)
  39. rep.Representation = "Surface"
  40. rep.DiffuseColor = [1, 1, 1]
  41. simple.ColorBy(rep, ("POINTS", None))
  42. # Grab data
  43. tmpFileName = path + "__.png"
  44. self.view.LockBounds = 1
  45. simple.SaveScreenshot(tmpFileName, self.view)
  46. self.view.LockBounds = 0
  47. if self.canWrite:
  48. # Convert data
  49. self.reader.SetFileName(tmpFileName)
  50. self.reader.Update()
  51. rgbArray = self.reader.GetOutput().GetPointData().GetArray(0)
  52. arraySize = rgbArray.GetNumberOfTuples()
  53. rawArray = vtkUnsignedCharArray()
  54. rawArray.SetNumberOfTuples(arraySize)
  55. for idx in range(arraySize):
  56. light = rgbArray.GetTuple3(idx)[0]
  57. rawArray.SetTuple1(idx, light)
  58. with open(path, "wb") as f:
  59. f.write(memoryview(rawArray))
  60. # Delete temporary file
  61. if self.cleanAfterMe:
  62. os.remove(tmpFileName)
  63. simple.Hide(source, self.view)
  64. def writeMeshArray(self, path, source):
  65. rep = simple.Show(source, self.view)
  66. rep.Representation = "Surface With Edges"
  67. rep.DiffuseColor = [0, 0, 0]
  68. rep.EdgeColor = [1.0, 1.0, 1.0]
  69. simple.ColorBy(rep, ("POINTS", None))
  70. # Grab data
  71. tmpFileName = path + "__.png"
  72. self.view.LockBounds = 1
  73. simple.SaveScreenshot(tmpFileName, self.view)
  74. self.view.LockBounds = 0
  75. if self.canWrite:
  76. # Convert data
  77. self.reader.SetFileName(tmpFileName)
  78. self.reader.Update()
  79. rgbArray = self.reader.GetOutput().GetPointData().GetArray(0)
  80. arraySize = rgbArray.GetNumberOfTuples()
  81. rawArray = vtkUnsignedCharArray()
  82. rawArray.SetNumberOfTuples(arraySize)
  83. for idx in range(arraySize):
  84. light = rgbArray.GetTuple3(idx)[0]
  85. rawArray.SetTuple1(idx, light)
  86. with open(path, "wb") as f:
  87. f.write(memoryview(rawArray))
  88. # Delete temporary file
  89. if self.cleanAfterMe:
  90. os.remove(tmpFileName)
  91. simple.Hide(source, self.view)
  92. def writeArray(self, path, source, name, component=0):
  93. rep = simple.Show(source, self.view)
  94. rep.Representation = "Surface"
  95. rep.DiffuseColor = [1, 1, 1]
  96. dataRange = [0.0, 1.0]
  97. fieldToColorBy = ["POINTS", name]
  98. self.view.ArrayNameToDraw = name
  99. self.view.ArrayComponentToDraw = component
  100. pdi = source.GetPointDataInformation()
  101. cdi = source.GetCellDataInformation()
  102. if pdi.GetArray(name):
  103. self.view.DrawCells = 0
  104. dataRange = pdi.GetArray(name).GetRange(component)
  105. fieldToColorBy[0] = "POINTS"
  106. elif cdi.GetArray(name):
  107. self.view.DrawCells = 1
  108. dataRange = cdi.GetArray(name).GetRange(component)
  109. fieldToColorBy[0] = "CELLS"
  110. else:
  111. print("No array with that name", name)
  112. return
  113. realRange = dataRange
  114. if dataRange[0] == dataRange[1]:
  115. dataRange = [dataRange[0] - 0.1, dataRange[1] + 0.1]
  116. simple.ColorBy(rep, fieldToColorBy)
  117. # Grab data
  118. tmpFileName = path + "__.png"
  119. self.view.ScalarRange = dataRange
  120. self.view.LockBounds = 1
  121. self.view.StartCaptureValues()
  122. simple.SaveScreenshot(tmpFileName, self.view)
  123. self.view.StopCaptureValues()
  124. self.view.LockBounds = 0
  125. if self.canWrite:
  126. # Convert data
  127. self.reader.SetFileName(tmpFileName)
  128. self.reader.Update()
  129. rgbArray = self.reader.GetOutput().GetPointData().GetArray(0)
  130. arraySize = rgbArray.GetNumberOfTuples()
  131. rawArray = vtkFloatArray()
  132. rawArray.SetNumberOfTuples(arraySize)
  133. minValue = 10000.0
  134. maxValue = -100000.0
  135. delta = (dataRange[1] - dataRange[0]) / 16777215.0 # 2^24 - 1 => 16,777,215
  136. for idx in range(arraySize):
  137. rgb = rgbArray.GetTuple3(idx)
  138. if rgb[0] != 0 or rgb[1] != 0 or rgb[2] != 0:
  139. value = dataRange[0] + delta * float(
  140. rgb[0] * 65536 + rgb[1] * 256 + rgb[2] - 1
  141. )
  142. rawArray.SetTuple1(idx, value)
  143. minValue = min(value, minValue)
  144. maxValue = max(value, maxValue)
  145. else:
  146. rawArray.SetTuple1(idx, float("NaN"))
  147. # print ('Array bounds', minValue, maxValue, 'compare to', dataRange)
  148. with open(path, "wb") as f:
  149. f.write(memoryview(rawArray))
  150. # Delete temporary file
  151. if self.cleanAfterMe:
  152. os.remove(tmpFileName)
  153. # Remove representation from view
  154. simple.Hide(source, self.view)
  155. return realRange