MainViewModel.py 10.7 KB
Newer Older
1
2
import json
from PySide2.QtCore import QObject, Signal
Andy Regensky's avatar
Andy Regensky committed
3
4
5
6
7
8
9
10
11
12
13
14
import numpy as np
from fishui import projections
from fishui import coordinate_conversion


class MainViewModel(QObject):

    didChange = Signal()

    def __init__(self, parent=None):
        super().__init__(parent)

15
16
17
18
19
20
21
22
        self.projections = {
            'equisolid': projections.EquisolidProjection,
            'equidistant': projections.EquidistantProjection,
            'orthographic': projections.OrthographicProjection,
            'stereographic': projections.StereographicProjection,
            'calibrated': projections.CalibratedProjection
        }

23
24
        self.sensor_size_mm = (5.2, 10.4)
        self.sensor_size_px = (1088, 2176)
Andy Regensky's avatar
Andy Regensky committed
25
        self.focal_length_mm = 1.8
26
        self.projection_key = 'equisolid'
27
        self.calibration_coeffs = None
Andy Regensky's avatar
Andy Regensky committed
28

Andy Regensky's avatar
Andy Regensky committed
29
        self.origin = (512, 512)
Andy Regensky's avatar
Andy Regensky committed
30
31
32
33
34
35
        self.blocksize = (64, 64)

        self.affineModel = False
        self.motion_vector_cp0 = (0, 0)
        self.motion_vector_cp1 = (0, 0)

Andy Regensky's avatar
Andy Regensky committed
36
        self.perspective_scale = 1
Andy Regensky's avatar
Andy Regensky committed
37
38
39
40
41

        # Values used for drawing
        self.samples_per_edge = 100
        self.projection = None
        self.perspective = None
42
        self.fov_180_radius = None
43
        self.center = None
Andy Regensky's avatar
Andy Regensky committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
        self.block_xf = None
        self.block_yf = None
        self.block_xp = None
        self.block_yp = None
        self.block_xpm = None
        self.block_ypm = None
        self.block_xfm = None
        self.block_yfm = None
        self.block_ctr_px = None
        self.block_ctr_py = None
        self.block_ctr_pxm = None
        self.block_ctr_pym = None
        self.uwc_sign = None
        self.updateProjections()

59
60
61
62
63
    @property
    def sensor_dimensions_valid(self):
        return np.isclose(self.sensor_size_px[0]/self.sensor_size_mm[0],
                          self.sensor_size_px[1]/self.sensor_size_mm[1])

Andy Regensky's avatar
Andy Regensky committed
64
65
66
67
68
69
70
71
72
73
74
75
    def setSensorSize(self, sensor_size):
        self.sensor_size_mm = sensor_size
        self.updateProjections()

    def setSensorResolution(self, resolution):
        self.sensor_size_px = resolution
        self.updateProjections()

    def setFocalLength(self, focal_length_mm):
        self.focal_length_mm = focal_length_mm
        self.updateProjections()

76
77
78
79
80
81
82
83
    def setProjection(self, projection_key, calib_file=None):
        if projection_key == 'calibrated' and calib_file is not None:
            self.focal_length_mm, self.calibration_coeffs = self.readCalibrationData(calib_file)
        elif projection_key == 'calibrated' and calib_file is None:
            raise Exception("Calibrated projection requires calibration file argument.")
        elif projection_key != 'calibrated' and calib_file is not None:
            raise Exception("Calibration file argument is valid for calibrated projection only.")
        self.projection_key = projection_key
84
85
        self.updateProjections()

Andy Regensky's avatar
Andy Regensky committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
    def setOrigin(self, origin):
        self.origin = origin
        self.updateProjections()

    def setBlocksize(self, blocksize):
        self.blocksize = blocksize
        self.updateProjections()

    def setAffineModel(self, enabled):
        if not self.affineModel and enabled:
            self.motion_vector_cp1 = self.motion_vector_cp0
        self.affineModel = enabled
        self.updateProjections()

    def setMotionVectorCP0(self, motion_vector):
        self.motion_vector_cp0 = motion_vector
        self.updateProjections()

    def setMotionVectorCP1(self, motion_vector):
        self.motion_vector_cp1 = motion_vector
        self.updateProjections()

    def setPerspectiveScale(self, scale):
        self.perspective_scale = scale
        self.updateProjections()

112
113
114
115
116
117
118
119
120
    def readCalibrationData(self, calib_file):
        with open(calib_file, 'r') as file:
            calib_json = json.load(file)
            focal_length = calib_json['focal_length']
            calib_coeffs = np.array([float(coeff) for coeff in calib_json['coefficients']])
        if len(calib_coeffs) == 0:
            raise ValueError
        return focal_length, calib_coeffs

Andy Regensky's avatar
Andy Regensky committed
121
    def updateProjections(self):
122
123
124
125
        # Do not update the projections, if the sensor itself is not valid.
        if not self.sensor_dimensions_valid:
            self.didChange.emit()
            return
Andy Regensky's avatar
Andy Regensky committed
126
        focal_length_px = projections.Projection.to_focal_length_px(self.focal_length_mm, self.sensor_size_mm, self.sensor_size_px)
127
        if self.projection_key == 'calibrated':
128
129
130
131
            px_per_mm = self.sensor_size_px[0]/self.sensor_size_mm[0]
            calib_coeffs = self.calibration_coeffs * px_per_mm
            self.projection = projections.CalibratedProjection(calib_coeffs, focal_length_px)
        else:
132
            self.projection = self.projections[self.projection_key](focal_length_px)
Andy Regensky's avatar
Andy Regensky committed
133
        self.perspective = projections.PerspectiveProjection(focal_length_px)
134
        self.fov_180_radius = self.projection.radius(np.deg2rad(90))
135
        self.center = (np.asarray(self.sensor_size_px) - 1) / 2
Andy Regensky's avatar
Andy Regensky committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
        self.blockSignals(True)
        self.updateFisheyeBlockpoints()
        self.updatePerspectiveBlockpoints()
        self.updatePerspectiveMovedBlockpoints()
        self.updateReprojectedBlockpoints()
        self.blockSignals(False)
        self.didChange.emit()

    def updateFisheyeBlockpoints(self):
        block_x = np.zeros(4 * self.samples_per_edge)
        block_y = np.zeros(4 * self.samples_per_edge)

        # Top edge.
        block_x[0:self.samples_per_edge] = np.linspace(self.origin[0], self.origin[0] + self.blocksize[0], self.samples_per_edge)
        block_y[0:self.samples_per_edge] = self.origin[1]

        # Right edge.
        block_x[self.samples_per_edge:2 * self.samples_per_edge] = self.origin[0] + self.blocksize[0]
        block_y[self.samples_per_edge:2 * self.samples_per_edge] = np.linspace(self.origin[1], self.origin[1] + self.blocksize[1], self.samples_per_edge)

        # Bottom edge.
        block_x[2 * self.samples_per_edge:3 * self.samples_per_edge] = np.linspace(self.origin[0] + self.blocksize[0], self.origin[0],
                                                                         self.samples_per_edge)
        block_y[2 * self.samples_per_edge:3 * self.samples_per_edge] = self.origin[1] + self.blocksize[1]

        # Left edge.
        block_x[3 * self.samples_per_edge:4 * self.samples_per_edge] = self.origin[0]
        block_y[3 * self.samples_per_edge:4 * self.samples_per_edge] = np.linspace(self.origin[1] + self.blocksize[1], self.origin[1],
                                                                         self.samples_per_edge)
165
166

        # Filter within FOV 180
167
168
        r, _ = coordinate_conversion.cartesian_to_polar(block_y - self.center[1],
                                                        block_x - self.center[0])
169
170
171
        block_x = block_x[r <= self.fov_180_radius]
        block_y = block_y[r <= self.fov_180_radius]

Andy Regensky's avatar
Andy Regensky committed
172
173
174
175
176
        self.block_xf = block_x
        self.block_yf = block_y
        self.didChange.emit()

    def updatePerspectiveBlockpoints(self):
177
178
        block_yfc = self.block_yf - self.center[1]
        block_xfc = self.block_xf - self.center[0]
Andy Regensky's avatar
Andy Regensky committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203

        r_f, phi = coordinate_conversion.cartesian_to_polar(block_yfc, block_xfc)
        r_p = self.projection.transform_to(self.perspective, r_f)
        self.uwc_sign = np.sign(r_p)
        block_ypc, block_xpc = coordinate_conversion.polar_to_cartesian(r_p, phi)

        self.block_yp = block_ypc
        self.block_xp = block_xpc
        self.didChange.emit()

    def updatePerspectiveMovedBlockpoints(self):
        if self.affineModel:
            self.updateControlBlockpoints()
            self.updatePerspectiveMovedBlockpointsAffine()
        else:
            self.block_ypm = self.block_yp + self.motion_vector_cp0[1] * self.uwc_sign
            self.block_xpm = self.block_xp + self.motion_vector_cp0[0] * self.uwc_sign
            self.didChange.emit()

    def updateReprojectedBlockpoints(self):
        r_pm, phi_m = coordinate_conversion.cartesian_to_polar(self.block_ypm, self.block_xpm)
        phi_m = phi_m - (self.uwc_sign < 0) * np.pi
        r_fm = self.perspective.transform_to(self.projection, r_pm)
        r_fm = r_fm + (self.uwc_sign < 0) * 2 * (self.projection.radius(np.deg2rad(90)) - r_fm)
        block_yfm, block_xfm = coordinate_conversion.polar_to_cartesian(r_fm, phi_m)
204
205
        self.block_yfm = block_yfm + self.center[1]
        self.block_xfm = block_xfm + self.center[0]
Andy Regensky's avatar
Andy Regensky committed
206
207
208
209
        self.didChange.emit()

    def updateControlBlockpoints(self):
        block_center = np.asarray(self.origin) + (np.asarray(self.blocksize) - 1) / 2
210
211
        r_f, phi = coordinate_conversion.cartesian_to_polar(block_center[1] - self.center[1],
                                                            block_center[0] - self.center[0])
Andy Regensky's avatar
Andy Regensky committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
        r_p = self.projection.transform_to(self.perspective, r_f)
        cp0_y, cp0_x = coordinate_conversion.polar_to_cartesian(r_p, phi)
        cp0_y = cp0_y - self.blocksize[1] / 2
        cp0_x = cp0_x - self.blocksize[0] / 2
        self.block_ctr_px = np.array([cp0_x, cp0_x + self.blocksize[0], cp0_x + self.blocksize[0], cp0_x])
        self.block_ctr_py = np.array([cp0_y, cp0_y, cp0_y + self.blocksize[1], cp0_y + self.blocksize[1]])
        self.didChange.emit()

    def updatePerspectiveMovedBlockpointsAffine(self):
        cp0_position = (self.block_ctr_py[0], self.block_ctr_px[0])
        cps_motion_vector = np.array([self.motion_vector_cp0[0], self.motion_vector_cp1[0],
                                      self.motion_vector_cp0[1], self.motion_vector_cp1[1]])

        block_yp_cp0 = self.block_yp - cp0_position[0]
        block_xp_cp0 = self.block_xp - cp0_position[1]

        block_ypm = np.empty_like(self.block_yp)
        block_xpm = np.empty_like(self.block_xp)
        for i in range(len(self.block_xp)):
            m = block_yp_cp0[i] / (self.blocksize[0] - 1)
            n = block_xp_cp0[i] / (self.blocksize[1] - 1)
            affine_matrix = np.array([[-m, m, 1 - n, n],
                                      [1 - n, n, m, -m]])
            mv = affine_matrix.dot(cps_motion_vector)
            block_ypm[i] = self.block_yp[i] + mv[0]
            block_xpm[i] = self.block_xp[i] + mv[1]

        block_ctr_pxm = np.empty(4)
        block_ctr_pym = np.empty(4)
        for i in range(4):
            m = (self.block_ctr_py[i] - cp0_position[0]) / (self.blocksize[0] - 1)
            n = (self.block_ctr_px[i] - cp0_position[1]) / (self.blocksize[1] - 1)
            affine_matrix = np.array([[-m, m, 1 - n, n],
                                      [1 - n, n, m, -m]])
            mv = affine_matrix.dot(cps_motion_vector)
            block_ctr_pym[i] = self.block_ctr_py[i] + mv[0]
            block_ctr_pxm[i] = self.block_ctr_px[i] + mv[1]

        self.block_ypm = block_ypm
        self.block_xpm = block_xpm
        self.block_ctr_pym = block_ctr_pym
        self.block_ctr_pxm = block_ctr_pxm
        self.didChange.emit()