File size: 1,505 Bytes
88b0dcb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
""" 
@Date: 2021/11/06
@description:
"""
import cv2
import numpy as np


def xyz2json(xyz, ratio, camera_height=1.6):
    xyz = xyz * camera_height
    ceiling_height = camera_height * ratio
    layout_height = camera_height + ceiling_height
    data = {
        'cameraHeight': camera_height,
        'layoutHeight': layout_height,
        'cameraCeilingHeight': ceiling_height,
        'layoutObj2ds': {
            'num': 0,
            'obj2ds': []
        },
        'layoutPoints': {
            'num': xyz.shape[0],
            'points': []
        },
        'layoutWalls': {
            'num': xyz.shape[0],
            'walls': []
        }
    }

    xyz = np.concatenate([xyz, xyz[0:1, :]], axis=0)
    R_180 = cv2.Rodrigues(np.array([0, -1 * np.pi, 0], np.float32))[0]
    for i in range(xyz.shape[0] - 1):
        a = np.dot(R_180, xyz[i, :])
        a[0] *= -1
        b = np.dot(R_180, xyz[i + 1, :])
        b[0] *= -1
        c = a.copy()
        c[1] = 0
        normal = np.cross(a - b, a - c)
        normal /= np.linalg.norm(normal)
        d = -np.sum(normal * a)
        plane = np.asarray([normal[0], normal[1], normal[2], d])

        data['layoutPoints']['points'].append({'xyz': a.tolist(), 'id': i})

        next_i = 0 if i + 1 >= (xyz.shape[0] - 1) else i + 1
        tmp = {
            'normal': normal.tolist(),
            'planeEquation': plane.tolist(),
            'pointsIdx': [i, next_i]
        }
        data['layoutWalls']['walls'].append(tmp)

    return data