// This file is part of libigl, a simple c++ geometry processing library. // // Copyright (C) 2017 Alec Jacobson // // 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 "shape_diameter_function.h" #include "random_dir.h" #include "barycenter.h" #include "ray_mesh_intersect.h" #include "per_vertex_normals.h" #include "per_face_normals.h" #include "EPS.h" #include "Hit.h" #include "parallel_for.h" #include #include #include template < typename DerivedP, typename DerivedN, typename DerivedS > IGL_INLINE void igl::shape_diameter_function( const std::function< double( const Eigen::Vector3f&, const Eigen::Vector3f&) > & shoot_ray, const Eigen::PlainObjectBase & P, const Eigen::PlainObjectBase & N, const int num_samples, Eigen::PlainObjectBase & S) { using namespace Eigen; const int n = P.rows(); // Resize output S.resize(n,1); // Embree seems to be parallel when constructing but not when tracing rays const MatrixXf D = random_dir_stratified(num_samples).cast(); const auto & inner = [&P,&N,&num_samples,&D,&S,&shoot_ray](const int p) { const Vector3f origin = P.row(p).template cast(); const Vector3f normal = N.row(p).template cast(); int num_hits = 0; double total_distance = 0; for(int s = 0;s 0) { // reverse ray d *= -1; } const double dist = shoot_ray(origin,d); if(std::isfinite(dist)) { total_distance += dist; num_hits++; } } S(p) = total_distance/(double)num_hits; }; parallel_for(n,inner,1000); } template < typename DerivedV, int DIM, typename DerivedF, typename DerivedP, typename DerivedN, typename DerivedS > IGL_INLINE void igl::shape_diameter_function( const igl::AABB & aabb, const Eigen::PlainObjectBase & V, const Eigen::PlainObjectBase & F, const Eigen::PlainObjectBase & P, const Eigen::PlainObjectBase & N, const int num_samples, Eigen::PlainObjectBase & S) { const auto & shoot_ray = [&aabb,&V,&F]( const Eigen::Vector3f& _s, const Eigen::Vector3f& dir)->double { Eigen::Vector3f s = _s+1e-4*dir; igl::Hit hit; if(aabb.intersect_ray( V, F, s .cast().eval(), dir.cast().eval(), hit)) { return hit.t; }else { return std::numeric_limits::infinity(); } }; return shape_diameter_function(shoot_ray,P,N,num_samples,S); } template < typename DerivedV, typename DerivedF, typename DerivedP, typename DerivedN, typename DerivedS > IGL_INLINE void igl::shape_diameter_function( const Eigen::PlainObjectBase & V, const Eigen::PlainObjectBase & F, const Eigen::PlainObjectBase & P, const Eigen::PlainObjectBase & N, const int num_samples, Eigen::PlainObjectBase & S) { if(F.rows() < 100) { // Super naive const auto & shoot_ray = [&V,&F]( const Eigen::Vector3f& _s, const Eigen::Vector3f& dir)->double { Eigen::Vector3f s = _s+1e-4*dir; igl::Hit hit; if(ray_mesh_intersect(s,dir,V,F,hit)) { return hit.t; }else { return std::numeric_limits::infinity(); } }; return shape_diameter_function(shoot_ray,P,N,num_samples,S); } AABB aabb; aabb.init(V,F); return shape_diameter_function(aabb,V,F,P,N,num_samples,S); } template < typename DerivedV, typename DerivedF, typename DerivedS> IGL_INLINE void igl::shape_diameter_function( const Eigen::PlainObjectBase & V, const Eigen::PlainObjectBase & F, const bool per_face, const int num_samples, Eigen::PlainObjectBase & S) { if (per_face) { DerivedV N; igl::per_face_normals(V, F, N); DerivedV P; igl::barycenter(V, F, P); return igl::shape_diameter_function(V, F, P, N, num_samples, S); } else { DerivedV N; igl::per_vertex_normals(V, F, N); return igl::shape_diameter_function(V, F, V, N, num_samples, S); } } #ifdef IGL_STATIC_LIBRARY // Explicit template instantiation template void igl::shape_diameter_function, Eigen::Matrix, Eigen::Matrix >(std::function const&, Eigen::Matrix const&)> const&, Eigen::PlainObjectBase > const&, Eigen::PlainObjectBase > const&, int, Eigen::PlainObjectBase >&); template void igl::shape_diameter_function, Eigen::Matrix, Eigen::Matrix >(std::function const&, Eigen::Matrix const&)> const&, Eigen::PlainObjectBase > const&, Eigen::PlainObjectBase > const&, int, Eigen::PlainObjectBase >&); template void igl::shape_diameter_function, Eigen::Matrix, Eigen::Matrix >(std::function const&, Eigen::Matrix const&)> const&, Eigen::PlainObjectBase > const&, Eigen::PlainObjectBase > const&, int, Eigen::PlainObjectBase >&); template void igl::shape_diameter_function, Eigen::Matrix, Eigen::Matrix >(std::function const&, Eigen::Matrix const&)> const&, Eigen::PlainObjectBase > const&, Eigen::PlainObjectBase > const&, int, Eigen::PlainObjectBase >&); template void igl::shape_diameter_function, Eigen::Matrix, Eigen::Matrix >(Eigen::PlainObjectBase > const&, Eigen::PlainObjectBase > const&, bool, int, Eigen::PlainObjectBase >&); #endif