-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cpp
More file actions
128 lines (121 loc) · 4.7 KB
/
Copy pathParser.cpp
File metadata and controls
128 lines (121 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
** EPITECH PROJECT, 2022
** B-OOP-400-MAR-4-1-raytracer-corentin.fortes
** File description:
** Paeser.cpp
*/
#include "Parser.hpp"
#include "Shape.hpp"
Parser::Parser(std::string path) : path(path)
{
cfg.readFile(path.c_str());
}
std::unique_ptr<RayTracer::Camera> Parser::getCamera()
{
int camera_width = cfg.lookup("camera.resolution.width");
int camera_height = cfg.lookup("camera.resolution.height");
double cam_x = cfg.lookup("camera.position.x");
double cam_y = cfg.lookup("camera.position.y");
double cam_z = cfg.lookup("camera.position.z");
std::unique_ptr<RayTracer::Camera> cam(new RayTracer::Camera(Math::Point3D(cam_x, cam_y, cam_z), camera_width, camera_height));
return cam;
}
void Parser::getLight()
{
const libconfig::Setting& ls = cfg.lookup("lights.light");
for (int i = 0; i < ls.getLength(); ++i) {
const libconfig::Setting& l = ls[i];
std::string type = l["type"];
if (type == "directional") {
double pourcent = l["percent"];
double x = l["x"];
double y = l["y"];
double z = l["z"];
std::unique_ptr<ILight> light(new Directional(Math::Vector3D(x, y, z), pourcent));
lights.insert(std::pair<int, std::unique_ptr<ILight>>(i, std::move(light)));
} else if (type == "point") {
double pourcent = l["percent"];
double x = l["x"];
double y = l["y"];
double z = l["z"];
std::unique_ptr<ILight> light(new Point(Math::Point3D(x, y, z), pourcent));
lights.insert(std::pair<int, std::unique_ptr<ILight>>(i, std::move(light)));
}
}
}
void Parser::getShape(std::string name)
{
std::string line;
std::ifstream inFile;
inFile.open(path);
while (line.find("lights") == std::string::npos) {
std::getline(inFile, line);
if (line.find("spheres") != std::string::npos) {
const libconfig::Setting& spheres = cfg.lookup("shapes.spheres");
for (int i = 0; i < spheres.getLength(); ++i) {
const libconfig::Setting& sphere = spheres[i];
double x = sphere["x"];
double y = sphere["y"];
double z = sphere["z"];
double r = sphere["radius"];
const libconfig::Setting& color = sphere["color"];
int r_ = color["r"];
int g_ = color["g"];
int b_ = color["b"];
std::unique_ptr<IShape> shape(new Sphere(Math::Point3D(x, y, z), r));
shape->setColor(r_, g_, b_);
shapes.insert(std::pair<int, std::unique_ptr<IShape>>(n, std::move(shape)));
n++;
}
}
if (line.find("planes") != std::string::npos) {
const libconfig::Setting& spheres = cfg.lookup("shapes.planes");
for (int i = 0; i < spheres.getLength(); ++i) {
const libconfig::Setting& sphere = spheres[i];
std::string axis = sphere["axis"];
char c = axis[0];
double pos = sphere["position"];
const libconfig::Setting& color = sphere["color"];
int r_ = color["r"];
int g_ = color["g"];
int b_ = color["b"];
std::unique_ptr<IShape> shape(new Plane(pos, c));
shape->setColor(r_, g_, b_);
shapes.insert(std::pair<int, std::unique_ptr<IShape>>(n, std::move(shape)));
n++;
}
}
if (line.find("cylindres") != std::string::npos) {
const libconfig::Setting& spheres = cfg.lookup("shapes.cylindres");
for (int i = 0; i < spheres.getLength(); ++i) {
const libconfig::Setting& sphere = spheres[i];
double x = sphere["x"];
double y = sphere["y"];
double z = sphere["z"];
double r = sphere["radius"];
std::string axis = sphere["axis"];
char c = axis[0];
const libconfig::Setting& color = sphere["color"];
int r_ = color["r"];
int g_ = color["g"];
int b_ = color["b"];
std::unique_ptr<IShape> shape(new Cylindre(Math::Point3D(x, y, z), r, c));
shape->setColor(r_, g_, b_);
shapes.insert(std::pair<int, std::unique_ptr<IShape>>(n, std::move(shape)));
n++;
}
}
}
}
void Parser::parsing()
{
std::ifstream inFile;
inFile.open(path);
while (!inFile.eof()) {
std::string line;
std::getline(inFile, line);
if (line.find("shapes") != std::string::npos)
getShape(line);
}
inFile.close();
}