Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "primitives/mesh.h"
- #include "util/misc.h"
- #include "math/coordinate-systems.h"
- #include "math/quadratic_equation.h"
- #include "performance/performance.h"
- #include "primitives/triangle-primitive.h"
- #include <assert.h>
- #include <map>
- #include <fstream>
- #include <sstream>
- #include "primitives/union-primitive.h"
- #include "primitives/bounding-box-accelerator.h"
- using namespace raytracer;
- using namespace math;
- namespace raytracer
- {
- namespace primitives
- {
- namespace _private_
- {
- class Mesh : public raytracer::primitives::_private_::PrimitiveImplementation
- {
- public:
- Primitive primitive;
- Mesh(const std::string &path){
- primitive=readMesh(path);
- };
- std::vector<std::shared_ptr<Hit>> find_all_hits(const math::Ray& ray) const override
- {
- return primitive->find_all_hits(ray);
- }
- bool find_first_positive_hit(const Ray& ray, Hit* hit) const override
- {
- return primitive->find_first_positive_hit(ray, hit);
- }
- math::Box bounding_box() const override {
- return this->childBoundingBox;
- }
- private:
- Primitive readMesh(const std::string &path) {
- std::map<int, Point3D> pointMap;
- std::map<int, Primitive> triangleMap;
- std::map<std::string,Primitive> boundingboxmap;
- std::ifstream file(path);
- if (!file) {
- std::cerr << "File not found";
- abort();
- }
- while (!file.eof()) {
- std::string line;
- std::getline(file, line);
- std::istringstream iss(line);
- std::string type;
- iss >> type;
- if (type == "P") {
- int i;
- double x, y, z;
- iss >> i, x, y, z;
- pointMap[i]=Point3D(x,y,z);
- }
- if (type == "T") {
- int i, p1, p2, p3;
- iss >> i, p1, p2, p3;
- Primitive triangle=raytracer::primitives::triangle(pointMap[p1], pointMap[p2], pointMap[p3]);
- triangleMap[i] = triangle;
- }
- if (type != "P"&&type != "T") {
- std::string a;
- std::vector<std::string> currentline;
- std::vector<Primitive> triangles;
- while (iss >> a) {
- currentline.push_back(a);
- }
- std::string bbnr=type+"_"+currentline[0];
- if (currentline[1] == "false") {
- int amount = atoi(currentline[2].c_str());
- for (int i = 3; i < amount+3; i++) {
- int number = atoi(currentline[i].c_str());
- auto triangle=triangleMap[number];
- triangles.push_back(triangle);
- }
- Primitive triangle_union = make_union(triangles);
- Primitive bb = bounding_box_accelerator(triangle_union);
- boundingboxmap[bbnr] = bb;
- }
- else {
- type.erase(0, 1);
- int indexbb = atoi(type.c_str())+1;
- std::string indexstring1 ="B"+std::to_string(indexbb) + "_"+currentline[2];
- std::string indexstring2 = "B"+std::to_string(indexbb) + "_" + currentline[3];
- Primitive bb1 = boundingboxmap[indexstring1];
- Primitive bb2 = boundingboxmap[indexstring2];
- boundingboxmap.erase(indexstring1);
- boundingboxmap.erase(indexstring2);
- std::vector<Primitive> list;
- list.push_back(bb1);
- list.push_back(bb2);
- Primitive bb_union = make_union(list);
- boundingboxmap[bbnr] = bounding_box_accelerator(bb_union);
- }
- }
- }
- return boundingboxmap["B1_1"];
- }
- };
- }
- }
- }
- Primitive raytracer::primitives::mesh(const std::string &path)
- {
- return Primitive(std::make_shared<raytracer::primitives::_private_::Mesh>(path));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement