IMP  2.3.1
The Integrative Modeling Platform
Vector3D.h
Go to the documentation of this file.
1 /**
2  * \file IMP/algebra/Vector3D.h \brief Simple 3D vector class.
3  *
4  * Copyright 2007-2014 IMP Inventors. All rights reserved.
5  *
6  */
7 
8 #ifndef IMPALGEBRA_VECTOR_3D_H
9 #define IMPALGEBRA_VECTOR_3D_H
10 
11 #include <IMP/base/types.h>
12 #include <IMP/base/base_macros.h>
13 #include <IMP/base/exception.h>
14 
15 #include <numeric>
16 //#include <cmath>
17 
18 #include "VectorD.h"
19 
20 IMPALGEBRA_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 //! Returns 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 //! Return a vector that is perpendicular to the given vector
36 /** \note This is occasionally refered to in the code as a "vertical" vector.
37  \see Vector3D
38 */
40  unsigned int maxi = 0;
41  if (std::abs(v[1]) > std::abs(v[0])) maxi = 1;
42  if (std::abs(v[2]) > std::abs(v[maxi])) maxi = 2;
43  if (std::abs(v[maxi]) < .0001) {
44  return Vector3D(0.0, 0.0, 0.0);
45  } else {
46  Vector3D ret = get_ones_vector_d<3>();
47  ret[maxi] = (-v[(maxi + 1) % 3] - v[(maxi + 2) % 3]) / v[maxi];
48  IMP_INTERNAL_CHECK(ret * v < .0001, "Vectors are not perpendicular");
49  return ret;
50  }
51 }
52 
53 //! Returns the centroid of a set of vectors
54 /** \see Vector3D
55  */
56 inline Vector3D get_centroid(const Vector3Ds &ps) {
57  return std::accumulate(ps.begin(), ps.end(), get_zero_vector_d<3>()) /
58  ps.size();
59 }
60 
61 //! Return the radius of gyration of a set of points
62 /**
63  \see IMP::atom::get_radius_of_gyration()
64  */
65 inline double get_radius_of_gyration(const Vector3Ds &ps) {
66  algebra::Vector3D centroid = get_centroid(ps);
67  double rg = 0;
68  for (unsigned int i = 0; i < ps.size(); i++) {
69  rg += get_squared_distance(ps[i], centroid);
70  }
71  rg /= ps.size();
72  return sqrt(rg);
73 }
74 
75 /** @} */
76 
77 IMPALGEBRA_END_NAMESPACE
78 
79 #endif /* IMPALGEBRA_VECTOR_3D_H */
Basic types used by IMP.
double get_squared_distance(const VectorD< D > &v1, const VectorD< D > &v2)
Compute the squared distance between two vectors.
Definition: VectorD.h:201
Exception definitions and assertions.
#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:141
Vector3D get_vector_product(const Vector3D &p1, const Vector3D &p2)
Returns the vector product (cross product) of two vectors.
Definition: Vector3D.h:31
Simple D vector class.
double get_radius_of_gyration(const Vector3Ds &ps)
Return the radius of gyration of a set of points.
Definition: Vector3D.h:65
Vector3D get_centroid(const Vector3Ds &ps)
Returns the centroid of a set of vectors.
Definition: Vector3D.h:56
VectorD< 3 > Vector3D
Definition: VectorD.h:395
Vector3D get_orthogonal_vector(const Vector3D &v)
Return a vector that is perpendicular to the given vector.
Definition: Vector3D.h:39
Various general useful macros for IMP.