|
| 1 | +//#include <QCoreApplication> |
| 2 | + |
| 3 | +#include <QDebug> |
| 4 | + |
| 5 | +#include <QVector> |
| 6 | +#include <QPointF> |
| 7 | + |
| 8 | +#include <math.h> |
| 9 | + |
| 10 | +#include "../../application/logic/toolbox/toolbox_math_geometry.h" |
| 11 | +#include "../../application/logic/model/core/S_Segment.h" |
| 12 | + |
| 13 | +#include "ImathVec.h" |
| 14 | +using namespace IMATH_INTERNAL_NAMESPACE; |
| 15 | + |
| 16 | +int test_relocation(); |
| 17 | + |
| 18 | +void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) |
| 19 | +{ |
| 20 | + QByteArray localMsg = msg.toLocal8Bit(); |
| 21 | + switch (type) { |
| 22 | + case QtDebugMsg: |
| 23 | + fprintf(stdout, "Dbg: %s\n", localMsg.constData()); |
| 24 | + //fprintf(stdout, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); |
| 25 | + break; |
| 26 | + case QtInfoMsg: |
| 27 | + fprintf(stdout, "Inf: %s\n", localMsg.constData()); |
| 28 | + //fprintf(stdout, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); |
| 29 | + break; |
| 30 | + case QtWarningMsg: |
| 31 | + fprintf(stdout, "warning: %s\n", localMsg.constData()); |
| 32 | + //fprintf(stdout, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); |
| 33 | + break; |
| 34 | + case QtCriticalMsg: |
| 35 | + fprintf(stdout, "critical: %s\n", localMsg.constData()); |
| 36 | + //fprintf(stdout, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); |
| 37 | + break; |
| 38 | + case QtFatalMsg: |
| 39 | + fprintf(stdout, "fatal: %s\n", localMsg.constData()); |
| 40 | + //fprintf(stdout, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); |
| 41 | + abort(); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +int main(int argc, char *argv[]) { |
| 47 | + //QCoreApplication a(argc, argv); |
| 48 | + //return a.exec(); |
| 49 | + |
| 50 | + qInstallMessageHandler(myMessageOutput); // Install the handler |
| 51 | + |
| 52 | + test_relocation(); |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +int test_relocation() { |
| 57 | + |
| 58 | + /* |
| 59 | + + + + |
| 60 | + \|/ |
| 61 | + +-+-+ |
| 62 | + /|\ |
| 63 | + + + + |
| 64 | + */ |
| 65 | + //testings in different directions for segment AB |
| 66 | + QVector<QPointF> qvectqpf_point_startBase { |
| 67 | + {0,-1}, |
| 68 | + {0,0}, |
| 69 | + {0,1}, |
| 70 | + |
| 71 | + {1,-1}, |
| 72 | + {1,0}, |
| 73 | + {1,1}, |
| 74 | + |
| 75 | + {-1,-1}, |
| 76 | + {-1,0}, |
| 77 | + {-1,1}, |
| 78 | + |
| 79 | + {cos( (M_PI/8.0)), sin( (M_PI/8.0))}, |
| 80 | + {cos(M_PI-(M_PI/8.0)), sin(M_PI-(M_PI/8.0))}, |
| 81 | + {cos(M_PI+(M_PI/8.0)), sin(M_PI+(M_PI/8.0))}, |
| 82 | + {cos( -(M_PI/8.0)), sin( -(M_PI/8.0))}, |
| 83 | + }; |
| 84 | + |
| 85 | + |
| 86 | + //and testing at different locations around zero for segment AB |
| 87 | + QVector<QPointF> qvectqpf_shiftForPointA_startBase = qvectqpf_point_startBase; |
| 88 | + |
| 89 | + QVector<double> qvectqpf_shiftMultiplier_forShiftForPointA { 1.0, 1.50, 3, 10}; |
| 90 | + |
| 91 | + //and testing with different segment AB Length |
| 92 | + QVector<double> qvectdbl_segmentLength { 1.0, |
| 93 | + 2.0, 5.14, 13.723, 12345.678 }; |
| 94 | + |
| 95 | + |
| 96 | + int count_OK_relocatedInsideSegment = 0; |
| 97 | + int count_OK_isAandRelocatedToA = 0; |
| 98 | + int count_OK_isBandRelocatedToB = 0; |
| 99 | + int count_OK_shouldNotBeRelocated = 0; |
| 100 | + |
| 101 | + int count_FAIL_relocatedInsideSegment = 0; |
| 102 | + int count_FAIL_isAandRelocatedToA = 0; |
| 103 | + int count_FAIL_isBandRelocatedToB = 0; |
| 104 | + int count_FAIL_shouldNotBeRelocated = 0; |
| 105 | + |
| 106 | + int total_tested = 0; |
| 107 | + int count_shouldBeRelocated = 0; |
| 108 | + int count_shouldNotBeRelocated = 0; |
| 109 | + |
| 110 | + int count_internalError = 0; |
| 111 | + |
| 112 | + for (auto pointA: qvectqpf_point_startBase) { |
| 113 | + for (auto shiftForPointA: qvectqpf_shiftForPointA_startBase) { |
| 114 | + for (auto shiftMultiplier: qvectqpf_shiftMultiplier_forShiftForPointA) { |
| 115 | + for (auto segmentLength: qvectdbl_segmentLength) { |
| 116 | + double A_x = pointA.x() + shiftForPointA.x()*shiftMultiplier; |
| 117 | + double A_y = pointA.y() + shiftForPointA.y()*shiftMultiplier; |
| 118 | + |
| 119 | + for (auto pointB: qvectqpf_point_startBase) { |
| 120 | + |
| 121 | + double B_x = A_x + pointB.x()*segmentLength; |
| 122 | + double B_y = A_y + pointB.x()*segmentLength; |
| 123 | + |
| 124 | + S_Segment segmentAB; |
| 125 | + segmentAB._ptA = {A_x, A_y}; |
| 126 | + segmentAB._ptB = {B_x, B_y}; |
| 127 | + segmentAB._bValid = true; |
| 128 | + |
| 129 | + if (segmentAB.length() < 1.0) { |
| 130 | + qDebug() << __FUNCTION__ << " bypassed segmentAB due to length() < 1.0 :" << segmentAB._ptA << " => " << segmentAB._ptB; |
| 131 | + continue; |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + //on A |
| 136 | + QPointF qpf_toRelocate_0 {A_x, A_y}; |
| 137 | + |
| 138 | + //on B |
| 139 | + QPointF qpf_toRelocate_1 {B_x, B_y}; |
| 140 | + |
| 141 | + //middle of segment AB |
| 142 | + QPointF qpf_toRelocate_2 {(A_x+B_x)/2.0, |
| 143 | + (A_y+B_y)/2.0}; |
| 144 | + |
| 145 | + //1/3 from A in segment AB |
| 146 | + Vec2<double> vec2_AB {B_x - A_x, |
| 147 | + B_y - A_y }; |
| 148 | + Vec2<double> vec2_ABnorm = vec2_AB.normalized(); |
| 149 | + |
| 150 | + QPointF qpf_toRelocate_3 {A_x + 1.0/3.0 * vec2_ABnorm.x, |
| 151 | + A_y + 1.0/3.0 * vec2_ABnorm.y}; |
| 152 | + |
| 153 | + //projectable on segment AB (close to the middle of segment AB) |
| 154 | + QPointF qpf_toRelocate_4 {qpf_toRelocate_2.x() + 0.1 * vec2_AB.length(), |
| 155 | + qpf_toRelocate_2.y() + 0.1 * vec2_AB.length()}; |
| 156 | + |
| 157 | + QPointF qpf_toRelocate_5 {qpf_toRelocate_2.x() + 0.1 * vec2_AB.length(), |
| 158 | + qpf_toRelocate_2.y() - 0.1 * vec2_AB.length()}; |
| 159 | + |
| 160 | + QPointF qpf_toRelocate_6 {qpf_toRelocate_2.x() - 0.1 * vec2_AB.length(), |
| 161 | + qpf_toRelocate_2.y() + 0.1 * vec2_AB.length()}; |
| 162 | + |
| 163 | + QPointF qpf_toRelocate_7 {qpf_toRelocate_2.x() - 0.1 * vec2_AB.length(), |
| 164 | + qpf_toRelocate_2.y() - 0.1 * vec2_AB.length()}; |
| 165 | + |
| 166 | + // not projectable on segment |
| 167 | + QPointF qpf_toRelocate_8 {A_x + 3.0 * vec2_AB.length() * vec2_ABnorm.x, |
| 168 | + A_y + 3.0 * vec2_AB.length() * vec2_ABnorm.y}; |
| 169 | + |
| 170 | + QPointF qpf_toRelocate_9 {A_x - 4.0 * vec2_AB.length() * vec2_ABnorm.x, |
| 171 | + A_y - 4.0 * vec2_AB.length() * vec2_ABnorm.y}; |
| 172 | + |
| 173 | + QVector<QPointF> qvectqpf_toRelocate; |
| 174 | + qvectqpf_toRelocate.push_back(qpf_toRelocate_0); |
| 175 | + qvectqpf_toRelocate.push_back(qpf_toRelocate_1); |
| 176 | + qvectqpf_toRelocate.push_back(qpf_toRelocate_2); |
| 177 | + qvectqpf_toRelocate.push_back(qpf_toRelocate_3); |
| 178 | + qvectqpf_toRelocate.push_back(qpf_toRelocate_4); |
| 179 | + qvectqpf_toRelocate.push_back(qpf_toRelocate_5); |
| 180 | + qvectqpf_toRelocate.push_back(qpf_toRelocate_6); |
| 181 | + qvectqpf_toRelocate.push_back(qpf_toRelocate_7); |
| 182 | + qvectqpf_toRelocate.push_back(qpf_toRelocate_8); |
| 183 | + qvectqpf_toRelocate.push_back(qpf_toRelocate_9); |
| 184 | + |
| 185 | + int idx_toRelocate = 0; |
| 186 | + for (auto pointC: qvectqpf_toRelocate) { |
| 187 | + |
| 188 | + qDebug() << __FUNCTION__ << "idx_toRelocate =" << idx_toRelocate; |
| 189 | + qDebug() << __FUNCTION__ << "segmentAB = " << segmentAB._ptA << " => " << segmentAB._ptB; |
| 190 | + qDebug() << __FUNCTION__ << "pointC = " << pointC; |
| 191 | + QPointF qpf_relocated_pointC {0, 0}; |
| 192 | + e_ResultDetailsOfRelocatedPointInSegment eRDORPIS = relocatePointInSegment(pointC, segmentAB, qpf_relocated_pointC); |
| 193 | + |
| 194 | + S_Segment segment_additionnalTest; |
| 195 | + |
| 196 | + if (eRDORPIS == e_RDORPIS_pointABCnotAligned) { |
| 197 | + count_internalError++; |
| 198 | + } else { |
| 199 | + |
| 200 | + if ( (eRDORPIS == e_RDORPIS_relocatedToA) |
| 201 | + ||(eRDORPIS == e_RDORPIS_relocatedToB) |
| 202 | + ||(eRDORPIS == e_RDORPIS_relocatedInsideSegment)) { |
| 203 | + qDebug() << __FUNCTION__ << "qpf_relocated_pointC =" << qpf_relocated_pointC; |
| 204 | + } |
| 205 | + |
| 206 | + switch (idx_toRelocate) { |
| 207 | + case 0: //A |
| 208 | + switch (eRDORPIS) { |
| 209 | + case e_RDORPIS_relocatedToA: |
| 210 | + case e_RDORPIS_relocatedInsideSegment: |
| 211 | + count_OK_isAandRelocatedToA++; |
| 212 | + break; |
| 213 | + |
| 214 | + case e_RDORPIS_notRelocated_AisCloser: |
| 215 | + case e_RDORPIS_relocatedToB: |
| 216 | + case e_RDORPIS_notRelocated_BisCloser: |
| 217 | + count_FAIL_isAandRelocatedToA++; |
| 218 | + qDebug() << "failure isA"; |
| 219 | + break; |
| 220 | + |
| 221 | + default: |
| 222 | + break; |
| 223 | + } |
| 224 | + break; |
| 225 | + |
| 226 | + case 1: //B |
| 227 | + switch (eRDORPIS) { |
| 228 | + case e_RDORPIS_relocatedToB: |
| 229 | + case e_RDORPIS_relocatedInsideSegment: |
| 230 | + count_OK_isBandRelocatedToB++; |
| 231 | + break; |
| 232 | + |
| 233 | + case e_RDORPIS_relocatedToA: |
| 234 | + case e_RDORPIS_notRelocated_AisCloser: |
| 235 | + case e_RDORPIS_notRelocated_BisCloser: |
| 236 | + count_FAIL_isBandRelocatedToB++; |
| 237 | + qDebug() << "failure isB"; |
| 238 | + break; |
| 239 | + default: |
| 240 | + break; |
| 241 | + } |
| 242 | + break; |
| 243 | + |
| 244 | + //inside segment |
| 245 | + case 2: |
| 246 | + case 3: |
| 247 | + case 4: |
| 248 | + case 5: |
| 249 | + case 6: |
| 250 | + case 7: |
| 251 | + switch (eRDORPIS) { |
| 252 | + case e_RDORPIS_relocatedInsideSegment: |
| 253 | + count_OK_relocatedInsideSegment++; |
| 254 | + if ( (idx_toRelocate == 2) |
| 255 | + ||(idx_toRelocate == 3)) { |
| 256 | + |
| 257 | + segment_additionnalTest._ptA = pointC; |
| 258 | + segment_additionnalTest._ptB = qpf_relocated_pointC; |
| 259 | + qDebug() << "distance pointC to qpf_relocated_pointC for test point C already on the segment: " |
| 260 | + << QString::number(segment_additionnalTest.length(), 'f', 14); |
| 261 | + } |
| 262 | + break; |
| 263 | + |
| 264 | + case e_RDORPIS_relocatedToA: |
| 265 | + case e_RDORPIS_notRelocated_AisCloser: |
| 266 | + case e_RDORPIS_relocatedToB: |
| 267 | + case e_RDORPIS_notRelocated_BisCloser: |
| 268 | + count_FAIL_relocatedInsideSegment++; |
| 269 | + qDebug() << "failure inside"; |
| 270 | + break; |
| 271 | + default: |
| 272 | + break; |
| 273 | + } |
| 274 | + break; |
| 275 | + |
| 276 | + //not projectable points on segment: |
| 277 | + case 8: |
| 278 | + case 9: |
| 279 | + switch (eRDORPIS) { |
| 280 | + case e_RDORPIS_notRelocated_AisCloser: |
| 281 | + case e_RDORPIS_notRelocated_BisCloser: |
| 282 | + count_OK_shouldNotBeRelocated++; |
| 283 | + break; |
| 284 | + |
| 285 | + case e_RDORPIS_relocatedInsideSegment: |
| 286 | + case e_RDORPIS_relocatedToA: |
| 287 | + case e_RDORPIS_relocatedToB: |
| 288 | + count_FAIL_shouldNotBeRelocated++; |
| 289 | + qDebug() << "failure not projectable"; |
| 290 | + break; |
| 291 | + |
| 292 | + default: |
| 293 | + break; |
| 294 | + } |
| 295 | + break; |
| 296 | + } |
| 297 | + } |
| 298 | + qDebug() << __FUNCTION__ << "---"; |
| 299 | + |
| 300 | + if (idx_toRelocate < 8) { |
| 301 | + count_shouldBeRelocated++; |
| 302 | + } else { |
| 303 | + count_shouldNotBeRelocated++; |
| 304 | + } |
| 305 | + total_tested++; |
| 306 | + idx_toRelocate++; |
| 307 | + } |
| 308 | + } |
| 309 | + } |
| 310 | + } |
| 311 | + } |
| 312 | + } |
| 313 | + |
| 314 | + qDebug() << __FUNCTION__ << "count_internalError :" << count_internalError; |
| 315 | + qDebug() << __FUNCTION__ ; |
| 316 | + |
| 317 | + qDebug() << __FUNCTION__ << " count_OK_isAandRelocatedToA : " << count_OK_isAandRelocatedToA; |
| 318 | + qDebug() << __FUNCTION__ << " count_FAIL_isAandRelocatedToA : " << count_FAIL_isAandRelocatedToA; |
| 319 | + qDebug() << __FUNCTION__; |
| 320 | + |
| 321 | + qDebug() << __FUNCTION__ << " count_OK_isBandRelocatedToB : " << count_OK_isBandRelocatedToB; |
| 322 | + qDebug() << __FUNCTION__ << " count_FAIL_isBandRelocatedToB : " << count_FAIL_isBandRelocatedToB; |
| 323 | + qDebug() << __FUNCTION__; |
| 324 | + |
| 325 | + qDebug() << __FUNCTION__ << " count_OK_relocatedInsideSegment : " << count_OK_relocatedInsideSegment; |
| 326 | + qDebug() << __FUNCTION__ << " count_FAIL_relocatedInsideSegment: " << count_FAIL_relocatedInsideSegment; |
| 327 | + qDebug() << __FUNCTION__; |
| 328 | + |
| 329 | + qDebug() << __FUNCTION__ << "count OK any relocated : " << count_OK_isAandRelocatedToA |
| 330 | + + count_OK_isBandRelocatedToB |
| 331 | + + count_OK_relocatedInsideSegment; |
| 332 | + qDebug() << __FUNCTION__ << "count FAIL any relocated : " << count_FAIL_isAandRelocatedToA |
| 333 | + + count_FAIL_isBandRelocatedToB |
| 334 | + + count_FAIL_relocatedInsideSegment; |
| 335 | + qDebug() << __FUNCTION__; |
| 336 | + |
| 337 | + qDebug() << __FUNCTION__ << "count_OK_shouldNotBeRelocated : " << count_OK_shouldNotBeRelocated; |
| 338 | + qDebug() << __FUNCTION__ << "count_FAIL_shouldNotBeRelocated : " << count_FAIL_shouldNotBeRelocated; |
| 339 | + qDebug() << __FUNCTION__; |
| 340 | + |
| 341 | + qDebug() << __FUNCTION__ << "total_tested : " << total_tested; |
| 342 | + qDebug() << __FUNCTION__ << "count_shouldBeRelocated : " << count_shouldBeRelocated; |
| 343 | + qDebug() << __FUNCTION__ << "count_shouldNotBeRelocated : " << count_shouldNotBeRelocated; |
| 344 | + |
| 345 | + /* test relocatePointInSegment #1 |
| 346 | +
|
| 347 | + { |
| 348 | + QPointF qpf_1 {7.0/4.0, 3.0}; |
| 349 | + S_Segment segment; |
| 350 | + segment._ptA = {0,0}; |
| 351 | + segment._ptB = {4,7}; |
| 352 | +
|
| 353 | + QPointF qpf_relocated {0, 0}; |
| 354 | + if (relocatePointInSegment(qpf_1, segment, qpf_relocated)) { |
| 355 | + qDebug() << __FUNCTION__ << "qpf_relocated = " << qpf_relocated; |
| 356 | + } |
| 357 | + } |
| 358 | +
|
| 359 | + { |
| 360 | + QPointF qpf_1 {1, 3.5}; |
| 361 | + S_Segment segment; |
| 362 | + segment._ptA = {0,0}; |
| 363 | + segment._ptB = {7,0}; |
| 364 | +
|
| 365 | + QPointF qpf_relocated {0, 0}; |
| 366 | + if (relocatePointInSegment(qpf_1, segment, qpf_relocated)) { |
| 367 | + qDebug() << __FUNCTION__ << "qpf_relocated = " << qpf_relocated; |
| 368 | + } |
| 369 | + } |
| 370 | +
|
| 371 | + { |
| 372 | + QPointF qpf_1 {1, 3.5}; |
| 373 | + S_Segment segment; |
| 374 | + segment._ptA = {7,0}; |
| 375 | + segment._ptB = {0,0}; |
| 376 | +
|
| 377 | + QPointF qpf_relocated {0, 0}; |
| 378 | + if (relocatePointInSegment(qpf_1, segment, qpf_relocated)) { |
| 379 | + qDebug() << __FUNCTION__ << "qpf_relocated = " << qpf_relocated; |
| 380 | + } |
| 381 | + } |
| 382 | +
|
| 383 | +
|
| 384 | + { |
| 385 | + QPointF qpf_1 {1, -3.5}; |
| 386 | + S_Segment segment; |
| 387 | + segment._ptA = {7,0}; |
| 388 | + segment._ptB = {-2,-6}; |
| 389 | +
|
| 390 | + QPointF qpf_relocated {0, 0}; |
| 391 | + if (relocatePointInSegment(qpf_1, segment, qpf_relocated)) { |
| 392 | + qDebug() << __FUNCTION__ << "qpf_relocated = " << qpf_relocated; |
| 393 | + } |
| 394 | +
|
| 395 | + }*/ |
| 396 | + |
| 397 | + return(0); |
| 398 | +} |
0 commit comments