Hi Alexey,
I've used following the code (taken from the code earlier in this thread - big thanks to you and the other contributors!) to change my bbox x and y dimensions (and center location), however I'm also seeing an unwanted shift in the bbox z center location when the transformation is applied. This is particularly strange because the chunk region size and center z values (checked via the console) are unchanged after the transformation. I've attached screenshots of the bbox before and after transformation, showing the console output for the region size and center.
My code:
import PhotoScan, math
doc = PhotoScan.app.document
chunk = doc.activeChunk
reg = chunk.region
trans = chunk.transform
newregion = PhotoScan.Region()
map_center = [-122.4004003, 37.7772147]
map_size = [50., 50.]
RotZDeg = 0.0
# SET MAPPING REGION (BOUNDING BOX)
# Set region center:
center_geo = PhotoScan.Vector([map_center[0], map_center[1], 0.]) # uses existing region height
v_temp = chunk.crs.unproject(center_geo)
v_temp.size = 4
v_temp.w = 1
centerLocal = chunk.transform.inv() * v_temp
centerLocal.size = 3
newregion.center = PhotoScan.Vector([centerLocal[0], centerLocal[1], reg.center[2]]) # uses existing region height
# Set region size
#<---- Rotation ---->
rot_untransformed = PhotoScan.Matrix().diag([1,1,1,1])
rot_temp = trans * rot_untransformed
s = math.sqrt(rot_temp[0, 0]**2 + rot_temp[0, 1]**2 + rot_temp[0, 2]**2)
R = PhotoScan.Matrix( [[rot_temp[0,0],rot_temp[0,1],rot_temp[0,2]], [rot_temp[1,0],rot_temp[1,1],rot_temp[1,2]], [rot_temp[2,0],rot_temp[2,1],rot_temp[2,2]]])
R = R * (1.0 / s)
#<---- Size ---->
inter_size = PhotoScan.Vector([0,0,0])
geo_size = PhotoScan.Vector(map_size) # uses original chunk region z size
inter_size = geo_size / s
newregion.size = PhotoScan.Vector([inter_size[0], inter_size[1], reg.size[2]])
# Set region rotation
SinRotZ = math.sin(math.radians(RotZDeg))
CosRotZ = math.cos(math.radians(RotZDeg))
RotMat = PhotoScan.Matrix([[CosRotZ, -SinRotZ, 0, 0], [SinRotZ, CosRotZ, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) # just rotate about z-axis
# rotate region bounding box
T = chunk.transform
v = PhotoScan.Vector([0, 0, 0, 1])
v_t = T * v
v_t.size = 3
m = chunk.crs.localframe(v_t)
m = m * T
m = RotMat*m
s = math.sqrt(m[0, 0]**2 + m[0, 1]**2 + m[0, 2]**2) # scale factor
R = PhotoScan.Matrix([[m[0, 0], m[0, 1], m[0, 2]], [m[1, 0], m[1, 1], m[1, 2]], [m[2, 0], m[2, 1], m[2, 2]]])
R = R * (1. / s)
newregion.rot = R.t()
# put newregion to chunk
chunk.region = newregion
Any ideas on what I need to do to keep the z size and center fixed would be greatly appreciated.