31 #include <initializer_list> 35 Point_d(
size_t dim = 3) : coords_(dim, 0) { }
37 Point_d(
const Point_d& other) : coords_(other.coords_) { }
39 Point_d(
const std::initializer_list<double>& list) : coords_(list) { }
41 template<
typename CoordsIt>
42 Point_d(CoordsIt begin, CoordsIt end) : coords_(begin, end) { }
44 size_t dimension()
const {
45 return coords_.size();
72 std::vector<double>::const_iterator begin()
const {
73 return coords_.begin();
76 std::vector<double>::const_iterator end()
const {
80 double& operator[](
unsigned i) {
84 const double& operator[](
unsigned i)
const {
88 double squared_norm()
const {
90 for (
auto x : coords_)
95 friend double squared_dist(
const Point_d& p1,
const Point_d& p2) {
96 assert(p1.dimension() == p2.dimension());
98 for (
unsigned i = 0; i < p1.coords_.size(); ++i)
99 res += (p1[i] - p2[i])*(p1[i] - p2[i]);
106 double operator*(
const Point_d& other)
const {
107 assert(dimension() == other.dimension());
109 for (
unsigned i = 0; i < coords_.size(); ++i)
110 res += coords_[i] * other[i];
117 Point_d cross_product(
const Point_d& other) {
118 assert(dimension() == 3 && other.dimension() == 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];
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];
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;
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;
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];
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());
167 std::vector<double> coords_;