Forum

Author Topic: Align coordinate system to bounding box  (Read 9427 times)

ThomasVD

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Align coordinate system to bounding box
« on: January 24, 2015, 03:12:22 PM »
Hi everyone,

I have no experience whatsoever with coding, but I'm trying to adapt this scrypt so it can be used in version 1.1.0:

Code: [Select]
#rotates model bounding box in accordance of coordinate system for active chunk
#bounding box size is kept
#compatibility: Agisoft PhotoScan Professional 0.9.0

import PhotoScan
import math

doc = PhotoScan.app.document

chunk = doc.activeChunk

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

s = math.sqrt(m[0,0]*m[0,0] + m[0,1]*m[0,1] + m[0,2]*m[0,2]) #scale factor
# S = PhotoScan.Matrix( [[s, 0, 0], [0, s, 0], [0, 0, s]] ) #scale matrix

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)

reg = chunk.region
reg.rot = R.t()
chunk.region = reg

So far I've made it into this:
Code: [Select]
import PhotoScan
import math

doc = PhotoScan.app.document
chunk = doc.chunk

T = chunk.transform.matrix

v_t = T * PhotoScan.Vector( [0,0,0,1] )
v_t.size = 3

m = chunk.crs.localframe(v_t)

m = m * T

s = math.sqrt(m[0,0]*m[0,0] + m[0,1]*m[0,1] + m[0,2]*m[0,2]) #scale factor
# S = PhotoScan.Matrix( [[s, 0, 0], [0, s, 0], [0, 0, s]] ) #scale matrix

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)

reg = chunk.region
reg.rot = R.t()
chunk.region = reg

However I get the following error message:

line 16, in <module>
    m = chunk.crs.localframe(v_t)
AttributeError: 'NoneType' object has no attribute 'localframe'

Any tips on how to overcome this issue?

Thanks in advance,

Thomas

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14012
    • View Profile
Re: Align coordinate system to bounding box
« Reply #1 on: January 24, 2015, 03:56:01 PM »
Hello Thomas,

If the chunk is not referenced in geographic coordinate system (chunk.crs == None) you can just assign m = T.
Best regards,
Alexey Pasumansky,
Agisoft LLC

ThomasVD

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Align coordinate system to bounding box
« Reply #2 on: January 24, 2015, 06:55:09 PM »
Hey Alex,

Thanks for the quick reply, however it still doesn't appear to work. The script now looks like this:

Code: [Select]
import PhotoScan
import math

doc = PhotoScan.app.document
chunk = doc.chunk

T = chunk.transform.matrix

v_t = T * PhotoScan.Vector( [0,0,0,1] )
v_t.size = 3

m = T

s = math.sqrt(m[0,0]*m[0,0] + m[0,1]*m[0,1] + m[0,2]*m[0,2]) #scale factor
# S = PhotoScan.Matrix( [[s, 0, 0], [0, s, 0], [0, 0, s]] ) #scale matrix

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)

reg = chunk.region
reg.rot = R.t()
chunk.region = reg

But rather than changing the coordinate system it just seems to change the position of the bounding box (see same scene before and after running script, in attachment). When I go to predefined views -> top view, the top view is still exactly the same as it was before running the script  :-\


Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14012
    • View Profile
Re: Align coordinate system to bounding box
« Reply #3 on: January 24, 2015, 07:20:30 PM »
Hello Thomas,

Then you just used the wrong script. Try this one (for all chunks in the workspace):

Code: [Select]
#rotates model coordinate system in accordance of bounding box for all chunks in project
#scale is kept
#compatibility: Agisoft PhotoScan Professional 1.1.0

import PhotoScan
import math

doc = PhotoScan.app.document

for chunk in doc.chunks:

R = chunk.region.rot #Bounding box rotation matrix
C = chunk.region.center #Bounding box center vector

if chunk.transform:
T = chunk.transform.matrix
s = math.sqrt(T[0,0]*T[0,0] + T[0,1]*T[0,1] + T[0,2]*T[0,2]) #scaling
S = PhotoScan.Matrix( [[s, 0, 0, 0], [0, s, 0, 0], [0, 0, s, 0], [0, 0, 0, 1]] ) #scale matrix
else:
S = PhotoScan.Matrix( [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] )

T = PhotoScan.Matrix( [[R[0,0],R[0,1],R[0,2], C[0]], [R[1,0],R[1,1],R[1,2],C[1]], [R[2,0],R[2,1],R[2,2],C[2]], [0,0,0,1]])

chunk.transform.matrix = S * T.inv() #resulting chunk transformation matrix
Best regards,
Alexey Pasumansky,
Agisoft LLC

ThomasVD

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Align coordinate system to bounding box
« Reply #4 on: January 24, 2015, 07:44:43 PM »
Thank you Alexey, there is no way I would ever have been able to script that myself. Works perfectly.

If I'm ever in St Petersburg I want to buy you and the rest of the Agisoft team a few bottles of champagne, the service really is exceptional.

Thanks again

ktfank

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Align coordinate system to bounding box
« Reply #5 on: August 17, 2017, 03:55:37 AM »
I'm running into an issue with Alexey's script. It works, but then my camera errors are huge. When I update and/or calibrate the cameras, it flips the model upside down again. This is only happening when I am georeferencing with geotagged images. When I use GCPs instead, the model remains orientated correctly after I update the cameras.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14012
    • View Profile
Re: Align coordinate system to bounding box
« Reply #6 on: August 17, 2017, 10:13:30 AM »
Hello ktfank,

The script is changing the orientation of the model in space, but when you use coordinates of the camera positions or markers to gereference it again, the previous changes applied by the script will be discarded.

You cannot have model referenced by two different approaches at the same time, so it may be reasonable to uncheck all cameras and markers in the Reference pane before running the script.
Best regards,
Alexey Pasumansky,
Agisoft LLC

ktfank

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Align coordinate system to bounding box
« Reply #7 on: December 15, 2017, 03:50:26 AM »
Another question for you Alexey: I am using a user-defined coordinate system for my models so that I can reference my GCPs/markers in NAVD88 (I downloaded geoid12a from the Agisoft  website). However now when I try to use this script to orient the bounding box to my markers I get an Error: "Vertical datum out of range". (I actually get the same error message printed repetitively, about 5 times.). This appears to be a problem only with the last update (1.3.4.5067) as I was successfully using the script with this same coordinate system before.

Is there a way to use the script with my chosen datums now?

Here are the parameters of my user-defined coordinate system:

Projected Coordinate System: NAD_1983_2011_UTM_Zone_10N
Projection Method: Transverse Mercator
    Latitude of natural origin: 0
    Longitude of natural origin: -123
    Scale factor at natural origin: 0.9996
    False Easting: 500000
    False Northing: 0
Geographic Coordinate System: NAD83(2011) (EPSG::6318)
Geodetic Datum: NAD83 (National Spatial Reference System 2011) (EPSG::1116)
Ellipsoid: GRS 1980 (EPSG::7019)
Prime Meridian: Greenwich (EPSG::8901)
Linear Units: metre (EPSG::9001)
Vertical Datum: North American Vertical Datum 1988 (EPSG::5103)
Vertical Units: metre (EPSG::9001)

Thank you!
« Last Edit: December 15, 2017, 04:07:30 AM by ktfank »