RDKit
Open-source cheminformatics and machine learning.
point.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2003-2021 Greg Landrum and other RDKit contributors
3 //
4 // @@ All Rights Reserved @@
5 // This file is part of the RDKit.
6 // The contents are covered by the terms of the BSD license
7 // which is included in the file license.txt, found at the root
8 // of the RDKit source tree.
9 //
10 
11 #include <RDGeneral/export.h>
12 #ifndef __RD_POINT_H__
13 #define __RD_POINT_H__
14 #include <iostream>
15 #include <cmath>
16 #include <vector>
17 #include <map>
18 
19 #ifndef M_PI
20 #define M_PI 3.14159265358979323846
21 #endif
22 
23 #include <RDGeneral/Invariant.h>
24 #include <Numerics/Vector.h>
25 #include <boost/smart_ptr.hpp>
26 
27 namespace RDGeom {
28 
30  // this is the virtual base class, mandating certain functions
31  public:
32  virtual ~Point() {}
33 
34  virtual double operator[](unsigned int i) const = 0;
35  virtual double &operator[](unsigned int i) = 0;
36 
37  virtual void normalize() = 0;
38  virtual double length() const = 0;
39  virtual double lengthSq() const = 0;
40  virtual unsigned int dimension() const = 0;
41 
42  virtual Point *copy() const = 0;
43 };
44 #ifndef _MSC_VER
45 // g++ (at least as of v9.3.0) generates some spurious warnings from here.
46 // disable them
47 #if !defined(__clang__) and defined(__GNUC__)
48 #pragma GCC diagnostic push
49 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
50 #endif
51 #endif
52 
53 // typedef class Point3D Point;
55  public:
56  double x{0.0};
57  double y{0.0};
58  double z{0.0};
59 
60  Point3D() {}
61  Point3D(double xv, double yv, double zv) : x(xv), y(yv), z(zv) {}
62 
63  ~Point3D() override = default;
64 
65  Point3D(const Point3D &other)
66  : Point(other), x(other.x), y(other.y), z(other.z) {}
67 
68  Point *copy() const override { return new Point3D(*this); }
69 
70  inline unsigned int dimension() const override { return 3; }
71 
72  inline double operator[](unsigned int i) const override {
73  PRECONDITION(i < 3, "Invalid index on Point3D");
74  if (i == 0) {
75  return x;
76  } else if (i == 1) {
77  return y;
78  } else {
79  return z;
80  }
81  }
82 
83  inline double &operator[](unsigned int i) override {
84  PRECONDITION(i < 3, "Invalid index on Point3D");
85  if (i == 0) {
86  return x;
87  } else if (i == 1) {
88  return y;
89  } else {
90  return z;
91  }
92  }
93 
94  Point3D &operator=(const Point3D &other) {
95  if (&other == this) {
96  return *this;
97  }
98  x = other.x;
99  y = other.y;
100  z = other.z;
101  return *this;
102  }
103 
104  Point3D &operator+=(const Point3D &other) {
105  x += other.x;
106  y += other.y;
107  z += other.z;
108  return *this;
109  }
110 
111  Point3D &operator-=(const Point3D &other) {
112  x -= other.x;
113  y -= other.y;
114  z -= other.z;
115  return *this;
116  }
117 
118  Point3D &operator*=(double scale) {
119  x *= scale;
120  y *= scale;
121  z *= scale;
122  return *this;
123  }
124 
125  Point3D &operator/=(double scale) {
126  x /= scale;
127  y /= scale;
128  z /= scale;
129  return *this;
130  }
131 
132  Point3D operator-() const {
133  Point3D res(x, y, z);
134  res.x *= -1.0;
135  res.y *= -1.0;
136  res.z *= -1.0;
137  return res;
138  }
139 
140  void normalize() override {
141  double l = this->length();
142  x /= l;
143  y /= l;
144  z /= l;
145  }
146 
147  double length() const override {
148  double res = x * x + y * y + z * z;
149  return sqrt(res);
150  }
151 
152  double lengthSq() const override {
153  // double res = pow(x,2) + pow(y,2) + pow(z,2);
154  double res = x * x + y * y + z * z;
155  return res;
156  }
157 
158  double dotProduct(const Point3D &other) const {
159  double res = x * (other.x) + y * (other.y) + z * (other.z);
160  return res;
161  }
162 
163  /*! \brief determines the angle between a vector to this point
164  * from the origin and a vector to the other point.
165  *
166  * The angle is unsigned: the results of this call will always
167  * be between 0 and M_PI
168  */
169  double angleTo(const Point3D &other) const {
170  double lsq = lengthSq() * other.lengthSq();
171  double dotProd = dotProduct(other);
172  dotProd /= sqrt(lsq);
173 
174  // watch for roundoff error:
175  if (dotProd <= -1.0) {
176  return M_PI;
177  }
178  if (dotProd >= 1.0) {
179  return 0.0;
180  }
181 
182  return acos(dotProd);
183  }
184 
185  /*! \brief determines the signed angle between a vector to this point
186  * from the origin and a vector to the other point.
187  *
188  * The results of this call will be between 0 and M_2_PI
189  */
190  double signedAngleTo(const Point3D &other) const {
191  double res = this->angleTo(other);
192  // check the sign of the z component of the cross product:
193  if ((this->x * other.y - this->y * other.x) < -1e-6) res = 2.0 * M_PI - res;
194  return res;
195  }
196 
197  /*! \brief Returns a normalized direction vector from this
198  * point to another.
199  *
200  */
201  Point3D directionVector(const Point3D &other) const {
202  Point3D res;
203  res.x = other.x - x;
204  res.y = other.y - y;
205  res.z = other.z - z;
206  res.normalize();
207  return res;
208  }
209 
210  /*! \brief Cross product of this point with the another point
211  *
212  * The order is important here
213  * The result is "this" cross with "other" not (other x this)
214  */
215  Point3D crossProduct(const Point3D &other) const {
216  Point3D res;
217  res.x = y * (other.z) - z * (other.y);
218  res.y = -x * (other.z) + z * (other.x);
219  res.z = x * (other.y) - y * (other.x);
220  return res;
221  }
222 
223  /*! \brief Get a unit perpendicular from this point (treating it as a vector):
224  *
225  */
227  Point3D res(0.0, 0.0, 0.0);
228  if (x) {
229  if (y) {
230  res.y = -1 * x;
231  res.x = y;
232  } else if (z) {
233  res.z = -1 * x;
234  res.x = z;
235  } else {
236  res.y = 1;
237  }
238  } else if (y) {
239  if (z) {
240  res.z = -1 * y;
241  res.y = z;
242  } else {
243  res.x = 1;
244  }
245  } else if (z) {
246  res.x = 1;
247  }
248  double l = res.length();
249  POSTCONDITION(l > 0.0, "zero perpendicular");
250  res /= l;
251  return res;
252  }
253 };
254 
255 // given a set of four pts in 3D compute the dihedral angle between the
256 // plane of the first three points (pt1, pt2, pt3) and the plane of the
257 // last three points (pt2, pt3, pt4)
258 // the computed angle is between 0 and PI
260  const Point3D &pt2,
261  const Point3D &pt3,
262  const Point3D &pt4);
263 
264 // given a set of four pts in 3D compute the signed dihedral angle between the
265 // plane of the first three points (pt1, pt2, pt3) and the plane of the
266 // last three points (pt2, pt3, pt4)
267 // the computed angle is between -PI and PI
269  const Point3D &pt1, const Point3D &pt2, const Point3D &pt3,
270  const Point3D &pt4);
271 
273  public:
274  double x{0.0};
275  double y{0.0};
276 
277  Point2D() {}
278  Point2D(double xv, double yv) : x(xv), y(yv) {}
279  ~Point2D() override = default;
280 
281  Point2D(const Point2D &other) : Point(other), x(other.x), y(other.y) {}
282  //! construct from a Point3D (ignoring the z coordinate)
283  Point2D(const Point3D &p3d) : Point(p3d), x(p3d.x), y(p3d.y) {}
284 
285  Point *copy() const override { return new Point2D(*this); }
286 
287  inline unsigned int dimension() const override { return 2; }
288 
289  inline double operator[](unsigned int i) const override {
290  PRECONDITION(i < 2, "Invalid index on Point2D");
291  if (i == 0) {
292  return x;
293  } else {
294  return y;
295  }
296  }
297 
298  inline double &operator[](unsigned int i) override {
299  PRECONDITION(i < 2, "Invalid index on Point2D");
300  if (i == 0) {
301  return x;
302  } else {
303  return y;
304  }
305  }
306 
307  Point2D &operator=(const Point2D &other) {
308  x = other.x;
309  y = other.y;
310  return *this;
311  }
312 
313  Point2D &operator+=(const Point2D &other) {
314  x += other.x;
315  y += other.y;
316  return *this;
317  }
318 
319  Point2D &operator-=(const Point2D &other) {
320  x -= other.x;
321  y -= other.y;
322  return *this;
323  }
324 
325  Point2D &operator*=(double scale) {
326  x *= scale;
327  y *= scale;
328  return *this;
329  }
330 
331  Point2D &operator/=(double scale) {
332  x /= scale;
333  y /= scale;
334  return *this;
335  }
336 
337  Point2D operator-() const {
338  Point2D res(x, y);
339  res.x *= -1.0;
340  res.y *= -1.0;
341  return res;
342  }
343 
344  void normalize() override {
345  double ln = this->length();
346  x /= ln;
347  y /= ln;
348  }
349 
350  void rotate90() {
351  double temp = x;
352  x = -y;
353  y = temp;
354  }
355 
356  double length() const override {
357  // double res = pow(x,2) + pow(y,2);
358  double res = x * x + y * y;
359  return sqrt(res);
360  }
361 
362  double lengthSq() const override {
363  double res = x * x + y * y;
364  return res;
365  }
366 
367  double dotProduct(const Point2D &other) const {
368  double res = x * (other.x) + y * (other.y);
369  return res;
370  }
371 
372  double angleTo(const Point2D &other) const {
373  Point2D t1, t2;
374  t1 = *this;
375  t2 = other;
376  t1.normalize();
377  t2.normalize();
378  double dotProd = t1.dotProduct(t2);
379  // watch for roundoff error:
380  if (dotProd < -1.0)
381  dotProd = -1.0;
382  else if (dotProd > 1.0)
383  dotProd = 1.0;
384  return acos(dotProd);
385  }
386 
387  double signedAngleTo(const Point2D &other) const {
388  double res = this->angleTo(other);
389  if ((this->x * other.y - this->y * other.x) < -1e-6) res = 2.0 * M_PI - res;
390  return res;
391  }
392 
393  Point2D directionVector(const Point2D &other) const {
394  Point2D res;
395  res.x = other.x - x;
396  res.y = other.y - y;
397  res.normalize();
398  return res;
399  }
400 };
401 
403  public:
404  typedef boost::shared_ptr<RDNumeric::Vector<double>> VECT_SH_PTR;
405 
406  PointND(unsigned int dim) {
408  dp_storage.reset(nvec);
409  }
410 
411  PointND(const PointND &other) : Point(other) {
413  new RDNumeric::Vector<double>(*other.getStorage());
414  dp_storage.reset(nvec);
415  }
416 
417  Point *copy() const override { return new PointND(*this); }
418 
419 #if 0
420  template <typename T>
421  PointND(const T &vals){
422  RDNumeric::Vector<double> *nvec = new RDNumeric::Vector<double>(vals.size(), 0.0);
423  dp_storage.reset(nvec);
424  unsigned int idx=0;
425  typename T::const_iterator it;
426  for(it=vals.begin();
427  it!=vals.end();
428  ++it){
429  nvec->setVal(idx,*it);
430  ++idx;
431  };
432  };
433 #endif
434 
435  ~PointND() override = default;
436 
437  inline double operator[](unsigned int i) const override {
438  return dp_storage.get()->getVal(i);
439  }
440 
441  inline double &operator[](unsigned int i) override {
442  return (*dp_storage.get())[i];
443  }
444 
445  inline void normalize() override { dp_storage.get()->normalize(); }
446 
447  inline double length() const override { return dp_storage.get()->normL2(); }
448 
449  inline double lengthSq() const override {
450  return dp_storage.get()->normL2Sq();
451  }
452 
453  unsigned int dimension() const override { return dp_storage.get()->size(); }
454 
455  PointND &operator=(const PointND &other) {
456  if (this == &other) return *this;
457 
459  new RDNumeric::Vector<double>(*other.getStorage());
460  dp_storage.reset(nvec);
461  return *this;
462  }
463 
464  PointND &operator+=(const PointND &other) {
465  (*dp_storage.get()) += (*other.getStorage());
466  return *this;
467  }
468 
469  PointND &operator-=(const PointND &other) {
470  (*dp_storage.get()) -= (*other.getStorage());
471  return *this;
472  }
473 
474  PointND &operator*=(double scale) {
475  (*dp_storage.get()) *= scale;
476  return *this;
477  }
478 
479  PointND &operator/=(double scale) {
480  (*dp_storage.get()) /= scale;
481  return *this;
482  }
483 
485  PRECONDITION(this->dimension() == other.dimension(),
486  "Point dimensions do not match");
487  PointND np(other);
488  np -= (*this);
489  np.normalize();
490  return np;
491  }
492 
493  double dotProduct(const PointND &other) const {
494  return dp_storage.get()->dotProduct(*other.getStorage());
495  }
496 
497  double angleTo(const PointND &other) const {
498  double dp = this->dotProduct(other);
499  double n1 = this->length();
500  double n2 = other.length();
501  if ((n1 > 1.e-8) && (n2 > 1.e-8)) {
502  dp /= (n1 * n2);
503  }
504  if (dp < -1.0)
505  dp = -1.0;
506  else if (dp > 1.0)
507  dp = 1.0;
508  return acos(dp);
509  }
510 
511  private:
512  VECT_SH_PTR dp_storage;
513  inline const RDNumeric::Vector<double> *getStorage() const {
514  return dp_storage.get();
515  }
516 };
517 #ifndef _MSC_VER
518 #if !defined(__clang__) and defined(__GNUC__)
519 #pragma GCC diagnostic pop
520 #endif
521 #endif
522 
523 typedef std::vector<RDGeom::Point *> PointPtrVect;
524 typedef PointPtrVect::iterator PointPtrVect_I;
525 typedef PointPtrVect::const_iterator PointPtrVect_CI;
526 
527 typedef std::vector<RDGeom::Point3D *> Point3DPtrVect;
528 typedef std::vector<RDGeom::Point2D *> Point2DPtrVect;
529 typedef Point3DPtrVect::iterator Point3DPtrVect_I;
530 typedef Point3DPtrVect::const_iterator Point3DPtrVect_CI;
531 typedef Point2DPtrVect::iterator Point2DPtrVect_I;
532 typedef Point2DPtrVect::const_iterator Point2DPtrVect_CI;
533 
534 typedef std::vector<const RDGeom::Point3D *> Point3DConstPtrVect;
535 typedef Point3DConstPtrVect::iterator Point3DConstPtrVect_I;
536 typedef Point3DConstPtrVect::const_iterator Point3DConstPtrVect_CI;
537 
538 typedef std::vector<Point3D> POINT3D_VECT;
539 typedef std::vector<Point3D>::iterator POINT3D_VECT_I;
540 typedef std::vector<Point3D>::const_iterator POINT3D_VECT_CI;
541 
542 typedef std::map<int, Point2D> INT_POINT2D_MAP;
543 typedef INT_POINT2D_MAP::iterator INT_POINT2D_MAP_I;
544 typedef INT_POINT2D_MAP::const_iterator INT_POINT2D_MAP_CI;
545 
546 RDKIT_RDGEOMETRYLIB_EXPORT std::ostream &operator<<(std::ostream &target,
547  const RDGeom::Point &pt);
548 
550  const RDGeom::Point3D &p2);
552  const RDGeom::Point3D &p2);
554  double v);
556  double v);
557 
559  const RDGeom::Point2D &p2);
561  const RDGeom::Point2D &p2);
563  double v);
565  double v);
566 
568  const RDGeom::PointND &p2);
570  const RDGeom::PointND &p2);
572  double v);
574  double v);
575 } // namespace RDGeom
576 
577 #endif
#define POSTCONDITION(expr, mess)
Definition: Invariant.h:117
#define PRECONDITION(expr, mess)
Definition: Invariant.h:109
unsigned int dimension() const override
Definition: point.h:287
Point2D(double xv, double yv)
Definition: point.h:278
Point2D(const Point2D &other)
Definition: point.h:281
Point2D directionVector(const Point2D &other) const
Definition: point.h:393
void rotate90()
Definition: point.h:350
double & operator[](unsigned int i) override
Definition: point.h:298
Point2D & operator=(const Point2D &other)
Definition: point.h:307
Point2D & operator-=(const Point2D &other)
Definition: point.h:319
Point2D & operator*=(double scale)
Definition: point.h:325
Point2D(const Point3D &p3d)
construct from a Point3D (ignoring the z coordinate)
Definition: point.h:283
~Point2D() override=default
Point2D & operator+=(const Point2D &other)
Definition: point.h:313
Point2D operator-() const
Definition: point.h:337
double dotProduct(const Point2D &other) const
Definition: point.h:367
double lengthSq() const override
Definition: point.h:362
Point2D & operator/=(double scale)
Definition: point.h:331
Point * copy() const override
Definition: point.h:285
double y
Definition: point.h:275
void normalize() override
Definition: point.h:344
double length() const override
Definition: point.h:356
double operator[](unsigned int i) const override
Definition: point.h:289
double signedAngleTo(const Point2D &other) const
Definition: point.h:387
double x
Definition: point.h:274
double angleTo(const Point2D &other) const
Definition: point.h:372
Point3D(const Point3D &other)
Definition: point.h:65
double lengthSq() const override
Definition: point.h:152
Point3D operator-() const
Definition: point.h:132
unsigned int dimension() const override
Definition: point.h:70
Point3D & operator*=(double scale)
Definition: point.h:118
double dotProduct(const Point3D &other) const
Definition: point.h:158
double signedAngleTo(const Point3D &other) const
determines the signed angle between a vector to this point from the origin and a vector to the other ...
Definition: point.h:190
double & operator[](unsigned int i) override
Definition: point.h:83
double operator[](unsigned int i) const override
Definition: point.h:72
double angleTo(const Point3D &other) const
determines the angle between a vector to this point from the origin and a vector to the other point.
Definition: point.h:169
Point3D crossProduct(const Point3D &other) const
Cross product of this point with the another point.
Definition: point.h:215
~Point3D() override=default
Point * copy() const override
Definition: point.h:68
double length() const override
Definition: point.h:147
Point3D(double xv, double yv, double zv)
Definition: point.h:61
double y
Definition: point.h:57
double x
Definition: point.h:56
double z
Definition: point.h:58
void normalize() override
Definition: point.h:140
Point3D & operator/=(double scale)
Definition: point.h:125
Point3D & operator=(const Point3D &other)
Definition: point.h:94
Point3D & operator-=(const Point3D &other)
Definition: point.h:111
Point3D directionVector(const Point3D &other) const
Returns a normalized direction vector from this point to another.
Definition: point.h:201
Point3D getPerpendicular() const
Get a unit perpendicular from this point (treating it as a vector):
Definition: point.h:226
Point3D & operator+=(const Point3D &other)
Definition: point.h:104
PointND & operator=(const PointND &other)
Definition: point.h:455
double & operator[](unsigned int i) override
Definition: point.h:441
double length() const override
Definition: point.h:447
unsigned int dimension() const override
Definition: point.h:453
PointND & operator-=(const PointND &other)
Definition: point.h:469
boost::shared_ptr< RDNumeric::Vector< double > > VECT_SH_PTR
Definition: point.h:404
double dotProduct(const PointND &other) const
Definition: point.h:493
~PointND() override=default
double lengthSq() const override
Definition: point.h:449
PointND & operator*=(double scale)
Definition: point.h:474
PointND(const PointND &other)
Definition: point.h:411
void normalize() override
Definition: point.h:445
double angleTo(const PointND &other) const
Definition: point.h:497
double operator[](unsigned int i) const override
Definition: point.h:437
Point * copy() const override
Definition: point.h:417
PointND(unsigned int dim)
Definition: point.h:406
PointND & operator/=(double scale)
Definition: point.h:479
PointND directionVector(const PointND &other)
Definition: point.h:484
PointND & operator+=(const PointND &other)
Definition: point.h:464
virtual ~Point()
Definition: point.h:32
virtual double lengthSq() const =0
virtual Point * copy() const =0
virtual double & operator[](unsigned int i)=0
virtual double length() const =0
virtual void normalize()=0
virtual unsigned int dimension() const =0
virtual double operator[](unsigned int i) const =0
A class to represent vectors of numbers.
Definition: Vector.h:29
void setVal(unsigned int i, TYPE val)
sets the index at a particular value
Definition: Vector.h:87
#define RDKIT_RDGEOMETRYLIB_EXPORT
Definition: export.h:361
Point3DPtrVect::iterator Point3DPtrVect_I
Definition: point.h:529
std::vector< RDGeom::Point * > PointPtrVect
Definition: point.h:523
RDKIT_RDGEOMETRYLIB_EXPORT RDGeom::Point3D operator/(const RDGeom::Point3D &p1, double v)
PointPtrVect::const_iterator PointPtrVect_CI
Definition: point.h:525
std::vector< const RDGeom::Point3D * > Point3DConstPtrVect
Definition: point.h:534
std::map< int, Point2D > INT_POINT2D_MAP
Definition: point.h:542
RDKIT_RDGEOMETRYLIB_EXPORT RDGeom::Point3D operator*(const RDGeom::Point3D &p1, double v)
Point3DConstPtrVect::iterator Point3DConstPtrVect_I
Definition: point.h:535
std::vector< Point3D >::iterator POINT3D_VECT_I
Definition: point.h:539
Point3DPtrVect::const_iterator Point3DPtrVect_CI
Definition: point.h:530
std::vector< RDGeom::Point3D * > Point3DPtrVect
Definition: point.h:527
RDKIT_RDGEOMETRYLIB_EXPORT RDGeom::Point3D operator-(const RDGeom::Point3D &p1, const RDGeom::Point3D &p2)
Point3DConstPtrVect::const_iterator Point3DConstPtrVect_CI
Definition: point.h:536
Point2DPtrVect::iterator Point2DPtrVect_I
Definition: point.h:531
INT_POINT2D_MAP::const_iterator INT_POINT2D_MAP_CI
Definition: point.h:544
RDKIT_RDGEOMETRYLIB_EXPORT double computeDihedralAngle(const Point3D &pt1, const Point3D &pt2, const Point3D &pt3, const Point3D &pt4)
std::vector< Point3D >::const_iterator POINT3D_VECT_CI
Definition: point.h:540
INT_POINT2D_MAP::iterator INT_POINT2D_MAP_I
Definition: point.h:543
std::vector< RDGeom::Point2D * > Point2DPtrVect
Definition: point.h:528
RDKIT_RDGEOMETRYLIB_EXPORT std::ostream & operator<<(std::ostream &target, const RDGeom::Point &pt)
Point2DPtrVect::const_iterator Point2DPtrVect_CI
Definition: point.h:532
std::vector< Point3D > POINT3D_VECT
Definition: point.h:538
RDKIT_RDGEOMETRYLIB_EXPORT double computeSignedDihedralAngle(const Point3D &pt1, const Point3D &pt2, const Point3D &pt3, const Point3D &pt4)
RDKIT_RDGEOMETRYLIB_EXPORT RDGeom::Point3D operator+(const RDGeom::Point3D &p1, const RDGeom::Point3D &p2)
PointPtrVect::iterator PointPtrVect_I
Definition: point.h:524
#define M_PI
Definition: point.h:20