💿🐜 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.

1577 lines
41 KiB

  1. /*
  2. * Copyright (C) 2021 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_VECTOR_HPP
  20. #define ANTKEEPER_MATH_VECTOR_HPP
  21. #include <algorithm>
  22. #include <cstddef>
  23. #include <cmath>
  24. #include <istream>
  25. #include <iterator>
  26. #include <ostream>
  27. #include <type_traits>
  28. #include <utility>
  29. namespace math {
  30. /**
  31. * *n*-dimensional vector.
  32. *
  33. * @tparam T Element type.
  34. * @tparam N Number of elements.
  35. */
  36. template <typename T, std::size_t N>
  37. struct vector
  38. {
  39. /// Vector element data type.
  40. typedef T element_type;
  41. /// Number of vector elements.
  42. static constexpr std::size_t element_count = N;
  43. /// Array of vector elements.
  44. element_type elements[N];
  45. /**
  46. * Returns a reference to the element at a given index.
  47. *
  48. * @param i Index of an element.
  49. *
  50. * @return Reference to the element at index @p i.
  51. */
  52. /// @{
  53. constexpr inline element_type& operator[](std::size_t i) noexcept
  54. {
  55. return elements[i];
  56. }
  57. constexpr inline const element_type& operator[](std::size_t i) const noexcept
  58. {
  59. return elements[i];
  60. }
  61. /// @}
  62. /// Returns a reference to the first element.
  63. /// @{
  64. constexpr inline element_type& front() noexcept
  65. {
  66. return elements[0];
  67. }
  68. constexpr inline const element_type& front() const noexcept
  69. {
  70. return elements[0];
  71. }
  72. /// @}
  73. /// Returns a reference to the last element.
  74. /// @{
  75. constexpr inline element_type& back() noexcept
  76. {
  77. return elements[N - 1];
  78. }
  79. constexpr inline const element_type& back() const noexcept
  80. {
  81. return elements[N - 1];
  82. }
  83. /// @}
  84. /// Returns a reference to the first element.
  85. /// @{
  86. constexpr inline element_type& x() noexcept
  87. {
  88. static_assert(N > 0, "Vector does not contain an x element.");
  89. return elements[0];
  90. }
  91. constexpr inline const element_type& x() const noexcept
  92. {
  93. static_assert(N > 0, "Vector does not contain an x element.");
  94. return elements[0];
  95. }
  96. /// @}
  97. /// Returns a reference to the second element.
  98. /// @{
  99. constexpr inline element_type& y() noexcept
  100. {
  101. static_assert(N > 1, "Vector does not contain a y element.");
  102. return elements[1];
  103. }
  104. constexpr inline const element_type& y() const noexcept
  105. {
  106. static_assert(N > 1, "Vector does not contain a y element.");
  107. return elements[1];
  108. }
  109. /// @}
  110. /// Returns a reference to the third element.
  111. /// @{
  112. constexpr inline element_type& z() noexcept
  113. {
  114. static_assert(N > 2, "Vector does not contain a z element.");
  115. return elements[2];
  116. }
  117. constexpr inline const element_type& z() const noexcept
  118. {
  119. static_assert(N > 2, "Vector does not contain a z element.");
  120. return elements[2];
  121. }
  122. /// @}
  123. /// Returns a pointer to the element array.
  124. /// @{
  125. constexpr inline element_type* data() noexcept
  126. {
  127. return elements;
  128. };
  129. constexpr inline const element_type* data() const noexcept
  130. {
  131. return elements;
  132. };
  133. /// @}
  134. /// Returns an iterator to the first element.
  135. /// @{
  136. constexpr inline element_type* begin() noexcept
  137. {
  138. return elements;
  139. }
  140. constexpr inline const element_type* begin() const noexcept
  141. {
  142. return elements;
  143. }
  144. constexpr inline const element_type* cbegin() const noexcept
  145. {
  146. return elements;
  147. }
  148. /// @}
  149. /// Returns an iterator to the element following the last element.
  150. /// @{
  151. constexpr inline element_type* end() noexcept
  152. {
  153. return elements + N;
  154. }
  155. constexpr inline const element_type* end() const noexcept
  156. {
  157. return elements + N;
  158. }
  159. constexpr inline const element_type* cend() const noexcept
  160. {
  161. return elements + N;
  162. }
  163. /// @}
  164. /// Returns a reverse iterator to the first element of the reversed vector.
  165. /// @{
  166. constexpr inline std::reverse_iterator<element_type*> rbegin() noexcept
  167. {
  168. return std::reverse_iterator<element_type*>(elements + N);
  169. }
  170. constexpr inline std::reverse_iterator<const element_type*> rbegin() const noexcept
  171. {
  172. return std::reverse_iterator<const element_type*>(elements + N);
  173. }
  174. constexpr inline std::reverse_iterator<const element_type*> crbegin() const noexcept
  175. {
  176. return std::reverse_iterator<const element_type*>(elements + N);
  177. }
  178. /// @}
  179. /// Returns a reverse iterator to the element following the last element of the reversed vector.
  180. /// @{
  181. constexpr inline std::reverse_iterator<element_type*> rend() noexcept
  182. {
  183. return std::reverse_iterator<element_type*>(elements);
  184. }
  185. constexpr inline std::reverse_iterator<const element_type*> rend() const noexcept
  186. {
  187. return std::reverse_iterator<const element_type*>(elements);
  188. }
  189. constexpr inline std::reverse_iterator<const element_type*> crend() const noexcept
  190. {
  191. return std::reverse_iterator<const element_type*>(elements);
  192. }
  193. /// @}
  194. /// Returns the number of elements in the vector.
  195. constexpr inline std::size_t size() const noexcept
  196. {
  197. return N;
  198. };
  199. /// @private
  200. template <class U, std::size_t... I>
  201. constexpr inline vector<U, N> type_cast(std::index_sequence<I...>) const noexcept
  202. {
  203. return {static_cast<U>(elements[I])...};
  204. }
  205. /**
  206. * Type-casts the elements of this vector using `static_cast`.
  207. *
  208. * @tparam U Target element type.
  209. *
  210. * @return Vector containing the type-casted elements.
  211. */
  212. template <class U>
  213. constexpr inline explicit operator vector<U, N>() const noexcept
  214. {
  215. return type_cast<U>(std::make_index_sequence<N>{});
  216. }
  217. /// @private
  218. template <std::size_t M, std::size_t... I>
  219. constexpr inline vector<T, M> size_cast(std::index_sequence<I...>) const noexcept
  220. {
  221. return {(I < N) ? elements[I] : T{0} ...};
  222. }
  223. /**
  224. * Size-casts this vector to a vector with a different number of elements. Casting to a greater number of elements causes new elements to be set to zero.
  225. *
  226. * @tparam M Target number of elements.
  227. *
  228. * @return *m*-dimensional vector.
  229. */
  230. template <std::size_t M>
  231. constexpr inline explicit operator vector<T, M>() const noexcept
  232. {
  233. return size_cast<M>(std::make_index_sequence<M>{});
  234. }
  235. /// Returns a zero vector, where every element is equal to zero.
  236. static constexpr vector zero() noexcept
  237. {
  238. return {};
  239. }
  240. /// @private
  241. template <std::size_t... I>
  242. static constexpr vector one(std::index_sequence<I...>) noexcept
  243. {
  244. //return {T{1}...};
  245. // MSVC bug workaround (I must be referenced for parameter pack expansion)
  246. return {(I ? T{1} : T{1})...};
  247. }
  248. /// Returns a vector of ones, where every element is equal to one.
  249. static constexpr vector one() noexcept
  250. {
  251. return one(std::make_index_sequence<N>{});
  252. }
  253. };
  254. /// Vector with two elements.
  255. template <typename T>
  256. using vector2 = vector<T, 2>;
  257. /// Vector with three elements.
  258. template <typename T>
  259. using vector3 = vector<T, 3>;
  260. /// Vector with four elements.
  261. template <typename T>
  262. using vector4 = vector<T, 4>;
  263. /**
  264. * Returns the absolute values of each element.
  265. *
  266. * @param x Input vector.
  267. *
  268. * @return Absolute values of input vector elements.
  269. */
  270. template <class T, std::size_t N>
  271. constexpr vector<T, N> abs(const vector<T, N>& x);
  272. /**
  273. * Adds two values.
  274. *
  275. * @param x First value.
  276. * @param y Second value.
  277. *
  278. * @return Sum of the two values.
  279. */
  280. /// @{
  281. template <class T, std::size_t N>
  282. constexpr vector<T, N> add(const vector<T, N>& x, const vector<T, N>& y) noexcept;
  283. template <class T, std::size_t N>
  284. constexpr vector<T, N> add(const vector<T, N>& x, T y) noexcept;
  285. /// @}
  286. /**
  287. * Checks if all elements of a boolean vector are `true`.
  288. *
  289. * @param x Vector to be tested for truth.
  290. *
  291. * @return `true` if all elements are `true`, `false` otherwise.
  292. */
  293. template <std::size_t N>
  294. constexpr bool all(const vector<bool, N>& x) noexcept;
  295. /**
  296. * Checks if any elements of a boolean vector are `true`.
  297. *
  298. * @param x Vector to be tested for truth.
  299. *
  300. * @return `true` if any elements are `true`, `false` otherwise.
  301. */
  302. template <std::size_t N>
  303. constexpr bool any(const vector<bool, N>& x) noexcept;
  304. /**
  305. * Performs a element-wise ceil operation.
  306. *
  307. * @param x Input vector.
  308. *
  309. * @return Component-wise ceil of input vector.
  310. */
  311. template <class T, std::size_t N>
  312. constexpr vector<T, N> ceil(const vector<T, N>& x);
  313. /**
  314. * Clamps the values of a vector's elements.
  315. *
  316. * @param x Vector to clamp.
  317. * @param min Minimum value.
  318. * @param max Maximum value.
  319. *
  320. * @return Clamped vector.
  321. */
  322. /// @{
  323. template <class T, std::size_t N>
  324. constexpr vector<T, N> clamp(const vector<T, N>& x, const vector<T, N>& min, const vector<T, N>& max);
  325. template <class T, std::size_t N>
  326. constexpr vector<T, N> clamp(const vector<T, N>& x, T min, T max);
  327. /// @}
  328. /**
  329. * Clamps the length of a vector.
  330. *
  331. * @param x Vector to clamp.
  332. * @param max_length Maximum length.
  333. *
  334. * @return Length-clamped vector.
  335. */
  336. template <class T, std::size_t N>
  337. vector<T, N> clamp_length(const vector<T, N>& x, T max_length);
  338. /**
  339. * Calculate the cross product of two vectors.
  340. *
  341. * @param x First vector.
  342. * @param y Second vector.
  343. *
  344. * @return Cross product of the two vectors.
  345. */
  346. template <class T>
  347. constexpr vector<T, 3> cross(const vector<T, 3>& x, const vector<T, 3>& y) noexcept;
  348. /**
  349. * Calculates the distance between two points.
  350. *
  351. * @param p0 First of two points.
  352. * @param p1 Second of two points.
  353. *
  354. * @return Distance between the two points.
  355. */
  356. template <class T, std::size_t N>
  357. T distance(const vector<T, N>& p0, const vector<T, N>& p1);
  358. /**
  359. * Calculates the squared distance between two points. The squared distance can be calculated faster than the distance because a call to `std::sqrt` is saved.
  360. *
  361. * @param p0 First of two points.
  362. * @param p1 Second of two points.
  363. *
  364. * @return Squared distance between the two points.
  365. */
  366. template <class T, std::size_t N>
  367. constexpr T distance_squared(const vector<T, N>& p0, const vector<T, N>& p1) noexcept;
  368. /**
  369. * Divides a vector by a value.
  370. *
  371. * @param x First value.
  372. * @param y Second value.
  373. *
  374. * @return Result of the division.
  375. */
  376. /// @{
  377. template <class T, std::size_t N>
  378. constexpr vector<T, N> div(const vector<T, N>& x, const vector<T, N>& y) noexcept;
  379. template <class T, std::size_t N>
  380. constexpr vector<T, N> div(const vector<T, N>& x, T y) noexcept;
  381. template <class T, std::size_t N>
  382. constexpr vector<T, N> div(T x, const vector<T, N>& y) noexcept;
  383. /// @}
  384. /**
  385. * Calculates the dot product of two vectors.
  386. *
  387. * @param x First vector.
  388. * @param y Second vector.
  389. *
  390. * @return Dot product of the two vectors.
  391. */
  392. template <class T, std::size_t N>
  393. constexpr T dot(const vector<T, N>& x, const vector<T, N>& y) noexcept;
  394. /**
  395. * Compares two vectors for equality
  396. *
  397. * @param x First vector.
  398. * @param y Second vector.
  399. *
  400. * @return Boolean vector containing the result of the element comparisons.
  401. */
  402. template <class T, std::size_t N>
  403. constexpr vector<bool, N> equal(const vector<T, N>& x, const vector<T, N>& y) noexcept;
  404. /**
  405. * Performs a element-wise floor operation.
  406. *
  407. * @param x Input vector.
  408. *
  409. * @return Component-wise floor of input vector.
  410. */
  411. template <class T, std::size_t N>
  412. constexpr vector<T, N> floor(const vector<T, N>& x);
  413. /**
  414. * Performs a multiply-add operation.
  415. *
  416. * @param x Input vector
  417. * @param y Value to multiply.
  418. * @param z Value to add.
  419. *
  420. * @return Vector containing the multiply-add results.
  421. */
  422. /// @{
  423. template <class T, std::size_t N>
  424. constexpr vector<T, N> fma(const vector<T, N>& x, const vector<T, N>& y, const vector<T, N>& z);
  425. template <class T, std::size_t N>
  426. constexpr vector<T, N> fma(const vector<T, N>& x, T y, T z);
  427. /// @}
  428. /**
  429. * Returns a vector containing the fractional part of each element.
  430. *
  431. * @param x Input vector.
  432. *
  433. * @return Fractional parts of input vector.
  434. */
  435. template <class T, std::size_t N>
  436. constexpr vector<T, N> fract(const vector<T, N>& x);
  437. /**
  438. * Performs a element-wise greater-than comparison of two vectors.
  439. *
  440. * @param x First vector.
  441. * @param y Second vector.
  442. *
  443. * @return Boolean vector containing the result of the element comparisons.
  444. */
  445. template <class T, std::size_t N>
  446. constexpr vector<bool, N> greater_than(const vector<T, N>& x, const vector<T, N>& y) noexcept;
  447. /**
  448. * Performs a element-wise greater-than or equal-to comparison of two vectors.
  449. *
  450. * @param x First vector.
  451. * @param y Second vector.
  452. *
  453. * @return Boolean vector containing the result of the element comparisons.
  454. */
  455. template <class T, std::size_t N>
  456. constexpr vector<bool, N> greater_than_equal(const vector<T, N>& x, const vector<T, N>& y) noexcept;
  457. /**
  458. * Calculates the length of a vector.
  459. *
  460. * @param x Vector of which to calculate the length.
  461. *
  462. * @return Length of the vector.
  463. */
  464. template <class T, std::size_t N>
  465. T length(const vector<T, N>& x);
  466. /**
  467. * Calculates the squared length of a vector. The squared length can be calculated faster than the length because a call to `std::sqrt` is saved.
  468. *
  469. * @param x Vector of which to calculate the squared length.
  470. *
  471. * @return Squared length of the vector.
  472. */
  473. template <class T, std::size_t N>
  474. constexpr T length_squared(const vector<T, N>& x) noexcept;
  475. /**
  476. * Performs a element-wise less-than comparison of two vectors.
  477. *
  478. * @param x First vector.
  479. * @param y Second vector.
  480. *
  481. * @return Boolean vector containing the result of the element comparisons.
  482. */
  483. template <class T, std::size_t N>
  484. constexpr vector<bool, N> less_than(const vector<T, N>& x, const vector<T, N>& y) noexcept;
  485. /**
  486. * Performs a element-wise less-than or equal-to comparison of two vectors.
  487. *
  488. * @param x First vector.
  489. * @param y Second vector.
  490. *
  491. * @return Boolean vector containing the result of the element comparisons.
  492. */
  493. template <class T, std::size_t N>
  494. constexpr vector<bool, N> less_than_equal(const vector<T, N>& x, const vector<T, N>& y) noexcept;
  495. /**
  496. * Returns a vector containing the maximum elements of two vectors.
  497. *
  498. * @param x First vector.
  499. * @param y Second vector.
  500. *
  501. * @return Maximum elements of the two vectors.
  502. */
  503. template <class T, std::size_t N>
  504. constexpr vector<T, N> max(const vector<T, N>& x, const vector<T, N>& y);
  505. /**
  506. * Returns the value of the greatest element in a vector.
  507. *
  508. * @param x Input vector.
  509. *
  510. * @return Value of the greatest element in the input vector.
  511. */
  512. template <class T, std::size_t N>
  513. constexpr T max(const vector<T, N>& x);
  514. /**
  515. * Returns a vector containing the minimum elements of two vectors.
  516. *
  517. * @param x First vector.
  518. * @param y Second vector.
  519. *
  520. * @return Minimum elements of the two vectors.
  521. */
  522. template <class T, std::size_t N>
  523. constexpr vector<T, N> min(const vector<T, N>& x, const vector<T, N>& y);
  524. /**
  525. * Returns the value of the smallest element in a vector.
  526. *
  527. * @param x Input vector.
  528. *
  529. * @return Value of the smallest element in the input vector.
  530. */
  531. template <class T, std::size_t N>
  532. constexpr T min(const vector<T, N>& x);
  533. /**
  534. * Calculates the element-wise remainder of the division operation `x / y`.
  535. *
  536. * @param x First vector.
  537. * @param y Second vector.
  538. *
  539. * @return Remainders of `x / y`.
  540. */
  541. /// @{
  542. template <class T, std::size_t N>
  543. constexpr vector<T, N> mod(const vector<T, N>& x, const vector<T, N>& y);
  544. template <class T, std::size_t N>
  545. constexpr vector<T, N> mod(const vector<T, N>& x, T y);
  546. /// @}
  547. /**
  548. * Multiplies two values.
  549. *
  550. * @param x First value.
  551. * @param y Second value.
  552. *
  553. * @return Product of the two values.
  554. */
  555. /// @{
  556. template <class T, std::size_t N>
  557. constexpr vector<T, N> mul(const vector<T, N>& x, const vector<T, N>& y) noexcept;
  558. template <class T, std::size_t N>
  559. constexpr vector<T, N> mul(const vector<T, N>& x, T y) noexcept;
  560. /// @}
  561. /**
  562. * Negates a vector.
  563. *
  564. * @param x Vector to negate.
  565. *
  566. * @return Negated vector.
  567. */
  568. template <class T, std::size_t N>
  569. constexpr vector<T, N> negate(const vector<T, N>& x) noexcept;
  570. /**
  571. * Calculates the unit vector in the same direction as the original vector.
  572. *
  573. * @param x Vector to normalize.
  574. *
  575. * @return Normalized vector.
  576. */
  577. template <class T, std::size_t N>
  578. vector<T, N> normalize(const vector<T, N>& x);
  579. /**
  580. * Logically inverts a boolean vector.
  581. *
  582. * @param x Vector to be inverted.
  583. *
  584. * @return Logically inverted vector.
  585. */
  586. template <class T, std::size_t N>
  587. constexpr vector<bool, N> not(const vector<T, N>& x) noexcept;
  588. /**
  589. * Compares two vectors for inequality
  590. *
  591. * @param x First vector.
  592. * @param y Second vector.
  593. *
  594. * @return Boolean vector containing the result of the element comparisons.
  595. */
  596. template <class T, std::size_t N>
  597. constexpr vector<bool, N> not_equal(const vector<T, N>& x, const vector<T, N>& y) noexcept;
  598. /**
  599. * Raises each element to a power.
  600. *
  601. * @param x Input vector
  602. * @param y Exponent.
  603. *
  604. * @return Vector with its elements raised to the given power.
  605. */
  606. /// @{
  607. template <class T, std::size_t N>
  608. vector<T, N> pow(const vector<T, N>& x, const vector<T, N>& y);
  609. template <class T, std::size_t N>
  610. vector<T, N> pow(const vector<T, N>& x, T y);
  611. /// @}
  612. /**
  613. * Performs a element-wise round operation.
  614. *
  615. * @param x Input vector
  616. *
  617. * @return Component-wise round of input vector.
  618. */
  619. template <class T, std::size_t N>
  620. constexpr vector<T, N> round(const vector<T, N>& x);
  621. /**
  622. * Returns a vector containing the signs of each element.
  623. *
  624. * @param x Input vector
  625. * @return Signs of input vector elements.
  626. */
  627. template <class T, std::size_t N>
  628. constexpr vector<T, N> sign(const vector<T, N>& x);
  629. /**
  630. * Takes the square root of each element.
  631. *
  632. * @param x Input vector
  633. *
  634. * @return Square roots of the input vector elements.
  635. */
  636. template <class T, std::size_t N>
  637. vector<T, N> sqrt(const vector<T, N>& x);
  638. /**
  639. * Subtracts a value by another value.
  640. *
  641. * @param x First value.
  642. * @param y Second value.
  643. *
  644. * @return Difference between the two values.
  645. */
  646. /// @{
  647. template <class T, std::size_t N>
  648. constexpr vector<T, N> sub(const vector<T, N>& x, const vector<T, N>& y) noexcept;
  649. template <class T, std::size_t N>
  650. constexpr vector<T, N> sub(const vector<T, N>& x, T y) noexcept;
  651. template <class T, std::size_t N>
  652. constexpr vector<T, N> sub(T x, const vector<T, N>& y) noexcept;
  653. /// @}
  654. /**
  655. * Calculates the sum of all elements in a vector.
  656. *
  657. * @param x Vector to sum.
  658. * @return Sum of the vector's elements.
  659. */
  660. template <class T, std::size_t N>
  661. constexpr T sum(const vector<T, N>& x) noexcept;
  662. /**
  663. * Makes an *m*-dimensional vector by rearranging and/or duplicating elements of an *n*-dimensional vector.
  664. *
  665. * @tparam Indices List of indices of elements in @p x.
  666. * @tparam T Vector element type.
  667. * @tparam N Number of dimensions in @p x.
  668. *
  669. * @param x Vector to swizzle.
  670. *
  671. * @return Vector containing elements from @p x in the order specified by @p Indices. The size of the returned vector is equivalent to the number of indices in @p Indices.
  672. */
  673. template <std::size_t... Indices, class T, std::size_t N>
  674. constexpr vector<T, sizeof...(Indices)> swizzle(const vector<T, N>& x) noexcept;
  675. /**
  676. * Performs a element-wise trunc operation.
  677. *
  678. * @param x Input vector
  679. * @return Component-wise trunc of input vector.
  680. */
  681. template <class T, std::size_t N>
  682. constexpr vector<T, N> trunc(const vector<T, N>& x);
  683. /// @private
  684. template <class T, std::size_t N, std::size_t... I>
  685. constexpr inline vector<T, N> abs(const vector<T, N>& x, std::index_sequence<I...>)
  686. {
  687. return {std::abs(x[I])...};
  688. }
  689. template <class T, std::size_t N>
  690. constexpr inline vector<T, N> abs(const vector<T, N>& x)
  691. {
  692. return abs(x, std::make_index_sequence<N>{});
  693. }
  694. /// @private
  695. template <class T, std::size_t N, std::size_t... I>
  696. constexpr inline vector<T, N> add(const vector<T, N>& x, const vector<T, N>& y, std::index_sequence<I...>) noexcept
  697. {
  698. return {(x[I] + y[I])...};
  699. }
  700. template <class T, std::size_t N>
  701. constexpr inline vector<T, N> add(const vector<T, N>& x, const vector<T, N>& y) noexcept
  702. {
  703. return add(x, y, std::make_index_sequence<N>{});
  704. }
  705. /// @private
  706. template <class T, std::size_t N, std::size_t... I>
  707. constexpr inline vector<T, N> add(const vector<T, N>& x, T y, std::index_sequence<I...>) noexcept
  708. {
  709. return {(x[I] + y)...};
  710. }
  711. template <class T, std::size_t N>
  712. constexpr inline vector<T, N> add(const vector<T, N>& x, T y) noexcept
  713. {
  714. return add(x, y, std::make_index_sequence<N>{});
  715. }
  716. /// @private
  717. template <std::size_t N, std::size_t... I>
  718. constexpr inline bool all(const vector<bool, N>& x, std::index_sequence<I...>) noexcept
  719. {
  720. return (x[I] && ...);
  721. }
  722. template <std::size_t N>
  723. constexpr inline bool all(const vector<bool, N>& x) noexcept
  724. {
  725. return all(x, std::make_index_sequence<N>{});
  726. }
  727. /// @private
  728. template <std::size_t N, std::size_t... I>
  729. constexpr inline bool any(const vector<bool, N>& x, std::index_sequence<I...>) noexcept
  730. {
  731. return (x[I] || ...);
  732. }
  733. template <std::size_t N>
  734. constexpr inline bool any(const vector<bool, N>& x) noexcept
  735. {
  736. return any(x, std::make_index_sequence<N>{});
  737. }
  738. /// @private
  739. template <class T, std::size_t N, std::size_t... I>
  740. constexpr inline vector<T, N> ceil(const vector<T, N>& x, std::index_sequence<I...>)
  741. {
  742. return {std::ceil(x[I])...};
  743. }
  744. template <class T, std::size_t N>
  745. constexpr inline vector<T, N> ceil(const vector<T, N>& x)
  746. {
  747. static_assert(std::is_floating_point<T>::value);
  748. return ceil(x, std::make_index_sequence<N>{});
  749. }
  750. /// @private
  751. template <class T, std::size_t N, std::size_t... I>
  752. constexpr inline vector<T, N> clamp(const vector<T, N>& x, const vector<T, N>& min_val, const vector<T, N>& max_val, std::index_sequence<I...>)
  753. {
  754. return {std::min<T>(max_val[I], std::max<T>(min_val[I], x[I]))...};
  755. }
  756. template <class T, std::size_t N>
  757. constexpr inline vector<T, N> clamp(const vector<T, N>& x, const vector<T, N>& min, const vector<T, N>& max)
  758. {
  759. return clamp(x, min, max, std::make_index_sequence<N>{});
  760. }
  761. /// @private
  762. template <class T, std::size_t N, std::size_t... I>
  763. constexpr inline vector<T, N> clamp(const vector<T, N>& x, T min, T max, std::index_sequence<I...>)
  764. {
  765. return {std::min<T>(max, std::max<T>(min, x[I]))...};
  766. }
  767. template <class T, std::size_t N>
  768. constexpr inline vector<T, N> clamp(const vector<T, N>& x, T min, T max)
  769. {
  770. return clamp(x, min, max, std::make_index_sequence<N>{});
  771. }
  772. template <class T, std::size_t N>
  773. vector<T, N> clamp_length(const vector<T, N>& x, T max_length)
  774. {
  775. static_assert(std::is_floating_point<T>::value);
  776. T length2 = length_squared(x);
  777. return (length2 > max_length * max_length) ? (x * max_length / std::sqrt(length2)) : x;
  778. }
  779. template <class T>
  780. constexpr inline vector<T, 3> cross(const vector<T, 3>& x, const vector<T, 3>& y) noexcept
  781. {
  782. return
  783. {
  784. x[1] * y[2] - y[1] * x[2],
  785. x[2] * y[0] - y[2] * x[0],
  786. x[0] * y[1] - y[0] * x[1]
  787. };
  788. }
  789. template <class T, std::size_t N>
  790. inline T distance(const vector<T, N>& p0, const vector<T, N>& p1)
  791. {
  792. static_assert(std::is_floating_point<T>::value);
  793. return length(sub(p0, p1));
  794. }
  795. template <class T, std::size_t N>
  796. constexpr inline T distance_squared(const vector<T, N>& p0, const vector<T, N>& p1) noexcept
  797. {
  798. return length_squared(sub(p0, p1));
  799. }
  800. /// @private
  801. template <class T, std::size_t N, std::size_t... I>
  802. constexpr inline vector<T, N> div(const vector<T, N>& x, const vector<T, N>& y, std::index_sequence<I...>) noexcept
  803. {
  804. return {(x[I] / y[I])...};
  805. }
  806. template <class T, std::size_t N>
  807. constexpr inline vector<T, N> div(const vector<T, N>& x, const vector<T, N>& y) noexcept
  808. {
  809. return div(x, y, std::make_index_sequence<N>{});
  810. }
  811. /// @private
  812. template <class T, std::size_t N, std::size_t... I>
  813. constexpr inline vector<T, N> div(const vector<T, N>& x, T y, std::index_sequence<I...>) noexcept
  814. {
  815. return {(x[I] / y)...};
  816. }
  817. template <class T, std::size_t N>
  818. constexpr inline vector<T, N> div(const vector<T, N>& x, T y) noexcept
  819. {
  820. return div(x, y, std::make_index_sequence<N>{});
  821. }
  822. /// @private
  823. template <class T, std::size_t N, std::size_t... I>
  824. constexpr inline vector<T, N> div(T x, const vector<T, N>& y, std::index_sequence<I...>) noexcept
  825. {
  826. return {(x / y[I])...};
  827. }
  828. template <class T, std::size_t N>
  829. constexpr inline vector<T, N> div(T x, const vector<T, N>& y) noexcept
  830. {
  831. return div(x, y, std::make_index_sequence<N>{});
  832. }
  833. /// @private
  834. template <class T, std::size_t N, std::size_t... I>
  835. constexpr inline T dot(const vector<T, N>& x, const vector<T, N>& y, std::index_sequence<I...>) noexcept
  836. {
  837. return ((x[I] * y[I]) + ...);
  838. }
  839. template <class T, std::size_t N>
  840. constexpr inline T dot(const vector<T, N>& x, const vector<T, N>& y) noexcept
  841. {
  842. return dot(x, y, std::make_index_sequence<N>{});
  843. }
  844. /// @private
  845. template <class T, std::size_t N, std::size_t... I>
  846. constexpr inline vector<bool, N> equal(const vector<T, N>& x, const vector<T, N>& y, std::index_sequence<I...>) noexcept
  847. {
  848. return {(x[I] == y[I])...};
  849. }
  850. template <class T, std::size_t N>
  851. constexpr inline vector<bool, N> equal(const vector<T, N>& x, const vector<T, N>& y) noexcept
  852. {
  853. return equal(x, y, std::make_index_sequence<N>{});
  854. }
  855. /// @private
  856. template <class T, std::size_t N, std::size_t... I>
  857. constexpr inline vector<T, N> floor(const vector<T, N>& x, std::index_sequence<I...>)
  858. {
  859. return {std::floor(x[I])...};
  860. }
  861. template <class T, std::size_t N>
  862. constexpr inline vector<T, N> floor(const vector<T, N>& x)
  863. {
  864. static_assert(std::is_floating_point<T>::value);
  865. return floor(x, std::make_index_sequence<N>{});
  866. }
  867. /// @private
  868. template <class T, std::size_t N, std::size_t... I>
  869. constexpr inline vector<T, N> fma(const vector<T, N>& x, const vector<T, N>& y, const vector<T, N>& z, std::index_sequence<I...>)
  870. {
  871. return {std::fma(x[I], y[I], z[I])...};
  872. }
  873. template <class T, std::size_t N>
  874. constexpr inline vector<T, N> fma(const vector<T, N>& x, const vector<T, N>& y, const vector<T, N>& z)
  875. {
  876. static_assert(std::is_floating_point<T>::value);
  877. return fma(x, y, z, std::make_index_sequence<N>{});
  878. }
  879. /// @private
  880. template <class T, std::size_t N, std::size_t... I>
  881. constexpr inline vector<T, N> fma(const vector<T, N>& x, T y, T z, std::index_sequence<I...>)
  882. {
  883. return {std::fma(x[I], y, z)...};
  884. }
  885. template <class T, std::size_t N>
  886. constexpr inline vector<T, N> fma(const vector<T, N>& x, T y, T z)
  887. {
  888. static_assert(std::is_floating_point<T>::value);
  889. return fma(x, y, z, std::make_index_sequence<N>{});
  890. }
  891. /// @private
  892. template <class T, std::size_t N, std::size_t... I>
  893. constexpr inline vector<T, N> fract(const vector<T, N>& x, std::index_sequence<I...>)
  894. {
  895. return {x[I] - std::floor(x[I])...};
  896. }
  897. template <class T, std::size_t N>
  898. constexpr inline vector<T, N> fract(const vector<T, N>& x)
  899. {
  900. static_assert(std::is_floating_point<T>::value);
  901. return fract(x, std::make_index_sequence<N>{});
  902. }
  903. /// @private
  904. template <class T, std::size_t N, std::size_t... I>
  905. constexpr inline vector<bool, N> greater_than(const vector<T, N>& x, const vector<T, N>& y, std::index_sequence<I...>) noexcept
  906. {
  907. return {(x[I] > y[I])...};
  908. }
  909. template <class T, std::size_t N>
  910. constexpr inline vector<bool, N> greater_than(const vector<T, N>& x, const vector<T, N>& y) noexcept
  911. {
  912. return greater_than(x, y, std::make_index_sequence<N>{});
  913. }
  914. /// @private
  915. template <class T, std::size_t N, std::size_t... I>
  916. constexpr inline vector<bool, N> greater_than_equal(const vector<T, N>& x, const vector<T, N>& y, std::index_sequence<I...>) noexcept
  917. {
  918. return {(x[I] >= y[I])...};
  919. }
  920. template <class T, std::size_t N>
  921. constexpr inline vector<bool, N> greater_than_equal(const vector<T, N>& x, const vector<T, N>& y) noexcept
  922. {
  923. return greater_than_equal(x, y, std::make_index_sequence<N>{});
  924. }
  925. template <class T, std::size_t N>
  926. inline T length(const vector<T, N>& x)
  927. {
  928. static_assert(std::is_floating_point<T>::value);
  929. return std::sqrt(dot(x, x));
  930. }
  931. template <class T, std::size_t N>
  932. constexpr inline T length_squared(const vector<T, N>& x) noexcept
  933. {
  934. return dot(x, x);
  935. }
  936. /// @private
  937. template <class T, std::size_t N, std::size_t... I>
  938. constexpr inline vector<bool, N> less_than(const vector<T, N>& x, const vector<T, N>& y, std::index_sequence<I...>) noexcept
  939. {
  940. return {(x[I] < y[I])...};
  941. }
  942. template <class T, std::size_t N>
  943. constexpr inline vector<bool, N> less_than(const vector<T, N>& x, const vector<T, N>& y) noexcept
  944. {
  945. return less_than(x, y, std::make_index_sequence<N>{});
  946. }
  947. /// @private
  948. template <class T, std::size_t N, std::size_t... I>
  949. constexpr inline vector<bool, N> less_than_equal(const vector<T, N>& x, const vector<T, N>& y, std::index_sequence<I...>) noexcept
  950. {
  951. return {(x[I] <= y[I])...};
  952. }
  953. template <class T, std::size_t N>
  954. constexpr inline vector<bool, N> less_than_equal(const vector<T, N>& x, const vector<T, N>& y) noexcept
  955. {
  956. return less_than_equal(x, y, std::make_index_sequence<N>{});
  957. }
  958. /// @private
  959. template <class T, std::size_t N, std::size_t... I>
  960. constexpr inline vector<T, N> max(const vector<T, N>& x, const vector<T, N>& y, std::index_sequence<I...>)
  961. {
  962. return {std::max<T>(x[I], y[I])...};
  963. }
  964. template <class T, std::size_t N>
  965. constexpr vector<T, N> max(const vector<T, N>& x, const vector<T, N>& y)
  966. {
  967. return max(x, y, std::make_index_sequence<N>{});
  968. }
  969. template <class T, std::size_t N>
  970. constexpr inline T max(const vector<T, N>& x)
  971. {
  972. return *std::max_element(x.elements, x.elements + N);
  973. }
  974. /// @private
  975. template <class T, std::size_t N, std::size_t... I>
  976. constexpr inline vector<T, N> min(const vector<T, N>& x, const vector<T, N>& y, std::index_sequence<I...>)
  977. {
  978. return {std::min<T>(x[I], y[I])...};
  979. }
  980. template <class T, std::size_t N>
  981. constexpr vector<T, N> min(const vector<T, N>& x, const vector<T, N>& y)
  982. {
  983. return min(x, y, std::make_index_sequence<N>{});
  984. }
  985. template <class T, std::size_t N>
  986. constexpr inline T min(const vector<T, N>& x)
  987. {
  988. return *std::min_element(x.elements, x.elements + N);
  989. }
  990. /// @private
  991. template <class T, std::size_t N, std::size_t... I>
  992. constexpr inline vector<T, N> mod(const vector<T, N>& x, const vector<T, N>& y, std::index_sequence<I...>)
  993. {
  994. return {std::fmod(x[I], y[I])...};
  995. }
  996. template <class T, std::size_t N>
  997. constexpr inline vector<T, N> mod(const vector<T, N>& x, const vector<T, N>& y)
  998. {
  999. static_assert(std::is_floating_point<T>::value);
  1000. return mod(x, y, std::make_index_sequence<N>{});
  1001. }
  1002. /// @private
  1003. template <class T, std::size_t N, std::size_t... I>
  1004. constexpr inline vector<T, N> mod(const vector<T, N>& x, T y, std::index_sequence<I...>)
  1005. {
  1006. return {std::fmod(x[I], y)...};
  1007. }
  1008. template <class T, std::size_t N>
  1009. constexpr inline vector<T, N> mod(const vector<T, N>& x, T y)
  1010. {
  1011. static_assert(std::is_floating_point<T>::value);
  1012. return mod(x, y, std::make_index_sequence<N>{});
  1013. }
  1014. /// @private
  1015. template <class T, std::size_t N, std::size_t... I>
  1016. constexpr inline vector<T, N> mul(const vector<T, N>& x, const vector<T, N>& y, std::index_sequence<I...>) noexcept
  1017. {
  1018. return {(x[I] * y[I])...};
  1019. }
  1020. template <class T, std::size_t N>
  1021. constexpr inline vector<T, N> mul(const vector<T, N>& x, const vector<T, N>& y) noexcept
  1022. {
  1023. return mul(x, y, std::make_index_sequence<N>{});
  1024. }
  1025. /// @private
  1026. template <class T, std::size_t N, std::size_t... I>
  1027. constexpr inline vector<T, N> mul(const vector<T, N>& x, T y, std::index_sequence<I...>) noexcept
  1028. {
  1029. return {(x[I] * y)...};
  1030. }
  1031. template <class T, std::size_t N>
  1032. constexpr inline vector<T, N> mul(const vector<T, N>& x, T y) noexcept
  1033. {
  1034. return mul(x, y, std::make_index_sequence<N>{});
  1035. }
  1036. /// @private
  1037. template <class T, std::size_t N, std::size_t... I>
  1038. constexpr inline vector<T, N> negate(const vector<T, N>& x, std::index_sequence<I...>) noexcept
  1039. {
  1040. return {(-x[I])...};
  1041. }
  1042. template <class T, std::size_t N>
  1043. constexpr inline vector<T, N> negate(const vector<T, N>& x) noexcept
  1044. {
  1045. return negate(x, std::make_index_sequence<N>{});
  1046. }
  1047. template <class T, std::size_t N>
  1048. inline vector<T, N> normalize(const vector<T, N>& x)
  1049. {
  1050. static_assert(std::is_floating_point<T>::value);
  1051. return mul(x, T{1} / length(x));
  1052. }
  1053. /// @private
  1054. template <class T, std::size_t N, std::size_t... I>
  1055. constexpr inline vector<bool, N> not(const vector<T, N>& x, std::index_sequence<I...>) noexcept
  1056. {
  1057. return {!x[I]...};
  1058. }
  1059. template <class T, std::size_t N>
  1060. constexpr inline vector<bool, N> not(const vector<T, N>& x) noexcept
  1061. {
  1062. return not(x, std::make_index_sequence<N>{});
  1063. }
  1064. /// @private
  1065. template <class T, std::size_t N, std::size_t... I>
  1066. constexpr inline vector<bool, N> not_equal(const vector<T, N>& x, const vector<T, N>& y, std::index_sequence<I...>) noexcept
  1067. {
  1068. return {(x[I] != y[I])...};
  1069. }
  1070. template <class T, std::size_t N>
  1071. constexpr inline vector<bool, N> not_equal(const vector<T, N>& x, const vector<T, N>& y) noexcept
  1072. {
  1073. return not_equal(x, y, std::make_index_sequence<N>{});
  1074. }
  1075. /// @private
  1076. template <class T, std::size_t N, std::size_t... I>
  1077. inline vector<T, N> pow(const vector<T, N>& x, const vector<T, N>& y, std::index_sequence<I...>)
  1078. {
  1079. return {std::pow(x[I], y[I])...};
  1080. }
  1081. template <class T, std::size_t N>
  1082. inline vector<T, N> pow(const vector<T, N>& x, const vector<T, N>& y)
  1083. {
  1084. static_assert(std::is_floating_point<T>::value);
  1085. return pow(x, y, std::make_index_sequence<N>{});
  1086. }
  1087. /// @private
  1088. template <class T, std::size_t N, std::size_t... I>
  1089. inline vector<T, N> pow(const vector<T, N>& x, T y, std::index_sequence<I...>)
  1090. {
  1091. return {std::pow(x[I], y)...};
  1092. }
  1093. template <class T, std::size_t N>
  1094. inline vector<T, N> pow(const vector<T, N>& x, T y)
  1095. {
  1096. static_assert(std::is_floating_point<T>::value);
  1097. return pow(x, y, std::make_index_sequence<N>{});
  1098. }
  1099. /// @private
  1100. template <class T, std::size_t N, std::size_t... I>
  1101. constexpr inline vector<T, N> round(const vector<T, N>& x, std::index_sequence<I...>)
  1102. {
  1103. return {std::round(x[I])...};
  1104. }
  1105. template <class T, std::size_t N>
  1106. constexpr inline vector<T, N> round(const vector<T, N>& x)
  1107. {
  1108. static_assert(std::is_floating_point<T>::value);
  1109. return round(x, std::make_index_sequence<N>{});
  1110. }
  1111. /// @private
  1112. template <class T, std::size_t N, std::size_t... I>
  1113. constexpr inline vector<T, N> sign(const vector<T, N>& x, std::index_sequence<I...>)
  1114. {
  1115. return {std::copysign(T{1}, x[I])...};
  1116. }
  1117. template <class T, std::size_t N>
  1118. constexpr inline vector<T, N> sign(const vector<T, N>& x)
  1119. {
  1120. static_assert(std::is_floating_point<T>::value);
  1121. return sign(x, std::make_index_sequence<N>{});
  1122. }
  1123. /// @private
  1124. template <class T, std::size_t N, std::size_t... I>
  1125. inline vector<T, N> sqrt(const vector<T, N>& x, std::index_sequence<I...>)
  1126. {
  1127. return {std::sqrt(x[I])...};
  1128. }
  1129. template <class T, std::size_t N>
  1130. inline vector<T, N> sqrt(const vector<T, N>& x, const vector<T, N>& y)
  1131. {
  1132. static_assert(std::is_floating_point<T>::value);
  1133. return pow(x, std::make_index_sequence<N>{});
  1134. }
  1135. /// @private
  1136. template <class T, std::size_t N, std::size_t... I>
  1137. constexpr inline vector<T, N> sub(const vector<T, N>& x, const vector<T, N>& y, std::index_sequence<I...>) noexcept
  1138. {
  1139. return {(x[I] - y[I])...};
  1140. }
  1141. template <class T, std::size_t N>
  1142. constexpr inline vector<T, N> sub(const vector<T, N>& x, const vector<T, N>& y) noexcept
  1143. {
  1144. return sub(x, y, std::make_index_sequence<N>{});
  1145. }
  1146. /// @private
  1147. template <class T, std::size_t N, std::size_t... I>
  1148. constexpr inline vector<T, N> sub(const vector<T, N>& x, T y, std::index_sequence<I...>) noexcept
  1149. {
  1150. return {(x[I] - y)...};
  1151. }
  1152. template <class T, std::size_t N>
  1153. constexpr inline vector<T, N> sub(const vector<T, N>& x, T y) noexcept
  1154. {
  1155. return sub(x, y, std::make_index_sequence<N>{});
  1156. }
  1157. /// @private
  1158. template <class T, std::size_t N, std::size_t... I>
  1159. constexpr inline vector<T, N> sub(T x, const vector<T, N>& y, std::index_sequence<I...>) noexcept
  1160. {
  1161. return {(x - y[I])...};
  1162. }
  1163. template <class T, std::size_t N>
  1164. constexpr inline vector<T, N> sub(T x, const vector<T, N>& y) noexcept
  1165. {
  1166. return sub(x, y, std::make_index_sequence<N>{});
  1167. }
  1168. /// @private
  1169. template <class T, std::size_t N, std::size_t... I>
  1170. constexpr inline T sum(const vector<T, N>& x, std::index_sequence<I...>) noexcept
  1171. {
  1172. return (x[I] + ...);
  1173. }
  1174. template <class T, std::size_t N>
  1175. constexpr inline T sum(const vector<T, N>& x) noexcept
  1176. {
  1177. return sum(x, std::make_index_sequence<N>{});
  1178. }
  1179. template <std::size_t... Indices, class T, std::size_t N>
  1180. constexpr inline vector<T, sizeof...(Indices)> swizzle(const vector<T, N>& x) noexcept
  1181. {
  1182. return {x[Indices]...};
  1183. }
  1184. /// @private
  1185. template <class T, std::size_t N, std::size_t... I>
  1186. constexpr inline vector<T, N> trunc(const vector<T, N>& x, std::index_sequence<I...>)
  1187. {
  1188. return {std::trunc(x[I])...};
  1189. }
  1190. template <class T, std::size_t N>
  1191. constexpr inline vector<T, N> trunc(const vector<T, N>& x)
  1192. {
  1193. static_assert(std::is_floating_point<T>::value);
  1194. return trunc(x, std::make_index_sequence<N>{});
  1195. }
  1196. namespace operators {
  1197. /// @copydoc add(const vector<T, N>&, const vector<T, N>&)
  1198. template <class T, std::size_t N>
  1199. constexpr inline vector<T, N> operator+(const vector<T, N>& x, const vector<T, N>& y) noexcept
  1200. {
  1201. return add(x, y);
  1202. }
  1203. /// @copydoc add(const vector<T, N>&, T)
  1204. /// @{
  1205. template <class T, std::size_t N>
  1206. constexpr inline vector<T, N> operator+(const vector<T, N>& x, T y) noexcept
  1207. {
  1208. return add(x, y);
  1209. }
  1210. template <class T, std::size_t N>
  1211. constexpr inline vector<T, N> operator+(T x, const vector<T, N>& y) noexcept
  1212. {
  1213. return add(y, x);
  1214. }
  1215. /// @}
  1216. /// @copydoc div(const vector<T, N>&, const vector<T, N>&)
  1217. template <class T, std::size_t N>
  1218. constexpr inline vector<T, N> operator/(const vector<T, N>& x, const vector<T, N>& y) noexcept
  1219. {
  1220. return div(x, y);
  1221. }
  1222. /// @copydoc div(const vector<T, N>&, T)
  1223. template <class T, std::size_t N>
  1224. constexpr inline vector<T, N> operator/(const vector<T, N>& x, T y) noexcept
  1225. {
  1226. return div(x, y);
  1227. }
  1228. /// @copydoc div(T, const vector<T, N>&)
  1229. template <class T, std::size_t N>
  1230. constexpr inline vector<T, N> operator/(T x, const vector<T, N>& y) noexcept
  1231. {
  1232. return div(x, y);
  1233. }
  1234. /// @copydoc mul(const vector<T, N>&, const vector<T, N>&)
  1235. template <class T, std::size_t N>
  1236. constexpr inline vector<T, N> operator*(const vector<T, N>& x, const vector<T, N>& y) noexcept
  1237. {
  1238. return mul(x, y);
  1239. }
  1240. /// @copydoc mul(const vector<T, N>&, T)
  1241. /// @{
  1242. template <class T, std::size_t N>
  1243. constexpr inline vector<T, N> operator*(const vector<T, N>& x, T y) noexcept
  1244. {
  1245. return mul(x, y);
  1246. }
  1247. template <class T, std::size_t N>
  1248. constexpr inline vector<T, N> operator*(T x, const vector<T, N>& y) noexcept
  1249. {
  1250. return mul(y, x);
  1251. }
  1252. /// @}
  1253. /// @copydoc negate(const vector<T, N>&)
  1254. template <class T, std::size_t N>
  1255. constexpr inline vector<T, N> operator-(const vector<T, N>& x) noexcept
  1256. {
  1257. return negate(x);
  1258. }
  1259. /// @copydoc sub(const vector<T, N>&, const vector<T, N>&)
  1260. template <class T, std::size_t N>
  1261. constexpr inline vector<T, N> operator-(const vector<T, N>& x, const vector<T, N>& y) noexcept
  1262. {
  1263. return sub(x, y);
  1264. }
  1265. /// @copydoc sub(const vector<T, N>&, T)
  1266. template <class T, std::size_t N>
  1267. constexpr inline vector<T, N> operator-(const vector<T, N>& x, T y) noexcept
  1268. {
  1269. return sub(x, y);
  1270. }
  1271. /// @copydoc sub(T, const vector<T, N>&)
  1272. template <class T, std::size_t N>
  1273. constexpr inline vector<T, N> operator-(T x, const vector<T, N>& y) noexcept
  1274. {
  1275. return sub(x, y);
  1276. }
  1277. /**
  1278. * Adds two values and stores the result in the first value.
  1279. *
  1280. * @param x First value.
  1281. * @param y Second value.
  1282. *
  1283. * @return Reference to the first value.
  1284. */
  1285. /// @{
  1286. template <class T, std::size_t N>
  1287. constexpr inline vector<T, N>& operator+=(vector<T, N>& x, const vector<T, N>& y) noexcept
  1288. {
  1289. return (x = x + y);
  1290. }
  1291. template <class T, std::size_t N>
  1292. constexpr inline vector<T, N>& operator+=(vector<T, N>& x, T y) noexcept
  1293. {
  1294. return (x = x + y);
  1295. }
  1296. /// @}
  1297. /**
  1298. * Subtracts the first value by the second value and stores the result in the first value.
  1299. *
  1300. * @param x First value.
  1301. * @param y Second value.
  1302. *
  1303. * @return Reference to the first value.
  1304. */
  1305. /// @{
  1306. template <class T, std::size_t N>
  1307. constexpr inline vector<T, N>& operator-=(vector<T, N>& x, const vector<T, N>& y) noexcept
  1308. {
  1309. return (x = x - y);
  1310. }
  1311. template <class T, std::size_t N>
  1312. constexpr inline vector<T, N>& operator-=(vector<T, N>& x, T y) noexcept
  1313. {
  1314. return (x = x - y);
  1315. }
  1316. /// @}
  1317. /**
  1318. * Multiplies two values and stores the result in the first value.
  1319. *
  1320. * @param x First value.
  1321. * @param y Second value.
  1322. *
  1323. * @return Reference to the first value.
  1324. */
  1325. /// @{
  1326. template <class T, std::size_t N>
  1327. constexpr inline vector<T, N>& operator*=(vector<T, N>& x, const vector<T, N>& y) noexcept
  1328. {
  1329. return (x = x * y);
  1330. }
  1331. template <class T, std::size_t N>
  1332. constexpr inline vector<T, N>& operator*=(vector<T, N>& x, T y) noexcept
  1333. {
  1334. return (x = x * y);
  1335. }
  1336. /// @}
  1337. /**
  1338. * Divides the first value by the second value and stores the result in the first value.
  1339. *
  1340. * @param x First value.
  1341. * @param y Second value.
  1342. *
  1343. * @return Reference to the first value.
  1344. */
  1345. /// @{
  1346. template <class T, std::size_t N>
  1347. constexpr inline vector<T, N>& operator/=(vector<T, N>& x, const vector<T, N>& y) noexcept
  1348. {
  1349. return (x = x / y);
  1350. }
  1351. template <class T, std::size_t N>
  1352. constexpr inline vector<T, N>& operator/=(vector<T, N>& x, T y) noexcept
  1353. {
  1354. return (x = x / y);
  1355. }
  1356. /// @}
  1357. /**
  1358. * Writes the elements of a vector to an output stream, with each element delimeted by a space.
  1359. *
  1360. * @param os Output stream.
  1361. * @param x Vector.
  1362. *
  1363. * @return Output stream.
  1364. */
  1365. template <class T, std::size_t N>
  1366. std::ostream& operator<<(std::ostream& os, const vector<T, N>& x)
  1367. {
  1368. for (std::size_t i = 0; i < N; ++i)
  1369. {
  1370. if (i)
  1371. os << ' ';
  1372. os << x[i];
  1373. }
  1374. return os;
  1375. }
  1376. /**
  1377. * Reads the elements of a vector from an input stream, with each element delimeted by a space.
  1378. *
  1379. * @param is Input stream.
  1380. * @param x Vector.
  1381. *
  1382. * @return Input stream.
  1383. */
  1384. template <class T, std::size_t N>
  1385. std::istream& operator>>(std::istream& is, vector<T, N>& x)
  1386. {
  1387. for (std::size_t i = 0; i < N; ++i)
  1388. is >> x[i];
  1389. return is;
  1390. }
  1391. } // namespace operators
  1392. } // namespace math
  1393. using namespace math::operators;
  1394. #endif // ANTKEEPER_MATH_VECTOR_HPP