cgal demo

该程序使用CGAL库中的Exact_predicates_inexact_constructions_kernel精确构造内核,计算一条三维直线与四面体的所有交点。通过遍历四面体的有限面并检测直线与三角面的交点,将交点存入向量中,最后输出交点数量及坐标。

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

[求一条三维直线与一个四面体的交点及其数量(Y)]

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Triangulation_3.h>

#include <iostream>
#include <fstream>
#include <list>
#include <set>
#include <vector>

typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Triangulation_3<K>      Triangulation;
typedef Triangulation::Finite_facets_iterator Finite_facets_iterator;
typedef K::Point_3 Point_3;
typedef K::Line_3 Line_3;
typedef K::Triangle_3 Triangle_3;
typedef K::Intersect_3 Intersect_3;

int main()
{

  Point_3 linea_b(0,0,0);
  Point_3 linea_e(1,1,1);

  Line_3 lina(linea_b,linea_e);


  std::list<Point_3> L;
  L.push_front(Point_3(0,0,0));
  L.push_front(Point_3(1,0,0));
  L.push_front(Point_3(0,1,0));
  L.push_front(Point_3(0,0,1));

  Triangulation T(L.begin(), L.end());

  std::vector<Point_3> intersect_points;
  for(Finite_facets_iterator fit = T.finite_facets_begin();fit!=T.finite_facets_end();fit++)
  {
      
      Point_3 one = fit->first->vertex(T.vertex_triple_index(fit->second,0))->point();
      Point_3 two = fit->first->vertex(T.vertex_triple_index(fit->second,1))->point();
      Point_3 thr = fit->first->vertex(T.vertex_triple_index(fit->second,2))->point();

      Triangle_3 tri(one,two,thr);

      CGAL::cpp11::result_of<Intersect_3(Line_3, Triangle_3)>::type result = intersection(lina, tri);
      if (result) 
      {
        const Point_3* p = boost::get<Point_3 >(&*result);

        bool similiar = false;
        for(size_t i=0;i<intersect_points.size();i++)
        {
            Point_3 &point = intersect_points[i];
            double distance = sqrt( pow(point.x()-p->x(),2) +
                                    pow(point.y()-p->y(),2) +
                                    pow(point.z()-p->z(),2) );

            if(distance<0.000001)
                similiar = true;
        }
        if(!similiar)
            intersect_points.push_back(*p);
      }
  }

  std::cout<<"Intersect Point Number: "<<intersect_points.size()<<std::endl;
  for(size_t i=0;i<intersect_points.size();i++)
  {
      std::cout<<intersect_points[i]<<std::endl;
  }

  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lst0426

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值