Skip to content

Commit 31489f9

Browse files
Patch main for auto positioning
1 parent 025b02a commit 31489f9

File tree

4 files changed

+82
-18
lines changed

4 files changed

+82
-18
lines changed

include/raytracer/datatypes.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ inline Vec3f operator-(Vec3f rhs, double val) {
6363
};
6464

6565
inline std::ostream& operator<<(std::ostream& os, Vec3f vec) {
66-
os << "[ " << vec.x << " " << vec.y << " " << vec.z << " ]" << std::endl;
66+
os << "[ " << vec.x << " " << vec.y << " " << vec.z << " ]";
6767
return os;
6868
}
6969

include/raytracer/geometry.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,12 @@ class Triangle : public Object {
157157

158158
class Scene {
159159
public:
160-
Scene(Objects&& eobjects,
160+
Scene(Objects&& objects,
161161
Lights&& lights,
162162
std::vector<GeometricVertex>&& geom_vertices);
163163

164+
void AddLight(Light &&);
165+
164166
const Objects& GetObjects() const;
165167
const Lights& GetLights() const;
166168
const std::vector<GeometricVertex>& GetGeometricVertices() const;

main.cpp

+74-16
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,95 @@
33

44
#include <raytracer/raytracer.hpp>
55

6-
static Vec3f GetCenter(const Scene& scene) {
7-
Vec3f c{0, 0, 0};
8-
for (auto&& v : scene.GetGeometricVertices()) {
9-
c = c + Vec3f{v.x, v.y, v.z};
6+
class BBox {
7+
public:
8+
BBox(const std::vector<GeometricVertex> &vertices) {
9+
const auto &v = vertices.front();
10+
11+
Vec3f min = Vec3f{v.x, v.y, v.z};
12+
Vec3f max = Vec3f{v.x, v.y, v.z};
13+
14+
for (auto&& v : vertices) {
15+
min.x = std::min(min.x, v.x);
16+
min.y = std::min(min.y, v.y);
17+
min.z = std::min(min.z, v.z);
18+
19+
max.x = std::max(max.x, v.x);
20+
max.y = std::max(max.y, v.y);
21+
max.z = std::max(max.z, v.z);
22+
}
23+
24+
_coords[0] = Vec3f{min.x, min.y, min.z};
25+
_coords[1] = Vec3f{max.x, min.y, min.z};
26+
_coords[2] = Vec3f{min.x, max.y, min.z};
27+
_coords[3] = Vec3f{max.x, max.y, min.z};
28+
_coords[4] = Vec3f{min.x, min.y, max.z};
29+
_coords[5] = Vec3f{max.x, min.y, max.z};
30+
_coords[6] = Vec3f{min.x, max.y, max.z};
31+
_coords[7] = Vec3f{max.x, max.y, max.z};
1032
}
11-
return c / scene.GetGeometricVertices().size();
12-
}
33+
34+
Vec3f GetCenter() const {
35+
return { (_coords[0].x + _coords[7].x) / 2,
36+
(_coords[0].y + _coords[7].y) / 2,
37+
(_coords[0].z + _coords[7].z) / 2 };
38+
}
39+
40+
double GetDiag() const {
41+
return (_coords[0] - _coords[6]).norm();
42+
}
43+
44+
private:
45+
std::array<Vec3f, 8> _coords;
46+
};
1347

1448
int main(int argc, const char** argv) {
1549
CameraOptions camera_opts(500, 500);
1650
RenderOptions render_opts{3};
1751

52+
if (argc < 2) {
53+
throw std::logic_error("Provide path to texture file (*.obj)");
54+
}
55+
56+
double zoom = 1.0;
57+
if (argc == 3) {
58+
zoom = std::stod(argv[2]);
59+
}
60+
1861
const std::string obj_filename = argv[1];
19-
const auto scene = Parse(obj_filename);
20-
const auto center = GetCenter(scene);
62+
auto scene = Parse(obj_filename);
63+
BBox bbox(scene.GetGeometricVertices());
64+
auto c = bbox.GetCenter();
65+
auto d = bbox.GetDiag();
66+
Vec3f look = {c.x + d * zoom,
67+
c.y + d * zoom,
68+
c.z + d * zoom};
69+
70+
std::cout << "[INFO] Zoom is equal to " << zoom << std::endl;
71+
std::cout << "[INFO] Object center is: " << c << std::endl;
72+
std::cout << "[INFO] Diag: " << d << std::endl;
2173

22-
std::cout << "Object center is: " << center << std::endl;
74+
if (scene.GetLights().empty()) {
75+
std::cout << "[WARNING] Scene doesn't contain any light object." << std::endl;
76+
// NB: Put some light by default.
77+
scene.AddLight(Light{look, {1, 1, 1}});
78+
}
79+
80+
std::cout << "[INFO] Number of objects on scene: " << scene.GetObjects().size() << std::endl;
2381

24-
camera_opts.look_from = std::array<double, 3>{std::stod(argv[2]),
25-
std::stod(argv[3]),
26-
std::stod(argv[4])};
27-
camera_opts.look_to = std::array<double, 3>{center.x, center.y, center.z};
82+
camera_opts.look_from = std::array<double, 3>{look.x, look.y, look.z};
83+
camera_opts.look_to = std::array<double, 3>{c.x, c.y, c.z};
2884

2985
using namespace std::chrono;
3086
auto start = high_resolution_clock::now();
31-
auto image = Render(obj_filename, camera_opts, render_opts);
87+
auto image = Render(scene, camera_opts, render_opts);
3288
auto end = high_resolution_clock::now();
3389
auto elapsed = duration_cast<milliseconds>(end-start).count();
34-
image.Write("output.png");
3590

36-
std::cout << "Elapsed time: " << elapsed << " ms" << std::endl;
91+
std::cout << "[INFO] Rendering time: " << elapsed << " ms" << std::endl;
92+
93+
image.Write("output.png");
94+
std::cout << "[INFO] Dump result to output.png" << std::endl;
3795

3896
return 0;
3997
}

src/geometry.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ const std::vector<GeometricVertex>& Scene::GetGeometricVertices() const {
2020
return _geom_vertices;
2121
}
2222

23+
void Scene::AddLight(Light &&light) {
24+
_lights.push_back(std::move(light));
25+
}
26+
2327
Vec3f Ray::at(double t) const {
2428
return orig + dir * t;
2529
}

0 commit comments

Comments
 (0)