From 7ddce0cecd40aa374243797d75812711da49f291 Mon Sep 17 00:00:00 2001
From: Vanessa Karolek <vaka1997@uni-koblenz.de>
Date: Wed, 23 Jun 2021 17:58:42 +0200
Subject: [PATCH] [#60] avoid if branching for speed up

---
 .../src/vkcv/camera/PilotCameraController.cpp | 23 ++++++--------
 .../vkcv/camera/TrackballCameraController.cpp | 30 +++++++------------
 2 files changed, 19 insertions(+), 34 deletions(-)

diff --git a/modules/camera/src/vkcv/camera/PilotCameraController.cpp b/modules/camera/src/vkcv/camera/PilotCameraController.cpp
index 8f3e65b6..faea9c19 100644
--- a/modules/camera/src/vkcv/camera/PilotCameraController.cpp
+++ b/modules/camera/src/vkcv/camera/PilotCameraController.cpp
@@ -39,24 +39,19 @@ namespace vkcv::camera {
     }
 
     void PilotCameraController::panView(double xOffset, double yOffset, Camera &camera) {
-        // handle yaw rotation
-        float yaw = camera.getYaw() + xOffset;
-        if (yaw < -180.0f) {
-            yaw += 360.0f;
-        }
-        else if (yaw > 180.0f) {
-            yaw -= 360.0f;
+        // update only if there is (valid) input
+        if (xOffset == 0.0 && yOffset == 0.0) {
+            return;
         }
+
+        // handle yaw rotation
+        float yaw = camera.getYaw() + static_cast<float>(xOffset);
+        yaw += 360.0f * (yaw < -180.0f) - 360.0f * (yaw > 180.0f);
         camera.setYaw(yaw);
 
         // handle pitch rotation
-        float pitch = camera.getPitch() - yOffset;
-        if (pitch > 89.0f) {
-            pitch = 89.0f;
-        }
-        if (pitch < -89.0f) {
-            pitch = -89.0f;
-        }
+        float pitch = camera.getPitch() - static_cast<float>(yOffset);
+        pitch = glm::clamp(pitch, -89.0f, 89.0f);
         camera.setPitch(pitch);
     }
     
diff --git a/modules/camera/src/vkcv/camera/TrackballCameraController.cpp b/modules/camera/src/vkcv/camera/TrackballCameraController.cpp
index 81a7e1d6..5d6a3859 100644
--- a/modules/camera/src/vkcv/camera/TrackballCameraController.cpp
+++ b/modules/camera/src/vkcv/camera/TrackballCameraController.cpp
@@ -12,33 +12,23 @@ namespace vkcv::camera {
     }
 
     void TrackballCameraController::setRadius(const float radius) {
-        if (radius < 0.1f) {
-            m_radius = 0.1f;
-        }
-        else {
-            m_radius = radius;
-        }
+        m_radius = 0.1f * (radius < 0.1f) + radius * (1 - (radius < 0.1f));
     }
 
     void TrackballCameraController::panView(double xOffset, double yOffset, Camera &camera) {
-        // handle yaw rotation
-        float yaw = camera.getYaw() + xOffset * m_cameraSpeed;
-        if (yaw < 0.0f) {
-            yaw += 360.0f;
-        }
-        else if (yaw > 360.0f) {
-            yaw -= 360.0f;
+        // update only if there is (valid) input
+        if (xOffset == 0.0 && yOffset == 0.0) {
+            return;
         }
+
+        // handle yaw rotation
+        float yaw = camera.getYaw() + static_cast<float>(xOffset) * m_cameraSpeed;
+        yaw += 360.0f * (yaw < 0.0f) - 360.0f * (yaw > 360.0f);
         camera.setYaw(yaw);
 
         // handle pitch rotation
-        float pitch = camera.getPitch() + yOffset * m_cameraSpeed;
-        if (pitch < 0.0f) {
-            pitch += 360.0f;
-        }
-        else if (pitch > 360.0f) {
-            pitch -= 360.0f;
-        }
+        float pitch = camera.getPitch() + static_cast<float>(yOffset) * m_cameraSpeed;
+        pitch += 360.0f * (pitch < 0.0f) - 360.0f * (pitch > 360.0f);
         camera.setPitch(pitch);
     }
 
-- 
GitLab