Loading [MathJax]/extensions/TeX/AMSsymbols.js
All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Modules Pages
Point.h
1 /* This file is part of the Gudhi Library. The Gudhi library
2  * (Geometric Understanding in Higher Dimensions) is a generic C++
3  * library for computational topology.
4  *
5  * Author(s): David Salinas
6  *
7  * Copyright (C) 2014 INRIA Sophia Antipolis-Mediterranee (France)
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23 
24 #ifndef POINT_H_
25 #define POINT_H_
26 
27 #include <cmath>
28 #include <vector>
29 #include <cassert>
30 #include <cstddef>
31 #include <initializer_list>
32 
33 class Point_d {
34  public:
35  Point_d(size_t dim = 3) : coords_(dim, 0) { }
36 
37  Point_d(const Point_d& other) : coords_(other.coords_) { }
38 
39  Point_d(const std::initializer_list<double>& list) : coords_(list) { }
40 
41  template<typename CoordsIt>
42  Point_d(CoordsIt begin, CoordsIt end) : coords_(begin, end) { }
43 
44  size_t dimension() const {
45  return coords_.size();
46  }
47 
48  double x() const {
49  return coords_[0];
50  }
51 
52  double y() const {
53  return coords_[1];
54  }
55 
56  double z() const {
57  return coords_[2];
58  }
59 
60  double& x() {
61  return coords_[0];
62  }
63 
64  double& y() {
65  return coords_[1];
66  }
67 
68  double& z() {
69  return coords_[2];
70  }
71 
72  std::vector<double>::const_iterator begin() const {
73  return coords_.begin();
74  }
75 
76  std::vector<double>::const_iterator end() const {
77  return coords_.end();
78  }
79 
80  double& operator[](unsigned i) {
81  return coords_[i];
82  }
83 
84  const double& operator[](unsigned i) const {
85  return coords_[i];
86  }
87 
88  double squared_norm() const {
89  double res = 0;
90  for (auto x : coords_)
91  res += x * x;
92  return res;
93  }
94 
95  friend double squared_dist(const Point_d& p1, const Point_d& p2) {
96  assert(p1.dimension() == p2.dimension());
97  double res = 0;
98  for (unsigned i = 0; i < p1.coords_.size(); ++i)
99  res += (p1[i] - p2[i])*(p1[i] - p2[i]);
100  return res;
101  }
102 
106  double operator*(const Point_d& other) const {
107  assert(dimension() == other.dimension());
108  double res = 0;
109  for (unsigned i = 0; i < coords_.size(); ++i)
110  res += coords_[i] * other[i];
111  return res;
112  }
113 
117  Point_d cross_product(const Point_d& other) {
118  assert(dimension() == 3 && other.dimension() == 3);
119  Point_d res(3);
120  res[0] = (*this)[1] * other[2] - (*this)[2] * other[1];
121  res[1] = (*this)[2] * other[0] - (*this)[0] * other[2];
122  res[2] = (*this)[0] * other[1] - (*this)[1] * other[0];
123  return res;
124  }
125 
126  Point_d operator+(const Point_d& other) const {
127  assert(dimension() == other.dimension());
128  Point_d res(dimension());
129  for (unsigned i = 0; i < coords_.size(); ++i)
130  res[i] = (*this)[i] + other[i];
131  return res;
132  }
133 
134  Point_d operator*(double lambda) const {
135  Point_d res(dimension());
136  for (unsigned i = 0; i < coords_.size(); ++i)
137  res[i] = (*this)[i] * lambda;
138  return res;
139  }
140 
141  Point_d operator/(double lambda) const {
142  Point_d res(dimension());
143  for (unsigned i = 0; i < coords_.size(); ++i)
144  res[i] = (*this)[i] / lambda;
145  return res;
146  }
147 
148  Point_d operator-(const Point_d& other) const {
149  assert(dimension() == other.dimension());
150  Point_d res(dimension());
151  for (unsigned i = 0; i < coords_.size(); ++i)
152  res[i] = (*this)[i] - other[i];
153  return res;
154  }
155 
156  friend Point_d unit_normal(const Point_d& p1, const Point_d& p2, const Point_d& p3) {
157  assert(p1.dimension() == 3);
158  assert(p2.dimension() == 3);
159  assert(p3.dimension() == 3);
160  Point_d p1p2 = p2 - p1;
161  Point_d p1p3 = p3 - p1;
162  Point_d res(p1p2.cross_product(p1p3));
163  return res / std::sqrt(res.squared_norm());
164  }
165 
166  private:
167  std::vector<double> coords_;
168 };
169 
170 #endif // POINT_H_
GUDHI  Version 2.0.1  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding. Generated on Mon Oct 2 2017 10:20:49 for GUDHI by doxygen 1.8.11