/
comet_vtk.cpp
3153 lines (2353 loc) · 89.4 KB
/
comet_vtk.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
/*
comet - an actin-based motility simulator
Copyright (C) 2009 Mark J Dayel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
When using the code in academic work please cite:
Dayel MJ, Akin O, Landeryou M, Risca V, Mogilner A, et al. (2009)
In Silico Reconstitution of Actin-Based Symmetry Breaking and Motility.
PLoS Biol 7(9):e1000201. doi:10.1371/journal.pbio.1000201
and include any modifications to the source code with the published work.
*/
#include "stdafx.h"
#include "Colour.h"
#ifdef LINK_VTK
#define NUCTEXTUREJPEG1 "nuctex.jpg"
#define NUCTEXTUREJPEG2 "~/comet/nuctex.jpg"
#ifndef NOKBHIT
#ifndef _WIN32
#include "kbhit.h"
#define kbhit keyb.kbhit
#define getch keyb.getch
#else
#include <conio.h>
#endif
#endif
#include <iostream>
#include <fstream>
#include <sstream>
//#include <string>
#include "comet_vtk.h"
#include "tracknodeinfo.h"
// vtk
#include "vtkCubeSource.h"
//
#include "vtkMath.h"
#include "vtkProperty.h"
#include "vtkSphereSource.h"
#include "vtkCylinderSource.h"
#include "vtkLineSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkGaussianSplatter.h"
#include "vtkImageData.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkTubeFilter.h"
#include "vtkUnstructuredGrid.h"
#include "vtkStructuredPoints.h"
#include "vtkAxes.h"
#include "vtkFloatArray.h"
#include "vtkCharArray.h"
#include "vtkUnsignedCharArray.h"
#include "vtkContourFilter.h"
#include "vtkLight.h"
#include "vtkCamera.h"
#include "vtkLookupTable.h"
#include "vtkInteractorObserver.h"
#include "vtkImageData.h"
#include "vtkPolyData.h"
#include "vtkCellArray.h"
//#include "vtkIdType.h"
#include "vtkCellData.h"
#include "vtkDataSet.h"
#include "vtkTransform.h"
#include "vtkTransformPolyDataFilter.h"
//#include "vtkSmartPointer.h"
#include "vtkCardinalSpline.h"
#include "vtkStructuredGrid.h"
#include "vtkHedgeHog.h"
#include "vtkStreamLine.h"
#include "vtkStreamTracer.h"
#include "vtkTubeFilter.h"
#include "vtkRungeKutta4.h"
#include "vtkRenderLargeImage.h"
#include "vtkAssembly.h"
#include "vtkExporter.h"
//#include "vtkPlanes.h"
#include "vtkPlane.h"
//#include "vtkPoints.h"
//#include "vtkNormals.h"
#include "vtkVectorText.h"
#include "vtkFollower.h"
#include "vtkLightCollection.h"
// vol rend
#include "vtkImageImport.h"
#include "vtkPiecewiseFunction.h"
#include "vtkColorTransferFunction.h"
#include "vtkVolumeProperty.h"
#include "vtkVolumeRayCastCompositeFunction.h"
#include "vtkVolumeRayCastMIPFunction.h"
#include "vtkVolumeRayCastMapper.h"
#include "vtkImageCast.h"
#include "vtkShepardMethod.h"
#include "vtkSmoothPolyDataFilter.h"
#include "vtkWindowedSincPolyDataFilter.h"
#include "vtkPolyDataNormals.h"
// output
#include "vtkDataWriter.h"
#include "vtkStructuredPointsWriter.h"
// ImageWriter
#include "vtkWindowToImageFilter.h"
#include "vtkJPEGWriter.h"
#include "vtkPNGWriter.h"
#include "vtkBMPWriter.h"
#include "vtkVRMLExporter.h"
// Texture map
#include "vtkTexture.h"
#include "vtkTextureMapToSphere.h"
#include "vtkTransformTextureCoords.h"
#include "vtkJPEGReader.h"
using namespace std; // REVISIT only iostream probably temporary -- remove later
// Simple visualisation uing the current 'actin' simulation object to get at the results
// Makes extensive use of the VTK visulisation toolkit (public.kitware.com/VTK/)
// tested with v4.4 (current stable Dec 2005), see examples for some of the uses below.
// This code is called by 'post' option, and reads 'cometparams.ini' to set options for the
// visualisation. Some of this is a bit clumsy, as inconsistent options can be set, by design
// whatever is requested will be added to the visualisation, it's up to the user to set
// sensible and compatible options.
CometVtkVis::CometVtkVis(bool VIEW_VTK, bool dummy_vtk) // this parameter *should* be whether to create a viewing window
// does it need to be true for off-screen as well?
{
vtk_dummy_object = dummy_vtk;
if (dummy_vtk)
return;
cout << "Initialising VTK..." << endl;
cout.flush();
voxel_scale = 4.0;
p_scale = 100;
nuc_opacity = 0.4;
vx_intensity_scale = 100.0;
file_prefix = "vtk";
//OptsInteractive = true;
OptsRenderNucleator = true;
OptsRenderNodes = false;
OptsRenderLinks = false;
OptsShadeLinks = false;
OptsVolumeRenderNodes = false;
OptsIsoRenderNodes = true;
OptsRenderTracks = false;
OptsRenderText = false;
OptsNormaliseFrames = false;
OptsSkipOutOfFocusPoints = true;
OptsRenderProjection = RIP;
OptsUseNucTextureMap = true;
renderwin_npx = 0;
renderwin_npy = 0;
VIS_PARALLELPROJECTION = true;
VTK_HIGHQUAL = false;
OptsCameraDistMult = 7;
VIS_LINETHICKNESS = 1.0;
//voxelscalefactor = ptheactin->dbl_pixels(1.0)/voxel_scale;
voxelscalefactor = (800 / 30) / voxel_scale; // fix this---the voxel scale stuff is a mess (and unnecessary?)
setOptions();
// kludge for os x rendering
//#ifdef __APPLE__
// VTK_AA_FACTOR = 1;
//#endif
// this is *.99 so that links on the surface don't look like they go inside the sphere
radius_pixels = voxelscalefactor * 0.99 * RADIUS;
capsule_length_pixels = voxelscalefactor * CAPSULE_HALF_LINEAR;
//if(renderwin_npx == 0 || renderwin_npy == 0) {
// if(POST_VTK_VIEW) {
renderwin_npx = VTK_WIDTH;
renderwin_npy = VTK_HEIGHT;
// } else {
//renderwin_npx = VTK_WIDTH * VTK_AA_FACTOR;
//renderwin_npy = VTK_HEIGHT * VTK_AA_FACTOR;
// }
//}
viewupvect=vect(0,0,-1);
// REVISIT: Make a renderer each time? or clear out and rebuild?
// a renderer and render window
//renderer = vtkRenderer::New();
//render_win = vtkRenderWindow::New();
//render_win->AddRenderer(renderer);
renderer = vtkRenderer::New();
renderer->SetBackground(0, 0, 0);
OptVIEW_VTK = VIEW_VTK;
//cout << " render win (nx, ny) = " << renderwin_npx << " " << renderwin_npy << endl;
//cout << " voxel (ni nj nk) = " << ni<<" " << nj << " " << nk << endl;
render_win = vtkRenderWindow::New();
//if(!(POST_VTK_VIEW || VIEW_VTK))
// render_win->OffScreenRenderingOn(); // this seems to do nothing...
render_win->SetSize(renderwin_npx, renderwin_npy);
//render_win->LineSmoothingOn();
//render_win->PointSmoothingOn();
// smoothing seems to cause lines on sphere for some reason
//if (!OptsRenderNodes) // smoothing is too slow if rendering the nodes
// render_win->PolygonSmoothingOn(); // seem to need this else rendering artifact on sphere
// const bool VTK_STEREO = true;
//
// if (VTK_STEREO)
// {
// render_win->StereoCapableWindowOn();
//#if VTK_MAJOR_VERSION > 4
// render_win->SetStereoTypeToAnaglyph();
//#else
// render_win->SetStereoTypeToRedBlue();
//#endif
// }
render_win->AddRenderer(renderer);
if(POST_VTK_VIEW || VIEW_VTK) // only create interactive window if we're actually using it
{
iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(render_win);
iren->Initialize();
}
//else
//{
if(VTK_HIGHQUAL) // increase quality - this is slow and doesn't improve the quality much
{
render_win->SetAAFrames(7);
}
//}
// scale linewidths by aa factor unless rendering to screen
if (!POST_VTK_VIEW)
linewidths = (double)VTK_AA_FACTOR * VIS_LINETHICKNESS;
else
linewidths = VIS_LINETHICKNESS;
if (!NO_COLBAR && OptsRenderLinks)
{ // slow, so only generate colorbar if we need to
// write the colourmap file
// this is kind of slow, so only write if doesn't already exist
sprintf(VTK_colmap_filename, "%stempVTKcolmap.png", TEMPDIR);
ifstream isthere(VTK_colmap_filename);
if (isthere)
{
isthere.close();
}
else
{ // colourmap file doesn't exist, so create
char tmpfilename[1024];
sprintf(tmpfilename, "%stempVTKcolmap.bmp", TEMPDIR);
ofstream outbmpfile;
// write out file with colourmap
Dbl2d imageR, imageG, imageB;
const int width = VTK_WIDTH * VTK_AA_FACTOR;
const int height = VTK_HEIGHT * VTK_AA_FACTOR;
imageR.resize(width);
imageG.resize(width);
imageB.resize(width);
for (int x = 0; x<width; x++)
{
imageR[x].resize(height);
imageG[x].resize(height);
imageB[x].resize(height);
fill(imageR[x].begin(),imageR[x].end(),0.0);
fill(imageG[x].begin(),imageG[x].end(),0.0);
fill(imageB[x].begin(),imageB[x].end(),0.0);
}
ptheactin->p_nuc->segs.write_colourmap_bitmap(imageR, imageG, imageB); //, VTK_AA_FACTOR);
outbmpfile.open(tmpfilename, ios::out | ios::binary | ios::trunc);
ptheactin->writebitmapheader(outbmpfile, width, height);
ptheactin->writebitmapfile(outbmpfile, imageR, imageG, imageB);
outbmpfile.close();
// change the black background to transparent, and convert to png:
char command1[1024];
sprintf(command1,
"%s -transparent black \"%s\" \"%s\"",
IMAGEMAGICKCONVERT, tmpfilename, VTK_colmap_filename);
mysystem(command1);
// remove the temp bitmap
sprintf(command1, "rm \"%s\"", tmpfilename);
mysystem(command1);
}
}
texturereadOK = false;
#if VTK_MAJOR_VERSION > 4
if (OptsUseNucTextureMap)
{
ifstream texturefile1(NUCTEXTUREJPEG1, ios::in);
ifstream texturefile2(NUCTEXTUREJPEG2, ios::in);
if (texturefile1)
{
texturefile1.close();
// read texture image
tx_reader = vtkJPEGReader::New();
cout << "Reading texture file '"<< NUCTEXTUREJPEG1 << "'...";
cout.flush();
tx_reader->SetFileName(NUCTEXTUREJPEG1);
cout << " done." << endl;
cout.flush();
texturereadOK = true; // todo: how to test if vtk read the jpeg properly?
}
else if (texturefile2)
{
texturefile2.close();
// read texture image
tx_reader = vtkJPEGReader::New();
cout << "Reading texture file '"<< NUCTEXTUREJPEG2 << "'...";
cout.flush();
tx_reader->SetFileName(NUCTEXTUREJPEG2);
cout << " done." << endl;
cout.flush();
texturereadOK = true;
}
else
{
texturereadOK = false;
cerr << "Can't read texture file '"<< NUCTEXTUREJPEG1 << "' or '" << NUCTEXTUREJPEG2 << "'" << endl;
}
}
#endif
cout << "VTK initialization finished" << endl;
cout.flush();
}
CometVtkVis::~CometVtkVis()
{
// finished
// REVISIT: deleting renderer kills the pipeline?
// iren->Delete();
//render_win->Delete();
if (vtk_dummy_object)
return;
render_win->RemoveRenderer(renderer);
render_win->Delete();
renderer->Delete();
if(POST_VTK_VIEW || OptVIEW_VTK) // increase quality for non-interactive
{
iren->Delete();
}
#if VTK_MAJOR_VERSION > 4
if (texturereadOK)
tx_reader->Delete();
#endif
// remove the scalebar png file
//char command1[1024];
//sprintf(command1, "rm %s", VTK_colmap_filename);
//mysystem(command1);
}
void CometVtkVis::RestartRenderWindow()
{
render_win->Delete();
if(POST_VTK_VIEW || OptVIEW_VTK) // increase quality for non-interactive
{
iren->Delete();
iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(render_win);
iren->Initialize();
}
}
void CometVtkVis::buildVTK(const int &framenumber, vect & cameraposition, vect & cameratarget)
{
//
// this doesn't work: makes it wobble (rounding problem with the int conversion)
//
// double minx, miny, minz;
//double maxx, maxy, maxz;
//int existantnodes;
// ptheactin->getnodeextents(minx, miny, minz,maxx, maxy, maxz, existantnodes);
// ni = int( mymax(-minz, maxz) * 2 * voxelscalefactor + 10);
// nj = int( mymax(-miny, maxy) * 2 * voxelscalefactor + 10);
// nk = int( mymax(-minx, maxx) * 2 * voxelscalefactor + 10);
// // we must've mixed up ni nj and nk somewhere---workaround to make them equal.
// ni = nj = nk = mymax( mymax ( ni , nj ) , nk);
bool BIGISOSURFACERANGE=true; // set to increase the array size if need to render large isosurfaces (slow)
// find out data size
ni = int(BMP_HEIGHT/voxel_scale) ;
nj = int(BMP_HEIGHT/voxel_scale) ;
nk = int(BMP_HEIGHT/voxel_scale) ;
if (BIGISOSURFACERANGE) // increase the range
{
ni*=2;
nj*=2;
nk*=2;
}
//cout << " render win (nx, ny) = " << renderwin_npx << " " << renderwin_npy << endl;
//cout << " voxel (ni nj nk) = " << ni<<" " << nj << " " << nk << endl;
setProjection(cameraposition,cameratarget); // also sets the rotation of the objects, so comes before actors:
// add objects to renderer
if(OptsRenderNucleator)
{
addNucleator(false); // render a solid nucleator
if (VTK_NUC_WIREFRAME)
addNucleator(true); // if wireframe, add another nucleator, in wireframe and slightly bigger
}
if(OptsRenderNodes)
addNodes();
if(OptsRenderLinks)
addLinks();
if(OptsVolumeRenderNodes || OptsIsoRenderNodes)
{
//addGuassianNodeVolume(true);
vtkStructuredPoints *spoints = vtkStructuredPoints::New();
createGaussianVoxelRepresentation(spoints);
if(OptsVolumeRenderNodes)
addStructuredPointVolumeRender(spoints);
if(OptsIsoRenderNodes)
{
double threshold = 250.0;
addStructuredPointIsoRender(spoints, threshold, Colour( 0.3, 0.6, 0.3), 0.3 ); // was 1.0
//addStructuredPointIsoRender(spoints, 250.0, Colour( 0.3, 0.6, 0.3), 1.0 );
//addStructuredPointIsoRender(spoints, 150.0, Colour( 0.6, 0.3, 0.2), 0.5 );
//addStructuredPointIsoRender(spoints, 100.0, Colour( 0.2, 0.3, 0.6), 0.5 );
addStructuredPointIsoRender(spoints, threshold / 2.5, Colour(0.3, 0.3, 0.3), 0.3);
}
spoints->Delete();
}
if(OptsRenderTracks)
{
if (TRACKS_LENGTHS)
addTrackDistances();
else
addTracks(framenumber);
//addAxes();
}
// if(OptsRenderText)
// addVoxelBound(renderer);
addLight();
//renderer->GetActiveCamera()->ViewingRaysModified();
//cout << "Rendering..." << endl;
//cout.flush();
// -- rendering
if(POST_VTK_VIEW)
{
cout << "Starting interactive mode" << endl;
iren->Start();
}
else
{
if (OptVIEW_VTK)
{
// render to screen
iren->Render();
}
// render to file
saveImage(framenumber);
// render rotation set
//saveImageRotationSet(framenumber);
// save vrml every 20 frames
//if (framenumber % 20 == 0)
if (!POST_PROCESSSINGLECPU) // don't make vrml unless rendering only one frame (POST_PROCESSSINGLECPU is set when multithreading)
saveVRML(framenumber);
}
// clear all actors from the renderer
#if VTK_MAJOR_VERSION < 5
renderer->RemoveAllProps();
#else
renderer->RemoveAllViewProps();
#endif
// this reuse of renderer seems to cause segfaults in vtk.
// CHECK: order matters in deletion of these objects
// otherwise too many X Servers error.
//render_win->RemoveRenderer(renderer);
//renderer->Delete();
}
// -- Helper functions
void CometVtkVis::addAxes()
{
// see finance.cxx in vtk examples
vtkAxes *axes = vtkAxes::New();
VTK_FLOAT_PRECISION origin[3] = {0.0, 0.0, 0.0};
axes->SetOrigin( origin );
axes->SetScaleFactor(1.3*voxelscalefactor * RADIUS);
vtkTubeFilter *axes_tubes = vtkTubeFilter::New();
axes_tubes->SetInput( axes->GetOutput() );
axes_tubes->SetRadius(axes->GetScaleFactor()/50.0);
axes_tubes->SetNumberOfSides(10);
vtkPolyDataMapper *axes_mapper = vtkPolyDataMapper::New();
axes_mapper->SetInput(axes_tubes->GetOutput());
vtkActor *axes_actor = vtkActor::New();
axes_actor->SetMapper(axes_mapper);
axes_mapper->Delete();
renderer->AddActor(axes_actor);
axes_actor->Delete();
}
void CometVtkVis::addVoxelBound()
{
// Add a crude voxel bounding box, sometimes useful
// create lines
VTK_FLOAT_PRECISION pt[3];
vtkLineSource *line1 = vtkLineSource::New();
vtkPolyDataMapper *map1 = vtkPolyDataMapper::New();
vtkActor *line_actor1 = vtkActor::New();
pt[0] = -ni/2;
pt[1] = -nj/2;
pt[2] = -nk/2;
line1->SetPoint1( pt );
pt[0] = -ni/2;
pt[1] = +nj/2;
pt[2] = -nk/2;
line1->SetPoint2( pt );
// add the actor to the scene
map1->SetInput(line1->GetOutput());
line_actor1->SetMapper(map1);
line_actor1->GetProperty ()->SetColor(1, 1, 0);
renderer->AddActor(line_actor1);
line1->Delete();
map1->Delete();
line_actor1->Delete();
vtkLineSource *line2 = vtkLineSource::New();
vtkPolyDataMapper *map2 = vtkPolyDataMapper::New();
vtkActor *line_actor2 = vtkActor::New();
pt[0] = -ni/2;
pt[1] = -nj/2;
pt[2] = -nk/2;
line2->SetPoint1( pt );
pt[0] = -ni/2;
pt[1] = -nj/2;
pt[2] = +nk/2;
line2->SetPoint2( pt );
// add the actor to the scene
map2->SetInput(line2->GetOutput());
line_actor2->SetMapper(map2);
line_actor2->GetProperty ()->SetColor(0, 1, 0);
renderer->AddActor(line_actor2);
line2->Delete();
map2->Delete();
line_actor2->Delete();
vtkLineSource *line3 = vtkLineSource::New();
vtkPolyDataMapper *map3 = vtkPolyDataMapper::New();
vtkActor *line_actor3 = vtkActor::New();
pt[0] = -ni/2;
pt[1] = -nj/2;
pt[2] = -nk/2;
line3->SetPoint1( pt );
pt[0] = +ni/2;
pt[1] = -nj/2;
pt[2] = -nk/2;
line3->SetPoint2( pt );
// add the actor to the scene
map3->SetInput(line3->GetOutput());
line_actor3->SetMapper(map3);
line_actor3->GetProperty ()->SetColor(1, 0, 0);
renderer->AddActor(line_actor3);
line3->Delete();
map3->Delete();
line_actor3->Delete();
}
void CometVtkVis::saveImageRotationSet(const int &framenumber)
{
double steps = 270;
vect focalpoint;
vect cameraorigpos;
// read the current positions
renderer->GetActiveCamera()->GetFocalPoint(focalpoint.x, focalpoint.y, focalpoint.z);
renderer->GetActiveCamera()->GetPosition(cameraorigpos.x,cameraorigpos.y,cameraorigpos.z);
vect camera_orig_vec = focalpoint-cameraorigpos;
double cameralen= camera_orig_vec.length();
double step = 360 / steps;
double tilt = 45 * (2 * PI / 360);
double tiltxscale = cos (tilt);
double tiltzscale = sin (tilt);
char filename[1024], command[1024];
//sprintf(tmpfilename , "%s%s_%05i.bmp", TEMPDIR, file_prefix.c_str(), framenumber );
sprintf(command , "mkdir -p %s%s_%05i", VTKDIR, file_prefix.c_str(), framenumber);
mysystem(command);
for (int frame = 1; frame <= steps ; ++frame)
{
double angle = 2 * PI * step * (double) frame / 360.0;
vect campos;
campos.x = focalpoint.x + (tiltxscale * cameralen * cos(angle));
campos.y = focalpoint.y + tiltzscale * cameralen * cos(angle);
campos.z = focalpoint.z + cameralen * sin(angle);
renderer->GetActiveCamera()->SetPosition(campos.x, campos.y, campos.z );
renderer->GetActiveCamera()->ComputeViewPlaneNormal();
renderer->ResetCameraClippingRange();
cout << "Rendering Frame " << frame << " of " << steps << endl;
cout.flush();
//saveBigImage(ren, frame);
sprintf(filename , "%s%s_%05i/%s_%05i_%05i.%s", VTKDIR, file_prefix.c_str(), framenumber, file_prefix.c_str(), framenumber, frame, BMP_OUTPUT_FILETYPE.c_str());
saveImage(frame, filename);
}
}
void CometVtkVis::saveImage(const int &framenumber)
{
char filename[1024];
//sprintf(tmpfilename , "%s%s_%05i.bmp", TEMPDIR, file_prefix.c_str(), framenumber );
sprintf(filename , "%s%s_%05i.%s", VTKDIR, file_prefix.c_str(), framenumber, BMP_OUTPUT_FILETYPE.c_str());
saveImage(framenumber, filename);
}
void CometVtkVis::saveImage(const int &framenumber, char* filename)
{
char tmpfilename[1024];//,filename[1024];
sprintf(tmpfilename , "%s%s_%05i.bmp", TEMPDIR, file_prefix.c_str(), framenumber );
//sprintf(filename , "%s%s_%05i.%s", VTKDIR, file_prefix.c_str(), framenumber, BMP_OUTPUT_FILETYPE.c_str());
//cout << "Saving " << filename << endl;
//vtkWindowToImageFilter *rwin_to_image = vtkWindowToImageFilter::New();
//rwin_to_image->SetInput(render_win);
vtkRenderLargeImage *renderLarge = vtkRenderLargeImage::New();
renderLarge->SetInput(renderer);
renderLarge->SetMagnification(VTK_AA_FACTOR*VTK_IMAGE_MULTIPLIER);
// vtkBMPWriter seems a bit faster than vtkPNGWriter, and we're compressing with IM to png anyway later
// so use BMP here
vtkBMPWriter *imagewriter = vtkBMPWriter::New();
imagewriter->SetInput( renderLarge->GetOutput() ); //rwin_to_image->GetOutput() );
imagewriter->SetFileName( tmpfilename );
imagewriter->Write();
imagewriter->Delete();
//rwin_to_image->Delete();
renderLarge->Delete();
char textstring[1024];
int text_height_pixels = int ( 25 * (double) TEXT_POINTSIZE / 20.0);
if (NO_IMAGE_TEXT)
{
*textstring=0;
}
else
{
sprintf(textstring, "-font helvetica -fill white -pointsize %i -draw \"text +%i+%i 'Frame % 6i' text +%i+%i 'Time % 6i'\"",
TEXT_POINTSIZE, 5 , text_height_pixels,
framenumber,
5 , text_height_pixels * 2 ,
int(framenumber * InterRecordIterations * DELTA_T) );
}
if (NO_COLBAR || !OptsRenderLinks) // don't need the colorbar if not plotting links
{
if (VTK_AA_FACTOR!=1)
{
char command1[1024];
sprintf(command1,
"(%s -quality %i -resize %f%% -density 300x300 %s \"%s\" \"%s\" ; rm \"%s\" ) &",
IMAGEMAGICKCONVERT, BMP_COMPRESSION, 100/(double)VTK_AA_FACTOR, textstring,
tmpfilename, filename, tmpfilename);
//cout << command1 << endl;
mysystem(command1);
}
else
{
char command1[1024];
sprintf(command1,
"(%s -quality %i -density 300x300 %s \"%s\" \"%s\" ; rm \"%s\" ) &",
IMAGEMAGICKCONVERT, BMP_COMPRESSION, textstring,
tmpfilename, filename, tmpfilename);
//cout << command1 << endl;
mysystem(command1);
}
}
else
{
if (VTK_AA_FACTOR!=1)
{
char command1[1024];
sprintf(command1,
"(%s -compose Dst_Over -composite -quality %i -resize %f%% -density 300x300 %s \"%s\" \"%s\" \"%s\" ; rm \"%s\" ) &",
IMAGEMAGICKCONVERT, BMP_COMPRESSION, 100/(double)VTK_AA_FACTOR, textstring,
VTK_colmap_filename, tmpfilename, filename, tmpfilename);
//cout << command1 << endl;
mysystem(command1);
}
else
{
char command1[1024];
sprintf(command1,
"(%s -compose Dst_Over -composite -quality %i %s \"%s\" \"%s\" \"%s\" ; rm \"%s\" ) &",
IMAGEMAGICKCONVERT, BMP_COMPRESSION, textstring,
VTK_colmap_filename, tmpfilename, filename, tmpfilename);
//cout << command1 << endl;
mysystem(command1);
}
}
}
void CometVtkVis::saveVRML(const int &framenumber)
{ // for some reason this produces *huge* files (>16MB) when nucleator texture is turned on
// it also doesn't seem to work at all...(lighting problem?)
char vrmltmpfilename[1024];
char vrmlfilename[1024];
sprintf(vrmltmpfilename , "%s%s_%05i.wrl", TEMPDIR, file_prefix.c_str(), framenumber );
sprintf(vrmlfilename , "%s%s_%05i.wrl", VRMLDIR, file_prefix.c_str(), framenumber );
// for some reason directional lights don't seem to work for vrml
// so remove them and switch on ambient lighting:
//renderer->GetLights()->RemoveAllItems();
//renderer->SetAmbient(0.8,0.8,0.8);
// render_win->Render();
rotationmatrix rotmat = ptheactin->nuc_to_world_rot;
double theta, phy, gamma;
rotmat.getangles(theta, phy, gamma);
double x,y,z,angle;
rotmat.getaxis_and_angle(x,y,z,angle);
cout << setprecision(3) ;
cout << "Nucleator rotation:" << theta << " " << phy << " "<< gamma << endl;
cout << "Nucleator displacement:" << ptheactin->p_nuc->position.x * voxelscalefactor << " "
<< ptheactin->p_nuc->position.y * voxelscalefactor << " "
<< ptheactin->p_nuc->position.z * voxelscalefactor << endl;
cout << "Writing vrml file " << vrmlfilename << endl;
// something may be wrong here? Links don't seem to work in the resulting vrml
// (co-ords are present, but links don't show up in vrml viewer)
// nucleator, isosurfaces and tracks seem OK
vtkVRMLExporter *VRML = vtkVRMLExporter::New();
VRML->SetInput(render_win);
VRML->SetFileName( vrmltmpfilename );
//VRML->Update();
VRML->Write();
VRML->Delete();
// vtk vrml export (for vtk 5.1 at least) fails to apply the transformation matrix to the nucleator
// so to set the nucleator position/rotation
// we write out the vrml, then search/replace the position and rotation of the nucleator using gsed
// note the algorithm for the rotation axis/angle may not be robust
char command5[1024];
sprintf(command5, "gsed '0,/translation 0 0 0/s//translation %f %f %f/' %s | gsed '0,/rotation 0 0 1 0/s//rotation %f %f %f %f/' " // > %s "
,ptheactin->p_nuc->position.x * voxelscalefactor
,ptheactin->p_nuc->position.y * voxelscalefactor
,ptheactin->p_nuc->position.z * voxelscalefactor
,vrmltmpfilename
,x
,y
,z
,angle);
//,vrmlfilename);
//cout << command5;
//mysystem(command5);
// capsule ends
// need to replace second and third positions with these:
//vect endcap1(0,0,CAPSULE_HALF_LINEAR*voxelscalefactor);
//vect endcap2(0,0,-CAPSULE_HALF_LINEAR*voxelscalefactor);
//rotmat.rotate(endcap1);
//rotmat.rotate(endcap2);
//cout << "Compressing vrml file " << vrmlfilename << endl;
char command1[1024];
if(OptsRenderNucleator) // only replace rotation/translation if we actually have a nucleator!
sprintf(command1 , "( %s | \"%s\" > \"%s%s\" ; rm \"%s\" ) &", command5, COMPRESSCOMMAND, vrmlfilename, COMPRESSEDEXTENSION, vrmltmpfilename );
else
sprintf(command1 , "( %s | \"%s\" > \"%s%s\" ; rm \"%s\" ) &", "cat", COMPRESSCOMMAND, vrmlfilename, COMPRESSEDEXTENSION, vrmltmpfilename );
mysystem(command1);
}
void CometVtkVis::addNucleator(bool wireframe)
{
// create assembly and add nucleator
vtkAssembly *nucleator_actor=vtkAssembly::New();
if (NUCSHAPE == nucleator::capsule)
add_capsule_to_assembly(nucleator_actor, wireframe);
else
add_sphere_to_assembly(nucleator_actor, wireframe); // sphere or ellipsoid
//rotationmatrix tmp_rotmatrix;
//tmp_rotmatrix = ptheactin->nuc_to_world_rot;
//tmp_rotmatrix.inverse();
// set nucleator rotation and translation matrix