Forum

Author Topic: Split chunks in 3D and saving each chunk on different file  (Read 4254 times)

lukasz.brozek

  • Newbie
  • *
  • Posts: 3
    • View Profile
Split chunks in 3D and saving each chunk on different file
« on: June 29, 2015, 10:37:05 AM »
It's code that I found on forum, but I add a few features:
  • split chunks in 3D
  • saving each chunk on different file
  • little modified script window

Code: [Select]
#building dense cloud, mesh and merging the result back is optional

import PhotoScan
from PySide import QtGui, QtCore

class SplitDlg(QtGui.QDialog):
# Okno  dialogowe
def __init__(self, parent):

QtGui.QDialog.__init__(self, parent)
self.setWindowTitle("Split in chunks")

self.gridX = 2
self.gridY = 2
self.gridZ = 2
self.gridWidth = 270
self.gridHeight = 270
self.gridDepth = 270
self.scale_box = 10

self.spinX_label = QtGui.QLabel("X")
self.spinX = QtGui.QSpinBox()
self.spinX.setMinimum(2)
self.spinX.setMaximum(20)
self.spinX.setFixedSize(75, 25)
self.spinY_label = QtGui.QLabel("Y")
self.spinY = QtGui.QSpinBox()
self.spinY.setMinimum(2)
self.spinY.setMaximum(20)
self.spinY.setFixedSize(75, 25)
self.spinZ_label = QtGui.QLabel("Z")
self.spinZ = QtGui.QSpinBox()
self.spinZ.setMinimum(2)
self.spinZ.setMaximum(20)
self.spinZ.setFixedSize(75, 25)
self.scale_box_Label1 = QtGui.QLabel("Scale box by")
self.scale_box_Label2 = QtGui.QLabel("%")
self.scale_box = QtGui.QSpinBox()
self.scale_box.setMinimum(1)
self.scale_box.setMaximum(100)
self.scale_box.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 indiv")

self.btnQuit = QtGui.QPushButton("Quit")
self.btnQuit.setFixedSize(75,50)

self.btnP1 = QtGui.QPushButton("Split")
self.btnP1.setFixedSize(75,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_label, 0, 1)
layout.addWidget(self.spinX, 1, 1)
layout.addWidget(self.spinY_label, 0, 2)
layout.addWidget(self.spinY, 1, 2)
layout.addWidget(self.spinZ_label, 0, 3)
layout.addWidget(self.spinZ, 1, 3)

layout.addWidget(self.scale_box, 2, 2)
layout.addWidget(self.scale_box_Label1, 2, 1)
layout.addWidget(self.scale_box_Label2, 2, 3)

layout.addWidget(self.btnP1, 4, 2) #split button
layout.addWidget(self.btnQuit, 4, 3) #quit button
#layout.addWidget(self.grid, 6, 1, 3, 3) #grid
self.setLayout(layout)

proc_split = lambda : self.splitChunks()

"""self.spinX.valueChanged.connect(self.updateGrid)
self.spinY.valueChanged.connect(self.updateGrid)
self.spinZ.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):
"""
Rysowanie nowej siatki
"""

self.gridX = self.spinX.value()
self.gridY = self.spinY.value()
self.gridZ = self.spinZ.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
partsZ = self.gridZ

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 / partsZ

offset = r_center - r_rotate * r_size /2

scale_box = self.scale_box.value() / 100
d = 0

#doc.save()

for k in range(1, partsZ + 1):
for j in range(1, partsY + 1):
for i in range(1, partsX + 1):
new_chunk = chunk.copy()
new_chunk.label = "Chunk "+ str(i)+ "\\" + str(j) + "\\" + str(k)
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, (k - 0.5) * z_scale])
new_center = offset + new_rot * new_center
new_size = PhotoScan.Vector([x_scale + x_scale * scale_box, y_scale + y_scale * scale_box, z_scale + z_scale * scale_box])
new_region.size = new_size
new_region.center = new_center
new_region.rot = new_rot

new_chunk.region = new_region

PhotoScan.app.update()

""""doc.save("Chunk {} na {}.psz".format(i, j))
doc.remove(doc.chunks[1:-1])
doc.remove(doc.chunks[1])"""
d=d+1
doc = PhotoScan.app.document
doc2 = PhotoScan.Document()
doc2.addChunk(doc.chunks[d].copy())
doc2.save("Chunk {} na {} na {}.psz".format(i, j, k))

doc.remove(doc.chunks[1:-1])
doc.remove(doc.chunks[1])
doc.save()

if mergeBack:
for i in range(1, len(doc.chunks)):
chunk = doc.chunks[i]
chunk.remove(chunk.cameras)
doc.chunks[0].model = None
doc.mergeChunks(doc.chunks, merge_dense_clouds = True, merge_models = True, merge_markers = True)
doc.remove(doc.chunks[1:-1])

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 chunks 3D", main)