contourdata.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "contourdata.h"
  2. #include <cmath>
  3. using namespace ArduinoJson;
  4. ContourData::ContourData() {}
  5. bool ContourData::readFromJsonFile(string jsonfilename)
  6. {
  7. return readFromJsonFile(jsonfilename,1,1);
  8. }
  9. // 缩放和旋转是有顺序要求的,根据试验统一使用先旋转然后再缩放。
  10. bool ContourData::readFromJsonFile(string jsonfilename,float scalex,float scaley)
  11. {
  12. _points.clear() ;
  13. ifstream ifs( jsonfilename.c_str() ) ;
  14. if( ifs.good()==false ) return false ;
  15. DynamicJsonBuffer jsonBuffer ;
  16. JsonArray& root = jsonBuffer.parse(ifs);
  17. for(int ip = 0 ; ip < root.size(); ++ ip ) {
  18. JsonArray& point = root[ip].as<JsonArray>() ;
  19. Point pt ;
  20. pt.x = point[0].as<float>()*scalex ;
  21. pt.y = point[1].as<float>()*scaley ;
  22. _points.push_back(pt) ;
  23. }
  24. return true ;
  25. }
  26. bool ContourData::readFromJsonFile(string jsonfilename,int translateX, int translateY, float scalex,float scaley)
  27. {
  28. bool ok = readFromJsonFile( jsonfilename, scalex, scaley) ;
  29. for(auto it = _points.begin(); it!=_points.end(); ++ it ) {
  30. it->x += translateX;
  31. it->y += translateY;
  32. }
  33. return ok;
  34. }
  35. bool ContourData::getBoundingBox( float& xmin,float& ymin, float& xmax, float& ymax )
  36. {
  37. if(_points.size()==0) return false ;
  38. xmin = xmax = _points[0].x ;
  39. ymin = ymax = _points[0].y ;
  40. for(int i = 1 ; i<_points.size();++i ) {
  41. xmin = std::min(xmin , _points[i].x) ;
  42. xmax = std::max(xmax,_points[i].x) ;
  43. ymin = std::min(ymin , _points[i].y) ;
  44. ymax = std::max(ymax,_points[i].y) ;
  45. }
  46. return true ;
  47. }
  48. ContourData ContourData::copyAndScale(float scalex,float scaley)
  49. {
  50. ContourData cd ;
  51. cd._points = this->_points ;
  52. for(auto it = cd._points.begin();it!=cd._points.end();++it ) {
  53. it->x = it->x * scalex ;
  54. it->y = it->y * scaley ;
  55. }
  56. return cd ;
  57. }
  58. void ContourData::rotate( float rotdeg )
  59. {
  60. for(int i = 0 ; i<_points.size();++i ) {
  61. _points[i] = rotateOnePoint( _points[i], rotdeg) ;
  62. }
  63. }
  64. ContourData::Point ContourData::rotateOnePoint( ContourData::Point pt0, float rotateAng)
  65. {
  66. const float DEG2RAD = 3.141592/180.0;
  67. float rad = rotateAng*DEG2RAD;
  68. float cosval = cosf(rad) ;
  69. float sinval = sinf(rad) ;
  70. ContourData::Point pt1 ;
  71. pt1.x = pt0.x*cosval - pt0.y*sinval ;
  72. pt1.y = pt0.x*sinval + pt0.y*cosval ;
  73. return pt1 ;
  74. }
  75. void ContourData::togglePointOrder()
  76. {
  77. int half = _points.size()/2 ;
  78. int n = _points.size()-1 ;
  79. for(int i = 0 ; i<half;++i ) {
  80. Point pt = _points[i] ;
  81. _points[i] = _points[n-i] ;
  82. _points[n-i] = pt ;
  83. }
  84. }