1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TimeRenderer.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 #include "vtkActor.h"
16 #include "vtkCamera.h"
17 #include "vtkCellArray.h"
18 #include "vtkConeSource.h"
19 #include "vtkCubeSource.h"
20 #include "vtkCullerCollection.h"
21 #include "vtkGarbageCollector.h"
22 #include "vtkGlyph3D.h"
23 #include "vtkPoints.h"
24 #include "vtkPolyData.h"
25 #include "vtkPolyDataMapper.h"
26 #include "vtkProperty.h"
27 #include "vtkRenderWindow.h"
28 #include "vtkRenderer.h"
29 #include "vtkStripper.h"
30 #include "vtkTimerLog.h"
31 #include "vtkTriangleFilter.h"
32 
main(int argc,char * argv[])33 int main( int argc, char *argv[] )
34 {
35   // For timings
36   int i;
37   int l, w, nActors, N, n;
38   vtkIdType aPnts;
39 
40   if (argc != 5)
41     {
42     l = 10;
43     w = 10;
44     aPnts = 15;
45     nActors = 100;
46     }
47   else
48     {
49     l = atoi(argv[1]);
50     w = atoi(argv[2]);
51     aPnts = atoi(argv[3]);
52     nActors = atoi(argv[4]);
53     }
54 
55   // n is the number of points per level
56   n = l * w;
57   // N is the total number of points
58   N = aPnts * nActors;
59 
60   float x, y, z;
61   vtkIdType *cdata = new vtkIdType [aPnts];
62   for (int j = 0; j < aPnts; j++)
63     {
64     cdata[j] = j;
65     }
66 
67   vtkProperty *prop = vtkProperty::New();
68 
69   //vtkGarbageCollector::DeferredCollectionPush();
70 
71   // create a rendering window and both renderers
72   vtkRenderer *ren1 = vtkRenderer::New();
73   ren1->GetCullers()->InitTraversal();
74   vtkRenderWindow *renWindow = vtkRenderWindow::New();
75   renWindow->AddRenderer(ren1);
76 
77   // Create a cube polydata
78   vtkPoints *cpnts = vtkPoints::New();
79   cpnts->SetNumberOfPoints(14);
80 
81   vtkCellArray *ccells = vtkCellArray::New();
82 
83   cpnts->SetPoint(0,   .1, -.1, -.1);
84   cpnts->SetPoint(1,  -.1, -.1, -.1);
85   cpnts->SetPoint(2,   .1,  .1, -.1);
86   cpnts->SetPoint(3,  -.1,  .1, -.1);
87   cpnts->SetPoint(4,  -.1,  .1,  .1);
88   cpnts->SetPoint(5,  -.1, -.1, -.1);
89   cpnts->SetPoint(6,  -.1, -.1,  .1);
90   cpnts->SetPoint(7,   .1, -.1, -.1);
91   cpnts->SetPoint(8,   .1, -.1,  .1);
92   cpnts->SetPoint(9,   .1,  .1, -.1);
93   cpnts->SetPoint(10,  .1,  .1,  .1);
94   cpnts->SetPoint(11, -.1,  .1,  .1);
95   cpnts->SetPoint(12,  .1, -.1,  .1);
96   cpnts->SetPoint(13, -.1, -.1,  .1);
97 
98   vtkIdType a[14];
99   for (i = 0; i < 14; i++)
100     {
101     a[i] = i;
102     }
103 
104   ccells->InsertNextCell(14L, a);
105   ccells->Squeeze();
106 
107   vtkPolyData *cube = vtkPolyData::New();
108   cube->SetPoints(cpnts);
109   cube->SetStrips(ccells);
110   cpnts->Delete();
111   ccells->Delete();
112 
113   vtkPolyDataMapper *mapper;
114   vtkCellArray *cells;
115   vtkActor *actor;
116   vtkGlyph3D *filter;
117   vtkPolyData *data;
118   vtkPoints *pnts = 0;
119   vtkTriangleFilter *tfilter;
120   vtkStripper *stripper;
121 
122   x = 0.0;
123   y = 0.0;
124   z = 0.0;
125   for (i = 0; i < N; i ++)
126     {
127     // See if we need to start a new actor
128     if ((i % aPnts) == 0)
129       {
130       if (pnts)
131         {
132         pnts->Delete();
133         }
134 
135       pnts = vtkPoints::New();
136       cells = vtkCellArray::New();
137       data = vtkPolyData::New();
138       filter = vtkGlyph3D::New();
139       mapper = vtkPolyDataMapper::New();
140       actor = vtkActor::New();
141       tfilter = vtkTriangleFilter::New();
142       stripper = vtkStripper::New();
143 
144       prop->SetInterpolationToFlat();
145       actor->SetProperty(prop);
146 
147       pnts->SetNumberOfPoints(aPnts);
148       cells->Allocate(aPnts);
149       cells->InsertNextCell(aPnts, cdata);
150       data->SetVerts(cells);
151       data->SetPoints(pnts);
152       tfilter->SetInputData(cube);
153       stripper->SetInputConnection(tfilter->GetOutputPort());
154       filter->SetSourceConnection(stripper->GetOutputPort());
155       filter->SetInputData(data);
156       mapper->SetInputConnection(filter->GetOutputPort());
157       actor->SetMapper(mapper);
158       ren1->AddActor(actor);
159 
160       // all these are held by way of the renderer, so do a fast unref
161       cells->Delete();
162       data->Delete();
163       filter->Delete();
164       mapper->Delete();
165       actor->Delete();
166       tfilter->Delete();
167       stripper->Delete();
168     }
169 
170     // See if we are on a new level)
171     if ((i % n) == 0)
172       {
173       z += 1.0;
174       x = 0.0;
175       y = 0.0;
176       }
177     else
178       {
179       if ((i % l) == 0)
180         {
181         x += 1.0;
182         y = 0.0;
183         }
184       else
185         {
186         y += 1.0;
187         }
188       }
189 
190     pnts->SetPoint(i % aPnts, x, y, z);
191     pnts->Modified();
192     }
193 
194   if (pnts)
195     {
196     pnts->Delete();
197     }
198 
199   // set the size of our window
200   renWindow->SetSize(500,500);
201 
202   // set the viewports and background of the renderers
203   //  ren1->SetViewport(0,0,0.5,1);
204   ren1->SetBackground(0.2,0.3,0.5);
205 
206   // draw the resulting scene
207   renWindow->Render();
208   ren1->GetActiveCamera()->Azimuth(3);
209   renWindow->Render();
210 
211   // Set up times
212   vtkTimerLog *tl = vtkTimerLog::New();
213 
214   tl->StartTimer();
215 
216   // do a azimuth of the cameras 3 degrees per iteration
217   // for (i = 0; i < 360; i += 3)
218 #if 1
219   for (i = 0; i < 360; i += 9)
220     {
221     ren1->GetActiveCamera()->Azimuth(3);
222     renWindow->Render();
223     }
224 #endif
225   tl->StopTimer();
226 
227   cerr << "Wall Time = " << tl->GetElapsedTime() << "\n";
228   cerr << "FrameRate = " << 120.0 / tl->GetElapsedTime() << "\n";
229 
230   // Clean up
231   cube->Delete();
232   vtkGarbageCollector::SetGlobalDebugFlag(1);
233   vtkGarbageCollector::SetGlobalDebugFlag(0);
234   prop->Delete();
235   ren1->Delete();
236   renWindow->Delete();
237   tl->Delete();
238   //vtkGarbageCollector::DeferredCollectionPop();
239 
240   delete [] cdata;
241   return 1;
242 }
243