X3DXmlHelper.cpp 10 KB

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