29 #ifndef CDPL_CHEM_SPATIALENTITYALIGNMENT_HPP
30 #define CDPL_CHEM_SPATIALENTITYALIGNMENT_HPP
36 #include <unordered_set>
39 #include <boost/functional/hash.hpp>
285 struct TopMappingCmpFunc
294 struct TopMappingHashFunc
299 return boost::hash_value(
m->getData());
303 struct TopMappingEqCmpFunc
308 return (m1->getData() == m2->getData());
312 typedef Util::ObjectStack<Util::STPairArray> TopMappingCache;
313 typedef std::vector<Math::Vector3D> Vector3DArray;
314 typedef std::vector<double> DoubleArray;
315 typedef std::multiset<Util::STPairArray*, TopMappingCmpFunc> TopMappingSet;
316 typedef typename TopMappingSet::iterator TopMappingSetIterator;
317 typedef std::unordered_set<const Util::STPairArray*, TopMappingHashFunc, TopMappingEqCmpFunc> TopMappingHashSet;
319 TopologicalAlignment topAlignment;
320 TopMappingSet topMappings;
321 TopMappingSetIterator nextTopMappingIter;
326 Math::KabschAlgorithm<double> kabschAlgorithm;
328 DoubleArray firstSetWeights;
330 DoubleArray secondSetWeights;
335 std::size_t minTopMappingSize;
338 TopMappingHashSet seenTopMappings;
339 TopMappingCache topMappingCache;
347 template <
typename T>
349 minTopMappingSize(3), changes(true), exhaustiveMode(true), topMappingCache(5000)
351 currTopMapping = allocTopMapping();
354 template <
typename T>
357 minTopMappingSize = min_size;
361 template <
typename T>
364 return minTopMappingSize;
367 template <
typename T>
374 template <
typename T>
381 template <
typename T>
388 template <
typename T>
395 template <
typename T>
398 topAlignConstrFunc = func;
402 template <
typename T>
406 return topAlignConstrFunc;
409 template <
typename T>
412 topAlignment.setEntityMatchFunction(func);
415 template <
typename T>
419 return topAlignment.getEntityMatchFunction();
422 template <
typename T>
425 topAlignment.setEntityPairMatchFunction(func);
428 template <
typename T>
432 return topAlignment.getEntityPairMatchFunction();
435 template <
typename T>
438 exhaustiveMode = exhaustive;
442 template <
typename T>
445 return exhaustiveMode;
448 template <
typename T>
451 return topAlignment.getNumEntities(first_set);
454 template <
typename T>
457 topAlignment.addEntity(entity, first_set);
461 template <
typename T>
464 topAlignment.clearEntities(first_set);
468 template <
typename T>
472 return topAlignment.getEntitiesBegin(first_set);
475 template <
typename T>
479 return topAlignment.getEntitiesEnd(first_set);
482 template <
typename T>
486 return topAlignment.getEntity(idx, first_set);
489 template <
typename T>
495 template <
typename T>
501 if (firstSetCoords.empty() || secondSetCoords.empty())
504 bool have_weights = !firstSetWeights.empty();
505 std::size_t min_sub_mpg_size = (minTopMappingSize < 3 ? minTopMappingSize : std::size_t(3));
507 while (nextTopMappingIter != topMappings.end()) {
508 currTopMapping = *nextTopMappingIter;
510 std::size_t num_points = currTopMapping->getSize();
512 refPoints.resize(3, num_points,
false);
513 alignedPoints.resize(3, num_points,
false);
516 almntWeights.resize(num_points, 1.0);
521 it != end; ++it, i++) {
523 std::size_t first_idx = it->first;
524 std::size_t sec_idx = it->second;
526 column(refPoints, i) = firstSetCoords[first_idx];
527 column(alignedPoints, i) = secondSetCoords[sec_idx];
530 almntWeights(i) = std::max(firstSetWeights[first_idx], secondSetWeights[sec_idx]);
533 if (exhaustiveMode && (num_points > min_sub_mpg_size)) {
536 for (std::size_t j = 0; j < num_points; j++) {
538 sub_mpg = allocTopMapping();
542 for (std::size_t k = 0; k < num_points; k++)
544 sub_mpg->
addElement(currTopMapping->getElement(k));
546 if (!seenTopMappings.insert(sub_mpg).second) {
550 topMappings.insert(sub_mpg);
555 topMappingCache.put();
559 if (!kabschAlgorithm.align(alignedPoints, refPoints)) {
560 ++nextTopMappingIter;
564 }
else if (!kabschAlgorithm.align(alignedPoints, refPoints, almntWeights)) {
565 ++nextTopMappingIter;
569 transform.assign(kabschAlgorithm.getTransform());
570 ++nextTopMappingIter;
578 template <
typename T>
584 template <
typename T>
588 return *currTopMapping;
591 template <
typename T>
594 firstSetCoords.clear();
595 secondSetCoords.clear();
597 firstSetWeights.clear();
598 secondSetWeights.clear();
601 for (ConstEntityIterator it = getEntitiesBegin(
true), end = getEntitiesEnd(
true); it != end; ++it) {
602 const EntityType& entity = *it;
604 firstSetCoords.push_back(coordsFunc(entity));
607 firstSetWeights.push_back(weightFunc(entity));
610 for (ConstEntityIterator it = getEntitiesBegin(
false), end = getEntitiesEnd(
false); it != end; ++it) {
611 const EntityType& entity = *it;
613 secondSetCoords.push_back(coordsFunc(entity));
616 secondSetWeights.push_back(weightFunc(entity));
620 topAlignment.
reset();
621 topMappingCache.putAll();
622 seenTopMappings.clear();
625 currTopMapping = allocTopMapping();
627 while (topAlignment.nextAlignment(*currTopMapping)) {
628 if (currTopMapping->getSize() < minTopMappingSize)
631 if (topAlignConstrFunc && !topAlignConstrFunc(*currTopMapping))
634 topMappings.insert(currTopMapping);
635 currTopMapping = allocTopMapping();
638 currTopMapping->clear();
640 nextTopMappingIter = topMappings.begin();
644 template <
typename T>
648 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 3D entities by combining topological entity matching...
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:484
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:470
bool exhaustiveSearchPerformed() const
Tells whether or not additional topological entity mappings (derived from the found max....
Definition: SpatialEntityAlignment.hpp:443
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:477
void setEntity3DCoordinatesFunction(const Entity3DCoordinatesFunction &func)
Specifies a function for the retrieval of entity 3D coordinates.
Definition: SpatialEntityAlignment.hpp:368
void addEntity(const EntityType &entity, bool first_set)
Adds an entity to the specified entity set.
Definition: SpatialEntityAlignment.hpp:455
void performExhaustiveSearch(bool exhaustive)
Specifies whether additional topological entity mappings (derived from the found max....
Definition: SpatialEntityAlignment.hpp:436
void setTopAlignmentConstraintFunction(const TopologicalAlignmentConstraintFunction &func)
Specifies a function for restricting allowed topological entity alignments.
Definition: SpatialEntityAlignment.hpp:396
void clearEntities(bool first_set)
Removes all entities in the specified entity set.
Definition: SpatialEntityAlignment.hpp:462
virtual ~SpatialEntityAlignment()
Virtual destructor.
Definition: SpatialEntityAlignment.hpp:119
const EntityMatchFunction & getEntityMatchFunction() const
Returns the function that was registered for restricting allowed entity mappings.
Definition: SpatialEntityAlignment.hpp:417
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:490
std::size_t getMinTopologicalMappingSize()
Returns the minimum number of topologically mapped entities that is required to enable a subsequent s...
Definition: SpatialEntityAlignment.hpp:362
bool nextAlignment()
Searches for the next alignment solution.
Definition: SpatialEntityAlignment.hpp:496
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:355
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:376
void setEntityPairMatchFunction(const EntityPairMatchFunction &func)
Specifies a function for checking the compatibility of entity pairs in the search for alignment solut...
Definition: SpatialEntityAlignment.hpp:423
const Math::Matrix4D & getTransform() const
Returns the alignment transformation matrix that was calculated in the last successful call to nextAl...
Definition: SpatialEntityAlignment.hpp:579
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:404
const EntityPairMatchFunction & getEntityPairMatchFunction() const
Returns the function that was registered for checking the compatibility of entity pairs.
Definition: SpatialEntityAlignment.hpp:430
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 entity mappings in the search for alignment solutions.
Definition: SpatialEntityAlignment.hpp:410
std::size_t getNumEntities(bool first_set) const
Returns the number of entities in the specified alignment entity set.
Definition: SpatialEntityAlignment.hpp:449
void setEntityWeightFunction(const EntityWeightFunction &func)
Specifies a function for the retrieval of entity weights for spatial alignment.
Definition: SpatialEntityAlignment.hpp:382
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:390
SpatialEntityAlignment()
Constructs the SpatialEntityAlignment instance.
Definition: SpatialEntityAlignment.hpp:348
const Util::STPairArray & getTopologicalMapping() const
Returns the topological entity mapping resulting from the last successful call to nextAlignment().
Definition: SpatialEntityAlignment.hpp:586
Computes a topological alignment between two sets of arbitrary entities by performing a maximum commo...
Definition: TopologicalEntityAlignment.hpp:65
boost::indirect_iterator< typename EntitySet::const_iterator, const EntityType > ConstEntityIterator
A constant iterator over the stored entities.
Definition: TopologicalEntityAlignment.hpp:81
Dynamic array class providing amortized constant time access to arbitrary elements.
Definition: Array.hpp:92
void clear()
Erases all elements.
Definition: Array.hpp:743
StorageType::const_iterator ConstElementIterator
A constant random access iterator used to iterate over the elements of the array.
Definition: Array.hpp:128
std::size_t getSize() const
Returns the number of elements stored in the array.
Definition: Array.hpp:707
void addElement(const ValueType &value=ValueType())
Inserts a new element at the end of the array.
Definition: Array.hpp:768
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:3469
VectorArray< Vector3D > Vector3DArray
Array storing vectors of type Math::Vector3D.
Definition: VectorArray.hpp:87
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:1400
CMatrix< double, 4, 4 > Matrix4D
Bounded 4×4 matrix holding floating-point values of type double.
Definition: Matrix.hpp:3509
Vector< double > DVector
Unbounded dense vector holding floating-point values of type double.
Definition: Vector.hpp:3268
CVector< double, 3 > Vector3D
Bounded 3 element vector holding floating-point values of type double.
Definition: Vector.hpp:3218
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:593
The namespace of the Chemical Data Processing Library.