123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- package com.fdkk.sxz;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.fdkk.sxz.util.FileUtils;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.Collectors;
- public class main {
- public static void main(String[] args) throws IOException {
- String json = "{\n" +
- " \"sweepLocations\": [\n" +
- " {\n" +
- " \"subgroup\": 0,\n" +
- " \"pose\": {\n" +
- " \"rotation\": {\n" +
- " \"w\": 0.95928264,\n" +
- " \"x\": -0.0017092235,\n" +
- " \"y\": -0.0016763769,\n" +
- " \"z\": -0.28243768\n" +
- " },\n" +
- " \"translation\": {\n" +
- " \"x\": 3.6112025,\n" +
- " \"y\": 0.12166519,\n" +
- " \"z\": 1\n" +
- " }\n" +
- " },\n" +
- " \"visibles3\": [\n" +
- " 1,\n" +
- " 2,\n" +
- " 3\n" +
- " ],\n" +
- " \"visibles2\": [\n" +
- " 1,\n" +
- " 2,\n" +
- " 3\n" +
- " ],\n" +
- " \"puck\": {\n" +
- " \"x\": 3.6112025,\n" +
- " \"y\": 0.12166519,\n" +
- " \"z\": -1.39718955\n" +
- " },\n" +
- " \"uuid\": \"0\",\n" +
- " \"visibles\": [\n" +
- " 1,\n" +
- " 2,\n" +
- " 3\n" +
- " ],\n" +
- " \"group\": 1\n" +
- " },\n" +
- " {\n" +
- " \"subgroup\": 0,\n" +
- " \"pose\": {\n" +
- " \"rotation\": {\n" +
- " \"w\": 0.97998166,\n" +
- " \"x\": -0.00051350286,\n" +
- " \"y\": -0.00007887775,\n" +
- " \"z\": -0.19908713\n" +
- " },\n" +
- " \"translation\": {\n" +
- " \"x\": 1.6813623,\n" +
- " \"y\": 0.20879818,\n" +
- " \"z\": 1\n" +
- " }\n" +
- " },\n" +
- " \"visibles3\": [\n" +
- " 0,\n" +
- " 2,\n" +
- " 3\n" +
- " ],\n" +
- " \"visibles2\": [\n" +
- " 0,\n" +
- " 2,\n" +
- " 3\n" +
- " ],\n" +
- " \"puck\": {\n" +
- " \"x\": 1.6813623,\n" +
- " \"y\": 0.20879818,\n" +
- " \"z\": -1.50\n" +
- " },\n" +
- " \"uuid\": \"2\",\n" +
- " \"visibles\": [\n" +
- " 0,\n" +
- " 2,\n" +
- " 3\n" +
- " ],\n" +
- " \"group\": 1\n" +
- " },\n" +
- " {\n" +
- " \"subgroup\": 0,\n" +
- " \"pose\": {\n" +
- " \"rotation\": {\n" +
- " \"w\": 0.7688388,\n" +
- " \"x\": -0.0022818057,\n" +
- " \"y\": 0.0016657045,\n" +
- " \"z\": -0.6394364\n" +
- " },\n" +
- " \"translation\": {\n" +
- " \"x\": -1.0941758,\n" +
- " \"y\": -0.15828772,\n" +
- " \"z\": 2\n" +
- " }\n" +
- " },\n" +
- " \"visibles3\": [\n" +
- " 0,\n" +
- " 1,\n" +
- " 3\n" +
- " ],\n" +
- " \"visibles2\": [\n" +
- " 0,\n" +
- " 1,\n" +
- " 3\n" +
- " ],\n" +
- " \"puck\": {\n" +
- " \"x\": -1.0941758,\n" +
- " \"y\": -0.15828772,\n" +
- " \"z\": -1.39718955\n" +
- " },\n" +
- " \"uuid\": \"4\",\n" +
- " \"visibles\": [\n" +
- " 0,\n" +
- " 1,\n" +
- " 3\n" +
- " ],\n" +
- " \"group\": 1\n" +
- " },\n" +
- " {\n" +
- " \"subgroup\": 1,\n" +
- " \"pose\": {\n" +
- " \"rotation\": {\n" +
- " \"w\": 0.75378096,\n" +
- " \"x\": -0.00085483055,\n" +
- " \"y\": 0.0011985971,\n" +
- " \"z\": -0.6571241\n" +
- " },\n" +
- " \"translation\": {\n" +
- " \"x\": -4.213454,\n" +
- " \"y\": -0.17369196,\n" +
- " \"z\": 0.07516695\n" +
- " }\n" +
- " },\n" +
- " \"visibles3\": [\n" +
- " 0,\n" +
- " 1,\n" +
- " 2\n" +
- " ],\n" +
- " \"visibles2\": [\n" +
- " 0,\n" +
- " 1,\n" +
- " 2\n" +
- " ],\n" +
- " \"puck\": {\n" +
- " \"x\": -4.213454,\n" +
- " \"y\": -0.17369196,\n" +
- " \"z\": -1.39718955\n" +
- " },\n" +
- " \"uuid\": \"6\",\n" +
- " \"visibles\": [\n" +
- " 0,\n" +
- " 1,\n" +
- " 2\n" +
- " ],\n" +
- " \"group\": 1\n" +
- " }\n" +
- " ]\n" +
- "}";
- JSONObject visionJson = JSON.parseObject(json);
- FileUtils.writeFile("D:\\test\\vision.txt", visionJson.toString());
- JSONArray sweepArray = visionJson.getJSONArray("sweepLocations");
- List<JSONObject> roamingPoint = new ArrayList();
- for (int i = 0; i < sweepArray.size(); i++) {
- JSONObject objectJson = sweepArray.getJSONObject(i);
- JSONObject newJson = new JSONObject();
- JSONObject puck = objectJson.getJSONObject("puck");
- JSONObject pose = objectJson.getJSONObject("pose");
- newJson.put("puck", puck);
- newJson.put("pose", pose);
- newJson.put("subgroup", objectJson.getInteger("subgroup"));
- roamingPoint.add(newJson);
- }
- Map<String, List<JSONObject>> subgroup = roamingPoint.stream().collect(
- Collectors.groupingBy(x -> x.getString("subgroup"))).entrySet().stream()
- .collect(Collectors.toMap(e -> "floor" + e.getKey(), Map.Entry::getValue));
- Map<String, Map<String, Double>> avg = new HashMap<>();
- if (subgroup.size() == 1) {
- for (String s : subgroup.keySet()) {
- List<JSONObject> jsonObjects = subgroup.get(s);
- Map<String, Double> avgMath = new HashMap<>();
- double puckTotal = jsonObjects.stream().collect(Collectors.averagingDouble(item -> item.getJSONObject("puck").getDouble("z")));
- double translationTotal = jsonObjects.stream().collect(Collectors.averagingDouble(item -> item.getJSONObject("pose").getJSONObject("translation").getDouble("z")));
- avgMath.put("translationTotalAvg", translationTotal);
- avgMath.put("puckTotalAvg", puckTotal);
- avg.put(s, avgMath);
- }
- }
- System.out.println(JSON.toJSONString(subgroup));
- }
- }
|