summaryrefslogtreecommitdiff
path: root/silx/gui/plot3d/items/scatter.py
blob: e8ffee10f973d9b2b021429b0f366c9dbbc36640 (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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2017-2019 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 and 3D scatter data item class.
"""

from __future__ import absolute_import

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

try:
    from collections import abc
except ImportError:  # Python2 support
    import collections as abc
import logging
import numpy

from ....utils.deprecation import deprecated
from ... import _glutils as glu
from ...plot._utils.delaunay import delaunay
from ..scene import function, primitives, utils

from ...plot.items import ScatterVisualizationMixIn
from .core import DataItem3D, Item3DChangedType, ItemChangedType
from .mixins import ColormapMixIn, SymbolMixIn
from ._pick import PickingResult


_logger = logging.getLogger(__name__)


class Scatter3D(DataItem3D, ColormapMixIn, SymbolMixIn):
    """Description of a 3D scatter plot.

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

    # TODO supports different size for each point

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

        noData = numpy.zeros((0, 1), dtype=numpy.float32)
        symbol, size = self._getSceneSymbol()
        self._scatter = primitives.Points(
            x=noData, y=noData, z=noData, value=noData, size=size)
        self._scatter.marker = symbol
        self._getScenePrimitive().children.append(self._scatter)

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

    def _updated(self, event=None):
        """Handle mix-in class updates"""
        if event in (ItemChangedType.SYMBOL, ItemChangedType.SYMBOL_SIZE):
            symbol, size = self._getSceneSymbol()
            self._scatter.marker = symbol
            self._scatter.setAttribute('size', size, copy=True)

        super(Scatter3D, self)._updated(event)

    def setData(self, x, y, z, value, copy=True):
        """Set the data of the scatter plot

        :param numpy.ndarray x: Array of X coordinates (single value not accepted)
        :param y: Points Y coordinate (array-like or single value)
        :param z: Points Z coordinate (array-like or single value)
        :param value: Points values (array-like or single value)
        :param bool copy:
            True (default) to copy the data,
            False to use provided data (do not modify!)
        """
        self._scatter.setAttribute('x', x, copy=copy)
        self._scatter.setAttribute('y', y, copy=copy)
        self._scatter.setAttribute('z', z, copy=copy)
        self._scatter.setAttribute('value', value, copy=copy)

        ColormapMixIn._setRangeFromData(self, self.getValueData(copy=False))
        self._updated(ItemChangedType.DATA)

    def getData(self, copy=True):
        """Returns data as provided to :meth:`setData`.

        :param bool copy: True to get a copy,
                          False to return internal data (do not modify!)
        :return: (x, y, z, value)
        """
        return (self.getXData(copy),
                self.getYData(copy),
                self.getZData(copy),
                self.getValueData(copy))

    def getXData(self, copy=True):
        """Returns X data coordinates.

        :param bool copy: True to get a copy,
                          False to return internal array (do not modify!)
        :return: X coordinates
        :rtype: numpy.ndarray
        """
        return self._scatter.getAttribute('x', copy=copy).reshape(-1)

    def getYData(self, copy=True):
        """Returns Y data coordinates.

        :param bool copy: True to get a copy,
                          False to return internal array (do not modify!)
        :return: Y coordinates
        :rtype: numpy.ndarray
        """
        return self._scatter.getAttribute('y', copy=copy).reshape(-1)

    def getZData(self, copy=True):
        """Returns Z data coordinates.

        :param bool copy: True to get a copy,
                          False to return internal array (do not modify!)
        :return: Z coordinates
        :rtype: numpy.ndarray
        """
        return self._scatter.getAttribute('z', copy=copy).reshape(-1)

    def getValueData(self, copy=True):
        """Returns data values.

        :param bool copy: True to get a copy,
                          False to return internal array (do not modify!)
        :return: data values
        :rtype: numpy.ndarray
        """
        return self._scatter.getAttribute('value', copy=copy).reshape(-1)

    @deprecated(reason="Consistency with PlotWidget items",
                replacement="getValueData", since_version="0.10.0")
    def getValues(self, copy=True):
        return self.getValueData(copy)

    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

        # Project data to NDC
        xData = self.getXData(copy=False)
        if len(xData) == 0:  # No data in the scatter
            return None

        primitive = self._getScenePrimitive()

        dataPoints = numpy.transpose((xData,
                                      self.getYData(copy=False),
                                      self.getZData(copy=False),
                                      numpy.ones_like(xData)))

        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 += self.getSymbolSize()
        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:
            return PickingResult(self,
                                 positions=dataPoints[picked, :3],
                                 indices=picked,
                                 fetchdata=self.getValueData)
        else:
            return None


class Scatter2D(DataItem3D, ColormapMixIn, SymbolMixIn,
                ScatterVisualizationMixIn):
    """2D scatter data with settable visualization mode.

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

    _VISUALIZATION_PROPERTIES = {
        ScatterVisualizationMixIn.Visualization.POINTS:
            ('symbol', 'symbolSize'),
        ScatterVisualizationMixIn.Visualization.LINES:
            ('lineWidth',),
        ScatterVisualizationMixIn.Visualization.SOLID: (),
    }
    """Dict {visualization mode: property names used in this mode}"""

    def __init__(self, parent=None):
        DataItem3D.__init__(self, parent=parent)
        ColormapMixIn.__init__(self)
        SymbolMixIn.__init__(self)
        ScatterVisualizationMixIn.__init__(self)

        self._heightMap = False
        self._lineWidth = 1.

        self._x = numpy.zeros((0,), dtype=numpy.float32)
        self._y = numpy.zeros((0,), dtype=numpy.float32)
        self._value = numpy.zeros((0,), dtype=numpy.float32)

        self._cachedLinesIndices = None
        self._cachedTrianglesIndices = None

        # Connect scene primitive to mix-in class
        ColormapMixIn._setSceneColormap(self, function.Colormap())

    def _updated(self, event=None):
        """Handle mix-in class updates"""
        if event in (ItemChangedType.SYMBOL, ItemChangedType.SYMBOL_SIZE):
            symbol, size = self._getSceneSymbol()
            for child in self._getScenePrimitive().children:
                if isinstance(child, primitives.Points):
                    child.marker = symbol
                    child.setAttribute('size', size, copy=True)

        elif event is ItemChangedType.VISIBLE:
            # TODO smart update?, need dirty flags
            self._updateScene()

        elif event is ItemChangedType.VISUALIZATION_MODE:
            self._updateScene()

        super(Scatter2D, self)._updated(event)

    def isPropertyEnabled(self, name, visualization=None):
        """Returns true if the property is used with visualization mode.

        :param str name: The name of the property to check, in:
                         'lineWidth', 'symbol', 'symbolSize'
        :param str visualization:
            The visualization mode for which to get the info.
            By default, it is the current visualization mode.
        :return:
        """
        assert name in ('lineWidth', 'symbol', 'symbolSize')
        if visualization is None:
            visualization = self.getVisualization()
        assert visualization in self.supportedVisualizations()
        return name in self._VISUALIZATION_PROPERTIES[visualization]

    def setHeightMap(self, heightMap):
        """Set whether to display the data has a height map or not.

        When displayed as a height map, the data values are used as
        z coordinates.

        :param bool heightMap:
            True to display a height map,
            False to display as 2D data with z=0
        """
        heightMap = bool(heightMap)
        if heightMap != self.isHeightMap():
            self._heightMap = heightMap
            self._updateScene()
            self._updated(Item3DChangedType.HEIGHT_MAP)

    def isHeightMap(self):
        """Returns True if data is displayed as a height map.

        :rtype: bool
        """
        return self._heightMap

    def getLineWidth(self):
        """Return the curve line width in pixels (float)"""
        return self._lineWidth

    def setLineWidth(self, width):
        """Set the width in pixel of the curve line

        See :meth:`getLineWidth`.

        :param float width: Width in pixels
        """
        width = float(width)
        assert width >= 1.
        if width != self._lineWidth:
            self._lineWidth = width
            for child in self._getScenePrimitive().children:
                if hasattr(child, 'lineWidth'):
                    child.lineWidth = width
            self._updated(ItemChangedType.LINE_WIDTH)

    def setData(self, x, y, value, copy=True):
        """Set the data represented by this item.

        Provided arrays must have the same length.

        :param numpy.ndarray x: X coordinates (array-like)
        :param numpy.ndarray y: Y coordinates (array-like)
        :param value: Points value: array-like or single scalar
        :param bool copy:
            True (default) to make a copy of the data,
            False to avoid copy if possible (do not modify the arrays).
        """
        x = numpy.array(
            x, copy=copy, dtype=numpy.float32, order='C').reshape(-1)
        y = numpy.array(
            y, copy=copy, dtype=numpy.float32, order='C').reshape(-1)
        assert len(x) == len(y)

        if isinstance(value, abc.Iterable):
            value = numpy.array(
                value, copy=copy, dtype=numpy.float32, order='C').reshape(-1)
            assert len(value) == len(x)
        else:  # Single scalar
            value = numpy.array((float(value),), dtype=numpy.float32)

        self._x = x
        self._y = y
        self._value = value

        # Reset cache
        self._cachedLinesIndices = None
        self._cachedTrianglesIndices = None

        # Store data range info
        ColormapMixIn._setRangeFromData(self, self.getValueData(copy=False))

        self._updateScene()

        self._updated(ItemChangedType.DATA)

    def getData(self, copy=True):
        """Returns data as provided to :meth:`setData`.

        :param bool copy: True to get a copy,
                          False to return internal data (do not modify!)
        :return: (x, y, value)
        """
        return (self.getXData(copy=copy),
                self.getYData(copy=copy),
                self.getValueData(copy=copy))

    def getXData(self, copy=True):
        """Returns X data coordinates.

        :param bool copy: True to get a copy,
                          False to return internal array (do not modify!)
        :return: X coordinates
        :rtype: numpy.ndarray
        """
        return numpy.array(self._x, copy=copy)

    def getYData(self, copy=True):
        """Returns Y data coordinates.

        :param bool copy: True to get a copy,
                          False to return internal array (do not modify!)
        :return: Y coordinates
        :rtype: numpy.ndarray
        """
        return numpy.array(self._y, copy=copy)

    def getValueData(self, copy=True):
        """Returns data values.

        :param bool copy: True to get a copy,
                          False to return internal array (do not modify!)
        :return: data values
        :rtype: numpy.ndarray
        """
        return numpy.array(self._value, copy=copy)

    @deprecated(reason="Consistency with PlotWidget items",
                replacement="getValueData", since_version="0.10.0")
    def getValues(self, copy=True):
        return self.getValueData(copy)

    def _pickPoints(self, context, points, threshold=1., sort='depth'):
        """Perform picking while in 'points' visualization mode

        :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

        # Project data to NDC
        primitive = self._getScenePrimitive()
        pointsNdc = primitive.objectToNDCTransform.transformPoints(
            points, perspectiveDivide=True)

        # Perform picking
        distancesNdc = numpy.abs(pointsNdc[:, :2] - rayNdc[0, :2])
        thresholdNdc = 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:
            return PickingResult(self,
                                 positions=points[picked, :3],
                                 indices=picked,
                                 fetchdata=self.getValueData)
        else:
            return None

    def _pickSolid(self, context, points):
        """Perform picking while in 'solid' visualization mode

        :param PickContext context: Current picking context
        """
        if self._cachedTrianglesIndices is None:
            _logger.info("Picking on Scatter2D before rendering")
            return None

        rayObject = context.getPickingSegment(frame=self._getScenePrimitive())
        if rayObject is None:  # No picking outside viewport
            return None
        rayObject = rayObject[:, :3]

        trianglesIndices = self._cachedTrianglesIndices.reshape(-1, 3)
        triangles = points[trianglesIndices, :3]
        selectedIndices, t, barycentric = glu.segmentTrianglesIntersection(
            rayObject, triangles)
        closest = numpy.argmax(barycentric, axis=1)

        indices = trianglesIndices.reshape(-1, 3)[selectedIndices, closest]

        if len(indices) == 0:  # No point is picked
            return None

        # Compute intersection points and get closest data point
        positions = t.reshape(-1, 1) * (rayObject[1] - rayObject[0]) + rayObject[0]

        return PickingResult(self,
                             positions=positions,
                             indices=indices,
                             fetchdata=self.getValueData)

    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]
        """
        xData = self.getXData(copy=False)
        if len(xData) == 0:  # No data in the scatter
            return None

        if self.isHeightMap():
            zData = self.getValueData(copy=False)
        else:
            zData = numpy.zeros_like(xData)

        points = numpy.transpose((xData,
                                  self.getYData(copy=False),
                                  zData,
                                  numpy.ones_like(xData)))

        mode = self.getVisualization()
        if mode is self.Visualization.POINTS:
            # TODO issue with symbol size: using pixel instead of points
            # Get "corrected" symbol size
            _, threshold = self._getSceneSymbol()
            return self._pickPoints(
                context, points, threshold=max(3., threshold))

        elif mode is self.Visualization.LINES:
            # Picking only at point
            return self._pickPoints(context, points, threshold=5.)

        else:  # mode == 'solid'
            return self._pickSolid(context, points)

    def _updateScene(self):
        self._getScenePrimitive().children = []  # Remove previous primitives

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

        x, y, value = self.getData(copy=False)
        if len(x) == 0:
            return  # Nothing to display

        mode = self.getVisualization()
        heightMap = self.isHeightMap()

        if mode is self.Visualization.POINTS:
            z = value if heightMap else 0.
            symbol, size = self._getSceneSymbol()
            primitive = primitives.Points(
                x=x, y=y, z=z, value=value,
                size=size,
                colormap=self._getSceneColormap())
            primitive.marker = symbol

        else:
            # TODO run delaunay in a thread
            # Compute lines/triangles indices if not cached
            if self._cachedTrianglesIndices is None:
                triangulation = delaunay(x, y)
                if triangulation is None:
                    return None
                self._cachedTrianglesIndices = numpy.ravel(
                    triangulation.simplices.astype(numpy.uint32))

            if (mode is self.Visualization.LINES and
                    self._cachedLinesIndices is None):
                # Compute line indices
                self._cachedLinesIndices = utils.triangleToLineIndices(
                    self._cachedTrianglesIndices, unicity=True)

            if mode is self.Visualization.LINES:
                indices = self._cachedLinesIndices
                renderMode = 'lines'
            else:
                indices = self._cachedTrianglesIndices
                renderMode = 'triangles'

            # TODO supports x, y instead of copy
            if heightMap:
                if len(value) == 1:
                    value = numpy.ones_like(x) * value
                coordinates = numpy.array((x, y, value), dtype=numpy.float32).T
            else:
                coordinates = numpy.array((x, y), dtype=numpy.float32).T

            # TODO option to enable/disable light, cache normals
            # TODO smooth surface
            if mode is self.Visualization.SOLID:
                if heightMap:
                    coordinates = coordinates[indices]
                    if len(value) > 1:
                        value = value[indices]
                    triangleNormals = utils.trianglesNormal(coordinates)
                    normal = numpy.empty((len(triangleNormals) * 3, 3),
                                         dtype=numpy.float32)
                    normal[0::3, :] = triangleNormals
                    normal[1::3, :] = triangleNormals
                    normal[2::3, :] = triangleNormals
                    indices = None
                else:
                    normal = (0., 0., 1.)
            else:
                normal = None

            primitive = primitives.ColormapMesh3D(
                coordinates,
                value.reshape(-1, 1),  # Makes it a 2D array
                normal=normal,
                colormap=self._getSceneColormap(),
                indices=indices,
                mode=renderMode)
            primitive.lineWidth = self.getLineWidth()
            primitive.lineSmooth = False

        self._getScenePrimitive().children = [primitive]