Skip to content
Snippets Groups Projects
Commit 37cd39c6 authored by Scott Wittenburg's avatar Scott Wittenburg
Browse files

Add a render window serializer for exporting to web

parent 4497bedd
No related branches found
No related tags found
No related merge requests found
......@@ -43,7 +43,17 @@ class TestDataEncoder(Testing.vtkTest):
# Now Base64 decode the string back to PNG image data bytes
inputArray = array.array('B', base64String)
outputBuffer = bytearray(len(inputArray))
utils = vtk.vtkIOCore.vtkBase64Utilities()
utils = None
try:
utils = vtk.vtkIOCore.vtkBase64Utilities()
except:
try:
utils = vtkIOCore.vtkBase64Utilities()
except:
print('Unable to import required vtkIOCore.vtkBase64Utilities')
return
actualLength = utils.DecodeSafely(inputArray, len(inputArray), outputBuffer, len(outputBuffer))
outputArray = bytearray(actualLength)
outputArray[:] = outputBuffer[0:actualLength]
......
......@@ -13,6 +13,7 @@ vtk_module(vtkWebCore
vtkCommonDataModel
vtkCommonSystem
vtkFiltersGeneral
vtkFiltersGeometry
vtkIOCore
vtkIOImage
vtkParallelCore
......
......@@ -9,3 +9,7 @@ include(vtkModuleMacrosPython)
# package.
vtk_module_python_package(${vtk-module} "vtk/web"
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}")
if(BUILD_TESTING)
add_subdirectory(Testing)
endif()
vtk_add_test_python(
NO_DATA NO_VALID NO_OUTPUT
TestSerializeRenderWindow.py
)
import json
import vtk
from vtk.web import render_window_serializer as rws
from vtk.test import Testing
class TestSerializeRenderWindow(Testing.vtkTest):
def testSerializeRenderWindow(self):
cone = vtk.vtkConeSource()
coneMapper = vtk.vtkPolyDataMapper()
coneMapper.SetInputConnection(cone.GetOutputPort())
coneActor = vtk.vtkActor()
coneActor.SetMapper(coneMapper)
ren = vtk.vtkRenderer()
ren.AddActor(coneActor)
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
ren.ResetCamera()
renWin.Render()
# Exercise some of the serialization functionality and make sure it
# does not generate a stack trace
context = rws.SynchronizationContext()
rws.initializeSerializers()
jsonData = rws.serializeInstance(None, renWin, rws.getReferenceId(renWin), context, 0)
# jsonStr = json.dumps(jsonData)
# print jsonStr
# print len(jsonStr)
if __name__ == "__main__":
Testing.main([(TestSerializeRenderWindow, 'test')])
......@@ -2,7 +2,15 @@ vtk_module(vtkWebPython
GROUPS
Web
EXCLUDE_FROM_WRAPPING
TEST_DEPENDS
vtkTestingCore
TEST_LABELS
VTKWEB
DEPENDS
vtkCommonCore
PRIVATE_DEPENDS
vtkFiltersGeometry
vtkWebCore
COMPILE_DEPENDS
vtkPython
OPTIONAL_PYTHON_LINK
......
import sys
import sys, re, hashlib, base64
py3 = sys.version_info.major > 2
arrayTypesMapping = [
' ', # VTK_VOID 0
' ', # VTK_BIT 1
'b', # VTK_CHAR 2
'B', # VTK_UNSIGNED_CHAR 3
'h', # VTK_SHORT 4
'H', # VTK_UNSIGNED_SHORT 5
'i', # VTK_INT 6
'I', # VTK_UNSIGNED_INT 7
'l', # VTK_LONG 8
'L', # VTK_UNSIGNED_LONG 9
'f', # VTK_FLOAT 10
'd', # VTK_DOUBLE 11
'L', # VTK_ID_TYPE 12
]
javascriptMapping = {
'b': 'Int8Array',
'B': 'Uint8Array',
'h': 'Int16Array',
'H': 'Int16Array',
'i': 'Int32Array',
'I': 'Uint32Array',
'l': 'Int32Array',
'L': 'Uint32Array',
'f': 'Float32Array',
'd': 'Float64Array'
}
if py3:
def iteritems(d, **kwargs):
return iter(d.items(**kwargs))
buffer = memoryview
base64Encode = lambda x: base64.b64encode(x).decode('utf-8')
else:
def iteritems(d, **kwargs):
return d.iteritems(**kwargs)
buffer = buffer
base64Encode = lambda x: x.encode('base64')
def hashDataArray(dataArray):
hashedBit = base64Encode(hashlib.md5(buffer(dataArray)).digest()).strip()
md5sum = re.sub('==$', '', hashedBit)
typeCode = arrayTypesMapping[dataArray.GetDataType()]
return '%s_%d%s' % (md5sum, dataArray.GetSize(), typeCode)
def getJSArrayType(dataArray):
return javascriptMapping[arrayTypesMapping[dataArray.GetDataType()]]
from vtk import *
from vtk.web import getJSArrayType
from vtk.web.camera import *
from vtk.web.query_data_model import *
from vtk.web import iteritems, buffer
......@@ -6,19 +7,6 @@ import json, os, math, gzip, shutil
# Global helper variables
encode_codes = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
arrayTypesMapping = ' bBhHiIlLfd'
jsMapping = {
'b': 'Int8Array',
'B': 'Uint8Array',
'h': 'Int16Array',
'H': 'Int16Array',
'i': 'Int32Array',
'I': 'Uint32Array',
'l': 'Int32Array',
'L': 'Uint32Array',
'f': 'Float32Array',
'd': 'Float64Array'
}
# -----------------------------------------------------------------------------
# Capture image from render window
......@@ -309,7 +297,7 @@ class DataProberDataSetBuilder(DataSetBuilder):
with open(self.dataHandler.getDataAbsoluteFilePath(field), 'wb') as f:
f.write(b)
self.DataProber['types'][field] = jsMapping[arrayTypesMapping[array.GetDataType()]]
self.DataProber['types'][field] = getJSArrayType(array)
if field in self.DataProber['ranges']:
dataRange = array.GetRange()
if dataRange[0] < self.DataProber['ranges'][field][0]:
......
......@@ -3,17 +3,13 @@ protocols that can be combined together to provide a flexible way to define
very specific web application.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import, division, print_function
from time import time
import os, sys, logging, types, inspect, traceback, logging, re
try:
from vtk.vtkWebCore import vtkWebApplication, vtkWebInteractionEvent
except ImportError:
from vtkWebCore import vtkWebApplication, vtkWebInteractionEvent
from vtk.vtkWebCore import vtkWebApplication, vtkWebInteractionEvent
from autobahn.wamp import register as exportRpc
# =============================================================================
......
This diff is collapsed.
......@@ -10,9 +10,7 @@ Use "--help" to list the supported arguments.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import, division, print_function
import sys, logging
......
......@@ -12,9 +12,7 @@ r"""
Port on which upload server should listen.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import, division, print_function
# import os to concatenate paths in a system independent way
import os
......
......@@ -3,9 +3,7 @@ WAMP related class for the purpose of vtkWeb.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import, division, print_function
import inspect, types, string, random, logging, six, json, re, base64, time
......@@ -29,11 +27,7 @@ from autobahn.twisted.websocket import WampWebSocketServerProtocol
from autobahn.twisted.websocket import WebSocketServerProtocol
from vtk.web import protocols
try:
from vtk.vtkWebCore import vtkWebApplication
except ImportError:
from vtkWebCore import vtkWebApplication
from vtk.vtkWebCore import vtkWebApplication
# =============================================================================
salt = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(32))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment