几何向量:Angle/SignedAngle函数解析

博客主要围绕二维和三维开发中向量夹角计算展开。介绍了用余弦定理和点乘计算夹角,还提及在Unity中实现相关功能。指出Angle函数不带顺逆时针判断,讲解了通过叉积判断顺逆,以及Vector3.SingedAngle函数的理解和设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      最近又跑去温习基础数序去了,没办法,人对某个事物的永久记忆是七次理解才能达成,所以抽空写一些常用的数学计算。

      在二维和三维开发中,计算向量之间夹角属于很常见的操作,在数学中我们可以使用下面:

       1.余弦定理,如果我们知道三边的情况下,使用余弦定理可以计算出任意角的角度,如图:      

         

        2.点乘(点积),我们可以通过点乘(点积)推导出:

               a·b = |a|*|b|*cosθ

               cosθ = (a·b)/(|a|*|b|)

        可以回过头去看下上面的公式定理和推导,加深记忆,接下来我们就在unity中实现一下,代码如下:

        

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AngleTest : MonoBehaviour
{

    void Start()
    {
        CalculateVector2Angle();
        CalculateVector3Angle();
    }

    private void CalculateVector2Angle()
    {
        Vector2 a = new Vector2(1, 3);
        Vector2 b = new Vector2(2, 8);

        Vector2 c = b - a;

        float apiangle = Vector2.Angle(a, b);
        float cosangle = Mathf.Acos((a.x * a.x + a.y * a.y + b.x * b.x + b.y * b.y - c.x * c.x-c.y*c.y) / (2 * Mathf.Sqrt(a.x * a.x + a.y * a.y) * Mathf.Sqrt(b.x * b.x + b.y * b.y))) * Mathf.Rad2Deg;
        float dotangle = Mathf.Acos((a.x * b.x + a.y * b.y) / (Mathf.Sqrt(a.x * a.x + a.y * a.y) * Mathf.Sqrt(b.x * b.x + b.y * b.y))) * Mathf.Rad2Deg;
#if UNITY_EDITOR
        Debug.LogFormat("vector2 apiangle = {0} cosangle = {1} dotangle = {2}", apiangle, cosangle, dotangle);
#endif
    }

    private void CalculateVector3Angle()
    {
        Vector3 a = new Vector3(1, 3, 5);
        Vector3 b = new Vector3(2, 8, 4);

        Vector3 c = b - a;

        float apiangle = Vector3.Angle(a, b);
        float cosangle = Mathf.Acos((a.x * a.x + a.y * a.y + a.z * a.z + b.x * b.x + b.y * b.y + b.z * b.z - c.x * c.x - c.y * c.y - c.z * c.z) / (2 * Mathf.Sqrt(a.x * a.x + a.y * a.y + a.z * a.z) * Mathf.Sqrt(b.x * b.x + b.y * b.y + b.z * b.z))) * Mathf.Rad2Deg;
        float dotangle = Mathf.Acos((a.x * b.x + a.y * b.y + a.z * b.z) / (Mathf.Sqrt(a.x * a.x + a.y * a.y + a.z * a.z) * Mathf.Sqrt(b.x * b.x + b.y * b.y + b.z * b.z))) * Mathf.Rad2Deg;
#if UNITY_EDITOR
        Debug.LogFormat("vector3 apiangle = {0} cosangle = {1} dotangle = {2}", apiangle, cosangle, dotangle);
#endif
    }
}

        

      因为我们对几何公式的理解,所以很容易实现这个Angle的功能,如果你细心你会发现一点,这个Angle函数不带顺逆时针旋转,也就是不带正负号。

      如果你是认真学习了三角函数,那么应该知道,在二维坐标系中,逆时针旋转方向的夹角为正角,如下图:

      

     那么三维中呢?这里有个叫做左手旋转定则的规范,文字描叙就是:伸出左手,大拇指翘起,四指握拳,那么大拇指指向旋转轴方向(比如Z轴),则其余四指旋转方向就是正角方向。改造一下上面的二维坐标系旋转夹角标识图到三维则如下:

      

      拿左手比划一下就看得出来了,那么我们可以默认认为,一般情况下,我们做二维中角度相关计算,虚拟Z轴指向纸内。

      回到需要谈论的问题,那就是Angle计算是不带顺逆时针判断的,也就是不带正负号,那么我们需要构建一个函数,它既计算了夹角,又判断了顺逆时针方向。

      unity提供了我们Vector.SingedAngle这个API去获取带正负号的夹角。

      如果我们设计这个函数的话,用叉积很方便,因为叉积就是判断顺逆时针旋转的,想象一下Vector2扩展出Z轴,同时两个Vector2向量a和b的扩展出的Vector3向量a'和b'的叉积向量c,那么向量c的z值正负号可以用来判断夹角的正负号,使用上面说的左手规则即可。函数设计如下:

       

private void CalculateVector2SingedAngle(Vector2 a, Vector2 b)
    {
        float apiangle = Vector2.SignedAngle(a, b);

        Vector3 a3d = new Vector3(a.x, a.y, 0);
        Vector3 b3d = new Vector3(b.x, b.y, 0);

        float crossangle = Vector2.Angle(a, b) * Mathf.Sign(Vector3.Cross(a3d, b3d).z);

#if UNITY_EDITOR
        Debug.LogFormat("vector2 apiangle = {0} crossangle = {1}", apiangle, crossangle);
#endif
    }

void Start()
    {
        Vector2 a = new Vector2(1, 3);
        Vector2 b = new Vector2(2, 8);
        CalculateVector2SingedAngle(a,b);
        a = new Vector2(1, 7);
        b = new Vector2(2, 8);
        CalculateVector2SingedAngle(a, b);
        a = new Vector2(-1, -7);
        b = new Vector2(2, 8);
        CalculateVector2SingedAngle(a, b);
    }

     

   那么Vector3.SingedAngle怎么去理解呢?先看下官方的函数设计,如下:

   

       看得出来吧,因为三维向量可以是任意朝向的,那么我们就必须限定一个旋转轴,不然我们怎么确定左手规则的大拇指朝向呢?那么我们来设计这个函数,两个Vector3向量a和b,他们的叉积向量c,我们就把向量c当做旋转轴,那么永远是正角,因为向量c是通过ab叉积反推出来的,向量c的左手规则四指旋转方向就是a到b的夹角正方向,所以如下:

       

private void CalculateVector3SingedAngle(Vector3 a, Vector3 b)
    {
        Vector3 cross = Vector3.Cross(a, b);
        Vector3 axis = cross;
        float apiangle = Vector3.SignedAngle(a, b, axis);
        float crossangle = Vector3.Angle(a, b);
#if UNITY_EDITOR
        Debug.LogFormat("vector3 apiangle = {0} crossangle = {1}", apiangle, crossangle);
#endif
    }

void Start()
    {
        Vector3 a = new Vector3(1, 3, 10);
        Vector3 b = new Vector3(2, 8, 4);
        CalculateVector3SingedAngle(a, b);
        a = new Vector3(1, 5, 2);
        b = new Vector3(2, 8, 5);
        CalculateVector3SingedAngle(a, b);
        a = new Vector3(-1, -7, -10);
        b = new Vector3(2, 4, -9);
        CalculateVector3SingedAngle(a, b);
        a = new Vector3(-1, 7, 8);
        b = new Vector3(2, -3, -5);
        CalculateVector3SingedAngle(a, b);
    }

       

        那如果我们使用其他旋转轴去当做axis参数呢?就需要判断axis和cross的夹角是否大于无符号90°,大于说明旋转相反了。函数设计如下:

     

 private void CalculateVector3SingedAngle(Vector3 a, Vector3 b)
    {
        Vector3 cross = Vector3.Cross(a, b);
        Vector3 axis = new Vector3(1, -5, 4);
        float apiangle = Vector3.SignedAngle(a, b, axis);
        float crossangle = Vector3.Angle(a, b) * Mathf.Sign(90 - Vector3.Angle(cross, axis));
#if UNITY_EDITOR
        Debug.LogFormat("vector3 apiangle = {0} crossangle = {1}", apiangle, crossangle);
#endif
    }

      

        稍微画一个示意图吧,如下:

    

     使用左手规则比划一下是不是发现,旋转方向随着θ和β角度不同而相反。

     好了,偶尔写一篇几何数学函数详解,顺便学英语。

Starting >>> image_subscriber_cpp --- stderr: image_subscriber_cpp cc1plus: warning: /opt/ros/humble/lib/libtf2_ros.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcl_interfaces__rosidl_generator_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcl_interfaces__rosidl_typesupport_fastrtps_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcl_interfaces__rosidl_typesupport_introspection_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcl_interfaces__rosidl_typesupport_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcl_interfaces__rosidl_typesupport_fastrtps_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcl_interfaces__rosidl_typesupport_introspection_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcl_interfaces__rosidl_typesupport_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcl_interfaces__rosidl_generator_py.so: not a directory cc1plus: warning: /usr/lib/x86_64-linux-gnu/libpython3.10.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librosidl_typesupport_fastrtps_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librmw.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librosidl_typesupport_fastrtps_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcutils.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcpputils.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librosidl_typesupport_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librosidl_typesupport_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librosidl_runtime_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librosidl_typesupport_introspection_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librosidl_typesupport_introspection_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libcomposition_interfaces__rosidl_generator_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libcomposition_interfaces__rosidl_typesupport_fastrtps_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libcomposition_interfaces__rosidl_typesupport_introspection_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libcomposition_interfaces__rosidl_typesupport_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libcomposition_interfaces__rosidl_typesupport_fastrtps_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libcomposition_interfaces__rosidl_typesupport_introspection_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libcomposition_interfaces__rosidl_typesupport_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libcomposition_interfaces__rosidl_generator_py.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librclcpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libcomponent_manager.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libbuiltin_interfaces__rosidl_generator_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libbuiltin_interfaces__rosidl_typesupport_fastrtps_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libbuiltin_interfaces__rosidl_typesupport_introspection_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libbuiltin_interfaces__rosidl_typesupport_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libbuiltin_interfaces__rosidl_typesupport_fastrtps_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libbuiltin_interfaces__rosidl_typesupport_introspection_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libbuiltin_interfaces__rosidl_typesupport_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libbuiltin_interfaces__rosidl_generator_py.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libgeometry_msgs__rosidl_generator_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libgeometry_msgs__rosidl_typesupport_fastrtps_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libgeometry_msgs__rosidl_typesupport_introspection_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libgeometry_msgs__rosidl_typesupport_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libgeometry_msgs__rosidl_typesupport_fastrtps_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libgeometry_msgs__rosidl_typesupport_introspection_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libgeometry_msgs__rosidl_typesupport_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libgeometry_msgs__rosidl_generator_py.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libmessage_filters.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libaction_msgs__rosidl_generator_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libaction_msgs__rosidl_typesupport_fastrtps_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libaction_msgs__rosidl_typesupport_fastrtps_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libaction_msgs__rosidl_typesupport_introspection_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libaction_msgs__rosidl_typesupport_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libaction_msgs__rosidl_typesupport_introspection_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libaction_msgs__rosidl_typesupport_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libaction_msgs__rosidl_generator_py.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcl.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcl_action.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librclcpp_action.so: not a directory cc1plus: warning: /usr/lib/x86_64-linux-gnu/libconsole_bridge.so.1.0: not a directory cc1plus: warning: /opt/ros/humble/lib/libtf2.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libstd_msgs__rosidl_generator_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libstd_msgs__rosidl_typesupport_fastrtps_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libstd_msgs__rosidl_typesupport_fastrtps_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libstd_msgs__rosidl_typesupport_introspection_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libstd_msgs__rosidl_typesupport_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libstd_msgs__rosidl_typesupport_introspection_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libstd_msgs__rosidl_typesupport_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libstd_msgs__rosidl_generator_py.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libtf2_msgs__rosidl_generator_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libtf2_msgs__rosidl_typesupport_fastrtps_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libtf2_msgs__rosidl_typesupport_introspection_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libtf2_msgs__rosidl_typesupport_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libtf2_msgs__rosidl_typesupport_fastrtps_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libtf2_msgs__rosidl_typesupport_introspection_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libtf2_msgs__rosidl_typesupport_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libtf2_msgs__rosidl_generator_py.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libsensor_msgs__rosidl_generator_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libsensor_msgs__rosidl_typesupport_fastrtps_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libsensor_msgs__rosidl_typesupport_fastrtps_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libsensor_msgs__rosidl_typesupport_introspection_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libsensor_msgs__rosidl_typesupport_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libsensor_msgs__rosidl_typesupport_introspection_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libsensor_msgs__rosidl_typesupport_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libsensor_msgs__rosidl_generator_py.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libvisualization_msgs__rosidl_generator_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libvisualization_msgs__rosidl_typesupport_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libvisualization_msgs__rosidl_typesupport_cpp.so: not a directory /home/lizhuo/ros2_ws/image_subscriber_cpp/src/image_subscriber.cpp: In member function ‘void ImageSubscriber::image_callback(sensor_msgs::msg::Image_<std::allocator<void> >::ConstSharedPtr)’: /home/lizhuo/ros2_ws/image_subscriber_cpp/src/image_subscriber.cpp:110:24: warning: unused variable ‘armor’ [-Wunused-variable] 110 | for (auto& armor : armors) { | ^~~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/c++allocator.h:33, from /usr/include/c++/11/bits/allocator.h:46, from /usr/include/c++/11/memory:64, from /opt/ros/humble/include/rclcpp/rclcpp/rclcpp.hpp:153, from /home/lizhuo/ros2_ws/image_subscriber_cpp/src/image_subscriber.cpp:1: /usr/include/c++/11/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = cv::RotatedRect; _Args = {cv::RotatedRect&, cv::RotatedRect&}; _Tp = cv::RotatedRect]’: /usr/include/c++/11/bits/alloc_traits.h:516:17: required from ‘static void std::allocator_traits<std::allocator<_Tp1> >::construct(std::allocator_traits<std::allocator<_Tp1> >::allocator_type&, _Up*, _Args&& ...) [with _Up = cv::RotatedRect; _Args = {cv::RotatedRect&, cv::RotatedRect&}; _Tp = cv::RotatedRect; std::allocator_traits<std::allocator<_Tp1> >::allocator_type = std::allocator<cv::RotatedRect>]’ /usr/include/c++/11/bits/vector.tcc:115:30: required from ‘std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {cv::RotatedRect&, cv::RotatedRect&}; _Tp = cv::RotatedRect; _Alloc = std::allocator<cv::RotatedRect>; std::vector<_Tp, _Alloc>::reference = cv::RotatedRect&]’ /home/lizhuo/ros2_ws/image_subscriber_cpp/src/image_subscriber.cpp:106:40: required from here /usr/include/c++/11/ext/new_allocator.h:162:11: error: no matching function for call to ‘cv::RotatedRect::RotatedRect(cv::RotatedRect&, cv::RotatedRect&)’ 162 | { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /usr/include/opencv4/opencv2/core.hpp:57, from /usr/include/opencv4/opencv2/core/core.hpp:48, from /opt/ros/humble/include/cv_bridge/cv_bridge/cv_bridge.h:43, from /home/lizhuo/ros2_ws/image_subscriber_cpp/src/image_subscriber.cpp:3: /usr/include/opencv4/opencv2/core/types.hpp:535:5: note: candidate: ‘cv::RotatedRect::RotatedRect(const Point2f&, const Point2f&, const Point2f&)’ 535 | RotatedRect(const Point2f& point1, const Point2f& point2, const Point2f& point3); | ^~~~~~~~~~~ /usr/include/opencv4/opencv2/core/types.hpp:535:5: note: candidate expects 3 arguments, 2 provided In file included from /usr/include/opencv4/opencv2/core.hpp:57, from /usr/include/opencv4/opencv2/core/core.hpp:48, from /opt/ros/humble/include/cv_bridge/cv_bridge/cv_bridge.h:43, from /home/lizhuo/ros2_ws/image_subscriber_cpp/src/image_subscriber.cpp:3: /usr/include/opencv4/opencv2/core/types.hpp:2080:1: note: candidate: ‘cv::RotatedRect::RotatedRect(const Point2f&, const Size2f&, float)’ 2080 | RotatedRect::RotatedRect(const Point2f& _center, const Size2f& _size, float _angle) | ^~~~~~~~~~~ /usr/include/opencv4/opencv2/core/types.hpp:2080:1: note: candidate expects 3 arguments, 2 provided /usr/include/opencv4/opencv2/core/types.hpp:2076:1: note: candidate: ‘cv::RotatedRect::RotatedRect()’ 2076 | RotatedRect::RotatedRect() | ^~~~~~~~~~~ /usr/include/opencv4/opencv2/core/types.hpp:2076:1: note: candidate expects 0 arguments, 2 provided In file included from /usr/include/opencv4/opencv2/core.hpp:57, from /usr/include/opencv4/opencv2/core/core.hpp:48, from /opt/ros/humble/include/cv_bridge/cv_bridge/cv_bridge.h:43, from /home/lizhuo/ros2_ws/image_subscriber_cpp/src/image_subscriber.cpp:3: /usr/include/opencv4/opencv2/core/types.hpp:519:18: note: candidate: ‘cv::RotatedRect::RotatedRect(const cv::RotatedRect&)’ 519 | class CV_EXPORTS RotatedRect | ^~~~~~~~~~~ /usr/include/opencv4/opencv2/core/types.hpp:519:18: note: candidate expects 1 argument, 2 provided /usr/include/opencv4/opencv2/core/types.hpp:519:18: note: candidate: ‘cv::RotatedRect::RotatedRect(cv::RotatedRect&&)’ /usr/include/opencv4/opencv2/core/types.hpp:519:18: note: candidate expects 1 argument, 2 provided gmake[2]: *** [CMakeFiles/image_subscriber.dir/build.make:76:CMakeFiles/image_subscriber.dir/src/image_subscriber.cpp.o] 错误 1 gmake[1]: *** [CMakeFiles/Makefile2:137:CMakeFiles/image_subscriber.dir/all] 错误 2 gmake: *** [Makefile:146:all] 错误 2 --- Failed <<< image_subscriber_cpp [21.9s, exited with code 2] Summary: 0 packages finished [23.1s] 1 package failed: image_subscriber_cpp 1 package had stderr output: image_subscriber_cpp lizhuo@lizhuo-virtual-machine:~/ros2_ws$ sudo apt install g++ [sudo] lizhuo 的密码: 正在读取软件包列表... 完成 正在分析软件包的依赖关系树... 完成 正在读取状态信息... 完成 g++ 已经是最新版 (4:11.2.0-1ubuntu1)。 升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。 lizhuo@lizhuo-virtual-machine:~/ros2_ws$ colcon build Starting >>> image_subscriber_cpp --- stderr: image_subscriber_cpp cc1plus: warning: /opt/ros/humble/lib/libtf2_ros.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcl_interfaces__rosidl_generator_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcl_interfaces__rosidl_typesupport_fastrtps_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcl_interfaces__rosidl_typesupport_introspection_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcl_interfaces__rosidl_typesupport_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcl_interfaces__rosidl_typesupport_fastrtps_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcl_interfaces__rosidl_typesupport_introspection_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcl_interfaces__rosidl_typesupport_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcl_interfaces__rosidl_generator_py.so: not a directory cc1plus: warning: /usr/lib/x86_64-linux-gnu/libpython3.10.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librosidl_typesupport_fastrtps_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librmw.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librosidl_typesupport_fastrtps_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcutils.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcpputils.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librosidl_typesupport_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librosidl_typesupport_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librosidl_runtime_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librosidl_typesupport_introspection_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librosidl_typesupport_introspection_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libcomposition_interfaces__rosidl_generator_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libcomposition_interfaces__rosidl_typesupport_fastrtps_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libcomposition_interfaces__rosidl_typesupport_introspection_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libcomposition_interfaces__rosidl_typesupport_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libcomposition_interfaces__rosidl_typesupport_fastrtps_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libcomposition_interfaces__rosidl_typesupport_introspection_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libcomposition_interfaces__rosidl_typesupport_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libcomposition_interfaces__rosidl_generator_py.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librclcpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libcomponent_manager.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libbuiltin_interfaces__rosidl_generator_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libbuiltin_interfaces__rosidl_typesupport_fastrtps_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libbuiltin_interfaces__rosidl_typesupport_introspection_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libbuiltin_interfaces__rosidl_typesupport_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libbuiltin_interfaces__rosidl_typesupport_fastrtps_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libbuiltin_interfaces__rosidl_typesupport_introspection_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libbuiltin_interfaces__rosidl_typesupport_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libbuiltin_interfaces__rosidl_generator_py.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libgeometry_msgs__rosidl_generator_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libgeometry_msgs__rosidl_typesupport_fastrtps_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libgeometry_msgs__rosidl_typesupport_introspection_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libgeometry_msgs__rosidl_typesupport_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libgeometry_msgs__rosidl_typesupport_fastrtps_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libgeometry_msgs__rosidl_typesupport_introspection_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libgeometry_msgs__rosidl_typesupport_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libgeometry_msgs__rosidl_generator_py.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libmessage_filters.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libaction_msgs__rosidl_generator_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libaction_msgs__rosidl_typesupport_fastrtps_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libaction_msgs__rosidl_typesupport_fastrtps_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libaction_msgs__rosidl_typesupport_introspection_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libaction_msgs__rosidl_typesupport_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libaction_msgs__rosidl_typesupport_introspection_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libaction_msgs__rosidl_typesupport_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libaction_msgs__rosidl_generator_py.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcl.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librcl_action.so: not a directory cc1plus: warning: /opt/ros/humble/lib/librclcpp_action.so: not a directory cc1plus: warning: /usr/lib/x86_64-linux-gnu/libconsole_bridge.so.1.0: not a directory cc1plus: warning: /opt/ros/humble/lib/libtf2.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libstd_msgs__rosidl_generator_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libstd_msgs__rosidl_typesupport_fastrtps_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libstd_msgs__rosidl_typesupport_fastrtps_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libstd_msgs__rosidl_typesupport_introspection_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libstd_msgs__rosidl_typesupport_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libstd_msgs__rosidl_typesupport_introspection_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libstd_msgs__rosidl_typesupport_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libstd_msgs__rosidl_generator_py.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libtf2_msgs__rosidl_generator_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libtf2_msgs__rosidl_typesupport_fastrtps_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libtf2_msgs__rosidl_typesupport_introspection_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libtf2_msgs__rosidl_typesupport_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libtf2_msgs__rosidl_typesupport_fastrtps_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libtf2_msgs__rosidl_typesupport_introspection_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libtf2_msgs__rosidl_typesupport_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libtf2_msgs__rosidl_generator_py.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libsensor_msgs__rosidl_generator_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libsensor_msgs__rosidl_typesupport_fastrtps_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libsensor_msgs__rosidl_typesupport_fastrtps_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libsensor_msgs__rosidl_typesupport_introspection_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libsensor_msgs__rosidl_typesupport_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libsensor_msgs__rosidl_typesupport_introspection_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libsensor_msgs__rosidl_typesupport_cpp.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libsensor_msgs__rosidl_generator_py.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libvisualization_msgs__rosidl_generator_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libvisualization_msgs__rosidl_typesupport_c.so: not a directory cc1plus: warning: /opt/ros/humble/lib/libvisualization_msgs__rosidl_typesupport_cpp.so: not a directory /home/lizhuo/ros2_ws/image_subscriber_cpp/src/image_subscriber.cpp: In member function ‘void ImageSubscriber::image_callback(sensor_msgs::msg::Image_<std::allocator<void> >::ConstSharedPtr)’: /home/lizhuo/ros2_ws/image_subscriber_cpp/src/image_subscriber.cpp:110:24: warning: unused variable ‘armor’ [-Wunused-variable] 110 | for (auto& armor : armors) { | ^~~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/c++allocator.h:33, from /usr/include/c++/11/bits/allocator.h:46, from /usr/include/c++/11/memory:64, from /opt/ros/humble/include/rclcpp/rclcpp/rclcpp.hpp:153, from /home/lizhuo/ros2_ws/image_subscriber_cpp/src/image_subscriber.cpp:1: /usr/include/c++/11/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = cv::RotatedRect; _Args = {cv::RotatedRect&, cv::RotatedRect&}; _Tp = cv::RotatedRect]’: /usr/include/c++/11/bits/alloc_traits.h:516:17: required from ‘static void std::allocator_traits<std::allocator<_Tp1> >::construct(std::allocator_traits<std::allocator<_Tp1> >::allocator_type&, _Up*, _Args&& ...) [with _Up = cv::RotatedRect; _Args = {cv::RotatedRect&, cv::RotatedRect&}; _Tp = cv::RotatedRect; std::allocator_traits<std::allocator<_Tp1> >::allocator_type = std::allocator<cv::RotatedRect>]’ /usr/include/c++/11/bits/vector.tcc:115:30: required from ‘std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {cv::RotatedRect&, cv::RotatedRect&}; _Tp = cv::RotatedRect; _Alloc = std::allocator<cv::RotatedRect>; std::vector<_Tp, _Alloc>::reference = cv::RotatedRect&]’ /home/lizhuo/ros2_ws/image_subscriber_cpp/src/image_subscriber.cpp:106:40: required from here /usr/include/c++/11/ext/new_allocator.h:162:11: error: no matching function for call to ‘cv::RotatedRect::RotatedRect(cv::RotatedRect&, cv::RotatedRect&)’ 162 | { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /usr/include/opencv4/opencv2/core.hpp:57, from /usr/include/opencv4/opencv2/core/core.hpp:48, from /opt/ros/humble/include/cv_bridge/cv_bridge/cv_bridge.h:43, from /home/lizhuo/ros2_ws/image_subscriber_cpp/src/image_subscriber.cpp:3: /usr/include/opencv4/opencv2/core/types.hpp:535:5: note: candidate: ‘cv::RotatedRect::RotatedRect(const Point2f&, const Point2f&, const Point2f&)’ 535 | RotatedRect(const Point2f& point1, const Point2f& point2, const Point2f& point3); | ^~~~~~~~~~~ /usr/include/opencv4/opencv2/core/types.hpp:535:5: note: candidate expects 3 arguments, 2 provided In file included from /usr/include/opencv4/opencv2/core.hpp:57, from /usr/include/opencv4/opencv2/core/core.hpp:48, from /opt/ros/humble/include/cv_bridge/cv_bridge/cv_bridge.h:43, from /home/lizhuo/ros2_ws/image_subscriber_cpp/src/image_subscriber.cpp:3: /usr/include/opencv4/opencv2/core/types.hpp:2080:1: note: candidate: ‘cv::RotatedRect::RotatedRect(const Point2f&, const Size2f&, float)’ 2080 | RotatedRect::RotatedRect(const Point2f& _center, const Size2f& _size, float _angle) | ^~~~~~~~~~~ /usr/include/opencv4/opencv2/core/types.hpp:2080:1: note: candidate expects 3 arguments, 2 provided /usr/include/opencv4/opencv2/core/types.hpp:2076:1: note: candidate: ‘cv::RotatedRect::RotatedRect()’ 2076 | RotatedRect::RotatedRect() | ^~~~~~~~~~~ /usr/include/opencv4/opencv2/core/types.hpp:2076:1: note: candidate expects 0 arguments, 2 provided In file included from /usr/include/opencv4/opencv2/core.hpp:57, from /usr/include/opencv4/opencv2/core/core.hpp:48, from /opt/ros/humble/include/cv_bridge/cv_bridge/cv_bridge.h:43, from /home/lizhuo/ros2_ws/image_subscriber_cpp/src/image_subscriber.cpp:3: /usr/include/opencv4/opencv2/core/types.hpp:519:18: note: candidate: ‘cv::RotatedRect::RotatedRect(const cv::RotatedRect&)’ 519 | class CV_EXPORTS RotatedRect | ^~~~~~~~~~~ /usr/include/opencv4/opencv2/core/types.hpp:519:18: note: candidate expects 1 argument, 2 provided /usr/include/opencv4/opencv2/core/types.hpp:519:18: note: candidate: ‘cv::RotatedRect::RotatedRect(cv::RotatedRect&&)’ /usr/include/opencv4/opencv2/core/types.hpp:519:18: note: candidate expects 1 argument, 2 provided gmake[2]: *** [CMakeFiles/image_subscriber.dir/build.make:76:CMakeFiles/image_subscriber.dir/src/image_subscriber.cpp.o] 错误 1 gmake[1]: *** [CMakeFiles/Makefile2:137:CMakeFiles/image_subscriber.dir/all] 错误 2 gmake: *** [Makefile:146:all] 错误 2 --- Failed <<< image_subscriber_cpp [14.0s, exited with code 2] Summary: 0 packages finished [15.1s] 1 package failed: image_subscriber_cpp 1 package had stderr output: image_subscriber_cpp
最新发布
08-12
### Halcon 中 `angle_lx` 函数的用法 在 Halcon 图像处理库中,`angle_lx` 是用于计算两条直线之间的夹角的函数[^1]。该函数通常应用于几何分析场景下,比如测量物体的角度偏差或者检测特定方向上的特征。 以下是关于此函数的一些重要说明: #### 参数描述 - **输入参数** - `Row1`, `Column1`: 定义第一条线的第一个端点坐标。 - `Row2`, `Column2`: 定义第一条线的第二个端点坐标。 - `Row3`, `Column3`: 定义第二条线的第一个端点坐标。 - `Row4`, `Column4`: 定义第二条线的第二个端点坐标。 - **输出参数** - `Angle`: 返回两直线间的夹角(单位为弧度)。角度范围通常是 \(-\pi\) 到 \(+\pi\) 或者 \(0\) 到 \(2\pi\),具体取决于实现方式[^2]。 #### 使用示例 下面是一个简单的代码示例展示如何调用 `angle_lx` 来获取两个向量间的角度差值。 ```hdevelop * Define two lines by their endpoints. Row1 := 100; Column1 := 100; Row2 := 200; Column2 := 100; Row3 := 150; Column3 := 150; Row4 := 150; Column4 := 250; * Calculate the angle between these two lines. angle_lx (Row1, Column1, Row2, Column2, Row3, Column3, Row4, Column4, Angle) * Convert result from radians to degrees for better readability. deg(Angle, AngleDegrees) disp_message(WindowHandle, 'The angle is '+num2str(AngleDegrees, 2)+'°', 'window', 12, 12, 'black', 'true') ``` 以上脚本定义了两条相互垂直的线段并打印它们之间形成的直角大小[^3]。 #### 特殊情况与注意事项 当任意一条给定的线长度接近零时(即起点和终点几乎重合),可能会导致数值不稳定甚至错误的结果。因此,在实际应用前应验证数据的有效性以避免潜在问题发生[^4]。 ---
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值