[libcamera-devel,v6,07/12] libcamera: transform: Add operations with Orientation
diff mbox series

Message ID 20231019140133.32090-8-jacopo.mondi@ideasonboard.com
State Accepted
Headers show
Series
  • libcamera: Replace CameraConfiguration::transform
Related show

Commit Message

Jacopo Mondi Oct. 19, 2023, 2:01 p.m. UTC
Add two operations that allows to combine Transform with Orientation.

- Transform operator/(const Orientation &o1, const Orientation &o2)
  allows to easily get back the Transform that needs to be applied to
  Orientation2 to get Orientation1

- Orientation operator*(const Orientation &o, const Transform &t)
  allows to apply a Transform to an Orientation and obtain the
  combination of the two

These two operations allow applications to use Transforms to
manipulate the Orientation inside the CameraConfiguration, if they
wish.

Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
Reviewed-by: David Plowman <david.plowman@raspberrypi.com>
---
 include/libcamera/transform.h |  3 +++
 src/libcamera/transform.cpp   | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)

Patch
diff mbox series

diff --git a/include/libcamera/transform.h b/include/libcamera/transform.h
index 14f1db0e8572..847a1f94713c 100644
--- a/include/libcamera/transform.h
+++ b/include/libcamera/transform.h
@@ -74,6 +74,9 @@  Transform transformFromRotation(int angle, bool *success = nullptr);
 Transform transformFromOrientation(const Orientation &orientation);
 Orientation transformToOrientation(const Transform &transform);
 
+Transform operator/(const Orientation &o1, const Orientation &o2);
+Orientation operator*(const Orientation &o1, const Transform &t);
+
 const char *transformToString(Transform t);
 
 } /* namespace libcamera */
diff --git a/src/libcamera/transform.cpp b/src/libcamera/transform.cpp
index 38735c525aa3..85bc2d28b0cd 100644
--- a/src/libcamera/transform.cpp
+++ b/src/libcamera/transform.cpp
@@ -362,6 +362,39 @@  Orientation transformToOrientation(const Transform &transform)
 	return Orientation::Rotate0;
 }
 
+/**
+ * \brief Return the Transform that applied to \a o2 gives \a o1
+ * \param o1 The Orientation to obtain
+ * \param o2 The base Orientation
+ *
+ * This operation can be used to easily compute the Transform to apply to a
+ * base orientation \a o2 to get the desired orientation \a o1.
+ *
+ * \return A Transform that applied to \a o2 gives \a o1
+ */
+Transform operator/(const Orientation &o1, const Orientation &o2)
+{
+	Transform t1 = transformFromOrientation(o1);
+	Transform t2 = transformFromOrientation(o2);
+
+	return -t2 * t1;
+}
+
+/**
+ * \brief Apply the Transform \a t on the orientation \a o
+ * \param o The orientation
+ * \param t The transform to apply on \a o
+ * \return The Orientation resulting from applying \a t on \a o
+ */
+Orientation operator*(const Orientation &o, const Transform &t)
+{
+	/*
+	 * Apply a Transform corresponding to the orientation first and
+	 * then apply \a t to it.
+	 */
+	return transformToOrientation(transformFromOrientation(o) * t);
+}
+
 /**
  * \brief Return a character string describing the transform
  * \param[in] t The transform to be described.