3D坐标下,一点在某一线段上的左右方向的判定

3D坐标下,一点在某一线段上的左右方向的判定

代码

C++ 复制代码
#include <iostream>
#include <Eigen/Dense>

#define M_PI 3.1415926

// 计算三点组成平面的参数和变换到XOY平面的变换矩阵
void computePlaneAndTransform(const Eigen::Vector3d& P1, const Eigen::Vector3d& P2, const Eigen::Vector3d& P3,
                              Eigen::Vector4d& planeParams, Eigen::Matrix4d& transformMatrix) {
    // 确保P1在原点
    assert((P1 - Eigen::Vector3d::Zero()).norm() < 1e-6);

    // 计算平面的法向量
    Eigen::Vector3d normal = P2.cross(P3);
    normal.normalize();
    std::cout << "z = " << normal.z() << std::endl;
    if (normal.z() < 0) {
        normal = -1 * normal;
    }
    std::cout << "z2 = " << normal.z() << std::endl;
    // 平面方程为 Ax + By + Cz + D = 0,这里P1在原点,D = 0
    planeParams << normal.x(), normal.y(), normal.z(), 0;

    // 计算旋转矩阵,使得平面的法向量与Z轴对齐
    Eigen::Vector3d zAxis(0, 0, 1);
    double cosTheta = normal.dot(zAxis);
    Eigen::Vector3d crossProduct = normal.cross(zAxis);
    double sinTheta = crossProduct.norm();
    if (sinTheta < 1e-6) {
        // 特殊情况,法向量与Z轴平行或反平行
        if (cosTheta < 0) {
            // 法向量与Z轴反平行
            transformMatrix = Eigen::Matrix4d::Identity();
            transformMatrix.block<3, 3>(0, 0) = Eigen::AngleAxisd(M_PI, Eigen::Vector3d(1, 0, 0)).toRotationMatrix();
        } else {
            // 法向量与Z轴平行
            transformMatrix = Eigen::Matrix4d::Identity();
        }
    } else {
        crossProduct.normalize();
        Eigen::Matrix3d rotationMatrix;
        rotationMatrix << cosTheta + crossProduct.x() * crossProduct.x() * (1 - cosTheta),
                       crossProduct.x()* crossProduct.y()* (1 - cosTheta) - crossProduct.z() * sinTheta,
                       crossProduct.x()* crossProduct.z()* (1 - cosTheta) + crossProduct.y() * sinTheta,

                       crossProduct.y()* crossProduct.x()* (1 - cosTheta) + crossProduct.z() * sinTheta,
                       cosTheta + crossProduct.y() * crossProduct.y() * (1 - cosTheta),
                       crossProduct.y()* crossProduct.z()* (1 - cosTheta) - crossProduct.x() * sinTheta,

                       crossProduct.z()* crossProduct.x()* (1 - cosTheta) - crossProduct.y() * sinTheta,
                       crossProduct.z()* crossProduct.y()* (1 - cosTheta) + crossProduct.x() * sinTheta,
                       cosTheta + crossProduct.z() * crossProduct.z() * (1 - cosTheta);
        transformMatrix.setIdentity();
        transformMatrix.block<3, 3>(0, 0) = rotationMatrix;
    }
}

// 判断点在向量的哪一侧
int determineSide(const Eigen::Vector3d& p4, const Eigen::Vector3d& p5, const Eigen::Vector3d& p6) {
    // 投影到XOY平面
    Eigen::Vector2d p4_2d = p4.head<2>();
    Eigen::Vector2d p5_2d = p5.head<2>();
    Eigen::Vector2d p6_2d = p6.head<2>();

    // 计算向量 P4->P5 和 P4->P6
    Eigen::Vector2d v1 = p5_2d - p4_2d;
    Eigen::Vector2d v2 = p6_2d - p4_2d;

    double crossResult = v1.x() * v2.y() - v1.y() * v2.x();
    std::cout << "crossResult = " << crossResult << std::endl;
    if (crossResult < 0) {
        // 右侧
        return 1;
    } else if (crossResult > 0) {
        // 左侧
        return 2;
    } else {
        // 共线
        return 0;
    }
}

int main() {
    Eigen::Vector3d P1 = Eigen::Vector3d::Zero();
    Eigen::Vector3d P2(10, -10, -13);
    Eigen::Vector3d P3(0, 15, 0);

    Eigen::Vector4d planeParams;
    Eigen::Matrix4d mat;

    computePlaneAndTransform(P1, P2, P3, planeParams, mat);

    // 将点变换到XOY平面
    Eigen::Vector3d P4 = (mat * Eigen::Vector4d(P1.x(), P1.y(), P1.z(), 1)).head<3>();
    Eigen::Vector3d P5 = (mat * Eigen::Vector4d(P2.x(), P2.y(), P2.z(), 1)).head<3>();
    Eigen::Vector3d P6 = (mat * Eigen::Vector4d(P3.x(), P3.y(), P3.z(), 1)).head<3>();
    std::cout << "x, y, z = " << P4.x() << ", " << P4.y() << ", " << P4.z() << std::endl;
    std::cout << "x, y, z = " << P5.x() << ", " << P5.y() << ", " << P5.z() << std::endl;
    std::cout << "x, y, z = " << P6.x() << ", " << P6.y() << ", " << P6.z() << std::endl;

    int side = determineSide(P4, P5, P6);
    std::cout << "P3在向量P1->P2的";
    if (side == 1) {
        std::cout << "右侧" << std::endl;
    } else if (side == 2) {
        std::cout << "左侧" << std::endl;
    } else {
        std::cout << "共线" << std::endl;
    }
    system("pause");
    return 0;
}
相关推荐
灏瀚星空1 小时前
Python数学可视化:3D参数曲面与隐式曲面绘制技术
开发语言·python·3d
高德技术2 小时前
全流程开源!高德3D贴图生成系统,白模一键生成真实感纹理贴图
3d·贴图
daifgFuture2 天前
Android 3D球形水平圆形旋转,旋转动态更换图片
android·3d
牧子川2 天前
【论文解读】CVPR2023 PoseFormerV2:3D人体姿态估计(附论文地址)
3d·cvpr2023·poseformerv2
资深设备全生命周期管理2 天前
优化版本,增加3D 视觉 查看前面的记录
3d
m0_748250742 天前
GPUCUDA 发展编年史:从 3D 渲染到 AI 大模型时代(上)
人工智能·3d
少林6593 天前
谷歌地图高清卫星地图2026中文版下载|谷歌地图3D卫星高清版 V7.3.6.9796 最新免费版下载 - 前端工具导航
3d·谷歌地图
LeonDL1683 天前
HALCON 深度学习训练 3D 图像的几种方式优缺点
人工智能·python·深度学习·3d·halcon·halcon训练3d图像·深度学习训练3d图像
xhload3d4 天前
图扑软件 | 带你体验 Low Poly 卡通三维世界
物联网·3d·智慧城市·html5·webgl·数字孪生·可视化·工业互联网·三维建模·工控·轻量化·中国风·卡通动画·写实风格·科技风·low poly
图扑数字孪生4 天前
基于 HT for Web 轻量化 3D 数字孪生数据中心解决方案
3d·数字孪生·三维可视化·数据中心·智慧机房