Commit 34f2130f authored by Nicolas Vuaille's avatar Nicolas Vuaille Committed by Kitware Robot

Merge topic 'paraviewSpecA'

4caddee2 Modify paraview adaptors to read specA
7ca34c86 write human-friendly json
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Merge-request: !112
parents 77c759a8 4caddee2
Pipeline #63726 failed with stage
in 0 seconds
......@@ -4,6 +4,7 @@ from __future__ import print_function
import cinema_python.database.file_store as file_store
import cinema_python.images.querymaker_specb as CT
from cinema_python.images import querymaker
from paraview import vtk
from paraview.vtk.numpy_interface import dataset_adapter as dsa
from cinema_python.images.camera_utils import convert_pose_to_camera
......@@ -89,8 +90,11 @@ class FileStoreAPI(object):
return []
def get_control_values(self, controlparametername):
"""returns values available for a given control parameter"""
return []
"""returns sorted list of available values for a control parameter"""
info = self.fs.get_parameter(controlparametername)
values = info["values"]
values.sort()
return values
def get_control_values_as_strings(self, controlparametername):
vals = self.get_control_values(controlparametername)
......@@ -107,24 +111,129 @@ class FileStoreAPI(object):
def get_field_valuerange(self, objectname, valueanme):
return []
def translate_query(self, q):
return []
def get_spec(self):
"""returns current database spec """
return ""
class FileStoreSpecA(FileStoreAPI):
def __init__(self, fs):
FileStoreAPI.__init__(self, fs, querymaker.QueryMaker_SpecA())
self.__cameras = []
def get_objects(self):
"""returns a list of viewable objects in the scene"""
objects = ["Cinema"]
return objects
def get_parents(self, objectname):
if objectname == "Cinema":
return []
return ["Cinema"]
def get_visibility(self, objectname):
"""returns an objects default visbility"""
return True
def get_control_parameters(self, objectname):
"""returns all parameters for this database"""
params = []
for param in self.fs.parameter_list.keys():
params.append(param)
return params
def get_cameras(self, ts):
"""returns a list of camera positions timestep is ignored."""
# use cached cameras, if available.
if 0 in self.__cameras:
return self.__cameras
# if not cached, read and cache.
md = self.fs.metadata
try:
eye = md["camera_eye"][0][:3]
at = md["camera_at"][0][:3]
up = md["camera_up"][0][:3]
nearfar = md["camera_nearfar"][0][:2]
angle = md["camera_angle"][0]
except KeyError:
print("Warning: Cannot initialiaze cameras." +
" Interaction may not work correctly")
return self.__cameras
phis = [0]
if "phi" in self.fs.parameter_list.keys():
phis = self.fs.get_parameter("phi")["values"]
thetas = [0]
if "theta" in self.fs.parameter_list.keys():
thetas = self.fs.get_parameter("theta")["values"]
camera = vtk.vtkCamera()
camera.SetPosition(eye)
camera.SetFocalPoint(at)
camera.SetViewUp(up)
camera.SetViewAngle(angle)
camera.SetClippingRange(nearfar[0], nearfar[1])
for phi in phis:
for theta in thetas:
c = vtk.vtkCamera()
c.DeepCopy(camera)
c.Azimuth(phi)
c.Elevation(theta)
c.OrthogonalizeViewUp()
self.__cameras.append(c)
return self.__cameras
def translate_query(self, q):
if type(q) == str:
q = literal_eval(q)
assert type(q) == dict
# update pose
poseIndex = q.get("pose", -1)
if type(poseIndex) == int:
if poseIndex != -1:
q["pose"] = self.fs.get_parameter("pose")["values"][poseIndex]
else:
return []
# be sure all params have a value in the new query
query = {}
for param in self.fs.parameter_list.keys():
default = [self.fs.get_parameter(param)["default"]]
query[param] = q.get(param, default)
# print "query", q
layers = self.qt.translateQuery(q)
layers = self.qt.translateQuery(query)
# convert layers to image data
return [layer2img(l) for l in layers]
layer = layers[0]
img = vtk.vtkImageData()
dims = [0, 0, 0]
if layer.hasColorArray():
ncolors = layer.getColorArray()
# now, the numpy array's shape matches the 2D image
dims[0] = ncolors.shape[1]
dims[1] = ncolors.shape[0]
img.SetDimensions(dims[0], dims[1], 1)
ncolors = ncolors.reshape((dims[0]*dims[1], -1))
vcolors = dsa.numpyTovtkDataArray(ncolors, "Colors")
img.GetPointData().SetScalars(vcolors)
nluminance = layer.getLuminance()
if nluminance is not None:
nluminance = nluminance.reshape((dims[0]*dims[1], -1))
vluminance = dsa.numpyTovtkDataArray(nluminance, "Luminance")
img.GetPointData().AddArray(vluminance)
return [img]
def get_spec(self):
return "specA"
class FileStoreSpecB(FileStoreAPI):
......@@ -191,11 +300,6 @@ class FileStoreSpecB(FileStoreAPI):
else:
return []
def get_control_values(self, controlparametername):
"""returns values available for a given control parameter"""
info = self.fs.get_parameter(controlparametername)
return info["values"]
def get_field_name(self, objectname):
(tmp0, field, tmp1) = self.fs.parameters_for_object(objectname)
return field
......@@ -267,6 +371,35 @@ class FileStoreSpecB(FileStoreAPI):
self.__cameras[tsindex] = cameras
return cameras
def translate_query(self, q):
if type(q) == str:
q = literal_eval(q)
assert type(q) == dict
# be sure all params have a value in the new query
query = {}
for param in self.fs.parameter_list.keys():
default = [self.fs.get_parameter(param)["default"]]
query[param] = q.get(param, default)
# update pose
poseIndex = query.get("pose", -1)
if type(poseIndex) == int:
if poseIndex != -1:
query["pose"] = self.fs.get_parameter(
"pose")["values"][poseIndex]
else:
return []
# print ("query", q)
layers = self.qt.translateQuery(query)
# convert layers to image data
return [layer2img(l) for l in layers]
def get_spec(self):
return "specC"
__warning_count = {}
......@@ -276,11 +409,16 @@ def load(filename):
fs = file_store.FileStore(filename)
fs.load()
if fs.metadata.get("type") == "parametric-image-stack":
return FileStoreSpecA(fs)
# check if we support this cinema database.
if fs.metadata.get("type") != "composite-image-stack":
print("Only 'composite-image-stack' file stores are supported.")
print("Only 'composite-image-stack' and 'parametric-image-stack'" +
" file stores are supported.")
raise RuntimeError(
"Only 'composite-image-stack' file stores are supported.")
"Only 'composite-image-stack' and 'parametric-image-stack'" +
" file stores are supported.")
if fs.metadata.get("camera_model") != "azimuth-elevation-roll":
print("Only 'azimuth-elevation-roll' cameras are supported.")
raise RuntimeError(
......
......@@ -77,7 +77,7 @@ class FileStore(store.Store):
if not os.path.exists(dirname):
os.makedirs(dirname)
with open(self.__dbfilename, mode="wb") as file:
json.dump(info_json, file)
json.dump(info_json, file, sort_keys=True, indent=4)
@property
def filename_pattern(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment