transforms.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include <osgViewer/Viewer>
  2. #include <osg/ShapeDrawable>
  3. #include <osg/Geode>
  4. #include <osgDB/ReadFile>
  5. #include <osg/Group>
  6. #include <osg/MatrixTransform>
  7. #include <osg/Matrix>
  8. #include <osgGA/TrackballManipulator>
  9. #include <osgUtil/LineSegmentIntersector>
  10. #include <osgUtil/IntersectionVisitor>
  11. #include <osg/PolygonMode>
  12. #include <vector>
  13. #include <iostream>
  14. #include <fstream>
  15. #include <string>
  16. #include <math.h>
  17. const char* openScadBase =
  18. "$fn = 100;\n"
  19. "module optiTrackPointBase(translation, rotation) {\n"
  20. "translate(translation) rotate(rotation) cylinder(10, 1, 1, false);\n"
  21. "}\n";
  22. class TrackPoint {
  23. public:
  24. TrackPoint(osg::Vec3 point, osg::Vec3 normal);
  25. osg::ref_ptr<osg::MatrixTransform> getUppermostRoot();
  26. osg::Vec3 getTranslation();
  27. osg::Vec3 getRotation();
  28. protected:
  29. osg::ref_ptr<osg::MatrixTransform> _translationGroup;
  30. osg::ref_ptr<osg::MatrixTransform> _rotationGroup;
  31. private:
  32. osg::Vec3 _point;
  33. osg::Vec3 _normal;
  34. };
  35. TrackPoint::TrackPoint(osg::Vec3 point, osg::Vec3 normal) {
  36. osg::ref_ptr<osg::Geode> geode = new osg::Geode;
  37. osg::ref_ptr<osg::ShapeDrawable> cylinder = new osg::ShapeDrawable();
  38. cylinder->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, 0.0f), 1.0f, 10.0f));
  39. cylinder->setColor(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
  40. geode->addDrawable(cylinder.get());
  41. _rotationGroup = new osg::MatrixTransform;
  42. _rotationGroup->addChild(geode.get());
  43. _rotationGroup->setMatrix(osg::Matrix::rotate(osg::Vec3f(0.0f, 0.0f, 1.0f), normal));
  44. _translationGroup = new osg::MatrixTransform;
  45. _translationGroup->addChild(_rotationGroup.get());
  46. _translationGroup->setMatrix(osg::Matrix::translate(point));
  47. _point = point;
  48. _normal = normal;
  49. }
  50. osg::ref_ptr<osg::MatrixTransform> TrackPoint::getUppermostRoot() {
  51. return _translationGroup.get();
  52. }
  53. osg::Vec3 TrackPoint::getTranslation() {
  54. return _point;
  55. }
  56. osg::Vec3 TrackPoint::getRotation() {
  57. printf("YNorm: %lf %lf %lf\n", _normal.x(), _normal.y(), _normal.z());
  58. osg::Vec3 start = osg::Vec3(0.0f, 0.0f, 1.0f);
  59. // From https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
  60. osg::Quat quat = osg::Quat(0.0f, 0.0f, 0.0f, 0.0f);
  61. quat.makeRotate(start, _normal);
  62. float sinr_cosp = 2 * (quat.w() * quat.x() + quat.y() * quat.z());
  63. float cosr_cosp = 1 - 2 * (quat.x() * quat.x() + quat.y() * quat.y());
  64. float xRotation = std::atan2(sinr_cosp, cosr_cosp) * 180.0 / M_PI;
  65. float sinp = 2 * (quat.w() * quat.y() - quat.z() * quat.x());
  66. float yRotation;
  67. if (std::abs(sinp) >= 1) {
  68. yRotation = std::copysign(M_PI / 2, sinp) * 180.0 / M_PI;
  69. } else {
  70. yRotation = std::asin(sinp) * 180.0 / M_PI;
  71. }
  72. float siny_cosp = 2 * (quat.w() * quat.z() + quat.x() * quat.y());
  73. float cosy_cosp = 1 - 2 * (quat.y() * quat.y() + quat.z() * quat.z());
  74. float zRotation = std::atan2(siny_cosp, cosy_cosp) * 180.0 / M_PI;
  75. return osg::Vec3(xRotation, yRotation, zRotation);
  76. }
  77. class OpenScadRenderer {
  78. public:
  79. void render(std::vector<TrackPoint*> points);
  80. };
  81. void OpenScadRenderer::render(std::vector<TrackPoint*> points) {
  82. std::ofstream scadFile;
  83. scadFile.open("/tmp/output.scad");
  84. scadFile << openScadBase;
  85. scadFile << "import(\"testbutton.stl\");\n";
  86. for (TrackPoint* point: points) {
  87. osg::Vec3 translation = point->getTranslation();
  88. osg::Vec3 rotation = point->getRotation();
  89. scadFile << "optiTrackPointBase([" << translation.x() << "," << translation.y() << "," << translation.z() << "], [" << rotation.x() << "," << rotation.y() << "," << rotation.z() << "]);\n";
  90. }
  91. scadFile.close();
  92. system("openscad -o /tmp/output.stl /tmp/output.scad");
  93. }
  94. class StoreHandler {
  95. public:
  96. void addTrackingPoint(osg::Vec3 point, osg::Vec3 normal);
  97. StoreHandler(osg::ref_ptr<osg::Group> root);
  98. std::vector<TrackPoint*> getPoints();
  99. protected:
  100. std::vector<TrackPoint*> points;
  101. private:
  102. osg::ref_ptr<osg::Group> _root;
  103. };
  104. void StoreHandler::addTrackingPoint(osg::Vec3 point, osg::Vec3 normal) {
  105. TrackPoint* trackPoint = new TrackPoint(point, normal);
  106. points.push_back(trackPoint);
  107. _root->addChild(trackPoint->getUppermostRoot());
  108. }
  109. StoreHandler::StoreHandler(osg::ref_ptr<osg::Group> root) {
  110. _root = root;
  111. }
  112. std::vector<TrackPoint*> StoreHandler::getPoints() {
  113. return points;
  114. }
  115. StoreHandler* storeHandler;
  116. OpenScadRenderer* openScadRenderer;
  117. class PickHandler: public osgGA::GUIEventHandler {
  118. public:
  119. osg::Node* getOrCreateSelectionCylinder();
  120. virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);
  121. void moveTo(osg::Vec3f position);
  122. void rotateToNormalVector(osg::Vec3f normal);
  123. protected:
  124. osg::ref_ptr<osg::MatrixTransform> _selectionTranslateGroup;
  125. osg::ref_ptr<osg::MatrixTransform> _selectionRotateGroup;
  126. };
  127. osg::Node* PickHandler::getOrCreateSelectionCylinder() {
  128. if (!_selectionTranslateGroup) {
  129. osg::ref_ptr<osg::Geode> geode = new osg::Geode;
  130. osg::ref_ptr<osg::ShapeDrawable> cylinder = new osg::ShapeDrawable();
  131. cylinder->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, 0.0f), 1.0f, 10.0f));
  132. cylinder->setColor(osg::Vec4(1.0f, 0.0f, 0.0f, 0.2f));
  133. geode->addDrawable(cylinder.get());
  134. _selectionRotateGroup = new osg::MatrixTransform;
  135. _selectionRotateGroup->addChild(geode.get());
  136. _selectionTranslateGroup = new osg::MatrixTransform;
  137. _selectionTranslateGroup->addChild(_selectionRotateGroup.get());
  138. }
  139. return _selectionTranslateGroup.get();
  140. }
  141. bool PickHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) {
  142. if (ea.getEventType() != osgGA::GUIEventAdapter::RELEASE || ea.getButton() != osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON || !(ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL)) {
  143. return false;
  144. }
  145. osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
  146. if (viewer) {
  147. osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
  148. osgUtil::IntersectionVisitor iv(intersector.get());
  149. iv.setTraversalMask(~0x1);
  150. viewer->getCamera()->accept(iv);
  151. if (intersector->containsIntersections()) {
  152. osgUtil::LineSegmentIntersector::Intersection result = *(intersector->getIntersections().begin());
  153. moveTo(result.localIntersectionPoint);
  154. rotateToNormalVector(result.localIntersectionNormal);
  155. storeHandler->addTrackingPoint(result.localIntersectionPoint, result.localIntersectionNormal);
  156. openScadRenderer->render(storeHandler->getPoints());
  157. }
  158. }
  159. return false;
  160. }
  161. void PickHandler::moveTo(osg::Vec3f position) {
  162. _selectionTranslateGroup->setMatrix(osg::Matrix::translate(position));
  163. }
  164. void PickHandler::rotateToNormalVector(osg::Vec3f normal) {
  165. _selectionRotateGroup->setMatrix(osg::Matrix::rotate(osg::Vec3f(0.0f, 0.0f, 1.0f), normal));
  166. }
  167. int main(int argc, char** argv) {
  168. // Root node of the scene
  169. osg::ref_ptr<osg::Group> root = new osg::Group;
  170. // Create the viewer
  171. osgViewer::Viewer viewer;
  172. viewer.setSceneData(root);
  173. viewer.realize();
  174. // Add axesNode under root
  175. osg::ref_ptr<osg::Node> axesNode = osgDB::readNodeFile("../../testdata/testbutton.stl");
  176. if (!axesNode) {
  177. printf("Origin node not loaded, model not found\n");
  178. return 1;
  179. }
  180. root->addChild(axesNode);
  181. // Attach a manipulator (it's usually done for us when we use viewer.run())
  182. osg::ref_ptr<osgGA::TrackballManipulator> tm = new osgGA::TrackballManipulator;
  183. viewer.setCameraManipulator(tm);
  184. storeHandler = new StoreHandler(root);
  185. openScadRenderer = new OpenScadRenderer();
  186. osg::ref_ptr<PickHandler> picker = new PickHandler();
  187. root->addChild(picker->getOrCreateSelectionCylinder());
  188. viewer.addEventHandler(picker.get());
  189. return viewer.run();
  190. }