def __init__(self, view_dependent=True): super().__init__() ov.VISUS_REGISTER_NODE_CLASS("visuslab.ViewerNode") self.field = None self.bounds = None self._data = None self._view_dependent = False self.camera = ov.OrthoCamera(1.3) self.view_dependent = view_dependent self.dataset_node = ov.DatasetNode() self.dataset_node.setName('Dataset node') self.addNode(self.dataset_node) self.query_node = ov.QueryNode() self.query_node.setVerbose(True) self.query_node.setName('query node') self.query_node.setAccessIndex(0) self.query_node.setProgression(ov.QueryNoProgression) self.query_node.setQuality(ov.QueryDefaultQuality) self.query_node.setViewDependentEnabled(True) self.addNode(self.query_node) self.connectNodes(self.dataset_node, "dataset", self.query_node) self.viewer_node = ViewerNode() self.viewer_node.setName('viewer node') self.addNode(self.viewer_node) self.connectNodes(self.query_node, "array", self.viewer_node) self.connectNodes(self.dataset_node, "dataset", self.viewer_node)
def bounds(self, bounds): self._bounds = bounds self.camera.guessPosition(bounds.toAxisAlignedBox()) pos = ov.Point3d() dir = ov.Point3d() vup = ov.Point3d() self.camera.getLookAt(pos, dir, vup)
def readData(self, data, resolution, load, print_attr): ov.DbModule.attach() if load: dataset = ov.LoadDataset(data) #world_dataset = ov.LoadDataset("http://atlantis.sci.utah.edu/mod_visus?dataset=BlueMarble") access = dataset.createAccess() if not dataset: raise Exception("Assert failed") if print_attr is not None: print("size ", dataset.getLogicBox().toString()) print("loaded dtype: ", dataset.getDefaultField().dtype.toString()) print("max resolution: ", dataset.getMaxResolution()) if resolution is None: resolution = dataset.getMaxResolution() else: resolution = int(dataset.getMaxResolution() * resolution) #print(" >>>>> ") #print(" Using Resolution: ", resolution, " max resolution = ", dataset.getMaxResolution()) #print(" >>>>> ") # define a box query to fetch data from a certain dataset, field and timestep query = ov.BoxQuery(dataset, dataset.getDefaultField(), dataset.getDefaultTime(), ord('r')) logic_box = dataset.getLogicBox() # set the bounding box for our query query.logic_box = logic_box # set the resolution query.end_resolutions.push_back(resolution) # prepare and execute the query dataset.beginQuery(query) dataset.executeQuery(access, query) # transform the result of the query to a numpy array data = ov.Array.toNumPy(query.buffer, bSqueeze=True, bShareMem=False) #print("read data shape: ", data.shape) if len(data.shape) > 3: squeeze = np.zeros( (data.shape[1], data.shape[2], data.shape[3])) squeeze_0 = data[ 0, :, :, :] #np.squeeze(data[0, :, :, :], axis=0) #squeeze_1 = data[1, :, :, :] #squeeze_2 = data[2, :, :, :] data = squeeze_0 ov.DbModule.detach() self.data = data #print(" shape data: ", data.shape) return data #if data.shape[0]<=3: # data = np.transpose(data,(1,2,0)) #why does it loose a dimention 3 color channel for small values # one color channel for 22 # color changes for lower res
def update_view(self): print("update_view") if self._data is not None: if self.view_dependent: if False: bounds = self.dataset_node.get_bounds() self.viewer_node.bounds = self.bounds else: x1,y1, x2, y2 = self.bounds.p1[0], self.bounds.p1[1], self.bounds.p2[0], self.bounds.p2[1] print('bounds:', x1, x2, y1, y2) frustum = ov.Frustum() frustum.loadModelview(ov.Matrix.identity(4)) frustum.loadProjection(ov.Matrix.ortho(x1, x2, y1, y2, -1, +1)) frustum.setViewport(ov.Rectangle2d(0, 0, x2 - x1, y2 - y1)) self.query_node.setNodeToScreen(frustum)
def load(self, url): self._data = ov.LoadDataset(url) # self.bounds = self.bounds.scaleAroundCenter(0.01) # 1% of the overall dataset self.dataset_node.setDataset(self._data, True) self.bounds = self.dataset_node.getBounds() self.query_node.setBounds(ov.Position(self.bounds)) self.query_node.setQueryBounds(ov.Position(self.bounds)) ba = self.bounds.toAxisAlignedBox() self.camera.guessPosition(ba) x2, y2 = self.viewer_node.view.viewport f = self.camera.getFinalFrustum(ov.Rectangle2d(0, 0, x2, y2)) self.query_node.setNodeToScreen(f) self.set_time(self._data.getDefaultTime()) self.set_field_name(self._data.getDefaultField().name)
from visuslab.simple import Simple import OpenVisus as ov if __name__ == '__main__': ov.SetCommandLine("__main__") ov.IdxModule.attach() ov.NodesModule.attach() ov.VISUS_REGISTER_NODE_CLASS("visuslab.ViewerNode") lab = Simple() lab.load("http://atlantis.sci.utah.edu/mod_visus?dataset=david") lab.start() ov.NodesModule.detach() ov.IdxModule.detach()
def convert_image(self, image=None, idx_filename='visus.idx'): self.image = image if image is not None else self.image ov.SetCommandLine("__main__") ov.DbModule.attach() # trick to speed up the conversion os.environ["VISUS_DISABLE_WRITE_LOCK"] = "1" # numpy display is Z,Y,X #print("image shape ", self.image.shape) height, width, depth = self.image.shape #img = np.transpose(self.image, [2,0,1]) #print(self.image.shape) idx_name = idx_filename #print("image", idx_name, "has dimensions", width, height, depth) # to disable offset just set this to 0 offset_x = 0 # numpy dtype -> OpenVisus dtype typestr = self.image.__array_interface__["typestr"] dtype = ov.DType(typestr[1] == "u", typestr[1] == "f", int(typestr[2]) * 8) dtype = ov.DType(3, dtype) #print("dtype written: ",dtype.toString()) dims = ov.PointNi(int(width + offset_x * depth), int(height), int(depth)) idx_file = ov.IdxFile() idx_file.logic_box = ov.BoxNi(ov.PointNi(0, 0, 0), dims) idx_file.fields.push_back(ov.Field('channel0', dtype)) idx_file.save(idx_name) #print("Created IDX file") dataset = ov.LoadDataset(idx_name) access = dataset.createAccess() if not dataset: raise Exception("Assert failed") # for by slice approach #for Z in range(0, depth): data = self.image #np.transpose(self.image, [1,0,2])#img[Z, :, :] slice_box = dataset.getLogicBox() #.getZSlab(Z, Z + 1) if not (slice_box.size()[0] == dims[0] and slice_box.size()[1] == dims[1]): raise Exception("Assert failed") query = ov.BoxQuery(dataset, dataset.getDefaultField(), dataset.getDefaultTime(), ord('w')) query.logic_box = slice_box dataset.beginQuery(query) if not query.isRunning(): raise Exception("Assert failed") buffer = ov.Array(query.getNumberOfSamples(), query.field.dtype) #buffer.fillWithValue(0) fill = ov.Array.toNumPy(buffer, bSqueeze=True, bShareMem=True) y1 = 0 y2 = height x1 = 0 # offset_x * Z x2 = x1 + width fill[0, y1:y2, x1:x2, :] = data query.buffer = buffer if not (dataset.executeQuery(access, query)): raise Exception("Assert failed") ov.DbModule.detach()