💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.0 KiB

  1. /*
  2. * Copyright (C) 2020 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code 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 General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_MATH_HPP
  20. #define ANTKEEPER_MATH_HPP
  21. #include <cstdlib>
  22. #include <type_traits>
  23. #include <vmq/vmq.hpp>
  24. using namespace vmq::operators;
  25. inline float frand(float start, float end)
  26. {
  27. float f = (float)std::rand() / RAND_MAX;
  28. return start + f * (end - start);
  29. }
  30. namespace math {
  31. /**
  32. * Reinterprets data as an `N`-dimensional vector of type `T`.
  33. *
  34. * @tparam N Number of vector dimensions.
  35. * @tparam T Scalar type.
  36. * @param data Data to reinterpret.
  37. */
  38. template <std::size_t N, typename T>
  39. inline vmq::vector<T, N>& as_vector(T& data)
  40. {
  41. static_assert(std::is_pod<vmq::vector<T, N>>::value);
  42. return reinterpret_cast<vmq::vector<T, N>&>(data);
  43. }
  44. /**
  45. * Reinterprets data as an `NxM` matrix of type `T`.
  46. *
  47. * @tparam N Number of columns.
  48. * @tparam M Number of rows.
  49. * @tparam T Element type.
  50. * @param data Data to reinterpret.
  51. */
  52. template <std::size_t N, std::size_t M, typename T>
  53. inline vmq::matrix<T, N, M>& as_matrix(T& data)
  54. {
  55. static_assert(std::is_pod<vmq::matrix<T, N, M>>::value);
  56. return reinterpret_cast<vmq::matrix<T, N, M>&>(data);
  57. }
  58. template <class T>
  59. vmq::vector<T, 3> project_on_plane(const vmq::vector<T, 3>& v, const vmq::vector<T, 3>& p, const vmq::vector<T, 3>& n)
  60. {
  61. return v - n * vmq::dot(v - p, n);
  62. }
  63. } // namespace math
  64. #endif // ANTKEEPER_MATH_HPP