JNGL
Vec2.hpp
Go to the documentation of this file.
1 // Copyright 2018-2021 Jan Niklas Hasse <[email protected]>
2 // For conditions of distribution and use, see copyright notice in LICENSE.txt
5 #pragma once
6 
7 #if !defined(__has_include) || __has_include("boost/version.hpp")
8 #include <boost/version.hpp>
9 #endif
10 #include <iosfwd>
11 
12 namespace jngl {
13 
35 class Vec2 {
36 public:
38  Vec2();
39 
41  Vec2(double x, double y);
42 
44  double x = 0;
45 
47  double y = 0;
48 
50  bool isNull() const;
51 
53  template <class Archive> void serialize(Archive& ar, const unsigned int) {
54  ar(x, y);
55  }
56 };
57 
60 
61 } // namespace jngl
62 
63 #if BOOST_VERSION >= 106200 || __has_include("boost/qvm/vec_traits.hpp")
64 #include <boost/qvm/vec_traits.hpp>
65 
66 namespace boost::qvm {
67 template <> struct vec_traits<jngl::Vec2> {
68  static int const dim = 2;
69  using scalar_type = double;
70 
71  template <int I> static inline scalar_type& write_element(jngl::Vec2& v) {
72  return (&v.x)[I];
73  }
74  template <int I> static inline scalar_type read_element(const jngl::Vec2& v) {
75  return (&v.x)[I];
76  }
77 
78  static inline scalar_type& write_element_idx(int i, jngl::Vec2& v) {
79  return (&v.x)[i];
80  }
81  static inline scalar_type read_element_idx(int i, jngl::Vec2 const& v) {
82  return (&v.x)[i];
83  }
84 };
85 } // namespace boost::qvm
86 
87 #include <boost/qvm/vec_operations.hpp>
88 
89 using boost::qvm::operator+=;
90 using boost::qvm::operator*=;
91 using boost::qvm::operator/=;
92 using boost::qvm::operator-=;
93 using boost::qvm::operator==;
94 using boost::qvm::operator!=;
95 using boost::qvm::operator+;
96 using boost::qvm::operator-;
97 using boost::qvm::operator/;
98 using boost::qvm::operator*;
99 #endif
Two-dimensional vector.
Definition: Vec2.hpp:35
Vec2()
Null vector.
double y
y component
Definition: Vec2.hpp:47
bool isNull() const
returns true if both x and y are exactly 0
Vec2(double x, double y)
Creates a vector {x, y}.
void serialize(Archive &ar, const unsigned int)
Helper function to use with cereal or Boost.Serialization.
Definition: Vec2.hpp:53
double x
x component
Definition: Vec2.hpp:44
JNGL's main namespace.
Definition: App.hpp:10
std::ostream & operator<<(std::ostream &, const Vec2 &)
Prints the vector like this: [x=…, y=…].