[1044] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
[1120] | 2 | // Copyright (C) 1999-2021 Maciej Komosinski and Szymon Ulatowski. |
---|
[1044] | 3 | // See LICENSE.txt for details. |
---|
| 4 | |
---|
| 5 | #include "measure-distribution.h" |
---|
| 6 | #include <common/nonstd_math.h> |
---|
| 7 | #include <limits> |
---|
| 8 | #include "EMD/emd.c" |
---|
| 9 | #include <iostream> |
---|
| 10 | |
---|
| 11 | #define FIELDSTRUCT SimilMeasureDistribution |
---|
| 12 | |
---|
| 13 | static ParamEntry simil_distribution_paramtab[] = { |
---|
[1054] | 14 | { "Creature: Similarity: Descriptor distribution", 1, 4, "SimilMeasureDistribution", "Evaluates morphological dissimilarity using distribution measure.", }, |
---|
[1044] | 15 | { "simil_density", 0, 0, "Density of surface sampling", "f 1 100 10", FIELD(density), "", }, |
---|
| 16 | { "simil_bin_num", 0, 0, "Number of bins", "d 1 1000 128", FIELD(bin_num), "", }, |
---|
[1120] | 17 | { "simil_samples_num", 0, 0, "Number of samples", "d 1 1048576 10000", FIELD(samples_num), "", }, //based on experiments, not much accuracy to gain when this is increased above 1000 |
---|
[1046] | 18 | { "evaluateDistance", 0, PARAM_DONTSAVE | PARAM_USERHIDDEN, "Evaluate model dissimilarity", "p f(oGeno,oGeno)", PROCEDURE(p_evaldistance), "Calculates dissimilarity between two models created from Geno objects.", }, |
---|
[1044] | 19 | { 0, }, |
---|
| 20 | }; |
---|
| 21 | |
---|
| 22 | #undef FIELDSTRUCT |
---|
| 23 | |
---|
| 24 | SimilMeasureDistribution::SimilMeasureDistribution() : localpar(simil_distribution_paramtab, this) |
---|
| 25 | { |
---|
| 26 | localpar.setDefault(); |
---|
| 27 | SimilMeasureDistribution::distribution_fun = &SimilMeasureDistribution::D2; //D1 and D2 are the best descriptors |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | double SimilMeasureDistribution::getDistance() |
---|
| 31 | { |
---|
| 32 | double dist = 0; |
---|
| 33 | for (int i = 0; i < 2; i++) |
---|
| 34 | { |
---|
| 35 | funs[i] = new std::pair<double, float>[bin_num](); |
---|
| 36 | for (int j = 0; j < bin_num; j++) |
---|
| 37 | funs[i][j] = std::make_pair(0, 0); |
---|
| 38 | } |
---|
[1120] | 39 | |
---|
[1044] | 40 | for (int i = 0; i < 2; i++) |
---|
| 41 | sst_models[i] = new SolidsShapeTypeModel((*models[i])); |
---|
[1120] | 42 | |
---|
| 43 | SimilMeasureDistribution::calculateFuns(); |
---|
[1044] | 44 | dist = SimilMeasureDistribution::compareFuns(); |
---|
[1120] | 45 | |
---|
[1044] | 46 | for (int i = 0; i < 2; i++) |
---|
| 47 | { |
---|
| 48 | SAFEDELETE(sst_models[i]); |
---|
| 49 | SAFEDELETEARRAY(funs[i]); |
---|
| 50 | } |
---|
| 51 | return dist; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | int SimilMeasureDistribution::setParams(std::vector<double> params) |
---|
| 55 | { |
---|
| 56 | for (unsigned int i = 0; i < params.size(); i++) |
---|
| 57 | if (params.at(i) <= 0) |
---|
| 58 | { |
---|
| 59 | logPrintf("SimilDistributionMeasure", "setParams", LOG_ERROR, "Param values should be larger than 0."); |
---|
| 60 | return -1; |
---|
| 61 | } |
---|
[1120] | 62 | |
---|
[1044] | 63 | density = params.at(0); |
---|
| 64 | bin_num = params.at(1); |
---|
| 65 | samples_num = params.at(2); |
---|
[1120] | 66 | |
---|
[1044] | 67 | return 0; |
---|
| 68 | } |
---|
| 69 | |
---|
[1120] | 70 | void SimilMeasureDistribution::calculateFun(std::pair<double, float> *fun, const Model &sampled) |
---|
[1044] | 71 | { |
---|
| 72 | int samples_taken = samples_num; |
---|
| 73 | |
---|
[1125] | 74 | //Check if total number of point pairs is smaller than samples number (just to avoid the calculation of the same stats for the same pairs of points). |
---|
[1121] | 75 | //This optimization turned out to have a minor effect, only present for very high simil_samples_num, and was not perfect anyway: |
---|
| 76 | //- samples are selected randomly so there are no guarantees that they will be repeated, |
---|
| 77 | //- even if they do, it has the benefit of averaging the result that becomes more stable, |
---|
| 78 | //- the concept of "point pairs" is not relevant when we randomly select more than two points, as some descriptors do. |
---|
| 79 | //int size = sampled.getPartCount(); |
---|
| 80 | //if (size < (int) sqrt((double) std::numeric_limits<int>::max())) //prevent exceeding int limits |
---|
[1130] | 81 | // samples_taken = std::min(samples_num, size*size); |
---|
[1044] | 82 | |
---|
[1120] | 83 | rndgen.seed(55); //For determinism. Otherwise the descriptors (that choose samples pseudo-randomly) for the same Model can yield different values and so the dissimilarity between the object and its copy will not be 0. |
---|
| 84 | std::uniform_int_distribution<> uniform_distrib(0, sampled.getPartCount() - 1); |
---|
| 85 | |
---|
[1044] | 86 | //Get sampled distribution |
---|
[1120] | 87 | std::vector<double> dist_vect; |
---|
| 88 | dist_vect.reserve(samples_taken); //we will add up to samples_taken elements to this vector |
---|
| 89 | (this->*SimilMeasureDistribution::distribution_fun)(samples_taken, uniform_distrib, sampled, dist_vect); |
---|
[1044] | 90 | |
---|
| 91 | auto result = std::minmax_element(dist_vect.begin(), dist_vect.end()); |
---|
| 92 | double min = *result.first; |
---|
| 93 | double max = *result.second; |
---|
| 94 | |
---|
| 95 | //Create histogram |
---|
[1120] | 96 | vector<int> hist(bin_num); |
---|
[1044] | 97 | int ind = 0; |
---|
| 98 | |
---|
| 99 | for (unsigned int j = 0; j < dist_vect.size(); j++) |
---|
| 100 | { |
---|
[1120] | 101 | ind = (int)std::floor((dist_vect.at(j) - min) * 1 / (max - min) * bin_num); |
---|
| 102 | if (ind <= (bin_num - 1)) |
---|
| 103 | hist[ind]++; |
---|
[1044] | 104 | else if (ind == bin_num) |
---|
[1120] | 105 | hist[bin_num - 1]++; |
---|
[1044] | 106 | } |
---|
| 107 | |
---|
| 108 | //Create pairs |
---|
| 109 | for (int j = 0; j < bin_num; j++) |
---|
| 110 | { |
---|
[1120] | 111 | fun[j] = std::make_pair(min + (max - min) / bin_num * (j + 0.5), hist[j]); |
---|
[1044] | 112 | } |
---|
| 113 | |
---|
| 114 | //Normalize |
---|
| 115 | float total_mass = 0; |
---|
| 116 | for (int j = 0; j < bin_num; j++) |
---|
[1120] | 117 | { |
---|
| 118 | total_mass += fun[j].second; |
---|
| 119 | } |
---|
[1044] | 120 | |
---|
| 121 | for (int j = 0; j < bin_num; j++) |
---|
| 122 | fun[j].second /= total_mass; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | void SimilMeasureDistribution::calculateFuns() |
---|
| 126 | { |
---|
| 127 | for (int i = 0; i < 2; i++) |
---|
| 128 | { |
---|
| 129 | Model sampled = SimilMeasureDistribution::sampleSurface(&sst_models[i]->getModel(), density); |
---|
| 130 | SimilMeasureDistribution::calculateFun(funs[i], sampled); |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | double SimilMeasureDistribution::compareFuns() |
---|
| 135 | { |
---|
| 136 | return SimilMeasureDistribution::EMD(funs[0], funs[1]); |
---|
| 137 | } |
---|
| 138 | |
---|
[1120] | 139 | void SimilMeasureDistribution::D1(int samples_taken, std::uniform_int_distribution<> &distribution, const Model &sampled, std::vector<double> &dist_vect) |
---|
[1044] | 140 | { |
---|
[1120] | 141 | int size = sampled.getPartCount(); |
---|
[1044] | 142 | double x = 0; |
---|
| 143 | double y = 0; |
---|
| 144 | double z = 0; |
---|
[1120] | 145 | |
---|
[1044] | 146 | for (int i = 0; i < size; i++) |
---|
| 147 | { |
---|
[1120] | 148 | Pt3D pos = sampled.getPart(i)->p; |
---|
[1044] | 149 | x += pos.x; |
---|
| 150 | y += pos.y; |
---|
| 151 | z += pos.z; |
---|
| 152 | } |
---|
[1120] | 153 | |
---|
| 154 | x = x / size; |
---|
| 155 | y = y / size; |
---|
| 156 | z = z / size; |
---|
| 157 | |
---|
| 158 | Pt3D centroid = { x, y, z }; |
---|
| 159 | |
---|
[1044] | 160 | for (int i = 0; i < samples_taken; i++) |
---|
| 161 | { |
---|
[1120] | 162 | int p1 = distribution(rndgen); |
---|
| 163 | double dist = sampled.getPart(p1)->p.distanceTo(centroid); |
---|
[1044] | 164 | if (dist > 0) |
---|
| 165 | dist_vect.push_back(dist); |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | |
---|
[1120] | 169 | void SimilMeasureDistribution::D2(int samples_taken, std::uniform_int_distribution<> &distribution, const Model &sampled, std::vector<double> &dist_vect) |
---|
[1044] | 170 | { |
---|
| 171 | for (int i = 0; i < samples_taken; i++) |
---|
| 172 | { |
---|
[1120] | 173 | int p1 = distribution(rndgen); |
---|
| 174 | int p2 = distribution(rndgen); |
---|
| 175 | double dist = sampled.getPart(p1)->p.distanceTo(sampled.getPart(p2)->p); |
---|
[1044] | 176 | if (dist > 0) |
---|
| 177 | dist_vect.push_back(dist); |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | |
---|
[1120] | 181 | void SimilMeasureDistribution::D3(int samples_taken, std::uniform_int_distribution<> &distribution, const Model &sampled, std::vector<double> &dist_vect) |
---|
[1044] | 182 | { |
---|
| 183 | for (int i = 0; i < samples_taken; i++) |
---|
| 184 | { |
---|
[1120] | 185 | int p1 = distribution(rndgen); |
---|
| 186 | int p2 = distribution(rndgen); |
---|
| 187 | int p3 = distribution(rndgen); |
---|
| 188 | |
---|
| 189 | Pt3D v(sampled.getPart(p2)->p); |
---|
| 190 | Pt3D w(sampled.getPart(p3)->p); |
---|
| 191 | v -= sampled.getPart(p1)->p; |
---|
| 192 | w -= sampled.getPart(p1)->p; |
---|
[1044] | 193 | Pt3D cross_prod(0); |
---|
| 194 | cross_prod.vectorProduct(v, w); |
---|
[1120] | 195 | |
---|
[1044] | 196 | double dist = 0.5 * cross_prod.length(); |
---|
| 197 | if (dist > 0) |
---|
| 198 | dist_vect.push_back(dist); |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | |
---|
[1120] | 202 | void SimilMeasureDistribution::D4(int samples_taken, std::uniform_int_distribution<> &distribution, const Model &sampled, std::vector<double> &dist_vect) |
---|
[1044] | 203 | { |
---|
| 204 | for (int i = 0; i < samples_taken; i++) |
---|
| 205 | { |
---|
[1120] | 206 | int a = distribution(rndgen); |
---|
| 207 | int b = distribution(rndgen); |
---|
| 208 | int c = distribution(rndgen); |
---|
| 209 | int d = distribution(rndgen); |
---|
| 210 | |
---|
| 211 | Pt3D ad(sampled.getPart(a)->p); |
---|
| 212 | Pt3D bd(sampled.getPart(b)->p); |
---|
| 213 | Pt3D cd(sampled.getPart(c)->p); |
---|
| 214 | |
---|
| 215 | ad -= sampled.getPart(d)->p; |
---|
| 216 | bd -= sampled.getPart(d)->p; |
---|
| 217 | cd -= sampled.getPart(d)->p; |
---|
| 218 | |
---|
[1044] | 219 | Pt3D cross_prod(0); |
---|
| 220 | cross_prod.vectorProduct(bd, cd); |
---|
| 221 | cross_prod.entrywiseProduct(ad); |
---|
[1120] | 222 | |
---|
| 223 | double dist = cross_prod.length() / 6; |
---|
[1044] | 224 | if (dist > 0) |
---|
| 225 | dist_vect.push_back(dist); |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | |
---|
[1120] | 229 | void SimilMeasureDistribution::A3(int samples_taken, std::uniform_int_distribution<> &distribution, const Model &sampled, std::vector<double> &dist_vect) |
---|
[1044] | 230 | { |
---|
| 231 | for (int i = 0; i < samples_taken; i++) |
---|
| 232 | { |
---|
[1120] | 233 | int p1 = distribution(rndgen); |
---|
| 234 | int p2 = distribution(rndgen); |
---|
| 235 | int p3 = distribution(rndgen); |
---|
| 236 | double a = sampled.getPart(p1)->p.distanceTo(sampled.getPart(p3)->p); |
---|
| 237 | double b = sampled.getPart(p3)->p.distanceTo(sampled.getPart(p2)->p); |
---|
| 238 | double c = sampled.getPart(p1)->p.distanceTo(sampled.getPart(p2)->p); |
---|
| 239 | double beta = acos((a * a + b * b - c * c) / (2 * a * b)); |
---|
| 240 | |
---|
[1044] | 241 | if (!std::isnan(beta)) |
---|
| 242 | dist_vect.push_back(beta); |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | |
---|
| 247 | float dist(feature_t* F1, feature_t* F2) |
---|
[1120] | 248 | { |
---|
| 249 | return abs((*F1) - (*F2)); |
---|
[1044] | 250 | } |
---|
| 251 | |
---|
| 252 | |
---|
| 253 | void SimilMeasureDistribution::fillPointsWeights(std::pair<double, float> *fun, feature_t *points, float *weights) |
---|
| 254 | { |
---|
| 255 | for (int j = 0; j < bin_num; j++) |
---|
| 256 | { |
---|
[1120] | 257 | points[j] = { fun[j].first }; |
---|
[1044] | 258 | weights[j] = fun[j].second; |
---|
| 259 | } |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | double SimilMeasureDistribution::EMD(std::pair<double, float> *fun1, std::pair<double, float> *fun2) |
---|
| 263 | { |
---|
| 264 | feature_t *points[2]; |
---|
| 265 | float *weights[2]; |
---|
[1120] | 266 | |
---|
[1044] | 267 | for (int i = 0; i < 2; i++) |
---|
| 268 | { |
---|
| 269 | points[i] = new feature_t[bin_num]; |
---|
| 270 | weights[i] = new float[bin_num](); |
---|
| 271 | } |
---|
| 272 | SimilMeasureDistribution::fillPointsWeights(fun1, points[0], weights[0]); |
---|
| 273 | SimilMeasureDistribution::fillPointsWeights(fun2, points[1], weights[1]); |
---|
| 274 | |
---|
[1120] | 275 | signature_t sig1 = { bin_num, points[0], weights[0] }, |
---|
| 276 | sig2 = { bin_num, points[1], weights[1] }; |
---|
| 277 | |
---|
[1062] | 278 | float e = emd(&sig1, &sig2, dist, 0, 0, bin_num, bin_num); |
---|
[1120] | 279 | |
---|
[1044] | 280 | for (int i = 0; i < 2; i++) |
---|
| 281 | { |
---|
| 282 | delete[] points[i]; |
---|
| 283 | delete[] weights[i]; |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | return e; |
---|
| 287 | } |
---|