OpenScadRenderer.cpp 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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, compensation) {\n"
  19. "translate(translation) rotate(rotation) {\n"
  20. "translate([0, 0, -compensation]) cylinder(length + compensation - 5, radius, radius, false);\n"
  21. "translate([0, 0, length - 5]) cylinder(5, 0.74, 0.74, false);\n"
  22. "}\n"
  23. "}\n"
  24. "module emfTrackPointBase(translation, rotation, width, height, depth, compensation) {\n"
  25. "translate(translation) rotate(rotation) {\n"
  26. "translate([-(width / 2), -(height / 2), -compensation]) cube([width, height, depth + compensation]);"
  27. "}\n"
  28. "}\n"
  29. "module steamVrTrackPointBase(translation, rotation, length, compensation) {\n"
  30. "translate(translation) rotate(rotation) {\n"
  31. "translate([0, 0, -compensation]) cylinder(length + compensation, 3.5, 3.5, false);\n"
  32. "translate([0, 0, length]) english_thread(diameter=1/4, threads_per_inch=20, length=0.393701);\n"
  33. "}\n"
  34. "}\n";
  35. bool OpenScadRenderer::openScadAvailable() {
  36. std::string path = openScadPath + " -v";
  37. int result = system(path.c_str());
  38. return result == 0;
  39. }
  40. void OpenScadRenderer::renderOptiTrack(std::vector<OptiTrackPoint*> points) {
  41. std::ofstream scadFile;
  42. scadFile.open(std::filesystem::temp_directory_path().u8string() + fileDelimiter + "trackpointapp_export_optitrack.scad");
  43. scadFile << openScadBase;
  44. scadFile << "import(\"trackpointapp_export.3mf\");\n";
  45. for (OptiTrackPoint* point: points) {
  46. osg::Vec3 translation = point->getTranslation();
  47. osg::Vec3 rotation = point->getRotation();
  48. scadFile << "optiTrackPointBase([" << translation.x() << "," << translation.y() << "," << translation.z() << "], [" << rotation.x() << "," << rotation.y() << "," << rotation.z() << "], " << point->getLength() << ", " << point->getRadius() << ", " << point->getNormalCompensation() << ");\n";
  49. }
  50. scadFile.close();
  51. 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";
  52. system(command.c_str());
  53. }
  54. void OpenScadRenderer::renderEMFTrack(std::vector<EMFTrackPoint*> points) {
  55. std::ofstream scadFile;
  56. scadFile.open(std::filesystem::temp_directory_path().u8string() + fileDelimiter + "trackpointapp_export_emftrack.scad");
  57. scadFile << openScadBase;
  58. scadFile << "difference() {\nimport(\"trackpointapp_export.3mf\");\n";
  59. for (EMFTrackPoint* point: points) {
  60. osg::Vec3 translation = point->getTranslation();
  61. osg::Vec3 rotation = point->getRotation();
  62. scadFile << "emfTrackPointBase([" << translation.x() << "," << translation.y() << "," << translation.z() << "], [" << rotation.x() << "," << rotation.y() << "," << rotation.z() << "], " << point->getWidth() << ", " << point->getHeight() << ", " << point->getDepth() << ", " << point->getNormalCompensation() << ");\n";
  63. }
  64. scadFile << "}\n";
  65. scadFile.close();
  66. 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";
  67. system(command.c_str());
  68. }
  69. void OpenScadRenderer::renderSteamVRTrack(std::vector<SteamVRTrackPoint*> points) {
  70. enableSteamvrThread();
  71. std::ofstream scadFile;
  72. scadFile.open(std::filesystem::temp_directory_path().u8string() + fileDelimiter + "trackpointapp_export_steamvrtrack.scad");
  73. scadFile << openScadBase;
  74. scadFile << "import(\"trackpointapp_export.3mf\");\n";
  75. for (SteamVRTrackPoint* point: points) {
  76. osg::Vec3 translation = point->getTranslation();
  77. osg::Vec3 rotation = point->getRotation();
  78. scadFile << "steamVrTrackPointBase([" << translation.x() << "," << translation.y() << "," << translation.z() << "], [" << rotation.x() << "," << rotation.y() << "," << rotation.z() << "], " << point->getLength() << ", " << point->getNormalCompensation() << ");\n";
  79. }
  80. scadFile.close();
  81. 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";
  82. system(command.c_str());
  83. }
  84. void OpenScadRenderer::enableSteamvrThread() {
  85. std::ofstream resourceFile;
  86. resourceFile.open(std::filesystem::temp_directory_path().u8string() + fileDelimiter + "threads.scad");
  87. resourceFile << threads_SCAD;
  88. resourceFile.close();
  89. }