AliceVision
Photogrammetric Computer Vision Framework
Point3d.hpp
1 // This file is part of the AliceVision project.
2 // Copyright (c) 2017 AliceVision contributors.
3 // This Source Code Form is subject to the terms of the Mozilla Public License,
4 // v. 2.0. If a copy of the MPL was not distributed with this file,
5 // You can obtain one at https://mozilla.org/MPL/2.0/.
6 
7 #pragma once
8 
9 #include <string>
10 #include <cmath>
11 #include <ostream>
12 
13 #include <Eigen/Core>
14 #include <aliceVision/stl/hash.hpp>
15 
16 // #include <geogram/basic/vecg.h>
17 namespace GEO {
18 template<unsigned int DIM, class T>
19 class vecng;
20 };
21 
22 namespace aliceVision {
23 
24 class Point3d
25 {
26  public:
27  union
28  {
29  struct
30  {
31  double x, y, z;
32  };
33  double m[3];
34  };
35 
36  inline Point3d()
37  {
38  x = 0.0;
39  y = 0.0;
40  z = 0.0;
41  }
42 
43  inline Point3d(double _x, double _y, double _z)
44  {
45  x = _x;
46  y = _y;
47  z = _z;
48  }
49 
50  inline Point3d(const double* const p)
51  {
52  x = p[0];
53  y = p[1];
54  z = p[2];
55  }
56 
57  inline Point3d& operator=(const Point3d& param)
58  {
59  x = param.x;
60  y = param.y;
61  z = param.z;
62  return *this;
63  }
64 
65  inline Point3d& operator+=(const Point3d& _p)
66  {
67  x += _p.x;
68  y += _p.y;
69  z += _p.z;
70  return *this;
71  }
72 
73  inline Point3d& operator/=(const Point3d& _p)
74  {
75  x /= _p.x;
76  y /= _p.y;
77  z /= _p.z;
78  return *this;
79  }
80 
81  inline Point3d& operator/=(double v)
82  {
83  x /= v;
84  y /= v;
85  z /= v;
86  return *this;
87  }
88 
89  inline bool operator==(const Point3d& param) const { return (x == param.x) && (y == param.y) && (z == param.z); }
90 
91  inline Point3d operator-(const Point3d& _p) const { return Point3d(x - _p.x, y - _p.y, z - _p.z); }
92 
93  inline Point3d operator-() const { return Point3d(-x, -y, -z); }
94 
95  inline Point3d operator+(const Point3d& _p) const { return Point3d(x + _p.x, y + _p.y, z + _p.z); }
96 
97  inline Point3d operator*(const double d) const { return Point3d(x * d, y * d, z * d); }
98 
99  inline Point3d operator/(const double d) const { return Point3d(x / d, y / d, z / d); }
100 
101  inline Point3d normalize() const
102  {
103  double d = std::sqrt(x * x + y * y + z * z);
104  return Point3d(x / d, y / d, z / d);
105  }
106 
107  inline double size() const
108  {
109  double d = x * x + y * y + z * z;
110  if (d == 0.0)
111  {
112  return 0.0;
113  }
114 
115  return sqrt(d);
116  }
117 
118  inline double size2() const { return x * x + y * y + z * z; }
119 
120  template<class T>
121  operator GEO::vecng<3, T>() const
122  {
123  return GEO::vecng<3, T>(x, y, z);
124  }
125 
126  friend double dist(const Point3d& p1, const Point3d& p2);
127 
128  friend double dot(const Point3d& p1, const Point3d& p2);
129  friend Point3d cross(const Point3d& a, const Point3d& b);
130  friend Point3d proj(const Point3d& e, const Point3d& a);
131 };
132 
133 inline double dist(const Point3d& p1, const Point3d& p2) { return (p1 - p2).size(); }
134 
135 inline double dot(const Point3d& p1, const Point3d& p2) { return p1.x * p2.x + p1.y * p2.y + p1.z * p2.z; }
136 
137 inline Point3d cross(const Point3d& a, const Point3d& b)
138 {
139  Point3d vc;
140  vc.x = a.y * b.z - a.z * b.y;
141  vc.y = a.z * b.x - a.x * b.z;
142  vc.z = a.x * b.y - a.y * b.x;
143 
144  return vc;
145 }
146 
147 inline Point3d proj(const Point3d& e, const Point3d& a) { return e * (dot(e, a) / dot(e, e)); }
148 
149 inline double tripleProduct(const Point3d& a, const Point3d& b, const Point3d& c) { return dot(a, cross(b, c)); }
150 
155 inline double tetrahedronSolidAngle(const Point3d& oa, const Point3d& ob, const Point3d& oc)
156 {
157  const double a = oa.size();
158  const double b = ob.size();
159  const double c = oc.size();
160 
161  const double nom = tripleProduct(oa, ob, oc);
162  const double den = (a * b * c + dot(oa, ob) * c + dot(oa, oc) * b + dot(ob, oc) * a);
163  return std::abs(std::atan2(nom, den) * 2.0);
164 }
165 
166 inline std::ostream& operator<<(std::ostream& stream, const Point3d& p)
167 {
168  stream << p.x << "," << p.y << "," << p.z;
169  return stream;
170 }
171 
172 inline Eigen::Matrix<double, 3, 1> toEigen(const Point3d& v) { return Eigen::Matrix<double, 3, 1>(v.m); }
173 
175 {
176  std::size_t operator()(const Point3d & p) const noexcept
177  {
178  std::size_t seed = 0;
179  stl::hash_combine(seed, p.x);
180  stl::hash_combine(seed, p.y);
181  stl::hash_combine(seed, p.z);
182  return seed;
183  }
184 };
185 
186 } // namespace aliceVision
aliceVision::Point3d
Definition: Point3d.hpp:24
GEO::vecng
Definition: Point3d.hpp:19
aliceVision::Point3dHash
Definition: Point3d.hpp:174
aliceVision
Definition: checkerDetector.cpp:32
aliceVision::tetrahedronSolidAngle
double tetrahedronSolidAngle(const Point3d &oa, const Point3d &ob, const Point3d &oc)
Solid angle of a tetrahedron. It takes 3 vectors OA, OB, AC to define the solid angle define by the t...
Definition: Point3d.hpp:155