View Issue Details

IDProjectCategoryView StatusLast Update
0001497DrawingPatchpublic2014-04-02 10:09
Reportergalou_breizh Assigned Towmayer  
PrioritynormalSeverityfeatureReproducibilityN/A
Status closedResolutionfixed 
Product Versiontrunk 
Summary0001497: Implement getCameraOrientation similarly to getViewDirection
DescriptionImplement getViewRotation and its Python bindings, so that Gui.ActiveDocument.ActiveView.getViewRotation() returns the rotation angle for the 3D view in radians.

Patched from github master as of 2014-03-26.
Additional InformationThere exists a function Gui.ActiveDocument.ActiveView.viewPosition() both functions return the same angle but the returned axis is different!
TagsNo tags attached.

Activities

galou_breizh

2014-03-27 08:24

reporter  

0001-Implement-getViewRotation.patch (3,954 bytes)   
From ff32ddbdc527d21b64f048a4ae5fe793f9436b09 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ga=C3=ABl=20Ecorchard?= <galou_breizh@yahoo.fr>
Date: Thu, 27 Mar 2014 09:17:12 +0100
Subject: [PATCH] Implement getViewRotation

Implement getViewRotation and its Python bindings, so that
Gui.ActiveDocument.ActiveView.getViewRotation() returns the rotation
angle for the 3D view in radians.
---
 src/Gui/View3DInventorViewer.cpp | 11 +++++++++++
 src/Gui/View3DInventorViewer.h   |  3 +++
 src/Gui/View3DPy.cpp             | 20 ++++++++++++++++++++
 src/Gui/View3DPy.h               |  1 +
 4 files changed, 35 insertions(+)

diff --git a/src/Gui/View3DInventorViewer.cpp b/src/Gui/View3DInventorViewer.cpp
index efff055..b78eec0 100644
--- a/src/Gui/View3DInventorViewer.cpp
+++ b/src/Gui/View3DInventorViewer.cpp
@@ -1286,6 +1286,17 @@ SbVec3f View3DInventorViewer::getViewDirection() const
     return lookat;
 }
 
+double View3DInventorViewer::getViewRotation() const
+{
+    SoCamera* cam = this->getCamera();
+    if (!cam) return 0.0; // this is the default
+    SbRotation camrot = cam->orientation.getValue();
+    SbVec3f axis;
+    float angle;
+    camrot.getValue(axis, angle);
+    return angle;
+}
+
 SbVec3f View3DInventorViewer::getUpDirection() const
 {
     SoCamera* cam = this->getCamera();
diff --git a/src/Gui/View3DInventorViewer.h b/src/Gui/View3DInventorViewer.h
index 7389506..79721cd 100644
--- a/src/Gui/View3DInventorViewer.h
+++ b/src/Gui/View3DInventorViewer.h
@@ -228,6 +228,9 @@ public:
      * The vector is normalized to length of 1.
      */
     SbVec3f getViewDirection() const;
+    /** Returns the view rotation
+     */
+    double getViewRotation() const; 
     /** Returns the up direction */
     SbVec3f getUpDirection() const;
     /** Returns the 3d point on the focal plane to the given 2d point. */
diff --git a/src/Gui/View3DPy.cpp b/src/Gui/View3DPy.cpp
index ec29f72..64afcce 100644
--- a/src/Gui/View3DPy.cpp
+++ b/src/Gui/View3DPy.cpp
@@ -100,6 +100,7 @@ void View3DInventorPy::init_type()
     add_varargs_method("getCamera",&View3DInventorPy::getCamera,"getCamera()");
     add_varargs_method("getCameraNode",&View3DInventorPy::getCameraNode,"getCameraNode()");
     add_varargs_method("getViewDirection",&View3DInventorPy::getViewDirection,"getViewDirection()");
+    add_varargs_method("getViewRotation",&View3DInventorPy::getViewRotation,"getViewRotation()");
     add_varargs_method("setCamera",&View3DInventorPy::setCamera,"setCamera()");
     add_varargs_method("setCameraOrientation",&View3DInventorPy::setCameraOrientation,"setCameraOrientation()");
     add_varargs_method("getCameraType",&View3DInventorPy::getCameraType,"getCameraType()");
@@ -807,6 +808,25 @@ Py::Object View3DInventorPy::getViewDirection(const Py::Tuple& args)
     }
 }
 
+Py::Object View3DInventorPy::getViewRotation(const Py::Tuple& args)
+{
+    if (!PyArg_ParseTuple(args.ptr(), ""))
+        throw Py::Exception();
+    try {
+        double rot = _view->getViewer()->getViewRotation();
+        return Py::Float(rot);
+    }
+    catch (const Base::Exception& e) {
+        throw Py::Exception(e.what());
+    }
+    catch (const std::exception& e) {
+        throw Py::Exception(e.what());
+    }
+    catch(...) {
+        throw Py::Exception("Unknown C++ exception");
+    }
+}
+
 
 Py::Object View3DInventorPy::setCamera(const Py::Tuple& args)
 {
diff --git a/src/Gui/View3DPy.h b/src/Gui/View3DPy.h
index 657bee9..f86651b 100644
--- a/src/Gui/View3DPy.h
+++ b/src/Gui/View3DPy.h
@@ -73,6 +73,7 @@ public:
     Py::Object saveVectorGraphic(const Py::Tuple&);
     Py::Object getCamera(const Py::Tuple&);
     Py::Object getViewDirection(const Py::Tuple&);
+    Py::Object getViewRotation(const Py::Tuple&);
     Py::Object setCamera(const Py::Tuple&);
     Py::Object setCameraOrientation(const Py::Tuple&);
     Py::Object getCameraType(const Py::Tuple&);
-- 
1.8.3.2

wmayer

2014-03-29 13:10

administrator   ~0004510

Is there any use case to have only the angle of the rotation without the axis?

And also the method name "getViewRotation()" does not fit to what it does because a rotation can be described by angle *and* axis (or quaternion or a matrix). An angle alone is more or less useless.

IMO, this method makes only sense when returning the SbRotation.

galou_breizh

2014-03-29 22:24

reporter   ~0004519

I agree with you that Rotation is actually usually though as SbRotation. In the Drawing workbench, there is a Direction parameter which is a 3-element vector and a Rotation, which is the angle, thus a float. This was the reason why I named the function getViewRotation.

I needed this function to write a macro that creates a new Drawing view which is the same as the current 3D view, cf. http://forum.freecadweb.org/viewtopic.php?f=22&t=6171

If we write the function getViewRotation that would return a SbRotation, then the function getViewDirection becomes redundant. Any thought? Another alternative would be getViewAngle, but I don't find it clear because it's not what is usually understood with view angle.

wmayer

2014-03-31 09:23

administrator   ~0004522

> then the function getViewDirection becomes redundant. Any thought?
In a certain way it becomes indeed redundant but I consider this a convenience method because it's not that obvious to apply the quaternion on the vector (0,0,-1) to get the current view direction.

So, if you don't mind then I would change your patch to return directly the SbRotation because this is the more flexible approach. And to extract the angle there you only need two little extra lines in the client code.

galou_breizh

2014-03-31 21:05

reporter   ~0004523

Do you mean that getViewDirection will return the axis from the (axis, angle) notation of the quaternion returned by getViewRotation?
If this is the case, then I don't see the point (except for backward compatibility) to have getViewDirection which can be obtained by "getViewRotation().Axis" in the Python client code, and similar in C++.

wmayer

2014-04-01 15:18

administrator   ~0004524

> Do you mean that getViewDirection will return the axis from the (axis, angle)
No, getViewDirection() gives you the direction from the user looking at the screen expressed in world coordinates.

The axis you get with getViewRotation() is the rotation axis.

Btw, instead of getViewRotation() I would rename the method to getViewOrientation() because this fits to the Coin/SoQt API.

galou_breizh

2014-04-01 19:33

reporter   ~0004525

Ok, this sounds good.

Actually I also needed the vector from the camera to the origin and had to invert getViewDirection for my needs. So this is perfect for me.

wmayer

2014-04-02 09:39

administrator   ~0004527

A little addition: I realized that the name getCameraOrientation() fits better because there is already a method setCameraOrientation() which is the semantic counterpart.

Related Changesets

FreeCAD: master 25d0242f

2014-04-02 11:40:36

wmayer

Details Diff
+ fixes 0001497: Implement getCameraOrientation similarly to getViewDirection Affected Issues
0001497
mod - src/Gui/View3DInventorViewer.cpp Diff File
mod - src/Gui/View3DInventorViewer.h Diff File
mod - src/Gui/View3DPy.cpp Diff File
mod - src/Gui/View3DPy.h Diff File

Issue History

Date Modified Username Field Change
2014-03-27 08:24 galou_breizh New Issue
2014-03-27 08:24 galou_breizh File Added: 0001-Implement-getViewRotation.patch
2014-03-29 13:10 wmayer Note Added: 0004510
2014-03-29 13:24 wmayer Status new => feedback
2014-03-29 22:24 galou_breizh Note Added: 0004519
2014-03-29 22:24 galou_breizh Status feedback => new
2014-03-31 09:23 wmayer Note Added: 0004522
2014-03-31 21:05 galou_breizh Note Added: 0004523
2014-04-01 15:18 wmayer Note Added: 0004524
2014-04-01 19:33 galou_breizh Note Added: 0004525
2014-04-02 09:39 wmayer Note Added: 0004527
2014-04-02 09:39 wmayer Summary Implement getViewRotation similarly to getViewDirection => Implement getCameraOrientation similarly to getViewDirection
2014-04-02 10:09 wmayer Changeset attached => FreeCAD Master master 25d0242f
2014-04-02 10:09 wmayer Assigned To => wmayer
2014-04-02 10:09 wmayer Status new => closed
2014-04-02 10:09 wmayer Resolution open => fixed