From 75506df3b51ab4d0612a1437a991bcce76907a18 Mon Sep 17 00:00:00 2001
From: Rainer Kartmann <rainer.kartmann@kit.edu>
Date: Fri, 9 Oct 2020 15:53:34 +0200
Subject: [PATCH] Rename aabb() and oobb() to loadAABB/OOBB() and make them
 return std::nullopt on error (instead of throwing exception)

---
 .../ObjectPoseObserver/ObjectPoseObserver.cpp |  2 +-
 .../libraries/ArmarXObjects/ObjectInfo.cpp    | 29 +++++++++++++++----
 .../libraries/ArmarXObjects/ObjectInfo.h      | 14 +++++++--
 3 files changed, 37 insertions(+), 8 deletions(-)

diff --git a/source/RobotAPI/components/ObjectPoseObserver/ObjectPoseObserver.cpp b/source/RobotAPI/components/ObjectPoseObserver/ObjectPoseObserver.cpp
index 6db9b9ad2..cfb470f8f 100644
--- a/source/RobotAPI/components/ObjectPoseObserver/ObjectPoseObserver.cpp
+++ b/source/RobotAPI/components/ObjectPoseObserver/ObjectPoseObserver.cpp
@@ -456,7 +456,7 @@ namespace armarx
         {
             try
             {
-                oobb = objectInfo->oobb();
+                oobb = objectInfo->loadOOBB();
             }
             catch (const std::ios_base::failure& e)
             {
diff --git a/source/RobotAPI/libraries/ArmarXObjects/ObjectInfo.cpp b/source/RobotAPI/libraries/ArmarXObjects/ObjectInfo.cpp
index ac272b10a..1dc1903ea 100644
--- a/source/RobotAPI/libraries/ArmarXObjects/ObjectInfo.cpp
+++ b/source/RobotAPI/libraries/ArmarXObjects/ObjectInfo.cpp
@@ -85,11 +85,20 @@ namespace armarx
         return file(".json", "_bb");
     }
 
-    simox::AxisAlignedBoundingBox ObjectInfo::aabb() const
+    std::optional<simox::AxisAlignedBoundingBox> ObjectInfo::loadAABB() const
     {
-        nlohmann::json j = nlohmann::read_json(boundingBoxJson().absolutePath);
-        nlohmann::json jaabb = j.at("aabb");
+        nlohmann::json j;
+        try
+        {
+            j = nlohmann::read_json(boundingBoxJson().absolutePath);
+        }
+        catch (const std::exception& e)
+        {
+            ARMARX_ERROR << e.what();
+            return std::nullopt;
+        }
 
+        nlohmann::json jaabb = j.at("aabb");
         auto center = jaabb.at("center").get<Eigen::Vector3f>();
         auto extents = jaabb.at("extents").get<Eigen::Vector3f>();
         auto min = jaabb.at("min").get<Eigen::Vector3f>();
@@ -106,9 +115,19 @@ namespace armarx
         return aabb;
     }
 
-    simox::OrientedBox<float> ObjectInfo::oobb() const
+    std::optional<simox::OrientedBox<float>> ObjectInfo::loadOOBB() const
     {
-        nlohmann::json j = nlohmann::read_json(boundingBoxJson().absolutePath);
+        nlohmann::json j;
+        try
+        {
+            j = nlohmann::read_json(boundingBoxJson().absolutePath);
+        }
+        catch (const std::exception& e)
+        {
+            ARMARX_ERROR << e.what();
+            return std::nullopt;
+        }
+
         nlohmann::json joobb = j.at("oobb");
         auto pos = joobb.at("pos").get<Eigen::Vector3f>();
         auto ori = joobb.at("ori").get<Eigen::Quaternionf>().toRotationMatrix();
diff --git a/source/RobotAPI/libraries/ArmarXObjects/ObjectInfo.h b/source/RobotAPI/libraries/ArmarXObjects/ObjectInfo.h
index c141bd9d4..5f50ec956 100644
--- a/source/RobotAPI/libraries/ArmarXObjects/ObjectInfo.h
+++ b/source/RobotAPI/libraries/ArmarXObjects/ObjectInfo.h
@@ -1,6 +1,7 @@
 #pragma once
 
 #include <filesystem>
+#include <optional>
 #include <string>
 
 #include "ObjectID.h"
@@ -63,8 +64,17 @@ namespace armarx
 
         PackageFileLocation boundingBoxJson() const;
 
-        simox::AxisAlignedBoundingBox aabb() const;
-        simox::OrientedBox<float> oobb() const;
+        /**
+         * @brief Load the AABB (axis-aligned bounding-box) from the bounding box JSON file.
+         * @return Return the AABB if successful, `std::nullopt` if file does not exist.
+         */
+        std::optional<simox::AxisAlignedBoundingBox> loadAABB() const;
+        /**
+         * @brief Load the OOBB (object-oriented bounding box) from the bounding box JSON file.
+         * The OOBB is defined the object's local frame.
+         * @return Return the OOBB if successful, `std::nullopt` if file does not exist.
+         */
+        std::optional<simox::OrientedBox<float>> loadOOBB() const;
 
 
         /**
-- 
GitLab