This repository has been archived by the owner on Oct 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvector_viz_main.cpp
2590 lines (2255 loc) · 67.5 KB
/
vector_viz_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
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <SDL.h>
#include <vector>
#include <cuda.h>
#include <cuda_runtime.h>
#include "renderer.h"
#include "SipYAML.hpp"
float PI=3.1415926535897932384f;
char inFileName[10000];
const float cBrightnessMultiplier = 45;
const float cLengthMultiplier = 0.1f;
const unsigned cMaxDotsPerLine = 30;
const float cBrightnessMin = 0.2f;
const float cBrightnessMax = 900.0f;
const float cBrightnessStep = 0.003f;
const float cLengthMin = 2.0f;
const float cLengthMax = 900.0f;
const float cLengthStep = 0.003f;
const int cudaMaxPoints = 24*1000*1000;
float gridSize = 1;
float rotLRInit = 0;
float rotUDInit = -20;
float distInit = 150;
float centerXInit = 0;
float centerYInit = 0;
float centerZInit = 0;
float brightnessInit = 100.0f;
float lengthInit = 100.0f;
float maxLength = 0;
float colorScale = 1.0f;
int initialDotDensity = -2;
int timeToSimulate = 0;
char *dumpFilename = NULL;
bool showParams = false;
bool offScreen = false;
bool useColor = false;
bool useSpeed = false;
bool useInPlane = false;
bool useOrtho = false;
bool showHeatmap = false;
bool showVectors = true;
int displayMode = 0;
const char* programName = "VectorViz v1.00";
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef min
#undef max
#endif
#if !defined(_DEBUG) && !defined(NO_FULLSCREEN)
//#define FULLSCREEN
#endif
#if defined(FULLSCREEN)
int screenW = 0;
int screenH = 0;
#else
int screenW = 1024;
int screenH = 768;
#endif
SDL_Window* display;
SDL_Surface* screen;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
Uint32 rmask = 0xff000000;
Uint32 gmask = 0x00ff0000;
Uint32 bmask = 0x0000ff00;
Uint32 amask = 0x000000ff;
#else
Uint32 rmask = 0x000000ff;
Uint32 gmask = 0x0000ff00;
Uint32 bmask = 0x00ff0000;
Uint32 amask = 0xff000000;
#endif
const uint8_t* GetGlyphBmp(int c);
int GetGlyphPitch();
int GetGlyphWidth();
int GetGlyphHeight();
unsigned kiss_z = 123456789; // 1 - billion
unsigned kiss_w = 378295763; // 1 - billion
unsigned kiss_jsr = 294827495; // 1 - RNG_MAX
unsigned kiss_jcong = 495749385; // 0 - RNG_MAX
unsigned RngUns()
{
//MWC x2
kiss_z=36969*(kiss_z&65535)+(kiss_z>>16);
kiss_w=18000*(kiss_w&65535)+(kiss_w>>16);
//SHR3
kiss_jsr^=(kiss_jsr<<13);
kiss_jsr^=(kiss_jsr>>17);
kiss_jsr^=(kiss_jsr<<5);
//linear congruential
kiss_jcong=69069*kiss_jcong+1234567;
return (((kiss_z<<16)+kiss_w)^kiss_jcong)+kiss_jsr;
}
const unsigned RNG_MAX = 4294967295; // 2^32 - 1
void UnrollRng()
{
for (int i=0; i<1000; i++)
RngUns();
}
// Random number form interval <0, 1>
double Rng01()
{
return (RngUns()+0.5) / (RNG_MAX+1.0);
}
class TIMER
{
//unsigned resetTime;
//time of resetting
unsigned resetTime;
public:
TIMER() {}
// void Reset();
// Resets timer to 0.
void Reset()
{ resetTime=(unsigned)SDL_GetTicks(); }
// unsigned E();
// Returns time in miliseconds that has
// passed from creation or from last Reset().
unsigned Get()
{ return (unsigned)SDL_GetTicks()-resetTime; }
void Subtract(int time)
{ resetTime+=time; }
};
inline Vec VecAdd(Vec a, Vec b) {
return Vec(a.x+b.x, a.y+b.y, a.z+b.z);
}
inline Vec VecSub(Vec a, Vec b) {
return Vec(a.x-b.x, a.y-b.y, a.z-b.z);
}
inline float DotProduct(Vec A, Vec B) {
return A.x*B.x + A.y*B.y + A.z*B.z;
}
inline float VecLen(Vec a) {
return sqrt(DotProduct(a,a));
}
inline Vec VecMul(Vec v,float t) {
v.x*=t;
v.y*=t;
v.z*=t;
return v;
}
inline Vec VecUnit(Vec a) {
return VecMul(a, 1.0f/VecLen(a));
}
inline Vec VecCross(Vec a, Vec b) {
return Vec(
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.z);
}
Vec RotUD(Vec v, float fi) //elevation
{
Vec r;
r.x = v.x;
r.z = cosf(fi)*v.z - sinf(fi) *v.y;
r.y = sinf(fi)*v.z + cosf(fi) *v.y;
return r;
}
Vec RotLR(Vec v, float fi) // azimuth
{
Vec r;
r.y = v.y;
r.x = cosf(fi)*v.x - sinf(fi) *v.z;
r.z = sinf(fi)*v.x + cosf(fi) *v.z;
return r;
}
Vec SphericalRotation(Vec v, float elevation, float azimuth)
{
return RotLR( RotUD(v,elevation), azimuth);
}
struct MovingPoint
{
Vec s; // (position) start of line/vector in the grid
float brightness;
Vec v; // vector from start to the end of the line
float offset; // in range 0-1, describes the current place of the moving point
};
std::vector<MovingPoint> createdMp;
std::vector<float> separatedMp;
std::vector<SMpBufDesc> mpBufDescCuda;
std::vector<SQuadBufDesc> quadBufDescCuda;
std::vector<float> quadData;
struct SCudaMemory
{
float* mpData;
SMpBufDesc* mpBufDesc;
unsigned* intensityRaster;
SQuadBufDesc *quadBufDesc;
float *quadData;
} cudaMem;
struct CameraArrangement
{
float dist;
float rotUD;
float rotLR;
Vec centerTranslation;
} cameraArrange;
Camera CreateCamera(CameraArrangement pk)
{
float azimuth = pk.rotLR/180*PI;
float elevation = pk.rotUD/180*PI;
Camera k;
k.dir = Vec(0,0,1);
k.xd = Vec(1,0,0);
k.yd = Vec(0,-1,0);
k.screenDist = screenW+screenW/2.0f;
k.dir = SphericalRotation( k.dir, elevation, azimuth) ;
k.xd = SphericalRotation( k.xd, elevation, azimuth );
k.yd = SphericalRotation( k.yd, elevation, azimuth );
k.upLeftCornerTrans = VecAdd( VecMul(k.xd,screenW/2.0f-0.5f), VecMul(k.yd,screenH/2.0f-0.5f) );
k.eye = SphericalRotation( Vec(0.0f,0.0f,-pk.dist), elevation, azimuth);
k.eye = VecAdd(k.eye, pk.centerTranslation);
return k;
}
struct IntensityBuffer
{
unsigned* pix;
int sizeX;
int sizeY;
} intRaster;
int GammaLinearToColorSRGB(double col)
{
if (col<=0.00304)
return int(col*12.92*255);
double up=1.055*pow(col,1/2.4)-0.055;
return int(up*255);
}
std::vector<int> gammaPre;
static const unsigned gammaPreSize=16000;
void SetupGammaPre(){
double step=1.0/gammaPreSize;
for (double i=0;i<gammaPreSize;i++) {
double midVal=i/gammaPreSize+step/2;
gammaPre.push_back( GammaLinearToColorSRGB(midVal) );
}
gammaPre.push_back( 255 );
}
struct SSys
{
bool isActive;
bool isQuit;
bool isMouseLeftPressed;
bool isMouseRightPressed;
bool isKeyLeftPressed;
bool isKeyRightPressed;
bool isKeyUpPressed;
bool isKeyDownPressed;
bool isKeyPgUpPressed;
bool isKeyPgDnPressed;
bool isKeyHomePressed;
bool isKeyEndPressed;
bool isKeyPadUp;
bool isKeyPadDown;
bool isKeyPadLeft;
bool isKeyPadRight;
};
struct SProgramState
{
SSys sys;
int pointCntExp;
float exposure;
float totalBrightness;
float length;
bool isAutoAdjust;
int nx;
int ny;
int nz;
bool isScreenshotRequest;
bool isPaused;
unsigned curTime;
TIMER programTime;
TIMER pressMouseRightTime;
TIMER pressMouseLeftTime;
TIMER pressKeyLeftTime;
TIMER pressKeyRightTime;
TIMER pressKeyUpTime;
TIMER pressKeyDownTime;
TIMER pressKeyPgUpTime;
TIMER pressKeyPgDnTime;
TIMER pressKeyHomeTime;
TIMER pressKeyEndTime;
TIMER pressKeyPadDownTime;
TIMER pressKeyPadUpTime;
TIMER pressKeyPadLeftTime;
TIMER pressKeyPadRightTime;
}ps;
struct VectorData
{
uint64_t indices;
unsigned prevMsBetweenPoints;
unsigned lastMpTime;
float dotSpread;
float dotBrightness;
Vec v;
//float len;
};
std::vector<VectorData> gridVector;
VectorData CreateGridVector(Vec inVec, uint64_t indices, float maxDotSpread)
{
VectorData gridVec;
gridVec.v = inVec;
float length = VecLen(inVec);
float dotSpread = 1/length;
if (dotSpread>maxDotSpread) dotSpread=maxDotSpread;
gridVec.dotSpread = dotSpread;
//gridVec.len = length;
gridVec.dotBrightness = length*length;
gridVec.prevMsBetweenPoints = 300;
gridVec.lastMpTime = 0-unsigned(Rng01()*300);
gridVec.indices = indices;
return gridVec;
}
struct Bytes4
{
uint8_t byte[4];
void Set(unsigned val){
//little endian
byte[0] = val&255;
byte[1] = (val>>8)&255;
byte[2] = (val>>16)&255;
byte[3] = (val>>24)&255;
}
Bytes4(unsigned val){ Set(val);}
Bytes4(int val) { Set(int(val)); }
Bytes4(float val){
unsigned *uval=(unsigned*) &val;
Set(*uval);
}
};
void WriteBytes4(FILE* file, Bytes4 data)
{
fwrite(data.byte,4,1,file);
}
bool StrBegEqu(const char* beg, char*str)
{
while(*beg!=0) {
if (*beg!=*str) return false;
beg++;
str++;
}
return true;
}
void ReadLineBeg(FILE* file, char* str, int maxLen)
{
int i=0;
while(!feof(file)) {
int chr = fgetc(file);
if (chr=='\n') break;
if (i<maxLen-1)
str[i++] = char(chr);
}
str[i]='\n';
}
void PermutateGridVectors()
{
for(int i=0; i<gridVector.size(); i++) {
VectorData temp = gridVector[i];
int rndI = unsigned(Rng01()*(gridVector.size()-i))+i;
gridVector[i] = gridVector[rndI];
gridVector[rndI] = temp;
}
}
float ReadFloat(FILE* file)
{
float f;
int ignore = fread(&f,1,4,file);
return f;
}
bool ReadInputFile(const char* inFileName)
{
int nx;
int ny;
FILE* inFile=fopen(inFileName,"rt");
if (inFile==NULL) return false;
const int textLineLength=200;
char str[textLineLength];
ReadLineBeg(inFile,str,textLineLength);
if (!StrBegEqu("VV TEXT FILE", str) && !StrBegEqu("VV FLOAT FILE", str)) {
fprintf(stderr,"Not a VVT of VVF file\n");
return false;
}
bool isVvfFile = StrBegEqu("VV FLOAT FILE", str);
fclose(inFile);
inFile=fopen(inFileName, isVvfFile? "rb" : "rt");
if (inFile==NULL) return false;
ReadLineBeg(inFile,str,textLineLength); //skip line
ReadLineBeg(inFile,str,textLineLength); //skip line
ReadLineBeg(inFile,str,textLineLength); //skip line
int ignore = fscanf(inFile,"%d%d",&nx, &ny);
if (nx*ny>6000*6000) {
printf("Input file grid dimensions too big\n");
return false;
}
ReadLineBeg(inFile,str,textLineLength); //skip rest of the line
ps.nx=1;
ps.ny=ny;
ps.nz=nx;
gridVector.resize(nx*ny);
if (showHeatmap) quadData.resize(nx * ny);
for(int yi=0; yi<ny; yi++)
for(int xi=0; xi<nx; xi++){
if (feof(inFile)){
printf("Unexpected end-of-file in input file\n");
return false;
}
int i = yi*nx + xi;
float u;
float x,y,z;
if (isVvfFile){
x = ReadFloat(inFile);
y = ReadFloat(inFile);
z = ReadFloat(inFile);
}
else
int ignore = fscanf(inFile,"%f%f%f%f",&u,&x,&y,&z);
gridVector[i].v = Vec( x, y, z);
gridVector[i].indices= (xi) + ((uint64_t)yi<<21);
auto length = VecLen(gridVector[i].v);
maxLength = std::max(maxLength, length);
if (showHeatmap) quadData[i] = length;
}
if (showHeatmap)
{
quadBufDescCuda.resize(1);
auto &desc = quadBufDescCuda.front();
desc.gridWidth = nx;
desc.gridHeight = ny;
desc.vertices[0] = Vec(0.f, nx * 0.5f, ny * -0.5f);
desc.vertices[1] = Vec(0.f, nx * -0.5f, ny * -0.5f);
desc.vertices[2] = Vec(0.f, nx * 0.5f, ny * 0.5f);
desc.vertices[3] = Vec(0.f, nx * -0.5f, ny * 0.5f);
desc.dataOffset = 0;
}
fclose(inFile);
return true;
}
bool ReadInputFile_FULL(const char* inFileName)
{
int nx;
int ny;
int nz;
FILE* inFile=fopen(inFileName,"rt");
if (inFile==NULL) return false;
const int textLineLength=200;
char str[textLineLength];
ReadLineBeg(inFile,str,textLineLength);
if (!StrBegEqu("VV TEXT FILE", str) && !StrBegEqu("VV FLOAT FILE", str)) {
fprintf(stderr,"Not a VVT of VVF file\n");
return false;
}
bool isVvfFile = StrBegEqu("VV FLOAT FILE", str);
fclose(inFile);
inFile=fopen(inFileName, isVvfFile? "rb" : "rt");
if (inFile==NULL) return false;
ReadLineBeg(inFile,str,textLineLength); //skip line
ReadLineBeg(inFile,str,textLineLength); //skip line
ReadLineBeg(inFile,str,textLineLength); //skip line
int ignore = fscanf(inFile,"%d%d%d",&nx, &ny, &nz);
// if (nx<60 || ny<60 || nz<60) {
// printf("Input file grid dimensions too small\n");
// return false;
// }
if (nx*ny>6000*6000) {
printf("Input file grid dimensions too big\n");
return false;
}
ReadLineBeg(inFile,str,textLineLength); //skip rest of the line
gridVector.resize(nx * ny * nz);
if (showHeatmap) quadData.resize(nx * ny * nz);
for (int zi=0; zi<nz; zi++)
for (int yi=0; yi<ny; yi++)
for (int xi=0; xi<nx; xi++)
{
if (feof(inFile)){
printf("Unexpected end-of-file in input file\n");
return false;
}
int pos = (zi * ny * nx) + (yi * nx) + (xi);
float u;
float x, y, z;
x = ReadFloat(inFile);
y = ReadFloat(inFile);
z = ReadFloat(inFile);
gridVector[pos].v = Vec( x, y, z);
auto length = VecLen(gridVector[pos].v);
maxLength = std::max(maxLength, length);
if (showHeatmap) quadData[pos] = length;
//gridVector[pos].indices= (yi<<16)+xi;
}
fclose(inFile);
return true;
}
void SaveVvfFile(char* filename, std::vector<VectorData> const &grid, int nx, int ny, int nz)
{
FILE* outFile=fopen(filename,"wb");
if (outFile==NULL) {
fprintf(stderr,"Error: Cannot open %s for writing\n", filename);
return;
}
fprintf(outFile,"VV FLOAT FILE\n");
fprintf(outFile,"\n");
fprintf(outFile,"\n");
if (nz == 1)
{
fprintf(outFile,"%d %d\n", nx, ny);
}
else
{
fprintf(outFile,"%d %d %d\n", nx, ny, nz);
}
for(int i=0; i<nx*ny*nz; i++){
Vec v = grid[i].v;
Vec outVec = Vec(-v.y, v.x, v.z);
WriteBytes4(outFile,Bytes4(outVec.x));
WriteBytes4(outFile,Bytes4(outVec.y));
WriteBytes4(outFile,Bytes4(outVec.z));
}
fclose(outFile);
}
bool merge_native_slice(const char *dirname, char const *nameRoot, int ngx, int ngy, int ngz, int snx, int sny, int snz, char *outFilename){
std::string str (dirname);
std::string str2 ("cut");
std::size_t found = str.find(str2);
std::string str_cuti = str.substr (found + 4);
int cuti = std::stoi(str_cuti);
int nx = snx / ngx;
int ny = sny / ngy;
int startidi = cuti / nx;
int endidi = startidi;
int startidj = 0;
int endidj = ngy - 1;
int startidk = 0;
int endidk = ngz - 1;
std::vector<VectorData> mergedGridVector;
std::vector<float> mergedQuadData;
mergedGridVector.resize(snx * sny);
if (showHeatmap) mergedQuadData.resize(snx * sny);
for (int idi = startidi; idi <= endidi; idi++)
{
for (int idj = startidj; idj <= endidj; idj++)
{
for (int idk = startidk; idk <= endidk; idk++)
{
char filename[300];
sprintf(filename, "%s/%s.%i.%i.%i.vvf", dirname, nameRoot, idi, idj, idk);
printf("Opening %s\n", filename);
if (!ReadInputFile(filename)) return false;
for(int yi=0; yi<ny; yi++)
{
for(int xi=0; xi<nx; xi++)
{
int i = yi*nx + xi;
int merged_yi = idj*ny + yi;
int merged_xi = idk*ny + xi;
int merged_i = ((merged_yi)*snx) + (merged_xi);
mergedGridVector[merged_i].v = gridVector[i].v;
mergedGridVector[merged_i].indices = (merged_yi) + ((uint64_t)merged_xi<<21);
if (showHeatmap) mergedQuadData[merged_i] = quadData[i];
}
}
}
}
}
ps.nx = 1;
ps.ny = sny;
ps.nz = snx;
gridVector = mergedGridVector;
if (showHeatmap)
{
quadData = mergedQuadData;
quadBufDescCuda.resize(1);
auto &desc = quadBufDescCuda.front();
desc.gridWidth = snx;
desc.gridHeight = sny;
desc.vertices[0] = Vec(0.f, snx * -0.5f, sny * 0.5f);
desc.vertices[1] = Vec(0.f, snx * -0.5f, sny * -0.5f);
desc.vertices[2] = Vec(0.f, snx * 0.5f, sny * 0.5f);
desc.vertices[3] = Vec(0.f, snx * 0.5f, sny * -0.5f);
desc.dataOffset = 0;
}
if (outFilename)
{
SaveVvfFile(outFilename, gridVector, snx, sny, 1);
}
return true;
}
bool merge_native_axis(const char *dirname, char const *nameRoot, int ngx, int ngy, int ngz, int snx, int sny, int snz, char *outFilename)
{
std::string str (dirname);
std::string str2 ("cut");
std::size_t found = str.find(str2);
std::string str_cutj = str.substr (found + 4);
int cutj = std::stoi(str_cutj);
int nx = snx / ngx;
int ny = sny / ngy;
int startidj = cutj / ny;
int endidj = startidj;
int startidi = 0;
int endidi = ngx - 1;
int startidk = 0;
int endidk = ngz - 1;
std::vector<VectorData> mergedGridVector;
std::vector<float> mergedQuadData;
mergedGridVector.resize(snx * snz);
if (showHeatmap) mergedQuadData.resize(snx * sny);
for (int idi = startidi; idi <= endidi; idi++)
{
for (int idj = startidj; idj <= endidj; idj++)
{
for (int idk = startidk; idk <= endidk; idk++)
{
char filename[300];
sprintf(filename, "%s/%s.%i.%i.%i.vvf", dirname, nameRoot, idi, idj, idk);
printf("Opening %s\n", filename);
if (!ReadInputFile(filename)) return false;
for(int yi=0; yi<ny; yi++)
{
for(int xi=0; xi<nx; xi++)
{
int i = yi*nx + xi;
int merged_yi = idi*ny + yi;
int merged_xi = idk*ny + xi;
int merged_i = ((merged_yi)*snx) + (merged_xi);
mergedGridVector[merged_i].v = gridVector[i].v;
mergedGridVector[merged_i].indices= ((uint64_t)merged_yi<<42) + ((uint64_t)merged_xi<<21);
if (showHeatmap) mergedQuadData[merged_i] = quadData[i];
}
}
}
}
}
ps.nx = snx;
ps.ny = snz;
ps.nz = 1;
gridVector = mergedGridVector;
if (showHeatmap)
{
quadData = mergedQuadData;
quadBufDescCuda.resize(1);
auto &desc = quadBufDescCuda.front();
desc.gridWidth = snx;
desc.gridHeight = sny;
desc.vertices[0] = Vec(sny * 0.5f, snx * -0.5f, 0.f);
desc.vertices[1] = Vec(sny * -0.5f, snx * -0.5f, 0.f);
desc.vertices[2] = Vec(sny * 0.5f, snx * 0.5f, 0.f);
desc.vertices[3] = Vec(sny * -0.5f, snx * 0.5f, 0.f);
desc.dataOffset = 0;
}
if (outFilename)
{
SaveVvfFile(outFilename, gridVector, snx, sny, 1);
}
return true;
}
bool merge_native_full(const char *dirname, char const *nameRoot, int ngx, int ngy, int ngz, int snx, int sny, int snz)
{
int nx = snx/ngx;
int ny = sny/ngy;
int nz = snz/ngz;
std::vector<VectorData> mergedGridVector;
mergedGridVector.resize(snx * sny * snz);
for (int idi = 0; idi < ngx; idi++)
{
for (int idj = 0; idj < ngy; idj++)
{
for (int idk = 0; idk < ngz; idk++)
{
char filename[300];
sprintf(filename, "%s/%s.%i.%i.%i.vvf", dirname, nameRoot, idi, idj, idk);
printf("Opening %s", filename);
if (!ReadInputFile_FULL(filename)) return false;
printf(", Done.\n");
//THIS COUNTS THROUGH THE NODE. THIS IS NOT THE SAME AS SLICE OR AXIS
for (int node_i = 0; node_i < nx; node_i++)
{
for (int node_j = 0; node_j < ny; node_j++)
{
for (int node_k = 0; node_k < nz; node_k++)
{
int imgu = idi * nx + node_i;
int imgv = idj * ny + node_j;
int imgw = idk * nz + node_k;
int merged_pos = ((imgw * sny) + imgv) * snx + imgu;
int node_pos = (node_i * ny * nx) + (node_j * nx) + (node_k);
mergedGridVector[merged_pos].v = gridVector[node_pos].v;
mergedGridVector[merged_pos].indices = ((uint64_t)imgu << 42) + ((uint64_t)imgv << 21) + imgw;
}
}
}
}
}
}
ps.nx = snx;
ps.ny = sny;
ps.nz = snz;
gridVector = mergedGridVector;
return true;
}
void RasterLine(std::vector<int2> &points, int x0, int y0, int x1, int y1)
{
int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
int dy = -abs(y1-y0), sy = y0<y1 ? 1 : -1;
int err = dx+dy, e2;
for(;;)
{
int2 point = { x0, y0 };
points.push_back(point);
if (x0==x1 && y0==y1) break;
e2 = 2*err;
if (e2 > dy) { err += dy; x0 += sx; }
if (e2 < dx) { err += dx; y0 += sy; }
}
}
bool ReadMergeInput(char const *dir, char const *nameRoot, int ngx, int ngy, int ngz, int snx, int sny, int snz, int plane, std::vector<int> &position, int axis, std::vector<int> &phi, char *outFilename, char *outNameRoot)
{
if (strstr(dir, "slice"))
{
return merge_native_slice(dir, nameRoot, ngx, ngy, ngz, snx, sny, snz, outFilename);
}
if (strstr(dir, "axis"))
{
return merge_native_axis(dir, nameRoot, ngx, ngy, ngz, snx, sny, snz, outFilename);
}
if (strstr(dir, "full"))
{
struct Slice
{
std::vector<int2> points;
int h;
int axis;
Vec normal;
Vec xAxis;
bool isAngle;
int param;
int begin;
int size;
};
std::vector<Slice> slices;
slices.reserve(phi.size() + position.size());
int bx;
int by;
int h;
switch (axis)
{
case 0: bx = sny; by = snz; h = snx; break;
case 1: bx = snx; by = snz; h = sny; break;
case 2: bx = snx; by = sny; h = snz; break;
default:
fprintf(stdout, "Please specify output/angle/axis using x, y or z\n");
return false;
}
for (auto angle: phi)
{
slices.emplace_back();
auto &slice = slices.back();
auto cx = (bx - 1) * 0.5f;
auto cy = (by - 1) * 0.5f;
auto rad = angle / 180.f * PI;
auto s = sin(rad);
auto c = cos(rad);
auto normal = Vec((axis == 2) ? 1 : 0, (axis == 0) ? 1 : 0, (axis == 1) ? 1 : 0);
slice.normal = normal;
auto s2 = s;
auto c2 = c;
if (axis == 0)
{
slice.normal.y = c;
slice.normal.z = -s;
}
else if (axis == 1)
{
slice.normal.x = s;
slice.normal.z = -c;
}
else if (axis == 2)
{
slice.normal.x = c;
slice.normal.y = -s;
}
if (fabs(s) / cy > fabs(c) / cx)
{
c = c / s * cy;
s = cy;
c2 = c2 / s2 * by;
s2 = by;
}
else
{
s = s / c * cx;
c = cx;
s2 = s2 / c2 * bx;
c2 = bx;
}
slice.xAxis = Vec(0, 0, 0);
if (axis == 0)
{
slice.xAxis.y = s2;
slice.xAxis.z = c2;
RasterLine(slice.points, cx - s, cy - c, cx + s, cy + c);
}
else if (axis == 1)
{
slice.xAxis.x = c2;
slice.xAxis.z = s2;
RasterLine(slice.points, cx - c, cy - s, cx + c, cy + s);
}
else if (axis == 2)
{
slice.xAxis.x = s2;
slice.xAxis.y = c2;
RasterLine(slice.points, cx - s, cy - c, cx + s, cy + c);
}
slice.h = h;
slice.axis = axis;
slice.isAngle = true;
slice.param = angle;
}
if (plane < 0 || plane > 2)
{
fprintf(stdout, "Please specify output/ortho/plane using x, y or z\n");
return false;
}
for (auto pos: position)
{
slices.emplace_back();
auto &slice = slices.back();
int2 beg;
int2 end;
switch (plane)
{
case 0: beg = make_int2(pos, 0); end = make_int2(pos + 1, sny); h = snz; break;
case 1: beg = make_int2(pos, 0); end = make_int2(pos + 1, snz); h = snx; break;
case 2: beg = make_int2( 0, pos); end = make_int2( snx, pos + 1); h = sny; break;
}
for (int2 point = beg; point.x < end.x; ++point.x)