Forum

Author Topic: Align recreated surface with horizontal plane  (Read 1050 times)

arjunkg96

  • Newbie
  • *
  • Posts: 10
    • View Profile
Align recreated surface with horizontal plane
« on: July 14, 2021, 12:50:46 AM »
Hello everyone,

I want to align the recreated surface with the horizontal plane so that I can create a DEM with 'Current view' (being the TOP VIEW). I can do this manually in the UI by simply rotating it and approximately setting the top view and the create DEM with current view. The challenge I am facing is to do this with the Python code so that I can automate it. I am very new to this, can somebody please tell me how I can align the recreated surface to a TOP VIEW through Python? So that I can then do chunk.buildDem() over that next.
My picture data is not geo referenced FYI.
Any help will be greatly appreciated.

Thank you in advance!

Paulo

  • Hero Member
  • *****
  • Posts: 748
    • View Profile
Re: Align recreated surface with horizontal plane
« Reply #1 on: July 14, 2021, 05:29:09 AM »
Hi arjungk96,

You would need to place 3 markers (m1, m2, m3) on model representing the desired horizontal plane X, Y axii   for your DEM creation. with m1 to m2 defining X and m1 to m3 defining Y as in:
Code: [Select]
def cross(a, b):
result = Metashape.Vector([a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y *b.x])
return result.normalized()

chunk = Metashape.app.document.chunk
X = (m2.position - m1.position).normalized()
Y = (m3.position - m1.position).normalized()
Z = cross(X,Y)
Y1 = -cross(X,Z)
T = Metashape.Matrix( [[X.x,Y1.x,Z.x,0],[X.y,Y1.y,Z.y,0],[X.z,Y1.z,Z.z,0],[0,0,0,1]] ).t()
chunk.transform.matrix = T

as you can see in following screen capture, the X, Y axii are now practically parallel with church back wall as defined by marhers 1, 2, 3
PS. I corrected code to use transpose of T and 0,0,0 translation....
« Last Edit: July 15, 2021, 10:48:08 AM by Paulo »
Best Regards,

Paul Pelletier

arjunkg96

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Align recreated surface with horizontal plane
« Reply #2 on: July 15, 2021, 02:17:22 AM »
Thank you Paulo,
The transformation is running well but the result is that the recreated surface just flips and orients in a weird angle. Alternatively, I just found out that I can create DEM by using markers. I was able to place three markers and then create the DEM on the Metashape application by clicking on BuildDEM >> planar >> projection plane >>markers. That gives me the result that I want. But I want to do this in code, can you please tell me how I can achieve the following through Python script?:

I want to do this in python : build DEM with the following parameters: 1) Projection planar, projection plane: markers, vertical axis: point 1 -> point 2, source data: depth maps.

In the python manual, it does not specify where I can change the projection source and projection plane and even specify the vertical axis. So could you please tell me how I can use the buildDem() function with the parameters (listed above) written with the correct syntax?

Really appreciate your help. Thank you in advance!

Paulo

  • Hero Member
  • *****
  • Posts: 748
    • View Profile
Re: Align recreated surface with horizontal plane
« Reply #3 on: July 15, 2021, 04:53:56 AM »
The code should work Please try to run again as I corrected some error (see previous post)....maybe you can supply the following information from your project:

m1.position, m2.position, m3.position where m1, m2 and m3 are 3 markers defining your required X, Y axii,
chunk.transform.matrix before running the code (normally should be Identity matrix for non referenced project),
T or chunk.transform.matrix after running the code

so i can better understand the issue...

you can also use buildDem method to generate your Dem as:

chunk.buildDem(source_data =  Metashape.ModelData, projection = demprojection) # case where using model as source for Dem

where :
demprojection = Metashape.OrthoProjection()
demprojection.type = Metashape.OrthoProjection.Type.Planar # define orthoprojection as Planar
demprojection.matrix = T # where T is projection matrix defined in previous code snipper....

however the buildDem does not take into account demprojection as being planar and thus does not produce Dem according to defined projection plane... maybe Alexey from Agisoft can help  :)
« Last Edit: July 15, 2021, 11:40:47 AM by Paulo »
Best Regards,

Paul Pelletier

arjunkg96

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Align recreated surface with horizontal plane
« Reply #4 on: July 16, 2021, 07:23:52 PM »
Thank you Paulo,
Your edited code works. The problem is that we need to place the markers carefully and manually. But in my use case, I need to do this rotation completely in Python without manually interfering with it. So, instead of placing the markers manually, I was thinking of just extracting the coordinates of the 3 vertices out of the 8 in the Bounding Box (as shown in the picture). Using these three points as our markers (m1, m2, m3) we can use your script to perform the rotation.

I was able to extract the coordinates of the 8 vertices of the BBox, but I am not sure in what order they are as there is no information about that in the user manual. Could you please tell me how I can extract the coordinates of just these 3 points in Python? I just need the coordinates of the points in the same order as shown in the picture.

I hope you got why I am extracting these coordinates of the BBox, I want them to act as the m1, m2, m3 markers as per your previous solution because I want to automate this process.

Thank you very much for your help in this.

Paulo

  • Hero Member
  • *****
  • Posts: 748
    • View Profile
Re: Align recreated surface with horizontal plane
« Reply #5 on: July 17, 2021, 03:31:54 AM »
OK,
Let us suppose we have a region = chunk.region, then the 4 vertices  (p1, p2, p3, p4) at top of bounding region (red face is bottom) are given by:
Code: [Select]
chunk= Metashape.app.document.chunk
region = chunk.region
width, depth, height = region.size
R = region.rot
p1 = region.center + R * Metashape.Vector((-width/2, -depth/2, height/2))
p2 = region.center + R * Metashape.Vector((width/2, -depth/2, height/2))
p3 = region.center + R * Metashape.Vector((width/2, depth/2, height/2))
p4 = region.center + R * Metashape.Vector((-width/2, depth/2, height/2))
of course the order of points 1 to 4 will depend on R (region.rot)... see attachment
Best Regards,

Paul Pelletier

arjunkg96

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Align recreated surface with horizontal plane
« Reply #6 on: July 19, 2021, 06:47:58 PM »
Thank you very much Paulo. Your support helped me get the task done!
Appreciate it very much.