瀏覽代碼

Fix performance bottleneck caused by unnecessary memory allocations.

tamasmeszaros 6 年之前
父節點
當前提交
2746958a89
共有 2 個文件被更改,包括 5 次插入3 次删除
  1. 3 3
      include/igl/ray_box_intersect.cpp
  2. 2 0
      include/igl/ray_mesh_intersect.cpp

+ 3 - 3
include/igl/ray_box_intersect.cpp

@@ -6,7 +6,7 @@
 // v. 2.0. If a copy of the MPL was not distributed with this file, You can
 // 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/.
 // obtain one at http://mozilla.org/MPL/2.0/.
 #include "ray_box_intersect.h"
 #include "ray_box_intersect.h"
-#include <vector>
+#include <array>
 
 
 template <
 template <
   typename Derivedsource,
   typename Derivedsource,
@@ -101,11 +101,11 @@ IGL_INLINE bool igl::ray_box_intersect(
   // This should be precomputed and provided as input
   // This should be precomputed and provided as input
   typedef Matrix<Scalar,1,3>  RowVector3S;
   typedef Matrix<Scalar,1,3>  RowVector3S;
   const RowVector3S inv_dir( 1./dir(0),1./dir(1),1./dir(2));
   const RowVector3S inv_dir( 1./dir(0),1./dir(1),1./dir(2));
-  const std::vector<bool> sign = { inv_dir(0)<0, inv_dir(1)<0, inv_dir(2)<0};
+  const std::array<bool, 3> sign = { inv_dir(0)<0, inv_dir(1)<0, inv_dir(2)<0};
   // http://people.csail.mit.edu/amy/papers/box-jgt.pdf
   // http://people.csail.mit.edu/amy/papers/box-jgt.pdf
   // "An Efficient and Robust Ray–Box Intersection Algorithm"
   // "An Efficient and Robust Ray–Box Intersection Algorithm"
   Scalar tymin, tymax, tzmin, tzmax;
   Scalar tymin, tymax, tzmin, tzmax;
-  std::vector<RowVector3S> bounds = {box.min(),box.max()};
+  std::array<RowVector3S, 2> bounds = {box.min(),box.max()};
   tmin = ( bounds[sign[0]](0)   - origin(0)) * inv_dir(0);
   tmin = ( bounds[sign[0]](0)   - origin(0)) * inv_dir(0);
   tmax = ( bounds[1-sign[0]](0) - origin(0)) * inv_dir(0);
   tmax = ( bounds[1-sign[0]](0) - origin(0)) * inv_dir(0);
   tymin = (bounds[sign[1]](1)   - origin(1)) * inv_dir(1);
   tymin = (bounds[sign[1]](1)   - origin(1)) * inv_dir(1);

+ 2 - 0
include/igl/ray_mesh_intersect.cpp

@@ -30,6 +30,8 @@ IGL_INLINE bool igl::ray_mesh_intersect(
   Vector3d s_d = s.template cast<double>();
   Vector3d s_d = s.template cast<double>();
   Vector3d dir_d = dir.template cast<double>();
   Vector3d dir_d = dir.template cast<double>();
   hits.clear();
   hits.clear();
+  hits.reserve(F.rows());
+
   // loop over all triangles
   // loop over all triangles
   for(int f = 0;f<F.rows();f++)
   for(int f = 0;f<F.rows();f++)
   {
   {