| 1 |
//########################################################################## |
| 2 |
//# # |
| 3 |
//# CLOUDCOMPARE PLUGIN: qCANUPO # |
| 4 |
//# # |
| 5 |
//# This program is free software; you can redistribute it and/or modify # |
| 6 |
//# it under the terms of the GNU General Public License as published by # |
| 7 |
//# the Free Software Foundation; version 2 or later of the License. # |
| 8 |
//# # |
| 9 |
//# This program is distributed in the hope that it will be useful, # |
| 10 |
//# but WITHOUT ANY WARRANTY; without even the implied warranty of # |
| 11 |
//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
| 12 |
//# GNU General Public License for more details. # |
| 13 |
//# # |
| 14 |
//# COPYRIGHT: UEB (UNIVERSITE EUROPEENNE DE BRETAGNE) / CNRS # |
| 15 |
//# # |
| 16 |
//########################################################################## |
| 17 |
|
| 18 |
//This file is directly inspired of the equivalently named file in the |
| 19 |
//original CANUPO project, by N. Brodu and D. Lague. |
| 20 |
|
| 21 |
#include "classifier.h" |
| 22 |
|
| 23 |
//Qt |
| 24 |
#include <QFile> |
| 25 |
|
| 26 |
//system |
| 27 |
#include <assert.h> |
| 28 |
#include <limits.h> |
| 29 |
|
| 30 |
Classifier::Classifier() |
| 31 |
: class1(0) |
| 32 |
, class2(0) |
| 33 |
, absMaxXY(0) |
| 34 |
, axisScaleRatio(1.0f) |
| 35 |
, descriptorID(DESC_INVALID) |
| 36 |
, dimPerScale(0) |
| 37 |
{} |
| 38 |
|
| 39 |
bool Classifier::checkRefPoints() |
| 40 |
{ |
| 41 |
// dot product with (+1,+1) vector gives the classification sign |
| 42 |
if (refPointPos.x + refPointPos.y < 0) |
| 43 |
{ |
| 44 |
std::swap(refPointPos, refPointNeg); |
| 45 |
} |
| 46 |
|
| 47 |
return (refPointPos.x + refPointPos.y >= 0); |
| 48 |
} |
| 49 |
|
| 50 |
float Classifier::classify2D_checkcondnum(const Point2D& P, const Point2D& R, float& condnumber) const |
| 51 |
{ |
| 52 |
condnumber = 0; |
| 53 |
if (path.size() < 2) |
| 54 |
{ |
| 55 |
assert(false); |
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
// consider each path segment as a mini-classifier |
| 60 |
// the segment PR[Pt-->Refpt] and each path's segment cross |
| 61 |
// iff each one classifies the end point of the other in different classes |
| 62 |
Point2D PR = R - P; |
| 63 |
Point2D u = PR; u.normalize(); |
| 64 |
|
| 65 |
unsigned numcross = 0; |
| 66 |
|
| 67 |
//we'll also look for the distance between P and the nearest segment |
| 68 |
float closestSquareDist = -1.0f; |
| 69 |
|
| 70 |
size_t segCount = path.size() - 1; |
| 71 |
for (size_t i = 0; i < segCount; ++i) |
| 72 |
{ |
| 73 |
//current path segment (or half-line!) |
| 74 |
Point2D AP = P - path[i]; |
| 75 |
Point2D AB = path[i + 1] - path[i]; |
| 76 |
Point2D v = AB; v.normalize(); |
| 77 |
|
| 78 |
condnumber = std::max<float>(condnumber, std::abs(v.dot(u))); |
| 79 |
|
| 80 |
// Compute whether PR[Pt-->Refpt] and that segment cross |
| 81 |
float denom = (u.x*v.y - v.x*u.y); |
| 82 |
if (denom != 0) |
| 83 |
{ |
| 84 |
// 1. check whether the given pt and the refpt are on different sides of the classifier line |
| 85 |
//we search for alpha and beta so that |
| 86 |
// P + alpha * PR = A + beta * AB |
| 87 |
float alpha = (AP.y * v.x - AP.x * v.y) / denom; |
| 88 |
bool pathIntersects = (alpha >= 0 && alpha*alpha <= PR.norm2()); |
| 89 |
if (pathIntersects) |
| 90 |
{ |
| 91 |
float beta = (AP.y * u.x - AP.x * u.y) / denom; |
| 92 |
|
| 93 |
// first and last lines are projected to infinity |
| 94 |
bool refSegIntersects = ((i == 0 || beta >= 0) && (i + 1 == segCount || beta * beta < AB.norm2())); //not "beta*beta <= AB.norm2()" because the equality case will be managed by the next segment! |
| 95 |
|
| 96 |
// crossing iif each segment/line separates the other |
| 97 |
if (refSegIntersects) |
| 98 |
numcross++; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
// closest distance from the point to that segment |
| 103 |
// 1. projection of the point of the line |
| 104 |
float squareDistToSeg = 0; |
| 105 |
float distAH = v.dot(AP); |
| 106 |
if ((i == 0 || distAH >= 0.0) && (i + 1 == segCount || distAH <= AB.norm())) |
| 107 |
{ |
| 108 |
// 2. Is the projection within the segment limit? yes => closest |
| 109 |
Point2D PH = (path[i] + v * distAH) - P; |
| 110 |
squareDistToSeg = PH.norm2(); |
| 111 |
} |
| 112 |
else |
| 113 |
{ |
| 114 |
// 3. otherwise closest is the minimum of the distance to the segment ends |
| 115 |
Point2D BP = P - path[i + 1]; |
| 116 |
squareDistToSeg = std::min(AP.norm2(), BP.norm2()); |
| 117 |
} |
| 118 |
|
| 119 |
if (closestSquareDist < 0 || squareDistToSeg < closestSquareDist) |
| 120 |
{ |
| 121 |
closestSquareDist = squareDistToSeg; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
assert(closestSquareDist >= 0); |
| 126 |
float deltaNorm = sqrt(closestSquareDist); |
| 127 |
|
| 128 |
return ((numcross & 1) == 0 ? deltaNorm : -deltaNorm); |
| 129 |
} |
| 130 |
|
| 131 |
float Classifier::classify2D(const Point2D& P) const |
| 132 |
{ |
| 133 |
float condpos = 0.0f; |
| 134 |
float condneg = 0.0f; |
| 135 |
float predpos = classify2D_checkcondnum(P, refPointPos, condpos); |
| 136 |
float predneg = classify2D_checkcondnum(P, refPointNeg, condneg); |
| 137 |
|
| 138 |
// normal nearly aligned = bad conditionning, the lower the dot prod the better |
| 139 |
return condpos < condneg ? predpos : -predneg; |
| 140 |
} |
| 141 |
|
| 142 |
Classifier::Point2D Classifier::project(const CorePointDesc& mscdata) const |
| 143 |
{ |
| 144 |
assert(weightsAxis1.size() == weightsAxis2.size()); |
| 145 |
assert(weightsAxis1.size() > 1); |
| 146 |
|
| 147 |
//There may be less weights than parameters in the descriptor |
| 148 |
//if we use a descriptor computed with more (bigger) scales. |
| 149 |
//In this case we assume the matching scales are all at the end! |
| 150 |
//(i.e. the smallest) |
| 151 |
size_t weightCount = weightsAxis1.size() - 1; |
| 152 |
size_t paramCount = mscdata.params.size(); |
| 153 |
assert(weightCount <= paramCount); |
| 154 |
unsigned shift = static_cast<unsigned>(paramCount - weightCount); |
| 155 |
|
| 156 |
Point2D P(weightsAxis1.back(), weightsAxis2.back()); |
| 157 |
|
| 158 |
for (size_t i = 0; i < weightCount; ++i) |
| 159 |
{ |
| 160 |
P.x += weightsAxis1[i] * mscdata.params[shift + i]; |
| 161 |
P.y += weightsAxis2[i] * mscdata.params[shift + i]; |
| 162 |
} |
| 163 |
|
| 164 |
return P; |
| 165 |
} |
| 166 |
|
| 167 |
float Classifier::classify(const CorePointDesc& mscdata) const |
| 168 |
{ |
| 169 |
Point2D P = project(mscdata); |
| 170 |
return classify2D(P); |
| 171 |
} |
| 172 |
|
| 173 |
bool Classifier::Load(QString filename, |
| 174 |
std::vector<Classifier>& classifiers, |
| 175 |
std::vector<float>& scales, |
| 176 |
QString& error, |
| 177 |
FileHeader* header/*=nullptr*/, |
| 178 |
bool headerOnly/*=false*/) |
| 179 |
{ |
| 180 |
QFile file(filename); |
| 181 |
if (!file.open(QIODevice::ReadOnly)) |
| 182 |
{ |
| 183 |
error = QString("Failed to open input classifier file!"); |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
scales.clear(); |
| 188 |
classifiers.clear(); |
| 189 |
|
| 190 |
//DGM: sadly we can't use a stream as data in prm files are saved in a strange way |
| 191 |
//QDataStream stream(&file); |
| 192 |
//--> changing the stream 'byte order' doesn't help :( |
| 193 |
//stream.setByteOrder(QDataStream::LittleEndian); |
| 194 |
|
| 195 |
// scales count |
| 196 |
unsigned nscales; |
| 197 |
file.read(reinterpret_cast<char*>(&nscales), sizeof(unsigned)); |
| 198 |
|
| 199 |
unsigned dimPerScale = 2; |
| 200 |
unsigned descriptorID = 1; |
| 201 |
|
| 202 |
if (nscales == 9999) |
| 203 |
{ |
| 204 |
//we are loading a new 'prm' file (generated by qCanupo) |
| 205 |
//descriptor ID |
| 206 |
file.read(reinterpret_cast<char*>(&descriptorID), sizeof(unsigned)); |
| 207 |
//dimension per scale |
| 208 |
file.read(reinterpret_cast<char*>(&dimPerScale), sizeof(unsigned)); |
| 209 |
//and now the real number of scales |
| 210 |
file.read(reinterpret_cast<char*>(&nscales), sizeof(unsigned)); |
| 211 |
} |
| 212 |
|
| 213 |
// values count |
| 214 |
try |
| 215 |
{ |
| 216 |
scales.resize(nscales); |
| 217 |
} |
| 218 |
catch (const std::bad_alloc&) |
| 219 |
{ |
| 220 |
error = QString("Not enough memory to load scales!"); |
| 221 |
return false; |
| 222 |
} |
| 223 |
|
| 224 |
// read scale values |
| 225 |
{ |
| 226 |
for (unsigned s = 0; s < nscales; ++s) |
| 227 |
file.read(reinterpret_cast<char*>(&scales[s]), sizeof(float)); |
| 228 |
} |
| 229 |
|
| 230 |
// number of classifiers |
| 231 |
unsigned nclassifiers; |
| 232 |
file.read(reinterpret_cast<char*>(&nclassifiers), sizeof(unsigned)); |
| 233 |
|
| 234 |
if (header) |
| 235 |
{ |
| 236 |
header->classifierCount = nclassifiers; |
| 237 |
header->dimPerScale = dimPerScale; |
| 238 |
header->descID = descriptorID; |
| 239 |
} |
| 240 |
|
| 241 |
if (headerOnly) |
| 242 |
{ |
| 243 |
//we can stop here |
| 244 |
return true; |
| 245 |
} |
| 246 |
|
| 247 |
// reserve classifiers array |
| 248 |
try |
| 249 |
{ |
| 250 |
classifiers.resize(nclassifiers); |
| 251 |
} |
| 252 |
catch (const std::bad_alloc&) |
| 253 |
{ |
| 254 |
error = QString("Not enough memory to load classifiers!"); |
| 255 |
return false; |
| 256 |
} |
| 257 |
|
| 258 |
// read classifiers |
| 259 |
try |
| 260 |
{ |
| 261 |
const unsigned fdim = nscales * dimPerScale; |
| 262 |
for (unsigned ci = 0; ci < nclassifiers; ++ci) |
| 263 |
{ |
| 264 |
Classifier& classifier = classifiers[ci]; |
| 265 |
|
| 266 |
classifier.dimPerScale = dimPerScale; |
| 267 |
classifier.descriptorID = descriptorID; |
| 268 |
|
| 269 |
classifier.scales = scales; //all classifiers inside a file have the same scales! |
| 270 |
file.read(reinterpret_cast<char*>(&classifier.class1), sizeof(int)); |
| 271 |
file.read(reinterpret_cast<char*>(&classifier.class2), sizeof(int)); |
| 272 |
|
| 273 |
classifier.weightsAxis1.resize(fdim + 1); |
| 274 |
{ |
| 275 |
for (unsigned i = 0; i <= fdim; ++i) |
| 276 |
file.read(reinterpret_cast<char*>(&classifier.weightsAxis1[i]), sizeof(float)); |
| 277 |
} |
| 278 |
classifier.weightsAxis2.resize(fdim + 1); |
| 279 |
{ |
| 280 |
for (unsigned i = 0; i <= fdim; ++i) |
| 281 |
file.read(reinterpret_cast<char*>(&classifier.weightsAxis2[i]), sizeof(float)); |
| 282 |
} |
| 283 |
|
| 284 |
unsigned pathsize; |
| 285 |
file.read(reinterpret_cast<char*>(&pathsize), sizeof(unsigned)); |
| 286 |
classifier.path.resize(pathsize); |
| 287 |
{ |
| 288 |
for (unsigned i = 0; i < pathsize; ++i) |
| 289 |
{ |
| 290 |
file.read(reinterpret_cast<char*>(&classifier.path[i].x), sizeof(float)); |
| 291 |
file.read(reinterpret_cast<char*>(&classifier.path[i].y), sizeof(float)); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
file.read(reinterpret_cast<char*>(&classifier.refPointPos.x), sizeof(float)); |
| 296 |
file.read(reinterpret_cast<char*>(&classifier.refPointPos.y), sizeof(float)); |
| 297 |
file.read(reinterpret_cast<char*>(&classifier.refPointNeg.x), sizeof(float)); |
| 298 |
file.read(reinterpret_cast<char*>(&classifier.refPointNeg.y), sizeof(float)); |
| 299 |
file.read(reinterpret_cast<char*>(&classifier.absMaxXY), sizeof(float)); |
| 300 |
file.read(reinterpret_cast<char*>(&classifier.axisScaleRatio), sizeof(float)); |
| 301 |
|
| 302 |
if (!classifier.checkRefPoints()) |
| 303 |
{ |
| 304 |
//DGM: strange test, that fails for valid classifiers! |
| 305 |
//error = QString("Invalid reference points in the classifier"); |
| 306 |
//return false; |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
catch (const std::bad_alloc&) |
| 311 |
{ |
| 312 |
error = QString("Not enough memory to load classifiers data!"); |
| 313 |
return false; |
| 314 |
} |
| 315 |
|
| 316 |
return true; |
| 317 |
} |
| 318 |
|
| 319 |
bool Classifier::save(QString filename, |
| 320 |
QString& error) |
| 321 |
{ |
| 322 |
QFile file(filename); |
| 323 |
if (!file.open(QIODevice::WriteOnly)) |
| 324 |
{ |
| 325 |
error = QString("Failed to open output file!"); |
| 326 |
return false; |
| 327 |
} |
| 328 |
|
| 329 |
//DGM: sadly we can't use a stream as data in prm files are saved in a strange way |
| 330 |
//see Classifier::Load |
| 331 |
|
| 332 |
if (descriptorID != DESC_DIMENSIONALITY || dimPerScale != 2) |
| 333 |
{ |
| 334 |
//we code the 'new' PRM files with 9999 in place of the number of scales! |
| 335 |
const unsigned headerCode = 9999; |
| 336 |
file.write(reinterpret_cast<const char*>(&headerCode), sizeof(unsigned)); |
| 337 |
|
| 338 |
//descriptor ID |
| 339 |
file.write(reinterpret_cast<const char*>(&descriptorID), sizeof(unsigned)); |
| 340 |
//dimension per scale |
| 341 |
file.write(reinterpret_cast<const char*>(&dimPerScale), sizeof(unsigned)); |
| 342 |
} |
| 343 |
|
| 344 |
// number of scales |
| 345 |
unsigned nscales = static_cast<unsigned>(scales.size()); |
| 346 |
file.write(reinterpret_cast<const char*>(&nscales), sizeof(unsigned)); |
| 347 |
|
| 348 |
// write scale values |
| 349 |
{ |
| 350 |
for (unsigned s = 0; s < nscales; ++s) |
| 351 |
file.write(reinterpret_cast<const char*>(&scales[s]), sizeof(float)); |
| 352 |
} |
| 353 |
|
| 354 |
// number of 2-class classifiers = 1! |
| 355 |
unsigned nclassifiers = 1; |
| 356 |
file.write(reinterpret_cast<const char*>(&nclassifiers), sizeof(unsigned)); |
| 357 |
|
| 358 |
// write classifier |
| 359 |
const unsigned fdim = nscales * dimPerScale; |
| 360 |
file.write(reinterpret_cast<const char*>(&class1), sizeof(int)); |
| 361 |
file.write(reinterpret_cast<const char*>(&class2), sizeof(int)); |
| 362 |
|
| 363 |
//weightsAxis1 |
| 364 |
{ |
| 365 |
for (unsigned i = 0; i <= fdim; ++i) |
| 366 |
file.write(reinterpret_cast<const char*>(&weightsAxis1[i]), sizeof(float)); |
| 367 |
} |
| 368 |
//weightsAxis2 |
| 369 |
{ |
| 370 |
for (unsigned i = 0; i <= fdim; ++i) |
| 371 |
file.write(reinterpret_cast<const char*>(&weightsAxis2[i]), sizeof(float)); |
| 372 |
} |
| 373 |
|
| 374 |
unsigned pathsize = static_cast<unsigned>(path.size()); |
| 375 |
file.write(reinterpret_cast<const char*>(&pathsize), sizeof(unsigned)); |
| 376 |
{ |
| 377 |
for (unsigned i = 0; i < pathsize; ++i) |
| 378 |
{ |
| 379 |
file.write(reinterpret_cast<const char*>(&path[i].x), sizeof(float)); |
| 380 |
file.write(reinterpret_cast<const char*>(&path[i].y), sizeof(float)); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
file.write(reinterpret_cast<const char*>(&refPointPos.x), sizeof(float)); |
| 385 |
file.write(reinterpret_cast<const char*>(&refPointPos.y), sizeof(float)); |
| 386 |
file.write(reinterpret_cast<const char*>(&refPointNeg.x), sizeof(float)); |
| 387 |
file.write(reinterpret_cast<const char*>(&refPointNeg.y), sizeof(float)); |
| 388 |
file.write(reinterpret_cast<const char*>(&absMaxXY), sizeof(float)); |
| 389 |
file.write(reinterpret_cast<const char*>(&axisScaleRatio), sizeof(float)); |
| 390 |
|
| 391 |
file.close(); |
| 392 |
|
| 393 |
return true; |
| 394 |
} |