compile.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2021 DeepMind Technologies Limited
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <cctype>
  15. #include <cstddef>
  16. #include <cstring>
  17. #include <iostream>
  18. #include <mujoco/mujoco.h>
  19. // help
  20. static constexpr char helpstring[] =
  21. "\n Usage: compile infile outfile\n"
  22. " infile can be in mjcf, urdf, mjb format\n"
  23. " outfile can be in mjcf, mjb, txt format\n\n"
  24. " Example: compile model.xml model.mjb\n";
  25. // deallocate and print message
  26. int finish(const char* msg = 0, mjModel* m = 0) {
  27. // deallocated everything
  28. if (m) {
  29. mj_deleteModel(m);
  30. }
  31. // print message
  32. if (msg) {
  33. std::cout << msg << std::endl;
  34. }
  35. return 0;
  36. }
  37. // possible file types
  38. enum {
  39. typeUNKNOWN = 0,
  40. typeXML,
  41. typeMJB,
  42. typeTXT
  43. };
  44. // determine file type
  45. int filetype(const char* filename) {
  46. // convert to lower case for string comparison
  47. char lower[1000];
  48. std::size_t i=0;
  49. while (i<std::strlen(filename) && i<999) {
  50. lower[i] = (char)tolower(filename[i]);
  51. i++;
  52. }
  53. lower[i] = 0;
  54. // find last dot
  55. int dot = (int)std::strlen(lower);
  56. while (dot>=0 && lower[dot]!='.') {
  57. dot--;
  58. }
  59. // no dot found
  60. if (dot<0) {
  61. return typeUNKNOWN;
  62. }
  63. // check extension
  64. if (!std::strcmp(lower+dot, ".xml") || !std::strcmp(lower+dot, ".urdf")) {
  65. return typeXML;
  66. } else if (!std::strcmp(lower+dot, ".mjb")) {
  67. return typeMJB;
  68. } else if (!std::strcmp(lower+dot, ".txt")) {
  69. return typeTXT;
  70. } else {
  71. return typeUNKNOWN;
  72. }
  73. }
  74. // main function
  75. int main(int argc, char** argv) {
  76. // model and error
  77. mjModel* m = 0;
  78. char error[1000];
  79. // print help if arguments are missing
  80. if (argc!=3) {
  81. return finish(helpstring);
  82. }
  83. // determine file types
  84. int type1 = filetype(argv[1]);
  85. int type2 = filetype(argv[2]);
  86. // check types
  87. if (type1==typeUNKNOWN || type1==typeTXT ||
  88. type2==typeUNKNOWN || (type1==typeMJB && type2==typeXML)) {
  89. return finish("Illegal combination of file formats");
  90. }
  91. // check if output file exists
  92. std::FILE* fp = std::fopen(argv[2], "r");
  93. if (fp) {
  94. std::cout << "Output file already exists, overwrite? (Y/n) ";
  95. char c;
  96. std::cin >> c;
  97. if (c!='y' && c!='Y') {
  98. std::fclose(fp);
  99. return finish();
  100. }
  101. }
  102. // load model
  103. if (type1==typeXML) {
  104. m = mj_loadXML(argv[1], 0, error, 1000);
  105. } else {
  106. m = mj_loadModel(argv[1], 0);
  107. }
  108. // check error
  109. if (!m) {
  110. if (type1==typeXML) {
  111. return finish(error, 0);
  112. } else {
  113. return finish("Could not load model", 0);
  114. }
  115. }
  116. // save model
  117. if (type2==typeXML) {
  118. if (!mj_saveLastXML(argv[2], m, error, 1000)) {
  119. return finish(error, m);
  120. }
  121. } else if (type2==typeMJB) {
  122. mj_saveModel(m, argv[2], 0, 0);
  123. } else {
  124. mj_printModel(m, argv[2]);
  125. }
  126. // finalize
  127. return finish("Done", m);
  128. }