-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.cpp
259 lines (230 loc) · 6.62 KB
/
main.cpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <fstream>
#include <sstream>
#include <string>
#include <iostream>
#include <vector>
#include "imgui/imgui.hh"
#include "imgui/imgui_impl_sdl.hh"
#include <functional>
#include "spline.h"
#include "spline_light.h"
#if defined(__EMSCRIPTEN__)
#define DIR_PATH ("/dir")
#include <emscripten.h>
#define SDL_DISABLE_IMMINTRIN_H
#include <SDL.h>
#include <SDL_opengles2.h>
#else
#define DIR_PATH ("/tmp/dir")
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#endif
#include <experimental/filesystem>
#define FILE_MAX_SIZE (20000)
#define WIDTH (1000)
#define HEIGHT (1000)
namespace fs =std::experimental::filesystem::v1;
std::vector<std::vector<float>> values;
std::vector<float> vec;
bool callback(SOURCE_TYPE* data, size_t size, size_t imf)
{
std::cout << "Imf=" << imf << std::endl;
values.push_back(std::vector<float>());
values[imf - 1].resize(size);
for (size_t i = 0; i < size; i++)
{
values[imf-1][i] = data[i];
}
// if(imf >=3)
// return false;//stop execution after 3rd IMF
return true;
};
SDL_Window* window_;
SDL_GLContext context_;
std::vector<std::string> listOfFiles;
std::vector<std::string>& getListOfFiles()
{
return listOfFiles;
}
int getXBottom(const std::vector<float>& v)
{
int res = std::numeric_limits<int>::max();
for(auto it : v)
{
if(it < res)
res = it;
}
return res-1;
}
int getXTop(const std::vector<float>& v)
{
int res = std::numeric_limits<int>::min();
for(auto it : v)
{
if(it > res)
res = it;
}
return res+1;
}
void init()
{
for (auto & p : fs::directory_iterator(DIR_PATH))
{
std::string path = p.path();
size_t pos = path.rfind('/')+1;
size_t npos = path.rfind('.')-pos;
listOfFiles.push_back(path.substr(pos, npos));
}
ImGui::CreateContext();
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
std::cout << "SDL_Init error" << std::endl;
return;
}
window_ = SDL_CreateWindow("", 0, 0, WIDTH, HEIGHT, SDL_WINDOW_OPENGL);
context_ = SDL_GL_CreateContext(window_);
ImGui_ImplSdl_Init(window_);
}
int processFile(std::string file, size_t algType, std::vector<float>& result)
{
std::string filePath = DIR_PATH;
filePath += "/" + file + ".txt";
std::cout << "Process file=" << filePath << std::endl;
std::fstream fs;
fs.open( filePath, std::fstream::in );
if( !fs.good() )
{
std::cout << "File doesn't exist" << std::endl;
return 0;
}
std::string line;
size_t fileLength=0;
values.clear();
vec.clear();
while ( std::getline(fs, line ))
{
std::string lline = line;
double val = 0;
val = std::stod(lline);
fileLength++;
vec.push_back( val );
if (fileLength >= FILE_MAX_SIZE)
break;
}
size_t quntN = vec.size();
//source signal
std::vector<SOURCE_TYPE> cvec( &vec[0], &vec[quntN] );
if ( vec.size() == quntN )
vec.push_back( *vec.rbegin() );
clock_t begin = clock();
int procres = 0;
if(algType == 1)
{
FlyContextE fce;
//temporary buffer
std::vector<SOURCE_TYPE> tBuf;
tBuf.resize(vec.size());
std::cout << "light spline" << std::endl;
procres = rParabEmd__LFlyEmb(&cvec[0], &tBuf[0], cvec.size(), 15, 15, &fce, callback);
}
else
{
ContextE ce;
alloc(&ce, FILE_MAX_SIZE);//you can init pointers by static buffers if you need, or write your own allocator for it
clearFlyC(&ce.minfc);
clearFlyC(&ce.maxfc);
//temporary buffer
std::vector<double> tBuf;
tBuf.resize(vec.size());
std::cout << "classic spline" << std::endl;
procres = rParabEmd__LEmb(&cvec[0], cvec.size(), 15, 15, &ce, &tBuf[0], callback);
release(&ce);
}
result = cvec;
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
std::cout << " Res=" << procres << " Time spent=" << time_spent << std::endl;
return 0;
}
std::vector<float> result;
void main_loop()
{
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_SetWindowPosition(window_, 0, 0);
ImGui::PushStyleColor(ImGuiCol_WindowBg, IM_COL32(0xF1, 0xF1, 0xF1,255));
ImGui::PushStyleColor(ImGuiCol_PopupBg, IM_COL32(0xF1, 0xF1, 0xF1,255));
ImGui::PushStyleColor(ImGuiCol_PlotLines, IM_COL32(0, 0, 0, 255));
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(0, 0, 0, 255));
SDL_GL_MakeCurrent(window_, context_);
ImGui_ImplSdl_NewFrame(window_);
ImGui::SetNextWindowSize(ImVec2(WIDTH, HEIGHT), ImGuiCond_Appearing);
bool showWin = 1;
ImGui::Begin("", &showWin, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove);
static std::string current_item;
if (ImGui::BeginCombo("##combo", current_item.c_str())) // The second parameter is the label previewed before opening the combo.
{
auto files = getListOfFiles();
for (int n = 0; n < files.size(); n++)
{
bool is_selected = (current_item.compare(files[n])==0); // You can store your selection however you want, outside or inside your objects
if (ImGui::Selectable(files[n].c_str(), is_selected))
{
current_item = files[n];
}
if (is_selected)
{
ImGui::SetItemDefaultFocus(); // You may set the initial focus when opening the combo (scrolling + for keyboard navigation support)
}
}
ImGui::EndCombo();
}
ImVec2 btnsize(200, 30);
if (ImGui::Button("Classic spline", btnsize))
{
result.clear();
processFile(current_item, 0, result);
}
ImGui::SameLine();
if (ImGui::Button("light weight spline", btnsize))
{
result.clear();
processFile(current_item, 1, result);
}
size_t lineH = (HEIGHT-300)/((values.size()>0)?values.size():1);
if(lineH >80)
lineH = 80;
ImGui::PlotLines("Source", &vec[0], vec.size(), 0, NULL, getXBottom(vec), getXTop(vec), ImVec2(0, lineH));
for(int i =0; i< values.size();i ++)
{
std::stringstream hdr;
hdr << "Imf(";
hdr << i+1;
hdr << ")";
std::string hdrStr = hdr.str().c_str();
ImGui::PlotLines(hdrStr.c_str(), &values[i][0], values[i].size(), 0, NULL, getXBottom(values[i]), getXTop(values[i]), ImVec2(0, lineH));
}
ImGui::PlotLines("(Result)", &result[0], result.size(), 0, NULL, getXBottom(result), getXTop(result), ImVec2(0, lineH));
ImGui::End();
ImGui::PopStyleColor(4);
glViewport(0, 0, WIDTH,HEIGHT);
ImVec4 g_clear_color = ImColor(0xF1, 0xF1, 0xF1);
glClearColor(g_clear_color.x, g_clear_color.y, g_clear_color.z, g_clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
ImGui::Render();
SDL_GL_SwapWindow(window_);
}
int main()
{
init();
#if defined(__EMSCRIPTEN__)
emscripten_set_main_loop(main_loop, 60, 1);
#else
while(true) {
main_loop();
SDL_Delay(1);
}
#endif
return 0;
}