tcpviewer.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # This file is part of libigl, a simple c++ geometry processing library.
  2. #
  3. # Copyright (C) 2017 Sebastian Koch <s.koch@tu-berlin.de> and Daniele Panozzo <daniele.panozzo@gmail.com>
  4. #
  5. # This Source Code Form is subject to the terms of the Mozilla Public License
  6. # v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. # obtain one at http://mozilla.org/MPL/2.0/.
  8. import socket
  9. import threading
  10. import pyigl as igl
  11. import array
  12. import time
  13. HOST = 'localhost' # Symbolic name meaning all available interfaces
  14. PORT = 50008 # Arbitrary non-privileged port
  15. def worker(viewer,lock,s):
  16. print("TCP iglviewer server listening on port " + str(PORT))
  17. try:
  18. while True:
  19. conn, addr = s.accept()
  20. lock.acquire()
  21. slist = []
  22. while True:
  23. buf = conn.recv(4096)
  24. if not buf:
  25. break
  26. slist.append(buf.decode('unicode_internal','ignore'))
  27. conn.close()
  28. data = ''.join(slist)
  29. temp = list(data)
  30. isempty = viewer.data().V.rows() == 0
  31. viewer.data().deserialize(temp)
  32. if isempty and viewer.data().V.rows() != 0:
  33. viewer.core.align_camera_center(viewer.data().V,viewer.data().F)
  34. lock.release()
  35. except:
  36. s.close()
  37. return
  38. class TCPViewer(igl.glfw.Viewer):
  39. def launch(self):
  40. try:
  41. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  42. s.connect((HOST, PORT))
  43. ser = self.data().serialize()
  44. a = array.array('u', ser)
  45. s.sendall(a)
  46. s.close()
  47. except:
  48. print("Failed to open socket, is tcpviewer running?")
  49. if __name__ == "__main__": # The main script is a server
  50. ## Try to open the socket first
  51. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  52. try:
  53. s.bind((HOST, PORT))
  54. except:
  55. print("Failed to bind, port already used.")
  56. exit(1)
  57. s.listen(1)
  58. viewer = igl.glfw.Viewer()
  59. lock = threading.Lock()
  60. t = threading.Thread(target=worker, args=(viewer,lock,s,))
  61. t.setDaemon(True)
  62. t.start()
  63. viewer.core.is_animating = True
  64. # viewer.data().dirty = int(0x03FF)
  65. viewer.launch_init(True,False)
  66. done = False
  67. while not done:
  68. lock.acquire()
  69. done = not viewer.launch_rendering(False)
  70. lock.release()
  71. time.sleep(0.000001) # DO NOT REMOVE ME
  72. viewer.launch_shut()