black_hole/preprocess/preprocess_main.cc

This file provides a command line tool to generate the precomputed textures of our black hole model, and to save them into binary files. It simply calls the preprocessing functions and saves their result.

#include <fstream>
#include <iostream>
#include "black_hole/preprocess/functions.h"
#include "math/binary_function.h"

namespace black_hole {
namespace preprocess {
namespace {

using dimensional::BinaryFunction;

template <unsigned int NX, unsigned int NY, class T1, class T2>
void SaveTexture(const BinaryFunction<NX, NY, Tuple2<T1, T2>>& texture,
                 const std::string& file_name) {
  std::unique_ptr<float[]> pixels(new float[2 * NX * NY + 2]);
  pixels[0] = static_cast<float>(NX);
  pixels[1] = static_cast<float>(NY);
  for (unsigned int y = 0; y < NY; ++y) {
    for (unsigned int x = 0; x < NX; ++x) {
      const auto& texel = texture.Get(x, y);
      const int offset = 2 * (x + y * NX) + 2;
      pixels[offset] = static_cast<float>(texel.x.to(T1::Unit()));
      pixels[offset + 1] = static_cast<float>(texel.y.to(T2::Unit()));
    }
  }
  std::ofstream output_stream(file_name,
                              std::ofstream::out | std::ofstream::binary);
  output_stream.write((const char*)pixels.get(),
                      (2 * NX * NY + 2) * sizeof(float));
  output_stream.close();
}

void Main(const std::string& output_dir) {
  RayDeflectionTexture ray_deflection_texture;
  ComputeRayDeflectionTexture(&ray_deflection_texture);
  SaveTexture(ray_deflection_texture, output_dir + "deflection.dat");

  RayInverseRadiusTexture ray_inverse_radius_texture;
  ComputeRayInverseRadiusTexture(&ray_inverse_radius_texture);
  SaveTexture(ray_inverse_radius_texture, output_dir + "inverse_radius.dat");
}

}  // namespace
}  // namespace preprocess
}  // namespace black_hole

int main(int argc, char** argv) {
  if (argc < 2) {
    std::cerr << "Usage: preprocess <output folder>" << std::endl;
    return -1;
  }
  black_hole::preprocess::Main(argv[1]);
  return 0;
}