contourdata.cpp 2.7 KB

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