tmontaigu / CloudCompare-PythonRuntime

Python plugin for CloudCompare
55 stars 15 forks source link

Create Viewport camera using Python Plugin #81

Open mariolino007 opened 1 year ago

mariolino007 commented 1 year ago

I am a beginner in Python programming, I am trying to create a new "camera viewport" with the following code, however I am having difficulty finding a way to assign the created viewport name, however I can't enter the X1, Y2, Z3 angles.

If anyone can help me I thank you very much.

is it possible to have more examples than those plublished at this address ? https://tmontaigu.github.io/CloudCompare-PythonPlugin/examples.html

CC & Python together are amazing !!! Congratulations !!!


import pycc
from cccorelib import CCVector3d
new_viewport_object = pycc.cc2DViewportObject()
new_viewport_parameters = new_viewport_object.getParameters()
new_camera_center = CCVector3d(2023, 301, 610)
auto_update_focal = False
new_focal_distance = 600.5
new_pivot_point = CCVector3d(2028, 305, 485)
new_viewport_parameters.setCameraCenter(new_camera_center, auto_update_focal)
new_viewport_parameters.setPivotPoint(new_pivot_point, auto_update_focal)
new_viewport_parameters.computeDistanceToHalfWidthRation()
new_viewport_parameters.computeDistanceToWidthRatio()
new_viewport_parameters.computeViewMatrix()
new_viewport_parameters.computeWidthAtFocalDist()
new_viewport_object.setParameters(new_viewport_parameters)
pycc.GetInstance().addToDB(new_viewport_object)
print("Camera Center:", new_viewport_parameters.getCameraCenter())
print("Focal Distance:", new_viewport_parameters.getFocalDistance())
print("Pivot Point:", new_viewport_parameters.getPivotPoint())
print("Distance To Half Width Ratio:", new_viewport_parameters.computeDistanceToHalfWidthRation())
print("Distance To Width Ratio:", new_viewport_parameters.computeDistanceToWidthRatio())
print("View Matrix:", new_viewport_parameters.computeViewMatrix())
print("Width At Focal Dist:", new_viewport_parameters.computeWidthAtFocalDist())
tmontaigu commented 1 year ago

setting the name is as easy as new_viewport_object.setName("my name")

mariolino007 commented 1 year ago

Thanks, But the angle X1 Y2 Z3 ?

Il sab 18 nov 2023, 13:19 tmontaigu @.***> ha scritto:

setting the name is as easy as new_viewport_object.setName("my name")

— Reply to this email directly, view it on GitHub https://github.com/tmontaigu/CloudCompare-PythonPlugin/issues/81#issuecomment-1817493832, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQRUVSBLAFRCNWFKFNLE4Q3YFCRVNAVCNFSM6AAAAAA7P224C2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMJXGQ4TGOBTGI . You are receiving this because you authored the thread.Message ID: @.***>

tmontaigu commented 1 year ago

What are these angles ?

mariolino007 commented 1 year ago

To define a camera you need an origin point, a second point indicating the direction of view of the camera and a third point indicating which is the up direction of the camera last you need the focal length of the camera. In a simpler way you need a point of origin a direction vector and a vector to indicate which is the up direction and as before the focal length. A last way is a point of origin, the focal length and the three Euler angles. The simplest system is the point of origin, the two vectors and the focal length.

Translated with www.DeepL.com/Translator (free version)

Il dom 19 nov 2023, 13:56 tmontaigu @.***> ha scritto:

What are these angles ?

— Reply to this email directly, view it on GitHub https://github.com/tmontaigu/CloudCompare-PythonPlugin/issues/81#issuecomment-1817847409, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQRUVSAQEGCXFLV2C3PSUTLYFH6XVAVCNFSM6AAAAAA7P224C2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMJXHA2DONBQHE . You are receiving this because you authored the thread.Message ID: @.***>

mariolino007 commented 1 year ago

https://www.cloudcompare.org/doc/wiki/index.php?title=Display%5CCamera_settings

tmontaigu commented 1 year ago

In that case I think the viewMat member of the paramerters could be what you are looking for

view_mat = pycc.ccGLMatrixd()
view_mat.initFromParameters(
    phi_rad=0.1,
    theta_rad=0.2,
    psi_rad=0.3,
    t3D=CCVector3d(0,0,0)
)

new_viewport_parameters.viewMat = view_mat;
mariolino007 commented 1 year ago

testing this code:

import pycc
from cccorelib import CCVector3d
new_viewport_object = pycc.cc2DViewportObject()
new_viewport_parameters = new_viewport_object.getParameters()
view_mat = pycc.ccGLMatrixd()
view_mat.initFromParameters(
    phi_rad=0.1,
    theta_rad=0.2,
    psi_rad=0.3,
    t3D=CCVector3d(2028,305,485)
)
new_focal_distance = 50
new_viewport_object.setName("camera1")
new_viewport_parameters.viewMat = view_mat;
pycc.GetInstance().addToDB(new_viewport_object)

all paramter of the viewport are all zero ....... With that modification were you able to set the angles instead ? Could you share the more complete code ?

tmontaigu commented 1 year ago

What do you mean they are zero ?

import pycc
from cccorelib import CCVector3d

new_viewport_object = pycc.cc2DViewportObject()
new_viewport_object.setName('my name')
new_viewport_parameters = new_viewport_object.getParameters()
new_camera_center = CCVector3d(2023, 301, 610)
auto_update_focal = False
new_focal_distance = 600.5
new_pivot_point = CCVector3d(2028, 305, 485)
view_mat = pycc.ccGLMatrixd()
view_mat.initFromParameters(
    phi_rad=0.1,
    theta_rad=0.2,
    psi_rad=0.3,
    t3D=CCVector3d(0,0,0)
)

new_viewport_parameters.setCameraCenter(new_camera_center, auto_update_focal)
new_viewport_parameters.setPivotPoint(new_pivot_point, auto_update_focal)
new_viewport_parameters.computeDistanceToHalfWidthRation()
new_viewport_parameters.computeDistanceToWidthRatio()
new_viewport_parameters.computeViewMatrix()
new_viewport_parameters.computeWidthAtFocalDist()
new_viewport_parameters.viewMat = view_mat;
new_viewport_parameters.setFocalDistance(new_focal_distance)

new_viewport_object.setParameters(new_viewport_parameters)

pycc.GetInstance().addToDB(new_viewport_object)

new_viewport_parameters = new_viewport_object.getParameters()

print("Camera Center:", new_viewport_parameters.getCameraCenter())
print("Focal Distance:", new_viewport_parameters.getFocalDistance())
print("Pivot Point:", new_viewport_parameters.getPivotPoint())
print("Distance To Half Width Ratio:", new_viewport_parameters.computeDistanceToHalfWidthRation())
print("Distance To Width Ratio:", new_viewport_parameters.computeDistanceToWidthRatio())
print("View Matrix:", new_viewport_parameters.computeViewMatrix())
print("Width At Focal Dist:", new_viewport_parameters.computeWidthAtFocalDist())
print("View Mat", view_mat.asArray())

I don't see any zero

mariolino007 commented 1 year ago

Now it seems to be working !!! Many thanks !!!

mariolino007 commented 1 year ago

using angles is quite complicated, I am trying to use FromViewDirAndUpDir however I have an unexpected error ?

import pycc
from cccorelib import CCVector3d
forward_vector = CCVector3d(0, 0, -1)
up_vector = CCVector3d(0, 1, 0)
rotation_matrix = pycc.ccGLMatrixd.FromViewDirAndUpDir(forward_vector, up_vector)
TypeError: Unable to convert function return value to a Python type! The signature was
    (forward: cccorelib.CCVector3d, up: cccorelib.CCVector3d) -> ccGLMatrixTpl<double>
At:
  <string>(8): <module>
tmontaigu commented 1 year ago

Indeed there is a problem with this function, I will push a fix for that

tmontaigu commented 1 year ago

Fixed with #82