-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdef.cpp
More file actions
626 lines (518 loc) · 18.2 KB
/
Copy pathdef.cpp
File metadata and controls
626 lines (518 loc) · 18.2 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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
//
// def.cpp
// MinorProject
// use clang++ -std=c++11 -stdlib=libc++ def.cpp to compile on terminal
// Created by Keshav Choudhary on 20/02/13.
// Copyright (c) 2013 Keshav Choudhary. All rights reserved.
//
#include "def.h"
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
unsigned long long universalTid;
// DEBUG VARIABLE
double InitializeData::convertDouble(string str) {
double ans;
stringstream sstr(str); // create a stringstream
sstr>>ans; // push the stream into the num
return ans;
}
void InitializeData::buildMap(const char *imageFile, const char *weatherMapFile) {
ifstream imfile(imageFile), wmfile(weatherMapFile);
int imx, imy, wmx, wmy, z;
imfile>>imx>>imy>>z;
wmfile>>wmx>>wmy;
assert(imx == wmx && imy == wmy);
imageStore.resize(imx);
for(int i = 0; i < imx; i++) {
imageStore[i].resize(imy);
for(int j = 0; j < imy; j++) {
imageStore[i][j].resize(z);
}
}
weatherDegree.resize(wmx);
for(int i = 0; i < wmx; i++) {
weatherDegree[i].resize(wmy);
}
for(int k = 0; k < z; k++) {
for(int i = 0; i < imx; i++) {
for(int j = 0; j < imy; j++)
imfile>>imageStore[i][j][k];
}
}
for(int i = 0; i < wmx; i++) {
for(int j = 0; j < wmy; j++)
wmfile>>weatherDegree[i][j];
}
for(int i = 0; i < imx; i++) {
for(int j = 0; j < imy; j++){
colormap[weatherDegree[i][j]] = make_pair(imageStore[i][j][0], make_pair(imageStore[i][j][1], imageStore[i][j][2]));
}
}
imfile.close();
wmfile.close();
}
void InitializeData::split(string data, vector<string> points) {
stringstream ss(data);
ss>>points[0]>>points[1]>>points[2];
}
void InitializeData::triPush(string p1, string p2, string p3, pair<int,float> vd, bool visible) {
unordered_map<string,vertex>::iterator point1 = map.find (p1);
if(point1 == map.end())
cout<<"Error : Vertex 1 not found"<<endl;
if(point1->second.curIndex >= 36)
cout<<"Vertex allready in 36 traingles"<<endl;
else {
point1->second.neighbours[point1->second.curIndex] = universalTid;
point1->second.curIndex++;
}
unordered_map<string,vertex>::iterator point2 =map.find (p2);
if(point2 == map.end())
cout<<"Error : Vertex 2 not found"<<endl;
if(point2->second.curIndex >= 36)
cout<<"Vertex allready in 36 traingles"<<endl;
else {
point2->second.neighbours[point2->second.curIndex] = universalTid;
point2->second.curIndex++;
}
unordered_map<string,vertex>::iterator point3 =map.find (p3);
if(point3 == map.end())
cout<<"Error : Vertex 3 not found"<<endl;
if(point3->second.curIndex >= 36)
cout<<"Vertex allready in 36 traingles"<<endl;
else {
point3->second.neighbours[point3->second.curIndex] = universalTid;
point3->second.curIndex++;
}
triangle t(&(point1->second),&(point2->second),&(point3->second),universalTid,vd.first,-vd.second,visible);
Triangles.push_back(t);
universalTid++;
}
void InitializeData::formTriangles(const char* fileName)
{
ifstream myfile (fileName);
pair<int,double> voxelDetails;
vector<string> cube(8);
string data;
if (myfile.is_open()) {
int index = 0;
int corroded;
while (myfile.good()) {
getline (myfile, data);
if(index == 0 ) {
stringstream ss(data);
int voxelID;
string aux;
double weathingD;
corroded = 1;
ss>>voxelID>>aux>>corroded;
if (aux.length() > 0)
weathingD = convertDouble(aux.substr(1));
else
weathingD = 0.0;
voxelDetails = make_pair(voxelID,weathingD); // would be used in each of the 12 traingles
}
else if (index >= 1 && index <= 8 )
cube[index-1] = data;
else if (index == 9){
stringstream ss(data);
string a[6];
int b[6],c[6],d[6];
ss>>a[0]>>b[0]>>c[0]>>d[0]>>a[1]>>b[1]>>c[1]>>d[1]>>a[2]>>b[2]>>c[2]>>d[2]>>a[3]>>b[3]>>c[3]>>d[3]>>a[4]>>b[4]>>c[4]>>d[4]>>a[5]>>b[5]>>c[5]>>d[5];
bool visibleVoxel = false;
if(b[0] == -1 || b[1] == -1|| b[2] == -1 || b[3] == -1 || b[4] == -1 || b[5] == -1 || d[0] == 1 || d[1] == 1|| d[2] == 1 || d[3] == 1 || d[4] == 1 || d[5] == 1)
visibleVoxel = true;
//Only if the voxel is visible I would push the points in my unordered map
if (visibleVoxel) {
for ( int num = 0; num < (int)cube.size(); num++) {
string data = cube[num];
vector<double> points(3);
unordered_map<string,vertex>::const_iterator got = map.find (data);
if ( got == map.end() ) { // new vertex
stringstream ss(data);
ss>>points[0]>>points[1]>>points[2];
vertex m(points[0],points[1],points[2]);
map[data] = m;
}
}
// Put the vertices in anti clockwise direction so
// the normals point outward from the surface.
/*
4++++++++0
+ +
5++++++++1 +
+ + + +
+ 7++++++++3
6++++++++2
*/
//right face -- 0
bool visible = false;
if(d[0] == 1 && corroded == 0)
visible = true;
triPush(cube[0], cube[1], cube[2],voxelDetails,visible);
triPush(cube[0], cube[2], cube[3],voxelDetails,visible);
//back face -- 1
visible = false;
if(d[1] == 1 && corroded == 0)
visible = true;
triPush(cube[4], cube[0], cube[7],voxelDetails,visible);
triPush(cube[0], cube[3], cube[7],voxelDetails,visible);
//left face -- 2
visible = false;
if(d[2] == 1 && corroded == 0)
visible = true;
triPush(cube[4], cube[7], cube[5],voxelDetails,visible);
triPush(cube[5], cube[7], cube[6],voxelDetails,visible);
//front face -- 3
visible = false;
if(d[3] == 1 && corroded == 0)
visible = true;
triPush(cube[1], cube[5], cube[6],voxelDetails,visible);
triPush(cube[1], cube[6], cube[2],voxelDetails,visible);
// top face -- 4
visible = false;
if(d[4] == 1 && corroded == 0)
visible = true;
triPush(cube[4], cube[5], cube[1],voxelDetails,visible);
triPush(cube[4], cube[1], cube[0],voxelDetails,visible);
//bottom face -- 5
visible = false;
if(d[5] == 1 && corroded == 0)
visible = true;
triPush(cube[2], cube[6], cube[7],voxelDetails,visible);
triPush(cube[2], cube[7], cube[3],voxelDetails,visible);
}
}
index = (index+1)%10;
}
}
}
map< double, pair< int, pair<int,int> > >::const_iterator findClose(map< double, pair< int, pair<int,int> > > mymap, double value) {
auto last = mymap.begin();
for(auto it = mymap.begin(); it != mymap.end(); it++) {
if (it->first > value) {
break;
}
else {
last = it;
}
}
return last;
}
vector<int> InitializeData::colorMap(float corval) {
vector<int> rgb(3,0);
if(corval >= 0.0 && corval <= 0.1)
{
rgb[0] = 215;
rgb[1] = 0;
rgb[2] = 0;
}
else if(corval > 0.1 && corval <= 0.2)
{
rgb[0] = 215;
rgb[1] = 95;
rgb[2] = 0;
}
else if(corval > 0.2 && corval <= 0.3)
{
rgb[0] = 215;
rgb[1] = 135;
rgb[2] = 0;
}
else if(corval > 0.3 && corval <= 0.4)
{
rgb[0] = 215;
rgb[1] = 175;
rgb[2] = 0;
}
else if(corval > 0.4 && corval <= 0.5)
{
rgb[0] = 215;
rgb[1] = 215;
rgb[2] = 0;
}
else if(corval > 0.5 && corval <= 0.6)
{
rgb[0] = 215;
rgb[1] = 255;
rgb[2] = 0;
}
else if(corval > 0.6 && corval <= 0.7)
{
rgb[0] = 255;
rgb[1] = 255;
rgb[2] = 0;
}
else if(corval > 0.7 && corval <= 0.8)
{
rgb[0] = 255;
rgb[1] = 215;
rgb[2] = 0;
}
else if(corval > 0.8 && corval <= 0.9)
{
rgb[0] = 255;
rgb[1] = 175;
rgb[2] = 0;
}
else
{
rgb[0] = 255;
rgb[1] = 135;
rgb[2] = 0;
}
return rgb;
}
// impart color to all the vertices depeding on their neighbourhood
void InitializeData::impartColor()
{
// we can run through all vertices use the neighbourhood information and impart them a color using the corrosion values of the neighbours
for ( auto it = map.begin(); it != map.end(); ++it )
{
vertex q = it->second;
float sum = 0.0f;
int count = 0;
for (int i = 0; i < 36 ; i++ )
{
if(q.neighbours[i] != -1)
{
sum += Triangles[q.neighbours[i]].corrosionLevel;
count++;
}
}
float val = sum / (1.0f * count);
it->second.avgCorrosionLevel = val;
if(colorModel == 1)
{
vector<int> rgb = colorMap(val);
it->second.r = 1.0*rgb[0]/255.0;
it->second.g = 1.0*rgb[1]/255.0;
it->second.b = 1.0*rgb[2]/255.0;
}
else if(colorModel == 2)
{
it->second.r = 0.2 + 0.4*(1.0-val);
it->second.g = 0.2 + 0.4*(1.0-val);
it->second.b = 0.2 + 0.4*(1.0-val);
}
else if (colorModel == 3) {
it->second.r = 0.1 + 0.8*(1.0-val);
it->second.g = 0.1 + 0.8*(1.0-val);
it->second.b = 0.1 + 0.8*(1.0-val);
}
else if (colorModel == 4) {
auto pos = findClose(colormap, val);
auto low = pos;
auto up = --pos;
double w1 = (val - up->first)/(low->first - up->first);
if (!(w1 >= 0.0 && w1 <= 1.0))
w1 = 1.0;
it->second.r = (double)(w1*(low->second.first) + (1.0-w1)*(up->second.first))/255.0;
it->second.g = (double)(w1*(low->second.second.first) + (1.0-w1)*(up->second.second.first))/255.0;
it->second.b = (double)(w1*(low->second.second.second) + (1.0-w1)*(up->second.second.second))/255.0;
}
}
}
void InitializeData::calculateNormals()
{
for(auto it = Triangles.begin(); it != Triangles.end(); ++it)
{
double ux = it->second->x - it->first->x;
double uy = it->second->y - it->first->y;
double uz = it->second->z - it->first->z;
double vx = it->third->x - it->first->x;
double vy = it->third->y - it->first->y;
double vz = it->third->z - it->first->z;
double _x = (uy*vz) - (uz*vy);
double _y = (uz*vx) - (ux*vz);
double _z = (ux*vy) - (uy*vx);
double magnitude = _x*_x + _y*_y +_z*_z;
if(magnitude == 0.0){
_x = 0.0;
_y = 0.0;
_z = 1.0;
}
else {
_x /= magnitude;
_y /= magnitude;
_z /= magnitude;
}
normal n(_x,_y,_z);
it->fnormal = n;
}
}
void InitializeData::formTrianglesTopAndBottom(const char* fileName)
{
ifstream myfile (fileName);
pair<int,double> voxelDetails;
vector<string> cube(8);
string data;
if (myfile.is_open()) {
int index = 0;
int corroded;
while (myfile.good()) {
getline (myfile, data);
if(index == 0 ) {
stringstream ss(data);
int voxelID;
string aux;
double weathingD;
corroded = 1;
ss>>voxelID>>aux>>corroded;
if (aux.length() > 0)
weathingD = convertDouble(aux.substr(1));
else
weathingD = 0.0;
voxelDetails = make_pair(voxelID,weathingD); // would be used in each of the 12 traingles
}
else if (index >= 1 && index <= 8 )
cube[index-1] = data;
else if (index == 9){
stringstream ss(data);
string a[6];
int b[6],c[6],d[6];
ss>>a[0]>>b[0]>>c[0]>>d[0]>>a[1]>>b[1]>>c[1]>>d[1]>>a[2]>>b[2]>>c[2]>>d[2]>>a[3]>>b[3]>>c[3]>>d[3]>>a[4]>>b[4]>>c[4]>>d[4]>>a[5]>>b[5]>>c[5]>>d[5];
bool visibleVoxel = false;
if(b[0] == -1 || b[1] == -1|| b[2] == -1 || b[3] == -1 || b[4] == -1 || b[5] == -1 || d[0] == 1 || d[1] == 1|| d[2] == 1 || d[3] == 1 || d[4] == 1 || d[5] == 1)
visibleVoxel = true;
//Only if the voxel is visible I would push the points in my unordered map
if (visibleVoxel) {
for ( int num = 0; num < (int)cube.size(); num++) {
string data = cube[num];
vector<double> points(3);
unordered_map<string,vertex>::const_iterator got = map.find (data);
if ( got == map.end() ) { // new vertex
stringstream ss(data);
ss>>points[0]>>points[1]>>points[2];
vertex m(points[0],points[1],points[2]);
map[data] = m;
}
}
// Put the vertices in anti clockwise direction so
// the normals point outward from the surface.
/*
4++++++++0
+ +
5++++++++1 +
+ + + +
+ 7++++++++3
6++++++++2
*/
//voxel is visible only is the top or bottom face is also visible
bool surface = false;
//top face
if((d[4] == 1 || d[5] == 1) && corroded == 0)
surface = true;
//right face -- 0
bool visible = false;
if(d[0] == 1 && corroded == 0 && surface)
visible = true;
triPush(cube[0], cube[1], cube[2],voxelDetails,visible);
triPush(cube[0], cube[2], cube[3],voxelDetails,visible);
//back face -- 1
visible = false;
if(d[1] == 1 && corroded == 0 && surface)
visible = true;
triPush(cube[4], cube[0], cube[7],voxelDetails,visible);
triPush(cube[0], cube[3], cube[7],voxelDetails,visible);
//left face -- 2
visible = false;
if(d[2] == 1 && corroded == 0 && surface)
visible = true;
triPush(cube[4], cube[7], cube[5],voxelDetails,visible);
triPush(cube[5], cube[7], cube[6],voxelDetails,visible);
//front face -- 3
visible = false;
if(d[3] == 1 && corroded == 0 && surface)
visible = true;
triPush(cube[1], cube[5], cube[6],voxelDetails,visible);
triPush(cube[1], cube[6], cube[2],voxelDetails,visible);
// top face -- 4
visible = false;
if(d[4] == 1 && corroded == 0 && surface)
visible = true;
triPush(cube[4], cube[5], cube[1],voxelDetails,visible);
triPush(cube[4], cube[1], cube[0],voxelDetails,visible);
//bottom face -- 5
visible = false;
if(d[5] == 1 && corroded == 0 && surface)
visible = true;
triPush(cube[2], cube[6], cube[7],voxelDetails,visible);
triPush(cube[2], cube[7], cube[3],voxelDetails,visible);
}
}
index = (index+1)%10;
}
}
}
void InitializeData::Calculate() {
Triangles.clear();
map.clear();
universalTid = 0;
if(sides)
formTriangles(VoxelFiles[currentFile].c_str());
else
formTrianglesTopAndBottom(VoxelFiles[currentFile].c_str());
cout<<"Traingles Formed"<<endl;
buildMap(IMAGEFILE, WEATHERMAPFILE);
cout<<"Weather Map read"<<endl;
impartColor();
cout<<"Color Imparted"<<endl;
calculateNormals();
cout<<"Normals Calculated"<<endl;
}
void InitializeData::changeColorModel() {
if (colorModel < MAXCOLORMODELS)
colorModel++;
else
colorModel = (colorModel+1)%MAXCOLORMODELS;
// We only need to change the color here
impartColor();
cout<<"Color Imparted"<<endl;
}
void InitializeData::changeSides() {
sides = !sides;
Calculate();
}
void InitializeData::changeFile(bool increase) {
// Increase
if (increase)
{
if(currentFile < (int)VoxelFiles.size()-1)
{
currentFile++;
Calculate();
}
}
else
{
if(currentFile > 0)
{
currentFile--;
Calculate();
}
}
}
void InitializeData::initializeData(vector<string> files)
{
// Initialize Data
colorModel = 1;
sides = true;
VoxelFiles = files;
currentFile = 0;
Calculate();
}
#ifdef __APPLE__
int main() {
InitializeData* node = new InitializeData();
node->initializeData();
return 0;
}
#endif