main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #include <osgViewer/Viewer>
  2. #include <osg/ShapeDrawable>
  3. #include <osg/Geode>
  4. #include <osgDB/ReadFile>
  5. #include <osg/Group>
  6. #include <osg/Switch>
  7. #include <osg/MatrixTransform>
  8. #include <osg/Matrix>
  9. #include <osgGA/TrackballManipulator>
  10. #include <osgUtil/LineSegmentIntersector>
  11. #include <osgUtil/IntersectionVisitor>
  12. #include <osg/PolygonMode>
  13. #include <vector>
  14. #include <iostream>
  15. #include <fstream>
  16. #include <string>
  17. #include <math.h>
  18. #include "lib3mf_implicit.hpp"
  19. const char* openScadBase =
  20. "$fn = 100;\n"
  21. "module optiTrackPointBase(translation, rotation) {\n"
  22. "translate(translation) rotate(rotation) cylinder(10, 1, 1, false);\n"
  23. "}\n";
  24. class TrackPoint {
  25. public:
  26. TrackPoint(osg::Vec3 point, osg::Vec3 normal);
  27. osg::ref_ptr<osg::MatrixTransform> getUppermostRoot();
  28. osg::Vec3 getTranslation();
  29. osg::Vec3 getRotation();
  30. protected:
  31. osg::ref_ptr<osg::MatrixTransform> _translationGroup;
  32. osg::ref_ptr<osg::MatrixTransform> _rotationGroup;
  33. osg::ref_ptr<osg::MatrixTransform> _originFixGroup;
  34. private:
  35. osg::Vec3 _origin;
  36. osg::Vec3 _normal;
  37. osg::Vec3 _trackOrigin;
  38. };
  39. TrackPoint::TrackPoint(osg::Vec3 point, osg::Vec3 normal) {
  40. osg::ref_ptr<osg::Geode> geode = new osg::Geode;
  41. osg::ref_ptr<osg::ShapeDrawable> cylinder = new osg::ShapeDrawable();
  42. cylinder->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, 0.0f), 1.0f, 10.0f));
  43. cylinder->setColor(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
  44. geode->addDrawable(cylinder.get());
  45. _rotationGroup = new osg::MatrixTransform;
  46. _rotationGroup->addChild(geode.get());
  47. _rotationGroup->setMatrix(osg::Matrix::rotate(osg::Vec3f(0.0f, 0.0f, 1.0f), normal));
  48. _originFixGroup = new osg::MatrixTransform;
  49. _originFixGroup->addChild(_rotationGroup.get());
  50. osg::Vec3f movementVector = normal.operator*(5.0f);
  51. _originFixGroup->setMatrix(osg::Matrix::translate(movementVector));
  52. _translationGroup = new osg::MatrixTransform;
  53. _translationGroup->addChild(_originFixGroup.get());
  54. _translationGroup->setMatrix(osg::Matrix::translate(point));
  55. _origin = point;
  56. _normal = normal;
  57. osg::Vec3 shift = normal.operator*(10.0f);
  58. _trackOrigin = shift.operator+(point);
  59. printf("TrackPoint is at %lf %lf %lf\n", _trackOrigin.x(), _trackOrigin.y(), _trackOrigin.z());
  60. }
  61. osg::ref_ptr<osg::MatrixTransform> TrackPoint::getUppermostRoot() {
  62. return _translationGroup.get();
  63. }
  64. osg::Vec3 TrackPoint::getTranslation() {
  65. return _origin;
  66. }
  67. osg::Vec3 TrackPoint::getRotation() {
  68. printf("YNorm: %lf %lf %lf\n", _normal.x(), _normal.y(), _normal.z());
  69. osg::Vec3 start = osg::Vec3(0.0f, 0.0f, 1.0f);
  70. // From https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
  71. osg::Quat quat = osg::Quat(0.0f, 0.0f, 0.0f, 0.0f);
  72. quat.makeRotate(start, _normal);
  73. float sinr_cosp = 2 * (quat.w() * quat.x() + quat.y() * quat.z());
  74. float cosr_cosp = 1 - 2 * (quat.x() * quat.x() + quat.y() * quat.y());
  75. float xRotation = std::atan2(sinr_cosp, cosr_cosp) * 180.0 / M_PI;
  76. float sinp = 2 * (quat.w() * quat.y() - quat.z() * quat.x());
  77. float yRotation;
  78. if (std::abs(sinp) >= 1) {
  79. yRotation = std::copysign(M_PI / 2, sinp) * 180.0 / M_PI;
  80. } else {
  81. yRotation = std::asin(sinp) * 180.0 / M_PI;
  82. }
  83. float siny_cosp = 2 * (quat.w() * quat.z() + quat.x() * quat.y());
  84. float cosy_cosp = 1 - 2 * (quat.y() * quat.y() + quat.z() * quat.z());
  85. float zRotation = std::atan2(siny_cosp, cosy_cosp) * 180.0 / M_PI;
  86. return osg::Vec3(xRotation, yRotation, zRotation);
  87. }
  88. class ThreeMFWriter {
  89. public:
  90. ThreeMFWriter();
  91. void writeTrackPoints(std::vector<TrackPoint*> points, std::string path);
  92. private:
  93. Lib3MF::PWrapper _wrapper;
  94. };
  95. ThreeMFWriter::ThreeMFWriter() {
  96. _wrapper = Lib3MF::CWrapper::loadLibrary();
  97. }
  98. void ThreeMFWriter::writeTrackPoints(std::vector<TrackPoint*> points, std::string path) {
  99. // Load the file created by OpenSCAD
  100. Lib3MF::PModel model = _wrapper->CreateModel();
  101. Lib3MF::PReader reader = model->QueryReader("3mf");
  102. reader->ReadFromFile("/tmp/output.3mf");
  103. Lib3MF::PMetaDataGroup metaData = model->GetMetaDataGroup();
  104. printf("Having %d MetaData entries\n", metaData->GetMetaDataCount());
  105. Lib3MF::PWriter writer = model->QueryWriter("3mf");
  106. writer->WriteToFile(path);
  107. }
  108. class OpenScadRenderer {
  109. public:
  110. void render(std::vector<TrackPoint*> points);
  111. };
  112. void OpenScadRenderer::render(std::vector<TrackPoint*> points) {
  113. std::ofstream scadFile;
  114. scadFile.open("/tmp/output.scad");
  115. scadFile << openScadBase;
  116. scadFile << "import(\"testbutton.stl\");\n";
  117. for (TrackPoint* point: points) {
  118. osg::Vec3 translation = point->getTranslation();
  119. osg::Vec3 rotation = point->getRotation();
  120. scadFile << "optiTrackPointBase([" << translation.x() << "," << translation.y() << "," << translation.z() << "], [" << rotation.x() << "," << rotation.y() << "," << rotation.z() << "]);\n";
  121. }
  122. scadFile.close();
  123. system("openscad -o /tmp/output.3mf /tmp/output.scad");
  124. }
  125. class StoreHandler {
  126. public:
  127. void addTrackingPoint(osg::Vec3 point, osg::Vec3 normal);
  128. StoreHandler(osg::ref_ptr<osg::Group> root);
  129. std::vector<TrackPoint*> getPoints();
  130. protected:
  131. std::vector<TrackPoint*> points;
  132. private:
  133. osg::ref_ptr<osg::Group> _root;
  134. };
  135. void StoreHandler::addTrackingPoint(osg::Vec3 point, osg::Vec3 normal) {
  136. TrackPoint* trackPoint = new TrackPoint(point, normal);
  137. points.push_back(trackPoint);
  138. _root->addChild(trackPoint->getUppermostRoot());
  139. }
  140. StoreHandler::StoreHandler(osg::ref_ptr<osg::Group> root) {
  141. _root = root;
  142. }
  143. std::vector<TrackPoint*> StoreHandler::getPoints() {
  144. return points;
  145. }
  146. StoreHandler* storeHandler;
  147. OpenScadRenderer* openScadRenderer;
  148. ThreeMFWriter* threeMFWriter;
  149. osg::ref_ptr<osg::Node> axesNode;
  150. class PickHandler: public osgGA::GUIEventHandler {
  151. public:
  152. osg::Node* getOrCreateSelectionCylinder();
  153. virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);
  154. void moveTo(osg::Vec3f position);
  155. void rotateToNormalVector(osg::Vec3f normal);
  156. void setVisibility(bool mode);
  157. protected:
  158. osg::ref_ptr<osg::Switch> _selectionSwitch;
  159. osg::ref_ptr<osg::MatrixTransform> _selectionTranslateGroup;
  160. osg::ref_ptr<osg::MatrixTransform> _selectionRotateGroup;
  161. osg::ref_ptr<osg::MatrixTransform> _selectionMoveToEndGroup;
  162. bool isSelection = false;
  163. };
  164. osg::Node* PickHandler::getOrCreateSelectionCylinder() {
  165. if (!_selectionTranslateGroup) {
  166. osg::ref_ptr<osg::Geode> geode = new osg::Geode;
  167. osg::ref_ptr<osg::ShapeDrawable> cylinder = new osg::ShapeDrawable();
  168. cylinder->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, 0.0f), 1.0f, 10.0f));
  169. cylinder->setColor(osg::Vec4(1.0f, 0.0f, 0.0f, 0.2f));
  170. geode->addDrawable(cylinder.get());
  171. _selectionRotateGroup = new osg::MatrixTransform;
  172. _selectionRotateGroup->addChild(geode.get());
  173. _selectionMoveToEndGroup = new osg::MatrixTransform;
  174. _selectionMoveToEndGroup->addChild(_selectionRotateGroup.get());
  175. _selectionTranslateGroup = new osg::MatrixTransform;
  176. _selectionTranslateGroup->addChild(_selectionMoveToEndGroup.get());
  177. _selectionSwitch = new osg::Switch;
  178. _selectionSwitch->addChild(_selectionTranslateGroup.get());
  179. }
  180. return _selectionSwitch.get();
  181. }
  182. bool PickHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) {
  183. if (ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL) {
  184. isSelection = !isSelection;
  185. setVisibility(false);
  186. }
  187. if (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE && ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON && isSelection) {
  188. osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
  189. if (viewer) {
  190. osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
  191. osgUtil::IntersectionVisitor iv(intersector.get());
  192. iv.setTraversalMask(~0x1);
  193. viewer->getCamera()->accept(iv);
  194. if (intersector->containsIntersections()) {
  195. for (const osgUtil::LineSegmentIntersector::Intersection result: intersector->getIntersections()) {
  196. if (std::find(result.nodePath.begin(), result.nodePath.end(), axesNode) != result.nodePath.end()) {
  197. moveTo(result.localIntersectionPoint);
  198. rotateToNormalVector(result.localIntersectionNormal);
  199. storeHandler->addTrackingPoint(result.localIntersectionPoint, result.localIntersectionNormal);
  200. openScadRenderer->render(storeHandler->getPoints());
  201. threeMFWriter->writeTrackPoints(storeHandler->getPoints(), "/tmp/export.3mf");
  202. break;
  203. }
  204. }
  205. }
  206. }
  207. }
  208. if (ea.getEventType() == osgGA::GUIEventAdapter::MOVE && isSelection) {
  209. osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
  210. if (viewer) {
  211. osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
  212. osgUtil::IntersectionVisitor iv(intersector.get());
  213. iv.setTraversalMask(~0x1);
  214. viewer->getCamera()->accept(iv);
  215. if (intersector->containsIntersections()) {
  216. for (const osgUtil::LineSegmentIntersector::Intersection result: intersector->getIntersections()) {
  217. if (std::find(result.nodePath.begin(), result.nodePath.end(), axesNode) != result.nodePath.end()) {
  218. moveTo(result.localIntersectionPoint);
  219. rotateToNormalVector(result.localIntersectionNormal);
  220. setVisibility(true);
  221. break;
  222. }
  223. }
  224. } else {
  225. setVisibility(false);
  226. }
  227. }
  228. }
  229. return false;
  230. }
  231. void PickHandler::moveTo(osg::Vec3f position) {
  232. _selectionTranslateGroup->setMatrix(osg::Matrix::translate(position));
  233. }
  234. void PickHandler::rotateToNormalVector(osg::Vec3f normal) {
  235. _selectionRotateGroup->setMatrix(osg::Matrix::rotate(osg::Vec3f(0.0f, 0.0f, 1.0f), normal));
  236. osg::Vec3f movementVector = normal.operator*(5.0f);
  237. _selectionMoveToEndGroup->setMatrix(osg::Matrix::translate(movementVector));
  238. }
  239. void PickHandler::setVisibility(bool mode) {
  240. _selectionSwitch->setValue(0, mode);
  241. }
  242. int main(int argc, char** argv) {
  243. // Root node of the scene
  244. osg::ref_ptr<osg::Group> root = new osg::Group;
  245. // Create the viewer
  246. osgViewer::Viewer viewer;
  247. viewer.setSceneData(root);
  248. viewer.realize();
  249. // Add axesNode under root
  250. axesNode = osgDB::readNodeFile("../../testdata/testbutton.stl");
  251. if (!axesNode) {
  252. printf("Origin node not loaded, model not found\n");
  253. return 1;
  254. }
  255. root->addChild(axesNode);
  256. // Attach a manipulator (it's usually done for us when we use viewer.run())
  257. osg::ref_ptr<osgGA::TrackballManipulator> tm = new osgGA::TrackballManipulator;
  258. viewer.setCameraManipulator(tm);
  259. storeHandler = new StoreHandler(root);
  260. openScadRenderer = new OpenScadRenderer();
  261. threeMFWriter = new ThreeMFWriter();
  262. osg::ref_ptr<PickHandler> picker = new PickHandler();
  263. root->addChild(picker->getOrCreateSelectionCylinder());
  264. viewer.addEventHandler(picker.get());
  265. return viewer.run();
  266. }