<commit_before>// ======================================================================== //
// Copyright 2015-2018 <NAME>                                            //
//                                                                          //
// 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.                                           //
// ======================================================================== //

// pbrt_parser
#include "pbrt_parser/Scene.h"
// ospcommon
#include "ospcommon/common.h"
// stl
#include <iostream>
#include <vector>
#include <sstream>
#include <set>

namespace pbrt_parser {

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

  bool endsWith(const std::string &s, const std::string &suffix)
  {
    return s.substr(s.size()-suffix.size(),suffix.size()) == suffix;
  }

  struct PBRTInfo {
    PBRTInfo(Scene::SP scene)
    {
      traverse(scene->world);
      numObjects.print("objects");
      numShapes.print("shapes");
      numShapes.print("triangles");
      numCurves.print("curves");
      numCurveSegments.print("curve segments");
    }

    void traverseTriangleMesh(Shape::SP mesh, bool firstTime)
    {
      if (mesh->findParam<float>("P") && mesh->findParam<int>("indices")) {
        numTriangles.add(firstTime,mesh->findParam<int>("indices")->getSize()/3);
      } else {
        throw std::runtime_error("triangle mesh, but not loaded ....");
      }
    }

    void traverseCurve(Shape::SP curve, bool firstTime)
    {
      if (curve->findParam<float>("P")) {
        numCurves.add(firstTime,1);
        numCurveSegments.add(firstTime,curve->findParam<float>("P")->getSize()-1);
      } else {
        throw std::runtime_error("curve, but no 'P' field!?");
      }
    }

    void traverse(Object::SP object)
    {
      const bool firstTime = (alreadyTraversed.find(object) == alreadyTraversed.end());
      alreadyTraversed.insert(object);

      numObjects.add(firstTime,1);
      numLights.add(firstTime,object->lightSources.size());
      numVolumes.add(firstTime,object->volumes.size());
      numShapes.add(firstTime,object->shapes.size());
      numInstances.add(firstTime,object->objectInstances.size());
      
      for (auto shape : object->shapes) {
        const std::string type = shape->type;
        if (type == "trianglemesh")
          traverseTriangleMesh(shape,firstTime);
        else if (type == "curve")
          traverseCurve(shape,firstTime);
        else
          std::cout << "un-handled shape " << shape->type << std::endl;
      }

      for (auto inst : object->objectInstances) {
        traverse(inst->object);
      }
    }

    std::set<Object::SP> alreadyTraversed;
    
    struct Counter {
      void print(const std::string &name)
      {
        std::cout << "number of " << name << std::endl;
        std::cout << " - unique    : " << ospcommon::prettyNumber(unique) << std::endl;
        std::cout << " - instanced : " << ospcommon::prettyNumber(instanced) << std::endl;
      }
      void add(bool firstTime, size_t N) { instanced += N; if (firstTime) unique += N; }
      
      size_t unique = 0;
      size_t instanced = 0;
    };

    Counter numInstances, numTriangles, numObjects, numVertices, numCurves, numCurveSegments, numShapes, numLights, numVolumes;
  };
  
  void pbrtInfo(int ac, char **av)
  {
    std::string fileName;
    for (int i=1;i<ac;i++) {
      const std::string arg = av[i];
      if (arg[0] == '-') {
          throw std::runtime_error("invalid argument '"+arg+"'");
      } else {
        fileName = arg;
      }          
    }
    
    std::cout << "-------------------------------------------------------" << std::endl;
    std::cout << "pbrtinfo - printing info on pbrt file ..." << std::endl;
    std::cout << "-------------------------------------------------------" << std::endl;
    
    std::shared_ptr<Scene> scene;
    try {
      scene
        = (endsWith(fileName,".pbff")||endsWith(fileName,".pb"))
        ? pbrtParser_readFromBinary(fileName)
        : pbrt_parser::Scene::parseFromFile(fileName);
      std::cout << " => yay! parsing successful..." << std::endl;
      PBRTInfo info(scene);
    } catch (std::runtime_error e) {
      std::cerr << "**** ERROR IN PARSING ****" << std::endl << e.what() << std::endl;
      std::cerr << "(this means that either there's something wrong with that PBRT file, or that the parser can't handle it)" << std::endl;
      exit(1);
    }
  }
  
} // ::pbrt_parser

int main(int ac, char **av)
{
  pbrt_parser::pbrtInfo(ac,av);
  return 0;
}
<commit_msg>bugfix: 'numtrianlges' now prints out triangles, not shapes<commit_after>// ======================================================================== //
// Copyright 2015-2018 <NAME>                                            //
//                                                                          //
// 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.                                           //
// ======================================================================== //

// pbrt_parser
#include "pbrt_parser/Scene.h"
// ospcommon
#include "ospcommon/common.h"
// stl
#include <iostream>
#include <vector>
#include <sstream>
#include <set>

namespace pbrt_parser {

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

  bool endsWith(const std::string &s, const std::string &suffix)
  {
    return s.substr(s.size()-suffix.size(),suffix.size()) == suffix;
  }

  struct PBRTInfo {
    PBRTInfo(Scene::SP scene)
    {
      traverse(scene->world);
      numObjects.print("objects");
      numShapes.print("shapes");
      numTriangles.print("triangles");
      numCurves.print("curves");
      numCurveSegments.print("curve segments");
    }

    void traverseTriangleMesh(Shape::SP mesh, bool firstTime)
    {
      if (mesh->findParam<float>("P") && mesh->findParam<int>("indices")) {
        numTriangles.add(firstTime,mesh->findParam<int>("indices")->getSize()/3);
      } else {
        throw std::runtime_error("triangle mesh, but not loaded ....");
      }
    }

    void traverseCurve(Shape::SP curve, bool firstTime)
    {
      if (curve->findParam<float>("P")) {
        numCurves.add(firstTime,1);
        numCurveSegments.add(firstTime,curve->findParam<float>("P")->getSize()-1);
      } else {
        throw std::runtime_error("curve, but no 'P' field!?");
      }
    }

    void traverse(Object::SP object)
    {
      const bool firstTime = (alreadyTraversed.find(object) == alreadyTraversed.end());
      alreadyTraversed.insert(object);

      numObjects.add(firstTime,1);
      numLights.add(firstTime,object->lightSources.size());
      numVolumes.add(firstTime,object->volumes.size());
      numShapes.add(firstTime,object->shapes.size());
      numInstances.add(firstTime,object->objectInstances.size());
      
      for (auto shape : object->shapes) {
        const std::string type = shape->type;
        if (type == "trianglemesh")
          traverseTriangleMesh(shape,firstTime);
        else if (type == "curve")
          traverseCurve(shape,firstTime);
        else
          std::cout << "un-handled shape " << shape->type << std::endl;
      }

      for (auto inst : object->objectInstances) {
        traverse(inst->object);
      }
    }

    std::set<Object::SP> alreadyTraversed;
    
    struct Counter {
      void print(const std::string &name)
      {
        std::cout << "number of " << name << std::endl;
        std::cout << " - unique    : " << ospcommon::prettyNumber(unique) << std::endl;
        std::cout << " - instanced : " << ospcommon::prettyNumber(instanced) << std::endl;
      }
      void add(bool firstTime, size_t N) { instanced += N; if (firstTime) unique += N; }
      
      size_t unique = 0;
      size_t instanced = 0;
    };

    Counter numInstances, numTriangles, numObjects, numVertices, numCurves, numCurveSegments, numShapes, numLights, numVolumes;
  };
  
  void pbrtInfo(int ac, char **av)
  {
    std::string fileName;
    for (int i=1;i<ac;i++) {
      const std::string arg = av[i];
      if (arg[0] == '-') {
          throw std::runtime_error("invalid argument '"+arg+"'");
      } else {
        fileName = arg;
      }          
    }
    
    std::cout << "-------------------------------------------------------" << std::endl;
    std::cout << "pbrtinfo - printing info on pbrt file ..." << std::endl;
    std::cout << "-------------------------------------------------------" << std::endl;
    
    std::shared_ptr<Scene> scene;
    try {
      scene
        = (endsWith(fileName,".pbff")||endsWith(fileName,".pb"))
        ? pbrtParser_readFromBinary(fileName)
        : pbrt_parser::Scene::parseFromFile(fileName);
      std::cout << " => yay! parsing successful..." << std::endl;
      PBRTInfo info(scene);
    } catch (std::runtime_error e) {
      std::cerr << "**** ERROR IN PARSING ****" << std::endl << e.what() << std::endl;
      std::cerr << "(this means that either there's something wrong with that PBRT file, or that the parser can't handle it)" << std::endl;
      exit(1);
    }
  }
  
} // ::pbrt_parser

int main(int ac, char **av)
{
  pbrt_parser::pbrtInfo(ac,av);
  return 0;
}

{'fraction_non_alphanumeric': 0.1894756589438139, 'fraction_numerical': 0.004127192571053372, 'mean_word_length': 2.0532646048109964, 'pattern_counts': {'":': 0, '<': 133, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2923254', 'n_tokens_mistral': 3092, 'n_tokens_neox': 2994, 'n_words': 677}