【3D】几何算法合集2(自用资源)方便cv

打印 上一主题 下一主题

主题 778|帖子 778|积分 2334

细分

  1. #include<cmath>
  2. #include<algorithm>
  3. #include <opencv2/core/core.hpp>
  4. #include <opencv2/imgproc/imgproc.hpp>
  5. #include <opencv2/highgui/highgui.hpp>
  6. #define sign(x) ((x)>eps?1:((x)<-eps?(-1):(0)))
  7. #define PI 3.1415926
复制代码
  1. #include "Algorithm.h"
  2. const double eps = 1e-8, inf = 1e50;
复制代码
平面类

  1. class plane3 {
  2. public:
  3.     cv::Point3d a, b, c;
  4.     plane3() {}
  5.     plane3(cv::Point3d _a, cv::Point3d _b, cv::Point3d _c) {
  6.         a = _a;
  7.         b = _b;
  8.         c = _c;
  9.     }
  10. };
复制代码
三维直线类

  1. class line3 {
  2. public:
  3.     cv::Point3d a, b;
  4.     line3() {}
  5.     line3(cv::Point3d _a, cv::Point3d _b) {
  6.         a = _a;
  7.         b = _b;
  8.     }
  9. };
复制代码
度数转弧度

  1. //度数转弧度
  2. double get_radian(double angel);
复制代码
  1. //度数转弧度
  2. double get_radian(double angel) {
  3.     return PI * angel / 180.0;
  4. }
复制代码
叉乘,返回向量

  1. //叉乘,返回向量
  2. cv::Point3d xmult(cv::Point3d a, cv::Point3d b);
复制代码
  1. cv::Point3d xmult(cv::Point3d a, cv::Point3d b) {
  2.     return cv::Point3d(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
  3. }
复制代码
点乘

  1. //点乘
  2. double dmult(cv::Point3d a, cv::Point3d b);
复制代码
  1. //点乘
  2. double dmult(cv::Point3d a, cv::Point3d b) {
  3.     return a.x * b.x + a.y * b.y + a.z * b.z;
  4. }
复制代码
向量

  1. //向量
  2. cv::Point3d Vec(cv::Point3d a, cv::Point3d b);
复制代码
  1. //向量
  2. cv::Point3d Vec(cv::Point3d a, cv::Point3d b) {
  3.     return cv::Point3d(a.x - b.x, a.y - b.y, a.z - b.z);
  4. }
复制代码
取模

  1. //取模
  2. double length(cv::Point3d v);
复制代码
  1. //取模
  2. double length(cv::Point3d v) {
  3.     return sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
  4. }
复制代码
判断直线平行

  1. //判断直线平行
  2. bool parallel(line3 u, line3 v);
复制代码
  1. //判断直线平行
  2. bool parallel(line3 u, line3 v) {
  3.     return sign(length(xmult(u.b - u.a, v.b - v.a))) == 0;
  4. }
复制代码
平面法向量

  1. //平面法向量
  2. cv::Point3d pvec(plane3 s);
复制代码
  1. //平面法向量
  2. cv::Point3d pvec(plane3 s) {
  3.     return xmult(s.b - s.a, s.c - s.a);
  4. }
复制代码
线线求交

  1. //线线求交
  2. cv::Point3d line_interseciton(line3 u, line3 v);
复制代码
  1. //线线求交
  2. cv::Point3d line_interseciton(line3 u, line3 v) {
  3.     cv::Point3d ret = u.a, v1 = xmult(u.a - v.a, v.b - v.a), v2 = xmult(u.b - u.a, v.b - v.a);
  4.     double t = length(v1) / length(v2) * (dmult(v1, v2) > 0 ? -1 : 1);
  5.     return ret + ((u.b - u.a) * t);
  6. }
复制代码
线面求交

  1. //线面求交
  2. cv::Point3d line_plane_intersection(line3 u, plane3 s);
复制代码
  1. //线面求交
  2. cv::Point3d line_plane_intersection(line3 u, plane3 s) {
  3.     cv::Point3d ret = pvec(s), der = u.b - u.a;
  4.     double t = dmult(ret, s.a - u.a) / dmult(ret, u.b - u.a);
  5.     return u.a + der * t;
  6. }
复制代码
判断两直线的位置关系

  1. //判断两直线的位置关系
  2. int line_to_line(line3 u, line3 v);
复制代码
  1. //判断两直线的位置关系
  2. int line_to_line(line3 u, line3 v) {
  3.     plane3 s1(u.a, u.b, v.a), s2(u.a, u.b, v.b);
  4.     if (sign(length(xmult(pvec(s1), pvec(s2)))))
  5.         return -1;//异面
  6.     else if (parallel(u, v))
  7.         return 0;//平行
  8.     else
  9.         return 1;//相交
  10. }
复制代码
直线到直线的距离 w是垂直向量,根据w算出标量

  1. //直线到直线的距离 w是垂直向量,根据w算出标量
  2. double Common_Vertical_Line(line3 u, line3 v, cv::Point3d& w);
复制代码
  1. //直线到直线的距离 w是垂直向量,根据w算出标量
  2. double Common_Vertical_Line(line3 u, line3 v, cv::Point3d& w) {
  3.     w = xmult(u.a - u.b, v.a - v.b);
  4.     return fabs(dmult(u.a - v.a, w) / length(w));
  5. }
复制代码
求三维下的两线交点

  1. //求三维下的两线交点
  2. bool Intersection_lines(line3 L1, line3 L2, cv::Point3d& insc);
复制代码
  1. //求三维下的两线交点
  2. bool Intersection_lines(line3 L1, line3 L2, cv::Point3d& insc) {
  3.     int relationship = line_to_line(L1, L2);
  4.     switch (relationship) {
  5.     case 1:                     //相交
  6.         insc = line_interseciton(L1, L2);
  7.         return true;
  8.     case 0:                     //平行
  9.         return false;
  10.     case -1: {        //异面
  11.         cv::Point3d v;
  12.         double closestDist = Common_Vertical_Line(L1, L2, v); //两直线的最近距离
  13.         plane3 p1(L1.a, L1.b, L1.a + v), p2(L2.a, L2.b, L2.a + v);
  14.         cv::Point3d line1_ans = line_plane_intersection(L1, p2); //交点
  15.         cv::Point3d line2_ans = line_plane_intersection(L2, p1); //交点
  16.         insc.x = (line1_ans.x + line2_ans.x) / 2;
  17.         insc.y = (line1_ans.y + line2_ans.y) / 2;
  18.         insc.z = (line1_ans.z + line2_ans.z) / 2;
  19.         return true;
  20.     }
  21.     default:
  22.         return false;
  23.     }
  24. }
复制代码
输入四个点,求交点法线的起点

  1. //输入四个点,求交点法线的起点
  2. bool get_joint(line3 L1, line3 L2, cv::Point3d& beg);
复制代码
  1. //输入四个点,求交点法线的起点
  2. bool get_joint(line3 L1, line3 L2,  cv::Point3d& beg) {
  3.     if (Intersection_lines(L1, L2, beg) == false) {
  4.         return false;
  5.     }
  6.     return true;
  7. }
复制代码
点线垂足

  1. //点线垂足
  2. cv::Point3d vertical_foot(cv::Point3d p, line3 u);
复制代码
  1. //点线垂足
  2. cv::Point3d vertical_foot(cv::Point3d p, line3 u) {
  3.     double t = dmult(p - u.a, u.b - u.a) / dist2(u.a, u.b);
  4.     cv::Point3d ret = u.a;
  5.     return ret + ((u.b - u.a) * t);
  6. }
复制代码
点线距离

  1. //点线距离
  2. double dist_point_to_line(cv::Point3d p, line3 u);
复制代码
  1. //点线距离
  2. double dist_point_to_line(cv::Point3d p, line3 u) {
  3.     return length(xmult(p - u.a, u.b - u.a)) / dist(u.a, u.b);
  4. }
复制代码
查抄三点共线

  1. //检查三点共线
  2. bool check_collinear(cv::Point3d pt1, cv::Point3d pt2, cv::Point3d pt3);
复制代码
  1. //检查三点共线 pt1 pt2是线
  2. bool check_collinear(cv::Point3d pt1, cv::Point3d pt2, cv::Point3d pt3) {
  3.     line3 line(pt1, pt2);
  4.     double d = dist_point_to_line(pt3, line);
  5.     if (sign(d) == 0) {
  6.         return true;//共线
  7.     }
  8.     return false;
  9. }
复制代码
三点求圆

  1. //三点求圆
  2. void centerCircle3d(cv::Point3d pt1, cv::Point3d pt2, cv::Point3d pt3, double& radius, cv::Point3d& center);
复制代码
  1. //三点求圆
  2. void centerCircle3d(cv::Point3d pt1, cv::Point3d pt2, cv::Point3d pt3, double& radius,cv::Point3d &center)
  3. {
  4.     double a1 = (pt1.y * pt2.z - pt2.y * pt1.z - pt1.y * pt3.z + pt3.y * pt1.z + pt2.y * pt3.z - pt3.y * pt2.z),
  5.         b1 = -(pt1.x * pt2.z - pt2.x * pt1.z - pt1.x * pt3.z + pt3.x * pt1.z + pt2.x * pt3.z - pt3.x * pt2.z),
  6.         c1 = (pt1.x * pt2.y - pt2.x * pt1.y - pt1.x * pt3.y + pt3.x * pt1.y + pt2.x * pt3.y - pt3.x * pt2.y),
  7.         d1 = -(pt1.x * pt2.y * pt3.z - pt1.x * pt3.y * pt2.z - pt2.x * pt1.y * pt3.z + pt2.x * pt3.y * pt1.z + pt3.x * pt1.y * pt2.z - pt3.x * pt2.y * pt1.z);
  8.     double a2 = 2 * (pt2.x - pt1.x),
  9.         b2 = 2 * (pt2.y - pt1.y),
  10.         c2 = 2 * (pt2.z - pt1.z),
  11.         d2 = pt1.x * pt1.x + pt1.y * pt1.y + pt1.z * pt1.z - pt2.x * pt2.x - pt2.y * pt2.y - pt2.z * pt2.z;
  12.     double a3 = 2 * (pt3.x - pt1.x),
  13.         b3 = 2 * (pt3.y - pt1.y),
  14.         c3 = 2 * (pt3.z - pt1.z),
  15.         d3 = pt1.x * pt1.x + pt1.y * pt1.y + pt1.z * pt1.z - pt3.x * pt3.x - pt3.y * pt3.y - pt3.z * pt3.z;
  16.     double x = -(b1 * c2 * d3 - b1 * c3 * d2 - b2 * c1 * d3 + b2 * c3 * d1 + b3 * c1 * d2 - b3 * c2 * d1)
  17.         / (a1 * b2 * c3 - a1 * b3 * c2 - a2 * b1 * c3 + a2 * b3 * c1 + a3 * b1 * c2 - a3 * b2 * c1);
  18.     double y = (a1 * c2 * d3 - a1 * c3 * d2 - a2 * c1 * d3 + a2 * c3 * d1 + a3 * c1 * d2 - a3 * c2 * d1)
  19.         / (a1 * b2 * c3 - a1 * b3 * c2 - a2 * b1 * c3 + a2 * b3 * c1 + a3 * b1 * c2 - a3 * b2 * c1);
  20.     double z = -(a1 * b2 * d3 - a1 * b3 * d2 - a2 * b1 * d3 + a2 * b3 * d1 + a3 * b1 * d2 - a3 * b2 * d1)
  21.         / (a1 * b2 * c3 - a1 * b3 * c2 - a2 * b1 * c3 + a2 * b3 * c1 + a3 * b1 * c2 - a3 * b2 * c1);
  22.     radius = sqrt((pt1.x - x) * (pt1.x - x) + (pt1.y - y) * (pt1.y - y) + (pt1.z - z) * (pt1.z - z));
  23.     center = { x,y,z };
  24. }
复制代码
拟合圆

  1. //拟合圆
  2. bool Fitting_radius(cv::Point3d pt1, cv::Point3d pt2, cv::Point3d pt3, double& r, cv::Point3d& center);
复制代码
  1. bool Fitting_radius(cv::Point3d pt1, cv::Point3d pt2, cv::Point3d pt3,double &r,cv::Point3d &center) {
  2.    
  3.     if (check_collinear(pt1, pt2, pt3)) {
  4.         return false;
  5.     }
  6.     centerCircle3d(pt1, pt2, pt3, r,center);
  7.     return true;
  8. }
复制代码
点线求垂足,pt1和pt2是线,会查共线

  1. //点线求垂足,pt1和pt2是线,会查共线
  2. bool get_intersection_foot(cv::Point3d pt1, cv::Point3d pt2, cv::Point3d pt3, cv::Point3d& intersection);
复制代码
  1. bool get_intersection_foot(cv::Point3d pt1, cv::Point3d pt2, cv::Point3d pt3, cv::Point3d& intersection)
  2. {
  3.     if (check_collinear(pt1, pt2, pt3)) {
  4.         return false;
  5.     }
  6.     line3 line(pt1, pt2);
  7.     intersection = vertical_foot(pt3, line);
  8.     return true;
  9. }
复制代码
三点求内切圆,pt2是交点,算夹角<180侧的内切圆

  1. //三点求内切圆,pt2是交点,算夹角<180侧的内切圆
  2. double get_inc_circle_R(const cv::Point3d& p1, const cv::Point3d& p2, const cv::Point3d& p3);
复制代码
  1. double get_inc_circle_R(const cv::Point3d& p1, const cv::Point3d& p2, const cv::Point3d& p3)
  2. {
  3.     //三角形三边边长
  4.     double p2p1, p3p2, p1p3;
  5.     p2p1 = norm(p2 - p1);
  6.     p3p2 = norm(p3 - p2);
  7.     p1p3 = norm(p1 - p3);
  8.     //三角形半周长
  9.     double semiperimeter;
  10.     semiperimeter = (p2p1 + p3p2 + p1p3) / 2.0;
  11.     //三角形面积
  12.     double area;
  13.     area = sqrt(semiperimeter * (semiperimeter - p2p1) * (semiperimeter - p3p2) * (semiperimeter - p1p3));
  14.     //计算三角形内接圆半径
  15.     double radius;
  16.     radius = 2 * area / (p2p1 + p3p2 + p1p3);
  17.     return radius;
  18. }
复制代码
未注释

1

  1. double dist(cv::Point3d a, cv::Point3d b);
  2. double dist2(cv::Point3d a, cv::Point3d b);
复制代码
  1. double dist(cv::Point3d a, cv::Point3d b) {
  2.     return length(a - b);
  3. }
  4. double dist2(cv::Point3d a, cv::Point3d b) {
  5.     return dmult(a - b, a - b);
  6. }
复制代码
2

  1. cv::Point3d get_midPoint(const cv::Point3d pt1, const cv::Point3d pt2);
复制代码
  1. cv::Point3d get_midPoint(const cv::Point3d pt1, const cv::Point3d pt2)
  2. {
  3.     cv::Point3d mid;
  4.     cv::Point3d vec = pt2 - pt1;
  5.     mid = pt1 + 0.5 * (vec);
  6.     return mid;
  7. }
复制代码
 3

  1. double convert_angle_360(double src);
复制代码
  1. double convert_angle_360(double src)
  2. {
  3.     double dst = src;
  4.     while(dst >= 360) {
  5.         dst -= 360;
  6.     }
  7.     while (dst < 0) {
  8.         dst += 360;
  9.     }
  10.     return dst;
  11. }
复制代码

汇总

头文件

  1. #pragma once#include <iostream>#include<cstdlib>#include<cmath>
  2. #include<algorithm>
  3. #include <opencv2/core/core.hpp>
  4. #include <opencv2/imgproc/imgproc.hpp>
  5. #include <opencv2/highgui/highgui.hpp>
  6. #define sign(x) ((x)>eps?1:((x)<-eps?(-1):(0)))
  7. #define PI 3.1415926class plane3 {
  8. public:
  9.     cv::Point3d a, b, c;
  10.     plane3() {}
  11.     plane3(cv::Point3d _a, cv::Point3d _b, cv::Point3d _c) {
  12.         a = _a;
  13.         b = _b;
  14.         c = _c;
  15.     }
  16. };class line3 {
  17. public:
  18.     cv::Point3d a, b;
  19.     line3() {}
  20.     line3(cv::Point3d _a, cv::Point3d _b) {
  21.         a = _a;
  22.         b = _b;
  23.     }
  24. };//度数转弧度
  25. double get_radian(double angel);//叉乘,返回向量
  26. cv::Point3d xmult(cv::Point3d a, cv::Point3d b);//点乘
  27. double dmult(cv::Point3d a, cv::Point3d b);//向量
  28. cv::Point3d Vec(cv::Point3d a, cv::Point3d b);//取模
  29. double length(cv::Point3d v);//判断直线平行
  30. bool parallel(line3 u, line3 v);//平面法向量
  31. cv::Point3d pvec(plane3 s);//线线求交
  32. cv::Point3d line_interseciton(line3 u, line3 v);//线面求交
  33. cv::Point3d line_plane_intersection(line3 u, plane3 s);//判断两直线的位置关系
  34. int line_to_line(line3 u, line3 v);//直线到直线的距离 w是垂直向量,根据w算出标量
  35. double Common_Vertical_Line(line3 u, line3 v, cv::Point3d& w);//求三维下的两线交点
  36. bool Intersection_lines(line3 L1, line3 L2, cv::Point3d& insc);//输入四个点,求交点法线的起点
  37. bool get_joint(line3 L1, line3 L2, cv::Point3d& beg);double dist2(cv::Point3d a, cv::Point3d b);//点线垂足
  38. cv::Point3d vertical_foot(cv::Point3d p, line3 u);double dist(cv::Point3d a, cv::Point3d b);//点线距离
  39. double dist_point_to_line(cv::Point3d p, line3 u);//检查三点共线
  40. bool check_collinear(cv::Point3d pt1, cv::Point3d pt2, cv::Point3d pt3);//三点求圆
  41. void centerCircle3d(cv::Point3d pt1, cv::Point3d pt2, cv::Point3d pt3, double& radius, cv::Point3d& center);//拟合圆
  42. bool Fitting_radius(cv::Point3d pt1, cv::Point3d pt2, cv::Point3d pt3, double& r, cv::Point3d& center);//点线求垂足,pt1和pt2是线,会查共线
  43. bool get_intersection_foot(cv::Point3d pt1, cv::Point3d pt2, cv::Point3d pt3, cv::Point3d& intersection);//三点求内切圆,pt2是交点,算夹角<180侧的内切圆
  44. double get_inc_circle_R(const cv::Point3d& p1, const cv::Point3d& p2, const cv::Point3d& p3);cv::Point3d get_midPoint(const cv::Point3d pt1, const cv::Point3d pt2);double convert_angle_360(double src);
复制代码
cpp文件

  1. #include "Algorithm.h"
  2. const double eps = 1e-8, inf = 1e50;//度数转弧度
  3. double get_radian(double angel) {
  4.     return PI * angel / 180.0;
  5. }//叉乘,返回向量cv::Point3d xmult(cv::Point3d a, cv::Point3d b) {
  6.     return cv::Point3d(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
  7. }//点乘
  8. double dmult(cv::Point3d a, cv::Point3d b) {
  9.     return a.x * b.x + a.y * b.y + a.z * b.z;
  10. }//向量
  11. cv::Point3d Vec(cv::Point3d a, cv::Point3d b) {
  12.     return cv::Point3d(a.x - b.x, a.y - b.y, a.z - b.z);
  13. }//取模
  14. double length(cv::Point3d v) {
  15.     return sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
  16. }//判断直线平行
  17. bool parallel(line3 u, line3 v) {
  18.     return sign(length(xmult(u.b - u.a, v.b - v.a))) == 0;
  19. }//平面法向量
  20. cv::Point3d pvec(plane3 s) {
  21.     return xmult(s.b - s.a, s.c - s.a);
  22. }//线线求交
  23. cv::Point3d line_interseciton(line3 u, line3 v) {
  24.     cv::Point3d ret = u.a, v1 = xmult(u.a - v.a, v.b - v.a), v2 = xmult(u.b - u.a, v.b - v.a);
  25.     double t = length(v1) / length(v2) * (dmult(v1, v2) > 0 ? -1 : 1);
  26.     return ret + ((u.b - u.a) * t);
  27. }//线面求交
  28. cv::Point3d line_plane_intersection(line3 u, plane3 s) {
  29.     cv::Point3d ret = pvec(s), der = u.b - u.a;
  30.     double t = dmult(ret, s.a - u.a) / dmult(ret, u.b - u.a);
  31.     return u.a + der * t;
  32. }//判断两直线的位置关系
  33. int line_to_line(line3 u, line3 v) {
  34.     plane3 s1(u.a, u.b, v.a), s2(u.a, u.b, v.b);
  35.     if (sign(length(xmult(pvec(s1), pvec(s2)))))
  36.         return -1;//异面
  37.     else if (parallel(u, v))
  38.         return 0;//平行
  39.     else
  40.         return 1;//相交
  41. }//直线到直线的距离 w是垂直向量,根据w算出标量
  42. double Common_Vertical_Line(line3 u, line3 v, cv::Point3d& w) {
  43.     w = xmult(u.a - u.b, v.a - v.b);
  44.     return fabs(dmult(u.a - v.a, w) / length(w));
  45. }//求三维下的两线交点
  46. bool Intersection_lines(line3 L1, line3 L2, cv::Point3d& insc) {
  47.     int relationship = line_to_line(L1, L2);
  48.     switch (relationship) {
  49.     case 1:                     //相交
  50.         insc = line_interseciton(L1, L2);
  51.         return true;
  52.     case 0:                     //平行
  53.         return false;
  54.     case -1: {        //异面
  55.         cv::Point3d v;
  56.         double closestDist = Common_Vertical_Line(L1, L2, v); //两直线的最近距离
  57.         plane3 p1(L1.a, L1.b, L1.a + v), p2(L2.a, L2.b, L2.a + v);
  58.         cv::Point3d line1_ans = line_plane_intersection(L1, p2); //交点
  59.         cv::Point3d line2_ans = line_plane_intersection(L2, p1); //交点
  60.         insc.x = (line1_ans.x + line2_ans.x) / 2;
  61.         insc.y = (line1_ans.y + line2_ans.y) / 2;
  62.         insc.z = (line1_ans.z + line2_ans.z) / 2;
  63.         return true;
  64.     }
  65.     default:
  66.         return false;
  67.     }
  68. }//输入四个点,求交点法线的起点
  69. bool get_joint(line3 L1, line3 L2,  cv::Point3d& beg) {
  70.     if (Intersection_lines(L1, L2, beg) == false) {
  71.         return false;
  72.     }
  73.     return true;
  74. }double dist2(cv::Point3d a, cv::Point3d b) {    return dmult(a - b, a - b);}//点线垂足
  75. cv::Point3d vertical_foot(cv::Point3d p, line3 u) {
  76.     double t = dmult(p - u.a, u.b - u.a) / dist2(u.a, u.b);
  77.     cv::Point3d ret = u.a;
  78.     return ret + ((u.b - u.a) * t);
  79. }double dist(cv::Point3d a, cv::Point3d b) {    return length(a - b);}//点线距离
  80. double dist_point_to_line(cv::Point3d p, line3 u) {
  81.     return length(xmult(p - u.a, u.b - u.a)) / dist(u.a, u.b);
  82. }//检查三点共线 pt1 pt2是线
  83. bool check_collinear(cv::Point3d pt1, cv::Point3d pt2, cv::Point3d pt3) {
  84.     line3 line(pt1, pt2);
  85.     double d = dist_point_to_line(pt3, line);
  86.     if (sign(d) == 0) {
  87.         return true;//共线
  88.     }
  89.     return false;
  90. }//三点求圆
  91. void centerCircle3d(cv::Point3d pt1, cv::Point3d pt2, cv::Point3d pt3, double& radius,cv::Point3d &center)
  92. {
  93.     double a1 = (pt1.y * pt2.z - pt2.y * pt1.z - pt1.y * pt3.z + pt3.y * pt1.z + pt2.y * pt3.z - pt3.y * pt2.z),
  94.         b1 = -(pt1.x * pt2.z - pt2.x * pt1.z - pt1.x * pt3.z + pt3.x * pt1.z + pt2.x * pt3.z - pt3.x * pt2.z),
  95.         c1 = (pt1.x * pt2.y - pt2.x * pt1.y - pt1.x * pt3.y + pt3.x * pt1.y + pt2.x * pt3.y - pt3.x * pt2.y),
  96.         d1 = -(pt1.x * pt2.y * pt3.z - pt1.x * pt3.y * pt2.z - pt2.x * pt1.y * pt3.z + pt2.x * pt3.y * pt1.z + pt3.x * pt1.y * pt2.z - pt3.x * pt2.y * pt1.z);
  97.     double a2 = 2 * (pt2.x - pt1.x),
  98.         b2 = 2 * (pt2.y - pt1.y),
  99.         c2 = 2 * (pt2.z - pt1.z),
  100.         d2 = pt1.x * pt1.x + pt1.y * pt1.y + pt1.z * pt1.z - pt2.x * pt2.x - pt2.y * pt2.y - pt2.z * pt2.z;
  101.     double a3 = 2 * (pt3.x - pt1.x),
  102.         b3 = 2 * (pt3.y - pt1.y),
  103.         c3 = 2 * (pt3.z - pt1.z),
  104.         d3 = pt1.x * pt1.x + pt1.y * pt1.y + pt1.z * pt1.z - pt3.x * pt3.x - pt3.y * pt3.y - pt3.z * pt3.z;
  105.     double x = -(b1 * c2 * d3 - b1 * c3 * d2 - b2 * c1 * d3 + b2 * c3 * d1 + b3 * c1 * d2 - b3 * c2 * d1)
  106.         / (a1 * b2 * c3 - a1 * b3 * c2 - a2 * b1 * c3 + a2 * b3 * c1 + a3 * b1 * c2 - a3 * b2 * c1);
  107.     double y = (a1 * c2 * d3 - a1 * c3 * d2 - a2 * c1 * d3 + a2 * c3 * d1 + a3 * c1 * d2 - a3 * c2 * d1)
  108.         / (a1 * b2 * c3 - a1 * b3 * c2 - a2 * b1 * c3 + a2 * b3 * c1 + a3 * b1 * c2 - a3 * b2 * c1);
  109.     double z = -(a1 * b2 * d3 - a1 * b3 * d2 - a2 * b1 * d3 + a2 * b3 * d1 + a3 * b1 * d2 - a3 * b2 * d1)
  110.         / (a1 * b2 * c3 - a1 * b3 * c2 - a2 * b1 * c3 + a2 * b3 * c1 + a3 * b1 * c2 - a3 * b2 * c1);
  111.     radius = sqrt((pt1.x - x) * (pt1.x - x) + (pt1.y - y) * (pt1.y - y) + (pt1.z - z) * (pt1.z - z));
  112.     center = { x,y,z };
  113. }bool Fitting_radius(cv::Point3d pt1, cv::Point3d pt2, cv::Point3d pt3,double &r,cv::Point3d &center) {
  114.    
  115.     if (check_collinear(pt1, pt2, pt3)) {
  116.         return false;
  117.     }
  118.     centerCircle3d(pt1, pt2, pt3, r,center);
  119.     return true;
  120. }bool get_intersection_foot(cv::Point3d pt1, cv::Point3d pt2, cv::Point3d pt3, cv::Point3d& intersection)
  121. {
  122.     if (check_collinear(pt1, pt2, pt3)) {
  123.         return false;
  124.     }
  125.     line3 line(pt1, pt2);
  126.     intersection = vertical_foot(pt3, line);
  127.     return true;
  128. }double get_inc_circle_R(const cv::Point3d& p1, const cv::Point3d& p2, const cv::Point3d& p3)
  129. {
  130.     //三角形三边边长
  131.     double p2p1, p3p2, p1p3;
  132.     p2p1 = norm(p2 - p1);
  133.     p3p2 = norm(p3 - p2);
  134.     p1p3 = norm(p1 - p3);
  135.     //三角形半周长
  136.     double semiperimeter;
  137.     semiperimeter = (p2p1 + p3p2 + p1p3) / 2.0;
  138.     //三角形面积
  139.     double area;
  140.     area = sqrt(semiperimeter * (semiperimeter - p2p1) * (semiperimeter - p3p2) * (semiperimeter - p1p3));
  141.     //计算三角形内接圆半径
  142.     double radius;
  143.     radius = 2 * area / (p2p1 + p3p2 + p1p3);
  144.     return radius;
  145. }cv::Point3d get_midPoint(const cv::Point3d pt1, const cv::Point3d pt2)
  146. {
  147.     cv::Point3d mid;
  148.     cv::Point3d vec = pt2 - pt1;
  149.     mid = pt1 + 0.5 * (vec);
  150.     return mid;
  151. }double convert_angle_360(double src)
  152. {
  153.     double dst = src;
  154.     while(dst >= 360) {
  155.         dst -= 360;
  156.     }
  157.     while (dst < 0) {
  158.         dst += 360;
  159.     }
  160.     return dst;
  161. }
复制代码




免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

徐锦洪

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表