|
13 | 13 |
|
14 | 14 | import math |
15 | 15 | import numpy as np |
| 16 | +from collections.abc import Iterable |
16 | 17 |
|
17 | 18 | # from spatialmath.base import symbolic as sym # HACK |
18 | 19 | from spatialmath.base.symbolic import issymbol, symtype |
@@ -371,6 +372,8 @@ def getvector( |
371 | 372 | >>> getvector(1) # scalar |
372 | 373 | >>> getvector([1]) |
373 | 374 | >>> getvector([[1]]) |
| 375 | + >>> getvector([1,2], 2) |
| 376 | + >>> # getvector([1,2], 3) --> ValueError |
374 | 377 |
|
375 | 378 | .. note:: |
376 | 379 | - For 'array', 'row' or 'col' output the NumPy dtype defaults to the |
@@ -519,68 +522,62 @@ def isvector(v: Any, dim: Optional[int] = None) -> bool: |
519 | 522 | return False |
520 | 523 |
|
521 | 524 |
|
522 | | -@overload |
523 | | -def getunit(v: float, unit: str = "rad") -> float: # pragma: no cover |
524 | | - ... |
525 | | - |
526 | | - |
527 | | -@overload |
528 | | -def getunit(v: NDArray, unit: str = "rad") -> NDArray: # pragma: no cover |
529 | | - ... |
530 | | - |
531 | | - |
532 | | -@overload |
533 | | -def getunit(v: List[float], unit: str = "rad") -> List[float]: # pragma: no cover |
534 | | - ... |
535 | | - |
536 | | - |
537 | | -@overload |
538 | | -def getunit(v: Tuple[float, ...], unit: str = "rad") -> List[float]: # pragma: no cover |
539 | | - ... |
540 | | - |
541 | | - |
542 | | -def getunit( |
543 | | - v: Union[float, NDArray, Tuple[float, ...], List[float]], unit: str = "rad" |
544 | | -) -> Union[float, NDArray, List[float]]: |
| 525 | +def getunit(v: ArrayLike, unit: str = "rad", dim=None) -> Union[float, NDArray]: |
545 | 526 | """ |
546 | | - Convert value according to angular units |
| 527 | + Convert values according to angular units |
547 | 528 |
|
548 | 529 | :param v: the value in radians or degrees |
549 | | - :type v: array_like(m) or ndarray(m) |
| 530 | + :type v: array_like(m) |
550 | 531 | :param unit: the angular unit, "rad" or "deg" |
551 | 532 | :type unit: str |
| 533 | + :param dim: expected dimension of input, defaults to None |
| 534 | + :type dim: int, optional |
552 | 535 | :return: the converted value in radians |
553 | | - :rtype: list(m) or ndarray(m) |
| 536 | + :rtype: ndarray(m) or float |
554 | 537 | :raises ValueError: argument is not a valid angular unit |
555 | 538 |
|
556 | | - The input can be a list or ndarray() and the output is the same type. |
| 539 | + The input value is assumed to be in units of ``unit`` and is converted to radians. |
557 | 540 |
|
558 | 541 | .. runblock:: pycon |
559 | 542 |
|
560 | 543 | >>> from spatialmath.base import getunit |
561 | 544 | >>> import numpy as np |
562 | 545 | >>> getunit(1.5, 'rad') |
| 546 | + >>> getunit(1.5, 'rad', dim=0) |
| 547 | + >>> # getunit([1.5], 'rad', dim=0) --> ValueError |
563 | 548 | >>> getunit(90, 'deg') |
564 | 549 | >>> getunit([90, 180], 'deg') |
565 | 550 | >>> getunit(np.r_[0.5, 1], 'rad') |
566 | 551 | >>> getunit(np.r_[90, 180], 'deg') |
| 552 | + >>> getunit(np.r_[90, 180], 'deg', dim=2) |
| 553 | + >>> # getunit([90, 180], 'deg', dim=3) --> ValueError |
| 554 | +
|
| 555 | + :note: |
| 556 | + - the input value is processed by :func:`getvector` and the argument ``dim`` can |
| 557 | + be used to check that ``v`` is the desired length. |
| 558 | + - the output is always an ndarray except if the input is a scalar and ``dim=0``. |
| 559 | +
|
| 560 | + :seealso: :func:`getvector` |
567 | 561 | """ |
568 | | - if unit == "rad": |
569 | | - if isinstance(v, tuple): |
570 | | - return list(v) |
571 | | - else: |
| 562 | + if not isinstance(v, Iterable) and dim == 0: |
| 563 | + # scalar in, scalar out |
| 564 | + if unit == "rad": |
572 | 565 | return v |
573 | | - elif unit == "deg": |
574 | | - if isinstance(v, np.ndarray) or np.isscalar(v): |
575 | | - return v * math.pi / 180 # type: ignore |
576 | | - elif isinstance(v, (list, tuple)): |
577 | | - return [x * math.pi / 180 for x in v] |
| 566 | + elif unit == "deg": |
| 567 | + return np.deg2rad(v) |
578 | 568 | else: |
579 | | - raise ValueError("bad argument") |
580 | | - else: |
581 | | - raise ValueError("invalid angular units") |
| 569 | + raise ValueError("invalid angular units") |
582 | 570 |
|
583 | | - return ret |
| 571 | + else: |
| 572 | + # scalar or iterable in, ndarray out |
| 573 | + # iterable passed in |
| 574 | + v = getvector(v, dim=dim) |
| 575 | + if unit == "rad": |
| 576 | + return v |
| 577 | + elif unit == "deg": |
| 578 | + return np.deg2rad(v) |
| 579 | + else: |
| 580 | + raise ValueError("invalid angular units") |
584 | 581 |
|
585 | 582 |
|
586 | 583 | def isnumberlist(x: Any) -> bool: |
|
0 commit comments