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 <alecjacobson@gmail.com>
  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::PlainObjectBase<DerivedV> & V,
  20. const Eigen::PlainObjectBase<DerivedT> & T,
  21. const Eigen::PlainObjectBase<DerivedF> & F,
  22. const bool wait)
  23. {
  24. using namespace std;
  25. // Build medit command, end with & so command returns without waiting
  26. stringstream command;
  27. command<<MEDIT_PATH<<" "<<TEMP_MESH_FILE<<" "<<TEMP_MEDIT_FILE;
  28. if(!wait)
  29. {
  30. command<<" &";
  31. }
  32. bool mesh_saved = writeMESH(TEMP_MESH_FILE,V,T,F);
  33. if(!mesh_saved)
  34. {
  35. return -1;
  36. }
  37. // Write default medit options
  38. const string default_medit_file_contents =
  39. "BackgroundColor 1 1 1\n"
  40. "LineColor 0 0 0\n"
  41. "WindowSize 1024 800\n"
  42. "RenderMode shading + lines\n";
  43. FILE * fp = fopen(TEMP_MEDIT_FILE,"w");
  44. if(fp == NULL)
  45. {
  46. cerr<<"^"<<__FUNCTION__<<": Could not write to "<<TEMP_MEDIT_FILE<<endl;
  47. return -1;
  48. }
  49. fprintf(fp,"%s",default_medit_file_contents.c_str());
  50. fclose(fp);
  51. try
  52. {
  53. return system(command.str().c_str());
  54. }catch(int e)
  55. {
  56. cerr<<"^"<<__FUNCTION__<<": Calling to medit crashed..."<<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 specialization
  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::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, bool);
  64. #endif