image.png

    1. #include <CGAL/Linear_cell_complex_for_combinatorial_map.h>
    2. #include <CGAL/Linear_cell_complex_constructors.h>
    3. #include <CGAL/Timer.h>
    4. #include <iostream>
    5. #include <fstream>
    6. #ifdef _DEBUG
    7. #include <CGAL/draw_linear_cell_complex.h>
    8. #endif // _DEBUG
    9. typedef CGAL::Linear_cell_complex_for_combinatorial_map<2, 3> LCC_3;
    10. typedef LCC_3::Dart_handle Dart_handle;
    11. typedef LCC_3::Point Point;
    12. typedef LCC_3::FT FT;
    13. //加载并简化OFF
    14. void load_and_simplify_off(LCC_3& lcc, const std::string& filename,
    15. bool updateattribs, int percent)
    16. {
    17. std::ifstream ifile(filename.c_str());
    18. if (ifile)
    19. {
    20. //加载OFF文件
    21. CGAL::load_off(lcc, ifile);
    22. CGAL::Timer timer;
    23. Dart_handle dh;
    24. std::size_t nb = (lcc.number_of_darts()*percent) / 200;
    25. timer.start();
    26. //updateattribs为false => 不更新属性 => 自动属性管理关闭
    27. if (!updateattribs) lcc.set_automatic_attributes_management(false);
    28. //?
    29. for (LCC_3::Dart_range::iterator it = lcc.darts().begin(),
    30. itend = lcc.darts().end(); it != itend && nb > 0; )
    31. {
    32. dh = it++;
    33. if (it != itend && it == lcc.beta<2>(dh)) ++it;
    34. lcc.remove_cell<1>(dh);
    35. --nb;
    36. }
    37. //updateattribs为false => 不更新属性 => 自动属性管理开启
    38. if (!updateattribs) lcc.set_automatic_attributes_management(true);
    39. timer.stop();
    40. //输出线性单元复合体的特征
    41. lcc.display_characteristics(std::cout);
    42. std::cout << ", valid=" << lcc.is_valid()
    43. << " time: " << timer.time() << " seconds." << std::endl;
    44. }
    45. }
    46. int main(int narg, char** argv)
    47. {
    48. if (narg > 1 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "-?")))
    49. {
    50. std::cout << "Usage: a.out file.off [percentage]" << std::endl;
    51. return EXIT_FAILURE;
    52. }
    53. std::string filename;
    54. if (narg == 1)
    55. {
    56. filename = std::string("data/armadillo.off");
    57. std::cout << "No filename given: use data/armadillo.off by default." << std::endl;
    58. }
    59. else filename = std::string(argv[1]);
    60. int percent = 30; // remove 30 percent of edges
    61. if (narg > 2) { percent = atoi(argv[2]); }
    62. std::cout << percent << "% edges to remove." << std::endl;
    63. LCC_3 lcc;
    64. std::cout << "Update attribute DURING operations: ";
    65. load_and_simplify_off(lcc, filename, true, percent);
    66. LCC_3 lcc2;
    67. std::cout << "Update attribute AFTER operations: ";
    68. load_and_simplify_off(lcc2, filename, false, percent);
    69. #ifdef _DEBUG
    70. CGAL::draw(lcc);
    71. #endif // _DEBUG
    72. return EXIT_SUCCESS;
    73. }