Chemical Data Processing Library C++ API - Version 1.2.0
FastGaussianShapeAlignment.hpp
Go to the documentation of this file.
1 /*
2  * FastGaussianShapeAlignment.hpp
3  *
4  * This file is part of the Chemical Data Processing Toolkit
5  *
6  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; see the file COPYING. If not, write to
20  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
29 #ifndef CDPL_SHAPE_FASTGAUSSIANSHAPEALIGNMENT_HPP
30 #define CDPL_SHAPE_FASTGAUSSIANSHAPEALIGNMENT_HPP
31 
32 #include <vector>
33 #include <cstddef>
34 #include <utility>
35 #include <unordered_map>
36 #include <memory>
37 #include <functional>
38 
39 #include <boost/functional/hash.hpp>
40 #include <boost/random/mersenne_twister.hpp>
41 
42 #include "CDPL/Shape/APIPrefix.hpp"
47 #include "CDPL/Math/Matrix.hpp"
51 
52 
53 namespace CDPL
54 {
55 
56  namespace Shape
57  {
58 
60  {
61 
62  typedef std::vector<AlignmentResult> ResultList;
63 
64  public:
65  static constexpr double DEF_OPTIMIZATION_STOP_GRADIENT = 1.0;
66  static constexpr std::size_t DEF_MAX_OPTIMIZATION_ITERATIONS = 20;
67  static constexpr unsigned int DEF_RESULT_SELECTION_MODE = AlignmentResultSelectionMode::BEST_PER_REFERENCE_SET;
68  static constexpr double DEF_SYMMETRY_THRESHOLD = 0.15;
69  static constexpr std::size_t DEF_NUM_RANDOM_STARTS = 4;
70  static constexpr double DEF_MAX_RANDOM_TRANSLATION = 2.0;
71 
72  typedef std::shared_ptr<FastGaussianShapeAlignment> SharedPointer;
73 
74  typedef ResultList::const_iterator ConstResultIterator;
75  typedef ResultList::iterator ResultIterator;
76 
77  typedef std::function<double(const AlignmentResult&)> ScoringFunction;
78  typedef std::function<bool(const AlignmentResult&, const AlignmentResult&)> ResultCompareFunction;
79 
81 
83 
85 
87 
89 
91 
93 
95 
97 
99 
100  void setResultSelectionMode(unsigned int mode);
101 
102  unsigned int getResultSelectionMode() const;
103 
104  void performAlignment(bool perf_align);
105 
106  bool performAlignment() const;
107 
108  void optimizeOverlap(bool optimize);
109 
110  bool optimizeOverlap() const;
111 
112  void greedyOptimization(bool greedy);
113 
114  bool greedyOptimization() const;
115 
116  void setMaxNumOptimizationIterations(std::size_t max_iter);
117 
118  std::size_t getMaxNumOptimizationIterations() const;
119 
120  void setOptimizationStopGradient(double grad_norm);
121 
123 
125 
126  void addReferenceShape(const GaussianShape& shape, bool new_set = true);
127 
128  void addReferenceShapes(const GaussianShapeSet& shapes, bool new_set = true);
129 
130  std::size_t getNumReferenceShapes() const;
131 
132  void genShapeCenterStarts(bool generate);
133 
134  bool genShapeCenterStarts() const;
135 
136  void genColorCenterStarts(bool generate);
137 
138  bool genColorCenterStarts() const;
139 
140  void genNonColorCenterStarts(bool generate);
141 
143 
144  void genRandomStarts(bool generate);
145 
146  bool genRandomStarts() const;
147 
148  void genForAlignedShapeCenters(bool generate);
149 
151 
152  void genForReferenceShapeCenters(bool generate);
153 
155 
156  void genForLargerShapeCenters(bool generate);
157 
159 
160  void setSymmetryThreshold(double thresh);
161 
163 
164  void setMaxRandomTranslation(double max_trans);
165 
166  double getMaxRandomTranslation() const;
167 
168  void setNumRandomStarts(std::size_t num_starts);
169 
170  std::size_t getNumRandomStarts() const;
171 
172  void setRandomSeed(unsigned int seed);
173 
174  bool align(const GaussianShape& shape);
175 
176  bool align(const GaussianShapeSet& shapes);
177 
178  std::size_t getNumResults() const;
179 
180  const AlignmentResult& getResult(std::size_t idx) const;
181 
182  AlignmentResult& getResult(std::size_t idx);
183 
185 
187 
189 
191 
193 
195 
197 
199 
200  private:
201  struct ShapeData
202  {
203 
204  struct Element
205  {
206 
207  std::size_t color;
209  double radius;
210  double delta;
211  double weightFactor;
212  double volume;
213  };
214 
215  typedef std::vector<Element> ElementArray;
216 
217  ElementArray elements;
218  std::size_t colElemOffs;
219  std::size_t setIndex;
220  std::size_t index;
221  unsigned int symClass;
223  double selfOverlap;
224  double colSelfOverlap;
225  bool equalNonColDelta;
226  };
227 
228  typedef std::pair<std::size_t, std::size_t> ResultID;
229 
230  void alignAndProcessResults(std::size_t ref_idx, std::size_t al_idx);
231  void processResult(AlignmentResult& res, std::size_t ref_idx, std::size_t al_idx);
232 
233  void setupShapeData(const GaussianShape& shape, ShapeData& data, bool ref);
234  void setupShapeDataElement(const GaussianShape::Element& gs_elem, ShapeData::Element& sd_elem) const;
235 
236  void prepareForAlignment();
237 
238  bool generateStartTransforms(const ShapeData& ref_data);
239 
240  void generateTransformsForElementCenters(const ShapeData& data, unsigned int axes_swap_flags, bool ref_shape);
241  void generateTransforms(const Math::Vector3D& ctr_trans, unsigned int axes_swap_flags);
242 
243  template <typename QE>
244  void addStartTransform(Math::Vector3D::ConstPointer ctr_trans_data, const Math::QuaternionExpression<QE>& rot_quat);
245 
246  void transformAlignedShape();
247 
248  double calcAlignmentFunctionValue(const QuaternionTransformation& xform_quat);
249  double calcAlignmentFunctionGradient(const QuaternionTransformation& xform_quat, QuaternionTransformation& xform_grad);
250 
251  double calcOverlap(const ShapeData& ref_data, const ShapeData& ovl_data, bool color) const;
252  double calcOverlapGradient(const ShapeData& ref_data, Math::Vector3DArray& grad) const;
253 
254  bool getResultIndex(const ResultID& res_id, std::size_t& res_idx);
255 
256  typedef std::vector<ShapeData> ShapeDataArray;
257  typedef std::unordered_map<ResultID, std::size_t, boost::hash<ResultID> > ResultIndexMap;
258  typedef std::vector<QuaternionTransformation> StartTransformList;
259  typedef boost::random::mt11213b RandomEngine;
261 
262  bool perfAlignment;
263  bool optOverlap;
264  bool greedyOpt;
265  std::size_t maxNumOptIters;
266  double optStopGrad;
267  unsigned int resultSelMode;
268  ResultCompareFunction resultCmpFunc;
269  ScoringFunction scoringFunc;
270  ShapeDataArray refShapeData;
271  ShapeData algdShapeData;
272  ResultIndexMap resIndexMap;
273  ResultList results;
274  std::size_t currSetIndex;
275  std::size_t currShapeIndex;
276  bool shapeCtrStarts;
277  bool colCtrStarts;
278  bool nonColCtrStarts;
279  bool randomStarts;
280  bool genForAlgdShape;
281  bool genForRefShape;
282  bool genForLargerShape;
283  double symThreshold;
284  double maxRandomTrans;
285  std::size_t numRandomStarts;
286  std::size_t numSubTransforms;
287  RandomEngine randomEngine;
288  StartTransformList startTransforms;
289  Math::Vector3DArray startPoseCoords;
290  Math::Vector3DArray optPoseCoordsGrad;
291  BFGSMinimizer minimizer;
292  Math::Matrix4D xformMatrix;
293  QuaternionTransformation normXformQuat;
294  std::size_t currRefShapeIdx;
295  };
296  } // namespace Shape
297 } // namespace CDPL
298 
299 #endif // CDPL_SHAPE_FASTGAUSSIANSHAPEALIGNMENT_HPP
Definition of constants in namespace CDPL::Shape::AlignmentResultSelectionMode.
Definition of the class CDPL::Shape::AlignmentResult.
Implementation of the BFGS optimization algorithm.
Definition of the class CDPL::Shape::GaussianShapeSet.
Definition of matrix data types.
Definition of various quaternion expression types and operations.
Definition of the type CDPL::Shape::QuaternionTransformation.
Definition of the preprocessor macro CDPL_SHAPE_API.
#define CDPL_SHAPE_API
Tells the compiler/linker which classes, functions and variables are part of the library API.
Definition of the class CDPL::Math::VectorArray.
Definition: Vector.hpp:1053
Definition: Expression.hpp:98
Definition: AlignmentResult.hpp:45
Definition: FastGaussianShapeAlignment.hpp:60
ResultList::const_iterator ConstResultIterator
Definition: FastGaussianShapeAlignment.hpp:74
AlignmentResult & getResult(std::size_t idx)
void genForAlignedShapeCenters(bool generate)
void setNumRandomStarts(std::size_t num_starts)
const ResultCompareFunction & getResultCompareFunction() const
void setMaxRandomTranslation(double max_trans)
std::shared_ptr< FastGaussianShapeAlignment > SharedPointer
Definition: FastGaussianShapeAlignment.hpp:72
void setMaxNumOptimizationIterations(std::size_t max_iter)
FastGaussianShapeAlignment(const GaussianShapeSet &ref_shapes)
std::function< bool(const AlignmentResult &, const AlignmentResult &)> ResultCompareFunction
Definition: FastGaussianShapeAlignment.hpp:78
void setResultCompareFunction(const ResultCompareFunction &func)
void genForReferenceShapeCenters(bool generate)
void setRandomSeed(unsigned int seed)
void setOptimizationStopGradient(double grad_norm)
unsigned int getResultSelectionMode() const
ConstResultIterator begin() const
ConstResultIterator getResultsBegin() const
void setResultSelectionMode(unsigned int mode)
std::size_t getMaxNumOptimizationIterations() const
void genNonColorCenterStarts(bool generate)
void setScoringFunction(const ScoringFunction &func)
const AlignmentResult & getResult(std::size_t idx) const
const ScoringFunction & getScoringFunction() const
bool align(const GaussianShape &shape)
void performAlignment(bool perf_align)
ResultList::iterator ResultIterator
Definition: FastGaussianShapeAlignment.hpp:75
FastGaussianShapeAlignment(const GaussianShape &ref_shape)
void genForLargerShapeCenters(bool generate)
ConstResultIterator end() const
std::function< double(const AlignmentResult &)> ScoringFunction
Definition: FastGaussianShapeAlignment.hpp:77
FastGaussianShapeAlignment(const FastGaussianShapeAlignment &alignment)=delete
void addReferenceShapes(const GaussianShapeSet &shapes, bool new_set=true)
FastGaussianShapeAlignment & operator=(const FastGaussianShapeAlignment &alignment)=delete
bool align(const GaussianShapeSet &shapes)
ConstResultIterator getResultsEnd() const
void addReferenceShape(const GaussianShape &shape, bool new_set=true)
Definition: GaussianShapeSet.hpp:46
Definition: GaussianShape.hpp:55
A data type for the descripton of arbitrary shapes composed of spheres approximated by gaussian funct...
Definition: GaussianShape.hpp:51
VectorArray< Vector3D > Vector3DArray
An array of Math::Vector3D objects.
Definition: VectorArray.hpp:84
CVector< double, 3 > Vector3D
A bounded 3 element vector holding floating point values of type double.
Definition: Vector.hpp:1637
constexpr unsigned int BEST_PER_REFERENCE_SET
Definition: AlignmentResultSelectionMode.hpp:45
CDPL_SHAPE_API void transform(GaussianShape &shape, const Math::Matrix4D &xform)
The namespace of the Chemical Data Processing Library.
Definition: FastGaussianShapeAlignment.hpp:205
Math::Vector3D center
Definition: FastGaussianShapeAlignment.hpp:208
double radius
Definition: FastGaussianShapeAlignment.hpp:209
std::size_t color
Definition: FastGaussianShapeAlignment.hpp:207
double volume
Definition: FastGaussianShapeAlignment.hpp:212
double weightFactor
Definition: FastGaussianShapeAlignment.hpp:211
double delta
Definition: FastGaussianShapeAlignment.hpp:210