IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
vector_generators.h
Go to the documentation of this file.
1/**
2 * \file IMP/algebra/vector_generators.h
3 * \brief Functions to generate vectors.
4 *
5 * Copyright 2007-2026 IMP Inventors. All rights reserved.
6 *
7 */
8
9#ifndef IMPALGEBRA_VECTOR_GENERATORS_H
10#define IMPALGEBRA_VECTOR_GENERATORS_H
11
12#include "VectorD.h"
13#include "Cylinder3D.h"
14#include "Cone3D.h"
15#include "Sphere3D.h"
16#include "SpherePatch3D.h"
17#include "BoundingBoxD.h"
18#include "utility.h"
19#include "internal/grid_range_d.h"
20#include "internal/internal_vector_generators.h"
21
22IMPALGEBRA_BEGIN_NAMESPACE
23
24/** @name Vector Generators
25
26 These functions generate vector objects. Some
27 of the methods, those with random in their name, generate
28 a single vector chosen uniformly from the specified domain.
29 Others, the cover methods, generate a set of points distributed
30 (somewhat) evenly over the domain.
31 @{
32 */
33
34//! Generate a random vector in a box with uniform density
35/** \see BoundingBoxD
36 \see VectorD
37 */
38template <int D>
40 return internal::get_random_vector_in(bb);
41}
42
43//! Generate a random vector on a box with uniform density
44/** \see BoundingBoxD
45 \see VectorD
46 */
47template <int D>
49 return internal::RandomVectorOnBB<D>::get(bb);
50}
51
52//! Generate a random vector in a sphere with uniform density
53/** \see VectorD
54 \see SphereD
55 */
56template <int D>
58 IMP_USAGE_CHECK(s.get_radius() > 0, "The sphere must have positive radius");
59 BoundingBoxD<D> bb = get_bounding_box(s);
60 double norm;
61 VectorD<D> ret;
62 double r2 = get_squared(s.get_radius());
63 // \todo This algorithm could be more efficient.
64 do {
65 ret = get_random_vector_in(bb);
66 norm = (s.get_center() - ret).get_squared_magnitude();
67 } while (norm > r2);
68 return ret;
69}
70
71#ifndef SWIG
72/** Generates a random vector in a circle
73 with uniform density with respect to the area of the circle
74
75 @param s a 2D sphere (circle)
76
77 \see VectorD
78 \see SphereD
79*/
80IMPALGEBRAEXPORT VectorD<2> get_random_vector_in(const SphereD<2> &s);
81#endif
82
83//! Generate a random vector in a cylinder with uniform density
84/** \see VectorD
85 \see Cylinder3D
86 */
87IMPALGEBRAEXPORT Vector3D get_random_vector_in(const Cylinder3D &c);
88
89//! returns a random vector on a sphere of radius 1 with uniform density
90//! and implementation optimized for the 3D + unit vector case
92 return internal::get_random_vector_on_unit_sphere();
93}
94
95//! Generate a random vector on a sphere with uniform density
96/** \see VectorD
97 \see SphereD
98 */
99template <int D>
101 return internal::get_random_vector_on(s);
102}
103
104//! Generate a set of vectors which covers a sphere uniformly
105/** The function is currently pretty slow, especially in non-optimized
106 builds. Complain if this bugs you. We might be able to do better,
107 at least in 3D.
108
109 Creates at least the requested number of points.
110 \cgalpredicate
111
112 \see VectorD
113 \see SphereD
114 */
115template <int D>
117 unsigned int n) {
118 return internal::uniform_cover_sphere(n, s.get_center(), s.get_radius(),
119 true);
120}
121
122//! Generate a set of 3d points that uniformly cover a cylinder
123/** \see VectorD
124 \see Cylinder3D
125*/
126IMPALGEBRAEXPORT Vector3Ds
127 get_uniform_surface_cover(const Cylinder3D &cyl, int number_of_points);
128
129//! Generate a set of 3D points that uniformly cover a hemisphere
130/** The points all lie on the upper hemisphere, eg, all their
131 z coordinates are greater than those of the center of the sphere.
132 */
133template <int D>
135 const SphereD<D> &s, unsigned int n) {
136 return internal::uniform_cover_sphere(n, s.get_center(), s.get_radius(),
137 false);
138}
139
140//! Generate a random vector on a unit simplex with uniform density
141/** \see VectorD
142 \see UnitSimplexD
143 */
144template <int D>
146 return internal::get_random_vector_on(s);
147}
148
149//! Generate a grid of 3d points on a cylinder surface
150/** \see Vector3D
151 \see Cylinder3D
152*/
153IMPALGEBRAEXPORT Vector3Ds
154 get_grid_surface_cover(const Cylinder3D &cyl, int number_of_cycles,
155 int number_of_points_on_cycle);
156
157//! Generate a set of 3d points that uniformly cover a patch of a sphere
158/**
159 \note the implementation can be improved
160 \see SpherePatch3D
161 \see VectorD
162 */
163IMPALGEBRAEXPORT Vector3Ds
165 unsigned int number_of_points);
166
167/** \see VectorD
168 \see Cone3D
169*/
170IMPALGEBRAEXPORT Vector3Ds
172 unsigned int number_of_points);
173
174/** Cover the interior of the bounding box by equal sized
175 parallelograms of approximately full-width s, returning the
176 list of centers of the cubes.
177 */
178template <int D>
180 const BoundingBoxD<D> &bb, double s) {
181 const unsigned int dim = bb.get_dimension();
182 Ints ns(dim);
183 algebra::VectorD<D> start(bb.get_corner_0());
184 algebra::VectorD<D> spacing(bb.get_corner_0());
185 for (unsigned int i = 0; i < dim; ++i) {
186 double w = bb.get_corner_1()[i] - bb.get_corner_0()[i];
187 if (w < s) {
188 start[i] = bb.get_corner_0()[i] + w * .5;
189 spacing[i] = 1;
190 ns[i] = 1;
191 } else {
192 ns[i] = static_cast<int>(std::floor(w / s));
193 spacing[i] = w / ns[i];
194 start[i] = bb.get_corner_0()[i] + spacing[i] * .5;
195 }
196 }
197 Ints cur(dim, 0);
199 do {
200 ret.push_back(start + get_elementwise_product(cur, spacing));
201 unsigned int i;
202 for (i = 0; i < dim; ++i) {
203 ++cur[i];
204 if (cur[i] == ns[i]) {
205 cur[i] = 0;
206 } else {
207 break;
208 }
209 }
210 if (i == dim) break;
211 } while (true);
212 return ret;
213}
214
215//! Generate a random chain with no collisions
216/** This function generates a random chain, starting at (0,0,0)
217 with n particles each with radius r. Consecutive particles are
218 approximately distance 2r apart and no pair of particles is closer
219 than 2r.
220
221 If an obstacles parameter is provided then chain spheres also don't
222 intersect the obstacle spheres.
223
224 \note The current implementation is not very clever and can be made
225 more clever if needed.
226 */
227IMPALGEBRAEXPORT Vector3Ds
228 get_random_chain(unsigned int n, double r,
229 const Vector3D &start = Vector3D(0, 0, 0),
230 const Sphere3Ds &obstacles = Sphere3Ds());
231
232/** @} */
233
234/** Return a cover of the surface of the volume defined by a union of balls
235 bounded by the spheres.
236
237 This is effectively a sampling of the solvent exposed surface of a set of
238 spheres. The density of points has approximately the passed value.
239
240 This method is much faster than get_connolly_surface().
241*/
242IMPALGEBRAEXPORT Vector3Ds
244 double points_per_square_angstrom);
245IMPALGEBRA_END_NAMESPACE
246
247#endif /* IMPALGEBRA_VECTOR_GENERATORS_H */
A bounding box in D dimensions.
Represent a cone in 3D.
Represent a cylinder in 3D.
Simple 3D sphere class.
Simple 3D sphere patch class.
Simple D vector class.
Functions to deal with very common math operations.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
A Cartesian vector in D-dimensions.
Definition VectorD.h:38
A more IMP-like version of the std::vector.
Definition Vector.h:60
An axis-aligned bounding box.
Represent a cone in 3D.
Definition Cone3D.h:32
Represent a cylinder in 3D.
Definition Cylinder3D.h:27
Represent a sphere in D-dimensions.
Definition SphereD.h:25
A sphere patch is defined as all points above the plane and on the sphere.
Represent a unit simplex embedded in D-dimensional real space.
Vector< VectorD< D > > get_uniform_surface_cover(const SphereD< D > &s, unsigned int n)
Generate a set of vectors which covers a sphere uniformly.
VectorD< D > get_elementwise_product(const VectorD< D > &a, const VectorD< D > &b)
Return the vector that is the elementwise product of the two.
Definition VectorD.h:443
Vector< VectorD< D > > get_uniform_upper_hemisphere_cover(const SphereD< D > &s, unsigned int n)
Generate a set of 3D points that uniformly cover a hemisphere.
VectorD< D > get_random_vector_on(const BoundingBoxD< D > &bb)
Generate a random vector on a box with uniform density.
IMP::Vector< Sphere3D > Sphere3Ds
Definition SphereD.h:103
VectorD< D > get_random_vector_in(const BoundingBoxD< D > &bb)
Generate a random vector in a box with uniform density.
Vector< VectorD< D > > get_grid_interior_cover_by_spacing(const BoundingBoxD< D > &bb, double s)
VectorD< 3 > get_random_vector_on_unit_sphere()
Vector3Ds get_grid_surface_cover(const Cylinder3D &cyl, int number_of_cycles, int number_of_points_on_cycle)
Generate a grid of 3d points on a cylinder surface.
Vector3Ds get_random_chain(unsigned int n, double r, const Vector3D &start=Vector3D(0, 0, 0), const Sphere3Ds &obstacles=Sphere3Ds())
Generate a random chain with no collisions.
Vector< VectorD< 3 > > Vector3Ds
Definition VectorD.h:409
IMP::Vector< Int > Ints
Standard way to pass a bunch of Int values.
Definition types.h:48
VectorD< 3 > Vector3D
Definition VectorD.h:407