20 #ifndef GAIA_FROZENCOSINEANGLEDISTANCE_H 21 #define GAIA_FROZENCOSINEANGLEDISTANCE_H 23 #include "frozendistance.h" 30 static const Real defaultUndefinedDistance = 0.0;
34 _useDefaultValue(
true), _defaultValue(defaultUndefinedDistance) {}
43 :
FrozenDistance(dataset), _useDefaultValue(
true), _defaultValue(defaultUndefinedDistance) {
44 init(QString::fromStdString(descriptorName));
48 :
FrozenDistance(dataset), _useDefaultValue(
true), _defaultValue(defaultUndefinedDistance) {
49 init(QString::fromUtf8(descriptorName));
56 validParams <<
"defaultValue";
58 if (params.contains(
"defaultValue")) {
59 _useDefaultValue =
true;
60 _defaultValue = params.value(
"defaultValue").toDouble();
63 _useDefaultValue =
false;
66 if (params.contains(
"descriptorName")) {
67 init(params.value(
"descriptorName"));
75 void init(
const QString& descriptorName) {
76 validParams <<
"descriptorName";
79 init(pos.first, pos.second - pos.first);
82 void init(
int offset,
int size) {
87 _norm.resize(_dataset.rows());
88 for (
int i=0; i<_dataset.rows(); i++) _norm[i] = _dataset.row(i).segment(_offset, _size).norm();
91 _acosLUT.resize(LUT_SIZE+1);
92 for (
int i=0; i<LUT_SIZE+1; i++) {
93 _acosLUT[i] = std::acos(
double(i)/LUT_SIZE) / M_PI;
97 void prepare(
const FrozenPoint& query) {
98 _qnorm = query.segment(_offset, _size).norm();
102 Real acos(Real x)
const {
103 if (x < 0)
return 1.0 - acos(-x);
104 x = qMin(x, (Real)1.0);
105 return _acosLUT.at((
int)(x*LUT_SIZE+0.5));
108 Real operator()(
int i,
const FrozenPoint& query)
const {
112 static const Real epsilon = 1e-6;
113 if (n1*n2 < epsilon) {
114 if (_useDefaultValue) {
115 return _defaultValue;
118 msg <<
"Could not compute cosine distance between '" << _dataset.
pointName(i)
119 <<
"' and query point because one of the two points is null, result is undefined";
123 Real result = _dataset.row(i).segment(_offset, _size).dot(query.segment(_offset, _size) / (n1*n2));
130 bool _useDefaultValue;
138 static const int LUT_SIZE = 4096;
139 QVector<Real> _acosLUT;
145 #endif // GAIA_FROZENCOSINEANGLEDISTANCE_H Definition: frozendistance.h:33
int dimension() const
Return the number of dimensions of this dataset.
Definition: frozendataset.cpp:36
const QString & pointName(int i) const
Return the name of the point with the given index.
Definition: frozendataset.cpp:47
Main Gaia namespace, which contains all the library functions.
Definition: addfield.cpp:22
QPair< int, int > descriptorLocation(const QString &descriptorName) const
Return the column indices of beginning and end fo the region spanning the descriptor.
Definition: frozendataset.cpp:181
Definition: frozencosineangledistance.h:28
Definition: parameter.h:34
A FrozenDataSet is a dataset that has been flagged as immutable.
Definition: frozendataset.h:49
Exception class that can take up to 3 arguments of any type, which will be serialized into a QString ...
Definition: gaiaexception.h:46