Browse Source

started coding orient_outward_ao, not done yet

Former-commit-id: b6b1c348c6ffd513ca7cfddfd402e03edbfe949e
Kenshi Takayama (kenshi 11 years ago
parent
commit
37c845a452

+ 2 - 1
examples/patches/example.cpp

@@ -28,6 +28,7 @@
 #include <igl/boost/components.h>
 #include <igl/boost/bfs_orient.h>
 #include <igl/orient_outward.h>
+#include <igl/orient_outward_ao.h>
 
 #include <Eigen/Core>
 #include <Eigen/Geometry>
@@ -519,7 +520,7 @@ void randomly_color(
   }
 }
 
-void randomize_colors(void * /*clientData*/)
+void TW_CALL randomize_colors(void * /*clientData*/)
 {
   push_undo();
   randomly_color(CC,s.C);

+ 14 - 0
examples/patches/example.vcxproj

@@ -53,6 +53,7 @@
       <Optimization>Disabled</Optimization>
       <PreprocessorDefinitions>IGL_HEADER_ONLY;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
+      <AdditionalIncludeDirectories>C:\Users\kenshi\dev\igl_hg\ext_toolboxes\embree-1.1beta\rtcore;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
     </ClCompile>
     <Link>
       <SubSystem>Console</SubSystem>
@@ -69,6 +70,7 @@
       <IntrinsicFunctions>true</IntrinsicFunctions>
       <PreprocessorDefinitions>IGL_HEADER_ONLY;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
+      <AdditionalIncludeDirectories>C:\Users\kenshi\dev\igl_hg\ext_toolboxes\embree-1.1beta\rtcore;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
     </ClCompile>
     <Link>
       <SubSystem>Console</SubSystem>
@@ -78,8 +80,20 @@
     </Link>
   </ItemDefinitionGroup>
   <ItemGroup>
+    <ClCompile Include="..\..\include\igl\orient_outward.cpp">
+      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
+      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
+    </ClCompile>
+    <ClCompile Include="..\..\include\igl\orient_outward_ao.cpp">
+      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
+      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
+    </ClCompile>
     <ClCompile Include="example.cpp" />
   </ItemGroup>
+  <ItemGroup>
+    <ClInclude Include="..\..\include\igl\orient_outward.h" />
+    <ClInclude Include="..\..\include\igl\orient_outward_ao.h" />
+  </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>

+ 11 - 0
examples/patches/example.vcxproj.filters

@@ -2,5 +2,16 @@
 <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <ItemGroup>
     <ClCompile Include="example.cpp" />
+    <ClCompile Include="..\..\include\igl\orient_outward.cpp" />
+    <ClCompile Include="..\..\include\igl\orient_outward_ao.cpp" />
+  </ItemGroup>
+  <ItemGroup>
+    <Filter Include="igl">
+      <UniqueIdentifier>{53eff656-6473-409c-9ad5-cac1983d91e1}</UniqueIdentifier>
+    </Filter>
+  </ItemGroup>
+  <ItemGroup>
+    <ClInclude Include="..\..\include\igl\orient_outward_ao.h" />
+    <ClInclude Include="..\..\include\igl\orient_outward.h" />
   </ItemGroup>
 </Project>

+ 95 - 0
include/igl/orient_outward_ao.cpp

@@ -0,0 +1,95 @@
+#include "orient_outward_ao.h"
+#include "per_face_normals.h"
+#include "barycenter.h"
+#include "doublearea.h"
+#include "matlab_format.h"
+#include "embree/ambient_occlusion.h"
+#include <iostream>
+#include <random>
+
+template <
+  typename DerivedV, 
+  typename DerivedF, 
+  typename DerivedC, 
+  typename DerivedFF, 
+  typename DerivedI>
+IGL_INLINE void igl::orient_outward_ao(
+  const Eigen::PlainObjectBase<DerivedV> & V,
+  const Eigen::PlainObjectBase<DerivedF> & F,
+  const Eigen::PlainObjectBase<DerivedC> & C,
+  const igl::EmbreeIntersector<PointMatrixType,FaceMatrixType,RowVector3> & ei,
+  const int num_samples,
+  Eigen::PlainObjectBase<DerivedFF> & FF,
+  Eigen::PlainObjectBase<DerivedI> & I)
+{
+  using namespace Eigen;
+  using namespace std;
+  assert(C.rows() == F.rows());
+  assert(F.cols() == 3);
+  assert(V.cols() == 3);
+
+  // number of faces
+  const int m = F.rows();
+  // number of patches
+  const int num_cc = C.maxCoeff()+1;
+  I.resize(num_cc);
+  if(&FF != &F)
+  {
+    FF = F;
+  }
+  PlainObjectBase<DerivedV> N;
+  Matrix<typename DerivedV::Scalar,Dynamic,1> A;
+  per_face_normals(V,F,N);
+  doublearea(V,F,A);
+  double minarea = A.minCoeff();
+  mt19937 engine;
+  engine.seed(time(0));
+  vector<int> ddist_probability(m);
+  for (int f = 0; f < m; ++f)
+      ddist_probability[f] = static_cast<int>(A(f) * 100. / minarea);
+  discrete_distribution<int> ddist(dist_probability.begin(), dist_probability.end());
+  uniform_real_distribution<double> rdist;
+  VectorXi face_occluded_front(m, 0);
+  VectorXi face_occluded_back (m, 0);
+#pragma omp parallel for
+  for (int i = 0; i < num_samples; ++i) {
+    int f = dist(engine);   // select face with probability proportional to face area
+    double t0 = rdist(engine);
+    double t1 = rdist(engine);
+    double t2 = rdist(engine);
+    double t_sum = t0 + t1 + t2;
+    t0 /= t_sum;
+    t1 /= t_sum;
+    t2 /= t_sum;
+    RowVector3d p = t0 * V.row(F(f,0)) + t1 * V.row(F(f,1)) + t1 * V.row(F(f,2));
+    RowVector3d n = N.row(f);
+    bool is_backside = rdist(engine) < 0.5;
+    if (is_backside)
+        n *= -1;
+    Matrix<typename DerivedV::Scalar,Dynamic,1> S;
+    ambient_occlusion(ei, p, n, 1, S);
+  
+  }
+  // take area weighted average
+  for(int c = 0;c<num_cc;c++)
+  {
+    //dot(c) /= (typename DerivedV::Scalar) totA(c);
+    //if(dot(c) < 0)
+    bool b = true;
+    I(c) = b;
+  }
+  // flip according to I
+  for(int f = 0;f<m;f++)
+  {
+    if(I(C(f)))
+    {
+      FF.row(f) = FF.row(f).reverse().eval();
+    }
+  }
+}
+
+#ifndef IGL_HEADER_ONLY
+// Explicit template specialization
+template void igl::orient_outward_ao<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
+#endif
+

+ 46 - 0
include/igl/orient_outward_ao.h

@@ -0,0 +1,46 @@
+#ifndef IGL_ORIENT_OUTWARD_AO_H
+#define IGL_ORIENT_OUTWARD_AO_H
+#include "igl_inline.h"
+#include <Eigen/Core>
+namespace igl
+{
+  // Forward define
+  template <
+    typename PointMatrixType,
+    typename FaceMatrixType,
+    typename RowVector3>
+  class EmbreeIntersector;
+  // Orient each component (identified by C) of a mesh (V,F) so the normals on
+  // average point away from the patch's centroid.
+  //
+  // Inputs:
+  //   V            #V by 3 list of vertex positions
+  //   F            #F by 3 list of triangle indices
+  //   C            #F list of components
+  //   ei           EmbreeIntersector containing (V,F)
+  //   num_samples  total number of rays to be shot
+  // Outputs:
+  //   FF  #F by 3 list of new triangle indices such that FF(~I,:) = F(~I,:) and
+  //     FF(I,:) = fliplr(F(I,:)) (OK if &FF = &F)
+  //   I  max(C)+1 list of whether face has been flipped
+  template <
+    typename DerivedV, 
+    typename DerivedF, 
+    typename DerivedC, 
+    typename DerivedFF, 
+    typename DerivedI>
+  IGL_INLINE void orient_outward_ao(
+    const Eigen::PlainObjectBase<DerivedV> & V,
+    const Eigen::PlainObjectBase<DerivedF> & F,
+    const Eigen::PlainObjectBase<DerivedC> & C,
+    const igl::EmbreeIntersector<PointMatrixType,FaceMatrixType,RowVector3> & ei,
+    const int num_samples,
+    Eigen::PlainObjectBase<DerivedFF> & FF,
+    Eigen::PlainObjectBase<DerivedI> & I);
+};
+
+#ifdef IGL_HEADER_ONLY
+#  include "orient_outward_ao.cpp"
+#endif
+
+#endif