Chemical Data Processing Library C++ API - Version 1.2.0
GaussianShapeAlignment.hpp
Go to the documentation of this file.
1 /*
2  * GaussianShapeAlignment.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_GAUSSIANSHAPEALIGNMENT_HPP
30 #define CDPL_SHAPE_GAUSSIANSHAPEALIGNMENT_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/iterator/transform_iterator.hpp>
41 
42 #include "CDPL/Shape/APIPrefix.hpp"
48 #include "CDPL/Math/Matrix.hpp"
50 
51 
52 namespace CDPL
53 {
54 
55  namespace Shape
56  {
57 
59  {
60 
61  typedef std::vector<AlignmentResult> ResultList;
62  typedef std::vector<GaussianShapeFunction*> ShapeFunctionList;
63 
64  typedef const GaussianShape& (*GetShapeFunction)(const GaussianShapeFunction*);
65 
66  public:
67  static constexpr double DEF_OPTIMIZATION_STOP_GRADIENT = 1.0;
68  static constexpr std::size_t DEF_MAX_OPTIMIZATION_ITERATIONS = 20;
69  static constexpr std::size_t DEF_MAX_PRODUCT_ORDER = 1;
70  static constexpr unsigned int DEF_RESULT_SELECTION_MODE = AlignmentResultSelectionMode::BEST_PER_REFERENCE_SET;
71  static constexpr double DEF_DISTANCE_CUTOFF = 0.0;
72 
73  typedef std::shared_ptr<GaussianShapeAlignment> SharedPointer;
74 
75  typedef ResultList::const_iterator ConstResultIterator;
76  typedef ResultList::iterator ResultIterator;
77  typedef boost::transform_iterator<GetShapeFunction, ShapeFunctionList::const_iterator> ConstShapeIterator;
78 
81  typedef std::function<double(const AlignmentResult&)> ScoringFunction;
82  typedef std::function<bool(const AlignmentResult&, const AlignmentResult&)> ResultCompareFunction;
83 
85 
87 
89 
90  GaussianShapeAlignment(const GaussianShapeAlignment& alignment) = delete;
91 
93 
95 
97 
99 
101 
103 
105 
107 
109 
111 
113 
115 
117 
119 
121 
123 
125 
127 
128  void setResultSelectionMode(unsigned int mode);
129 
130  unsigned int getResultSelectionMode() const;
131 
132  void calcSelfOverlaps(bool calc);
133 
134  bool calcSelfOverlaps() const;
135 
136  void calcColorSelfOverlaps(bool calc);
137 
138  bool calcColorSelfOverlaps() const;
139 
140  void calcColorOverlaps(bool calc);
141 
142  bool calcColorOverlaps() const;
143 
144  void performAlignment(bool perf_align);
145 
146  bool performAlignment() const;
147 
148  void optimizeOverlap(bool optimize);
149 
150  bool optimizeOverlap() const;
151 
152  void greedyOptimization(bool greedy);
153 
154  bool greedyOptimization() const;
155 
156  void setMaxNumOptimizationIterations(std::size_t max_iter);
157 
158  std::size_t getMaxNumOptimizationIterations() const;
159 
160  void setOptimizationStopGradient(double grad_norm);
161 
163 
164  void setMaxOrder(std::size_t max_order);
165 
166  std::size_t getMaxOrder() const;
167 
168  void setDistanceCutoff(double cutoff);
169 
170  double getDistanceCutoff() const;
171 
173 
174  void addReferenceShape(const GaussianShape& shape, bool new_set = true);
175 
176  void addReferenceShapes(const GaussianShapeSet& shapes, bool new_set = true);
177 
178  std::size_t getNumReferenceShapes() const;
179 
180  const GaussianShape& getReferenceShape(std::size_t idx) const;
181 
183 
185 
186  bool align(const GaussianShape& shape);
187 
188  bool align(const GaussianShapeSet& shapes);
189 
190  std::size_t getNumResults() const;
191 
192  const AlignmentResult& getResult(std::size_t idx) const;
193 
194  AlignmentResult& getResult(std::size_t idx);
195 
197 
199 
201 
203 
204  private:
205  typedef GaussianShape::SharedPointer GaussianShapePtr;
206 
207  struct ShapeMetaData
208  {
209 
210  std::size_t setIndex;
211  std::size_t index;
212  unsigned int symClass;
214  double selfOverlap;
215  double colSelfOverlap;
216  GaussianShapePtr shape;
217  };
218 
219  typedef std::pair<std::size_t, std::size_t> ResultID;
220 
221  void prepareForAlignment(GaussianShapeFunction& func, ShapeMetaData& data, bool ref);
222 
223  void processResults(std::size_t ref_idx, std::size_t al_idx);
224 
225  bool getResultIndex(const ResultID& res_id, std::size_t& res_idx);
226 
227  GaussianShapeFunction* allocShapeFunction(const GaussianShape& shape);
228 
229  typedef Util::ObjectStack<GaussianShapeFunction> ShapeFunctionCache;
230  typedef std::vector<ShapeMetaData> ShapeMetaDataArray;
231  typedef std::unordered_map<ResultID, std::size_t, boost::hash<ResultID> > ResultIndexMap;
232 
233  ShapeFunctionCache shapeFuncCache;
234  bool calcSlfOverlaps;
235  bool calcColSlfOverlaps;
236  unsigned int resultSelMode;
237  ResultCompareFunction resultCmpFunc;
238  ScoringFunction scoringFunc;
239  GaussianShapeFunctionAlignment shapeFuncAlmnt;
240  ShapeFunctionList refShapeFuncs;
241  ShapeMetaDataArray refShapeMetaData;
242  GaussianShapeFunction algdShapeFunc;
243  ShapeMetaData algdShapeMetaData;
244  ResultIndexMap resIndexMap;
245  ResultList results;
246  std::size_t currSetIndex;
247  std::size_t currShapeIndex;
248  };
249  } // namespace Shape
250 } // namespace CDPL
251 
252 #endif // CDPL_SHAPE_GAUSSIANSHAPEALIGNMENT_HPP
Definition of constants in namespace CDPL::Shape::AlignmentResultSelectionMode.
Definition of the class CDPL::Shape::AlignmentResult.
Definition of the class CDPL::Shape::GaussianShapeFunctionAlignment.
Definition of the class CDPL::Shape::GaussianShapeFunction.
Definition of the class CDPL::Shape::GaussianShapeSet.
Definition of matrix data types.
Definition of the class CDPL::Util::ObjectStack.
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: AlignmentResult.hpp:45
Definition: FastGaussianShapeOverlapFunction.hpp:47
Definition: GaussianShapeAlignmentStartGenerator.hpp:49
Definition: GaussianShapeAlignment.hpp:59
PrincipalAxesAlignmentStartGenerator & getDefaultStartGenerator()
ResultList::const_iterator ConstResultIterator
Definition: GaussianShapeAlignment.hpp:75
ConstShapeIterator getReferenceShapesEnd() const
GaussianShapeFunctionAlignment::ColorFilterFunction ColorFilterFunction
Definition: GaussianShapeAlignment.hpp:79
GaussianShapeFunctionAlignment::ColorMatchFunction ColorMatchFunction
Definition: GaussianShapeAlignment.hpp:80
GaussianShapeAlignment(const GaussianShapeSet &ref_shapes)
const PrincipalAxesAlignmentStartGenerator & getDefaultStartGenerator() const
void addReferenceShape(const GaussianShape &shape, bool new_set=true)
std::shared_ptr< GaussianShapeAlignment > SharedPointer
Definition: GaussianShapeAlignment.hpp:73
void setDistanceCutoff(double cutoff)
boost::transform_iterator< GetShapeFunction, ShapeFunctionList::const_iterator > ConstShapeIterator
Definition: GaussianShapeAlignment.hpp:77
void setScoringFunction(const ScoringFunction &func)
std::function< bool(const AlignmentResult &, const AlignmentResult &)> ResultCompareFunction
Definition: GaussianShapeAlignment.hpp:82
const FastGaussianShapeOverlapFunction & getDefaultOverlapFunction() const
FastGaussianShapeOverlapFunction & getDefaultOverlapFunction()
AlignmentResult & getResult(std::size_t idx)
const GaussianShape & getReferenceShape(std::size_t idx) const
GaussianShapeAlignment & operator=(const GaussianShapeAlignment &alignment)=delete
std::size_t getNumReferenceShapes() const
const ColorFilterFunction & getColorFilterFunction() const
const ResultCompareFunction & getResultCompareFunction() const
ConstShapeIterator getReferenceShapesBegin() const
GaussianShapeAlignment(const GaussianShapeAlignment &alignment)=delete
const ColorMatchFunction & getColorMatchFunction() const
void setStartGenerator(GaussianShapeAlignmentStartGenerator &gen)
void optimizeOverlap(bool optimize)
GaussianShapeOverlapFunction & getOverlapFunction() const
GaussianShapeAlignmentStartGenerator & getStartGenerator() const
std::size_t getMaxNumOptimizationIterations() const
void setResultSelectionMode(unsigned int mode)
ConstResultIterator getResultsBegin() const
bool align(const GaussianShapeSet &shapes)
void setOverlapFunction(GaussianShapeOverlapFunction &func)
void setColorMatchFunction(const ColorMatchFunction &func)
const ScoringFunction & getScoringFunction() const
ConstResultIterator getResultsEnd() const
std::function< double(const AlignmentResult &)> ScoringFunction
Definition: GaussianShapeAlignment.hpp:81
void performAlignment(bool perf_align)
void setOptimizationStopGradient(double grad_norm)
void addReferenceShapes(const GaussianShapeSet &shapes, bool new_set=true)
ResultList::iterator ResultIterator
Definition: GaussianShapeAlignment.hpp:76
const AlignmentResult & getResult(std::size_t idx) const
void setMaxOrder(std::size_t max_order)
void setMaxNumOptimizationIterations(std::size_t max_iter)
void setResultCompareFunction(const ResultCompareFunction &func)
void setColorFilterFunction(const ColorFilterFunction &func)
GaussianShapeAlignment(const GaussianShape &ref_shape)
unsigned int getResultSelectionMode() const
bool align(const GaussianShape &shape)
Definition: GaussianShapeFunctionAlignment.hpp:52
GaussianShapeOverlapFunction::ColorMatchFunction ColorMatchFunction
Definition: GaussianShapeFunctionAlignment.hpp:69
GaussianShapeOverlapFunction::ColorFilterFunction ColorFilterFunction
Definition: GaussianShapeFunctionAlignment.hpp:68
Definition: GaussianShapeFunction.hpp:53
Definition: GaussianShapeOverlapFunction.hpp:49
Definition: GaussianShapeSet.hpp:46
A data type for the descripton of arbitrary shapes composed of spheres approximated by gaussian funct...
Definition: GaussianShape.hpp:51
std::shared_ptr< GaussianShape > SharedPointer
Definition: GaussianShape.hpp:112
Definition: PrincipalAxesAlignmentStartGenerator.hpp:49
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.