int main(int argc, char *argv[]) { QApplication a(argc, argv); DrawingArea *area = new DrawingArea(); QPushButton *triangualteBut = new QPushButton(); triangualteBut->setText("Triangulate"); QVBoxLayout *lay = new QVBoxLayout(); lay->addWidget(area); //lay->addWidget(triangualteBut); QWidget window; window.setLayout(lay); window.show(); Triangulator *t = new Triangulator(area); QObject::connect(triangualteBut,SIGNAL(clicked()),t,SLOT(triangulate())); t->triangulate(); return a.exec(); }
Bool ApplinkImporter::parseObjFile(char* path) { char line[256]; vector<string> stringArray; vector<string> subArray; ifstream fsImportObj; fsImportObj.open(path); if(!fsImportObj.is_open()){GePrint("File " + String(path) + " not found!");return false;} GePrint("Open file: " + String(path) + "."); long faceIndex = 0; int groupIndex = -1; // First group at 0 long pointIndex = 0; long texCoordIndex = 0; String matName; long idxMat = -1; StatusSetText("Parse file..."); while(!fsImportObj.eof()) { fsImportObj.getline(line, 255); //GePrint("row: " + String(buffer)); if(!strncmp("mtllib ", line, 7)) { char file[256]; sscanf(line, "mtllib %s", file); this->mtlFilePath = path; this->mtlFilePath.ClearSuffix(); this->mtlFilePath.SetFile(file); } else if(!strncmp("usemtl ", line, 7)) { String newMat; stringArray.clear(); istringstream iss(line); copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter<vector<string> >(stringArray)); newMat = stringArray[1].c_str(); bool inMats = false; for(unsigned int i = 0; i < this->matArray.size(); i++) { if(this->matArray[i].Name == newMat) { inMats = true; idxMat = i; break; } } if(!inMats) { this->matArray.resize(this->matArray.size() + 1); this->matArray[this->matArray.size() - 1].Name = newMat; idxMat = (long)this->matArray.size() - 1; } } else if(!strncmp("g ", line, 2)) { groupIndex++; faceIndex = 0; sscanf(line, "g %s", &groups[groupIndex].groupName); } else if(!strncmp("v ", line, 2)) { sscanf(line, "v %lf %lf %lf", &(verticies[pointIndex].x), &(verticies[pointIndex].y), &(verticies[pointIndex].z)); pointIndex++; } else if(!strncmp("vt", line, 2)) { sscanf(line, "vt %lf %lf", &(this->uvw[texCoordIndex].x), &(this->uvw[texCoordIndex].y)); sscanf(line, "vt %lf %lf %lf", &(this->uvw[texCoordIndex].x), &(this->uvw[texCoordIndex].y), &(this->uvw[texCoordIndex].z)); this->uvw[texCoordIndex].y = 1 - this->uvw[texCoordIndex].y; texCoordIndex++; } else if(!strncmp("f ", line, 2)) { if(idxMat != -1) { groups[groupIndex].polyMatIdx[faceIndex] = idxMat; } //GePrint("size: " + LongToString(sizeof(line))); stringArray.clear(); istringstream iss(line); copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter<vector<string> >(stringArray)); //GePrint("size: " + LongToString(stringArray.size())); subArray.clear(); int numFaces = (int)(stringArray.size() -1); if(numFaces == 3 || numFaces == 4) { for(UInt32 i = 1; i <= numFaces; i++) { subArray = this->Split(stringArray[i], '/'); for(UInt32 j=0; j < subArray.size(); j++) { string vIdx = subArray[j]; if(j==0)//position { groups[groupIndex].faces[faceIndex].vp[i-1] = atol(vIdx.c_str()) -1; } else if(j ==1 && vIdx != "")//tex coord { groups[groupIndex].faces[faceIndex].vt[i-1] = atol(vIdx.c_str()) - 1; } //Normals is not persent in this plugin. } } if(numFaces == 3)// this triangle, non quad. { groups[groupIndex].faces[faceIndex].vp[3] = atol(subArray[0].c_str()) - 1; if(subArray.size() > 1 && subArray[1] != "") { groups[groupIndex].faces[faceIndex].vt[3] = atol(subArray[1].c_str()) - 1; } } faceIndex++; } else// N-Gons { Triangulator* t = createTriangulator(); vector<UInt32> vpIdxs; vector<UInt32> vtIdxs; for(UInt32 j=1; j <= numFaces; j++) { subArray = this->Split(stringArray[j], '/'); UInt32 idx = atol(subArray[0].c_str()) -1; vpIdxs.push_back(idx); if(subArray[1] != "") { vtIdxs.push_back(atol(subArray[1].c_str()) -1); } t->addPoint(verticies[idx].x, verticies[idx].y, verticies[idx].z); } unsigned int tcount; unsigned int *indices = t->triangulate(tcount); for(UInt32 f = 0; f < tcount; f++) { for(UInt32 v=0; v < 4; v++) { UInt32 vv = 2-v; if(v == 3) vv = 0; groups[groupIndex].faces[faceIndex].vp[v] = vpIdxs[indices[f*3+vv]];//vp if(vpIdxs.size() > 0)//vt { groups[groupIndex].faces[faceIndex].vt[v] = vtIdxs[indices[f*3+vv]]; } } faceIndex++; //GePrint("Face3: " + LongToString(groups[groupIndex].faces[faceIndex].vp[0]) + ", " + LongToString(groups[groupIndex].faces[faceIndex].vp[1]) + ", " + LongToString(groups[groupIndex].faces[faceIndex].vp[2])); } releaseTriangulator(t); } } } fsImportObj.close(); return true; }
//----------------------------------------------------------------------- void Extruder::_extrudeCapImpl(TriangleBuffer& buffer) const { std::vector<int> indexBuffer; PointList pointList; buffer.rebaseOffset(); Triangulator t; if (mShapeToExtrude) t.setShapeToTriangulate(mShapeToExtrude); else t.setMultiShapeToTriangulate(mMultiShapeToExtrude); t.triangulate(indexBuffer, pointList); buffer.estimateIndexCount(2*indexBuffer.size()); buffer.estimateVertexCount(2*pointList.size()); //begin cap buffer.rebaseOffset(); Quaternion qBegin = Utils::_computeQuaternion(mExtrusionPath->getDirectionAfter(0)); if (mRotationTrack) { Real angle = mRotationTrack->getFirstValue(); qBegin = qBegin*Quaternion((Radian)angle, Vector3::UNIT_Z); } Real scaleBegin=1.; if (mScaleTrack) scaleBegin = mScaleTrack->getFirstValue(); for (size_t j =0;j<pointList.size();j++) { Vector2 vp2 = pointList[j]; Vector3 vp(vp2.x, vp2.y, 0); Vector3 normal = -Vector3::UNIT_Z; Vector3 newPoint = mExtrusionPath->getPoint(0)+qBegin*(scaleBegin*vp); addPoint(buffer, newPoint, qBegin*normal, vp2); } for (size_t i=0;i<indexBuffer.size()/3;i++) { buffer.index(indexBuffer[i*3]); buffer.index(indexBuffer[i*3+2]); buffer.index(indexBuffer[i*3+1]); } // end cap buffer.rebaseOffset(); Quaternion qEnd = Utils::_computeQuaternion(mExtrusionPath->getDirectionBefore(mExtrusionPath->getSegCount())); if (mRotationTrack) { Real angle = mRotationTrack->getLastValue(); qEnd = qEnd*Quaternion((Radian)angle, Vector3::UNIT_Z); } Real scaleEnd=1.; if (mScaleTrack) scaleEnd = mScaleTrack->getLastValue(); for (size_t j =0;j<pointList.size();j++) { Vector2 vp2 = pointList[j]; Vector3 vp(vp2.x, vp2.y, 0); Vector3 normal = Vector3::UNIT_Z; Vector3 newPoint = mExtrusionPath->getPoint(mExtrusionPath->getSegCount())+qEnd*(scaleEnd*vp); addPoint(buffer, newPoint, qEnd*normal, vp2); } for (size_t i=0;i<indexBuffer.size()/3;i++) { buffer.index(indexBuffer[i*3]); buffer.index(indexBuffer[i*3+1]); buffer.index(indexBuffer[i*3+2]); } }
int main( int argc, const char* argv[] ) { double max_area; double min_angle; po::options_description po_desc( "Allowed options" ); po_desc.add_options() ( "help", "produce help message" ) ( "max-size,s", po::value<double>( &max_area )->default_value( 0.01 ), "set the maximum triangle area for the refinement algorithm" ) ( "min-angle,a", po::value<double>( &min_angle )->default_value( 21 ), "set the minimum angle for the refinement algorithm" ) ( "input-file", po::value<std::string>(), "input file describing the polygonal boundary (in Well-Known Text format)"); po::positional_options_description po_pdesc; po_pdesc.add("input-file", -1); po::variables_map po_vm; po::store( po::command_line_parser( argc, argv ).options( po_desc ).positional( po_pdesc ).run(), po_vm ); po::notify( po_vm ); if ( po_vm.count( "help" ) ) { std::cout << po_desc << std::endl; return EXIT_SUCCESS; } if ( ! po_vm.count( "input-file" ) ) { std::cout << "Name of the input file not specified\n"; std::cout << po_desc << std::endl; return EXIT_FAILURE; } std::cout << "Parameters used:" << std::endl << " maximum triangle area = " << max_area << std::endl << " minimum angle = " << min_angle << std::endl; try { Polygon boundary; read_polygon( po_vm["input-file"].as< std::string >(), boundary ); Mesh mesh; Triangulator<Mesh> triangulator; triangulator.triangulate( boundary, mesh ); io::write_eps( "mesh_1.eps", mesh ); mesh.make_cdt(); io::write_eps( "mesh_2.eps", mesh ); Mesher mesher; mesher.refine( mesh, max_area, min_angle ); io::write_eps( "mesh_3.eps", mesh ); io::write_stl( "mesh_3.stl", mesh ); io::write_off( "mesh_3.off", mesh ); io::write_obj( "mesh_3.obj", mesh ); io::write_ply( "mesh_3.ply", mesh ); Relax relax; relax.relax( mesh ); io::write_eps( "mesh_4.eps", mesh ); // smoother smooth; // smooth.smooth(m, 1); // Postscript_stream ps5("mesh_5.eps", m.bounding_box()); // ps5 << m; // // code does not pass a debug assert: // meshgen.refine(0.0001000, 25); // Postscript_stream ps6("mesh_6.eps", m.bounding_box()); // ps6 << m; // // smooth.smooth(m, 5); // // Postscript_stream ps7("mesh_7.eps", m.bounding_box()); // // ps7 << m; std::cout << "Final mesh:" << std::endl << " # nodes: " << mesh.number_of_nodes() << std::endl << " # edges: " << mesh.number_of_edges() << std::endl << " # faces: " << mesh.number_of_faces() << std::endl; } catch ( boost::exception& e ) { std::cerr << boost::diagnostic_information( e ); } return EXIT_SUCCESS; }
//----------------------------------------------------------------------- void Lathe::_latheCapImpl(TriangleBuffer& buffer) const { std::vector<int> indexBuffer; PointList pointList; buffer.rebaseOffset(); Triangulator t; Shape shapeCopy; MultiShape multishapeCopy; if (mShapeToExtrude) { shapeCopy = *mShapeToExtrude; shapeCopy.close(); t.setShapeToTriangulate(&shapeCopy); } else { multishapeCopy = *mMultiShapeToExtrude; multishapeCopy.close(); t.setMultiShapeToTriangulate(mMultiShapeToExtrude); } t.triangulate(indexBuffer, pointList); buffer.estimateIndexCount(2*indexBuffer.size()); buffer.estimateVertexCount(2*pointList.size()); //begin cap buffer.rebaseOffset(); Quaternion q; q.FromAngleAxis(mAngleBegin, Vector3::UNIT_Y); for (size_t j =0;j<pointList.size();j++) { Vector2 vp2 = pointList[j]; Vector3 vp(vp2.x, vp2.y, 0); Vector3 normal = Vector3::UNIT_Z; addPoint(buffer, q*vp, q*normal, vp2); } for (size_t i=0;i<indexBuffer.size()/3;i++) { buffer.index(indexBuffer[i*3]); buffer.index(indexBuffer[i*3+1]); buffer.index(indexBuffer[i*3+2]); } //end cap buffer.rebaseOffset(); q.FromAngleAxis(mAngleEnd, Vector3::UNIT_Y); for (size_t j =0;j<pointList.size();j++) { Vector2 vp2 = pointList[j]; Vector3 vp(vp2.x, vp2.y, 0); Vector3 normal = -Vector3::UNIT_Z; addPoint(buffer, q*vp, q*normal, vp2); } for (size_t i=0;i<indexBuffer.size()/3;i++) { buffer.index(indexBuffer[i*3]); buffer.index(indexBuffer[i*3+2]); buffer.index(indexBuffer[i*3+1]); } }