-
Notifications
You must be signed in to change notification settings - Fork 5
/
split_in_chunks.py
208 lines (153 loc) · 7.71 KB
/
split_in_chunks.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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
204
205
206
207
208
#adds custom menu item
#allows to split the original chunk into multiple chunks with smaller bounding boxes forming a grid
#building dense cloud, mesh and merging the result back is optional
import PhotoScan
from PySide import QtGui, QtCore
class SplitDlg(QtGui.QDialog):
def __init__(self, parent):
QtGui.QDialog.__init__(self, parent)
self.setWindowTitle("Split in chunks")
self.gridX = 2
self.gridY = 2
self.gridWidth = 198
self.gridHeight = 198
self.spinX = QtGui.QSpinBox()
self.spinX.setMinimum(2)
self.spinX.setMaximum(20)
self.spinX.setFixedSize(75, 25)
self.spinY = QtGui.QSpinBox()
self.spinY.setMinimum(2)
self.spinY.setMaximum(20)
self.spinY.setFixedSize(75, 25)
self.chkMesh = QtGui.QCheckBox("Build Mesh")
self.chkMesh.setFixedSize(130,50)
self.chkMesh.setToolTip("Generates mesh for each cell in grid")
self.chkDense = QtGui.QCheckBox("Build Dense Cloud")
self.chkDense.setFixedSize(130,50)
self.chkDense.setWhatsThis("Builds dense cloud for each cell in grid")
self.chkMerge = QtGui.QCheckBox("Merge Back")
self.chkMerge.setFixedSize(90,50)
self.chkMerge.setToolTip("Merges back the processing products formed in the individual cells")
self.btnQuit = QtGui.QPushButton("Quit")
self.btnQuit.setFixedSize(90,50)
self.btnP1 = QtGui.QPushButton("Split")
self.btnP1.setFixedSize(90,50)
self.grid = QtGui.QLabel(" ")
self.grid.resize(self.gridWidth, self.gridHeight)
tempPixmap = QtGui.QPixmap(self.gridWidth, self.gridHeight)
tempImage = tempPixmap.toImage()
for y in range(self.gridHeight):
for x in range(self.gridWidth):
if not (x and y) or (x == self.gridWidth - 1) or (y == self.gridHeight - 1):
tempImage.setPixel(x, y, QtGui.qRgb(0, 0, 0))
elif (x == self.gridWidth / 2) or (y == self.gridHeight / 2):
tempImage.setPixel(x, y, QtGui.qRgb(0, 0, 0))
else:
tempImage.setPixel(x, y, QtGui.qRgb(255, 255, 255))
tempPixmap = tempPixmap.fromImage(tempImage)
self.grid.setPixmap(tempPixmap)
self.grid.show()
layout = QtGui.QGridLayout() #creating layout
layout.addWidget(self.spinX, 0, 0)
layout.addWidget(self.spinY, 0, 1)
layout.addWidget(self.chkDense, 0, 2)
layout.addWidget(self.chkMesh, 0, 3)
layout.addWidget(self.chkMerge, 0, 4)
layout.addWidget(self.btnP1, 2, 2)
layout.addWidget(self.btnQuit, 2, 3)
layout.addWidget(self.grid, 1, 0, 2, 2)
self.setLayout(layout)
proc_split = lambda : self.splitChunks()
self.spinX.valueChanged.connect(self.updateGrid)
self.spinY.valueChanged.connect(self.updateGrid)
QtCore.QObject.connect(self.btnP1, QtCore.SIGNAL("clicked()"), proc_split)
QtCore.QObject.connect(self.btnQuit, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("reject()"))
self.exec()
def updateGrid(self):
"""
Draw new grid
"""
self.gridX = self.spinX.value()
self.gridY = self.spinY.value()
tempPixmap = QtGui.QPixmap(self.gridWidth, self.gridHeight)
tempImage = tempPixmap.toImage()
tempImage.fill(QtGui.qRgb(240, 240, 240))
for y in range(int(self.gridHeight / self.gridY) * self.gridY):
for x in range(int(self.gridWidth / self.gridX) * self.gridX):
if not (x and y) or (x == self.gridWidth - 1) or (y == self.gridHeight - 1):
tempImage.setPixel(x, y, QtGui.qRgb(0, 0, 0))
elif y > int(self.gridHeight / self.gridY) * self.gridY:
tempImage.setPixel(x, y, QtGui.qRgb(240, 240, 240))
elif x > int(self.gridWidth / self.gridX) * self.gridX:
tempImage.setPixel(x, y, QtGui.qRgb(240, 240, 240))
else:
tempImage.setPixel(x, y, QtGui.qRgb(255, 255, 255))
for y in range(0, int(self.gridHeight / self.gridY + 1) * self.gridY, int(self.gridHeight / self.gridY)):
for x in range(int(self.gridWidth / self.gridX) * self.gridX):
tempImage.setPixel(x, y, QtGui.qRgb(0, 0, 0))
for x in range(0, int(self.gridWidth / self.gridX + 1) * self.gridX, int(self.gridWidth / self.gridX)):
for y in range(int(self.gridHeight / self.gridY) * self.gridY):
tempImage.setPixel(x, y, QtGui.qRgb(0, 0, 0))
tempPixmap = tempPixmap.fromImage(tempImage)
self.grid.setPixmap(tempPixmap)
self.grid.show()
return True
def splitChunks(self):
self.gridX = self.spinX.value()
self.gridY = self.spinY.value()
partsX = self.gridX
partsY = self.gridY
print("Script started")
buildMesh = self.chkMesh.isChecked()
buildDense = self.chkDense.isChecked()
mergeBack = self.chkMerge.isChecked()
doc = PhotoScan.app.document
chunk = doc.chunk
region = chunk.region
r_center = region.center
r_rotate = region.rot
r_size = region.size
x_scale = r_size.x / partsX
y_scale = r_size.y / partsY
z_scale = r_size.z
offset = r_center - r_rotate * r_size /2.
for j in range(1, partsY + 1): #creating new chunks and adjusting bounding box
for i in range(1, partsX + 1):
new_chunk = chunk.copy()
new_chunk.label = "Chunk "+ str(i)+ "\\" + str(j)
new_chunk.model = None
doc.addChunk(new_chunk)
new_region = PhotoScan.Region()
new_rot = r_rotate
new_center = PhotoScan.Vector([(i - 0.5) * x_scale, (j - 0.5) * y_scale, 0.5 * z_scale])
new_center = offset + new_rot * new_center
new_size = PhotoScan.Vector([x_scale, y_scale, z_scale])
new_region.size = new_size
new_region.center = new_center
new_region.rot = new_rot
new_chunk.region = new_region
PhotoScan.app.update()
if buildDense:
new_chunk.buildDenseCloud(quality = PhotoScan.Quality.MediumQuality, filter = PhotoScan.FilterMode.AggressiveFiltering)
if buildMesh:
if new_chunk.dense_cloud:
new_chunk.buildModel(surface = PhotoScan.SurfaceType.HeightField, source = PhotoScan.PointsSource.DensePoints, interpolation = PhotoScan.Interpolation.EnabledInterpolation, face_count = PhotoScan.FaceCount.HighFaceCount)
else:
new_chunk.buildModel(surface = PhotoScan.SurfaceType.HeightField, source = PhotoScan.PointsSource.SparsePoints, interpolation = PhotoScan.Interpolation.EnabledInterpolation, face_count = PhotoScan.FaceCount.HighFaceCount)
new_chunk.depth_maps = None
if mergeBack:
for i in range(1, len(doc.chunks)):
chunk = doc.chunks[i]
chunk.remove(chunk.cameras)
doc.chunks[0].model = None #removing model from original chunk, just for case
doc.mergeChunks(doc.chunks, merge_dense_clouds = True, merge_models = True, merge_markers = True) #merging all smaller chunks into single one
doc.remove(doc.chunks[1:-1]) #removing smaller chunks.
print("Script finished")
return True
def main():
global doc
doc = PhotoScan.app.document
app = QtGui.QApplication.instance()
parent = app.activeWindow()
dlg = SplitDlg(parent)
PhotoScan.app.addMenuItem("Custom/Split in chunks", main)