4*4矩阵转换成二维平面坐标

x33g5p2x  于2022-07-14 转载在 其他  
字(11.3k)|赞(0)|评价(0)|浏览(685)

一、需求:基于苹果数据4*4矩阵数据,转换成二维平面坐标系

二、解决方法与步骤:

用到的方法:

1、Matrix4的转化_Catirl的博客-CSDN博客

2、平面内直角坐标系中坐标旋转变换公式_Eric_Wangyz的博客-CSDN博客_坐标旋转变换公式

苹果数据效果:

{
  "windows" : [
    {
      "category" : {
        "window" : {

        }
      },
      "confidence" : {
        "medium" : {

        }
      },
      "dimensions" : [
        4.4304699897766113,
        2.4727039337158203,
        0
      ],
      "completedEdges" : [

      ],
      "parentIdentifier" : "B4002F3F-2741-405F-9A82-18FF65284C8C",
      "identifier" : "70ABA8D4-0AA8-4404-940E-04309F358EBC",
      "curve" : null,
      "transform" : [
        -0.30633658170700073,
        0,
        -0.95192313194274902,
        0,
        0,
        0.99999994039535522,
        0,
        0,
        0.9519231915473938,
        0,
        -0.30633658170700073,
        0,
        -3.3060400485992432,
        -0.20823472738265991,
        2.7119290828704834,
        1
      ]
    }
  ],
  "doors" : [
    {
      "category" : {
        "door" : {
          "isOpen" : false
        }
      },
      "confidence" : {
        "medium" : {

        }
      },
      "dimensions" : [
        1.0301132202148438,
        2.5737776756286621,
        0
      ],
      "completedEdges" : [

      ],
      "parentIdentifier" : "CC22237C-B6ED-44B5-8547-180A3A0E5994",
      "identifier" : "B19AD43B-3C4D-4DA5-A5C0-34A1D39F5892",
      "curve" : null,
      "transform" : [
        0.3063366711139679,
        0,
        0.95192307233810425,
        0,
        0,
        1,
        0,
        0,
        -0.95192307233810425,
        0,
        0.3063366711139679,
        0,
        0.90193623304367065,
        -0.24133116006851196,
        -0.033205278217792511,
        1
      ]
    },
    {
      "category" : {
        "door" : {
          "isOpen" : false
        }
      },
      "confidence" : {
        "medium" : {

        }
      },
      "dimensions" : [
        1.0522669553756714,
        2.411841869354248,
        0
      ],
      "completedEdges" : [

      ],
      "parentIdentifier" : "CC22237C-B6ED-44B5-8547-180A3A0E5994",
      "identifier" : "8095E4C9-2986-446E-9FC9-D92225777FBC",
      "curve" : null,
      "transform" : [
        0.30633670091629028,
        0,
        0.95192313194274902,
        0,
        0,
        0.99999994039535522,
        0,
        0,
        -0.95192307233810425,
        0,
        0.3063366711139679,
        0,
        1.2751809358596802,
        -0.2488710880279541,
        1.1266312599182129,
        1
      ]
    },
    {
      "category" : {
        "door" : {
          "isOpen" : true
        }
      },
      "confidence" : {
        "medium" : {

        }
      },
      "dimensions" : [
        0.79290330410003662,
        2.3365452289581299,
        0
      ],
      "completedEdges" : [

      ],
      "parentIdentifier" : "CC22237C-B6ED-44B5-8547-180A3A0E5994",
      "identifier" : "717D86E1-F273-4577-85DF-E03B933D6188",
      "curve" : null,
      "transform" : [
        0.30633661150932312,
        0,
        0.95192325115203857,
        0,
        0,
        1,
        0,
        0,
        -0.95192325115203857,
        0,
        0.30633661150932312,
        0,
        0.5105128288269043,
        -0.35994744300842285,
        -1.249530553817749,
        1
      ]
    }
  ],
  "walls" : [
    {
      "category" : {
        "wall" : {

        }
      },
      "confidence" : {
        "high" : {

        }
      },
      "dimensions" : [
        5.6710019111633301,
        2.9569838047027588,
        0
      ],
      "completedEdges" : [

      ],
      "parentIdentifier" : "8D161454-7233-4E76-BC11-3E3186A8B6D6",
      "identifier" : "B4002F3F-2741-405F-9A82-18FF65284C8C",
      "curve" : null,
      "transform" : [
        -0.30633664131164551,
        0,
        -0.9519231915473938,
        0,
        0,
        0.99999994039535522,
        0,
        0,
        0.95192313194274902,
        0,
        -0.30633658170700073,
        0,
        -3.4495170116424561,
        -0.049728155136108398,
        2.2660825252532959,
        1
      ]
    },
    {
      "category" : {
        "wall" : {

        }
      },
      "confidence" : {
        "high" : {

        }
      },
      "dimensions" : [
        4.8466057777404785,
        2.9569838047027588,
        0
      ],
      "completedEdges" : [

      ],
      "parentIdentifier" : "5ADD04DA-13E7-4010-B381-D3307FFD5E46",
      "identifier" : "F4BCF245-4442-422E-9894-177078F54AE0",
      "curve" : null,
      "transform" : [
        0.9519231915473938,
        0,
        -0.30633658170700073,
        0,
        0,
        0.99999994039535522,
        0,
        0,
        0.30633652210235596,
        0,
        0.95192307233810425,
        0,
        -2.0113365650177002,
        -0.049728155136108398,
        -1.1754434108734131,
        1
      ]
    },
    {
      "category" : {
        "wall" : {

        }
      },
      "confidence" : {
        "high" : {

        }
      },
      "dimensions" : [
        4.1486597061157227,
        2.9569838047027588,
        0
      ],
      "completedEdges" : [

      ],
      "parentIdentifier" : "25A96F47-C12D-4EFE-B971-472089DF4594",
      "identifier" : "4AD4A1DE-4257-49F2-B788-EAEE5223107C",
      "curve" : null,
      "transform" : [
        -0.9519231915473938,
        0,
        0.30633658170700073,
        0,
        0,
        0.99999994039535522,
        0,
        0,
        -0.30633652210235596,
        0,
        -0.95192307233810425,
        0,
        0.18788377940654755,
        -0.049728155136108398,
        4.5193233489990234,
        1
      ]
    },
    {
      "category" : {
        "wall" : {

        }
      },
      "confidence" : {
        "high" : {

        }
      },
      "dimensions" : [
        0.69794571399688721,
        2.9569838047027588,
        0
      ],
      "completedEdges" : [

      ],
      "parentIdentifier" : "44C4678D-23CB-4C12-B6C4-762CB0452E60",
      "identifier" : "74CA573A-F192-47E2-A262-EEF9CAF44E7E",
      "curve" : null,
      "transform" : [
        -0.95192313194274902,
        0,
        0.30633693933486938,
        0,
        0,
        0.99999994039535522,
        0,
        0,
        -0.306336909532547,
        0,
        -0.95192307233810425,
        0,
        -2.2487037181854248,
        -0.049728155136108398,
        4.8583579063415527,
        1
      ]
    },
    {
      "category" : {
        "wall" : {

        }
      },
      "confidence" : {
        "high" : {

        }
      },
      "dimensions" : [
        0.4236811101436615,
        2.9569838047027588,
        0
      ],
      "completedEdges" : [

      ],
      "parentIdentifier" : "D510898F-31C8-4986-8A1C-9EE75CC68D91",
      "identifier" : "4912B095-361F-4914-8F1A-FB2F9A0C3C94",
      "curve" : null,
      "transform" : [
        -0.30633756518363953,
        0,
        -0.95192277431488037,
        0,
        0,
        0.99999994039535522,
        0,
        0,
        0.95192277431488037,
        0,
        -0.30633756518363953,
        0,
        -1.8516137599945068,
        -0.049728155136108398,
        4.9531106948852539,
        1
      ]
    },
    {
      "category" : {
        "wall" : {

        }
      },
      "confidence" : {
        "high" : {

        }
      },
      "dimensions" : [
        6.0946831703186035,
        2.9569838047027588,
        0
      ],
      "completedEdges" : [

      ],
      "parentIdentifier" : "34D0C39D-D4D7-48B4-9DE0-7ACD7E6EFBB9",
      "identifier" : "CC22237C-B6ED-44B5-8547-180A3A0E5994",
      "curve" : null,
      "transform" : [
        0.30633670091629028,
        0,
        0.9519231915473938,
        0,
        0,
        0.99999994039535522,
        0,
        0,
        -0.95192313194274902,
        0,
        0.30633664131164551,
        0,
        1.2289738655090332,
        -0.049728155136108398,
        0.98304539918899536,
        1
      ]
    }
  ],
  "openings" : [

  ],
  "objects" : [
    {
      "dimensions" : [
        1.5987249612808228,
        0.94955956935882568,
        0.065000005066394806
      ],
      "confidence" : {
        "medium" : {

        }
      },
      "transform" : [
        0.95192325115203857,
        0,
        -0.30633643269538879,
        0,
        0,
        0.99999994039535522,
        0,
        0,
        0.30633637309074402,
        0,
        0.9519231915473938,
        0,
        -1.6764084100723267,
        -0.001988828182220459,
        -0.99941855669021606,
        1
      ],
      "category" : {
        "television" : {

        }
      },
      "identifier" : "C3BEB09E-7408-42CE-8954-E87B355B4299"
    },
    {
      "dimensions" : [
        0.51159495115280151,
        1.0113105773925781,
        0.56274306774139404
      ],
      "confidence" : {
        "medium" : {

        }
      },
      "transform" : [
        -0.95192283391952515,
        0,
        0.30633756518363953,
        0,
        0,
        1,
        0,
        0,
        -0.30633753538131714,
        0,
        -0.95192289352416992,
        0,
        -0.40752917528152466,
        -1.0225648880004883,
        3.9929168224334717,
        1
      ],
      "category" : {
        "chair" : {

        }
      },
      "identifier" : "B8105472-71D6-417E-9E2C-F1155FB1F1F2"
    },
    {
      "dimensions" : [
        0.4749830961227417,
        0.40395355224609375,
        0.57471352815628052
      ],
      "confidence" : {
        "medium" : {

        }
      },
      "transform" : [
        -0.95192235708236694,
        0,
        0.30633917450904846,
        0,
        0,
        1.0000001192092896,
        0,
        0,
        -0.30633923411369324,
        0,
        -0.95192247629165649,
        0,
        -0.55003666877746582,
        -1.3262432813644409,
        3.1927976608276367,
        1
      ],
      "category" : {
        "chair" : {

        }
      },
      "identifier" : "2A7F638D-DAB2-4FAB-A4EA-FA0F9793390A"
    },
    {
      "dimensions" : [
        0.47498306632041931,
        0.40395355224609375,
        0.57471358776092529
      ],
      "confidence" : {
        "medium" : {

        }
      },
      "transform" : [
        -0.30632853507995605,
        0,
        -0.95192575454711914,
        0,
        0,
        1.0000001192092896,
        0,
        0,
        0.95192593336105347,
        0,
        -0.30632859468460083,
        0,
        -1.6257414817810059,
        -1.3262432813644409,
        1.6771179437637329,
        1
      ],
      "category" : {
        "chair" : {

        }
      },
      "identifier" : "0ED886B4-014C-47E9-B952-F868561FC3B0"
    },
    {
      "dimensions" : [
        0.52009981870651245,
        0.94679880142211914,
        0.57890421152114868
      ],
      "confidence" : {
        "medium" : {

        }
      },
      "transform" : [
        0.21847167611122131,
        0,
        0.97584319114685059,
        0,
        0,
        0.99999988079071045,
        0,
        0,
        -0.97584307193756104,
        0,
        0.21847164630889893,
        0,
        1.4021255970001221,
        -1.0548207759857178,
        2.5414259433746338,
        1
      ],
      "category" : {
        "chair" : {

        }
      },
      "identifier" : "5A194FF7-5D66-4FFA-9379-4B801234BA3A"
    },
    {
      "dimensions" : [
        3.7170906066894531,
        0.83971703052520752,
        1.2917746305465698
      ],
      "confidence" : {
        "high" : {

        }
      },
      "transform" : [
        0.30633649230003357,
        0,
        0.9519231915473938,
        0,
        0,
        1.0000001192092896,
        0,
        0,
        -0.95192337036132812,
        0,
        0.30633652210235596,
        0,
        -1.0929687023162842,
        -1.1083616018295288,
        1.5056676864624023,
        1
      ],
      "category" : {
        "table" : {

        }
      },
      "identifier" : "2524B480-8A47-4ED7-BD97-0A9C6ACE8A95"
    },
    {
      "dimensions" : [
        3.3376014232635498,
        2.3581094741821289,
        0.49721428751945496
      ],
      "confidence" : {
        "medium" : {

        }
      },
      "transform" : [
        -0.95192277431488037,
        0,
        0.30633765459060669,
        0,
        0,
        1,
        0,
        0,
        -0.3063376247882843,
        0,
        -0.95192277431488037,
        0,
        -0.2743067741394043,
        -0.34916538000106812,
        4.4068951606750488,
        1
      ],
      "category" : {
        "storage" : {

        }
      },
      "identifier" : "52E23C35-59C5-404B-A46F-DBDB2A5BECCC"
    }
  ],
  "version" : 1
}

1、4*4矩阵转成二维:基于四维矩阵中心点,求两端点坐标

# 墙体信息
  for wall in json_data["walls"]:
    # 墙体中心坐标
    center_p = np.array([wall["transform"][-4], wall["transform"][-2]])
    length = wall["dimensions"][0]

    transform01 = abs(wall["transform"][10])
    if wall["transform"][10] < 0:
        transform02 = wall["transform"][8]
    else:
        transform02 = -wall["transform"][8]

# 基于中心点与四维矩阵,获取起始点
    p1 = center_p + 0.5 * length * np.array([transform01, transform02])
    p2 = center_p - 0.5 * length * np.array([transform01, transform02])

2、平面坐标系转化

cos = abs(json_data["walls"][0]["transform"][10])
sin = -abs(json_data["walls"][0]["transform"][8])

# # # 坐标翻转
x1 = p1[0] * cos - p1[1] * sin
y1 = p1[1] * cos + p1[0] * sin
p1 = Point2D(int(round(x1 * 1000, 0)), int(round(y1 * 1000, 0)))

x2 = p2[0] * cos - p2[1] * sin
y2 = p2[1] * cos + p2[0] * sin
p2 = Point2D(int(round(x2 * 1000, 0)), int(round(y2 * 1000, 0)))

3、整合代码:

import json
from sympy.geometry import *

PATH = r"D:\Desktop\room74.json"
f = open(PATH, 'rb')
j = json.load(f)
f.close()

def get_start_end_point(center_p, length, transform01, transform02, cos, sin):
    # 基于中心点与四维矩阵,获取起始点
    p1 = center_p + 0.5 * length * np.array([transform01, transform02])
    p2 = center_p - 0.5 * length * np.array([transform01, transform02])

    # # # 坐标翻转
    x1 = p1[0] * cos - p1[1] * sin
    y1 = p1[1] * cos + p1[0] * sin
    p1 = Point2D(int(round(x1 * 1000, 0)), int(round(y1 * 1000, 0)))

    x2 = p2[0] * cos - p2[1] * sin
    y2 = p2[1] * cos + p2[0] * sin
    p2 = Point2D(int(round(x2 * 1000, 0)), int(round(y2 * 1000, 0)))

    return p1, p2

def get_wall_points(json_data):
    # 获取墙所有的坐标点
    wall_p_list = []
    # 墙体信息
    cos = abs(json_data["walls"][0]["transform"][10])
    sin = -abs(json_data["walls"][0]["transform"][8])
    for wall in json_data["walls"]:
        # 墙体中心坐标
        center_p = np.array([wall["transform"][-4], wall["transform"][-2]])
        length = wall["dimensions"][0]

        transform01 = abs(wall["transform"][10])
        if wall["transform"][10] < 0:
            transform02 = wall["transform"][8]
        else:
            transform02 = -wall["transform"][8]

        p1, p2 = get_start_end_point(center_p, length, transform01, transform02, cos, sin)
        wall_p_list.extend([p1, p2])

    return wall_p_list

# 获取墙所有的坐标点:
wall_points_list = get_wall_points(json_data)

print(wall_points_list )

4、结果:

[[Point2D(760, -869), Point2D(-270, -869)], [Point2D(1989, -869), Point2D(937, -869)], [Point2D(-637, -869), Point2D(-1430, -869)]]

相关文章