Skip to content

Commit e9d2223

Browse files
clemens-frickej042
authored andcommitted
chore: Type settings and vertices and beginning of show.
1 parent 886b651 commit e9d2223

File tree

3 files changed

+43
-26
lines changed

3 files changed

+43
-26
lines changed

gustaf/settings.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
33
Global variables/constants that's used throughout `gustaf`.
44
"""
55

6-
TOLERANCE = 1e-10
6+
from typing import Literal
77

8-
FLOAT_DTYPE = "float64"
9-
INT_DTYPE = "int32"
8+
TOLERANCE: float = 1e-10
9+
10+
FLOAT_DTYPE: Literal["float64"] = "float64"
11+
INT_DTYPE: Literal["int32"] = "int32"
1012

1113
# OPTIONS are <"vedo" | "trimesh" | "matplotlib">
12-
VISUALIZATION_BACKEND = "vedo"
14+
VISUALIZATION_BACKEND: Literal["vedo"] = "vedo"
1315

14-
VEDO_DEFAULT_OPTIONS = {
16+
VEDO_DEFAULT_OPTIONS: dict[str, dict] = {
1517
"vertex": {},
1618
"edges": {},
1719
"faces": {},
1820
"volumes": {},
1921
}
2022

21-
NTHREADS = 1
23+
NTHREADS: int = 1

gustaf/show.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
Everything related to show/visualization.
44
"""
55

6+
from __future__ import annotations
7+
68
import sys
9+
from typing import TYPE_CHECKING
710

811
import numpy as np
912

@@ -38,6 +41,11 @@
3841
vedo = ModuleImportRaiser("vedo", err)
3942
vedoUGrid = vedo
4043

44+
if TYPE_CHECKING:
45+
from typing import Any
46+
47+
from gustaf._base import GustafBase
48+
4149

4250
# enable `gus.show()`
4351
# taken from https://stackoverflow.com/questions/1060796/callable-modules
@@ -82,7 +90,9 @@ def show(*args, **kwargs):
8290
return_show_list = kwargs.get("return_showable_list", False)
8391
axes = kwargs.get("axes", None)
8492

85-
def clear_vedo_plotter(plotter, num_renderers, skip_cl=skip_clear):
93+
def clear_vedo_plotter(
94+
plotter: vedo.Plotter, num_renderers: int, skip_cl: bool = skip_clear
95+
):
8696
"""enough said."""
8797
# for whatever reason it is desired
8898
if skip_cl:
@@ -98,7 +108,7 @@ def clear_vedo_plotter(plotter, num_renderers, skip_cl=skip_clear):
98108

99109
return None
100110

101-
def cam_tuple_to_list(dict_cam):
111+
def cam_tuple_to_list(dict_cam: None | dict[str, Any]):
102112
"""if entity is tuple, turns it into list."""
103113
if dict_cam is None:
104114
return None
@@ -235,7 +245,9 @@ def cam_tuple_to_list(dict_cam):
235245
return plt
236246

237247

238-
def make_showable(obj, as_dict=False, **kwargs):
248+
# TODO the type of obj is not really easy to define since it in reality it
249+
# should be gustaf.Vertice but it might also be splinepy.Spline
250+
def make_showable(obj: GustafBase, as_dict: bool = False, **kwargs):
239251
"""Generates a vedo obj based on `kind` attribute from given obj, as well
240252
as show_options.
241253
@@ -411,7 +423,9 @@ def make_showable(obj, as_dict=False, **kwargs):
411423

412424
# possibly relocate, is this actually used?
413425
# could not find any usage in this repo
414-
def interpolate_vedo_dictcam(cameras, resolutions, spline_degree=1):
426+
def interpolate_vedo_dictcam(
427+
cameras: list[dict[str, Any]], resolutions: int, spline_degree: int = 1
428+
):
415429
"""Interpolate between vedo dict cameras.
416430
417431
Parameters

gustaf/vertices.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
from gustaf.helpers.options import Option as _Option
2121

2222
if TYPE_CHECKING:
23+
import sys
2324
from typing import Any
2425

25-
from gustaf.edges import EdgesShowOption as _EdgesShowOption
26-
from gustaf.faces import FacesShowOption as _FacesShowOption
26+
if sys.version_info >= (3, 10):
27+
from typing import Self
28+
else:
29+
from typing_extensions import Self
30+
2731
from gustaf.helpers.data import Unique2DFloats as _Unique2DFloats
28-
from gustaf.volumes import VolumesShowOption as _VolumesShowOption
2932

3033

3134
class VerticesShowOption(_helpers.options.ShowOption):
@@ -226,9 +229,7 @@ def vertex_data(self) -> _VertexData:
226229
return self._vertex_data
227230

228231
@property
229-
def show_options(
230-
self,
231-
) -> _EdgesShowOption | _FacesShowOption | _VolumesShowOption:
232+
def show_options(self) -> _helpers.options.ShowOption:
232233
"""
233234
Returns a show option manager for this object. Behaves similar to
234235
dict.
@@ -262,7 +263,7 @@ def whatami(self) -> str:
262263

263264
@_helpers.data.ComputedMeshData.depends_on(["vertices"])
264265
def unique_vertices(
265-
self, tolerance: Any | None = None, **kwargs: Any
266+
self, tolerance: float | None = None, **kwargs: Any
266267
) -> _Unique2DFloats:
267268
"""Returns a namedtuple that holds unique vertices info. Unique here
268269
means "close-enough-within-tolerance".
@@ -343,7 +344,7 @@ def bounds_diagonal_norm(self) -> float:
343344

344345
def update_vertices(
345346
self, mask: _np.ndarray, inverse: _np.ndarray | None = None
346-
) -> Any:
347+
) -> Vertices:
347348
"""Update vertices with a mask. In other words, keeps only masked
348349
vertices. Adapted from `github.com/mikedh/trimesh`. Updates
349350
connectivity accordingly too.
@@ -394,14 +395,14 @@ def update_vertices(
394395
vertices = vertices[mask]
395396

396397
def update_vertex_data(
397-
obj: Any, m: _np.ndarray, vertex_data: dict
398+
obj: Vertices, mask: _np.ndarray, vertex_data: dict
398399
) -> Any:
399400
"""apply mask to vertex data if there's any."""
400401
new_data = _helpers.data.VertexData(obj)
401402

402403
for key, values in vertex_data.items():
403404
# should work, since this is called after updating vertices
404-
new_data[key] = values[m]
405+
new_data[key] = values[mask]
405406

406407
obj._vertex_data = new_data
407408

@@ -434,7 +435,7 @@ def select_vertices(self, ranges: list[list[float]]) -> _np.ndarray:
434435
"""
435436
return _utils.arr.select_with_ranges(self.vertices, ranges)
436437

437-
def remove_vertices(self, ids):
438+
def remove_vertices(self, ids: _np.ndarray) -> Vertices:
438439
"""Removes vertices with given vertex ids.
439440
440441
Parameters
@@ -451,8 +452,8 @@ def remove_vertices(self, ids):
451452
return self.update_vertices(mask)
452453

453454
def merge_vertices(
454-
self, tolerance: Any | None = None, **kwargs: Any
455-
) -> Any:
455+
self, tolerance: float | None = None, **kwargs: Any
456+
) -> Vertices:
456457
"""Based on unique vertices, merge vertices if it is mergeable.
457458
458459
Parameters
@@ -503,7 +504,7 @@ def show(self, **kwargs):
503504
"""
504505
return _show.show(self, **kwargs)
505506

506-
def copy(self) -> Any:
507+
def copy(self) -> Vertices:
507508
"""Returns deepcopy of self.
508509
509510
Parameters
@@ -525,7 +526,7 @@ def copy(self) -> Any:
525526
return copied
526527

527528
@classmethod
528-
def concat(cls, *instances: Any) -> Any:
529+
def concat(cls, *instances: Vertices) -> Vertices:
529530
"""Sequentially put them together to make one object.
530531
531532
Parameters
@@ -590,7 +591,7 @@ def is_concatable(inst: Any) -> bool:
590591
else:
591592
return Vertices(vertices=_np.vstack(vertices))
592593

593-
def __add__(self, to_add):
594+
def __add__(self, to_add: Self) -> Self:
594595
"""Concat in form of +.
595596
596597
Parameters

0 commit comments

Comments
 (0)