Changeset 530

Show
Ignore:
Timestamp:
03/15/08 18:35:17 (16 months ago)
Author:
hobu
Message:

formatting

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/apps/txt2las.c

    r508 r530  
    307307    int i; 
    308308    int dry = FALSE; 
    309     int use_stdin = FALSE; 
    310309    int verbose = FALSE; 
    311310    char* file_name_in = 0; 
     
    449448            file_creation_year = (unsigned short)atoi(argv[i]); 
    450449        } 
    451         else if (   strcmp(argv[i], "--stdin") == 0 || 
    452                     strcmp(argv[i], "-ilas") == 0 
    453                 )  
    454         { 
    455             use_stdin = TRUE; 
    456         } 
    457         else if (i == argc - 2 && file_name_in == NULL && file_name_out == NULL) 
     450        else if (   i == argc - 2  
     451                    && file_name_in == NULL &&  
     452                    file_name_out == NULL 
     453                ) 
    458454        { 
    459455            file_name_in = argv[i]; 
    460456        } 
    461         else if (i == argc - 1 && file_name_in == NULL && file_name_out == NULL) 
     457        else if (   i == argc - 1 &&  
     458                    file_name_in == NULL &&  
     459                    file_name_out == NULL 
     460                ) 
    462461        { 
    463462            file_name_in = argv[i]; 
    464463        } 
    465         else if (i == argc - 1 && file_name_in && file_name_out == NULL) 
     464        else if (   i == argc - 1 &&  
     465                    file_name_in &&  
     466                    file_name_out == NULL 
     467                ) 
    466468        { 
    467469            file_name_out = argv[i]; 
     
    474476        int len = strlen(file_name_in); 
    475477        file_name_out = strdup(file_name_in); 
    476         if (file_name_out[len-3] == '.' ||  
    477             file_name_out[len-2] == 'g' ||  
    478             file_name_out[len-1] == 'z' 
    479            ) 
    480         { 
    481             len = len - 4; 
    482         } 
     478 
    483479        while (len > 0 && file_name_out[len] != '.') 
    484480        { 
     
    497493    { 
    498494        LASError_Print("both input and output filenames are null!"); 
     495        usage(); 
    499496        exit(1); 
    500497    } 
    501  
    502498 
    503499    file_in = fopen(file_name_in, "r"); 
     
    509505    } 
    510506 
    511 /*    // create a cheaper parse string that only looks for 'x' 'y' 'z' and 'r' 
    512 */ 
    513       parse_less = strdup(parse_string); 
    514       for (i = 0; i < (int)strlen(parse_string); i++) 
    515       { 
    516           if (parse_less[i] != 'x' && parse_less[i] != 'y' && parse_less[i] != 'z' && parse_less[i] != 'r')  
    517           { 
    518               parse_less[i] = 's'; 
    519           } 
    520       } 
    521       do 
    522       { 
    523           parse_less[i] = '\0'; 
    524           i--; 
    525       } while (parse_less[i] == 's'); 
    526 /* 
    527       // first pass to figure out the bounding box and number of returns 
    528 */ 
    529  
    530     fprintf(stderr, "first pass over file '%s' with parse '%s'\n", file_name_in, parse_less); 
    531  
    532 /*    // read the first line 
    533 */ 
    534       while (fgets(line, sizeof(char) * MAX_CHARACTERS_PER_LINE, file_in)) 
    535       { 
    536           if (parse(parse_less, line, xyz, &point, &gps_time)) 
    537           { 
    538 /*            // init the bounding box 
    539 */ 
    540               VecCopy3dv(xyz_min, xyz); 
    541               VecCopy3dv(xyz_max, xyz); 
    542  
    543 /*            // mark that we found the first point 
    544 */ 
    545               number_of_point_records = 1; 
    546  
    547 /*            // create return histogram 
    548 */ 
    549               number_of_points_by_return[LASPoint_GetReturnNumber(point)]++; 
    550 /*            // we can stop this loop 
    551 */ 
    552               break; 
    553           } 
    554           else 
    555           { 
    556               fprintf(stderr, "WARNING: cannot parse '%s' with '%s'. skipping ...\n", line, parse_less); 
    557           } 
    558       } 
    559  
    560 /*    // did we manage to parse a line 
    561 */ 
    562       if (number_of_point_records != 1) 
    563       { 
    564           fprintf(stderr, "ERROR: could not parse any lines with '%s'\n", parse_less); 
    565           exit(1); 
    566       } 
    567  
    568 /*    // loop over the remaining lines 
    569 */ 
    570       while (fgets(line, sizeof(char) * MAX_CHARACTERS_PER_LINE, file_in)) 
    571       { 
    572           if (parse(parse_less, line, xyz, &point, &gps_time)) 
    573           { 
    574 /*            // update bounding box 
    575 */ 
    576               VecUpdateMinMax3dv(xyz_min, xyz_max, xyz); 
    577  
    578 /*            // count points 
    579 */ 
    580               number_of_point_records++; 
    581  
    582 /*            // create return histogram 
    583 */ 
    584               number_of_points_by_return[LASPoint_GetReturnNumber(point)]++; 
    585           } 
    586           else 
    587           { 
    588               fprintf(stderr, "WARNING: cannot parse '%s' with '%s'. skipping ...\n", line, parse_less); 
    589           } 
    590       } 
    591  
    592 /*    // output some stats 
    593 */     
    594       if (verbose) 
    595       { 
    596           fprintf(stderr, "npoints %d min %g %g %g max %g %g %g\n", number_of_point_records, xyz_min[0], xyz_min[1], xyz_min[2], xyz_max[0], xyz_max[1], xyz_max[2]); 
    597           fprintf(stderr, "return histogram %d %d %d %d %d %d %d %d\n", number_of_points_by_return[0], number_of_points_by_return[1], number_of_points_by_return[2], number_of_points_by_return[3], number_of_points_by_return[4], number_of_points_by_return[5], number_of_points_by_return[6], number_of_points_by_return[7]); 
    598       } 
    599  
    600 /*    // close the input file 
    601 */     
     507    /* create a cheaper parse string that only looks for 'x' 'y' 'z' and 'r' */ 
     508    parse_less = strdup(parse_string); 
     509    for (i = 0; i < (int)strlen(parse_string); i++) 
     510    { 
     511        if (parse_less[i] != 'x' &&  
     512            parse_less[i] != 'y' &&  
     513            parse_less[i] != 'z' &&  
     514            parse_less[i] != 'r')  
     515        { 
     516            parse_less[i] = 's'; 
     517        } 
     518    } 
     519 
     520    do 
     521    { 
     522        parse_less[i] = '\0'; 
     523        printf("nuking %d for %c\n", i, parse_less[i]); 
     524        i--; 
     525    } while (parse_less[i] == 's'); 
     526     
     527     
     528    /* first pass to figure out the bounding box and number of returns */ 
     529    if (verbose) { 
     530        fprintf(stderr,  
     531                "first pass over file '%s' with parse '%s'\n",  
     532                file_name_in,  
     533                parse_less); 
     534    } 
     535 
     536    /* read the first line */ 
     537    while (fgets(line, sizeof(char) * MAX_CHARACTERS_PER_LINE, file_in)) 
     538    { 
     539        if (parse(parse_less, line, xyz, &point, &gps_time)) 
     540        { 
     541            /* init the bounding box */ 
     542            VecCopy3dv(xyz_min, xyz); 
     543            VecCopy3dv(xyz_max, xyz); 
     544 
     545            /* mark that we found the first point */ 
     546            number_of_point_records = 1; 
     547 
     548            /* create return histogram */ 
     549            number_of_points_by_return[LASPoint_GetReturnNumber(point)]++; 
     550             
     551            /* we can stop this loop */ 
     552            break; 
     553        } 
     554        else 
     555        { 
     556            fprintf(stderr, "WARNING: cannot parse '%s' with '%s'. skipping ...\n",  
     557                    line,  
     558                    parse_less); 
     559        } 
     560    } 
     561 
     562    /* did we manage to parse a line? */ 
     563    if (number_of_point_records != 1) 
     564    { 
     565        fprintf(stderr, "ERROR: could not parse any lines with '%s'\n",  
     566                parse_less); 
     567        exit(1); 
     568    } 
     569 
     570    /* loop over the remaining lines */ 
     571    while (fgets(line, sizeof(char) * MAX_CHARACTERS_PER_LINE, file_in)) 
     572    { 
     573        if (parse(parse_less, line, xyz, &point, &gps_time)) 
     574        { 
     575            /* update bounding box */ 
     576            VecUpdateMinMax3dv(xyz_min, xyz_max, xyz); 
     577 
     578            /* count points */ 
     579            number_of_point_records++; 
     580 
     581            /* create return histogram */ 
     582            number_of_points_by_return[LASPoint_GetReturnNumber(point)]++; 
     583        } 
     584        else 
     585        { 
     586            fprintf(stderr, "WARNING: cannot parse '%s' with '%s'. skipping ...\n",  
     587            line,  
     588            parse_less); 
     589        } 
     590    } 
     591 
     592    /* output some stats */ 
     593    if (verbose) 
     594    { 
     595        fprintf(stderr,  
     596                "npoints %d min %g %g %g max %g %g %g\n",  
     597                number_of_point_records,  
     598                xyz_min[0],  
     599                xyz_min[1],  
     600                xyz_min[2],  
     601                xyz_max[0],  
     602                xyz_max[1],  
     603                xyz_max[2] 
     604                ); 
     605        fprintf(stderr,  
     606                "return histogram %d %d %d %d %d %d %d %d\n",  
     607                number_of_points_by_return[0],  
     608                number_of_points_by_return[1],  
     609                number_of_points_by_return[2],  
     610                number_of_points_by_return[3],  
     611                number_of_points_by_return[4],  
     612                number_of_points_by_return[5],  
     613                number_of_points_by_return[6],  
     614                number_of_points_by_return[7] 
     615                ); 
     616    } 
     617 
     618    /* close the input file */     
    602619      fclose(file_in); 
    603620 
    604 /*    // compute bounding box after quantization 
    605 */ 
    606  
    607  
    608       for (i = 0; i < 3; i++) 
    609       { 
    610           xyz_min_quant[i] = (int)(0.5 + (xyz_min[i] - xyz_offset[i]) / xyz_scale[i]); 
    611           xyz_max_quant[i] = (int)(0.5 + (xyz_max[i] - xyz_offset[i]) / xyz_scale[i]); 
    612       } 
    613  
    614  
    615  
    616       for (i = 0; i < 3; i++) 
    617       { 
    618           xyz_min_dequant[i] = xyz_offset[i] + (xyz_min_quant[i] * xyz_scale[i]); 
    619           xyz_max_dequant[i] = xyz_offset[i] + (xyz_max_quant[i] * xyz_scale[i]); 
    620       } 
    621  
    622 /*    // make sure there is not sign flip 
    623 */ 
     621    /*  compute bounding box after quantization */ 
     622    for (i = 0; i < 3; i++) 
     623    { 
     624        xyz_min_quant[i] = (int)(0.5 + (xyz_min[i] - xyz_offset[i]) / xyz_scale[i]); 
     625        xyz_max_quant[i] = (int)(0.5 + (xyz_max[i] - xyz_offset[i]) / xyz_scale[i]); 
     626    } 
     627 
     628    for (i = 0; i < 3; i++) 
     629    { 
     630        xyz_min_dequant[i] = xyz_offset[i] + (xyz_min_quant[i] * xyz_scale[i]); 
     631        xyz_max_dequant[i] = xyz_offset[i] + (xyz_max_quant[i] * xyz_scale[i]); 
     632    } 
     633 
    624634 
    625635#define log_xor !=0==! 
    626636 
    627       for (i = 0; i < 3; i++) 
    628       { 
    629           if ((xyz_min[i] > 0) log_xor (xyz_min_dequant[i] > 0)) 
    630           { 
    631               fprintf(stderr, "WARNING: quantization sign flip for %s min coord %g -> %g. use offset or scale up\n", (i ? (i == 1 ? "y" : "z") : "x"), xyz_min[i], xyz_min_dequant[i]); 
    632           } 
    633           if ((xyz_max[i] > 0) log_xor (xyz_max_dequant[i] > 0)) 
    634           { 
    635               fprintf(stderr, "WARNING: quantization sign flip for %s max coord %g -> %g. use offset or scale up\n", (i ? (i == 1 ? "y" : "z") : "x"), xyz_max[i], xyz_max_dequant[i]); 
    636           } 
    637       } 
     637    /* make sure there is not sign flip */ 
     638    for (i = 0; i < 3; i++) 
     639    { 
     640        if ((xyz_min[i] > 0) log_xor (xyz_min_dequant[i] > 0)) 
     641        { 
     642            fprintf(stderr,  
     643                    "WARNING: quantization sign flip for %s min coord %g -> %g. use offset or scale up\n",  
     644                    (i ? (i == 1 ? "y" : "z") : "x"),  
     645                    xyz_min[i],  
     646                    xyz_min_dequant[i] 
     647                   ); 
     648        } 
     649        if ((xyz_max[i] > 0) log_xor (xyz_max_dequant[i] > 0)) 
     650        { 
     651            fprintf(stderr,  
     652                    "WARNING: quantization sign flip for %s max coord %g -> %g. use offset or scale up\n",  
     653                    (i ? (i == 1 ? "y" : "z") : "x"),  
     654                    xyz_max[i],  
     655                    xyz_max_dequant[i] 
     656                   ); 
     657        } 
     658    } 
    638659 
    639660#undef log_xor 
    640661 
    641 /*    // populate the header 
    642 */ 
     662    /* populate the header */ 
    643663       
    644       header = LASHeader_Create(); 
    645  
    646       if (system_identifier) LASHeader_SetSystemId(header, system_identifier); 
    647       if (generating_software) LASHeader_SetSoftwareId(header, generating_software); 
    648       LASHeader_SetCreationDOY(header, file_creation_day); 
    649       LASHeader_SetCreationYear(header, file_creation_year); 
     664    header = LASHeader_Create(); 
     665 
     666    if (system_identifier) LASHeader_SetSystemId(header, system_identifier); 
     667    if (generating_software) LASHeader_SetSoftwareId(header, generating_software); 
     668    LASHeader_SetCreationDOY(header, file_creation_day); 
     669    LASHeader_SetCreationYear(header, file_creation_year); 
    650670       
    651671 
    652       if (strstr(parse_string,"t")) 
    653       { 
    654           LASHeader_SetDataFormatId(header, 1); 
    655  
    656       } 
    657       else 
    658       { 
    659           LASHeader_SetDataFormatId(header, 0); 
    660       } 
    661       LASHeader_SetPointRecordsCount(header, number_of_point_records); 
    662       LASHeader_SetScale(header, xyz_scale[0], xyz_scale[1], xyz_scale[2]); 
    663       LASHeader_SetOffset(header, xyz_offset[0], xyz_offset[1], xyz_offset[2]); 
    664       LASHeader_SetMin(header, xyz_min_dequant[0], xyz_min_dequant[1], xyz_min_dequant[2]); 
    665       LASHeader_SetMax(header, xyz_max_dequant[0], xyz_max_dequant[1], xyz_max_dequant[2]); 
    666       LASHeader_SetPointRecordsByReturnCount(header, 0, number_of_points_by_return[1]); 
    667       LASHeader_SetPointRecordsByReturnCount(header, 1, number_of_points_by_return[2]); 
    668       LASHeader_SetPointRecordsByReturnCount(header, 2, number_of_points_by_return[3]); 
    669       LASHeader_SetPointRecordsByReturnCount(header, 3, number_of_points_by_return[4]); 
    670       LASHeader_SetPointRecordsByReturnCount(header, 4, number_of_points_by_return[5]); 
    671  
    672  
    673  
    674 /*    // reopen input file for the second pass 
    675 */ 
    676       file_in = fopen(file_name_in, "r"); 
    677  
    678       if (file_in == 0) 
    679       { 
    680           fprintf(stderr, "ERROR: could not open '%s' for second pass\n",file_name_in); 
    681           exit(1); 
    682       } 
    683  
    684 /*    // open the output pipe 
    685 */ 
    686  
     672    if (strstr(parse_string,"t")) 
     673    { 
     674        LASHeader_SetDataFormatId(header, 1); 
     675    } 
     676    else 
     677    { 
     678        LASHeader_SetDataFormatId(header, 0); 
     679    } 
     680    LASHeader_SetPointRecordsCount(header, number_of_point_records); 
     681    LASHeader_SetScale(header, xyz_scale[0], xyz_scale[1], xyz_scale[2]); 
     682    LASHeader_SetOffset(header, xyz_offset[0], xyz_offset[1], xyz_offset[2]); 
     683    LASHeader_SetMin(header, xyz_min_dequant[0], xyz_min_dequant[1], xyz_min_dequant[2]); 
     684    LASHeader_SetMax(header, xyz_max_dequant[0], xyz_max_dequant[1], xyz_max_dequant[2]); 
     685    LASHeader_SetPointRecordsByReturnCount(header, 0, number_of_points_by_return[1]); 
     686    LASHeader_SetPointRecordsByReturnCount(header, 1, number_of_points_by_return[2]); 
     687    LASHeader_SetPointRecordsByReturnCount(header, 2, number_of_points_by_return[3]); 
     688    LASHeader_SetPointRecordsByReturnCount(header, 3, number_of_points_by_return[4]); 
     689    LASHeader_SetPointRecordsByReturnCount(header, 4, number_of_points_by_return[5]); 
     690 
     691 
     692 
     693    /* reopen input file for the second pass */ 
     694    file_in = fopen(file_name_in, "r"); 
     695 
     696    if (file_in == 0) 
     697    { 
     698        fprintf(stderr, "ERROR: could not open '%s' for second pass\n",file_name_in); 
     699        exit(1); 
     700    } 
     701 
     702    /* 
     703        because the output goes to a file we can do everything in a  
     704        single pass and compute the header information along the way  
     705    */ 
    687706     
    688 /*       
    689     if (laswriter->open(stdout, &header, olaz ? 1 : 0) == false) 
    690       { 
    691           fprintf(stderr, "ERROR: could not open laswriter\n"); 
    692           exit(1); 
    693       } 
    694  
    695     fprintf(stderr, "second pass over file '%s' with parse '%s' writing to '%s'\n", file_name_in, parse_string, file_name_out ? file_name_out : "stdout"); 
    696  
    697       // loop over points 
    698  
    699       while (fgets(line, sizeof(char) * MAX_CHARACTERS_PER_LINE, file_in)) 
    700       { 
    701           if (parse(parse_string, line, xyz, &point, &gps_time)) 
    702           { 
    703               point.x = (int)(0.5 + (xyz[0] - xyz_offset[0]) / xyz_scale[0]); 
    704               point.y = (int)(0.5 + (xyz[1] - xyz_offset[1]) / xyz_scale[1]); 
    705               point.z = (int)(0.5 + (xyz[2] - xyz_offset[2]) / xyz_scale[2]); 
    706               laswriter->write_point(&point, gps_time); 
    707               number_of_point_records--; 
    708           } 
    709           else 
    710           { 
    711               fprintf(stderr, "WARNING: cannot parse '%s' with '%s'. skipping ...\n", line, parse_string); 
    712           } 
    713       } 
    714  
    715       if (number_of_point_records) 
    716       { 
    717           fprintf(stderr, "WARNING: second pass has different number of points (%d instead of %d)\n", header.number_of_point_records - number_of_point_records, header.number_of_point_records); 
    718       } 
    719  
    720       LASWriter_Destroy(writer); 
    721  
    722       if (verbose) 
    723       { 
    724           fprintf(stderr, "done.\n"); 
    725       } 
    726  
    727       fclose(file_in); 
    728   } 
    729   else 
    730   { 
    731 */ 
    732 /* 
    733     // because the output goes to a file we can do everything in a single pass 
    734     // and compute the header information along the way and then set it at the 
    735     // end by fopen() the file with "rb+" 
    736  
    737     // open input file 
    738 */ 
    739  
    740     if (file_name_in) 
    741       { 
    742  
    743           file_in = fopen(file_name_in, "r"); 
    744  
    745           if (file_in == NULL) 
    746           { 
    747               LASError_Print("Could not open input file"); 
    748               exit(1); 
    749           } 
    750       } 
    751  
    752 /*    // open output file 
    753 */ 
    754       printf("Creating file...\n"); 
    755   writer = LASWriter_Create(file_name_out, header, LAS_MODE_WRITE); 
    756   if (!writer) {  
    757       fprintf(stderr,  
    758               "Error! %d, %s, in method %s\n", 
    759               LASError_GetLastErrorNum(), 
    760               LASError_GetLastErrorMsg(), 
    761               LASError_GetLastErrorMethod() 
    762              );  
    763       exit(1); 
    764   }      
    765  
    766  
    767     fprintf(stderr, "scanning %s with parse '%s' writing to %s\n", file_name_in ? file_name_in : "stdin" , parse_string, file_name_out); 
    768  
    769 /*    // read the first line 
    770 */ 
    771       while (fgets(line, sizeof(char) * MAX_CHARACTERS_PER_LINE, file_in)) 
    772       { 
    773           if (parse(parse_string, line, xyz, &point, &gps_time)) 
    774           { 
    775 /*            // init the bounding box 
    776 */ 
    777               VecCopy3dv(xyz_min, xyz); 
    778               VecCopy3dv(xyz_max, xyz); 
    779  
    780 /*            // we found the first point 
    781 */ 
    782               number_of_point_records = 1; 
    783  
    784 /*            // create return histogram 
    785 */ 
    786               number_of_points_by_return[LASPoint_GetReturnNumber(point)]++; 
    787 /*        // compute the quantized x, y, and z values 
    788 */ 
    789               LASPoint_SetX(point, 0.5 + (xyz[0] - xyz_offset[0]) / xyz_scale[0]); 
    790               LASPoint_SetY(point, 0.5 + (xyz[1] - xyz_offset[1]) / xyz_scale[1]); 
    791               LASPoint_SetX(point, 0.5 + (xyz[2] - xyz_offset[2]) / xyz_scale[2]); 
    792  
    793 /*        // write the first point 
    794 */ 
    795               LASWriter_WritePoint(writer, point); 
    796  
    797 /*            // we can stop this loop 
    798 */ 
    799               break; 
    800           } 
    801           else 
    802           { 
    803               fprintf(stderr, "WARNING: cannot parse '%s' with '%s'. skipping ...\n", line, parse_string); 
    804           } 
    805       } 
    806  
    807 /*    // did we manage to parse a line 
    808 */ 
    809       if (number_of_point_records != 1) 
    810       { 
    811           fprintf(stderr, "ERROR: could not parse any lines with '%s'\n", parse_string); 
    812           exit(1); 
    813       } 
    814  
    815 /*    // loop over the remaining lines 
    816 */ 
    817       while (fgets(line, sizeof(char) * MAX_CHARACTERS_PER_LINE, file_in)) 
    818       { 
    819           if (parse(parse_string, line, xyz, &point, &gps_time)) 
    820           { 
    821  
    822 /*            // update bounding box 
    823 */ 
    824               VecUpdateMinMax3dv(xyz_min, xyz_max, xyz); 
    825  
    826 /*            // count points 
    827 */ 
    828               number_of_point_records++; 
    829  
    830 /*            // create return histogram 
    831 */ 
    832               number_of_points_by_return[LASPoint_GetReturnNumber(point)]++; 
    833 /*        // compute the quantized x, y, and z values 
    834 */ 
    835               LASPoint_SetX(point, 0.5 + (xyz[0] - xyz_offset[0]) / xyz_scale[0]); 
    836               LASPoint_SetY(point, 0.5 + (xyz[1] - xyz_offset[1]) / xyz_scale[1]); 
    837               LASPoint_SetX(point, 0.5 + (xyz[2] - xyz_offset[2]) / xyz_scale[2]); 
    838  
    839 /*        // write the first point 
    840   */ 
    841               LASWriter_WritePoint(writer, point); 
    842           } 
    843           else 
    844           { 
    845               fprintf(stderr, "WARNING: cannot parse '%s' with '%s'. skipping ...\n", line, parse_string); 
    846           } 
    847       } 
    848  
    849 /*    // done writing the points 
    850 */ 
    851  
    852 /*    if (file_in != stdin) fclose(file_in); 
    853 */ 
    854  
    855       LASWriter_Destroy(writer); 
    856  
    857 /*    // output some stats 
    858 */ 
    859        
    860       if (verbose) 
    861       { 
    862           fprintf(stderr, "npoints %d min %g %g %g max %g %g %g\n", number_of_point_records, xyz_min[0], xyz_min[1], xyz_min[2], xyz_max[0], xyz_max[1], xyz_max[2]); 
    863           fprintf(stderr, "return histogram %d %d %d %d %d %d %d %d\n", number_of_points_by_return[0], number_of_points_by_return[1], number_of_points_by_return[2], number_of_points_by_return[3], number_of_points_by_return[4], number_of_points_by_return[5], number_of_points_by_return[6], number_of_points_by_return[7]); 
    864       } 
    865  
    866 /*    // compute bounding box after quantization 
    867 */ 
    868  
    869  
    870       for (i = 0; i < 3; i++) 
    871       { 
    872           xyz_min_quant[i] = (int)(0.5 + (xyz_min[i] - xyz_offset[i]) / xyz_scale[i]); 
    873           xyz_max_quant[i] = (int)(0.5 + (xyz_max[i] - xyz_offset[i]) / xyz_scale[i]); 
    874       } 
    875  
    876  
    877  
    878       for (i = 0; i < 3; i++) 
    879       { 
    880           xyz_min_dequant[i] = xyz_offset[i] + (xyz_min_quant[i] * xyz_scale[i]); 
    881           xyz_max_dequant[i] = xyz_offset[i] + (xyz_max_quant[i] * xyz_scale[i]); 
    882       } 
    883  
    884 /*    // make sure there is not sign flip 
    885 */ 
    886  
    887 #define log_xor !=0==! 
    888  
    889       for (i = 0; i < 3; i++) 
    890       { 
    891           if ((xyz_min[i] > 0) log_xor (xyz_min_dequant[i] > 0)) 
    892           { 
    893               fprintf(stderr, "WARNING: quantization sign flip for %s min coord %g -> %g. use offset or scale up\n", (i ? (i == 1 ? "y" : "z") : "x"), xyz_min[i], xyz_min_dequant[i]); 
    894           } 
    895           if ((xyz_max[i] > 0) log_xor (xyz_max_dequant[i] > 0)) 
    896           { 
    897               fprintf(stderr, "WARNING: quantization sign flip for %s max coord %g -> %g. use offset or scale up\n", (i ? (i == 1 ? "y" : "z") : "x"), xyz_max[i], xyz_max_dequant[i]); 
    898           } 
    899       } 
    900  
    901 #undef log_xor 
    902  
    903 /*    // re-open output file to rewrite the missing header information 
    904  
    905  
    906       file_out = fopen(file_name_out, "rb+"); 
     707    /* open output file */ 
     708    printf("Creating file...\n"); 
     709    writer = LASWriter_Create(file_name_out, header, LAS_MODE_WRITE); 
     710    if (!writer) {  
     711        LASError_Print("Could not open file for write mode "); 
     712        exit(1); 
     713    }      
     714 
     715    if (verbose) { 
     716        fprintf(stderr,  
     717                "scanning %s with parse '%s' writing to %s\n",  
     718                file_name_in ,  
     719                parse_string,  
     720                file_name_out 
     721               ); 
     722    } 
    907723     
    908     if (file_out == 0) 
    909     { 
    910             fprintf(stderr, "ERROR: could not open re-output file '%s'\n",file_name_out); 
    911             exit(1); 
    912       } 
    913  
    914     // rewrite the information 
    915  
    916  
    917  
    918     fseek(file_out, 107, SEEK_SET); 
    919     fwrite(&number_of_point_records, sizeof(unsigned int), 5, file_out); 
    920  
    921     fseek(file_out, 111, SEEK_SET); 
    922     fwrite(&(number_of_points_by_return[1]), sizeof(unsigned int), 5, file_out); 
    923  
    924     fseek(file_out, 179, SEEK_SET); 
    925     fwrite(&(xyz_max_dequant[0]), sizeof(double), 1, file_out); 
    926     fwrite(&(xyz_min_dequant[0]), sizeof(double), 1, file_out); 
    927     fwrite(&(xyz_max_dequant[1]), sizeof(double), 1, file_out); 
    928     fwrite(&(xyz_min_dequant[1]), sizeof(double), 1, file_out); 
    929     fwrite(&(xyz_max_dequant[2]), sizeof(double), 1, file_out); 
    930     fwrite(&(xyz_min_dequant[2]), sizeof(double), 1, file_out); 
    931  
    932     // close the rewritten file 
    933  
    934     fclose(file_out); 
    935 */ 
    936  
     724 
     725    /* read the first line */ 
     726    while (fgets(line, sizeof(char) * MAX_CHARACTERS_PER_LINE, file_in)) 
     727    { 
     728        if (parse(parse_string, line, xyz, &point, &gps_time)) 
     729        { 
     730            /* init the bounding box */ 
     731            VecCopy3dv(xyz_min, xyz); 
     732            VecCopy3dv(xyz_max, xyz); 
     733 
     734            /* mark that we found the first point */ 
     735            number_of_point_records = 1; 
     736 
     737            /* create return histogram */ 
     738            number_of_points_by_return[LASPoint_GetReturnNumber(point)]++; 
     739             
     740            /* compute the quantized x, y, and z values */ 
     741            LASPoint_SetX(point, 0.5 + (xyz[0] - xyz_offset[0]) / xyz_scale[0]); 
     742            LASPoint_SetY(point, 0.5 + (xyz[1] - xyz_offset[1]) / xyz_scale[1]); 
     743            LASPoint_SetX(point, 0.5 + (xyz[2] - xyz_offset[2]) / xyz_scale[2]); 
     744 
     745            /* write the first point */ 
     746            LASWriter_WritePoint(writer, point); 
     747            printf("Writing point..."); 
     748               
     749            /* we can stop this loop */ 
     750            break; 
     751        } 
     752        else 
     753        { 
     754            fprintf(stderr, "WARNING: cannot parse '%s' with '%s'. skipping ...\n",  
     755                    line,  
     756                    parse_string); 
     757        } 
     758    } 
     759 
     760    /* did we manage to parse a line? */ 
     761    if (number_of_point_records != 1) 
     762    { 
     763        fprintf(stderr, "ERROR: could not parse any lines with '%s'\n",  
     764                parse_less); 
     765        exit(1); 
     766    } 
     767 
     768    /* loop over the remaining lines */ 
     769    while (fgets(line, sizeof(char) * MAX_CHARACTERS_PER_LINE, file_in)) 
     770    { 
     771        if (parse(parse_string, line, xyz, &point, &gps_time)) 
     772        { 
     773            /* update bounding box */ 
     774            VecUpdateMinMax3dv(xyz_min, xyz_max, xyz); 
     775 
     776            /* count points */ 
     777            number_of_point_records++; 
     778 
     779            /* create return histogram */ 
     780            number_of_points_by_return[LASPoint_GetReturnNumber(point)]++; 
     781 
     782            /* compute the quantized x, y, and z values */ 
     783            LASPoint_SetX(point, 0.5 + (xyz[0] - xyz_offset[0]) / xyz_scale[0]); 
     784            LASPoint_SetY(point, 0.5 + (xyz[1] - xyz_offset[1]) / xyz_scale[1]); 
     785            LASPoint_SetX(point, 0.5 + (xyz[2] - xyz_offset[2]) / xyz_scale[2]); 
     786 
     787            /* write the first point */ 
     788            LASWriter_WritePoint(writer, point); 
     789        } 
     790        else 
     791        { 
     792            fprintf(stderr, "WARNING: cannot parse '%s' with '%s'. skipping ...\n",  
     793            line,  
     794            parse_string); 
     795        } 
     796    } 
     797 
     798 
     799    /* close up stuff */ 
     800    fclose(file_in); 
     801    LASWriter_Destroy(writer); 
    937802    if (verbose) 
    938       { 
    939           fprintf(stderr, "done.\n"); 
    940       } 
     803    { 
     804        fprintf(stderr, "done.\n"); 
     805    } 
    941806 
    942807