Allium  0.1.0
point.hpp
1 // Copyright 2020 Hannah Rittich
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ALLIUM_MESH_POINT_HPP
16 #define ALLIUM_MESH_POINT_HPP
17 
18 #include <allium/util/assert.hpp>
19 #include <allium/la/vector_trait.hpp>
20 #include <ostream>
21 #include <algorithm>
22 #include <numeric>
23 
24 namespace allium {
25 
30  template <typename N, int D>
31  class Point : public VectorTrait<Point<N,D>, N> {
32  public:
33  Point() = default;
34 
35  Point(const std::initializer_list<N> &entries) {
36  allium_assert(entries.size() == D, "Correct initializer length.");
37 
38  int i = 0;
39  for (auto e : entries) {
40  m_entries[i] = e;
41  ++i;
42  }
43  }
44 
45  static Point full(N initial_value) {
46  Point p;
47  std::fill(p.m_entries.begin(), p.m_entries.end(), initial_value);
48  return p;
49  }
50 
51  size_t rows() { return D; }
52 
53  N& operator[] (int i) {
54  return m_entries[i];
55  }
56 
57  const N& operator[] (int i) const {
58  return const_cast<Point<N,D>&>(*this)[i];
59  }
60 
61  bool operator== (const Point& rhs) const {
62  for (int i = 0; i < D; ++i) {
63  if (m_entries[i] != rhs[i]) {
64  return false;
65  }
66  }
67  return true;
68  }
69 
70  Point operator+= (const Point& rhs) {
71  for (int i = 0; i < D; ++i) {
72  m_entries[i] += rhs[i];
73  }
74  return *this;
75  }
76 
77  Point operator*= (N factor) {
78  for (int i = 0; i < D; ++i) {
79  m_entries[i] *= factor;
80  }
81  return *this;
82  }
83 
84  Point<N, D+1> joined(N x) {
85  Point<N, D+1> result;
86  for (int i = 0; i < D; ++i) {
87  result[i] = (*this)[i];
88  }
89  result[D] = x;
90  return result;
91  }
92 
93  template <typename Pred>
94  bool all_of(Pred pred) {
95  return std::all_of(m_entries.begin(), m_entries.end(), pred);
96  }
97 
98  N prod() const {
99  return std::accumulate(m_entries.begin(), m_entries.end(),
100  N(1),
101  [](N a, N b) { return a*b; });
102  }
103 
104  private:
105  std::array<N, D> m_entries;
106  };
107 
108  template <typename N, int D>
109  std::ostream& operator<< (std::ostream& os, const Point<N, D>& p) {
110  bool first = true;
111  os << "(";
112  for (int i = 0; i < D; ++i) {
113  if (first)
114  first = false;
115  else
116  os << ", ";
117  os << p[i];
118  }
119  os << ")";
120 
121  return os;
122  }
123 
124 }
125 
126 #endif
A point in D dimensions.
Definition: point.hpp:31
Generates common vector operations automatically.
Definition: vector_trait.hpp:31