Skip to content

napari utilities

clamp_point_to_layer_bounding_box(point, layer)

Ensure that a point is inside of the bounding box of a given layer. If the point has a coordinate outside of the bounding box, the value is clipped to the max extent of the bounding box.

Parameters

point : np.ndarray n-dimensional point as an (n,) ndarray. Multiple points can be passed as an (n, D) array. layer : napari.layers.Layer napari layer to get the bounding box from

Returns

clamped_point : np.ndarray point clamped to the limits of the layer bounding box

Notes

This function is derived from the napari function: napari.utils.geometry.clamp_point_to_bounding_box

Source code in src/napari_threedee/utils/napari_utils.py
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
def clamp_point_to_layer_bounding_box(point: np.ndarray, layer):
    """Ensure that a point is inside of the bounding box of a given layer. 
    If the point has a coordinate outside of the bounding box, the value 
    is clipped to the max extent of the bounding box.

    Parameters
    ----------
    point : np.ndarray
        n-dimensional point as an (n,) ndarray. Multiple points can
        be passed as an (n, D) array.
    layer : napari.layers.Layer
        napari layer to get the bounding box from

    Returns
    -------
    clamped_point : np.ndarray
        `point` clamped to the limits of the layer bounding box

    Notes
    -----
    This function is derived from the napari function:
        `napari.utils.geometry.clamp_point_to_bounding_box`
    """
    dims_displayed = get_dims_displayed(layer)
    bbox = layer._display_bounding_box(dims_displayed)
    clamped_point = np.clip(point, bbox[:, 0], bbox[:, 1] - 1)
    return clamped_point

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
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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
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
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
123
124
125
126
127
128
129
130
131
132
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
47
48
49
50
51
52
53
54
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
57
58
59
60
61
62
63
64
65
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

point_in_layer_bounding_box(point, layer)

Determine whether an nD point is inside a layers nD bounding box.

Parameters

point : np.ndarray (n,) array containing nD point coordinates to check. layer : napari.layers.Layer napari layer to get the bounding box from

Returns

bool True if the point is in the bounding box of the layer, otherwise False

Notes

For a more general point-in-bbox function, see: napari_threedee.utils.geometry.point_in_bounding_box

Source code in src/napari_threedee/utils/napari_utils.py
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
def point_in_layer_bounding_box(point, layer):
    """Determine whether an nD point is inside a layers nD bounding box.

    Parameters
    ----------
    point : np.ndarray
        (n,) array containing nD point coordinates to check.
    layer : napari.layers.Layer
        napari layer to get the bounding box from

    Returns
    -------
    bool
        True if the point is in the bounding box of the layer,
        otherwise False

    Notes
    -----
    For a more general point-in-bbox function, see:
        `napari_threedee.utils.geometry.point_in_bounding_box`
    """
    dims_displayed = get_dims_displayed(layer)
    bbox = layer._display_bounding_box(dims_displayed).T
    if np.any(point < bbox[0]) or np.any(point > bbox[1]):
        return False
    else:
        return True

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
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
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)