main.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package com.fdkk.sxz;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.fdkk.sxz.util.FileUtils;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.stream.Collectors;
  12. public class main {
  13. public static void main(String[] args) throws IOException {
  14. String json = "{\n" +
  15. " \"sweepLocations\": [\n" +
  16. " {\n" +
  17. " \"subgroup\": 0,\n" +
  18. " \"pose\": {\n" +
  19. " \"rotation\": {\n" +
  20. " \"w\": 0.95928264,\n" +
  21. " \"x\": -0.0017092235,\n" +
  22. " \"y\": -0.0016763769,\n" +
  23. " \"z\": -0.28243768\n" +
  24. " },\n" +
  25. " \"translation\": {\n" +
  26. " \"x\": 3.6112025,\n" +
  27. " \"y\": 0.12166519,\n" +
  28. " \"z\": 1\n" +
  29. " }\n" +
  30. " },\n" +
  31. " \"visibles3\": [\n" +
  32. " 1,\n" +
  33. " 2,\n" +
  34. " 3\n" +
  35. " ],\n" +
  36. " \"visibles2\": [\n" +
  37. " 1,\n" +
  38. " 2,\n" +
  39. " 3\n" +
  40. " ],\n" +
  41. " \"puck\": {\n" +
  42. " \"x\": 3.6112025,\n" +
  43. " \"y\": 0.12166519,\n" +
  44. " \"z\": -1.39718955\n" +
  45. " },\n" +
  46. " \"uuid\": \"0\",\n" +
  47. " \"visibles\": [\n" +
  48. " 1,\n" +
  49. " 2,\n" +
  50. " 3\n" +
  51. " ],\n" +
  52. " \"group\": 1\n" +
  53. " },\n" +
  54. " {\n" +
  55. " \"subgroup\": 0,\n" +
  56. " \"pose\": {\n" +
  57. " \"rotation\": {\n" +
  58. " \"w\": 0.97998166,\n" +
  59. " \"x\": -0.00051350286,\n" +
  60. " \"y\": -0.00007887775,\n" +
  61. " \"z\": -0.19908713\n" +
  62. " },\n" +
  63. " \"translation\": {\n" +
  64. " \"x\": 1.6813623,\n" +
  65. " \"y\": 0.20879818,\n" +
  66. " \"z\": 1\n" +
  67. " }\n" +
  68. " },\n" +
  69. " \"visibles3\": [\n" +
  70. " 0,\n" +
  71. " 2,\n" +
  72. " 3\n" +
  73. " ],\n" +
  74. " \"visibles2\": [\n" +
  75. " 0,\n" +
  76. " 2,\n" +
  77. " 3\n" +
  78. " ],\n" +
  79. " \"puck\": {\n" +
  80. " \"x\": 1.6813623,\n" +
  81. " \"y\": 0.20879818,\n" +
  82. " \"z\": -1.50\n" +
  83. " },\n" +
  84. " \"uuid\": \"2\",\n" +
  85. " \"visibles\": [\n" +
  86. " 0,\n" +
  87. " 2,\n" +
  88. " 3\n" +
  89. " ],\n" +
  90. " \"group\": 1\n" +
  91. " },\n" +
  92. " {\n" +
  93. " \"subgroup\": 0,\n" +
  94. " \"pose\": {\n" +
  95. " \"rotation\": {\n" +
  96. " \"w\": 0.7688388,\n" +
  97. " \"x\": -0.0022818057,\n" +
  98. " \"y\": 0.0016657045,\n" +
  99. " \"z\": -0.6394364\n" +
  100. " },\n" +
  101. " \"translation\": {\n" +
  102. " \"x\": -1.0941758,\n" +
  103. " \"y\": -0.15828772,\n" +
  104. " \"z\": 2\n" +
  105. " }\n" +
  106. " },\n" +
  107. " \"visibles3\": [\n" +
  108. " 0,\n" +
  109. " 1,\n" +
  110. " 3\n" +
  111. " ],\n" +
  112. " \"visibles2\": [\n" +
  113. " 0,\n" +
  114. " 1,\n" +
  115. " 3\n" +
  116. " ],\n" +
  117. " \"puck\": {\n" +
  118. " \"x\": -1.0941758,\n" +
  119. " \"y\": -0.15828772,\n" +
  120. " \"z\": -1.39718955\n" +
  121. " },\n" +
  122. " \"uuid\": \"4\",\n" +
  123. " \"visibles\": [\n" +
  124. " 0,\n" +
  125. " 1,\n" +
  126. " 3\n" +
  127. " ],\n" +
  128. " \"group\": 1\n" +
  129. " },\n" +
  130. " {\n" +
  131. " \"subgroup\": 1,\n" +
  132. " \"pose\": {\n" +
  133. " \"rotation\": {\n" +
  134. " \"w\": 0.75378096,\n" +
  135. " \"x\": -0.00085483055,\n" +
  136. " \"y\": 0.0011985971,\n" +
  137. " \"z\": -0.6571241\n" +
  138. " },\n" +
  139. " \"translation\": {\n" +
  140. " \"x\": -4.213454,\n" +
  141. " \"y\": -0.17369196,\n" +
  142. " \"z\": 0.07516695\n" +
  143. " }\n" +
  144. " },\n" +
  145. " \"visibles3\": [\n" +
  146. " 0,\n" +
  147. " 1,\n" +
  148. " 2\n" +
  149. " ],\n" +
  150. " \"visibles2\": [\n" +
  151. " 0,\n" +
  152. " 1,\n" +
  153. " 2\n" +
  154. " ],\n" +
  155. " \"puck\": {\n" +
  156. " \"x\": -4.213454,\n" +
  157. " \"y\": -0.17369196,\n" +
  158. " \"z\": -1.39718955\n" +
  159. " },\n" +
  160. " \"uuid\": \"6\",\n" +
  161. " \"visibles\": [\n" +
  162. " 0,\n" +
  163. " 1,\n" +
  164. " 2\n" +
  165. " ],\n" +
  166. " \"group\": 1\n" +
  167. " }\n" +
  168. " ]\n" +
  169. "}";
  170. JSONObject visionJson = JSON.parseObject(json);
  171. FileUtils.writeFile("D:\\test\\vision.txt", visionJson.toString());
  172. JSONArray sweepArray = visionJson.getJSONArray("sweepLocations");
  173. List<JSONObject> roamingPoint = new ArrayList();
  174. for (int i = 0; i < sweepArray.size(); i++) {
  175. JSONObject objectJson = sweepArray.getJSONObject(i);
  176. JSONObject newJson = new JSONObject();
  177. JSONObject puck = objectJson.getJSONObject("puck");
  178. JSONObject pose = objectJson.getJSONObject("pose");
  179. newJson.put("puck", puck);
  180. newJson.put("pose", pose);
  181. newJson.put("subgroup", objectJson.getInteger("subgroup"));
  182. roamingPoint.add(newJson);
  183. }
  184. Map<String, List<JSONObject>> subgroup = roamingPoint.stream().collect(
  185. Collectors.groupingBy(x -> x.getString("subgroup"))).entrySet().stream()
  186. .collect(Collectors.toMap(e -> "floor" + e.getKey(), Map.Entry::getValue));
  187. Map<String, Map<String, Double>> avg = new HashMap<>();
  188. if (subgroup.size() == 1) {
  189. for (String s : subgroup.keySet()) {
  190. List<JSONObject> jsonObjects = subgroup.get(s);
  191. Map<String, Double> avgMath = new HashMap<>();
  192. double puckTotal = jsonObjects.stream().collect(Collectors.averagingDouble(item -> item.getJSONObject("puck").getDouble("z")));
  193. double translationTotal = jsonObjects.stream().collect(Collectors.averagingDouble(item -> item.getJSONObject("pose").getJSONObject("translation").getDouble("z")));
  194. avgMath.put("translationTotalAvg", translationTotal);
  195. avgMath.put("puckTotalAvg", puckTotal);
  196. avg.put(s, avgMath);
  197. }
  198. }
  199. System.out.println(JSON.toJSONString(subgroup));
  200. }
  201. }