You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
3.7 KiB

  1. #!/usr/bin/python3
  2. import visvis as vv
  3. import numpy as np
  4. import time
  5. def translateArrayToVV(array):
  6. scaling = (float(array[0][1]) - float(array[0][0]), float(array[2][1]) - float(array[2][0]), float(array[4][1]) - float(array[4][0]))
  7. translation = (float(array[0][0]) + scaling[0] * 0.5, float(array[2][0]) + scaling[0] * 0.5, float(array[4][0]) + scaling[0] * 0.5)
  8. return translation, scaling
  9. class VisVisPlotter():
  10. def __init__(self, stateValuations, goalStates, deadlockStates, stepwise):
  11. self.app = vv.use()
  12. self.fig = vv.clf()
  13. self.ax = vv.cla()
  14. self.stateColor = (0,0,0.75,0.4)
  15. self.failColor = (0.8,0.8,0.8,1.0) # (1,0,0,0.8)
  16. self.goalColor = (0,1,0,1.0)
  17. self.stateScaling = (2,2,2) #(.5,.5,.5)
  18. self.plotedStates = set()
  19. self.plotedMeshes = set()
  20. self.stepwise = stepwise
  21. auxStates = [0,1,2,25518]
  22. self.goals = set([(stateValuation.x, stateValuation.y, stateValuation.z) for stateId, stateValuation in stateValuations.items() if stateId in goalStates and not stateId in auxStates])
  23. self.fails = set([(stateValuation.x, stateValuation.y, stateValuation.z) for stateId, stateValuation in stateValuations.items() if stateId in deadlockStates and not stateId in auxStates])
  24. def plotScenario(self, saveScreenshot=True):
  25. for goal in self.goals:
  26. state = vv.solidBox(goal, scaling=self.stateScaling)
  27. state.faceColor = self.goalColor
  28. for fail in self.fails:
  29. state = vv.solidBox(fail, scaling=self.stateScaling)
  30. state.faceColor = self.failColor
  31. self.ax.SetLimits((-16,16),(-10,10),(-7,7))
  32. self.ax.SetView({'zoom':0.025, 'elevation':20, 'azimuth':30})
  33. if saveScreenshot: vv.screenshot("000.png", sf=3, bg='w', ob=vv.gcf())
  34. def run(self):
  35. self.ax.SetLimits((-16,16),(-10,10),(-7,7))
  36. self.app.Run()
  37. def clear(self):
  38. axes = vv.gca()
  39. axes.Clear()
  40. def plotStates(self, states, coloring="", removeMeshes=False):
  41. if self.stepwise and removeMeshes:
  42. self.clear()
  43. self.plotScenario(saveScreenshot=False)
  44. if not coloring:
  45. coloring = self.stateColor
  46. plotedRegions = set()
  47. for state in states:
  48. if state in self.plotedStates: continue # what are the implications of this?
  49. coordinates = (state.x, state.y, state.z)
  50. print(f"plotting {state} at {coordinates}")
  51. if coordinates in plotedRegions: continue
  52. state = vv.solidBox(coordinates, scaling=(1,1,1))#(0.5,0.5,0.5))
  53. state.faceColor = coloring
  54. plotedRegions.add(coordinates)
  55. if self.stepwise:
  56. self.plotedMeshes.add(state)
  57. self.plotedStates.update(states)
  58. def takeScreenshot(self, iteration, prefix=""):
  59. self.ax.SetLimits((-16,16),(-10,10),(-7,7))
  60. #config = [(90, 00), (45, 00), (0, 00), (-45, 00), (-90, 00)]
  61. config = [(45, -150), (45, -100), (45, -60), (45, 60), (45, 120)]
  62. for elevation, azimuth in config:
  63. filename = f"{prefix}{'_' if prefix else ''}{iteration:03}_{elevation}_{azimuth}.png"
  64. print(f"Saving Screenshot to {filename}")
  65. self.ax.SetView({'zoom':0.025, 'elevation':elevation, 'azimuth':azimuth})
  66. vv.screenshot(filename, sf=3, bg='w', ob=vv.gcf())
  67. def turnCamera(self):
  68. config = [(45, -150), (45, -100), (45, -60), (45, 60), (45, 120)]
  69. self.ax.SetLimits((-16,16),(-10,10),(-7,7))
  70. for elevation, azimuth in config:
  71. self.ax.SetView({'zoom':0.025, 'elevation':elevation, 'azimuth':azimuth})
  72. if __name__ == '__main__':
  73. main()