25 #define FilePerLine 100
27 void phi2color (vector<uint8_t>::iterator px,
cv1d & phi,
int d, vector<vector<float>> & colors) ;
44 const vector<vector<float>> allcolorslist = {
52 const vector<vector<vector<float>>> allcolors = {
53 {{231./256., 37./256., 100./256.}},
59 string BasePath = TEXTURINGPATH
"/../Samples/" ;
62 string BasePath =
"../Samples/" ;
68 vector <Timestep>
Ts ;
82 const bool singlefiles = true ;
85 int initialise (map <string,string> args) ;
87 int set_grid (
int nb) ;
88 void spaceloop (
v1d View,
uint tsint,
int nrotate,
int dim) ;
89 void timeloop (
v1d View,
uint tsint,
int nrotate) ;
90 void hereandnow(
v1d View,
uint tsint,
int nrotate) ;
92 int SetNewViewPoint (map <string,string> args) ;
94 void Render (vector <string> & filerendered,
cv1d & View,
int nrotate,
int time,
cv2d &X,
cv1d & R,
cv2d &A) ;
95 int viewpermute (
v1d & View) ;
100 int write_vtkmap (map <string,string> args) ;
101 int write_colormap_vtk_base () ;
102 int write_colormap_nrrd_base (map <string,string> args) ;
116 inline void dispvector (
const v1d &
a) {
for (
auto v:
a) printf(
"%g ", v); printf(
"\n") ; fflush(stdout) ; }
117 inline void dispvector (
const v1f &
a) {
for (
auto v:
a) printf(
"%g ", v); printf(
"\n") ; fflush(stdout) ; }
118 inline void dispvector (
const vector<int> &
a) {
for (
auto v:
a) printf(
"%d ", v); printf(
"\n") ; fflush(stdout) ; }
122 {T->
timeloop(View, tsint,nrotate) ; }
126 {T->
spaceloop(View, tsint,nrotate, dim) ; }
134 string Directory=args[
"path"] ;
136 set_grid (atoi(args[
"resolution"].c_str())) ;
139 std::filesystem::directory_iterator D(BasePath + Directory) ;
140 vector <string> tmpfilelst ;
141 vector <std::pair<int,string>> filelistloc, filelistA ;
142 for (
auto& p : std::filesystem::directory_iterator(BasePath+Directory)) tmpfilelst.push_back(p.path().string()) ;
143 regex exprloc{
".*dump-([0-9]+).csv"};
144 regex exprA{
".*dumpA-([0-9]+).csv"};
146 for (
auto &
a : tmpfilelst)
148 if (regex_match(
a, what, exprloc)) filelistloc.push_back(make_pair(stoi(what[1].str()),
a)) ;
149 if (regex_match(
a, what, exprA)) filelistA.push_back(make_pair(stoi(what[1].str()),
a)) ;
151 std::sort(filelistloc.begin(), filelistloc.end()) ;
152 std::sort(filelistA.begin() , filelistA.end() ) ;
153 TsName.resize(filelistloc.size(), 0) ;
154 for (
uint i=0 ; i<filelistloc.size() ; i++) TsName[i]=filelistloc[i].first ;
157 Ts.resize(filelistloc.size()) ;
158 for (
uint i=0 ; i<filelistloc.size() ; i++)
161 csvread_XR (filelistloc[i].second.c_str(), Ts[i].X, R,
d) ;
162 printf(
"%d ", i) ; fflush(stdout) ;
164 for (
uint i=0 ; i<filelistA.size() ; i++)
csvread_A(filelistA[i].second.c_str(), Ts[i].A,
d) ;
169 for (
int i=0 ; i<
N ; i++)
171 rotate(v.X[i].begin(), v.X[i].begin()+nrotate, v.X[i].end()) ;
181 for (
uint i=0 ; i<
d-3 ; i++)
187 auto tmpbound = max_element(R.begin(), R.end()) ;
191 if (
static_cast<uint>(
d-1)>allcolorslist.size()) printf(
"ERR: not enough color gradients!!\n") ;
194 colors=allcolorslist ;
197 printf(
"[[[%ld]]]", View.size()) ; fflush(stdout) ;
198 RenderedAlready.resize(2*(
d-3+1), 0) ;
199 ViewPoint.resize(
d-3+1, INT_MIN) ;
200 NewViewPoint.resize(
d-3+1, 0) ;
201 FileList.resize(
d-3+1) ;
202 Threads.resize(
d-3+1) ;
204 singlerendered=false ;
221 NewViewPoint.clear() ;
223 for (
auto & Thr : Threads)
226 auto ThrID = Thr.native_handle() ;
227 pthread_cancel(ThrID);
233 vector<Timestep>().swap(Ts) ;
236 NewViewPoint.clear() ;
238 RenderedAlready.clear() ;
239 for (
auto & u : FileList)
241 std::filesystem::remove(v.c_str()) ;
249 printf(
"|%s|", args[
"ts"].c_str()) ; fflush(stdout) ;
250 int Time=atoi(args[
"ts"].c_str()) ;
251 printf(
"H") ; fflush(stdout) ;
253 printf(
"[]%ld[]", View.size() ) ; fflush(stdout) ;
254 View[0]=View[1]=View[2]=NAN ;
255 printf(
"E") ; fflush(stdout) ;
256 for (
uint dd=3 ; dd<
d ; dd++)
258 sprintf(dimstr,
"x%d", dd+1) ;
259 View[dd]=atof(args[dimstr].c_str()) ;
261 printf(
"F") ; fflush(stdout) ;
262 nrotate = viewpermute (View) ;
263 if (nrotate != 3 &&
d>3) printf(
"WHAT? No the first 3D should be NaNs ...") ;
264 for (
uint i=0 ; i<
d-3 ; i++) {NewViewPoint[i] =
static_cast<int>(round(View[i]/
DeltaX));}
265 printf(
"B") ; fflush(stdout) ;
266 auto tmp = find(TsName.begin(), TsName.end(), Time) ;
267 if (tmp == TsName.end()) {printf(
"WARN: not found timestep\n") ;
return -1 ; }
268 NewViewPoint[
d-3] = tmp-TsName.begin() ;
269 printf(
"C") ; fflush(stdout) ;
270 if (atoi(args[
"running"].c_str())==1) runfast=true ;
272 printf(
"D") ; fflush(stdout) ;
279 if (singlerendered) {singlerendered=false ;
return true ; }
281 for (
uint i=0 ; i<
d-3 ; i++)
282 if (NewViewPoint[i]<RenderedAlready[i*2] || NewViewPoint[i]>RenderedAlready[i*2+1])
286 if (NewViewPoint[
d-3]<RenderedAlready[(
d-3)*2]) t = NewViewPoint[
d-3] +Ts.size() ;
287 else t= NewViewPoint[
d-3] ;
289 if (t>=RenderedAlready[(
d-3)*2]+RenderedAlready[(
d-3)*2+1])
return false ;
302 if (! isrendered() && runfast) {
dispvector(RenderedAlready) ; hereandnow(View, NewViewPoint[
d-3], nrotate) ; singlerendered=true ; }
305 for (
uint i=0 ; i<
d-3 ; i++)
307 for (
uint j=0 ; j<
d-3+1 ; j++)
310 if (NewViewPoint[j] != ViewPoint[j])
312 if (Threads[i].joinable())
314 auto ThreadID = Threads[i].native_handle() ;
315 pthread_cancel(ThreadID);
319 Threads[i] = std::thread(runthread_spaceloop<d>,
this, View, NewViewPoint[
d-3], nrotate, i) ;
324 for (
uint j=0 ; j<
d-3 ; j++)
326 if (NewViewPoint[j] != ViewPoint[j])
328 if (Threads[
d-3].joinable())
330 auto ThreadID = Threads[
d-3].native_handle() ;
331 pthread_cancel(ThreadID) ;
332 Threads[
d-3].join() ;
335 Threads[
d-3] = std::thread(runthread_timeloop<d>,
this, View, NewViewPoint[
d-3], nrotate) ;
339 if (
d==3 && justloaded)
341 Threads[
d-3] = std::thread(runthread_timeloop<d>,
this, View, NewViewPoint[
d-3], nrotate) ;
345 ViewPoint=NewViewPoint ;
354 for (
auto & v : FileList[dim]) std::filesystem::remove(v.c_str()) ;
355 FileList[dim].clear() ;
357 auto Viewdec = View ;
365 if (Viewdec[dim]>
Boundaries[0][dim]) Render(FileList[dim], Viewdec, nrotate, TsName[tsint], Ts[tsint].X, R, Ts[tsint].A) ;
366 RenderedAlready[2*dim] = Viewdec[dim] /
DeltaX ;
367 if (View[dim]<
Boundaries[1][dim]) Render(FileList[dim], View, nrotate, TsName[tsint], Ts[tsint].X, R, Ts[tsint].A) ;
368 RenderedAlready[2*dim+1] = View[dim] /
DeltaX ;
370 RenderedAlready[2*dim] = INT_MIN ;
371 RenderedAlready[2*dim+1] = INT_MAX ;
372 printf(
"Spaceloop is done") ; fflush(stdout) ;
378 int dim=FileList.size()-1 ;
379 for (
auto & v : FileList[dim]) std::filesystem::remove(v.c_str()) ;
381 FileList[dim].clear() ;
382 uint timeidxinit=tsint ;
384 RenderedAlready[2*(
d-3)] = tsint ;
385 RenderedAlready[2*(
d-3)+1] = 0;
387 Render(FileList[dim],View, nrotate, TsName[tsint], Ts[tsint].X, R, Ts[tsint].A) ;
388 printf(
"%d ", tsint) ; fflush(stdout) ;
390 printf(
"%d ", tsint) ; fflush(stdout) ;
391 RenderedAlready[2*(
d-3)+1] ++ ;
392 if (tsint>=Ts.size()) tsint=0 ;
393 }
while (tsint != timeidxinit) ;
394 printf(
"Timeloop is done") ; fflush(stdout) ;
400 int dim=FileList.size()-1 ;
401 for (
auto & v : FileList[dim]) std::filesystem::remove(v.c_str()) ;
403 FileList[dim].clear() ;
405 Render(FileList[dim],View, nrotate, TsName[tsint], Ts[tsint].X, R, Ts[tsint].A) ;
412 v1d phi (
d-1,0), phinew(
d-1,0) ;
413 vector<uint8_t> img ;
int stride ;
417 img.resize(Nlambda*Ntheta*3,0) ;
426 for (
int i=0 ; i<
N ; i++)
429 double rsqr = R[i]*R[i] ;
431 for (
uint j=0 ; j<
d-3 ; j++)
432 rsqr -= (View[j]-X[i][j])*(View[j]-X[i][j]) ;
442 for (
uint j=0 ; j<
d-3 ; j++)
444 double cosine = (View[j]-X[i][j])/R[i] ;
445 for (
uint k=0 ; k<j ; k++)
446 cosine /= sin(phi[k]) ;
447 phi[j] = acos(cosine) ;
451 for (
int j = 0 ; j<Nlambda ; j++)
452 for (
int k=0 ; k<Ntheta ; k++)
458 for (
uint dd=0 ; dd<
d-3 ; dd++) {sp[dd]=View[dd]-X[i][dd] ;}
461 for (
uint j=0 ; j<
d-3 ; j++) sp[
d-3] *= sin(phi[j]) ;
462 sp[
d-2]=sp[
d-3] ; sp[
d-1]=sp[
d-3] ;
472 sp[
d-3] *= sin(phi[
d-3])*cos(phi[
d-2]-M_PI/2.) ;
473 sp[
d-2] *= sin(phi[
d-3])*sin(phi[
d-2]-M_PI/2.) ;
474 sp[
d-1] *= cos(phi[
d-3]) ;
479 sp[
d-3] *= cos(phi[
d-3]) ;
480 sp[
d-2] *= sin(phi[
d-3])*cos(-phi[
d-2]) ;
481 sp[
d-1] *= sin(phi[
d-3])*sin(-phi[
d-2]) ;
488 rotate(sp.begin(), sp.begin()+((
d-nrotate)%
d), sp.end()) ;
491 rotate(spturned.begin(), spturned.begin()+nrotate, spturned.end()) ;
502 phi2color (img.begin() + imgx0 * 3 + k*3 + j*stride*3, phinew,
d, colors) ;
509 write_img(path, Ntheta, Nlambda, img.data()) ;
510 filerendered.push_back(path);
524 filerendered.push_back(path);
534 Nlambda=Ntheta=(1<<nb);
539 colors=allcolors[1] ;
547 for (
int i=0 ; i<Nlambda ; i++)
lambdagrid[i]= M_PI/(2.*Nlambda)+ M_PI/Nlambda*i ;
548 for (
int i=0 ; i<Ntheta-1 ; i++)
thetagrid[i] =2*M_PI/(2.*(Ntheta-1) )+2*M_PI/(Ntheta-1) *i ;
559 while (isnan(View[0]))
561 rotate(View.begin(), View.begin()+1 , View.end()) ;
566 auto b = find_if(View.begin(), View.end(), [](
double dd) { return std::isnan(dd); } ) ;
567 rotate(View.begin(), b+3, View.end()) ;
568 nrotate = b-View.begin()+3 ;
570 nrotate %= View.size() ;
580 sprintf (path,
"%s/Texture-%d-%05d",
DirectorySave.c_str(), n, time) ;
581 for (
uint i=0 ; i<
d-3 ; i++)
584 sprintf(path,
"%s-%.1f", tmp, View[i]) ;
586 strcat(path,
".png") ;
592 sprintf (path,
"%s/TextureTile-%05d",
DirectorySave.c_str(), time) ;
593 for (
uint i=0 ; i<
d-3 ; i++)
596 sprintf(path,
"%s-%.1f", tmp, View[i]) ;
598 strcat(path,
".png") ;
615 v1d View ;
int nrotate=3 ;
616 View.push_back(atof(args[
"x4"].c_str())) ;
617 int time = atoi(args[
"ts"].c_str()) ;
618 int idx = atoi(args[
"N"].c_str()) ;
621 v1d phi (
d-1,0), phinew(
d-1,0) ;
622 vector<uint8_t> img ;
624 img.resize(3*Nlambda*(Ntheta-1),0) ;
628 sprintf(path,
"Colormap-Surface-%d-%d.vtk", idx, time) ;
629 vtkout=fopen(path,
"w") ;
630 fprintf(vtkout,
"# vtk DataFile Version 2.0\nTexture surface for ND DEM\nASCII\nDATASET UNSTRUCTURED_GRID\nPOINTS %d float\n", Nlambda*(Ntheta-1)) ;
633 double rsqr = R[idx]*R[idx] ;
634 for (
uint j=0 ; j<
d-3 ; j++)
635 rsqr -= (View[j]-Ts[time].X[idx][j])*(View[j]-Ts[time].X[idx][j]) ;
640 for (
uint j=0 ; j<
d-3 ; j++)
642 double cosine = (View[j]-Ts[time].X[idx][j])/R[idx] ;
643 for (
uint k=0 ; k<j ; k++)
644 cosine /= sin(phi[k]) ;
645 phi[j] = acos(cosine) ;
648 for (
int j = 0 ; j<Nlambda ; j++)
649 for (
int k=0 ; k<Ntheta-1 ; k++)
656 for (
uint dd=0 ; dd<
d-3 ; dd++) {sp[dd]=View[dd]-Ts[time].X[idx][dd] ;}
659 for (
uint j=0 ; j<
d-3 ; j++) sp[
d-3] *= sin(phi[j]) ;
660 sp[
d-2]=sp[
d-3] ; sp[
d-1]=sp[
d-3] ;
661 sp[
d-3] *= cos(phi[
d-3]) ;
662 sp[
d-2] *= sin(phi[
d-3])*cos(phi[
d-2]) ;
663 sp[
d-1] *= sin(phi[
d-3])*sin(phi[
d-2]) ;
668 rotate(sp.begin(), sp.begin()+((
d-nrotate)%
d), sp.end()) ;
671 rotate(spturned.begin(), spturned.begin()+nrotate, spturned.end()) ;
674 fprintf(vtkout,
"%g %g %g\n", phinew[0], phinew[1], phinew[2]) ;
676 phi2color (img.begin()+j*(Ntheta-1)*3+k*3, phinew,
d, colors) ;
679 fprintf(vtkout,
"\nCELLS %d %d\n", (Nlambda-1)*(Ntheta-2), (Nlambda-1)*(Ntheta-2)*5 ) ;
680 for (
int i=0 ; i<Nlambda-1 ; i++)
681 for (
int j=0 ; j<Ntheta-2 ; j++)
682 fprintf(vtkout,
"4 %d %d %d %d\n", i*(Ntheta-1)+j, i*(Ntheta-1)+j+1, (i+1)*(Ntheta-1)+j+1, (i+1)*(Ntheta-1)+j) ;
684 fprintf(vtkout,
"\nCELL_TYPES %d\n", (Nlambda-1)*(Ntheta-2)) ;
685 for (
int i=0 ; i<(Nlambda-1)*(Ntheta-2) ; i++) fprintf(vtkout,
"9 ") ;
686 fprintf(vtkout,
"\n") ;
688 fprintf(vtkout,
"\nPOINT_DATA %d\nCOLOR_SCALARS Color 3\n", Nlambda*(Ntheta-1)) ;
689 for (
int j = 0 ; j<Nlambda ; j++)
690 for (
int k=0 ; k<Ntheta-1 ; k++)
691 fprintf(vtkout,
"%g %g %g\n", img[j*(Ntheta-1)*3+k*3]/256., img[j*(Ntheta-1)*3+k*3+1]/256., img[j*(Ntheta-1)*3+k*3+2]/256.) ;
Handles all the computation for the textures.
Definition: Texturing.h:39
v2d Boundaries
List of boundaries.
Definition: Texturing.h:66
vector< vector< float > > colors
Colors vector for the different dimensions.
Definition: Texturing.h:43
vector< int > TsName
Actual integer timestep.
Definition: Texturing.h:69
vector< int > NewViewPoint
Used if the location×tep require a modification. Eventually transfered to ViewPoint when the ren...
Definition: Texturing.h:73
v1d lambdagrid
Pixel grid in latitude.
Definition: Texturing.h:56
bool justloaded
Keep track if no render happened yet (used for the 3D simulation)
Definition: Texturing.h:78
int nrotate
Definition: Texturing.h:79
v1d thetagrid
Pixel grid in longitude.
Definition: Texturing.h:57
vector< std::thread > Threads
Rendering thread list.
Definition: Texturing.h:81
vector< int > RenderedAlready
Keeps track of what location/timesteps have already been rendered and do not need to be recomputed.
Definition: Texturing.h:74
vector< Timestep > Ts
List of Timestep.
Definition: Texturing.h:68
void rescale(v1f &c, cv1f sum)
Coloring rescaling.
v1d R
Particle radii.
Definition: Texturing.h:70
v1d View
Location of the rendering in the ND space.
Definition: Texturing.h:71
vector< int > ViewPoint
Current location×tep for rendering.
Definition: Texturing.h:72
bool runfast
Render only the current location×tep, not doing any additional rendering for caching purposes.
Definition: Texturing.h:75
bool singlerendered
Render a single location×tep.
Definition: Texturing.h:76
int N
Number of grains.
Definition: Texturing.h:65
vector< vector< string > > FileList
Data file list.
Definition: Texturing.h:80
Individual timestep data.
Definition: Texturing.h:31
v2d X
Particle locations.
Definition: Texturing.h:33
v2d A
Particle attached frame.
Definition: Texturing.h:34
const vector< float > cv1f
Definition: Typedefs.h:16
vector< vector< double > > v2d
Definition: Typedefs.h:10
vector< float > v1f
Definition: Typedefs.h:12
unsigned int uint
Definition: Typedefs.h:8
static void matvecmult(v1d &res, cv1d &A, cv1d &B)
Multiply a matrix with a vector, in place.
Definition: Tools.h:701
static double hyperspherical_xtophi(cv1d &x, v1d &phi)
Convert from cartesian to hyperspherical coordinates.
Definition: Tools.h:889
const vector< double > cv1d
Definition: Typedefs.h:13
const vector< vector< double > > cv2d
Definition: Typedefs.h:14
static void transpose_inplace(v1d &a)
Transpose in-place.
Definition: Tools.h:259
static void clear()
Get the class ready for a different dimension.
Definition: Tools.h:339
vector< double > v1d
Definition: Typedefs.h:9
static void initialise()
Initialise the member variables, in particular the variables to handle skew-symmetric flattened matri...
Definition: Tools.h:318
int viewpermute(v1d &View)
Rotate the viewpoint so that the rendered view is using the first 3 dimensions. Effectively unused si...
Definition: Texturing.h:554
int write_vtkmap(map< string, string > args)
Outputs the texture as a vtk surface.
Definition: Texturing.h:613
int csvread_A(const char path[], v2d &result, int d)
Reader for CSV orientation informations.
Definition: io.cpp:21
v1d thetagrid
Definition: io.h:33
void runthread_spaceloop(Texturing< d > *T, v1d View, uint tsint, int nrotate, int dim)
Function to call Texturing::spaceloop()
Definition: Texturing.h:125
bool blenderrender
Definition: Server.cpp:24
int csvread_XR(const char path[], v2d &result, v1d &R, int d)
Reader for CSV location informations.
Definition: io.cpp:48
void filepathname(char *path, int n, int time, cv1d &View)
Generates the proper texture name.
Definition: Texturing.h:577
void dispvector(const v1d &a)
Convenient function to print a vector on screen.
Definition: Texturing.h:116
int SetNewViewPoint(map< string, string > args)
Modify the current location and/or timestep en render.
Definition: Texturing.h:247
void hereandnow(v1d View, uint tsint, int nrotate)
Render the current location at the current timestep.
Definition: Texturing.h:398
int write_colormap_vtk_base()
Output the colormap as a vtk volume.
Definition: Texturing.h:602
int set_grid(int nb)
Set the grid in latitude-longitude.
Definition: Texturing.h:530
void Render(vector< string > &filerendered, cv1d &View, int nrotate, int time, cv2d &X, cv1d &R, cv2d &A)
Do the actual rendering of the textures.
Definition: Texturing.h:409
void phi2color(vector< uint8_t >::iterator px, cv1d &phi, int d, vector< vector< float >> &colors)
Convert from hyperspherical coordinates to actual pixel color, using the provided vector of colors.
Definition: Texturing.cpp:18
#define FilePerLine
Line size for single file tiled output.
Definition: Texturing.h:25
int clean()
Clean the data to get ready for another simulation rendering.
Definition: Texturing.h:210
void runthread_timeloop(Texturing< d > *T, v1d View, uint tsint, int nrotate)
Function to call Texturing::timeloop()
Definition: Texturing.h:121
int write_colormap_nrrd_base(map< string, string > args)
Output the texture as an NRRD file.
Definition: Texturing.h:607
void timeloop(v1d View, uint tsint, int nrotate)
Run all the rendering for the curent timestep, for all other location in dimensions higher than 3D vi...
Definition: Texturing.h:376
int write_colormap_vtk(int d, vector< vector< float >> &colors)
Writer for VTK colormaps.
Definition: io.cpp:77
int MasterRender()
Handle the rendering threads.
Definition: Texturing.h:297
#define DeltaX
Step in location for the visualisation.
Definition: Texturing.h:24
int initialise(map< string, string > args)
Loads all the data for the requested simulation in memory.
Definition: Texturing.h:130
void spaceloop(v1d View, uint tsint, int nrotate, int dim)
Run all the rendering in the current location, at all timesteps.
Definition: Texturing.h:351
int write_img(char path[], int w, int h, uint8_t *px)
Write for png textures.
Definition: io.cpp:6
int write_NrrdIO(string path, int d, vector< vector< float >> &colors)
Writer for NRRD colormaps.
Definition: io.cpp:116
bool isrendered()
Verify if the current location/timestep is already rendered.
Definition: Texturing.h:277
void filepathname(char *path, int n, int time, cv1d View)
Filename generator.
Definition: json.hpp:5678
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1181
PUGI_IMPL_FN void sort(I begin, I end, const Pred &pred)
Definition: pugixml.cpp:7707