int main (int argc, char * argv[])
{
  std::string tsp_file;
  size_t width(800), height(600);
  int start(0), end(1), res(16);

  if (argc > 3)
  {
    tsp_file = argv[1];
    start = atoi(argv[2]);
    end = atoi(argv[3]);
  }
  else
  {
    printf("Usage: TsplineViewer tsp_file_sequence start_index end_index\n");
    printf("   tsp-file-sequence:  filename_%%d.tsp\n\n");
    return 0;
  }

  if(argc > 4)
    res = atoi(argv[4]);

  std::ostringstream os;
  os << "T-spline Sequence (" << tsp_file << ")";
  tgTomGineThread viewer (width, height, os.str());
  viewer.SetClearColor(1.0f);
  viewer.SetInputSpeeds(0.1);

  // schnipp
  TomGine::tgCamera cam = viewer.GetCamera();
//    dino_sparse_front
  float ext_data[16] = {  -0.382363, -0.097398, -0.918871, 0.000105,
                          -0.181148, 0.983042, -0.028819, -0.027788,
                          0.906093, 0.155430, -0.393519, -0.365332,
                          0.000000, 0.000000, 0.000000, 1.000000 };
//     dino
  float int_data[16] = { 10.344999, 0.000000, 0.010219, 0.000000,
                          0.000000, 13.856250, -0.164375, 0.000000,
                          0.000000, 0.000000, -1.000200, -0.020002,
                          0.000000, 0.000000, -1.000000, 0.000000};
  TomGine::mat4 E(ext_data);
  TomGine::mat4 I(int_data);
  cam.SetIntrinsic(I.transpose());
  cam.SetExtrinsic(E.transpose());
  viewer.SetCamera(cam);
  // schnapp

  SequenceViewer sv(viewer);
  sv.meshes.resize(end-start+1);

  for(int i=start; i<=end && !viewer.Stopped(); i++)
  {
    // Tspline
    char file[256];
    sprintf(file, tsp_file.c_str(), i);
    printf("[main] Loading T-spline %d: '%s'\n", i, file);
    Tspline tsp;
    File::Load(tsp, file);

    TomGine::tgTextureModel& mesh = sv.meshes[i];
    mesh.m_material.Color( 0.1,0.1,0.1,1.0,
                           0.6,1.0,0.6,1.0,
                           0.5,0.5,0.5,1.0, 50);
    mesh.SetColor(0.5f,0.5f,0.5f,0.5f);

    convertTspline2tgModel(tsp, mesh, res, res, true);
    convertTsplineControl2tgModel(tsp, mesh);
//    convertTsplineEdges2tgModel(tsp, mesh, 4, 1e-4);
    mesh.m_line_color = TomGine::vec3(0,0,0);
    mesh.m_line_width = 5.0f;
    mesh.m_point_color = TomGine::vec3(0,0,0);
    mesh.m_point_size = 15.0f;

    if(!tsp.texture.empty())
    {
      // texture from tspline
      printf("[main] tseigen normalizep.texture.size: %lu  tsp.texture: %s\n", tsp.texture.size(), tsp.texture.c_str());
      unsigned found = tsp_file.find_last_of("/\\");
      std::string texture = tsp_file.substr(0,found) + "/" + tsp.texture;
      mesh.m_tex_cv.push_back(cv::imread(texture));
      mesh.m_face_tex_id.assign(mesh.m_faces.size(), 0);
      mesh.SetColor(1.0f,1.0f,1.0f);
    }

    //    convertTsplineControl2tgModel(tsp, mesh);

    //    TomGine::vec3 cor(0,0,0);
    //    for(size_t i=0; i<mesh.m_lines.size(); i++)
    //    {
    //      vec3 a = mesh.m_lines[i].start;
    //      vec3 b = mesh.m_lines[i].end;
    //      viewer.AddLine3D(a.x,a.y,a.z, b.x,b.y,b.z, 0,0,128, 1.0);
    //    }

    //    size_t cpi(0);
    //    Tspline::Vertex_iterator vit;
    //    for(vit=tsp.vertices_begin(); vit!=tsp.vertices_end(); vit++)
    //    {
    //      Point3d cp = vit->data().GetCP();
    //      viewer.AddPoint3D(cp.x(),cp.y(),cp.z(), 0,0,255, 5.0f);
    //      std::ostringstream os;
    //      os << cpi;
    //      viewer.AddLabel3D(os.str(), 12, cp.x(),cp.y(),cp.z());
    //      cor+=vec3(cp.x(),cp.y(),cp.z());
    //      cpi++;
    //    }
    //    cor /= cpi;
    //    viewer.LookAt(cor);
    //    viewer.SetRotationCenter(cor);

//    mesh.m_lines.clear();
//    mesh.m_points.clear();

    //    convertTsplineEdges2tgModel(tsp, mesh, 4);
    //    mesh.m_line_color = TomGine::vec3(1,0,0);
    //    mesh.m_line_width = 4.0;

    if(i==start)
    {
      mesh.ComputeBoundingSphere();
      viewer.SetInputSpeeds(1.0f, mesh.m_bs.radius, mesh.m_bs.radius);
      viewer.SetRotationCenter(mesh.m_bs.center);
    }
  }

  printf("[main] Loading T-splines done\n");



  //  viewer.AddModel3D(meshes);


  viewer.Update();
  viewer.WaitForEvent (TMGL_Press, TMGL_Escape);
  return (0);
}