1
0

Rect.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /******************************************************************************
  2. Copyright (C) 2025 by Patrick Heyer <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #pragma once
  15. #include <algorithm>
  16. #include <limits>
  17. namespace {
  18. template<typename numberType> inline double safeConvertToDouble(numberType number)
  19. {
  20. constexpr double minDoubleValue = 0.0;
  21. constexpr double maxDoubleValue = std::numeric_limits<double>::max();
  22. if (number > maxDoubleValue) {
  23. return maxDoubleValue;
  24. } else if (number < minDoubleValue) {
  25. return minDoubleValue;
  26. } else {
  27. return static_cast<double>(number);
  28. }
  29. }
  30. } // namespace
  31. namespace OBS {
  32. struct Rect {
  33. double width_ = 0.0;
  34. double height_ = 0.0;
  35. Rect() = default;
  36. template<typename widthType, typename heightType> Rect(widthType width, heightType height)
  37. {
  38. setWidth(width);
  39. setHeight(height);
  40. }
  41. template<typename returnType> returnType getWidth() const
  42. {
  43. static_assert(std::is_convertible_v<double, returnType>,
  44. "Cannot convert <double> to requested return type");
  45. return static_cast<returnType>(width_);
  46. };
  47. template<typename numberType> void setWidth(numberType width)
  48. {
  49. static_assert(std::is_same_v<numberType, double> || std::is_convertible_v<numberType, double>,
  50. "Cannot convert provided argument to <double>");
  51. if (std::is_same<numberType, double>::value) {
  52. width_ = std::max(static_cast<double>(width), 0.0);
  53. } else {
  54. width_ = safeConvertToDouble(width);
  55. }
  56. }
  57. template<typename returnType> returnType getHeight() const
  58. {
  59. static_assert(std::is_convertible_v<double, returnType>,
  60. "Cannot convert <double> to requested return type");
  61. return static_cast<returnType>(height_);
  62. };
  63. template<typename numberType> void setHeight(numberType height)
  64. {
  65. static_assert(std::is_same_v<numberType, double> || std::is_convertible_v<numberType, double>,
  66. "Cannot convert provided argument to <double>");
  67. if (std::is_same<numberType, double>::value) {
  68. height_ = std::max(static_cast<double>(height), 0.0);
  69. } else {
  70. height_ = safeConvertToDouble(height);
  71. }
  72. }
  73. bool isZero() const;
  74. friend bool operator==(const Rect &lhs, const Rect &rhs);
  75. friend bool operator!=(const Rect &lhs, const Rect &rhs);
  76. };
  77. } // namespace OBS