IMP  2.0.0
The Integrative Modeling Platform
saxs/utility.h
Go to the documentation of this file.
1 /**
2  * \file IMP/saxs/utility.h
3  * \brief Functions to deal with very common saxs operations
4  * Copyright 2007-2013 IMP Inventors. All rights reserved.
5 */
6 
7 #ifndef IMPSAXS_UTILITY_H
8 #define IMPSAXS_UTILITY_H
9 
10 #include <IMP/saxs/saxs_config.h>
11 #include "FormFactorTable.h"
12 #include <IMP/exception.h>
13 #include <IMP/core/XYZ.h>
14 
15 IMPSAXS_BEGIN_NAMESPACE
16 
17 inline void get_coordinates(const Particles& particles,
18  std::vector<algebra::Vector3D>& coordinates) {
19  // copy everything in advance for fast access
20  coordinates.resize(particles.size());
21  for (unsigned int i=0; i<particles.size(); i++) {
22  coordinates[i] = core::XYZ(particles[i]).get_coordinates();
23  }
24 }
25 
26 inline void get_form_factors(const Particles& particles,
27  FormFactorTable* ff_table,
28  Floats& form_factors,
29  FormFactorType ff_type) {
30  form_factors.resize(particles.size());
31  for (unsigned int i=0; i<particles.size(); i++) {
32  form_factors[i] = ff_table->get_form_factor(particles[i], ff_type);
33  }
34 }
35 
36 //! compute max distance
37 inline Float compute_max_distance(const Particles& particles) {
38  Float max_dist2 = 0;
39  std::vector<algebra::Vector3D> coordinates(particles.size());
40  get_coordinates(particles, coordinates);
41  for (unsigned int i = 0; i < coordinates.size(); i++) {
42  for (unsigned int j = i + 1; j < coordinates.size(); j++) {
43  Float dist2 = get_squared_distance(coordinates[i], coordinates[j]);
44  if(dist2 > max_dist2)
45  max_dist2 = dist2;
46  }
47  }
48  return sqrt(max_dist2);
49 }
50 
51 //! compute max distance between pairs of particles one from particles1
52 //! and the other from particles2
53 inline Float compute_max_distance(const Particles& particles1,
54  const Particles& particles2) {
55  Float max_dist2 = 0;
56  std::vector<algebra::Vector3D> coordinates1, coordinates2;
57  get_coordinates(particles1, coordinates1);
58  get_coordinates(particles2, coordinates2);
59 
60  for (unsigned int i = 0; i < coordinates1.size(); i++) {
61  for (unsigned int j = i + 1; j < coordinates2.size(); j++) {
62  Float dist2 = get_squared_distance(coordinates1[i], coordinates2[j]);
63  if(dist2 > max_dist2)
64  max_dist2 = dist2;
65  }
66  }
67  return sqrt(max_dist2);
68 }
69 
70 //! compute radius_of_gyration
71 inline Float radius_of_gyration(const Particles& particles) {
72  algebra::Vector3D centroid(0.0, 0.0, 0.0);
73  std::vector<algebra::Vector3D> coordinates(particles.size());
74  get_coordinates(particles, coordinates);
75  for (unsigned int i = 0; i < particles.size(); i++) {
76  centroid += coordinates[i];
77  }
78  centroid /= particles.size();
79  Float rg = 0;
80  for (unsigned int i = 0; i < particles.size(); i++) {
81  rg += get_squared_distance(coordinates[i], centroid);
82  }
83  rg /= particles.size();
84  return sqrt(rg);
85 }
86 
87 IMPSAXS_END_NAMESPACE
88 
89 #endif /* IMPSAXS_UTILITY_H */