Chemical Data Processing Library C++ API - Version 1.1.1
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 
91 
93 
95 
97 
99 
101 
103 
105 
107 
109 
111 
113 
115 
117 
119 
121 
123 
124  void setResultSelectionMode(unsigned int mode);
125 
126  unsigned int getResultSelectionMode() const;
127 
128  void calcSelfOverlaps(bool calc);
129 
130  bool calcSelfOverlaps() const;
131 
132  void calcColorSelfOverlaps(bool calc);
133 
134  bool calcColorSelfOverlaps() const;
135 
136  void calcColorOverlaps(bool calc);
137 
138  bool calcColorOverlaps() const;
139 
140  void performAlignment(bool perf_align);
141 
142  bool performAlignment() const;
143 
144  void optimizeOverlap(bool optimize);
145 
146  bool optimizeOverlap() const;
147 
148  void greedyOptimization(bool greedy);
149 
150  bool greedyOptimization() const;
151 
152  void setMaxNumOptimizationIterations(std::size_t max_iter);
153 
154  std::size_t getMaxNumOptimizationIterations() const;
155 
156  void setOptimizationStopGradient(double grad_norm);
157 
159 
160  void setMaxOrder(std::size_t max_order);
161 
162  std::size_t getMaxOrder() const;
163 
164  void setDistanceCutoff(double cutoff);
165 
166  double getDistanceCutoff() const;
167 
169 
170  void addReferenceShape(const GaussianShape& shape, bool new_set = true);
171 
172  void addReferenceShapes(const GaussianShapeSet& shapes, bool new_set = true);
173 
174  std::size_t getNumReferenceShapes() const;
175 
176  const GaussianShape& getReferenceShape(std::size_t idx) const;
177 
179 
181 
182  bool align(const GaussianShape& shape);
183 
184  bool align(const GaussianShapeSet& shapes);
185 
186  std::size_t getNumResults() const;
187 
188  const AlignmentResult& getResult(std::size_t idx) const;
189 
190  AlignmentResult& getResult(std::size_t idx);
191 
193 
195 
197 
199 
200  private:
201  typedef GaussianShape::SharedPointer GaussianShapePtr;
202 
203  struct ShapeMetaData
204  {
205 
206  std::size_t setIndex;
207  std::size_t index;
208  unsigned int symClass;
210  double selfOverlap;
211  double colSelfOverlap;
212  GaussianShapePtr shape;
213  };
214 
215  typedef std::pair<std::size_t, std::size_t> ResultID;
216 
218 
219  GaussianShapeAlignment& operator=(const GaussianShapeAlignment& alignment);
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
CDPL::Shape::GaussianShapeAlignment::setMaxOrder
void setMaxOrder(std::size_t max_order)
CDPL::Shape::GaussianShapeAlignment::ConstResultIterator
ResultList::const_iterator ConstResultIterator
Definition: GaussianShapeAlignment.hpp:75
CDPL::Shape::GaussianShapeAlignment::getScoringFunction
const ScoringFunction & getScoringFunction() const
CDPL::Shape::GaussianShapeAlignmentStartGenerator
Definition: GaussianShapeAlignmentStartGenerator.hpp:49
ObjectStack.hpp
Definition of the class CDPL::Util::ObjectStack.
CDPL::Shape::GaussianShapeAlignment::ColorFilterFunction
GaussianShapeFunctionAlignment::ColorFilterFunction ColorFilterFunction
Definition: GaussianShapeAlignment.hpp:79
CDPL::Shape::GaussianShapeSet
Definition: GaussianShapeSet.hpp:46
CMatrix< double, 4, 4 >
CDPL::Shape::GaussianShapeAlignment::getDefaultStartGenerator
const PrincipalAxesAlignmentStartGenerator & getDefaultStartGenerator() const
CDPL::Shape::GaussianShapeAlignment::getResultsEnd
ConstResultIterator getResultsEnd() const
CDPL::Shape::GaussianShapeAlignment::setResultCompareFunction
void setResultCompareFunction(const ResultCompareFunction &func)
CDPL::Shape::GaussianShapeAlignment::getResult
AlignmentResult & getResult(std::size_t idx)
CDPL::Shape::GaussianShapeAlignment::addReferenceShape
void addReferenceShape(const GaussianShape &shape, bool new_set=true)
CDPL::Shape::GaussianShapeFunctionAlignment
Definition: GaussianShapeFunctionAlignment.hpp:52
CDPL::Shape::GaussianShapeAlignment::clearReferenceShapes
void clearReferenceShapes()
CDPL::Shape::GaussianShapeAlignment::getReferenceShape
const GaussianShape & getReferenceShape(std::size_t idx) const
CDPL::Shape::PrincipalAxesAlignmentStartGenerator
Definition: PrincipalAxesAlignmentStartGenerator.hpp:49
CDPL::Shape::GaussianShapeAlignment::getNumReferenceShapes
std::size_t getNumReferenceShapes() const
AlignmentResult.hpp
Definition of the class CDPL::Shape::AlignmentResult.
CDPL::Shape::GaussianShapeAlignment::calcSelfOverlaps
void calcSelfOverlaps(bool calc)
CDPL::Shape::transform
CDPL_SHAPE_API void transform(GaussianShape &shape, const Math::Matrix4D &xform)
GaussianShapeFunctionAlignment.hpp
Definition of the class CDPL::Shape::GaussianShapeFunctionAlignment.
CDPL::Shape::GaussianShapeAlignment::getResultCompareFunction
const ResultCompareFunction & getResultCompareFunction() const
CDPL::Shape::GaussianShapeAlignment::~GaussianShapeAlignment
~GaussianShapeAlignment()
CDPL::Shape::AlignmentResultSelectionMode::BEST_PER_REFERENCE_SET
const unsigned int BEST_PER_REFERENCE_SET
Definition: AlignmentResultSelectionMode.hpp:45
CDPL::Shape::GaussianShapeAlignment::setColorFilterFunction
void setColorFilterFunction(const ColorFilterFunction &func)
CDPL::Shape::GaussianShapeAlignment::getColorFilterFunction
const ColorFilterFunction & getColorFilterFunction() const
CDPL::Shape::GaussianShapeAlignment::optimizeOverlap
bool optimizeOverlap() const
CDPL::Shape::GaussianShapeAlignment::calcColorSelfOverlaps
void calcColorSelfOverlaps(bool calc)
CDPL::Shape::GaussianShapeAlignment::performAlignment
void performAlignment(bool perf_align)
CDPL::Shape::GaussianShapeAlignment::setStartGenerator
void setStartGenerator(GaussianShapeAlignmentStartGenerator &gen)
CDPL::Shape::GaussianShapeAlignment::ColorMatchFunction
GaussianShapeFunctionAlignment::ColorMatchFunction ColorMatchFunction
Definition: GaussianShapeAlignment.hpp:80
CDPL::Shape::GaussianShapeAlignment::calcColorOverlaps
void calcColorOverlaps(bool calc)
CDPL::Shape::GaussianShapeAlignment::getDistanceCutoff
double getDistanceCutoff() const
CDPL::Shape::GaussianShapeAlignment::getColorMatchFunction
const ColorMatchFunction & getColorMatchFunction() const
CDPL::Shape::GaussianShapeAlignment::setOverlapFunction
void setOverlapFunction(GaussianShapeOverlapFunction &func)
CDPL::Shape::GaussianShapeAlignment::getMaxOrder
std::size_t getMaxOrder() const
GaussianShapeSet.hpp
Definition of the class CDPL::Shape::GaussianShapeSet.
AlignmentResultSelectionMode.hpp
Definition of constants in namespace CDPL::Shape::AlignmentResultSelectionMode.
bool
CDPL::Shape::GaussianShapeAlignment::getReferenceShapesBegin
ConstShapeIterator getReferenceShapesBegin() const
CDPL::Shape::GaussianShapeFunctionAlignment::ColorFilterFunction
GaussianShapeOverlapFunction::ColorFilterFunction ColorFilterFunction
Definition: GaussianShapeFunctionAlignment.hpp:68
CDPL::Shape::GaussianShapeAlignment::GaussianShapeAlignment
GaussianShapeAlignment()
CDPL::Shape::GaussianShape::SharedPointer
std::shared_ptr< GaussianShape > SharedPointer
Definition: GaussianShape.hpp:112
CDPL::Shape::GaussianShapeAlignment::calcColorSelfOverlaps
bool calcColorSelfOverlaps() const
CDPL::Shape::GaussianShapeAlignment::setMaxNumOptimizationIterations
void setMaxNumOptimizationIterations(std::size_t max_iter)
CDPL::Shape::GaussianShapeAlignment::greedyOptimization
void greedyOptimization(bool greedy)
CDPL::Shape::GaussianShapeAlignment::GaussianShapeAlignment
GaussianShapeAlignment(const GaussianShapeSet &ref_shapes)
double
CDPL::Shape::GaussianShapeAlignment
Definition: GaussianShapeAlignment.hpp:59
CDPL::Shape::GaussianShapeAlignment::getOptimizationStopGradient
double getOptimizationStopGradient() const
CDPL::Shape::GaussianShapeAlignment::ResultCompareFunction
std::function< bool(const AlignmentResult &, const AlignmentResult &)> ResultCompareFunction
Definition: GaussianShapeAlignment.hpp:82
GaussianShapeFunction.hpp
Definition of the class CDPL::Shape::GaussianShapeFunction.
CDPL::Shape::GaussianShapeAlignment::getStartGenerator
GaussianShapeAlignmentStartGenerator & getStartGenerator() const
CDPL::Shape::GaussianShape
A data type for the descripton of arbitrary shapes composed of spheres approximated by gaussian funct...
Definition: GaussianShape.hpp:51
CDPL::Shape::GaussianShapeAlignment::setOptimizationStopGradient
void setOptimizationStopGradient(double grad_norm)
CDPL::Shape::GaussianShapeAlignment::SharedPointer
std::shared_ptr< GaussianShapeAlignment > SharedPointer
Definition: GaussianShapeAlignment.hpp:73
CDPL::Shape::GaussianShapeAlignment::ResultIterator
ResultList::iterator ResultIterator
Definition: GaussianShapeAlignment.hpp:76
CDPL::Shape::GaussianShapeAlignment::align
bool align(const GaussianShape &shape)
CDPL::Shape::GaussianShapeAlignment::getDefaultStartGenerator
PrincipalAxesAlignmentStartGenerator & getDefaultStartGenerator()
CDPL::Shape::GaussianShapeAlignment::getResult
const AlignmentResult & getResult(std::size_t idx) const
CDPL::Shape::GaussianShapeAlignment::setResultSelectionMode
void setResultSelectionMode(unsigned int mode)
CDPL::Shape::GaussianShapeAlignment::align
bool align(const GaussianShapeSet &shapes)
CDPL::Shape::GaussianShapeAlignment::setScoringFunction
void setScoringFunction(const ScoringFunction &func)
CDPL::Shape::GaussianShapeOverlapFunction
Definition: GaussianShapeOverlapFunction.hpp:49
CDPL::Shape::GaussianShapeFunction
Definition: GaussianShapeFunction.hpp:53
CDPL::Shape::GaussianShapeAlignment::getResultsEnd
ResultIterator getResultsEnd()
CDPL_SHAPE_API
#define CDPL_SHAPE_API
Tells the compiler/linker which classes, functions and variables are part of the library API.
CDPL::Shape::GaussianShapeAlignment::setColorMatchFunction
void setColorMatchFunction(const ColorMatchFunction &func)
CDPL::Shape::GaussianShapeAlignment::optimizeOverlap
void optimizeOverlap(bool optimize)
CDPL::Shape::GaussianShapeAlignment::getDefaultOverlapFunction
FastGaussianShapeOverlapFunction & getDefaultOverlapFunction()
CDPL::Shape::GaussianShapeAlignment::ConstShapeIterator
boost::transform_iterator< GetShapeFunction, ShapeFunctionList::const_iterator > ConstShapeIterator
Definition: GaussianShapeAlignment.hpp:77
CDPL::Shape::GaussianShapeAlignment::ScoringFunction
std::function< double(const AlignmentResult &)> ScoringFunction
Definition: GaussianShapeAlignment.hpp:81
CDPL::Shape::FastGaussianShapeOverlapFunction
Definition: FastGaussianShapeOverlapFunction.hpp:47
CDPL
The namespace of the Chemical Data Processing Library.
CDPL::Shape::GaussianShapeAlignment::performAlignment
bool performAlignment() const
APIPrefix.hpp
Definition of the preprocessor macro CDPL_SHAPE_API.
CDPL::Shape::GaussianShapeAlignment::getMaxNumOptimizationIterations
std::size_t getMaxNumOptimizationIterations() const
CDPL::Shape::GaussianShapeAlignment::addReferenceShapes
void addReferenceShapes(const GaussianShapeSet &shapes, bool new_set=true)
CDPL::Shape::GaussianShapeAlignment::getOverlapFunction
GaussianShapeOverlapFunction & getOverlapFunction() const
CDPL::Shape::AlignmentResult
Definition: AlignmentResult.hpp:45
CDPL::Shape::GaussianShapeAlignment::getResultSelectionMode
unsigned int getResultSelectionMode() const
CDPL::Shape::GaussianShapeAlignment::greedyOptimization
bool greedyOptimization() const
CDPL::Shape::GaussianShapeAlignment::calcSelfOverlaps
bool calcSelfOverlaps() const
CDPL::Shape::GaussianShapeFunctionAlignment::ColorMatchFunction
GaussianShapeOverlapFunction::ColorMatchFunction ColorMatchFunction
Definition: GaussianShapeFunctionAlignment.hpp:69
CDPL::Shape::GaussianShapeAlignment::getResultsBegin
ConstResultIterator getResultsBegin() const
Matrix.hpp
Definition of matrix data types.
CDPL::Util::ObjectStack< GaussianShapeFunction >
CDPL::Shape::GaussianShapeAlignment::setDistanceCutoff
void setDistanceCutoff(double cutoff)
CDPL::Shape::GaussianShapeAlignment::calcColorOverlaps
bool calcColorOverlaps() const
CDPL::Shape::GaussianShapeAlignment::getResultsBegin
ResultIterator getResultsBegin()
CDPL::Shape::GaussianShapeAlignment::getReferenceShapesEnd
ConstShapeIterator getReferenceShapesEnd() const
CDPL::Shape::GaussianShapeAlignment::GaussianShapeAlignment
GaussianShapeAlignment(const GaussianShape &ref_shape)
CDPL::Shape::GaussianShapeAlignment::getDefaultOverlapFunction
const FastGaussianShapeOverlapFunction & getDefaultOverlapFunction() const
CDPL::Shape::GaussianShapeAlignment::getNumResults
std::size_t getNumResults() const