int main() { std::array<std::string,3> fileName = {"cities_3.data", "cities_10.data", "cities_50.data"}; std::array<std::string,3> filePathDistance = {"shortest_3_DKT.txt", "shortest_10_DKT.txt", "shortest_50_DKT.txt"}; for (int i = 0; i < fileName.size(); i++) { int nodes = lines_count(fileName[i]); //distance matrix std::vector<std::vector<int>> d = input2vector(fileName[i]); std::ofstream fileOut1; fileOut1.open(filePathDistance[i]); std::ofstream fileOut2; fileOut2.open("timer_DKT.txt", std::ios_base::app); std::chrono::time_point<std::chrono::system_clock> start, end; start = std::chrono::system_clock::now(); for (int i = 1; i <= nodes; i++){ for (int j = i+1; j <= nodes; j++){ int infinity = 1234567;//know from the input file //set all to infinity std::vector<City> city(nodes,City(infinity,false,0)); int shortest = Dijkstra(nodes,i,j,city,d); path_recon_to_file(fileOut1, i, j, nodes, city); fileOut1 << "\t\t" << shortest; fileOut1 << std::endl; } } end = std::chrono::system_clock::now(); std::chrono::duration<double> elapsed_seconds = end-start; fileOut2 << std::fixed << std::setprecision(0)<< fileName[i] << "\t";//fix precision fileOut2 << std::fixed << std::setprecision(14)<<elapsed_seconds.count(); fileOut2 << std::endl; std::cout << "elapsed time to find shortest route with Dijkstra for " << fileName[i] << " is: " << std::fixed << std::setprecision(14)<<elapsed_seconds.count() << " seconds" << std::endl; } }
int fim_common_test() { /* * this function should test the correctness of the functions in this file. * it should be used for debug purposes, for Fim maintainance. * */ printf("%d\n",0==lines_count("" ,6)); printf("%d\n",0==lines_count("aab" ,6)); printf("%d\n",1==lines_count("aaaaba\nemk" ,6)); printf("%d\n",2==lines_count("aaaaba\nemk\n" ,6)); printf("%d\n",3==lines_count("aaaabaa\nemk\n" ,6)); printf("%d\n",*next_row("123\n",3)=='\0'); printf("%d\n",*next_row("123\n4",3)=='4'); printf("%d\n",*next_row("12",3)=='\0'); printf("%d\n",*next_row("1234",3)=='4'); return 0; }
int MiniConsole::add(const char* cso) { char *s=NULL,*b=NULL; int nc; int nl,ol=cline; char *cs=NULL;/* using co would mean provoking the compiler */ cs=dupstr(cso); if(!cs)goto rerr; nc=strlen(cs); if(!nc)goto rerr; nl=lines_count(cs,lwidth); // we count exactly the number of new entries needed in the arrays we have if((s=const_cast<char*>(strchr(cs,'\n')))!=NULL && s!=cs)nl+=(ccol+(s-cs-1))/lwidth;// single line with \n or multiline else nl+=(strlen(cs)+ccol)/lwidth; // single line, with no terminators /* * we check for room (please note that nl >= the effective new lines introduced , while * nc amounts to the exact extra room needed ) * */ if(nc+1+(int)(bp-buffer)>bsize || nl+1+cline>lsize)return -2;//no room : realloc needed ; 1 is for secur1ty scroll=scroll-nl<0?0:scroll-nl; // we copy the whole new string in our buffer strcpy(bp,cs); fim_free(cs); cs=NULL; sanitize_string_from_nongraph_except_newline(bp,0); s=bp-ccol;// we will work in our buffer space now on b=s; while(*s && (s=(char*)next_row(s,lwidth))!=NULL && *s) { line[++cline]=s;// we keep track of each new line ccol=0; bp=s; }// !s || !*s if(!*s && s-b==lwidth){line[++cline]=(bp=s);}// we keep track of the last line too if(ol==cline) { ccol=strlen(line[cline]); // we update the current (right after last) column bp+=strlen(bp); // the buffer points to the current column } else { ccol=strlen(bp); // we update the current (right after last) column bp+=ccol; // the buffer points to the current column } return 0; rerr: fim_free(cs); return -1; }
int MiniConsole::reformat(int newlwidth) { /* * This method reformats the whole buffer array; that is, it recomputes * line information for it, thus updating the whole line array contents. * It may fail, in the case a new line width (smaller) is desired, because * more line information would be needed. * * If the new lines are longer than before, then it could not fail. * Upon a successful execution, the width is updated. * * */ int nls; if(newlwidth==lwidth)return 0;//are we sure? if(newlwidth< lwidth) { // realloc needed if ( ( nls=lines_count(buffer, newlwidth) + 1 ) < lsize ) if ( grow_lines( nls )!=0 )return -1; } if(newlwidth> lwidth || ( lines_count(buffer, newlwidth) + 1 < lsize ) ) { // no realloc, no risk fim::string buf=buffer; if(((int)buf.size())==((int)(bp-buffer))) { ccol=0;cline=0;lwidth=newlwidth;*line=buffer;bp=buffer; // the easy way add(buf.c_str());// by adding a very big chunk of text, we make sure it gets formatted. return 0; } // if some error happened in buf string initialization, we return -1 return -1; } return -1; }
void wavefront_export(const mesh_t *mesh, const char *path) { // XXX: Merge faces that can be merged into bigger ones. // Allow to chose between quads or triangles. // Also export mlt file for the colors. block_t *block; voxel_vertex_t* verts; vec3_t v; int nb_quads, i, j; mat4_t mat; FILE *out; const int N = BLOCK_SIZE; UT_array *lines; line_t line, face, *line_ptr; utarray_new(lines, &line_icd); verts = calloc(N * N * N * 6 * 4, sizeof(*verts)); face = (line_t){"f "}; MESH_ITER_BLOCKS(mesh, block) { mat = mat4_identity; mat4_itranslate(&mat, block->pos.x, block->pos.y, block->pos.z); mat4_itranslate(&mat, -N / 2 + 0.5, -N / 2 + 0.5, -N / 2 + 0.5); nb_quads = block_generate_vertices(block->data, 0, verts); for (i = 0; i < nb_quads; i++) { // Put the vertices. for (j = 0; j < 4; j++) { v = vec3(verts[i * 4 + j].pos.x, verts[i * 4 + j].pos.y, verts[i * 4 + j].pos.z); v = mat4_mul_vec3(mat, v); line = (line_t){"v ", .v = v}; face.vs[j] = lines_add(lines, &line); } // Put the normals for (j = 0; j < 4; j++) { v = vec3(verts[i * 4 + j].normal.x, verts[i * 4 + j].normal.y, verts[i * 4 + j].normal.z); line = (line_t){"vn", .vn = v}; face.vns[j] = lines_add(lines, &line); } lines_add(lines, &face); } } out = fopen(path, "w"); fprintf(out, "# Goxel " GOXEL_VERSION_STR "\n"); line_ptr = NULL; while( (line_ptr = (line_t*)utarray_next(lines, line_ptr))) { if (strncmp(line_ptr->type, "v ", 2) == 0) fprintf(out, "v %g %g %g\n", VEC3_SPLIT(line_ptr->v)); } while( (line_ptr = (line_t*)utarray_next(lines, line_ptr))) { if (strncmp(line_ptr->type, "vn", 2) == 0) fprintf(out, "vn %g %g %g\n", VEC3_SPLIT(line_ptr->vn)); } while( (line_ptr = (line_t*)utarray_next(lines, line_ptr))) { if (strncmp(line_ptr->type, "f ", 2) == 0) fprintf(out, "f %d//%d %d//%d %d//%d %d//%d\n", line_ptr->vs[0], line_ptr->vns[0], line_ptr->vs[1], line_ptr->vns[1], line_ptr->vs[2], line_ptr->vns[2], line_ptr->vs[3], line_ptr->vns[3]); } fclose(out); utarray_free(lines); } void ply_export(const mesh_t *mesh, const char *path) { block_t *block; voxel_vertex_t* verts; vec3_t v; uvec3b_t c; int nb_quads, i, j; mat4_t mat; FILE *out; const int N = BLOCK_SIZE; UT_array *lines; line_t line, face, *line_ptr; utarray_new(lines, &line_icd); verts = calloc(N * N * N * 6 * 4, sizeof(*verts)); face = (line_t){"f "}; MESH_ITER_BLOCKS(mesh, block) { mat = mat4_identity; mat4_itranslate(&mat, block->pos.x, block->pos.y, block->pos.z); mat4_itranslate(&mat, -N / 2 + 0.5, -N / 2 + 0.5, -N / 2 + 0.5); nb_quads = block_generate_vertices(block->data, 0, verts); for (i = 0; i < nb_quads; i++) { // Put the vertices. for (j = 0; j < 4; j++) { v = vec3(verts[i * 4 + j].pos.x, verts[i * 4 + j].pos.y, verts[i * 4 + j].pos.z); v = mat4_mul_vec3(mat, v); c = verts[i * 4 + j].color.rgb; line = (line_t){"v ", .v = v, .c = c}; face.vs[j] = lines_add(lines, &line); } // Put the normals for (j = 0; j < 4; j++) { v = vec3(verts[i * 4 + j].normal.x, verts[i * 4 + j].normal.y, verts[i * 4 + j].normal.z); line = (line_t){"vn", .vn = v}; face.vns[j] = lines_add(lines, &line); } lines_add(lines, &face); } } out = fopen(path, "w"); fprintf(out, "ply\n"); fprintf(out, "format ascii 1.0\n"); fprintf(out, "comment Generated from Goxel " GOXEL_VERSION_STR "\n"); fprintf(out, "element vertex %d\n", lines_count(lines, "v ")); fprintf(out, "property float x\n"); fprintf(out, "property float y\n"); fprintf(out, "property float z\n"); fprintf(out, "property uchar red\n"); fprintf(out, "property uchar green\n"); fprintf(out, "property uchar blue\n"); fprintf(out, "element face %d\n", lines_count(lines, "f ")); fprintf(out, "property list uchar int vertex_index\n"); fprintf(out, "end_header\n"); line_ptr = NULL; while( (line_ptr = (line_t*)utarray_next(lines, line_ptr))) { if (strncmp(line_ptr->type, "v ", 2) == 0) fprintf(out, "%g %g %g %d %d %d\n", VEC3_SPLIT(line_ptr->v), VEC3_SPLIT(line_ptr->c)); } while( (line_ptr = (line_t*)utarray_next(lines, line_ptr))) { if (strncmp(line_ptr->type, "f ", 2) == 0) fprintf(out, "4 %d %d %d %d\n", line_ptr->vs[0] - 1, line_ptr->vs[1] - 1, line_ptr->vs[2] - 1, line_ptr->vs[3] - 1); } fclose(out); utarray_free(lines); } static void export_as_obj(goxel_t *goxel, const char *path) { wavefront_export(goxel->layers_mesh, path); }