main.cpp 9.9 KB

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