#include "contourdata.h" #include using namespace ArduinoJson; ContourData::ContourData() {} bool ContourData::readFromJsonFile(string jsonfilename) { return readFromJsonFile(jsonfilename,1,1); } // 缩放和旋转是有顺序要求的,根据试验统一使用先旋转然后再缩放。 bool ContourData::readFromJsonFile(string jsonfilename,float scalex,float scaley) { _points.clear() ; ifstream ifs( jsonfilename.c_str() ) ; if( ifs.good()==false ) return false ; DynamicJsonBuffer jsonBuffer ; JsonArray& root = jsonBuffer.parse(ifs); for(int ip = 0 ; ip < root.size(); ++ ip ) { JsonArray& point = root[ip].as() ; Point pt ; pt.x = point[0].as()*scalex ; pt.y = point[1].as()*scaley ; _points.push_back(pt) ; } return true ; } bool ContourData::readFromJsonFile(string jsonfilename,int translateX, int translateY, float scalex,float scaley) { bool ok = readFromJsonFile( jsonfilename, scalex, scaley) ; for(auto it = _points.begin(); it!=_points.end(); ++ it ) { it->x += translateX; it->y += translateY; } return ok; } bool ContourData::getBoundingBox( float& xmin,float& ymin, float& xmax, float& ymax ) { if(_points.size()==0) return false ; xmin = xmax = _points[0].x ; ymin = ymax = _points[0].y ; for(int i = 1 ; i<_points.size();++i ) { xmin = std::min(xmin , _points[i].x) ; xmax = std::max(xmax,_points[i].x) ; ymin = std::min(ymin , _points[i].y) ; ymax = std::max(ymax,_points[i].y) ; } return true ; } ContourData ContourData::copyAndScale(float scalex,float scaley) { ContourData cd ; cd._points = this->_points ; for(auto it = cd._points.begin();it!=cd._points.end();++it ) { it->x = it->x * scalex ; it->y = it->y * scaley ; } return cd ; } void ContourData::rotate( float rotdeg ) { for(int i = 0 ; i<_points.size();++i ) { _points[i] = rotateOnePoint( _points[i], rotdeg) ; } } ContourData::Point ContourData::rotateOnePoint( ContourData::Point pt0, float rotateAng) { const float DEG2RAD = 3.141592/180.0; float rad = rotateAng*DEG2RAD; float cosval = cosf(rad) ; float sinval = sinf(rad) ; ContourData::Point pt1 ; pt1.x = pt0.x*cosval - pt0.y*sinval ; pt1.y = pt0.x*sinval + pt0.y*cosval ; return pt1 ; } void ContourData::togglePointOrder() { int half = _points.size()/2 ; int n = _points.size()-1 ; for(int i = 0 ; i