InferRange.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #pragma once
  2. #include <iostream>
  3. #include <rapidjson/document.h>
  4. #include <rapidjson/rapidjson.h>
  5. #include <rapidjson/stringbuffer.h>
  6. #include <rapidjson/writer.h>
  7. #include <vector>
  8. using namespace std;
  9. class Point
  10. {
  11. public:
  12. int keyPointIndex;
  13. int x;
  14. int y;
  15. public:
  16. Point() {}
  17. ~Point() {}
  18. bool jsonToObject(const rapidjson::Value& object){
  19. const auto end = object.MemberEnd();
  20. if(end == object.FindMember("keyPointIndex") || !object["keyPointIndex"].IsInt()){
  21. return false;
  22. }else{
  23. keyPointIndex = object["keyPointIndex"].GetInt();
  24. }
  25. if(end == object.FindMember("point") || !object["point"].IsObject()){
  26. return false;
  27. }else{
  28. const rapidjson::Value& obj = object["point"];
  29. const auto end1 = obj.MemberEnd();
  30. if(end1 == obj.FindMember("x") || !obj["x"].IsInt()){
  31. return false;
  32. }else{
  33. x = ((float)obj["x"].GetInt() / 574) * 1920;
  34. }
  35. if(end1 == obj.FindMember("y") || !obj["y"].IsInt()){
  36. return false;
  37. }else{
  38. y = ((float)obj["y"].GetInt() / 396) * 1080;
  39. }
  40. }
  41. return true;
  42. }
  43. };
  44. class InferRange
  45. {
  46. public:
  47. vector<Point> m_points;
  48. public:
  49. InferRange() {}
  50. ~InferRange() {}
  51. bool jsonToObject(const std::string& json){
  52. rapidjson::Document doc;
  53. if (doc.Parse<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) {
  54. return false;
  55. }
  56. if(doc.IsArray())
  57. {
  58. const rapidjson::Value& objs = doc;
  59. for(int i = 0; i < (int)objs.Size(); i++){
  60. Point point;
  61. if(!point.jsonToObject(objs[i])){
  62. return false;
  63. }
  64. m_points.push_back(point);
  65. }
  66. }else{
  67. return false;
  68. }
  69. return true;
  70. }
  71. };