OpenScadRenderer.cpp 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Include own headers
  2. #include "OpenScadRenderer.hpp"
  3. // Include modules
  4. #include "PlatformSupport.hpp"
  5. #include "scad.hpp"
  6. // Include dependencies
  7. #include <iostream>
  8. #include <fstream>
  9. #include <filesystem>
  10. #include <cstdio>
  11. #include <memory>
  12. #include <stdexcept>
  13. #include <string>
  14. #include <array>
  15. const char* openScadBase =
  16. "$fn = 100;\n"
  17. "use <threads.scad>\n"
  18. "module optiTrackPointBase(translation, rotation, length, radius) {\n"
  19. "translate(translation) rotate(rotation) cylinder(length, radius, radius, false);\n"
  20. "}\n"
  21. "module emfTrackPointBase(translation, rotation, width, height, depth) {\n"
  22. "translate(translation) rotate(rotation) {\n"
  23. "translate([-(width / 2), -(height / 2), 0]) cube([width, height, depth]);"
  24. "}\n"
  25. "}\n"
  26. "module steamVrTrackPointBase(translation, rotation, length) {\n"
  27. "translate(translation) rotate(rotation) {\n"
  28. "cylinder(length, 3.5, 3.5, false);\n"
  29. "translate([0, 0, length]) english_thread(diameter=1/4, threads_per_inch=20, length=0.393701);\n"
  30. "}\n"
  31. "}\n";
  32. bool OpenScadRenderer::openScadAvailable() {
  33. std::string path = openScadPath + " -v";
  34. int result = system(path.c_str());
  35. return result == 0;
  36. }
  37. void OpenScadRenderer::renderOptiTrack(std::vector<OptiTrackPoint*> points) {
  38. std::ofstream scadFile;
  39. scadFile.open(std::filesystem::temp_directory_path().u8string() + fileDelimiter + "trackpointapp_export_optitrack.scad");
  40. scadFile << openScadBase;
  41. scadFile << "import(\"trackpointapp_export.3mf\");\n";
  42. for (OptiTrackPoint* point: points) {
  43. osg::Vec3 translation = point->getTranslation();
  44. osg::Vec3 rotation = point->getRotation();
  45. scadFile << "optiTrackPointBase([" << translation.x() << "," << translation.y() << "," << translation.z() << "], [" << rotation.x() << "," << rotation.y() << "," << rotation.z() << "], " << point->getLength() << ", " << point->getRadius() << ");\n";
  46. }
  47. scadFile.close();
  48. std::string command = openScadPath + " -o " + std::filesystem::temp_directory_path().u8string() + fileDelimiter + "trackpointapp_render_optitrack.3mf " + std::filesystem::temp_directory_path().u8string() + fileDelimiter + "trackpointapp_export_optitrack.scad";
  49. system(command.c_str());
  50. }
  51. void OpenScadRenderer::renderEMFTrack(std::vector<EMFTrackPoint*> points) {
  52. std::ofstream scadFile;
  53. scadFile.open(std::filesystem::temp_directory_path().u8string() + fileDelimiter + "trackpointapp_export_emftrack.scad");
  54. scadFile << openScadBase;
  55. scadFile << "difference() {\nimport(\"trackpointapp_export.3mf\");\n";
  56. for (EMFTrackPoint* point: points) {
  57. osg::Vec3 translation = point->getTranslation();
  58. osg::Vec3 rotation = point->getRotation();
  59. scadFile << "emfTrackPointBase([" << translation.x() << "," << translation.y() << "," << translation.z() << "], [" << rotation.x() << "," << rotation.y() << "," << rotation.z() << "], " << point->getWidth() << ", " << point->getHeight() << ", " << point->getDepth() << ");\n";
  60. }
  61. scadFile << "}\n";
  62. scadFile.close();
  63. std::string command = openScadPath + " -o " + std::filesystem::temp_directory_path().u8string() + fileDelimiter + "trackpointapp_render_emftrack.3mf " + std::filesystem::temp_directory_path().u8string() + fileDelimiter + "trackpointapp_export_emftrack.scad";
  64. system(command.c_str());
  65. }
  66. void OpenScadRenderer::renderSteamVRTrack(std::vector<SteamVRTrackPoint*> points) {
  67. enableSteamvrThread();
  68. std::ofstream scadFile;
  69. scadFile.open(std::filesystem::temp_directory_path().u8string() + fileDelimiter + "trackpointapp_export_steamvrtrack.scad");
  70. scadFile << openScadBase;
  71. scadFile << "import(\"trackpointapp_export.3mf\");\n";
  72. for (SteamVRTrackPoint* point: points) {
  73. osg::Vec3 translation = point->getTranslation();
  74. osg::Vec3 rotation = point->getRotation();
  75. scadFile << "steamVrTrackPointBase([" << translation.x() << "," << translation.y() << "," << translation.z() << "], [" << rotation.x() << "," << rotation.y() << "," << rotation.z() << "], " << point->getLength() << ");\n";
  76. }
  77. scadFile.close();
  78. std::string command = openScadPath + " -o " + std::filesystem::temp_directory_path().u8string() + fileDelimiter + "trackpointapp_render_steamvrtrack.3mf " + std::filesystem::temp_directory_path().u8string() + fileDelimiter + "trackpointapp_export_steamvrtrack.scad";
  79. system(command.c_str());
  80. }
  81. void OpenScadRenderer::enableSteamvrThread() {
  82. std::ofstream resourceFile;
  83. resourceFile.open(std::filesystem::temp_directory_path().u8string() + fileDelimiter + "threads.scad");
  84. resourceFile << threads_SCAD;
  85. resourceFile.close();
  86. }