// This file is part of libigl, a simple c++ geometry processing library. // // Copyright (C) 2022 Vladimir S. FONOV // // This Source Code Form is subject to the terms of the Mozilla Public License // v. 2.0. If a copy of the MPL was not distributed with this file, You can // obtain one at http://mozilla.org/MPL/2.0/ #include "fast_find_intersections.h" #include "AABB.h" #include "tri_tri_intersect.h" template < typename DerivedV1, typename DerivedF1, typename DerivedV2, typename DerivedF2, typename DerivedI, typename DerivedE> IGL_INLINE void igl::fast_find_intersections( const Eigen::MatrixBase& V1, const Eigen::MatrixBase& F1, const Eigen::MatrixBase& V2, const Eigen::MatrixBase& F2, Eigen::PlainObjectBase& intersect_pairs, Eigen::PlainObjectBase& edges ) { using AABBTree=igl::AABB; AABBTree tree; tree.init(V1,F1); fast_find_intersections(tree, V1, F1, V2, F2, intersect_pairs, edges); } template < typename DerivedV1, typename DerivedF1, typename DerivedV2, typename DerivedF2, typename DerivedI, typename DerivedE> IGL_INLINE void igl::fast_find_intersections( const AABB & tree, const Eigen::MatrixBase& V1, const Eigen::MatrixBase& F1, const Eigen::MatrixBase& V2, const Eigen::MatrixBase& F2, Eigen::PlainObjectBase& intersect_pairs, Eigen::PlainObjectBase& edges ) { using AABBTree=igl::AABB; using Scalar=typename DerivedV1::Scalar; using BBOX=Eigen::AlignedBox; using VERTEX=Eigen::Matrix; std::vector _edges; std::vector _intersect_pairs; for(int i=0; i check_intersect = [&](const AABBTree &t,int d) -> void { if(t.m_primitive != -1) //check for the actual intersection //t.is_leaf() { bool coplanar=false; VERTEX edge1,edge2; if(igl::tri_tri_intersection_test_3d( V2.row(F2(i,0)), V2.row(F2(i,1)), V2.row(F2(i,2)), V1.row(F1(t.m_primitive,0)),V1.row(F1(t.m_primitive,1)),V1.row(F1(t.m_primitive,2)), coplanar, edge1,edge2)) { if(!coplanar) { _intersect_pairs.push_back(t.m_primitive); _intersect_pairs.push_back(i); _edges.push_back(edge1); _edges.push_back(edge2); } } } else { if(t.m_box.intersects( tri_box )) { // need to check all branches check_intersect(*t.m_left, d+1); check_intersect(*t.m_right,d+1); } } }; // run actual search check_intersect(tree, 0); } edges.resize(_edges.size(), 3); for(int i=0; i!=_edges.size(); ++i) { edges.row(i) = _edges[i]; } intersect_pairs.resize(_intersect_pairs.size()/2,2); for(int i=0; i!=_intersect_pairs.size(); ++i) { intersect_pairs(i/2, i%2) = _intersect_pairs[i]; } } #ifdef IGL_STATIC_LIBRARY // Explicit template instantiation template void igl::fast_find_intersections, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::PlainObjectBase >&, Eigen::PlainObjectBase >&); template void igl::fast_find_intersections, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::PlainObjectBase >&, Eigen::PlainObjectBase >&); #endif