⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 txt2las.cpp

📁 Lidar数据处理时
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			  // mark that we found the first point
			  number_of_point_records = 1;
			  // create return histogram
			  number_of_points_by_return[point.return_number]++;
			  // we can stop this loop
			  break;
		  }
		  else
		  {
			  fprintf(stderr, "WARNING: cannot parse '%s' with '%s'. skipping ...\n", line, parse_less);
		  }
	  }

	  // did we manage to parse a line

	  if (number_of_point_records != 1)
	  {
		  fprintf(stderr, "ERROR: could not parse any lines with '%s'\n", parse_less);
		  exit(1);
	  }

	  // loop over the remaining lines

	  while (fgets(line, sizeof(char) * MAX_CHARACTERS_PER_LINE, file_in))
	  {
		  if (parse(parse_less, line, xyz, &point, &gps_time))
		  {
			  // update bounding box
			  VecUpdateMinMax3dv(xyz_min, xyz_max, xyz);
			  // count points
			  number_of_point_records++;
			  // create return histogram
			  number_of_points_by_return[point.return_number]++;
		  }
		  else
		  {
			  fprintf(stderr, "WARNING: cannot parse '%s' with '%s'. skipping ...\n", line, parse_less);
		  }
	  }

	  // output some stats
	  
	  if (verbose)
	  {
		  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]);
		  fprintf(stderr, "return histogram %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]);
	  }

	  // close the input file
	  
	  fclose(file_in);

	  // compute bounding box after quantization

	  int xyz_min_quant[3];
	  int xyz_max_quant[3];

	  for (i = 0; i < 3; i++)
	  {
		  xyz_min_quant[i] = (int)(0.5 + (xyz_min[i] - xyz_offset[i]) / xyz_scale[i]);
		  xyz_max_quant[i] = (int)(0.5 + (xyz_max[i] - xyz_offset[i]) / xyz_scale[i]);
	  }

	  double xyz_min_dequant[3];
	  double xyz_max_dequant[3];

	  for (i = 0; i < 3; i++)
	  {
		  xyz_min_dequant[i] = xyz_offset[i] + (xyz_min_quant[i] * xyz_scale[i]);
		  xyz_max_dequant[i] = xyz_offset[i] + (xyz_max_quant[i] * xyz_scale[i]);
	  }

	  // make sure there is not sign flip

#define log_xor !=0==!

	  for (i = 0; i < 3; i++)
	  {
		  if ((xyz_min[i] > 0) log_xor (xyz_min_dequant[i] > 0))
		  {
			  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]);
		  }
		  if ((xyz_max[i] > 0) log_xor (xyz_max_dequant[i] > 0))
		  {
			  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]);
		  }
	  }

#undef log_xor

	  // populate the header

	  LASheader header;

	  if (system_identifier) strncpy(header.system_identifier, system_identifier, 32);
	  if (generating_software) strncpy(header.generating_software, generating_software, 32);
	  header.file_creation_day = file_creation_day;
	  header.file_creation_year = file_creation_year;
	  if (strstr(parse_string,"t"))
	  {
		  header.point_data_format = 1;
		  header.point_data_record_length = 28;
	  }
	  else
	  {
		  header.point_data_format = 0;
		  header.point_data_record_length = 20;
	  }
	  header.number_of_point_records = number_of_point_records;
	  header.x_scale_factor = xyz_scale[0];
	  header.y_scale_factor = xyz_scale[1];
	  header.z_scale_factor = xyz_scale[2];
	  header.x_offset = xyz_offset[0];
	  header.y_offset = xyz_offset[1];
	  header.z_offset = xyz_offset[2];
	  header.min_x = xyz_min_dequant[0];
	  header.min_y = xyz_min_dequant[1];
	  header.min_z = xyz_min_dequant[2];
	  header.max_x = xyz_max_dequant[0];
	  header.max_y = xyz_max_dequant[1];
	  header.max_z = xyz_max_dequant[2];
	  header.number_of_points_by_return[0] = number_of_points_by_return[1];
	  header.number_of_points_by_return[1] = number_of_points_by_return[2];
	  header.number_of_points_by_return[2] = number_of_points_by_return[3];
	  header.number_of_points_by_return[3] = number_of_points_by_return[4];
	  header.number_of_points_by_return[4] = number_of_points_by_return[5];

	  // reopen input file for the second pass

	  if (strstr(file_name_in, ".gz"))
	  {
#ifdef _WIN32
		  file_in = fopenGzipped(file_name_in, "r");
#else
		  fprintf(stderr, "ERROR: no support for gzipped input\n");
		  exit(1);
#endif
	  }
	  else
	  {
		  file_in = fopen(file_name_in, "r");
	  }

	  if (file_in == 0)
	  {
		  fprintf(stderr, "ERROR: could not open '%s' for second pass\n",file_name_in);
		  exit(1);
	  }

	  // open the output pipe

    LASwriter* laswriter = new LASwriter();

    if (laswriter->open(stdout, &header, olaz ? 1 : 0) == false)
	  {
		  fprintf(stderr, "ERROR: could not open laswriter\n");
		  exit(1);
	  }

    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");

	  // loop over points

	  while (fgets(line, sizeof(char) * MAX_CHARACTERS_PER_LINE, file_in))
	  {
		  if (parse(parse_string, line, xyz, &point, &gps_time))
		  {
			  point.x = (int)(0.5 + (xyz[0] - xyz_offset[0]) / xyz_scale[0]);
			  point.y = (int)(0.5 + (xyz[1] - xyz_offset[1]) / xyz_scale[1]);
			  point.z = (int)(0.5 + (xyz[2] - xyz_offset[2]) / xyz_scale[2]);
			  laswriter->write_point(&point, gps_time);
			  number_of_point_records--;
		  }
		  else
		  {
			  fprintf(stderr, "WARNING: cannot parse '%s' with '%s'. skipping ...\n", line, parse_string);
		  }
	  }

	  if (number_of_point_records)
	  {
		  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);
	  }

	  laswriter->close();

	  if (verbose)
	  {
		  fprintf(stderr, "done.\n");
	  }

	  fclose(file_in);
  }
  else
  {
    // because the output goes to a file we can do everything in a single pass
    // and compute the header information along the way and then set it at the
    // end by fopen() the file with "rb+"

    // open input file

	  FILE* file_in;

    if (file_name_in)
	  {
		  if (strstr(file_name_in, ".gz"))
		  {
#ifdef _WIN32
			  file_in = fopenGzipped(file_name_in, "r");
#else
			  fprintf(stderr, "ERROR: no support for gzipped input\n");
			  exit(1);
#endif
		  }
		  else
		  {
			  file_in = fopen(file_name_in, "r");
		  }
		  if (file_in == 0)
		  {
			  fprintf(stderr, "ERROR: could not open input file '%s'\n",file_name_in);
			  exit(1);
		  }
	  }
	  else
	  {
      if (!itxt)
      {
  		  fprintf(stderr, "WARNING: no input specified. reading from stdin.\n");
      }
		  file_in = stdin;
	  }

	  // open output file

	  FILE* file_out = fopen(file_name_out, "wb");
    
    if (file_out == 0)
    {
			fprintf(stderr, "ERROR: could not open output file '%s'\n",file_name_out);
			exit(1);
	  }

    // populate header as much as possible (missing: bounding box, number of points, number of returns)

    LASheader header;

	  if (system_identifier) strncpy(header.system_identifier, system_identifier, 32);
	  if (generating_software) strncpy(header.generating_software, generating_software, 32);
	  header.file_creation_day = file_creation_day;
	  header.file_creation_year = file_creation_year;
	  if (strstr(parse_string,"t"))
	  {
		  header.point_data_format = 1;
		  header.point_data_record_length = 28;
	  }
	  else
	  {
		  header.point_data_format = 0;
		  header.point_data_record_length = 20;
	  }
	  header.x_scale_factor = xyz_scale[0];
	  header.y_scale_factor = xyz_scale[1];
	  header.z_scale_factor = xyz_scale[2];
	  header.x_offset = xyz_offset[0];
	  header.y_offset = xyz_offset[1];
	  header.z_offset = xyz_offset[2];

    // did the user request compressed output

	  int compression = 0;
		if (strstr(file_name_out, ".laz") || strstr(file_name_out, ".las.lz") || olaz)
		{
			compression = 1;
		}

    // create the las writer

    LASwriter* laswriter = new LASwriter();

	  if (laswriter->open(file_out, &header, compression) == false)
	  {
		  fprintf(stderr, "ERROR: could not open laswriter\n");
		  exit(1);
	  }

    fprintf(stderr, "scanning %s with parse '%s' writing to %s\n", file_name_in ? file_name_in : "stdin" , parse_string, file_name_out);

	  // read the first line

	  while (fgets(line, sizeof(char) * MAX_CHARACTERS_PER_LINE, file_in))
	  {
		  if (parse(parse_string, line, xyz, &point, &gps_time))
		  {
			  // init the bounding box
			  VecCopy3dv(xyz_min, xyz);
			  VecCopy3dv(xyz_max, xyz);
			  // we found the first point
			  number_of_point_records = 1;
			  // create return histogram
			  number_of_points_by_return[point.return_number]++;
        // compute the quantized x, y, and z values
        point.x = (int)(0.5 + (xyz[0] - xyz_offset[0]) / xyz_scale[0]);
			  point.y = (int)(0.5 + (xyz[1] - xyz_offset[1]) / xyz_scale[1]);
			  point.z = (int)(0.5 + (xyz[2] - xyz_offset[2]) / xyz_scale[2]);
        // write the first point
			  laswriter->write_point(&point, gps_time);
			  // we can stop this loop
			  break;
		  }
		  else
		  {
			  fprintf(stderr, "WARNING: cannot parse '%s' with '%s'. skipping ...\n", line, parse_string);
		  }
	  }

	  // did we manage to parse a line

	  if (number_of_point_records != 1)
	  {
		  fprintf(stderr, "ERROR: could not parse any lines with '%s'\n", parse_string);
		  exit(1);
	  }

	  // loop over the remaining lines

	  while (fgets(line, sizeof(char) * MAX_CHARACTERS_PER_LINE, file_in))
	  {
		  if (parse(parse_string, line, xyz, &point, &gps_time))
		  {
			  // update bounding box
			  VecUpdateMinMax3dv(xyz_min, xyz_max, xyz);
			  // count points
			  number_of_point_records++;
			  // create return histogram
			  number_of_points_by_return[point.return_number]++;
        // compute the quantized x, y, and z values
        point.x = (int)(0.5 + (xyz[0] - xyz_offset[0]) / xyz_scale[0]);
			  point.y = (int)(0.5 + (xyz[1] - xyz_offset[1]) / xyz_scale[1]);
			  point.z = (int)(0.5 + (xyz[2] - xyz_offset[2]) / xyz_scale[2]);
        // write the first point
			  laswriter->write_point(&point, gps_time);
		  }
		  else
		  {
			  fprintf(stderr, "WARNING: cannot parse '%s' with '%s'. skipping ...\n", line, parse_string);
		  }
	  }

    // done writing the points

	  if (file_in != stdin) fclose(file_in);
    laswriter->close();
    fclose(file_out);

    // output some stats
	  
	  if (verbose)
	  {
		  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]);
		  fprintf(stderr, "return histogram %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]);
	  }

	  // compute bounding box after quantization

	  int xyz_min_quant[3];
	  int xyz_max_quant[3];

	  for (i = 0; i < 3; i++)
	  {
		  xyz_min_quant[i] = (int)(0.5 + (xyz_min[i] - xyz_offset[i]) / xyz_scale[i]);
		  xyz_max_quant[i] = (int)(0.5 + (xyz_max[i] - xyz_offset[i]) / xyz_scale[i]);
	  }

	  double xyz_min_dequant[3];
	  double xyz_max_dequant[3];

	  for (i = 0; i < 3; i++)
	  {
		  xyz_min_dequant[i] = xyz_offset[i] + (xyz_min_quant[i] * xyz_scale[i]);
		  xyz_max_dequant[i] = xyz_offset[i] + (xyz_max_quant[i] * xyz_scale[i]);
	  }

	  // make sure there is not sign flip

#define log_xor !=0==!

	  for (i = 0; i < 3; i++)
	  {
		  if ((xyz_min[i] > 0) log_xor (xyz_min_dequant[i] > 0))
		  {
			  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]);
		  }
		  if ((xyz_max[i] > 0) log_xor (xyz_max_dequant[i] > 0))
		  {
			  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]);
		  }
	  }

#undef log_xor

	  // re-open output file to rewrite the missing header information

	  file_out = fopen(file_name_out, "rb+");
    
    if (file_out == 0)
    {
			fprintf(stderr, "ERROR: could not open re-output file '%s'\n",file_name_out);
			exit(1);
	  }

    // rewrite the information

    fseek(file_out, 107, SEEK_SET);
    fwrite(&number_of_point_records, sizeof(unsigned int), 5, file_out);

    fseek(file_out, 111, SEEK_SET);
    fwrite(&(number_of_points_by_return[1]), sizeof(unsigned int), 5, file_out);

    fseek(file_out, 179, SEEK_SET);
    fwrite(&(xyz_max_dequant[0]), sizeof(double), 1, file_out);
    fwrite(&(xyz_min_dequant[0]), sizeof(double), 1, file_out);
    fwrite(&(xyz_max_dequant[1]), sizeof(double), 1, file_out);
    fwrite(&(xyz_min_dequant[1]), sizeof(double), 1, file_out);
    fwrite(&(xyz_max_dequant[2]), sizeof(double), 1, file_out);
    fwrite(&(xyz_min_dequant[2]), sizeof(double), 1, file_out);

    // close the rewritten file

    fclose(file_out);

    if (verbose)
	  {
		  fprintf(stderr, "done.\n");
	  }
  }

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -