File size: 21,528 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 | #include <Base/Builder3D.h>
#include <Inventor/SoDB.h>
#include <Inventor/SoInput.h>
#include <Inventor/nodes/SoSeparator.h>
#include <QDebug>
#include <QTest>
#include <sstream>
Q_DECLARE_METATYPE(Base::Vector3f)
Q_DECLARE_METATYPE(Base::ColorRGB)
Q_DECLARE_METATYPE(Base::Line3f)
Q_DECLARE_METATYPE(Base::BindingElement::Binding)
Q_DECLARE_METATYPE(Base::DrawStyle)
Q_DECLARE_METATYPE(Base::DrawStyle::Style)
Q_DECLARE_METATYPE(Base::PolygonOffset)
Q_DECLARE_METATYPE(Base::PolygonOffset::Style)
class testInventorBuilder: public QObject
{
Q_OBJECT
public:
testInventorBuilder()
: builder(output)
{}
~testInventorBuilder() override = default;
SoNode* loadBuffer(const std::string& buffer)
{
SoInput in;
in.setBuffer((void*)buffer.c_str(), buffer.size());
return SoDB::readAll(&in);
}
private Q_SLOTS:
void initTestCase()
{
SoDB::init();
}
void initTestCase_data()
{}
void cleanupTestCase()
{
SoDB::finish();
}
void init()
{}
void cleanup()
{
// clear the buffer
output.str(std::string());
}
void test_Invalid()
{
SoNode* node = loadBuffer("Hello, World");
QCOMPARE(node, nullptr);
}
void test_Output()
{
std::stringstream str;
Base::InventorBuilder builder(str);
QCOMPARE(str.str().c_str(), "#Inventor V2.1 ascii \n\n");
}
void test_MaterialBinding_data()
{
// clang-format off
QTest::addColumn<Base::BindingElement::Binding>("input");
QTest::addColumn<QString>("result");
QTest::newRow("MaterialBinding") << Base::BindingElement::Binding::Overall
<< "MaterialBinding { value OVERALL } \n";
QTest::newRow("MaterialBinding") << Base::BindingElement::Binding::PerPart
<< "MaterialBinding { value PER_PART } \n";
QTest::newRow("MaterialBinding") << Base::BindingElement::Binding::PerPartIndexed
<< "MaterialBinding { value PER_PART_INDEXED } \n";
QTest::newRow("MaterialBinding") << Base::BindingElement::Binding::PerFace
<< "MaterialBinding { value PER_FACE } \n";
QTest::newRow("MaterialBinding") << Base::BindingElement::Binding::PerFaceIndexed
<< "MaterialBinding { value PER_FACE_INDEXED } \n";
QTest::newRow("MaterialBinding") << Base::BindingElement::Binding::PerVertex
<< "MaterialBinding { value PER_VERTEX } \n";
QTest::newRow("MaterialBinding") << Base::BindingElement::Binding::PerVertexIndexed
<< "MaterialBinding { value PER_VERTEX_INDEXED } \n";
QTest::newRow("MaterialBinding") << Base::BindingElement::Binding::Default
<< "MaterialBinding { value OVERALL } \n";
QTest::newRow("MaterialBinding") << Base::BindingElement::Binding::None
<< "MaterialBinding { value OVERALL } \n";
// clang-format on
}
void test_MaterialBinding()
{
QFETCH(Base::BindingElement::Binding, input);
QFETCH(QString, result);
Base::MaterialBindingItem item;
item.setValue(input);
builder.addNode(item);
QString string = QString::fromStdString(output.str());
QCOMPARE(string, result);
}
void test_Label_data()
{
auto result =
R"(Label {
label "FreeCAD"
}
)";
QTest::addColumn<QString>("input");
QTest::addColumn<QString>("result");
QTest::newRow("Label") << "FreeCAD" << result;
}
void test_Label()
{
QFETCH(QString, input);
QFETCH(QString, result);
Base::LabelItem item {input.toStdString()};
builder.addNode(item);
QString string = QString::fromStdString(output.str());
QCOMPARE(string, result);
}
void test_Info_data()
{
auto result =
R"(Info {
string "FreeCAD"
}
)";
QTest::addColumn<QString>("input");
QTest::addColumn<QString>("result");
QTest::newRow("Info") << "FreeCAD" << result;
}
void test_Info()
{
QFETCH(QString, input);
QFETCH(QString, result);
Base::InfoItem item {input.toStdString()};
builder.addNode(item);
QString string = QString::fromStdString(output.str());
QCOMPARE(string, result);
}
void test_Text2_data()
{
auto result = "Text2 { string \"FreeCAD\" }\n";
QTest::addColumn<QString>("input");
QTest::addColumn<QString>("result");
QTest::newRow("Text2") << "FreeCAD" << result;
}
void test_Text2()
{
QFETCH(QString, input);
QFETCH(QString, result);
Base::Text2Item item {input.toStdString()};
builder.addNode(item);
QString string = QString::fromStdString(output.str());
QCOMPARE(string, result);
}
void test_BaseColor_data()
{
auto result =
R"(BaseColor {
rgb 0.21 0.3 0.4
}
)";
QTest::addColumn<Base::ColorRGB>("input");
QTest::addColumn<QString>("result");
QTest::newRow("BaseColor") << Base::ColorRGB {0.21F, 0.3F, 0.4F} << result;
}
void test_BaseColor()
{
QFETCH(Base::ColorRGB, input);
QFETCH(QString, result);
Base::BaseColorItem item {input};
builder.addNode(item);
QString string = QString::fromStdString(output.str());
QCOMPARE(string, result);
}
void test_Material_data()
{
auto result =
R"(Material {
diffuseColor 1 0 0
}
)";
QTest::addColumn<Base::ColorRGB>("input");
QTest::addColumn<QString>("result");
QTest::newRow("Material") << Base::ColorRGB {1, 0, 0} << result;
}
void test_Material()
{
QFETCH(Base::ColorRGB, input);
QFETCH(QString, result);
Base::MaterialItem item;
item.setDiffuseColor({input});
builder.addNode(item);
QString string = QString::fromStdString(output.str());
QCOMPARE(string, result);
}
void test_Materials_data()
{
auto result =
R"(Material {
diffuseColor [
1 0 0
0 1 0
0 0 1
]
}
)";
QTest::addColumn<Base::ColorRGB>("input1");
QTest::addColumn<Base::ColorRGB>("input2");
QTest::addColumn<Base::ColorRGB>("input3");
QTest::addColumn<QString>("result");
QTest::newRow("Material") << Base::ColorRGB {1, 0, 0} << Base::ColorRGB {0, 1, 0}
<< Base::ColorRGB {0, 0, 1} << result;
}
void test_Materials()
{
QFETCH(Base::ColorRGB, input1);
QFETCH(Base::ColorRGB, input2);
QFETCH(Base::ColorRGB, input3);
QFETCH(QString, result);
Base::MaterialItem item;
item.setDiffuseColor({input1, input2, input3});
builder.addNode(item);
QString string = QString::fromStdString(output.str());
QCOMPARE(string, result);
SoNode* node = loadBuffer(output.str());
QVERIFY(node != nullptr);
QVERIFY(node->getRefCount() == 0);
}
void test_DrawStyle_data()
{
auto result =
R"(DrawStyle {
style FILLED
pointSize 3
lineWidth 3
linePattern 0xf0f0
}
)";
QTest::addColumn<Base::DrawStyle::Style>("styleEnum");
QTest::addColumn<ushort>("pointSize");
QTest::addColumn<ushort>("lineWidth");
QTest::addColumn<ushort>("linePattern");
QTest::addColumn<QString>("result");
QTest::newRow("DrawStyle")
<< Base::DrawStyle::Style::Filled << ushort(3) << ushort(3) << ushort(0xf0f0) << result;
}
void test_DrawStyle()
{
QFETCH(Base::DrawStyle::Style, styleEnum);
QFETCH(ushort, pointSize);
QFETCH(ushort, lineWidth);
QFETCH(ushort, linePattern);
QFETCH(QString, result);
Base::DrawStyleItem item;
Base::DrawStyle style;
style.style = styleEnum;
style.pointSize = pointSize;
style.lineWidth = lineWidth;
style.linePattern = linePattern;
item.setValue(style);
builder.addNode(item);
QString string = QString::fromStdString(output.str());
QCOMPARE(string, result);
}
void test_ShapeHints_data()
{
auto result =
R"(ShapeHints {
creaseAngle 0.5
vertexOrdering UNKNOWN_ORDERING
shapeType UNKNOWN_SHAPE_TYPE
}
)";
QTest::addColumn<float>("input");
QTest::addColumn<QString>("result");
QTest::newRow("ShapeHints") << 0.5F << result;
}
void test_ShapeHints()
{
QFETCH(float, input);
QFETCH(QString, result);
Base::ShapeHintsItem item {input};
builder.addNode(item);
QString string = QString::fromStdString(output.str());
QCOMPARE(string, result);
}
void test_PolygonOffset_data()
{
auto result =
R"(PolygonOffset {
factor 2
units 1
styles FILLED
on FALSE
}
)";
QTest::addColumn<Base::PolygonOffset::Style>("styleEnum");
QTest::addColumn<float>("factor");
QTest::addColumn<float>("units");
QTest::addColumn<bool>("on");
QTest::addColumn<QString>("result");
QTest::newRow("PolygonOffset")
<< Base::PolygonOffset::Style::Filled << 2.0F << 1.0F << false << result;
}
void test_PolygonOffset()
{
QFETCH(Base::PolygonOffset::Style, styleEnum);
QFETCH(float, factor);
QFETCH(float, units);
QFETCH(bool, on);
QFETCH(QString, result);
Base::PolygonOffsetItem item;
Base::PolygonOffset offset;
offset.factor = factor;
offset.units = units;
offset.style = styleEnum;
offset.on = on;
item.setValue(offset);
builder.addNode(item);
QString string = QString::fromStdString(output.str());
QCOMPARE(string, result);
}
void test_PointSet_data()
{
auto result = "PointSet { }\n";
QTest::addColumn<QString>("result");
QTest::newRow("PointSet") << result;
}
void test_PointSet()
{
QFETCH(QString, result);
Base::PointSetItem item;
builder.addNode(item);
QString string = QString::fromStdString(output.str());
QCOMPARE(string, result);
}
void test_LineSet_data()
{
auto result = "LineSet { }\n";
QTest::addColumn<QString>("result");
QTest::newRow("LineSet") << result;
}
void test_LineSet()
{
QFETCH(QString, result);
Base::LineSetItem item;
builder.addNode(item);
QString string = QString::fromStdString(output.str());
QCOMPARE(string, result);
}
void test_FaceSet_data()
{
QTest::addColumn<int>("num");
QTest::newRow("FaceSet") << 2;
}
void test_FaceSet()
{
QFETCH(int, num);
Base::FaceSetItem item {{num}};
builder.addNode(item);
SoNode* node = loadBuffer(output.str());
QVERIFY(node != nullptr);
}
void test_IndexedLineSet_data()
{
QTest::addColumn<int>("c1");
QTest::addColumn<int>("c2");
QTest::addColumn<int>("c3");
QTest::newRow("IndexedLineSet") << 0 << 1 << 2;
}
void test_IndexedLineSet()
{
QFETCH(int, c1);
QFETCH(int, c2);
QFETCH(int, c3);
Base::IndexedLineSetItem item {{c1, c2, c3}};
builder.addNode(item);
SoNode* node = loadBuffer(output.str());
QVERIFY(node != nullptr);
}
void test_IndexedFaceSet_data()
{
QTest::addColumn<int>("c1");
QTest::addColumn<int>("c2");
QTest::addColumn<int>("c3");
QTest::addColumn<int>("c4");
QTest::newRow("IndexedFaceSet") << 0 << 1 << 2 << -1;
}
void test_IndexedFaceSet()
{
QFETCH(int, c1);
QFETCH(int, c2);
QFETCH(int, c3);
QFETCH(int, c4);
Base::IndexedFaceSetItem item {{c1, c2, c3, c4}};
builder.addNode(item);
SoNode* node = loadBuffer(output.str());
QVERIFY(node != nullptr);
}
void test_Transform()
{
Base::Placement plm;
Base::TransformItem item {plm};
builder.addNode(item);
SoNode* node = loadBuffer(output.str());
QVERIFY(node != nullptr);
}
void test_Normal_data()
{
auto result =
R"(Normal {
vector 1 0 0.5
}
)";
QTest::addColumn<Base::Vector3f>("input");
QTest::addColumn<QString>("result");
QTest::newRow("Normal") << Base::Vector3f {1, 0, 0.5} << result;
}
void test_Normal()
{
QFETCH(Base::Vector3f, input);
QFETCH(QString, result);
builder.addNode(Base::NormalItem {{input}});
QString string = QString::fromStdString(output.str());
QCOMPARE(string, result);
}
void test_LineItem_data()
{
QTest::addColumn<Base::Line3f>("line");
QTest::addColumn<Base::DrawStyle>("style");
QTest::newRow("LineItem") << Base::Line3f {} << Base::DrawStyle {};
}
void test_LineItem()
{
QFETCH(Base::Line3f, line);
QFETCH(Base::DrawStyle, style);
Base::LineItem item {line, style};
builder.addNode(item);
SoNode* node = loadBuffer(output.str());
QVERIFY(node != nullptr);
}
void test_MultiLineItem_data()
{
QTest::addColumn<Base::Vector3f>("p1");
QTest::addColumn<Base::Vector3f>("p2");
QTest::addColumn<Base::Vector3f>("p3");
QTest::addColumn<Base::DrawStyle>("style");
QTest::newRow("MultiLineItem") << Base::Vector3f {0, 0, 0} << Base::Vector3f {1, 0, 0}
<< Base::Vector3f {1, 1, 0} << Base::DrawStyle {};
}
void test_MultiLineItem()
{
QFETCH(Base::Vector3f, p1);
QFETCH(Base::Vector3f, p2);
QFETCH(Base::Vector3f, p3);
QFETCH(Base::DrawStyle, style);
Base::MultiLineItem item {{p1, p2, p3}, style};
builder.addNode(item);
SoNode* node = loadBuffer(output.str());
QVERIFY(node != nullptr);
}
void test_ArrowItem_data()
{
QTest::addColumn<Base::Line3f>("line");
QTest::addColumn<Base::DrawStyle>("style");
QTest::newRow("Arrow") << Base::Line3f {Base::Vector3f {0, 0, 10}, Base::Vector3f {}}
<< Base::DrawStyle {};
}
void test_ArrowItem()
{
QFETCH(Base::Line3f, line);
QFETCH(Base::DrawStyle, style);
Base::ArrowItem item {line, style};
builder.addNode(item);
SoNode* node = loadBuffer(output.str());
QVERIFY(node != nullptr);
}
void test_BoundingBoxItem_data()
{
QTest::addColumn<Base::Vector3f>("p1");
QTest::addColumn<Base::Vector3f>("p2");
QTest::addColumn<Base::DrawStyle>("style");
QTest::newRow("BoundingBoxItem")
<< Base::Vector3f {0, 0, 0} << Base::Vector3f {1, 1, 1} << Base::DrawStyle {};
}
void test_BoundingBoxItem()
{
QFETCH(Base::Vector3f, p1);
QFETCH(Base::Vector3f, p2);
QFETCH(Base::DrawStyle, style);
Base::BoundingBoxItem item {p1, p2, style};
builder.addNode(item);
SoNode* node = loadBuffer(output.str());
QVERIFY(node != nullptr);
}
void test_Coordinate3Item_data()
{
QTest::addColumn<Base::Vector3f>("p1");
QTest::addColumn<Base::Vector3f>("p2");
QTest::addColumn<Base::Vector3f>("p3");
QTest::newRow("Coordinate3Item")
<< Base::Vector3f {0, 0, 0} << Base::Vector3f {1, 0, 0} << Base::Vector3f {1, 1, 0};
}
void test_Coordinate3Item()
{
QFETCH(Base::Vector3f, p1);
QFETCH(Base::Vector3f, p2);
QFETCH(Base::Vector3f, p3);
Base::Coordinate3Item item {{p1, p2, p3}};
builder.addNode(item);
SoNode* node = loadBuffer(output.str());
QVERIFY(node != nullptr);
}
void test_PointItem_data()
{
QTest::addColumn<Base::Vector3f>("point");
QTest::addColumn<Base::DrawStyle>("style");
QTest::newRow("PointItem") << Base::Vector3f {} << Base::DrawStyle {};
}
void test_PointItem()
{
QFETCH(Base::Vector3f, point);
QFETCH(Base::DrawStyle, style);
Base::PointItem item {point, style};
builder.addNode(item);
SoNode* node = loadBuffer(output.str());
QVERIFY(node != nullptr);
}
void test_NormalBinding_data()
{
// clang-format off
QTest::addColumn<Base::BindingElement::Binding>("input");
QTest::addColumn<QString>("result");
QTest::newRow("NormalBinding") << Base::BindingElement::Binding::Overall
<< "NormalBinding { value OVERALL }\n";
QTest::newRow("NormalBinding") << Base::BindingElement::Binding::PerPart
<< "NormalBinding { value PER_PART }\n";
QTest::newRow("NormalBinding") << Base::BindingElement::Binding::PerPartIndexed
<< "NormalBinding { value PER_PART_INDEXED }\n";
QTest::newRow("NormalBinding") << Base::BindingElement::Binding::PerFace
<< "NormalBinding { value PER_FACE }\n";
QTest::newRow("NormalBinding") << Base::BindingElement::Binding::PerFaceIndexed
<< "NormalBinding { value PER_FACE_INDEXED }\n";
QTest::newRow("NormalBinding") << Base::BindingElement::Binding::PerVertex
<< "NormalBinding { value PER_VERTEX }\n";
QTest::newRow("NormalBinding") << Base::BindingElement::Binding::PerVertexIndexed
<< "NormalBinding { value PER_VERTEX_INDEXED }\n";
QTest::newRow("NormalBinding") << Base::BindingElement::Binding::Default
<< "NormalBinding { value OVERALL }\n";
QTest::newRow("NormalBinding") << Base::BindingElement::Binding::None
<< "NormalBinding { value OVERALL }\n";
// clang-format on
}
void test_NormalBinding()
{
QFETCH(Base::BindingElement::Binding, input);
QFETCH(QString, result);
Base::NormalBindingItem item;
item.setValue(input);
builder.addNode(item);
QString string = QString::fromStdString(output.str());
QCOMPARE(string, result);
}
void test_Cylinder_data()
{
auto result =
R"(Cylinder {
radius 3
height 7
parts (SIDES | TOP | BOTTOM)
}
)";
QTest::addColumn<float>("radius");
QTest::addColumn<float>("height");
QTest::addColumn<QString>("result");
QTest::newRow("Cylinder") << 3.0F << 7.0F << result;
}
void test_Cylinder()
{
QFETCH(float, radius);
QFETCH(float, height);
QFETCH(QString, result);
Base::CylinderItem item;
item.setRadius(radius);
item.setHeight(height);
builder.addNode(item);
QString string = QString::fromStdString(output.str());
QCOMPARE(string, result);
}
void test_Cone_data()
{
auto result = "Cone { bottomRadius 2 height 10 }\n";
QTest::addColumn<float>("radius");
QTest::addColumn<float>("height");
QTest::addColumn<QString>("result");
QTest::newRow("Cone") << 2.0F << 10.0F << result;
}
void test_Cone()
{
QFETCH(float, radius);
QFETCH(float, height);
QFETCH(QString, result);
Base::ConeItem item;
item.setBottomRadius(radius);
item.setHeight(height);
builder.addNode(item);
QString string = QString::fromStdString(output.str());
QCOMPARE(string, result);
}
void test_Sphere_data()
{
auto result = "Sphere { radius 4 }\n";
QTest::addColumn<float>("input");
QTest::addColumn<QString>("result");
QTest::newRow("Sphere") << 4.0F << result;
}
void test_Sphere()
{
QFETCH(float, input);
QFETCH(QString, result);
Base::SphereItem item;
item.setRadius(input);
builder.addNode(item);
QString string = QString::fromStdString(output.str());
QCOMPARE(string, result);
}
void test_NurbsSurface_data()
{
QTest::addColumn<float>("knot1");
QTest::addColumn<float>("knot2");
QTest::addColumn<int>("poles");
QTest::newRow("Nurbs") << 0.0F << 1.0F << 2;
}
void test_NurbsSurface()
{
QFETCH(float, knot1);
QFETCH(float, knot2);
QFETCH(int, poles);
Base::NurbsSurfaceItem item;
item.setKnotVector({knot1, knot2}, {knot1, knot2});
item.setControlPoints(poles, poles);
builder.addNode(item);
SoNode* node = loadBuffer(output.str());
QVERIFY(node != nullptr);
}
private:
std::stringstream output;
Base::InventorBuilder builder;
};
QTEST_GUILESS_MAIN(testInventorBuilder)
#include "InventorBuilder.moc"
|