Skip to content

Commit 824c6f1

Browse files
committed
Improve & Update progress struct
1 parent 607d4b6 commit 824c6f1

File tree

3 files changed

+44
-32
lines changed

3 files changed

+44
-32
lines changed

main.cpp

+35-22
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,35 @@
1717
#include "camera.h"
1818

1919
struct Progress {
20-
int num;
20+
int num = 0;
2121
int total;
2222
std::chrono::system_clock::time_point start;
2323
double delta_t = 0;
24+
double previous_elapsed_time = 0;
25+
int count = 0;
26+
int unit;
2427

25-
Progress(int total) : num(0), total(total) , start(std::chrono::system_clock::now()){}
28+
Progress(int total) : total(total), start(std::chrono::system_clock::now()), previous_elapsed_time(elapsed()),
29+
unit(total / 400) {}
2630

2731
void show() {
28-
double percent = (double)num / total * 100;
29-
delta_t += 1. / std::max(elapsed(), 1.) * (elapsed() / std::max(num, 1) - delta_t);
32+
double elapsed_time = elapsed();
33+
double percent = (double) num / total * 100;
34+
35+
double recently_delta_t = num ? (elapsed_time - previous_elapsed_time) / (num - unit * count) : 0;
36+
if (num >= unit * (count + 1)) {
37+
count = num / unit;
38+
previous_elapsed_time = elapsed_time;
39+
}
40+
delta_t += 0.005 / std::max(elapsed_time, 1.) * (recently_delta_t - delta_t);
3041
double remain = delta_t * (total - num);
3142
std::cout << "\r" << std::fixed << std::setprecision(2) <<
32-
percent << "%\tremain: " << (int)remain << "[s]" << std::flush;
43+
percent << "%\tremain: " << (int) remain << "[s]\ttotal: " << elapsed_time << "[s]" << std::flush;
3344
}
3445

3546
double elapsed() {
36-
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - start).count() / 1000.;
47+
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - start).count() /
48+
1000.;
3749
}
3850

3951
void end() {
@@ -42,35 +54,37 @@ struct Progress {
4254
};
4355

4456
int main() {
45-
Image img(512, 512);
57+
Image img(256, 256);
4658

47-
// コーネルボックス
59+
// コーネルボックス
4860
// std::vector<std::shared_ptr<ShapeBase>> shapes{
49-
// std::make_shared<Sphere>(Vector3(0, 10003.5, 0), 10000, Vector3(), Diffuse(Vector3(0.75))), // 上壁
50-
// std::make_shared<Sphere>(Vector3(10004, 0, 0), 10000, Vector3(), Diffuse(Vector3(0.25, 0.25, 0.75))) , //右壁
51-
// std::make_shared<Sphere>(Vector3(0, -10003.5, 0), 10000, Vector3(), Diffuse(Vector3(0.75))), // 下壁
52-
// std::make_shared<Sphere>(Vector3(-10004, 0, 0), 10000, Vector3(), Diffuse(Vector3(0.75, 0.25, 0.25))), // 左壁
53-
// std::make_shared<Sphere>(Vector3(0, 0, 10004), 10000, Vector3(0), Diffuse(Vector3(0.75))), // 奥壁
54-
// std::make_shared<Sphere>(Vector3(0, 23.48, 2), 20, Vector3(12), Diffuse(Vector3())), // ライト
61+
// std::make_shared<Sphere>(Vector3(0, 10003.5, 0), 10000, Vector3(), Diffuse(Vector3(0.75))), // 上壁
62+
// std::make_shared<Sphere>(Vector3(10004, 0, 0), 10000, Vector3(), Diffuse(Vector3(0.25, 0.25, 0.75))), //右壁
63+
// std::make_shared<Sphere>(Vector3(0, -10003.5, 0), 10000, Vector3(), Diffuse(Vector3(0.75))), // 下壁
64+
// std::make_shared<Sphere>(Vector3(-10004, 0, 0), 10000, Vector3(), Diffuse(Vector3(0.75, 0.25, 0.25))), // 左壁
65+
// std::make_shared<Sphere>(Vector3(0, 0, 10004), 10000, Vector3(0), Diffuse(Vector3(0.75))), // 奥壁
66+
// std::make_shared<Sphere>(Vector3(0, 23.48, 2), 20, Vector3(12), Diffuse(Vector3())), // ライト
5567
//
56-
// std::make_shared<Sphere>(Vector3(-2, -2.3, 3), 1.2, Vector3(), Diffuse(Vector3(0.999))),
57-
// std::make_shared<Sphere>(Vector3(2, -2.3, 2), 1.2, Vector3(), Diffuse(Vector3(0.25, 0.75, 0.25))),
58-
//};
68+
// std::make_shared<Sphere>(Vector3(-2, -2.3, 3), 1.2, Vector3(), Diffuse(Vector3(0.999))),
69+
// std::make_shared<Sphere>(Vector3(2, -2.3, 2), 1.2, Vector3(), Diffuse(Vector3(0.25, 0.75, 0.25))),
70+
// std::make_shared<ParallelLight>(Vector3(0, 0, -1), M_PI * 15/180, Vector3(1), Diffuse(Vector3(0))),
71+
// };
5972

6073
//
6174
std::vector<std::shared_ptr<ShapeBase>> shapes{
6275
std::make_shared<Sphere>(Vector3(0, -10004, 0), 10000, Vector3(), Diffuse(Vector3(0.75))),
6376
std::make_shared<Sphere>(Vector3(-2, -2.8, 3), 1.2, Vector3(), Diffuse(Vector3(0.999))),
6477
std::make_shared<Sphere>(Vector3(2, -2.8, 2), 1.2, Vector3(), Diffuse(Vector3(0.25, 0.75, 0.25))),
6578
std::make_shared<Sphere>(Vector3(0, -3, 6), 1, Vector3(), Fuzz(Vector3(0.75), 0.2 * M_PI)),
66-
std::make_shared<ParallelLight>(Vector3(1, 2, 1), M_PI * 30/180, Vector3(2), Diffuse(Vector3(0))) //太陽の半頂角
79+
std::make_shared<ParallelLight>(Vector3(1, 2, 1), M_PI * 15/180, Vector3(1), Diffuse(Vector3(0))),
6780
};
6881

69-
Scene scene(shapes, Vector3(0.5, 0.6, 0.9), 1024, 100);
70-
std::shared_ptr<CameraBase> camera = std::make_shared<NormalCamera>(img, Vector3(0, 0, -6), Vector3(0), M_PI*3/8);
82+
Scene scene(shapes, Vector3(0.15, 0.2, 0.35), 4*1000, 1e5);
83+
std::shared_ptr<CameraBase> camera = std::make_shared<NormalCamera>(
84+
img, Vector3(0, 0, -6), Vector3(0), M_PI * 3 / 8);
7185
Progress progress(img.size);
7286

73-
#pragma omp parallel for schedule(auto)
87+
#pragma omp parallel for schedule(dynamic, 1)
7488
for (int x = 0; x < img.width; ++x) {
7589
for (int y = 0; y < img.height; ++y) {
7690
#ifdef NDEBUG
@@ -89,7 +103,6 @@ int main() {
89103
img.set_pixel(x, y, L / scene.spp);
90104
}
91105
}
92-
93106
img.output_ppm("./result.ppm");
94107
progress.end();
95108
return 0;

scene.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,10 @@ struct Scene {
4141
if (!hit || ray.depth >= lim_depth) {
4242
return sky_color;
4343
}
44-
Vector3 L_illuminance = hit->hit_shape_ptr->illuminance;
45-
46-
auto[next_ray, f] = hit->hit_shape_ptr->reflect(hit->point, ray.direction, hit->normal);
44+
auto[next_ray, f, L_illuminance] = hit->hit_shape_ptr->reflect(hit->point, ray.direction, hit->normal);
4745
next_ray.depth = ray.depth + 1;
4846

49-
Vector3 next_L = random_::rand() < roulette_p && f != Vector3() ? L(next_ray, roulette_p) : Vector3();
47+
Vector3 next_L = random_::rand() > roulette_p || f == Vector3(0) ? Vector3(0) : L(next_ray, roulette_p);
5048
return L_illuminance + next_L * f / roulette_p;
5149
}
5250
};

shape.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct ShapeBase {
2525

2626
virtual std::optional<Hit> intersect(const Ray &ray) const = 0;
2727

28-
virtual std::tuple<Ray, Vector3>
28+
virtual std::tuple<Ray, Vector3, Vector3>
2929
reflect(const Vector3 &point, const Vector3 &in_direction, const Vector3 &normal) const = 0;
3030
};
3131

@@ -61,8 +61,9 @@ struct Sphere : public ShapeBase {
6161
return {};
6262
}
6363

64-
std::tuple<Ray, Vector3> reflect(const Vector3 &point, const Vector3 &in_direction, const Vector3 &normal) const {
65-
return material_ptr->reflect(point, in_direction, normal);
64+
std::tuple<Ray, Vector3, Vector3> reflect(const Vector3 &point, const Vector3 &in_direction, const Vector3 &normal) const {
65+
auto [next_ray, f] = material_ptr->reflect(point, in_direction, normal);
66+
return {next_ray, f, illuminance};
6667
};
6768

6869
};
@@ -74,7 +75,7 @@ struct ParallelLight : ShapeBase {
7475

7576
template<class T>
7677
ParallelLight(const Vector3 &direction, double half_vertex_angle, const Vector3 &illuminance, T material_ptr) :
77-
ShapeBase(illuminance / 2 / M_PI / (1 - std::cos(half_vertex_angle)), std::make_shared<T>(material_ptr)),
78+
ShapeBase(illuminance, std::make_shared<T>(material_ptr)),
7879
direction(direction), half_vertex_angle(half_vertex_angle) {}
7980

8081
std::optional<Hit> intersect(const Ray &ray) const {
@@ -87,8 +88,8 @@ struct ParallelLight : ShapeBase {
8788
return {};
8889
};
8990

90-
std::tuple<Ray, Vector3> reflect(const Vector3 &, const Vector3 &, const Vector3 &) const {
91-
return {Ray(), Vector3()};
91+
std::tuple<Ray, Vector3, Vector3> reflect(const Vector3 &, const Vector3 &, const Vector3 &) const {
92+
return {Ray(), Vector3(), illuminance / 2 / M_PI / (1 - std::cos(half_vertex_angle))};
9293
};
9394
};
9495

0 commit comments

Comments
 (0)