summaryrefslogtreecommitdiff
path: root/silx/gui/plot3d/scene/axes.py
blob: e35e5e1b9f6f06bc1d075fa914450d954db493de (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
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2018 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.
#
# ###########################################################################*/
"""Primitive displaying a text field in the scene."""

from __future__ import absolute_import, division, unicode_literals

__authors__ = ["T. Vincent"]
__license__ = "MIT"
__date__ = "17/10/2016"


import logging
import numpy

from ...plot._utils import ticklayout

from . import core, primitives, text, transform


_logger = logging.getLogger(__name__)


class LabelledAxes(primitives.GroupBBox):
    """A group displaying a bounding box with axes labels around its children.
    """

    def __init__(self):
        super(LabelledAxes, self).__init__()
        self._ticksForBounds = None

        self._font = text.Font()

        self._boxVisibility = True

        # TODO offset labels from anchor in pixels

        self._xlabel = text.Text2D(font=self._font)
        self._xlabel.align = 'center'
        self._xlabel.transforms = [self._boxTransforms,
                                   transform.Translate(tx=0.5)]
        self._children.insert(-1, self._xlabel)

        self._ylabel = text.Text2D(font=self._font)
        self._ylabel.align = 'center'
        self._ylabel.transforms = [self._boxTransforms,
                                   transform.Translate(ty=0.5)]
        self._children.insert(-1, self._ylabel)

        self._zlabel = text.Text2D(font=self._font)
        self._zlabel.align = 'center'
        self._zlabel.transforms = [self._boxTransforms,
                                   transform.Translate(tz=0.5)]
        self._children.insert(-1, self._zlabel)

        # Init tick lines with dummy pos
        self._tickLines = primitives.DashedLines(
            positions=((0., 0., 0.), (0., 0., 0.)))
        self._tickLines.dash = 5, 10
        self._tickLines.visible = False
        self._children.insert(-1, self._tickLines)

        self._tickLabels = core.Group()
        self._children.insert(-1, self._tickLabels)

        # Sync color
        self.tickColor = 1., 1., 1., 1.

    def _updateBoxAndAxes(self):
        """Update bbox and axes position and size according to children.

        Overridden from GroupBBox
        """
        super(LabelledAxes, self)._updateBoxAndAxes()

        bounds = self._group.bounds(dataBounds=True)
        if bounds is not None:
            tx, ty, tz = (bounds[1] - bounds[0]) / 2.
        else:
            tx, ty, tz = 0.5, 0.5, 0.5

        self._xlabel.transforms[-1].tx = tx
        self._ylabel.transforms[-1].ty = ty
        self._zlabel.transforms[-1].tz = tz

    @property
    def tickColor(self):
        """Color of ticks and text labels.

        This does NOT set bounding box color.
        Use :attr:`color` for the bounding box.
        """
        return self._xlabel.foreground

    @tickColor.setter
    def tickColor(self, color):
        self._xlabel.foreground = color
        self._ylabel.foreground = color
        self._zlabel.foreground = color
        transparentColor = color[0], color[1], color[2], color[3] * 0.6
        self._tickLines.setAttribute('color', transparentColor)
        for label in self._tickLabels.children:
            label.foreground = color

    @property
    def font(self):
        """Font of axes text labels (Font)"""
        return self._font

    @font.setter
    def font(self, font):
        self._font = font
        self._xlabel.font = font
        self._ylabel.font = font
        self._zlabel.font = font
        for label in self._tickLabels.children:
            label.font = font

    @property
    def xlabel(self):
        """Text label of the X axis (str)"""
        return self._xlabel.text

    @xlabel.setter
    def xlabel(self, text):
        self._xlabel.text = text

    @property
    def ylabel(self):
        """Text label of the Y axis (str)"""
        return self._ylabel.text

    @ylabel.setter
    def ylabel(self, text):
        self._ylabel.text = text

    @property
    def zlabel(self):
        """Text label of the Z axis (str)"""
        return self._zlabel.text

    @zlabel.setter
    def zlabel(self, text):
        self._zlabel.text = text

    @property
    def boxVisible(self):
        """Returns bounding box, axes labels and grid visibility."""
        return self._boxVisibility

    @boxVisible.setter
    def boxVisible(self, visible):
        self._boxVisibility = bool(visible)
        for child in self._children:
            if child == self._tickLines:
                if self._ticksForBounds is not None:
                    child.visible = self._boxVisibility
            elif child != self._group:
                child.visible = self._boxVisibility

    def _updateTicks(self):
        """Check if ticks need update and update them if needed."""
        bounds = self._group.bounds(transformed=False, dataBounds=True)
        if bounds is None:  # No content
            if self._ticksForBounds is not None:
                self._ticksForBounds = None
                self._tickLines.visible = False
                self._tickLabels.children = []  # Reset previous labels

        elif (self._ticksForBounds is None or
                not numpy.all(numpy.equal(bounds, self._ticksForBounds))):
            self._ticksForBounds = bounds

            # Update ticks
            ticklength = numpy.abs(bounds[1] - bounds[0])

            xticks, xlabels = ticklayout.ticks(*bounds[:, 0])
            yticks, ylabels = ticklayout.ticks(*bounds[:, 1])
            zticks, zlabels = ticklayout.ticks(*bounds[:, 2])

            # Update tick lines
            coords = numpy.empty(
                ((len(xticks) + len(yticks) + len(zticks)), 4, 3),
                dtype=numpy.float32)
            coords[:, :, :] = bounds[0, :]  # account for offset from origin

            xcoords = coords[:len(xticks)]
            xcoords[:, :, 0] = numpy.asarray(xticks)[:, numpy.newaxis]
            xcoords[:, 1, 1] += ticklength[1]  # X ticks on XY plane
            xcoords[:, 3, 2] += ticklength[2]  # X ticks on XZ plane

            ycoords = coords[len(xticks):len(xticks) + len(yticks)]
            ycoords[:, :, 1] = numpy.asarray(yticks)[:, numpy.newaxis]
            ycoords[:, 1, 0] += ticklength[0]  # Y ticks on XY plane
            ycoords[:, 3, 2] += ticklength[2]  # Y ticks on YZ plane

            zcoords = coords[len(xticks) + len(yticks):]
            zcoords[:, :, 2] = numpy.asarray(zticks)[:, numpy.newaxis]
            zcoords[:, 1, 0] += ticklength[0]  # Z ticks on XZ plane
            zcoords[:, 3, 1] += ticklength[1]  # Z ticks on YZ plane

            self._tickLines.setPositions(coords.reshape(-1, 3))
            self._tickLines.visible = self._boxVisibility

            # Update labels
            color = self.tickColor
            offsets = bounds[0] - ticklength / 20.
            labels = []
            for tick, label in zip(xticks, xlabels):
                text2d = text.Text2D(text=label, font=self.font)
                text2d.align = 'center'
                text2d.foreground = color
                text2d.transforms = [transform.Translate(
                    tx=tick, ty=offsets[1], tz=offsets[2])]
                labels.append(text2d)

            for tick, label in zip(yticks, ylabels):
                text2d = text.Text2D(text=label, font=self.font)
                text2d.align = 'center'
                text2d.foreground = color
                text2d.transforms = [transform.Translate(
                    tx=offsets[0], ty=tick, tz=offsets[2])]
                labels.append(text2d)

            for tick, label in zip(zticks, zlabels):
                text2d = text.Text2D(text=label, font=self.font)
                text2d.align = 'center'
                text2d.foreground = color
                text2d.transforms = [transform.Translate(
                    tx=offsets[0], ty=offsets[1], tz=tick)]
                labels.append(text2d)

            self._tickLabels.children = labels  # Reset previous labels

    def prepareGL2(self, context):
        self._updateTicks()
        super(LabelledAxes, self).prepareGL2(context)