int igl::unproject_in_mesh( const int x, const int y, const igl::EmbreeIntersector & ei, Eigen::PlainObjectBase<Derivedobj> & obj, std::vector<igl::Hit > & hits) { using namespace igl; using namespace std; using namespace Eigen; // Source and direction on screen Vector3f win_s = Vector3f(x,y,0); Vector3f win_d(x,y,1); // Source, destination and direction in world Vector3f s,d,dir; unproject(win_s,s); unproject(win_d,d); dir = d-s; // Shoot ray, collect all hits (could just collect first two) int num_rays_shot; hits.clear(); ei.intersectRay(s,dir,hits,num_rays_shot); switch(hits.size()) { case 0: break; case 1: { obj = (s + dir*hits[0].t).cast<typename Derivedobj::Scalar>(); break; } case 2: default: { obj = 0.5*((s + dir*hits[0].t) + (s + dir*hits[1].t)).cast<typename Derivedobj::Scalar>(); break; } } return hits.size(); }
int main(int argc, char * argv[]) { using namespace Eigen; using namespace igl; using namespace std; // init mesh string filename = "../shared/beast.obj"; if(argc < 2) { cerr<<"Usage:"<<endl<<" ./example input.obj"<<endl; cout<<endl<<"Opening default mesh..."<<endl; }else { // Read and prepare mesh filename = argv[1]; } // dirname, basename, extension and filename string d,b,ext,f; pathinfo(filename,d,b,ext,f); // Convert extension to lower case transform(ext.begin(), ext.end(), ext.begin(), ::tolower); vector<vector<double > > vV,vN,vTC; vector<vector<int > > vF,vFTC,vFN; if(ext == "obj") { // Convert extension to lower case if(!igl::readOBJ(filename,vV,vTC,vN,vF,vFTC,vFN)) { return 1; } }else if(ext == "off") { // Convert extension to lower case if(!igl::readOFF(filename,vV,vF,vN)) { return 1; } }else if(ext == "wrl") { // Convert extension to lower case if(!igl::readWRL(filename,vV,vF)) { return 1; } //}else //{ // // Convert extension to lower case // MatrixXi T; // if(!igl::readMESH(filename,V,T,F)) // { // return 1; // } // //if(F.size() > T.size() || F.size() == 0) // { // boundary_faces(T,F); // } } if(vV.size() > 0) { if(!list_to_matrix(vV,V)) { return 1; } triangulate(vF,F); } // Compute normals, centroid, colors, bounding box diagonal per_vertex_normals(V,F,N); mid = 0.5*(V.colwise().maxCoeff() + V.colwise().minCoeff()); bbd = (V.colwise().maxCoeff() - V.colwise().minCoeff()).maxCoeff(); // Init embree ei.init(V.cast<float>(),F.cast<int>()); // Init glut glutInit(&argc,argv); if( !TwInit(TW_OPENGL, NULL) ) { // A fatal error occured fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError()); return 1; } // Create a tweak bar rebar.TwNewBar("TweakBar"); rebar.TwAddVarRW("scene_rot", TW_TYPE_QUAT4F, &scene_rot, ""); rebar.TwAddVarRW("lights_on", TW_TYPE_BOOLCPP, &lights_on, "key=l"); rebar.TwAddVarRW("color", TW_TYPE_COLOR4F, color.data(), "colormode=hls"); rebar.TwAddVarRW("ao_factor", TW_TYPE_DOUBLE, &ao_factor, "min=0 max=1 step=0.2 keyIncr=] keyDecr=[ "); rebar.TwAddVarRW("ao_normalize", TW_TYPE_BOOLCPP, &ao_normalize, "key=n"); rebar.TwAddVarRW("ao_on", TW_TYPE_BOOLCPP, &ao_on, "key=a"); rebar.TwAddVarRW("light_intensity", TW_TYPE_DOUBLE, &light_intensity, "min=0 max=0.4 step=0.1 keyIncr=} keyDecr={ "); rebar.load(REBAR_NAME); glutInitDisplayString( "rgba depth double samples>=8 "); glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)/2.0,glutGet(GLUT_SCREEN_HEIGHT)); glutCreateWindow("ambient-occlusion"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(key); glutMouseFunc(mouse); glutMotionFunc(mouse_drag); glutPassiveMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT); glutMainLoop(); return 0; }
int main(int argc, char * argv[]) { using namespace std; using namespace Eigen; using namespace igl; string filename = "../shared/decimated-knight.obj"; string skel_filename = ""; output_filename = ""; switch(argc) { case 4: output_filename = argv[3]; //fall through case 3: skel_filename = argv[2]; if(output_filename.size() == 0) { output_filename = skel_filename; } //fall through case 2: // Read and prepare mesh filename = argv[1]; break; default: cerr<<"Usage:"<<endl<<" ./example input.obj [input/output.tgf]"<<endl; cout<<endl<<"Opening default mesh..."<<endl; } // print key commands cout<<"[Click] and [drag] Create bone (or select node) and reposition."<<endl; cout<<"⇧ +[Click] and [drag] Select node (or create one) and _pull out_ new bone."<<endl; cout<<"⌥ +[Click] and [drag] Rotate secene."<<endl; cout<<"⌫ Delete selected node(s) and incident bones."<<endl; cout<<"A,a Select all."<<endl; cout<<"D,d Deselect all."<<endl; cout<<"C,c Snap close nodes."<<endl; cout<<"P,p Split \"parent\" bone(s) of selection by creating new node(s)."<<endl; cout<<"R,r Breadth first search at selection to redirect skeleton into tree."<<endl; cout<<"S,s Save current skeleton to output .tgf file."<<endl; cout<<"U,u Project then unproject inside mesh (as if dragging each by ε)."<<endl; cout<<"Y,Y Symmetrize selection over plane through object centroid and right vector."<<endl; cout<<"Z,z Snap to canonical view."<<endl; cout<<"⌘ Z Undo."<<endl; cout<<"⇧ ⌘ Z Redo."<<endl; cout<<"^C,ESC Exit (without saving)."<<endl; // dirname, basename, extension and filename string dir,base,ext,name; pathinfo(filename,dir,base,ext,name); // Convert extension to lower case transform(ext.begin(), ext.end(), ext.begin(), ::tolower); vector<vector<double > > vV,vN,vTC; vector<vector<int > > vF,vFTC,vFN; if(ext == "obj") { // Convert extension to lower case if(!igl::readOBJ(filename,vV,vTC,vN,vF,vFTC,vFN)) { return 1; } }else if(ext == "off") { // Convert extension to lower case if(!igl::readOFF(filename,vV,vF,vN)) { return 1; } }else if(ext == "wrl") { // Convert extension to lower case if(!igl::readWRL(filename,vV,vF)) { return 1; } //}else //{ // // Convert extension to lower case // MatrixXi T; // if(!igl::readMESH(filename,V,T,F)) // { // return 1; // } // //if(F.size() > T.size() || F.size() == 0) // { // boundary_facets(T,F); // } } if(vV.size() > 0) { if(!list_to_matrix(vV,V)) { return 1; } polygon_mesh_to_triangle_mesh(vF,F); } if(output_filename.size() == 0) { output_filename = dir+"/"+name+".tgf"; } if(file_exists(output_filename.c_str())) { cout<<YELLOWGIN("Output set to overwrite "<<output_filename)<<endl; }else { cout<<BLUEGIN("Output set to "<<output_filename)<<endl; } if(skel_filename.length() > 0) { readTGF(skel_filename,s.C,s.BE); } init_relative(); ei.init(V.cast<float>(),F.cast<int>()); // Init glut glutInit(&argc,argv); if( !TwInit(TW_OPENGL, NULL) ) { // A fatal error occured fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError()); return 1; } // Create a tweak bar rebar.TwNewBar("TweakBar"); rebar.TwAddVarRW("camera_rotation", TW_TYPE_QUAT4D, camera.m_rotation_conj.coeffs().data(), "open readonly=true"); TwType RotationTypeTW = ReTwDefineEnumFromString("RotationType", "igl_trackball,two-a...-fixed-up"); rebar.TwAddVarCB( "rotation_type", RotationTypeTW, set_rotation_type,get_rotation_type,NULL,"keyIncr=] keyDecr=["); rebar.TwAddVarRW("skeleton_on_top", TW_TYPE_BOOLCPP,&skeleton_on_top,"key=O"); rebar.TwAddVarRW("wireframe", TW_TYPE_BOOLCPP,&wireframe,"key=l"); TwType SkelStyleTypeTW = ReTwDefineEnumFromString("SkelStyleType", "3d,vector-graphics"); rebar.TwAddVarRW("style",SkelStyleTypeTW,&skel_style,""); rebar.TwAddVarRW("alpha",TW_TYPE_DOUBLE,&alpha, "keyIncr=} keyDecr={ min=0 max=1 step=0.1"); rebar.load(REBAR_NAME); // Init antweakbar glutInitDisplayString( "rgba depth double samples>=8 "); glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)/2.0,glutGet(GLUT_SCREEN_HEIGHT)/2.0); glutCreateWindow("skeleton-builder"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(key); glutMouseFunc(mouse); glutMotionFunc(mouse_drag); glutPassiveMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT); glutMainLoop(); return 0; }
IGL_INLINE void igl::bone_visible( const Eigen::PlainObjectBase<DerivedV> & V, const Eigen::PlainObjectBase<DerivedF> & F, const igl::EmbreeIntersector & ei, const Eigen::PlainObjectBase<DerivedSD> & s, const Eigen::PlainObjectBase<DerivedSD> & d, Eigen::PlainObjectBase<Derivedflag> & flag) { using namespace igl; using namespace std; using namespace Eigen; flag.resize(V.rows()); const double sd_norm = (s-d).norm(); // Embree seems to be parallel when constructing but not when tracing rays #pragma omp parallel for // loop over mesh vertices for(int v = 0;v<V.rows();v++) { const Vector3d Vv = V.row(v); // Project vertex v onto line segment sd //embree.intersectSegment double t,sqrd; Vector3d projv; // degenerate bone, just snap to s if(sd_norm < DOUBLE_EPS) { t = 0; sqrd = (Vv-s).array().pow(2).sum(); projv = s; }else { // project onto (infinite) line project_to_line( Vv(0),Vv(1),Vv(2),s(0),s(1),s(2),d(0),d(1),d(2), projv(0),projv(1),projv(2),t,sqrd); // handle projections past endpoints if(t<0) { t = 0; sqrd = (Vv-s).array().pow(2).sum(); projv = s; } else if(t>1) { t = 1; sqrd = (Vv-d).array().pow(2).sum(); projv = d; } } igl::Hit hit; // perhaps 1.0 should be 1.0-epsilon, or actually since we checking the // incident face, perhaps 1.0 should be 1.0+eps const Vector3d dir = (Vv-projv)*1.0; if(ei.intersectSegment( projv.template cast<float>(), dir.template cast<float>(), hit)) { // mod for double sided lighting const int fi = hit.id % F.rows(); //if(v == 1228-1) //{ // Vector3d bc,P; // bc << 1 - hit.u - hit.v, hit.u, hit.v; // barycentric // P = V.row(F(fi,0))*bc(0) + // V.row(F(fi,1))*bc(1) + // V.row(F(fi,2))*bc(2); // cout<<(fi+1)<<endl; // cout<<bc.transpose()<<endl; // cout<<P.transpose()<<endl; // cout<<hit.t<<endl; // cout<<(projv + dir*hit.t).transpose()<<endl; // cout<<Vv.transpose()<<endl; //} // Assume hit is valid, so not visible flag(v) = false; // loop around corners of triangle for(int c = 0;c<F.cols();c++) { if(F(fi,c) == v) { // hit self, so no hits before, so vertex v is visible flag(v) = true; break; } } // Hit is actually past v if(!flag(v) && (hit.t*hit.t*dir.squaredNorm())>sqrd) { flag(v) = true; } }else { // no hit so vectex v is visible flag(v) = true; } } }