Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * MARTINS.js Free Edition
  3. * GPU-accelerated Augmented Reality for the web
  4. * Copyright (C) 2022 Alexandre Martins <alemartf(at)gmail.com>
  5. * https://github.com/alemart/martins-js
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License version 3
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. * utils.ts
  20. * Generic utilities
  21. */
  22. import { AssertionError, IllegalArgumentError } from './errors';
  23. import { Resolution, computeResolution } from '../core/resolution';
  24. import { SpeedySize } from 'speedy-vision/types/core/speedy-size';
  25. /**
  26. * Nullable type
  27. */
  28. export type Nullable<T> = T | null;
  29. /**
  30. * Generic utilities
  31. */
  32. export class Utils
  33. {
  34. /**
  35. * Log a message
  36. * @param message
  37. * @param args optional additional messages
  38. */
  39. static log(message: string, ...args: string[]): void
  40. {
  41. console.log('[martins-js]', message, ...args);
  42. }
  43. /**
  44. * Display a warning
  45. * @param message
  46. * @param args optional additional messages
  47. */
  48. static warning(message: string, ...args: string[]): void
  49. {
  50. console.warn('[martins-js]', message, ...args);
  51. }
  52. /**
  53. * Display an error message
  54. * @param message
  55. * @param args optional additional messages
  56. */
  57. static error(message: string, ...args: string[]): void
  58. {
  59. console.error('[martins-js]', message, ...args);
  60. }
  61. /**
  62. * Assertion
  63. * @param expr expression
  64. * @param errorMessage optional error message
  65. * @throws {AssertionError}
  66. */
  67. static assert(expr: boolean, errorMessage: string = ''): void
  68. {
  69. if(!expr)
  70. throw new AssertionError(errorMessage);
  71. }
  72. /**
  73. * Returns a range [0, 1, ..., n-1]
  74. * @param n non-negative integer
  75. * @returns range from 0 to n-1, inclusive
  76. */
  77. static range(n: number): number[]
  78. {
  79. if((n |= 0) < 0)
  80. throw new IllegalArgumentError();
  81. return Array.from({ length: n }, (_, i) => i);
  82. }
  83. /**
  84. * Convert a resolution type to a resolution measured in pixels
  85. * @param resolution resolution type
  86. * @param aspectRatio width / height ratio
  87. * @returns resolution measured in pixels
  88. */
  89. static resolution(resolution: Resolution, aspectRatio: number): SpeedySize
  90. {
  91. return computeResolution(resolution, aspectRatio);
  92. }
  93. }