-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageMaker.cpp
More file actions
129 lines (110 loc) · 3.52 KB
/
ImageMaker.cpp
File metadata and controls
129 lines (110 loc) · 3.52 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
129
//
// Created by xbendik on 03.01.2017.
//
#include <fstream>
#include <iostream>
#include <sstream>
#include "ImageMaker.h"
ImageMaker::ImageMaker(double w, double h): width(w), height(h), curr({0,0}), scale_ratio(300), frame(1), sequence(1) {}
void ImageMaker::addLocation(std::vector<double> location) {
curr[0] += location[0] * scale_ratio;
curr[1] += location[1] * scale_ratio;
curr_ns = location;
locations.push_back(curr);
}
void ImageMaker::restart(){
curr = {0,0};
frame = 1;
sequence++;
locations.clear();
}
void ImageMaker::renderInput(std::string path) {
if(path == "") {
std::stringstream p;
p << "outputs\\input_";
p << sequence << "_" << frame << "_" << (int(curr[0]) + 100) << "_" << (int(curr[1]) + 100) << ".pgm";
path = p.str();
}
int offset = 100; //starting position is (offset,offset)
int x = 0, y = 0;
//build and fill canvas
std::vector<std::vector<int>> canvas(height, std::vector<int>(width,255)); //1 is white, 0 is black
for(auto& it : locations){
x = int(it[0]) + offset;
y = int(it[1]) + offset;
//std::cout << "x,y: " << x << " " << y << std::endl;
if(x >= 0 and x < width and y >= 0 and y < height) {
canvas[y][x] = 100;
}
}
//create PGM
std::ofstream outfile (path);
outfile << "P2" << std::endl;
outfile << width << " " << height << std::endl;
outfile << "255" << std::endl;
for(auto& it : canvas){
for(auto& it2: it){
outfile << it2 << " ";
}
outfile << std::endl;
}
outfile.close();
return;
}
void ImageMaker::renderProbs(std::string path, std::vector<std::vector<double> > points){
if(path == "") {
std::stringstream p;
p << "outputs\\prob_";
p << sequence << "_" << frame << "_" << (int(curr[0]) + 100) << "_" << (int(curr[1]) + 100) << ".ppm";
path = p.str();
}
int offset = 100; //starting position is (offset,offset)
int x = 0, y = 0;
std::vector<std::vector<std::vector<int> > > canvas (height, std::vector<std::vector<int> >(width, std::vector<int>(3,255)));
//calculate color scale
double max = points[0][2];
double min = max;
for(auto& it : points){
if(max < it[2])
max = it[2];
if(min > it[2])
min = it[2];
}
double one = max - min;
//prob of next points, the blacker the more probable
int color = 0;
for(auto& it : points){
x = int(it[0] * scale_ratio) + int(curr[0]) + offset;
y = int(it[1] * scale_ratio) + int(curr[1]) + offset;
color = 255 - int(((it[2] - min)/one) * 255);
if(x >= 0 and x < width and y >= 0 and y < height) {
canvas[y][x] = {color, color, color};
}
}
//underlying text
for(auto& it : locations){
x = int(it[0]) + offset;
y = int(it[1]) + offset;
if(x >= 0 and x < width and y >= 0 and y < height) {
canvas[y][x] = {255,0,0};
}
}
//mark current location (blue color)
canvas[int(curr[1]) + offset][int(curr[0]) + offset] = {0,0,255};
//create PPM
std::ofstream outfile (path);
outfile << "P3" << std::endl;
outfile << width << " " << height << std::endl;
outfile << "255" << std::endl;
for(auto& it : canvas){
for(auto& it2: it){
for(auto& color: it2) {
outfile << color << " ";
}
}
outfile << std::endl;
}
outfile.close();
frame++;
return;
}