hva_file.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. XCC Utilities and Library
  3. Copyright (C) 2000 Olaf van der Spek <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "stdafx.h"
  16. #include "hva_file.h"
  17. #include <fstream>
  18. #include "multi_line.h"
  19. #include "virtual_tfile.h"
  20. bool Chva_file::is_valid() const
  21. {
  22. const t_hva_header& h = header();
  23. int size = get_size();
  24. return !(sizeof(t_hva_header) > size
  25. || !h.c_frames
  26. || !h.c_sections
  27. || sizeof(t_hva_header) + (48 * h.c_frames + 16) * h.c_sections != size);
  28. }
  29. int Chva_file::extract_as_csv(const string& name) const
  30. {
  31. ofstream f(name.c_str());
  32. const int c_frames = get_c_frames();
  33. const int c_sections = get_c_sections();
  34. f << "Count frames;" << c_frames << endl
  35. << "Count sections;" << c_sections << endl
  36. << endl;
  37. for (int i = 0; i < c_sections; i++)
  38. f << get_section_id(i) << ";;;;;";
  39. for (int j = 0; j < c_frames; j++)
  40. {
  41. f << endl;
  42. for (int y = 0; y < 3; y++)
  43. {
  44. for (int i = 0; i < c_sections; i++)
  45. {
  46. for (int x = 0; x < 4; x++)
  47. {
  48. f << get_transform_matrix(i, j)[x + 4 * y] << ';';
  49. }
  50. f << ';';
  51. }
  52. f << endl;
  53. }
  54. }
  55. return f.fail();
  56. }
  57. Cvirtual_binary hva_file_write(const byte* s, int cb_s)
  58. {
  59. Cvirtual_binary d;
  60. const char sep = ';';
  61. Cvirtual_tfile f;
  62. f.load_data(Cvirtual_binary(s, cb_s));
  63. Cmulti_line l;
  64. l = f.read_line();
  65. l.get_next_line(sep);
  66. int c_frames = atoi(l.get_next_line(sep).c_str());
  67. l = f.read_line();
  68. l.get_next_line(sep);
  69. int c_sections = atoi(l.get_next_line(sep).c_str());
  70. f.read_line();
  71. byte* w = d.write_start(64 << 10);
  72. t_hva_header& header = *reinterpret_cast<t_hva_header*>(w);
  73. strcpy(header.id, "NONE");
  74. header.c_frames = c_frames;
  75. header.c_sections = c_sections;
  76. w += sizeof(t_hva_header);
  77. l = f.read_line();
  78. for (int i = 0; i < c_sections; i++)
  79. {
  80. strcpy(reinterpret_cast<char*>(w), l.get_next_line(sep).c_str());
  81. w += 16;
  82. l.get_next_line(sep);
  83. l.get_next_line(sep);
  84. l.get_next_line(sep);
  85. l.get_next_line(sep);
  86. }
  87. float* transform_matrix = reinterpret_cast<float*>(w);
  88. for (int j = 0; j < c_frames; j++)
  89. {
  90. for (int y = 0; y < 3; y++)
  91. {
  92. l = f.read_line();
  93. for (int i = 0; i < c_sections; i++)
  94. {
  95. for (int x = 0; x < 4; x++)
  96. {
  97. transform_matrix[12 * (c_frames * i + j) + x + 4 * y] = atof(l.get_next_line(sep).c_str());
  98. }
  99. l.get_next_line(sep);
  100. }
  101. }
  102. f.read_line();
  103. }
  104. w += 4 * 12 * c_frames * c_sections;
  105. d.set_size(w - d.data());
  106. return d;
  107. }