IMP logo
IMP Reference Guide  2.20.1
The Integrative Modeling Platform
vector_metrics.h
Go to the documentation of this file.
1 /**
2  * \file IMP/algebra/vector_metrics.h \brief Functions to generate vectors.
3  *
4  * Copyright 2007-2023 IMP Inventors. All rights reserved.
5  *
6  */
7 
8 #ifndef IMPALGEBRA_VECTOR_METRICS_H
9 #define IMPALGEBRA_VECTOR_METRICS_H
10 
11 #include "VectorD.h"
12 #include "BoundingBoxD.h"
13 #include <IMP/types.h>
14 #include <IMP/Object.h>
15 #include <iostream>
16 #include <IMP/Pointer.h>
17 #include <IMP/WeakPointer.h>
18 #include <algorithm>
19 #include <numeric>
20 #include <cereal/access.hpp>
21 #include <cereal/types/polymorphic.hpp>
22 #include <cereal/types/base_class.hpp>
23 
24 IMPALGEBRA_BEGIN_NAMESPACE
25 
26 //! The base class for a metric on VectorKDs.
27 class IMPALGEBRAEXPORT VectorKDMetric : public Object {
28  public:
29  VectorKDMetric(std::string name);
30  virtual double get_distance(const VectorKD &a, const VectorKD &b) const = 0;
31  virtual VectorKD get_centroid(const VectorKDs &vs) const = 0;
32 };
33 
35 
36 //! The l2 norm on the distance vector.
37 class IMPALGEBRAEXPORT EuclideanVectorKDMetric : public VectorKDMetric {
38  friend class cereal::access;
39  template<class Archive> void serialize(Archive &ar) {
40  ar(cereal::base_class<Object>(this));
41  }
43  public:
44  EuclideanVectorKDMetric(std::string name = "EuclideanVectorKDMetric%1%");
45  double get_distance(const VectorKD &a, const VectorKD &b) const override {
46  return get_l2_norm(a - b);
47  }
48  VectorKD get_centroid(const VectorKDs &vs) const override {
49  IMP_USAGE_CHECK(!vs.empty(), "Needs things to have a centroid");
50  VectorKD sum = std::accumulate(vs.begin(), vs.end(),
51  get_zero_vector_kd(vs[0].get_dimension()));
52  return sum / vs.size();
53  }
55 };
56 
57 /** The l-infinity norm on the difference between the two vectors. And the
58  centroid is the center of the bounding box of the vectors.
59  */
60 class IMPALGEBRAEXPORT MaxVectorKDMetric : public VectorKDMetric {
61  friend class cereal::access;
62  template<class Archive> void serialize(Archive &ar) {
63  ar(cereal::base_class<Object>(this));
64  }
66  public:
67  MaxVectorKDMetric(std::string name = "MaxVectorKDMetric%1%");
68  double get_distance(const VectorKD &a, const VectorKD &b) const override {
69  return get_linf_norm(a - b);
70  }
71  VectorKD get_centroid(const VectorKDs &vs) const override {
72  IMP_USAGE_CHECK(!vs.empty(), "Needs things to have a centroid");
73  BoundingBoxKD bb = std::accumulate(vs.begin(), vs.end(),
74  BoundingBoxKD(vs[0].get_dimension()));
75  return .5 * (bb.get_corner(0) + bb.get_corner(1));
76  }
78 };
79 
80 IMPALGEBRA_END_NAMESPACE
81 
82 #endif /* IMPALGEBRA_VECTOR_METRICS_H */
BoundingBoxD<-1 > BoundingBoxKD
Typedef for Python.
Definition: BoundingBoxD.h:183
Basic types used by IMP.
VectorD< D > get_zero_vector_kd(int Di)
Return a dynamically sized vector of zeros.
Definition: VectorD.h:280
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
The l2 norm on the distance vector.
Common base class for heavy weight IMP objects.
Definition: Object.h:111
#define IMP_OBJECT_SERIALIZE_DECL(Name)
Declare methods needed for serialization of Object pointers.
Definition: object_macros.h:95
A bounding box in D dimensions.
Simple D vector class.
A weak pointer to an Object or RefCountedObject.
An axis-aligned bounding box.
Definition: BoundingBoxD.h:28
Vector3D get_centroid(const Vector3Ds &ps)
Return the centroid of a set of vectors.
Definition: Vector3D.h:68
#define IMP_OBJECTS(Name, PluralName)
Define the types for storing lists of object pointers.
Definition: object_macros.h:44
A nullptr-initialized pointer to an IMP Object.
A shared base class to help in debugging and things.
The base class for a metric on VectorKDs.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168
VectorD<-1 > VectorKD
Definition: VectorD.h:441
double get_distance(const Line3D &s, const Vector3D &p)
Get closest distance between a line and a point.