IMP  2.0.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-2013 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 
23 /** \name 3D Vectors
24  We provide a specialization of VectorD for 3-space and
25  several additional functions on it.
26  @{
27 */
28 
29 //! Returns the vector product (cross product) of two vectors.
30 /** \relatesalso Vector3D
31  */
33  const Vector3D& p2) {
34  return Vector3D(p1[1]*p2[2]-p1[2]*p2[1],
35  p1[2]*p2[0]-p1[0]*p2[2],
36  p1[0]*p2[1]-p1[1]*p2[0]);
37 }
38 //! Return a vector that is perpendicular to the given vector
39 /** Or, if you are Israeli, it is a vertical vector.
40  \relatesalso Vector3D
41 */
43  unsigned int maxi=0;
44  if (std::abs(v[1]) > std::abs(v[0])) maxi=1;
45  if (std::abs(v[2]) > std::abs(v[maxi])) maxi=2;
46  if (std::abs(v[maxi]) < .0001) {
47  return Vector3D(0.0,0.0,0.0);
48  } else {
49  Vector3D ret= get_ones_vector_d<3>();
50  ret[maxi]=(-v[(maxi+1)%3]-v[(maxi+2)%3])/v[maxi];
51  IMP_INTERNAL_CHECK(ret*v < .0001, "Vectors are not perpendicular");
52  return ret;
53  }
54 }
55 
56 //! Returns the centroid of a set of vectors
57 /** \relatesalso Vector3D
58  */
59 inline Vector3D get_centroid(const Vector3Ds &ps) {
60  return std::accumulate(ps.begin(), ps.end(),
61  get_zero_vector_d<3>())/ps.size();
62 }
63 
64 //! Return the radius of gyration of a set of points
65 /**
66  \see IMP::atom::get_radius_of_gyration()
67  */
68 inline double get_radius_of_gyration(const Vector3Ds &ps) {
69  algebra::Vector3D centroid= get_centroid(ps);
70  double rg = 0;
71  for (unsigned int i = 0; i < ps.size(); i++) {
72  rg += get_squared_distance(ps[i], centroid);
73  }
74  rg /= ps.size();
75  return sqrt(rg);
76 }
77 
78 
79 /** @} */
80 
81 IMPALGEBRA_END_NAMESPACE
82 
83 #endif /* IMPALGEBRA_VECTOR_3D_H */