Skip to content

napari utilities

data_to_world_normal(vector, layer)

Convert a normal vector defining an orientation from data coordinates to world coordinates. For example, this would be used to a plane normal.

https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/geometry/transforming-normals.html

Parameters

vector : tuple, list, 1D array A vector in data coordinates. layer : napari.layers.BaseLayer The napari layer to get the transform from.

Returns

np.ndarray Transformed vector in data coordinates. This returns a unit vector.

Source code in src/napari_threedee/utils/napari_utils.py
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
def data_to_world_normal(vector, layer):
    """Convert a normal vector defining an orientation from data coordinates to world coordinates.
    For example, this would be used to a plane normal.

    https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/geometry/transforming-normals.html

    Parameters
    ----------
    vector : tuple, list, 1D array
        A vector in data coordinates.
    layer : napari.layers.BaseLayer
        The napari layer to get the transform from.

    Returns
    -------
    np.ndarray
        Transformed vector in data coordinates. This returns a unit vector.
    """
    unit_vector = np.asarray(vector) / np.linalg.norm(vector)

    # get the transform
    inverse_transform = layer._transforms[1:].simplified.inverse.linear_matrix
    transpose_inverse_transform = inverse_transform.T

    # transform the vector
    transformed_vector = np.matmul(transpose_inverse_transform, unit_vector)

    return transformed_vector / np.linalg.norm(transformed_vector)

data_to_world_ray(vector, layer)

Convert a vector defining an orientation from data coordinates to world coordinates. For example, this would be used to convert the view ray.

Parameters

vector : tuple, list, 1D array A vector in data coordinates. layer : napari.layers.BaseLayer The napari layer to get the transform from.

Returns

np.ndarray Transformed vector in data coordinates.

Source code in src/napari_threedee/utils/napari_utils.py
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
def data_to_world_ray(vector, layer):
    """Convert a vector defining an orientation from data coordinates to world coordinates.
    For example, this would be used to convert the view ray.

    Parameters
    ----------
    vector : tuple, list, 1D array
        A vector in data coordinates.
    layer : napari.layers.BaseLayer
        The napari layer to get the transform from.

    Returns
    -------
    np.ndarray
        Transformed vector in data coordinates.
    """
    p1 = np.asarray(layer.data_to_world(vector))
    p0 = np.asarray(layer.data_to_world(np.zeros_like(vector)))
    normalized_vector = (p1 - p0) / np.linalg.norm(p1 - p0)

    return normalized_vector

get_mouse_position_in_displayed_dimensions(event)

Get the position under the mouse in scene (displayed world) coordinates.

Parameters

event The mouse event.

Returns

click_dir_data_3d : np.ndarray The click direction in displayed data coordiantes

Source code in src/napari_threedee/utils/napari_utils.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
def get_mouse_position_in_displayed_dimensions(event) -> np.ndarray:
    """Get the position under the mouse in scene (displayed world) coordinates.

    Parameters
    ----------
    event
        The mouse event.

    Returns
    -------
    click_dir_data_3d : np.ndarray
        The click direction in displayed data coordiantes
    """
    click_position_world = event.position
    return np.asarray(click_position_world)[list(event.dims_displayed)]

get_mouse_position_in_displayed_layer_data_coordinates(layer, event)

Get the mouse click position and direction in layer data displayed coordinates.

Parameters

layer : napari.layers.Layer The layer to convert the coordinates to. event The mouse event.

Returns

click_position_data_3d : np.ndarray The click position in displayed data coordinates. click_dir_data_3d : np.ndarray The click direction in displayed data coordiantes

Source code in src/napari_threedee/utils/napari_utils.py
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
def get_mouse_position_in_displayed_layer_data_coordinates(layer, event) -> Tuple[np.ndarray, np.ndarray]:
    """Get the mouse click position and direction in layer data displayed coordinates.

    Parameters
    ----------
    layer : napari.layers.Layer
        The layer to convert the coordinates to.
    event
        The mouse event.

    Returns
    -------
    click_position_data_3d : np.ndarray
        The click position in displayed data coordinates.
    click_dir_data_3d : np.ndarray
        The click direction in displayed data coordiantes
    """
    click_position_world = event.position
    click_position_data_3d = np.asarray(
        layer._world_to_displayed_data(
            click_position_world,
            event.dims_displayed
        )
    )
    click_dir_data_3d = np.asarray(
        layer._world_to_displayed_data_ray(
            event.view_direction,
            event.dims_displayed
        )
    )

    return click_position_data_3d, click_dir_data_3d

get_napari_visual(viewer, layer)

Get the visual class for a given layer

Parameters

viewer The napari viewer object layer The napari layer object for which to find the visual.

Returns

visual The napari visual class for the layer.

Source code in src/napari_threedee/utils/napari_utils.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def get_napari_visual(viewer, layer):
    """Get the visual class for a given layer

    Parameters
    ----------
    viewer
        The napari viewer object
    layer
        The napari layer object for which to find the visual.

    Returns
    -------
    visual
        The napari visual class for the layer.
    """
    visual = viewer.window._qt_window._qt_viewer.layer_to_visual[layer]

    return visual

get_view_direction_in_displayed_dimensions(event)

Get the view direction under the mouse in scene (displayed world) coordinates.

Parameters

event: Event napari mouse event.

Source code in src/napari_threedee/utils/napari_utils.py
119
120
121
122
123
124
125
126
127
128
def get_view_direction_in_displayed_dimensions(event) -> np.ndarray:
    """Get the view direction under the mouse in scene (displayed world) coordinates.

    Parameters
    ----------
    event: Event
        napari mouse event.
    """
    view_direction_world = event.view_direction
    return np.asarray(view_direction_world)[list(event.dims_displayed)]

get_vispy_layer_node(viewer, layer)

Get the vispy node associated with a layer

Source code in src/napari_threedee/utils/napari_utils.py
43
44
45
46
47
48
49
50
def get_vispy_layer_node(viewer: napari.Viewer, layer):
    """Get the vispy node associated with a layer"""
    napari_visual = get_napari_visual(viewer, layer)

    if isinstance(layer, Image):
        return napari_visual._layer_node.get_node(3)
    elif isinstance(layer, Points):
        return napari_visual.node

get_vispy_root_node(viewer, layer)

Get the vispy node at the root of the scene graph.

This is the node that layers are added to.

Source code in src/napari_threedee/utils/napari_utils.py
53
54
55
56
57
58
59
60
61
def get_vispy_root_node(viewer: napari.Viewer, layer):
    """Get the vispy node at the root of the scene graph.

    This is the node that layers are added to.
    """
    # this will need to be updated in napari 0.5.0
    # viewer.window._qt_window._qt_viewer.canvas.view.scene
    qt_viewer = viewer.window._qt_window._qt_viewer
    return qt_viewer.view.scene

world_to_data_normal(vector, layer)

Convert a normal vector defining an orientation from world coordinates to data coordinates. For example, this would be used to a plane normal.

https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/geometry/transforming-normals.html

Parameters

vector : tuple, list, 1D array A vector in world coordinates. layer : napari.layers.BaseLayer The napari layer to get the transform from.

Returns

np.ndarray Transformed vector in data coordinates. This returns a unit vector.

Source code in src/napari_threedee/utils/napari_utils.py
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
def world_to_data_normal(vector, layer):
    """Convert a normal vector defining an orientation from world coordinates to data coordinates.
    For example, this would be used to a plane normal.

    https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/geometry/transforming-normals.html

    Parameters
    ----------
    vector : tuple, list, 1D array
        A vector in world coordinates.
    layer : napari.layers.BaseLayer
        The napari layer to get the transform from.

    Returns
    -------
    np.ndarray
        Transformed vector in data coordinates. This returns a unit vector.
    """
    unit_vector = np.asarray(vector) / np.linalg.norm(vector)

    # get the transform
    # the napari transform is from layer -> world.
    # We want the inverse of the world ->  layer, so we just take the napari transform
    inverse_transform = layer._transforms[1:].simplified.linear_matrix
    transpose_inverse_transform = inverse_transform.T

    # transform the vector
    transformed_vector = np.matmul(transpose_inverse_transform, unit_vector)

    return transformed_vector / np.linalg.norm(transformed_vector)