summaryrefslogtreecommitdiff
path: root/silx/gui/plot3d/items/image.py
blob: 4e2b396d809a757b89d6f93a5d018437fec7d114 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2017-2021 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""This module provides 2D data and RGB(A) image item class.
"""

from __future__ import absolute_import

__authors__ = ["T. Vincent"]
__license__ = "MIT"
__date__ = "15/11/2017"

import numpy

from ..scene import primitives, utils
from .core import DataItem3D, ItemChangedType
from .mixins import ColormapMixIn, InterpolationMixIn
from ._pick import PickingResult


class _Image(DataItem3D, InterpolationMixIn):
    """Base class for images

    :param parent: The View widget this item belongs to.
    """

    def __init__(self, parent=None):
        DataItem3D.__init__(self, parent=parent)
        InterpolationMixIn.__init__(self)

    def _setPrimitive(self, primitive):
        InterpolationMixIn._setPrimitive(self, primitive)

    def getData(self, copy=True):
        raise NotImplementedError()

    def _pickFull(self, context):
        """Perform picking in this item at given widget position.

        :param PickContext context: Current picking context
        :return: Object holding the results or None
        :rtype: Union[None,PickingResult]
        """
        rayObject = context.getPickingSegment(frame=self._getScenePrimitive())
        if rayObject is None:
            return None

        points = utils.segmentPlaneIntersect(
            rayObject[0, :3],
            rayObject[1, :3],
            planeNorm=numpy.array((0., 0., 1.), dtype=numpy.float64),
            planePt=numpy.array((0., 0., 0.), dtype=numpy.float64))

        if len(points) == 1:  # Single intersection
            if points[0][0] < 0. or points[0][1] < 0.:
                return None  # Outside image
            row, column = int(points[0][1]), int(points[0][0])
            data = self.getData(copy=False)
            height, width = data.shape[:2]
            if row < height and column < width:
                return PickingResult(
                    self,
                    positions=[(points[0][0], points[0][1], 0.)],
                    indices=([row], [column]))
            else:
                return None  # Outside image
        else:  # Either no intersection or segment and image are coplanar
            return None


class ImageData(_Image, ColormapMixIn):
    """Description of a 2D image data.

    :param parent: The View widget this item belongs to.
    """

    def __init__(self, parent=None):
        _Image.__init__(self, parent=parent)
        ColormapMixIn.__init__(self)

        self._data = numpy.zeros((0, 0), dtype=numpy.float32)

        self._image = primitives.ImageData(self._data)
        self._getScenePrimitive().children.append(self._image)

        # Connect scene primitive to mix-in class
        ColormapMixIn._setSceneColormap(self, self._image.colormap)
        _Image._setPrimitive(self, self._image)

    def setData(self, data, copy=True):
        """Set the image data to display.

        The data will be casted to float32.

        :param numpy.ndarray data: The image data
        :param bool copy: True (default) to copy the data,
                          False to use as is (do not modify!).
        """
        self._image.setData(data, copy=copy)
        self._setColormappedData(self.getData(copy=False), copy=False)
        self._updated(ItemChangedType.DATA)

    def getData(self, copy=True):
        """Get the image data.

        :param bool copy:
            True (default) to get a copy,
            False to get internal representation (do not modify!).
        :rtype: numpy.ndarray
        :return: The image data
        """
        return self._image.getData(copy=copy)


class ImageRgba(_Image, InterpolationMixIn):
    """Description of a 2D data RGB(A) image.

    :param parent: The View widget this item belongs to.
    """

    def __init__(self, parent=None):
        _Image.__init__(self, parent=parent)
        InterpolationMixIn.__init__(self)

        self._data = numpy.zeros((0, 0, 3), dtype=numpy.float32)

        self._image = primitives.ImageRgba(self._data)
        self._getScenePrimitive().children.append(self._image)

        # Connect scene primitive to mix-in class
        _Image._setPrimitive(self, self._image)

    def setData(self, data, copy=True):
        """Set the RGB(A) image data to display.

        Supported array format: float32 in [0, 1], uint8.

        :param numpy.ndarray data:
            The RGBA image data as an array of shape (H, W, Channels)
        :param bool copy: True (default) to copy the data,
                          False to use as is (do not modify!).
        """
        self._image.setData(data, copy=copy)
        self._updated(ItemChangedType.DATA)

    def getData(self, copy=True):
        """Get the image data.

        :param bool copy:
            True (default) to get a copy,
            False to get internal representation (do not modify!).
        :rtype: numpy.ndarray
        :return: The image data
        """
        return self._image.getData(copy=copy)


class _HeightMap(DataItem3D):
    """Base class for 2D data array displayed as a height field.

    :param parent: The View widget this item belongs to.
    """

    def __init__(self, parent=None):
        DataItem3D.__init__(self, parent=parent)
        self.__data = numpy.zeros((0, 0), dtype=numpy.float32)

    def _pickFull(self, context, threshold=0., sort='depth'):
        """Perform picking in this item at given widget position.

        :param PickContext context: Current picking context
        :param float threshold: Picking threshold in pixel.
            Perform picking in a square of size threshold x threshold.
        :param str sort: How returned indices are sorted:

            - 'index' (default): sort by the value of the indices
            - 'depth':  Sort by the depth of the points from the current
              camera point of view.
        :return: Object holding the results or None
        :rtype: Union[None,PickingResult]
        """
        assert sort in ('index', 'depth')

        rayNdc = context.getPickingSegment(frame='ndc')
        if rayNdc is None:  # No picking outside viewport
            return None

        # TODO no colormapped or color data
        # Project data to NDC
        heightData = self.getData(copy=False)
        if heightData.size == 0:
            return  # Nothing displayed

        height, width = heightData.shape
        z = numpy.ravel(heightData)
        y, x = numpy.mgrid[0:height, 0:width]
        dataPoints = numpy.transpose((numpy.ravel(x),
                                      numpy.ravel(y),
                                      z,
                                      numpy.ones_like(z)))

        primitive = self._getScenePrimitive()

        pointsNdc = primitive.objectToNDCTransform.transformPoints(
            dataPoints, perspectiveDivide=True)

        # Perform picking
        distancesNdc = numpy.abs(pointsNdc[:, :2] - rayNdc[0, :2])
        # TODO issue with symbol size: using pixel instead of points
        threshold += 1.  # symbol size
        thresholdNdc = 2. * threshold / numpy.array(primitive.viewport.size)
        picked = numpy.where(numpy.logical_and(
                numpy.all(distancesNdc < thresholdNdc, axis=1),
                numpy.logical_and(rayNdc[0, 2] <= pointsNdc[:, 2],
                                  pointsNdc[:, 2] <= rayNdc[1, 2])))[0]

        if sort == 'depth':
            # Sort picked points from front to back
            picked = picked[numpy.argsort(pointsNdc[picked, 2])]

        if picked.size > 0:
            # Convert indices from 1D to 2D
            return PickingResult(self,
                                 positions=dataPoints[picked, :3],
                                 indices=(picked // width, picked % width),
                                 fetchdata=self.getData)
        else:
            return None

    def setData(self, data, copy: bool=True):
        """Set the height field data.

        :param data:
        :param copy: True (default) to copy the data,
            False to use as is (do not modify!).
        """
        data = numpy.array(data, copy=copy)
        assert data.ndim == 2

        self.__data = data
        self._updated(ItemChangedType.DATA)

    def getData(self, copy: bool=True) -> numpy.ndarray:
        """Get the height field 2D data.

        :param bool copy:
            True (default) to get a copy,
            False to get internal representation (do not modify!).
        """
        return numpy.array(self.__data, copy=copy)


class HeightMapData(_HeightMap, ColormapMixIn):
    """Description of a 2D height field associated to a colormapped dataset.

    :param parent: The View widget this item belongs to.
    """

    def __init__(self, parent=None):
        _HeightMap.__init__(self, parent=parent)
        ColormapMixIn.__init__(self)

        self.__data = numpy.zeros((0, 0), dtype=numpy.float32)

    def _updated(self, event=None):
        if event == ItemChangedType.DATA:
            self.__updateScene()
        super()._updated(event=event)

    def __updateScene(self):
        """Update display primitive to use"""
        self._getScenePrimitive().children = []  # Remove previous primitives
        ColormapMixIn._setSceneColormap(self, None)

        if not self.isVisible():
            return  # Update when visible

        data = self.getColormappedData(copy=False)
        heightData = self.getData(copy=False)

        if data.size == 0 or heightData.size == 0:
            return  # Nothing to display

        # Display as a set of points
        height, width = heightData.shape
        # Generates coordinates
        y, x = numpy.mgrid[0:height, 0:width]

        if data.shape != heightData.shape:  # data and height size miss-match
            # Colormapped data is interpolated (nearest-neighbour) to match the height field
            data = data[numpy.floor(y * data.shape[0] / height).astype(numpy.int),
                        numpy.floor(x * data.shape[1] / height).astype(numpy.int)]

        x = numpy.ravel(x)
        y = numpy.ravel(y)

        primitive = primitives.Points(
            x=x,
            y=y,
            z=numpy.ravel(heightData),
            value=numpy.ravel(data),
            size=1)
        primitive.marker = 's'
        ColormapMixIn._setSceneColormap(self, primitive.colormap)
        self._getScenePrimitive().children = [primitive]

    def setColormappedData(self, data, copy: bool=True):
        """Set the 2D data used to compute colors.

        :param data: 2D array of data
        :param copy: True (default) to copy the data,
            False to use as is (do not modify!).
        """
        data = numpy.array(data, copy=copy)
        assert data.ndim == 2

        self.__data = data
        self._updated(ItemChangedType.DATA)

    def getColormappedData(self, copy: bool=True) -> numpy.ndarray:
        """Returns the 2D data used to compute colors.

        :param copy:
            True (default) to get a copy,
            False to get internal representation (do not modify!).
        """
        return numpy.array(self.__data, copy=copy)


class HeightMapRGBA(_HeightMap):
    """Description of a 2D height field associated to a RGB(A) image.

    :param parent: The View widget this item belongs to.
    """

    def __init__(self, parent=None):
        _HeightMap.__init__(self, parent=parent)

        self.__rgba = numpy.zeros((0, 0, 3), dtype=numpy.float32)

    def _updated(self, event=None):
        if event == ItemChangedType.DATA:
            self.__updateScene()
        super()._updated(event=event)

    def __updateScene(self):
        """Update display primitive to use"""
        self._getScenePrimitive().children = []  # Remove previous primitives

        if not self.isVisible():
            return  # Update when visible

        rgba = self.getColorData(copy=False)
        heightData = self.getData(copy=False)
        if rgba.size == 0 or heightData.size == 0:
            return  # Nothing to display

        # Display as a set of points
        height, width = heightData.shape
        # Generates coordinates
        y, x = numpy.mgrid[0:height, 0:width]

        if rgba.shape[:2] != heightData.shape:  # image and height size miss-match
            # RGBA data is interpolated (nearest-neighbour) to match the height field
            rgba = rgba[numpy.floor(y * rgba.shape[0] / height).astype(numpy.int),
                        numpy.floor(x * rgba.shape[1] / height).astype(numpy.int)]

        x = numpy.ravel(x)
        y = numpy.ravel(y)

        primitive = primitives.ColorPoints(
            x=x,
            y=y,
            z=numpy.ravel(heightData),
            color=rgba.reshape(-1, rgba.shape[-1]),
            size=1)
        primitive.marker = 's'
        self._getScenePrimitive().children = [primitive]

    def setColorData(self, data, copy: bool=True):
        """Set the RGB(A) image to use.

        Supported array format: float32 in [0, 1], uint8.

        :param data:
            The RGBA image data as an array of shape (H, W, Channels)
        :param copy: True (default) to copy the data,
            False to use as is (do not modify!).
        """
        data = numpy.array(data, copy=copy)
        assert data.ndim == 3
        assert data.shape[-1] in (3, 4)
        # TODO check type

        self.__rgba = data
        self._updated(ItemChangedType.DATA)

    def getColorData(self, copy: bool=True) -> numpy.ndarray:
        """Get the RGB(A) image data.

        :param copy: True (default) to get a copy,
            False to get internal representation (do not modify!).
        """
        return numpy.array(self.__rgba, copy=copy)