transforms.cpp 9.1 KB

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