<commit_before>// ======================================================================== //
// Copyright 2009-2016 Intel Corporation                                    //
//                                                                          //
// Licensed under the Apache License, Version 2.0 (the "License");          //
// you may not use this file except in compliance with the License.         //
// You may obtain a copy of the License at                                  //
//                                                                          //
//     http://www.apache.org/licenses/LICENSE-2.0                           //
//                                                                          //
// Unless required by applicable law or agreed to in writing, software      //
// distributed under the License is distributed on an "AS IS" BASIS,        //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and      //
// limitations under the License.                                           //
// ======================================================================== //

// run the parser with assertions, even in release mode:
#undef NDEBUG

#include "miniSG.h"
#include "importer.h"
// xml lib
#include "common/xml/XML.h"
// std
#include <fstream>
#include <set>

namespace ospray {
  namespace miniSG {

    using std::cout;
    using std::endl;

    void warnIgnore(const std::string &nodeType)
    {
      static std::set<std::string> alreadyWarned;
      if (alreadyWarned.find(nodeType) != alreadyWarned.end()) return;
      alreadyWarned.insert(nodeType);
      std::cout << "#miniSG::X3D: ignoring node type " << nodeType 
                << " (more instances may follow)"
                << std::endl;
      
    }
    void parseVectorOfVec3fas(std::vector<vec3fa> &vec, const std::string &str)
    {
      char *s = strdup(str.c_str());
      const char *delim = "\n\t\r, ";
      char *tok = strtok(s,delim);
      while (tok) {
        vec3fa v;
        v.x = atof(tok);

        tok = strtok(NULL,delim);
        if (!tok) break;
        v.y = atof(tok);
        
        tok = strtok(NULL,delim);
        if (!tok) break;
        v.z = atof(tok);

        vec.push_back(v);

        tok = strtok(NULL,delim);
      }
      free(s);
    }
    void parseIndexedFaceSet(Model &model, const affine3f &xfm, xml::Node *root)
    {
      Ref<Mesh> mesh = new Mesh;
      mesh->material = new Material;

      // -------------------------------------------------------
      // parse coordinate indices
      // -------------------------------------------------------
      std::string coordIndex = root->getProp("coordIndex");
      assert(coordIndex != "");

      char *s = strdup(coordIndex.c_str()); coordIndex = "";
      const char *delim = "\n\t\r, ";
      char *tok = strtok(s,delim);
      std::vector<int> ID;
      while (tok) {
        long thisID = atol(tok);
        if (thisID == -1) {
          for (size_t i = 2; i < ID.size(); i++) {
            Triangle t;
            t.v0 = ID[0];
            t.v1 = ID[i-1];
            t.v2 = ID[i];
            mesh->triangle.push_back(t);
          }
          ID.clear();
        } else {
          ID.push_back(thisID);
        }
        tok = strtok(NULL,delim);
      }
      free(s);

      // -------------------------------------------------------
      // now, parse children for vertex arrays
      // -------------------------------------------------------
      for (size_t childID = 0; childID < root->child.size(); childID++) {
        xml::Node *node = root->child[childID];
        
        if (node->name == "Coordinate") {
          parseVectorOfVec3fas(mesh->position,node->getProp("point"));
          continue;
        }
        if (node->name == "Normal") {
          parseVectorOfVec3fas(mesh->normal,node->getProp("vector"));
          continue;
        }
        if (node->name == "Color") {
          /* ignore for now */
          parseVectorOfVec3fas(mesh->color,node->getProp("color"));
          // warnIgnore("'Color' (in IndexedFaceSet)");
          continue;
        }

        throw std::runtime_error("importX3D: unknown child type '"
                                 + node->name + "' to 'IndexedFaceSet' node");
      }
<commit_msg>Don't abort when encountering unkown types in X3D file, just warn<commit_after>// ======================================================================== //
// Copyright 2009-2016 Intel Corporation                                    //
//                                                                          //
// Licensed under the Apache License, Version 2.0 (the "License");          //
// you may not use this file except in compliance with the License.         //
// You may obtain a copy of the License at                                  //
//                                                                          //
//     http://www.apache.org/licenses/LICENSE-2.0                           //
//                                                                          //
// Unless required by applicable law or agreed to in writing, software      //
// distributed under the License is distributed on an "AS IS" BASIS,        //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and      //
// limitations under the License.                                           //
// ======================================================================== //

// run the parser with assertions, even in release mode:
#undef NDEBUG

#include "miniSG.h"
#include "importer.h"
// xml lib
#include "common/xml/XML.h"
// std
#include <fstream>
#include <set>

namespace ospray {
  namespace miniSG {

    using std::cout;
    using std::endl;

    void warnIgnore(const std::string &nodeType)
    {
      static std::set<std::string> alreadyWarned;
      if (alreadyWarned.find(nodeType) != alreadyWarned.end()) return;
      alreadyWarned.insert(nodeType);
      std::cout << "#miniSG::X3D: ignoring node type " << nodeType 
                << " (more instances may follow)"
                << std::endl;
      
    }
    void parseVectorOfVec3fas(std::vector<vec3fa> &vec, const std::string &str)
    {
      char *s = strdup(str.c_str());
      const char *delim = "\n\t\r, ";
      char *tok = strtok(s,delim);
      while (tok) {
        vec3fa v;
        v.x = atof(tok);

        tok = strtok(NULL,delim);
        if (!tok) break;
        v.y = atof(tok);
        
        tok = strtok(NULL,delim);
        if (!tok) break;
        v.z = atof(tok);

        vec.push_back(v);

        tok = strtok(NULL,delim);
      }
      free(s);
    }
    void parseIndexedFaceSet(Model &model, const affine3f &xfm, xml::Node *root)
    {
      Ref<Mesh> mesh = new Mesh;
      mesh->material = new Material;

      // -------------------------------------------------------
      // parse coordinate indices
      // -------------------------------------------------------
      std::string coordIndex = root->getProp("coordIndex");
      assert(coordIndex != "");

      char *s = strdup(coordIndex.c_str()); coordIndex = "";
      const char *delim = "\n\t\r, ";
      char *tok = strtok(s,delim);
      std::vector<int> ID;
      while (tok) {
        long thisID = atol(tok);
        if (thisID == -1) {
          for (size_t i = 2; i < ID.size(); i++) {
            Triangle t;
            t.v0 = ID[0];
            t.v1 = ID[i-1];
            t.v2 = ID[i];
            mesh->triangle.push_back(t);
          }
          ID.clear();
        } else {
          ID.push_back(thisID);
        }
        tok = strtok(NULL,delim);
      }
      free(s);

      // -------------------------------------------------------
      // now, parse children for vertex arrays
      // -------------------------------------------------------
      for (size_t childID = 0; childID < root->child.size(); childID++) {
        xml::Node *node = root->child[childID];
        
        if (node->name == "Coordinate") {
          parseVectorOfVec3fas(mesh->position,node->getProp("point"));
          continue;
        }
        if (node->name == "Normal") {
          parseVectorOfVec3fas(mesh->normal,node->getProp("vector"));
          continue;
        }
        if (node->name == "Color") {
          /* ignore for now */
          parseVectorOfVec3fas(mesh->color,node->getProp("color"));
          // warnIgnore("'Color' (in IndexedFaceSet)");
          continue;
        }

        std::cout << "importX3D: unknown child type '" << node->name
          << "' to 'IndexedFaceSet' node\n";
      }

{'fraction_non_alphanumeric': 0.20674285714285714, 'fraction_numerical': 0.0067428571428571425, 'mean_word_length': 1.380576713819369, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 27, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4111216', 'n_tokens_mistral': 2348, 'n_tokens_neox': 2268, 'n_words': 588}