IMP logo
IMP Reference Guide  develop.eb1b99edaa,2026/06/22
The Integrative Modeling Platform
Vector3D.h
Go to the documentation of this file.
1 /**
2  * \file IMP/Vector3D.h \brief Simple 3D vector class.
3  *
4  * Copyright 2007-2026 IMP Inventors. All rights reserved.
5  *
6  */
7 
8 #ifndef IMPKERNEL_VECTOR_3D_H
9 #define IMPKERNEL_VECTOR_3D_H
10 
11 #include <IMP/types.h>
12 #include <IMP/base_macros.h>
13 #include <IMP/exception.h>
14 
15 #include <numeric>
16 #include <cmath>
17 
18 #include "VectorD.h"
19 
20 IMPKERNEL_BEGIN_NAMESPACE
21 
22 /** \name 3D Vectors
23  We provide a specialization of VectorD for 3-space and
24  several additional functions on it.
25  @{
26 */
27 
28 //! Return the vector product (cross product) of two vectors.
29 /** \see Vector3D
30  */
31 inline Vector3D get_vector_product(const Vector3D &p1, const Vector3D &p2) {
32  return Vector3D(p1[1] * p2[2] - p1[2] * p2[1], p1[2] * p2[0] - p1[0] * p2[2],
33  p1[0] * p2[1] - p1[1] * p2[0]);
34 }
35 
36 /* //! Get angle between vectors p1 and p2 (with appropriate sign depending on where */
37 /* //! p2 is in relation to p1) */
38 /* inline Vector3D get_angle(const Vector3D &p1, const Vector3D &p2) { */
39 /* // see wikipedia on great circles - atan formula is numerically more stable */
40 /* // than using acos or asin. */
41 /* double sin_sigma = get_vector_product(p1,p2).get_magnitude(); */
42 /* double cos_sigma = p1*p2; */
43 /* double sigma = atan2(sin_sigma,cos_sigma); */
44 /* return sigma; */
45 /* } */
46 
47 //! Return a vector that is perpendicular to the given vector
48 /** \see Vector3D
49 */
51  unsigned int maxi = 0;
52  if (std::abs(v[1]) > std::abs(v[0])) maxi = 1;
53  if (std::abs(v[2]) > std::abs(v[maxi])) maxi = 2;
54  if (std::abs(v[maxi]) < .0001) {
55  return Vector3D(0.0, 0.0, 0.0);
56  } else {
57  Vector3D ret = get_ones_vector_d<3>();
58  ret[maxi] = (-v[(maxi + 1) % 3] - v[(maxi + 2) % 3]) / v[maxi];
59  IMP_INTERNAL_CHECK(ret * v < .0001, "Vectors are not perpendicular");
60  return ret;
61  }
62 }
63 
64 //! Return the centroid of a set of vectors
65 /** \see Vector3D
66  */
67 inline Vector3D get_centroid(const Vector3Ds &ps) {
68  return std::accumulate(ps.begin(), ps.end(), get_zero_vector_d<3>()) /
69  ps.size();
70 }
71 
72 //! Return the radius of gyration of a set of points
73 /**
74  \see IMP::atom::get_radius_of_gyration()
75  */
76 inline double get_radius_of_gyration(const Vector3Ds &ps) {
77  Vector3D centroid = get_centroid(ps);
78  double rg = 0;
79  for (unsigned int i = 0; i < ps.size(); i++) {
80  rg += get_squared_distance(ps[i], centroid);
81  }
82  rg /= ps.size();
83  return sqrt(rg);
84 }
85 
86 /** @} */
87 
88 IMPKERNEL_END_NAMESPACE
89 
90 #endif /* IMPKERNEL_VECTOR_3D_H */
Basic types used by IMP.
Vector3D get_orthogonal_vector(const Vector3D &v)
Return a vector that is perpendicular to the given vector.
Definition: Vector3D.h:50
Exception definitions and assertions.
Vector3D get_centroid(const Vector3Ds &ps)
Return the centroid of a set of vectors.
Definition: Vector3D.h:67
A more IMP-like version of the std::vector.
Definition: Vector.h:50
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
Definition: check_macros.h:139
double get_radius_of_gyration(const Vector3Ds &ps)
Return the radius of gyration of a set of points.
Definition: Vector3D.h:76
VectorD< 3 > Vector3D
Definition: VectorD.h:407
Simple D vector class.
double get_squared_distance(const VectorD< D > &v1, const VectorD< D > &v2)
Compute the squared distance between two vectors.
Definition: VectorD.h:187
Vector3D get_vector_product(const Vector3D &p1, const Vector3D &p2)
Return the vector product (cross product) of two vectors.
Definition: Vector3D.h:31
Various general useful macros for IMP.