29 #ifndef CDPL_CHEM_SPATIALENTITYALIGNMENT_HPP
30 #define CDPL_CHEM_SPATIALENTITYALIGNMENT_HPP
36 #include <unordered_set>
39 #include <boost/functional/hash.hpp>
287 struct TopMappingCmpFunc
296 struct TopMappingHashFunc
301 return boost::hash_value(
m->getData());
305 struct TopMappingEqCmpFunc
310 return (m1->getData() == m2->getData());
314 typedef Util::ObjectStack<Util::STPairArray> TopMappingCache;
315 typedef std::vector<Math::Vector3D> Vector3DArray;
316 typedef std::vector<double> DoubleArray;
317 typedef std::multiset<Util::STPairArray*, TopMappingCmpFunc> TopMappingSet;
318 typedef typename TopMappingSet::iterator TopMappingSetIterator;
319 typedef std::unordered_set<const Util::STPairArray*, TopMappingHashFunc, TopMappingEqCmpFunc> TopMappingHashSet;
321 TopologicalAlignment topAlignment;
322 TopMappingSet topMappings;
323 TopMappingSetIterator nextTopMappingIter;
328 Math::KabschAlgorithm<double> kabschAlgorithm;
330 DoubleArray firstSetWeights;
332 DoubleArray secondSetWeights;
337 std::size_t minTopMappingSize;
340 TopMappingHashSet seenTopMappings;
341 TopMappingCache topMappingCache;
349 template <
typename T>
351 minTopMappingSize(3), changes(true), exhaustiveMode(true), topMappingCache(5000)
353 currTopMapping = allocTopMapping();
356 template <
typename T>
359 minTopMappingSize = min_size;
363 template <
typename T>
366 return minTopMappingSize;
369 template <
typename T>
376 template <
typename T>
383 template <
typename T>
390 template <
typename T>
397 template <
typename T>
400 topAlignConstrFunc = func;
404 template <
typename T>
408 return topAlignConstrFunc;
411 template <
typename T>
414 topAlignment.setEntityMatchFunction(func);
417 template <
typename T>
421 return topAlignment.getEntityMatchFunction();
424 template <
typename T>
427 topAlignment.setEntityPairMatchFunction(func);
430 template <
typename T>
434 return topAlignment.getEntityPairMatchFunction();
437 template <
typename T>
440 exhaustiveMode = exhaustive;
444 template <
typename T>
447 return exhaustiveMode;
450 template <
typename T>
453 return topAlignment.getNumEntities(first_set);
456 template <
typename T>
459 topAlignment.addEntity(entity, first_set);
463 template <
typename T>
466 topAlignment.clearEntities(first_set);
470 template <
typename T>
474 return topAlignment.getEntitiesBegin(first_set);
477 template <
typename T>
481 return topAlignment.getEntitiesEnd(first_set);
484 template <
typename T>
488 return topAlignment.getEntity(idx, first_set);
491 template <
typename T>
497 template <
typename T>
503 if (firstSetCoords.empty() || secondSetCoords.empty())
506 bool have_weights = !firstSetWeights.empty();
507 std::size_t min_sub_mpg_size = (minTopMappingSize < 3 ? minTopMappingSize : std::size_t(3));
509 while (nextTopMappingIter != topMappings.end()) {
510 currTopMapping = *nextTopMappingIter;
512 std::size_t num_points = currTopMapping->getSize();
514 refPoints.resize(3, num_points,
false);
515 alignedPoints.resize(3, num_points,
false);
518 almntWeights.resize(num_points, 1.0);
523 it != end; ++it, i++) {
525 std::size_t first_idx = it->first;
526 std::size_t sec_idx = it->second;
528 column(refPoints, i) = firstSetCoords[first_idx];
529 column(alignedPoints, i) = secondSetCoords[sec_idx];
532 almntWeights(i) = std::max(firstSetWeights[first_idx], secondSetWeights[sec_idx]);
535 if (exhaustiveMode && (num_points > min_sub_mpg_size)) {
538 for (std::size_t j = 0; j < num_points; j++) {
540 sub_mpg = allocTopMapping();
544 for (std::size_t k = 0; k < num_points; k++)
546 sub_mpg->
addElement(currTopMapping->getElement(k));
548 if (!seenTopMappings.insert(sub_mpg).second) {
552 topMappings.insert(sub_mpg);
557 topMappingCache.put();
561 if (!kabschAlgorithm.align(alignedPoints, refPoints)) {
562 ++nextTopMappingIter;
566 }
else if (!kabschAlgorithm.align(alignedPoints, refPoints, almntWeights)) {
567 ++nextTopMappingIter;
571 transform.assign(kabschAlgorithm.getTransform());
572 ++nextTopMappingIter;
580 template <
typename T>
586 template <
typename T>
590 return *currTopMapping;
593 template <
typename T>
596 firstSetCoords.clear();
597 secondSetCoords.clear();
599 firstSetWeights.clear();
600 secondSetWeights.clear();
603 for (ConstEntityIterator it = getEntitiesBegin(
true), end = getEntitiesEnd(
true); it != end; ++it) {
604 const EntityType& entity = *it;
606 firstSetCoords.push_back(coordsFunc(entity));
609 firstSetWeights.push_back(weightFunc(entity));
612 for (ConstEntityIterator it = getEntitiesBegin(
false), end = getEntitiesEnd(
false); it != end; ++it) {
613 const EntityType& entity = *it;
615 secondSetCoords.push_back(coordsFunc(entity));
618 secondSetWeights.push_back(weightFunc(entity));
622 topAlignment.
reset();
623 topMappingCache.putAll();
624 seenTopMappings.clear();
627 currTopMapping = allocTopMapping();
629 while (topAlignment.nextAlignment(*currTopMapping)) {
630 if (currTopMapping->getSize() < minTopMappingSize)
633 if (topAlignConstrFunc && !topAlignConstrFunc(*currTopMapping))
636 topMappings.insert(currTopMapping);
637 currTopMapping = allocTopMapping();
640 currTopMapping->clear();
642 nextTopMappingIter = topMappings.begin();
646 template <
typename T>
650 return topMappingCache.get();
Implementation of the Kabsch algorithm.
Definition of matrix data types.
Definition of class CDPL::Util::ObjectStack.
Definition of class CDPL::Chem::TopologicalEntityAlignment.
Definition of vector data types.
Computes a spatial alignment between two sets of entities (with 3D coordinates) by composing topologi...
Definition: SpatialEntityAlignment.hpp:71
const EntityType & getEntity(std::size_t idx, bool first_set) const
Returns a non-const reference to the stored entity at index idx in the specified set.
Definition: SpatialEntityAlignment.hpp:486
ConstEntityIterator getEntitiesBegin(bool first_set) const
Returns a constant iterator pointing to the beginning of the entities stored in the specified set.
Definition: SpatialEntityAlignment.hpp:472
bool exhaustiveSearchPerformed() const
Tells whether topological mappings are enumerated exhaustively before spatial alignment.
Definition: SpatialEntityAlignment.hpp:445
ConstEntityIterator getEntitiesEnd(bool first_set) const
Returns a constant iterator pointing to the end of the entities stored in the specified set.
Definition: SpatialEntityAlignment.hpp:479
void setEntity3DCoordinatesFunction(const Entity3DCoordinatesFunction &func)
Specifies a function for the retrieval of entity 3D-coordinates.
Definition: SpatialEntityAlignment.hpp:370
void addEntity(const EntityType &entity, bool first_set)
Adds an entity to the specified alignment entity set.
Definition: SpatialEntityAlignment.hpp:457
void performExhaustiveSearch(bool exhaustive)
Specifies whether topological mappings shall be enumerated exhaustively before spatial alignment,...
Definition: SpatialEntityAlignment.hpp:438
void setTopAlignmentConstraintFunction(const TopologicalAlignmentConstraintFunction &func)
Specifies a function for restricting allowed topological entity alignments.
Definition: SpatialEntityAlignment.hpp:398
void clearEntities(bool first_set)
Removes all entities in the specified alignment entity set.
Definition: SpatialEntityAlignment.hpp:464
virtual ~SpatialEntityAlignment()
Virtual destructor.
Definition: SpatialEntityAlignment.hpp:119
const EntityMatchFunction & getEntityMatchFunction() const
Returns the function that was registered for restricting allowed topological entity mappings.
Definition: SpatialEntityAlignment.hpp:419
TopologicalAlignment::ConstEntityIterator ConstEntityIterator
A constant iterator over the stored entities.
Definition: SpatialEntityAlignment.hpp:84
TopologicalAlignment::EntityMatchFunction EntityMatchFunction
Generic wrapper class used to store a user-defined topological entity match constraint function.
Definition: SpatialEntityAlignment.hpp:104
void reset()
Discards the current alignment-search state so that the next call to nextAlignment() restarts the top...
Definition: SpatialEntityAlignment.hpp:492
std::size_t getMinTopologicalMappingSize()
Returns the minimum number of topologically mapped entities that is required to enable a subsequent s...
Definition: SpatialEntityAlignment.hpp:364
bool nextAlignment()
Searches for the next alignment solution.
Definition: SpatialEntityAlignment.hpp:498
void setMinTopologicalMappingSize(std::size_t min_size)
Specifies the minimum number of topologically mapped entities that is required to enable a subsequent...
Definition: SpatialEntityAlignment.hpp:357
std::function< double(const EntityType &)> EntityWeightFunction
Generic wrapper class used to store a user-defined entity alignment weight function.
Definition: SpatialEntityAlignment.hpp:99
const Entity3DCoordinatesFunction & getEntity3DCoordinatesFunction() const
Returns the function that was registered for the retrieval of entity 3D-coordinates.
Definition: SpatialEntityAlignment.hpp:378
void setEntityPairMatchFunction(const EntityPairMatchFunction &func)
Specifies a function for checking the compatibility of entity-pairs in the search for alignment solut...
Definition: SpatialEntityAlignment.hpp:425
const Math::Matrix4D & getTransform() const
Returns the alignment transformation matrix that was calculated in the last successful call to nextAl...
Definition: SpatialEntityAlignment.hpp:581
std::function< bool(const Util::STPairArray &)> TopologicalAlignmentConstraintFunction
Generic wrapper class used to store a user-defined predicate to restrict allowed topological entity a...
Definition: SpatialEntityAlignment.hpp:89
const TopologicalAlignmentConstraintFunction & getTopAlignmentConstraintFunction() const
Returns the function that was registered for restricting allowed topological entity alignments.
Definition: SpatialEntityAlignment.hpp:406
const EntityPairMatchFunction & getEntityPairMatchFunction() const
Returns the function that was registered for checking the compatibility of entity-pairs.
Definition: SpatialEntityAlignment.hpp:432
TopologicalAlignment::EntityPairMatchFunction EntityPairMatchFunction
Generic wrapper class used to store a user-defined entity-pair match constraint function.
Definition: SpatialEntityAlignment.hpp:109
void setEntityMatchFunction(const EntityMatchFunction &func)
Specifies a function for restricting allowed topological entity mappings in the search for alignment ...
Definition: SpatialEntityAlignment.hpp:412
std::size_t getNumEntities(bool first_set) const
Returns the number of entities in the specified alignment entity set.
Definition: SpatialEntityAlignment.hpp:451
void setEntityWeightFunction(const EntityWeightFunction &func)
Specifies a function for the retrieval of entity weights for spatial alignment.
Definition: SpatialEntityAlignment.hpp:384
std::function< const Math::Vector3D &(const EntityType &)> Entity3DCoordinatesFunction
Generic wrapper class used to store a user-defined entity 3D-coordinates function.
Definition: SpatialEntityAlignment.hpp:94
T EntityType
The actual entity type.
Definition: SpatialEntityAlignment.hpp:79
const EntityWeightFunction & getEntityWeightFunction() const
Returns the function that was registered for the retrieval of entity weights for spatial alignment.
Definition: SpatialEntityAlignment.hpp:392
SpatialEntityAlignment()
Constructs the SpatialEntityAlignment instance.
Definition: SpatialEntityAlignment.hpp:350
const Util::STPairArray & getTopologicalMapping() const
Returns the topological entity mapping resulting from the last successful call to nextAlignment().
Definition: SpatialEntityAlignment.hpp:588
Computes a topological alignment between two sets of entities by reducing the alignment problem to a ...
Definition: TopologicalEntityAlignment.hpp:64
boost::indirect_iterator< typename EntitySet::const_iterator, const EntityType > ConstEntityIterator
A constant iterator over the stored entities.
Definition: TopologicalEntityAlignment.hpp:80
Dynamic array class providing amortized constant time access to arbitrary elements.
Definition: Array.hpp:92
void clear()
Erases all elements.
Definition: Array.hpp:740
StorageType::const_iterator ConstElementIterator
A constant random access iterator used to iterate over the elements of the array.
Definition: Array.hpp:125
std::size_t getSize() const
Returns the number of elements stored in the array.
Definition: Array.hpp:704
void addElement(const ValueType &value=ValueType())
Inserts a new element at the end of the array.
Definition: Array.hpp:765
constexpr unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
constexpr unsigned int m
Specifies that the stereocenter has m configuration.
Definition: CIPDescriptor.hpp:116
Matrix< double > DMatrix
Unbounded dense matrix holding floating point values of type double..
Definition: Matrix.hpp:3145
VectorArray< Vector3D > Vector3DArray
Array storing vectors of type Math::Vector3D.
Definition: VectorArray.hpp:85
MatrixColumn< M > column(MatrixExpression< M > &e, typename MatrixColumn< M >::SizeType j)
Returns a mutable column proxy for column j of the matrix expression e.
Definition: MatrixProxy.hpp:1259
CMatrix< double, 4, 4 > Matrix4D
Bounded 4x4 matrix holding floating point values of type double.
Definition: Matrix.hpp:3185
Vector< double > DVector
Unbounded dense vector holding floating point values of type double.
Definition: Vector.hpp:2987
CVector< double, 3 > Vector3D
Bounded 3 element vector holding floating point values of type double.
Definition: Vector.hpp:2937
void transform(VectorArray< CVector< T, Dim > > &va, const CMatrix< T1, Dim, Dim > &xform)
Transforms each -dimensional vector in the array with the -dimensional square matrix xform.
Definition: VectorArrayFunctions.hpp:54
Array< STPair > STPairArray
Array storing pairs of unsigned integers of type std::size_t.
Definition: Array.hpp:590
The namespace of the Chemical Data Processing Library.