18#include <initializer_list>
22 Point_d(
size_t dim = 3) : coords_(dim, 0) { }
24 Point_d(
const Point_d& other) : coords_(other.coords_) { }
26 Point_d(
const std::initializer_list<double>& list) : coords_(list) { }
28 template<
typename CoordsIt>
29 Point_d(CoordsIt begin, CoordsIt end) : coords_(begin, end) { }
31 size_t dimension()
const {
32 return coords_.size();
59 std::vector<double>::const_iterator begin()
const {
60 return coords_.begin();
63 std::vector<double>::const_iterator end()
const {
67 double& operator[](
unsigned i) {
71 const double& operator[](
unsigned i)
const {
75 double squared_norm()
const {
77 for (
auto x : coords_)
82 friend double squared_dist(
const Point_d& p1,
const Point_d& p2) {
83 assert(p1.dimension() == p2.dimension());
85 for (
unsigned i = 0; i < p1.coords_.size(); ++i)
86 res += (p1[i] - p2[i])*(p1[i] - p2[i]);
93 double operator*(
const Point_d& other)
const {
94 assert(dimension() == other.dimension());
96 for (
unsigned i = 0; i < coords_.size(); ++i)
97 res += coords_[i] * other[i];
104 Point_d cross_product(
const Point_d& other) {
105 assert(dimension() == 3 && other.dimension() == 3);
107 res[0] = (*this)[1] * other[2] - (*this)[2] * other[1];
108 res[1] = (*this)[2] * other[0] - (*this)[0] * other[2];
109 res[2] = (*this)[0] * other[1] - (*this)[1] * other[0];
113 Point_d operator+(
const Point_d& other)
const {
114 assert(dimension() == other.dimension());
115 Point_d res(dimension());
116 for (
unsigned i = 0; i < coords_.size(); ++i)
117 res[i] = (*
this)[i] + other[i];
121 Point_d operator*(
double lambda)
const {
122 Point_d res(dimension());
123 for (
unsigned i = 0; i < coords_.size(); ++i)
124 res[i] = (*
this)[i] * lambda;
128 Point_d operator/(
double lambda)
const {
129 Point_d res(dimension());
130 for (
unsigned i = 0; i < coords_.size(); ++i)
131 res[i] = (*
this)[i] / lambda;
135 Point_d operator-(
const Point_d& other)
const {
136 assert(dimension() == other.dimension());
137 Point_d res(dimension());
138 for (
unsigned i = 0; i < coords_.size(); ++i)
139 res[i] = (*
this)[i] - other[i];
143 friend Point_d unit_normal(
const Point_d& p1,
const Point_d& p2,
const Point_d& p3) {
144 assert(p1.dimension() == 3);
145 assert(p2.dimension() == 3);
146 assert(p3.dimension() == 3);
147 Point_d p1p2 = p2 - p1;
148 Point_d p1p3 = p3 - p1;
149 Point_d res(p1p2.cross_product(p1p3));
150 return res / std::sqrt(res.squared_norm());
154 std::vector<double> coords_;