void MainWindow::on_actionObject_Coordinates_Output_triggered() { QDir dir(workDirectory); //进入工作目录 if(!dir.exists() || workDirectory==""){ QMessageBox::warning(this,tr("Entering Workspace error!"),tr("No workspace has been opened!")); return; } QString image3DName=dir.absoluteFilePath(QString("NineImagesModel.md")); if(image3DName==""){ QMessageBox::warning(this,tr("Object coordinates output error!"),tr("No file \"NineImagesModel.md\" found!")); return; } IMAGE3D *image=new IMAGE3D[imageSize()]; if(!readSingleImage3D(image3DName.toStdString(),image)){ QMessageBox::warning(this,tr("Object coordinates output error!"),tr("Reading file \"NineImagesModel.md\" failed!")); return; } /* QString outFileName("ObjectCoors.txt"); if(!writeImage3DCoordinates(dir.absoluteFilePath(outFileName).toStdString(),image,imageWidth,imageHeight)){ QMessageBox::warning(this,tr("Object coordinates output error!"),tr("Writing file \"ObjectCoors.txt\" failed!")); return; } */ QString pcdFileName("ObjectCoors.pcd"); if(!writeToPCD(dir.absoluteFilePath(pcdFileName).toStdString(),image,imageWidth,imageHeight)){ QMessageBox::warning(this,tr("Object coordinates output error!"),tr("Writing file \"ObjectCoors.pcd\" failed!")); return; } delete [] image; QMessageBox::information(this,tr("Object coordinates output succeeds!"),tr("Object coordinates have been output to file \"ObjectCoors.txt\" and \"ObjectCoors.pcd\".")); }
int main(int argc, char* argv[]) { if (argc < 2) { printf("%s cloud.pts/cloud.pcd [out.pcd]\n",argv[0]); return 1; } std::vector<Point> p = LoadFromPTS(argv[1]); writeToPCD(&p,argv[2]); }
int main(int argc, char* argv[]) { if (argc < 3) { printf("Usage: ./ray_tracing input.ply outputFolder\n"); return 1; } std::vector<Point> vertices; std::vector<Triangle> faces; std::vector<Plane> planes; std::vector<Point> pointcloud; readPLY(argv[1],&vertices,&faces); //get bounding box double minX=vertices[0].x,maxX=vertices[0].x; double minY=vertices[0].y,maxY=vertices[0].y; double minZ=vertices[0].z,maxZ=vertices[0].z; // for (size_t i=1;i<vertices.size();i++) { // if (vertices[i].x < minX) minX = vertices[i].x; // else if (vertices[i].x > maxX) maxX = vertices[i].x; // if (vertices[i].y < minY) minY = vertices[i].y; // else if (vertices[i].y > maxY) maxY = vertices[i].y; // if (vertices[i].z < minZ) minZ = vertices[i].z; // else if (vertices[i].z > maxZ) maxZ = vertices[i].z; // } for (size_t i=0;i<faces.size();i++) { Triangle t = faces[i]; Point point[3] = {vertices[t.id1],vertices[t.id2],vertices[t.id3]}; for (int j=0;j<3;j++) { if (point[j].x < minX) minX = point[j].x; else if (point[j].x > maxX) maxX = point[j].x; if (point[j].y < minY) minY = point[j].y; else if (point[j].y > maxY) maxY = point[j].y; if (point[j].z < minZ) minZ = point[j].z; else if (point[j].z > maxZ) maxZ = point[j].z; } } printf("Bounding box: x:(%.2f %.2f) y:(%.2f %.2f) z:(%.2f %.2f)\n",minX,maxX,minY,maxY,minZ,maxZ); Point centroid = { (minX + maxX) / 2, (minY + maxY) / 2, (minZ + maxZ) / 2 }; //get normals for (size_t i=0;i<faces.size();i++) { Plane v = getPlane(&vertices,faces[i]); planes.push_back(v); } double resolution = 0.01; //radians int numCameras=16; char buffer[128]; #if USE_Y_VERTICAL double radius = (maxX-minX) > (maxZ-minZ) ? (maxX-minX)*2 : (maxZ-minZ)*2; #else double radius = (maxX-minX) > (maxY-minY) ? (maxX-minX)*2 : (maxY-minY)*2; #endif double noise_sigma = 0 * radius; double alpha=M_PI/2/numCameras; for (int k=0;k<numCameras;k++) { double fov = M_PI/8 + M_PI/4*rand()/RAND_MAX; pointcloud.clear(); #if USE_Y_VERTICAL Point cameraOrigin = { centroid.x + radius * sin(alpha) + noise_sigma * rand() / RAND_MAX, centroid.y + noise_sigma * rand() / RAND_MAX, centroid.z + radius * cos(alpha) + noise_sigma * rand() / RAND_MAX }; #else Point cameraOrigin = { centroid.x + radius * cos(alpha) + noise_sigma * rand() / RAND_MAX, centroid.y + radius * sin(alpha) + noise_sigma * rand() / RAND_MAX, centroid.z + noise_sigma * rand() / RAND_MAX }; #endif Vector principalDirection = { centroid.x - cameraOrigin.x, centroid.y - cameraOrigin.y, centroid.z - cameraOrigin.z }; principalDirection = normalize(principalDirection); #if RADIAL_SAMPLING for (double theta=-fov/2;theta<fov/2;theta+=resolution) { for (double phi=-fov/2;phi<fov/2;phi+=resolution) { #if USE_Y_VERTICAL Vector rayDirection = { principalDirection.z * sin(theta) * cos(phi) + principalDirection.x * cos(theta) * cos(phi), sin(phi), principalDirection.z * cos(theta) * cos(phi) - principalDirection.x * sin(theta) * cos(phi) }; #else Vector rayDirection = { principalDirection.x * cos(theta) * cos(phi) - principalDirection.y * sin(theta) * cos(phi), principalDirection.x * sin(theta) * cos(phi) + principalDirection.y * cos(theta) * cos(phi), sin(phi) }; #endif Point rayOrigin = cameraOrigin; #else #if USE_Y_VERTICAL for (double theta=minY;theta<maxY;theta+=(maxY-minY)*resolution) { for (double phi=-radius*fov/M_PI/2;phi<radius*fov/M_PI/2;phi+=(radius*fov/M_PI)*resolution) { Vector rayDirection = principalDirection; Point rayOrigin = { cameraOrigin.x + phi * principalDirection.z, theta, cameraOrigin.z - phi * principalDirection.x }; #else for (double theta=minZ;theta<maxZ;theta+=(maxZ-minZ)*resolution) { for (double phi=-radius*fov/M_PI/2;phi<radius*fov/M_PI/2;phi+=(radius*fov/M_PI)*resolution) { Vector rayDirection = principalDirection; Point rayOrigin = { cameraOrigin.x + phi * principalDirection.y, cameraOrigin.y - phi * principalDirection.x, theta }; #endif #endif bool isValid = false; Point closestPoint; double minDistance = DBL_MAX; //find intersection for each triangle for (size_t i=0;i<faces.size();i++) { double distance; if (intersects(rayOrigin,rayDirection,planes[i],&distance)) { if (distance < minDistance) { Point intersection = { rayOrigin.x + rayDirection.x * distance, rayOrigin.y + rayDirection.y * distance, rayOrigin.z + rayDirection.z * distance }; if (triangleContains(&vertices,faces[i],planes[i],intersection)) { isValid = true; closestPoint = intersection; minDistance = distance; } } } } if (isValid) { pointcloud.push_back(closestPoint); } } } alpha += 2*M_PI/numCameras; if (pointcloud.size() > 0) { sprintf(buffer,"%s/%d-cloud.pcd",argv[2],k); writeToPCD(buffer,&pointcloud); } } return 0; }
int main(int argc, char* argv[]) { if (argc < 3) { printf("Usage: ./zbuffer input.ply [output.pcd,outputFolder]\n"); return 1; } std::vector<Point> vertices; std::vector<Triangle> faces; std::vector<Point> pointcloud; char buffer[128]; bool merge = opendir(argv[2]) == NULL; readPLY(argv[1],&vertices,&faces); //get bounding box double minX=vertices[0].x,maxX=vertices[0].x; double minY=vertices[0].y,maxY=vertices[0].y; double minZ=vertices[0].z,maxZ=vertices[0].z; for (size_t i=1; i<vertices.size(); i++) { if (vertices[i].x < minX) minX = vertices[i].x; else if (vertices[i].x > maxX) maxX = vertices[i].x; if (vertices[i].y < minY) minY = vertices[i].y; else if (vertices[i].y > maxY) maxY = vertices[i].y; if (vertices[i].z < minZ) minZ = vertices[i].z; else if (vertices[i].z > maxZ) maxZ = vertices[i].z; } printf("Bounding box: x:(%.2f %.2f) y:(%.2f %.2f) z:(%.2f %.2f)\n",minX,maxX,minY,maxY,minZ,maxZ); Point centroid = { (minX + maxX) / 2, (minY + maxY) / 2, (minZ + maxZ) / 2 }; for (size_t i = 0; i < vertices.size(); i++) { vertices[i].x -= centroid.x; vertices[i].y -= centroid.y; vertices[i].z -= centroid.z; } int width = RESOLUTION; int height = RESOLUTION; OSMesaContext ctx; ctx = OSMesaCreateContextExt(OSMESA_RGB, 32, 0, 0, NULL ); unsigned char * pbuffer = new unsigned char [3 * width * height]; // Bind the buffer to the context and make it current if (!OSMesaMakeCurrent(ctx, (void*)pbuffer, GL_UNSIGNED_BYTE, width, height)) printf("fail to create MESA context\n"); OSMesaPixelStore(OSMESA_Y_UP, 0); glEnable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); glDisable(GL_CULL_FACE); glPolygonMode(GL_FRONT, GL_FILL); glMatrixMode(GL_PROJECTION); glLoadIdentity(); float fov = 70; float fov_scale = 2 * tan(fov / 2 / 180 * M_PI); float zfar = 100000; gluPerspective(fov,1,1,zfar); glViewport(0, 0, width, height); float cameraX = maxX - minX; float cameraY = maxY - minY; float cameraZ = maxZ - minZ; float cx = 0.5 * (width + 1); float cy = 0.5 * (height + 1); unsigned int* depth = new unsigned int[width * height]; float rho = sqrt(cameraX*cameraX + cameraY*cameraY); float theta = atan2(cameraY, cameraX); int depthBits=0; glGetIntegerv(GL_DEPTH_BITS, &depthBits); printf("depth buffer bits %d\n",depthBits); int numViews = INCLUDE_TOP ? NUM_CAMERAS + 2 : NUM_CAMERAS; for (int k = 0; k < numViews; k++) { // if (!merge) // pointcloud.clear(); float rx = rho * cos(theta); float ry = rho * sin(theta); theta += 2 * 3.14159265 / NUM_CAMERAS; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); if (k < NUM_CAMERAS) gluLookAt(rx,ry, cameraZ, 0,0,0, 0,0,1); else if (k == NUM_CAMERAS) gluLookAt(0,0, cameraZ*4, 0,0,0, 1,0,0); else if (k == NUM_CAMERAS + 1) gluLookAt(0,0, -cameraZ*4, 0,0,0, 1,0,0); GLfloat R[16] = {}; glGetFloatv(GL_MODELVIEW_MATRIX, R); // printf("%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n",R[0],R[1],R[2],R[3],R[4],R[5],R[6],R[7],R[8],R[9],R[10],R[11],R[12],R[13],R[14],R[15]); // printf("camera: %f %f %f\n",rx,ry,cameraZ); glBegin(GL_TRIANGLES); glColor3ub(150, 150, 150); for (size_t i = 0; i < faces.size(); i++) { Point p1 = vertices[faces[i].id1]; Point p2 = vertices[faces[i].id2]; Point p3 = vertices[faces[i].id3]; glVertex3f(p1.x, p1.y, p1.z); glVertex3f(p2.x, p2.y, p2.z); glVertex3f(p3.x, p3.y, p3.z); } glEnd(); glFinish(); // done rendering GLint outWidth, outHeight, bitPerDepth; GLboolean ret = OSMesaGetDepthBuffer(ctx, &outWidth, &outHeight, &bitPerDepth, (void**)&depth); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { float buffer_z = (float) depth[j * width + i] / 0xFFFFFFFF; if (buffer_z > 0 && buffer_z < 1) { // float z = -1 / (1 - buffer_z); float z = 1 / (buffer_z - 1 - buffer_z / zfar); float x = (i - cx) * -z / width * fov_scale; float y = (j - cy) * -z / height * fov_scale; x -= R[12]; y -= R[13]; z -= R[14]; Point p = { R[0] * x + R[1] * y + R[2] * z, R[4] * x + R[5] * y + R[6] * z, R[8] * x + R[9] * y + R[10] * z }; pointcloud.push_back(p); } } } // printf("pointcloud: %lu\n",pointcloud.size()); if (!merge && pointcloud.size() > 0) { int n=0; while (true) { sprintf(buffer,"%s/%d-cloud.pcd",argv[2],n); FILE* f = fopen(buffer,"r"); if (!f) { writeToPCD(buffer,&pointcloud); break; } fclose(f); n++; } } } if (merge && pointcloud.size() > 0) { sprintf(buffer,"%s",argv[2]); writeToPCD(buffer,&pointcloud); } // sprintf(buffer,"%s/vertex.pcd",argv[2]); // writeToPCD(buffer,&vertices); OSMesaDestroyContext(ctx); return 0; }