launch_medit.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "launch_medit.h"
  9. #include "writeMESH.h"
  10. #include <cstdio>
  11. #include <iostream>
  12. #include <string>
  13. #include <sstream>
  14. #define MEDIT_PATH "/opt/local/bin/medit"
  15. #define TEMP_MESH_FILE "/var/tmp/temp.mesh"
  16. #define TEMP_MEDIT_FILE "/var/tmp/temp.medit"
  17. template <typename DerivedV, typename DerivedT, typename DerivedF>
  18. IGL_INLINE int igl::launch_medit(
  19. const Eigen::MatrixBase<DerivedV> & V,
  20. const Eigen::MatrixBase<DerivedT> & T,
  21. const Eigen::MatrixBase<DerivedF> & F,
  22. const bool wait)
  23. {
  24. // Build medit command, end with & so command returns without waiting
  25. std::stringstream command;
  26. command<<MEDIT_PATH<<" "<<TEMP_MESH_FILE<<" "<<TEMP_MEDIT_FILE;
  27. if(!wait)
  28. {
  29. command<<" &";
  30. }
  31. bool mesh_saved = writeMESH(TEMP_MESH_FILE,V,T,F);
  32. if(!mesh_saved)
  33. {
  34. return -1;
  35. }
  36. // Write default medit options
  37. const std::string default_medit_file_contents =
  38. "BackgroundColor 1 1 1\n"
  39. "LineColor 0 0 0\n"
  40. "WindowSize 1024 800\n"
  41. "RenderMode shading + lines\n";
  42. FILE * fp = fopen(TEMP_MEDIT_FILE,"w");
  43. if(fp == NULL)
  44. {
  45. std::cerr<<"^"<<__FUNCTION__<<": Could not write to "<<TEMP_MEDIT_FILE<<std::endl;
  46. return -1;
  47. }
  48. fprintf(fp,"%s",default_medit_file_contents.c_str());
  49. fclose(fp);
  50. try
  51. {
  52. return system(command.str().c_str());
  53. }catch(int e)
  54. {
  55. (void)e;
  56. std::cerr<<"^"<<__FUNCTION__<<": Calling to medit crashed..."<<std::endl;
  57. return -1;
  58. }
  59. // Could clean up and delete these files but not really worth it
  60. }
  61. #ifdef IGL_STATIC_LIBRARY
  62. // Explicit template instantiation
  63. template int igl::launch_medit<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, bool);
  64. #endif