00001 #include <vector>
00002
00003 using namespace std;
00004
00005
00006 #include <vcg/complex/complex.h>
00007 #include <vcg/complex/algorithms/update/topology.h>
00008 #include <vcg/complex/algorithms/update/component_ep.h>
00009 #include <vcg/complex/algorithms/update/bounding.h>
00010 #include <vcg/complex/algorithms/update/quality.h>
00011 #include <vcg/complex/algorithms/update/color.h>
00012 #include <vcg/complex/algorithms/update/flag.h>
00013 #include <vcg/complex/algorithms/stat.h>
00014 #include <vcg/complex/algorithms/clean.h>
00015 #include <vcg/complex/algorithms/intersection.h>
00016 #include <vcg/space/index/grid_static_ptr.h>
00017 #include <vcg/space/index/spatial_hashing.h>
00018 #include <vcg/complex/algorithms/closest.h>
00019
00020
00021 #include <wrap/io_trimesh/import.h>
00022 #include <wrap/io_trimesh/export_ply.h>
00023
00024 using namespace vcg;
00025
00026 class MyFace;
00027 class MyEdge;
00028 class MyVertex;
00029
00030 struct MyUsedTypes : public UsedTypes< Use<MyVertex> ::AsVertexType,
00031 Use<MyEdge> ::AsEdgeType,
00032 Use<MyFace> ::AsFaceType>{};
00033
00034
00035 class MyVertex : public Vertex< MyUsedTypes, vertex::Coord3f, vertex::BitFlags, vertex::Normal3f, vertex::Mark,vertex::Color4b, vertex::Qualityf>{};
00036 class MyEdge : public Edge<MyUsedTypes>{};
00037 class MyFace : public Face <MyUsedTypes, face::VertexRef,face::BitFlags,face::Mark, face::Normal3f> {};
00038
00039 class MyMesh : public tri::TriMesh< vector<MyVertex>, vector<MyFace > >{};
00040
00041
00042 typedef vcg::GridStaticPtr<MyMesh::FaceType, MyMesh::ScalarType> TriMeshGrid;
00043
00044
00045 int main(int argc,char ** argv)
00046 {
00047 if (argc<2)
00048 {
00049 printf("\n");
00050 printf(" Compute an approximation of the shape diameter function\n");
00051 printf(" Usage: trimesh_intersection <filename> [angle samplenum]\n\n");
00052 printf(" <filename> Mesh model for which to compute the sdf (PLY format).\n");
00053 printf(" angle the wideness (degree) of the cone of ray that must be shot from each vertex (default 45)\n");
00054 printf(" samplenum the oversampling factor (0 -> one ray, 1, 9 ray, 2-> 25 rays (default 2)\n");
00055
00056 return 0;
00057 }
00058
00059 MyMesh m;
00060 int t0=clock();
00061
00062 int err = tri::io::Importer<MyMesh>::Open(m,argv[1]);
00063 if(err) {
00064 printf("Error in reading %s: '%s'\n",argv[1],tri::io::Importer<MyMesh>::ErrorMsg(err));
00065 exit(-1);
00066 }
00067
00068 float widenessRad = math::ToRad(20.0);
00069
00070 if(argc>2) {
00071 widenessRad = math::ToRad(atof(argv[2]));
00072 printf("Setting wideness to %f degree\n",atof(argv[2]));
00073 }
00074 int n_samples=2;
00075 if(argc>3) n_samples = atoi(argv[3]);
00076 int samplePerVert = (n_samples*2+ 1)*(n_samples*2+ 1);
00077 printf("Using oversampling to %i (%i sample per vertex)\n",n_samples,samplePerVert);
00078
00079
00080
00081 int dup = tri::Clean<MyMesh>::RemoveDuplicateVertex(m);
00082 int unref = tri::Clean<MyMesh>::RemoveUnreferencedVertex(m);
00083
00084 if (dup > 0 || unref > 0)
00085 printf("Removed %i duplicate and %i unreferenced vertices from mesh %s\n",dup,unref,argv[1]);
00086
00087
00088 tri::UpdateBounding<MyMesh>::Box(m);
00089 tri::UpdateNormal<MyMesh>::PerFaceNormalized(m);
00090 tri::UpdateNormal<MyMesh>::PerVertexAngleWeighted(m);
00091 tri::UpdateNormal<MyMesh>::NormalizePerVertex(m);
00092
00093 TriMeshGrid static_grid;
00094 static_grid.Set(m.face.begin(), m.face.end());
00095
00096 typedef MyMesh::ScalarType ScalarType;
00097 int t1=clock();
00098 float t;
00099 MyMesh::FaceType *rf;
00100 MyMesh::VertexIterator vi;
00101 float maxDist=m.bbox.Diag();
00102 float offset= maxDist / 10000.0;
00103 int totRay=0;
00104
00105 ScalarType deltaRad=widenessRad/(ScalarType)(n_samples*2);
00106 if(n_samples==0) deltaRad=0;
00107
00108 tri::UpdateQuality<MyMesh>::VertexConstant(m,0);
00109 for(vi=m.vert.begin();vi!=m.vert.end();++vi)
00110 {
00111 vcg::Ray3f ray;
00112 ray.SetOrigin((*vi).cP()-((*vi).cN()*offset));
00113 Point3f dir0 = -(*vi).cN();
00114 int cnt=0;
00115 ScalarType theta_init,phi_init,ro;
00116 dir0.ToPolarRad(ro,theta_init,phi_init);
00117 for (int x=-n_samples;x<=n_samples;x++)
00118 for (int y=-n_samples;y<=n_samples;y++)
00119 {
00120 ScalarType theta=theta_init+x*deltaRad;
00121 ScalarType phi=phi_init+y*deltaRad;
00122
00123 if (theta<0) theta=2.0*M_PI+theta;
00124
00125 Point3f dir;
00126 dir.FromPolarRad(ro,theta,phi);
00127 dir.Normalize();
00128 ray.SetDirection(dir);
00129
00130 rf = tri::DoRay<MyMesh,TriMeshGrid>(m,static_grid,ray,maxDist,t);
00131 if(rf)
00132 {
00133 (*vi).Q()+=t;
00134 cnt++;
00135 }
00136 }
00137 if(cnt>0){
00138 (*vi).Q()/=cnt;
00139 totRay+=cnt;
00140 }
00141 }
00142 int t2 = clock();
00143 tri::UpdateColor<MyMesh>::PerVertexQualityRamp(m);
00144 tri::io::ExporterPLY<MyMesh>::Save(m,"SDF.ply",tri::io::Mask::IOM_VERTCOLOR+tri::io::Mask::IOM_VERTQUALITY);
00145
00146 printf("Initializated in %i msec\n",t1-t0);
00147 printf("Completed in %i msec\n",t2-t1);
00148 printf("Shoot %i rays and found %i intersections\n",m.VN()*samplePerVert,totRay);
00149
00150 return 0;
00151 }
00152