Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. #include "primitives/mesh.h"
  2. #include "util/misc.h"
  3. #include "math/coordinate-systems.h"
  4. #include "math/quadratic_equation.h"
  5. #include "performance/performance.h"
  6. #include "primitives/triangle-primitive.h"
  7. #include <assert.h>
  8. #include <map>
  9. #include <fstream>
  10. #include <sstream>
  11. #include "primitives/union-primitive.h"
  12. #include "primitives/bounding-box-accelerator.h"
  13.  
  14. using namespace raytracer;
  15. using namespace math;
  16.  
  17. namespace raytracer
  18. {
  19.     namespace primitives
  20.     {
  21.         namespace _private_
  22.         {
  23.             class Mesh : public raytracer::primitives::_private_::PrimitiveImplementation
  24.             {
  25.             public:
  26.                 Primitive primitive;
  27.  
  28.                 Mesh(const std::string &path){
  29.                     primitive=readMesh(path);
  30.                 };
  31.  
  32.  
  33.                 std::vector<std::shared_ptr<Hit>> find_all_hits(const math::Ray& ray) const override
  34.                 {
  35.                     return primitive->find_all_hits(ray);
  36.                 }
  37.  
  38.                 bool find_first_positive_hit(const Ray& ray, Hit* hit) const override
  39.                 {
  40.                     return primitive->find_first_positive_hit(ray, hit);
  41.                 }
  42.  
  43.                 math::Box bounding_box() const override {
  44.                     return this->childBoundingBox;
  45.                 }
  46.             private:
  47.                 Primitive readMesh(const std::string &path) {
  48.                     std::map<int, Point3D> pointMap;
  49.                     std::map<int, Primitive> triangleMap;
  50.                     std::map<std::string,Primitive> boundingboxmap;
  51.                     std::ifstream file(path);
  52.                     if (!file) {
  53.                         std::cerr << "File not found";
  54.                         abort();
  55.                     }
  56.                     while (!file.eof()) {
  57.                         std::string line;
  58.                         std::getline(file, line);
  59.                         std::istringstream iss(line);
  60.                         std::string type;
  61.                         iss >> type;
  62.                         if (type == "P") {
  63.                             int i;
  64.                             double x, y, z;
  65.                             iss >> i, x, y, z;
  66.                             pointMap[i]=Point3D(x,y,z);
  67.                         }
  68.                         if (type == "T") {
  69.                             int i, p1, p2, p3;
  70.                             iss >> i, p1, p2, p3;
  71.                             Primitive triangle=raytracer::primitives::triangle(pointMap[p1], pointMap[p2], pointMap[p3]);
  72.                             triangleMap[i] = triangle;
  73.                         }
  74.                         if (type != "P"&&type != "T") {
  75.                             std::string a;
  76.                             std::vector<std::string> currentline;
  77.                             std::vector<Primitive> triangles;
  78.                             while (iss >> a) {
  79.                                 currentline.push_back(a);
  80.                             }
  81.                             std::string bbnr=type+"_"+currentline[0];
  82.                             if (currentline[1] == "false") {
  83.                                 int amount = atoi(currentline[2].c_str());
  84.                                 for (int i = 3; i < amount+3; i++) {
  85.                                     int number = atoi(currentline[i].c_str());
  86.                                     auto triangle=triangleMap[number];
  87.                                     triangles.push_back(triangle);
  88.                                 }
  89.                                 Primitive triangle_union = make_union(triangles);
  90.                                 Primitive bb = bounding_box_accelerator(triangle_union);
  91.                                 boundingboxmap[bbnr] = bb;
  92.                             }
  93.                             else {
  94.                                 type.erase(0, 1);
  95.                                 int indexbb = atoi(type.c_str())+1;
  96.                                 std::string indexstring1 ="B"+std::to_string(indexbb) + "_"+currentline[2];
  97.                                 std::string indexstring2 = "B"+std::to_string(indexbb) + "_" + currentline[3];
  98.                                 Primitive bb1 = boundingboxmap[indexstring1];
  99.                                 Primitive bb2 = boundingboxmap[indexstring2];
  100.                                 boundingboxmap.erase(indexstring1);
  101.                                 boundingboxmap.erase(indexstring2);
  102.                                 std::vector<Primitive> list;
  103.                                 list.push_back(bb1);
  104.                                 list.push_back(bb2);
  105.                                 Primitive bb_union = make_union(list);
  106.                                 boundingboxmap[bbnr] = bounding_box_accelerator(bb_union);
  107.                             }
  108.  
  109.                         }
  110.  
  111.                     }
  112.                     return boundingboxmap["B1_1"];
  113.                 }
  114.  
  115.             };
  116.         }
  117.     }
  118. }
  119.  
  120.  
  121. Primitive raytracer::primitives::mesh(const std::string &path)
  122. {
  123.     return Primitive(std::make_shared<raytracer::primitives::_private_::Mesh>(path));
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement