X3DXmlHelper.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #include "X3DXmlHelper.h"
  2. #include "X3DImporter.hpp"
  3. #include <assimp/ParsingUtils.h>
  4. namespace Assimp {
  5. bool X3DXmlHelper::getColor3DAttribute(XmlNode &node, const char *attributeName, aiColor3D &color) {
  6. std::string val;
  7. if (XmlParser::getStdStrAttribute(node, attributeName, val)) {
  8. std::vector<std::string> values;
  9. tokenize<std::string>(val, values, " ");
  10. if (values.size() != 3) {
  11. Throw_ConvertFail_Str2ArrF(node.name(), attributeName);
  12. }
  13. auto it = values.begin();
  14. color.r = stof(*it++);
  15. color.g = stof(*it++);
  16. color.b = stof(*it);
  17. return true;
  18. }
  19. return false;
  20. }
  21. bool X3DXmlHelper::getVector2DAttribute(XmlNode &node, const char *attributeName, aiVector2D &color) {
  22. std::string val;
  23. if (XmlParser::getStdStrAttribute(node, attributeName, val)) {
  24. std::vector<std::string> values;
  25. tokenize<std::string>(val, values, " ");
  26. if (values.size() != 2) {
  27. Throw_ConvertFail_Str2ArrF(node.name(), attributeName);
  28. }
  29. auto it = values.begin();
  30. color.x = stof(*it++);
  31. color.y = stof(*it);
  32. return true;
  33. }
  34. return false;
  35. }
  36. bool X3DXmlHelper::getVector3DAttribute(XmlNode &node, const char *attributeName, aiVector3D &color) {
  37. std::string val;
  38. if (XmlParser::getStdStrAttribute(node, attributeName, val)) {
  39. std::vector<std::string> values;
  40. tokenize<std::string>(val, values, " ");
  41. if (values.size() != 3) {
  42. Throw_ConvertFail_Str2ArrF(node.name(), attributeName);
  43. }
  44. auto it = values.begin();
  45. color.x = stof(*it++);
  46. color.y = stof(*it++);
  47. color.z = stof(*it);
  48. return true;
  49. }
  50. return false;
  51. }
  52. bool X3DXmlHelper::getBooleanArrayAttribute(XmlNode &node, const char *attributeName, std::vector<bool> &boolArray) {
  53. std::string val;
  54. if (XmlParser::getStdStrAttribute(node, attributeName, val)) {
  55. std::vector<std::string> values;
  56. tokenize<std::string>(val, values, " ");
  57. auto it = values.begin();
  58. while (it != values.end()) {
  59. auto s = *it++;
  60. if (!s.empty())
  61. boolArray.push_back(s[0] == 't' || s[0] == '1');
  62. else
  63. Throw_ConvertFail_Str2ArrB(node.name(), attributeName);
  64. }
  65. return true;
  66. }
  67. return false;
  68. }
  69. bool X3DXmlHelper::getDoubleArrayAttribute(XmlNode &node, const char *attributeName, std::vector<double> &doubleArray) {
  70. std::string val;
  71. if (XmlParser::getStdStrAttribute(node, attributeName, val)) {
  72. std::vector<std::string> values;
  73. tokenize<std::string>(val, values, " ");
  74. auto it = values.begin();
  75. while (it != values.end()) {
  76. auto s = *it++;
  77. if (!s.empty())
  78. doubleArray.push_back(atof(s.c_str()));
  79. else
  80. Throw_ConvertFail_Str2ArrD(node.name(), attributeName);
  81. }
  82. return true;
  83. }
  84. return false;
  85. }
  86. bool X3DXmlHelper::getFloatArrayAttribute(XmlNode &node, const char *attributeName, std::vector<float> &floatArray) {
  87. std::string val;
  88. if (XmlParser::getStdStrAttribute(node, attributeName, val)) {
  89. std::vector<std::string> values;
  90. tokenize<std::string>(val, values, " ");
  91. auto it = values.begin();
  92. while (it != values.end()) {
  93. auto s = *it++;
  94. if (!s.empty())
  95. floatArray.push_back((float)atof(s.c_str()));
  96. else
  97. Throw_ConvertFail_Str2ArrF(node.name(), attributeName);
  98. }
  99. return true;
  100. }
  101. return false;
  102. }
  103. bool X3DXmlHelper::getInt32ArrayAttribute(XmlNode &node, const char *attributeName, std::vector<int32_t> &intArray) {
  104. std::string val;
  105. if (XmlParser::getStdStrAttribute(node, attributeName, val)) {
  106. std::vector<std::string> values;
  107. tokenize<std::string>(val, values, " ");
  108. auto it = values.begin();
  109. while (it != values.end()) {
  110. auto s = *it++;
  111. if (!s.empty())
  112. intArray.push_back((int32_t)atof(s.c_str()));
  113. else
  114. Throw_ConvertFail_Str2ArrI(node.name(), attributeName);
  115. }
  116. return true;
  117. }
  118. return false;
  119. }
  120. bool X3DXmlHelper::getStringListAttribute(XmlNode &node, const char *attributeName, std::list<std::string> &stringList) {
  121. std::string val;
  122. if (XmlParser::getStdStrAttribute(node, attributeName, val)) {
  123. std::vector<std::string> values;
  124. tokenize<std::string>(val, values, " ");
  125. auto it = values.begin();
  126. std::string currentConcat = "";
  127. bool inQuotes = false;
  128. while (it != values.end()) {
  129. auto s = *it++;
  130. if (!s.empty()) {
  131. if (inQuotes) {
  132. if (*(s.rbegin()) == '"') {
  133. stringList.push_back(currentConcat + s.substr(0, s.length() - 1));
  134. currentConcat = "";
  135. inQuotes = false;
  136. } else {
  137. currentConcat += " " + s;
  138. }
  139. } else {
  140. if (s[0] == '"') {
  141. currentConcat = s.substr(1);
  142. inQuotes = true;
  143. } else {
  144. stringList.push_back(s);
  145. }
  146. }
  147. } else if (!inQuotes)
  148. Throw_ConvertFail_Str2ArrI(node.name(), attributeName);
  149. }
  150. if (inQuotes) Throw_ConvertFail_Str2ArrI(node.name(), attributeName);
  151. return true;
  152. }
  153. return false;
  154. }
  155. bool X3DXmlHelper::getStringArrayAttribute(XmlNode &node, const char *attributeName, std::vector<std::string> &stringArray) {
  156. std::list<std::string> tlist;
  157. if (getStringListAttribute(node, attributeName, tlist)) {
  158. if (!tlist.empty()) {
  159. stringArray.reserve(tlist.size());
  160. for (std::list<std::string>::iterator it = tlist.begin(); it != tlist.end(); ++it) {
  161. stringArray.push_back(*it);
  162. }
  163. return true;
  164. }
  165. }
  166. return false;
  167. }
  168. bool X3DXmlHelper::getVector2DListAttribute(XmlNode &node, const char *attributeName, std::list<aiVector2D> &vectorList) {
  169. std::string val;
  170. if (XmlParser::getStdStrAttribute(node, attributeName, val)) {
  171. std::vector<std::string> values;
  172. tokenize<std::string>(val, values, " ");
  173. if (values.size() % 2) Throw_ConvertFail_Str2ArrF(node.name(), attributeName);
  174. auto it = values.begin();
  175. while (it != values.end()) {
  176. aiVector2D tvec;
  177. tvec.x = (float)atof((*it++).c_str());
  178. tvec.y = (float)atof((*it++).c_str());
  179. vectorList.push_back(tvec);
  180. }
  181. return true;
  182. }
  183. return false;
  184. }
  185. bool X3DXmlHelper::getVector2DArrayAttribute(XmlNode &node, const char *attributeName, std::vector<aiVector2D> &vectorArray) {
  186. std::list<aiVector2D> tlist;
  187. if (getVector2DListAttribute(node, attributeName, tlist)) {
  188. if (!tlist.empty()) {
  189. vectorArray.reserve(tlist.size());
  190. for (std::list<aiVector2D>::iterator it = tlist.begin(); it != tlist.end(); ++it) {
  191. vectorArray.push_back(*it);
  192. }
  193. return true;
  194. }
  195. }
  196. return false;
  197. }
  198. bool X3DXmlHelper::getVector3DListAttribute(XmlNode &node, const char *attributeName, std::list<aiVector3D> &vectorList) {
  199. std::string val;
  200. if (XmlParser::getStdStrAttribute(node, attributeName, val)) {
  201. std::vector<std::string> values;
  202. tokenize<std::string>(val, values, " ");
  203. if (values.size() % 3 != 0) Throw_ConvertFail_Str2ArrF(node.name(), attributeName);
  204. auto it = values.begin();
  205. while (it != values.end()) {
  206. aiVector3D tvec;
  207. tvec.x = (float)atof((*it++).c_str());
  208. tvec.y = (float)atof((*it++).c_str());
  209. tvec.z = (float)atof((*it++).c_str());
  210. vectorList.push_back(tvec);
  211. }
  212. return true;
  213. }
  214. return false;
  215. }
  216. bool X3DXmlHelper::getVector3DArrayAttribute(XmlNode &node, const char *attributeName, std::vector<aiVector3D> &vectorArray) {
  217. std::list<aiVector3D> tlist;
  218. if (getVector3DListAttribute(node, attributeName, tlist)) {
  219. if (!tlist.empty()) {
  220. vectorArray.reserve(tlist.size());
  221. for (std::list<aiVector3D>::iterator it = tlist.begin(); it != tlist.end(); ++it) {
  222. vectorArray.push_back(*it);
  223. }
  224. return true;
  225. }
  226. }
  227. return false;
  228. }
  229. bool X3DXmlHelper::getColor3DListAttribute(XmlNode &node, const char *attributeName, std::list<aiColor3D> &colorList) {
  230. std::string val;
  231. if (XmlParser::getStdStrAttribute(node, attributeName, val)) {
  232. std::vector<std::string> values;
  233. tokenize<std::string>(val, values, " ");
  234. if (values.size() % 3 != 0) Throw_ConvertFail_Str2ArrF(node.name(), attributeName);
  235. auto it = values.begin();
  236. while (it != values.end()) {
  237. aiColor3D tvec;
  238. tvec.r = (float)atof((*it++).c_str());
  239. tvec.g = (float)atof((*it++).c_str());
  240. tvec.b = (float)atof((*it++).c_str());
  241. colorList.push_back(tvec);
  242. }
  243. return true;
  244. }
  245. return false;
  246. }
  247. bool X3DXmlHelper::getColor4DListAttribute(XmlNode &node, const char *attributeName, std::list<aiColor4D> &colorList) {
  248. std::string val;
  249. if (XmlParser::getStdStrAttribute(node, attributeName, val)) {
  250. std::vector<std::string> values;
  251. tokenize<std::string>(val, values, " ");
  252. if (values.size() % 4 != 0) Throw_ConvertFail_Str2ArrF(node.name(), attributeName);
  253. auto it = values.begin();
  254. while (it != values.end()) {
  255. aiColor4D tvec;
  256. tvec.r = (float)atof((*it++).c_str());
  257. tvec.g = (float)atof((*it++).c_str());
  258. tvec.b = (float)atof((*it++).c_str());
  259. tvec.a = (float)atof((*it++).c_str());
  260. colorList.push_back(tvec);
  261. }
  262. return true;
  263. }
  264. return false;
  265. }
  266. } // namespace Assimp