source: XMLIO_V2/dev/dev_rv/src4/xmlio/vtk/lscereader.cpp @ 236

Last change on this file since 236 was 236, checked in by hozdoba, 13 years ago
File size: 33.3 KB
Line 
1
2#ifndef LSCE_EXPORTS
3#       include "lscereader.hpp"
4#else
5#       include "vtkLSCEReader.h"
6#endif //LSCE_EXPORTS
7
8
9// BibliothÚque VTK
10#include "vtkObjectFactory.h"
11#include "vtkInformation.h"
12#include "vtkInformationVector.h"
13#include "vtkStreamingDemandDrivenPipeline.h"
14
15#include "vtkPointLocator.h"
16
17#include <vtkProperty.h>
18#include <vtkDataSetMapper.h>
19#include <vtkActor.h>
20#include <vtkRenderWindow.h>
21#include <vtkRenderer.h>
22#include <vtkRenderWindowInteractor.h>
23#include <vtkLegendScaleActor.h>
24
25#include "vtkMath.h"
26
27#include "inetcdf4_impl.hpp"
28#include "inetcdf4_adv_impl.hpp"
29
30// Type Fortran
31#define ARRAY(valuetype, numdims) boost::shared_ptr<boost::multi_array<valuetype, numdims> >
32
33#define ARRAY_ASSIGN(value, valuetype, numdims, extent)\
34   value.reset(new boost::multi_array<valuetype, numdims>(boost::extents extent))
35
36#define ARRAY_CREATE(value, valuetype, numdims, extent)\
37   ARRAY(valuetype, numdims) value =                   \
38   ARRAY(valuetype, numdims)(new boost::multi_array<valuetype, numdims>(boost::extents extent))
39
40// Type C
41#define ARRAY_C_ASSIGN(value, valuetype, numdims, extent)\
42   value = ARRAY(valuetype, numdims)                     \
43   (new boost::multi_array<valuetype, numdims>(boost::extents extent, c_storage_order()))
44
45#define ARRAY_C_CREATE(value, valuetype, numdims, extent)\
46   ARRAY_C_ASSIGN(ARRAY(valuetype, numdims) value, valuetype, numdims, extent)
47
48
49#ifndef LSCE_EXPORTS
50namespace xmlioserver
51{
52   namespace vtk
53   {
54#endif //LSCE_EXPORTS
55       
56      /// ////////////////////// Définitions ////////////////////// ///
57      vtkStandardNewMacro(vtkLSCEReader);
58
59      vtkLSCEReader::vtkLSCEReader(void)
60         : FileName(), CurGridType(RECTILINEAR), VarNames()
61         , A3D(true), ATemporal(true), ACell(true)
62         , Reader()
63      {
64         this->SetNumberOfInputPorts (0);
65         this->SetNumberOfOutputPorts(1);
66      }
67
68      vtkLSCEReader::~vtkLSCEReader(void)
69      {
70
71      }
72
73      ///---------------------------------------------------------------
74
75      void vtkLSCEReader::SetFileName(const vtkStdString & fileName)
76      {
77         this->FileName = fileName;
78         this->Reader   = boost::shared_ptr<xmlioserver::io::CINetCDF4Adv>
79                                       (new xmlioserver::io::CINetCDF4Adv(fileName));
80         this->Modified();
81      }
82     
83      void vtkLSCEReader::SetVariable(const vtkStdString & variable)
84      {
85         this->RemoveAllSelectedVariables();
86         if (Reader->hasVariable(variable))
87            this->AddVariableToSelection(variable);
88         this->Modified();
89      }
90
91      const char * vtkLSCEReader::GetFileName(void) const
92      {
93         return (this->FileName.c_str());
94      }
95
96      void vtkLSCEReader::PrintSelf(ostream& os, vtkIndent indent)
97      {
98        this->Superclass::PrintSelf(os,indent);
99      }
100
101      //----------------------------------------------------------------
102      void vtkLSCEReader::AddVariableToSelection(const vtkStdString & varName)
103      {
104         if (this->Reader->isRectilinear(varName))
105            this->SetGridType (RECTILINEAR);
106         else if (this->Reader->isCurvilinear(varName))
107            this->SetGridType (CURVILINEAR);
108         else
109            this->SetGridType (UNSTRUCTURED);
110
111         this->VarNames.insert(varName);
112      }
113
114      void vtkLSCEReader::RemoveSelectedVariable(const vtkStdString & varName)
115      {
116         std::set<vtkStdString>::iterator it = this->VarNames.find(varName);
117         if (it != this->VarNames.end()) this->VarNames.erase(it);
118      }
119
120      void vtkLSCEReader::RemoveAllSelectedVariables(void)
121      {
122         this->VarNames.clear();
123      }
124
125      const std::set<vtkStdString> &
126            vtkLSCEReader::GetSelectedVariables(void) const
127      {
128         return (this->VarNames);
129      }
130
131      bool vtkLSCEReader::HasSelectedVariable(void) const
132      {
133         return (this->VarNames.size() != 0);
134      }
135
136      //----------------------------------------------------------------
137
138      void vtkLSCEReader::SetGridType (GridType type)
139      {
140         if ((this->IsUnstructured() && (type == CURVILINEAR)) ||
141             (this->IsUnstructured() && (type == RECTILINEAR)) ||
142             (this->IsCurvilinear()  && (type == RECTILINEAR)))
143            this->RemoveAllSelectedVariables();
144         this->CurGridType = type;
145      }
146
147      bool vtkLSCEReader::IsUnstructured(void) const
148      {
149         return (this->CurGridType == UNSTRUCTURED);
150      }
151
152      bool vtkLSCEReader::IsCurvilinear(void) const
153      {
154         return (this->CurGridType == CURVILINEAR);
155      }
156
157      bool vtkLSCEReader::IsRectilinear(void) const
158      {
159         return (this->CurGridType == RECTILINEAR);
160      }
161
162      //----------------------------------------------------------------
163
164      void vtkLSCEReader::AcceptTemporalOnly(bool value)
165      {
166         this->ATemporal = value;
167      }
168
169      void vtkLSCEReader::Accept3DOnly(bool value)
170      {
171         this->A3D = value;
172      }
173
174      void vtkLSCEReader::AcceptCellOnly(bool value)
175      {
176         this->ACell = value;
177      }
178
179      //----------------------------------------------------------------
180
181      int vtkLSCEReader::ProcessRequest
182                           (vtkInformation       *  request,
183                            vtkInformationVector ** vtkNotUsed(inputVector),
184                            vtkInformationVector *  outputVector)
185      {
186         
187         if(request->Has(vtkDemandDrivenPipeline::REQUEST_DATA()))
188         {
189            return (this->RequestData(NULL, NULL, outputVector));
190         }
191
192         if(request->Has(vtkDemandDrivenPipeline::REQUEST_DATA_OBJECT()))
193         {
194         //   return (this->RequestDataObject(NULL, NULL, outputVector));
195         }
196
197         if(request->Has(vtkDemandDrivenPipeline::REQUEST_INFORMATION()))
198         {
199              return (this->RequestInformation(NULL, NULL, outputVector));
200         }
201
202         if(request->Has(vtkStreamingDemandDrivenPipeline::REQUEST_UPDATE_EXTENT()))
203         {
204            return (this->RequestUpdateExtent(NULL, NULL, outputVector));
205         }
206
207         return (1);
208      }
209
210      //----------------------------------------------------------------
211
212      void vtkLSCEReader::ShowVariable(const vtkStdString & filename,
213                                       const vtkStdString & varname)
214      {
215         vtkSmartPointer<vtkLSCEReader> lscereader =
216               vtkSmartPointer<vtkLSCEReader>::New();
217
218         lscereader->SetFileName(filename);
219         lscereader->AddVariableToSelection(varname);
220         lscereader->Update();
221
222         // Création d'un mappeur et d'un acteur.
223         vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New();
224         mapper->SetInputConnection(lscereader->GetOutput()->GetProducerPort());
225         mapper->SetScalarRange  (1000, 10000);
226
227         vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
228         actor->SetMapper(mapper);
229         actor->GetProperty()->SetRepresentationToWireframe();
230
231         // Création d'une fenêtre de rendu.
232         vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
233         vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
234         renderWindow->AddRenderer(renderer);
235         vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
236            vtkSmartPointer<vtkRenderWindowInteractor>::New();
237         renderWindowInteractor->SetRenderWindow(renderWindow);
238
239
240         vtkSmartPointer<vtkLegendScaleActor> legendScaleActor =
241            vtkSmartPointer<vtkLegendScaleActor>::New();
242
243         // Ajout de l'acteur à la scÚne.
244         renderer->AddActor(actor);
245         renderer->AddActor(legendScaleActor);
246
247         renderer->GradientBackgroundOn();
248         renderer->SetBackground(1,1,1);
249         renderer->SetBackground2(0,0,0);
250
251         // Rendu et intéraction
252         renderWindow->Render();
253         renderWindowInteractor->Start();
254      }
255
256      //----------------------------------------------------------------
257
258      void vtkLSCEReader::CreateRectilinearGrid(vtkRectilinearGrid * grid,
259                                                vtkInformation     * outInfo,
260                                                vtkFloatArray      * xspacing,
261                                                vtkFloatArray      * yspacing,
262                                                vtkFloatArray      * zspacing,
263                                                vtkIntArray        * dimensions)
264      {
265            int extent[6] = { 0, dimensions->GetValue(0) - 1
266                            , 0, dimensions->GetValue(1) - 1
267                            , 0, dimensions->GetValue(2) - 1};
268            outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),extent,6);
269            grid->SetExtent (extent);
270            grid->SetDimensions (dimensions->GetValue(0),
271                                 dimensions->GetValue(1),
272                                 dimensions->GetValue(2));
273            grid->SetXCoordinates (xspacing);
274            grid->SetYCoordinates (yspacing);
275            grid->SetZCoordinates (zspacing);
276      }
277
278      void vtkLSCEReader::CreateStructuredGrid(vtkStructuredGrid * grid,
279                                               vtkInformation    * outInfo,
280                                               vtkPoints         * points,
281                                               vtkIntArray       * dimensions)
282      {
283            int extent[6] = { 0, dimensions->GetValue(0) - 1
284                            , 0, dimensions->GetValue(1) - 1
285                            , 0, dimensions->GetValue(2) - 1};
286            outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),extent,6);
287            grid->SetExtent (extent);
288            grid->SetDimensions (dimensions->GetValue(0),
289                                 dimensions->GetValue(1),
290                                 dimensions->GetValue(2));
291            grid->SetPoints(points);
292      }
293
294      void vtkLSCEReader::CreateUnstructuredGrid(vtkUnstructuredGrid * grid,
295                                                 vtkInformation      * outInfo,
296                                                 vtkPoints           * points,
297                                                 vtkCellArray        * cells,
298                                                 int                   celltype)
299      {
300         grid->SetPoints(points);
301         grid->SetCells(celltype, cells);
302      }
303
304      //----------------------------------------------------------------
305
306      void vtkLSCEReader::GetRectilinearConnectivity(int dimZ, int dimY, int dimX, vtkCellArray * cells)
307      {
308         if (dimZ == 1 && dimY >  1 && dimX >  1) // 2D
309         {
310            vtkIdType pts[4] ;
311
312            for (int i = 0, k = 0; i < (dimX - 1); i++)
313               for (int j = 0; j < (dimY - 1); j++, k++)
314            {
315               pts[0] =  i * dimY + j;
316               pts[1] =  i * dimY + j + 1;
317               pts[2] =  (i+1) * dimY + j + 1;
318               pts[3] =  (i+1) * dimY + j;
319               cells->InsertNextCell(4,  pts);
320            }
321         }
322         else if (dimZ > 1 && dimY >  1 && dimX >  1) // 3D
323         {
324            vtkIdType pts[8];
325            for (int i = 0, l = 0; i < (dimX - 1); i++)
326               for (int j = 0; j < (dimY - 1); j++)
327                  for (int k = 0; k < (dimZ - 1); k++, l++)
328            {
329               pts[0] =  i * dimY * dimZ + j * dimZ + k;
330               pts[1] =  i * dimY * dimZ + j * dimZ + k + 1;
331               pts[2] =  (i+1) * dimY * dimZ + j * dimZ + k + 1;
332               pts[3] =  (i+1) * dimY * dimZ + j * dimZ + k;
333
334               pts[4] =  i * dimY * dimZ  + (j + 1) * dimZ + k ;
335               pts[5] =  i * dimY * dimZ  + (j + 1) * dimZ + k +1;
336               pts[6] =  (i+1) * dimY * dimZ + (j + 1) * dimZ + k + 1;
337               pts[7] =  (i+1) * dimY * dimZ + (j + 1) * dimZ + k ;
338               cells->InsertNextCell(8,  pts);
339            }
340         }
341      }
342
343      //----------------------------------------------------------------
344
345      void vtkLSCEReader::CreateSimpleGrid (int xi, int xf,
346                                            int yi, int yf,
347                                            int zi, int zf,
348                                            vtkFloatArray * xspacing,
349                                            vtkFloatArray * yspacing,
350                                            vtkFloatArray * zspacing,
351                                            vtkIntArray   * dimensions)
352      {
353         dimensions->InsertNextValue (xf - xi + 1); // X
354         dimensions->InsertNextValue (yf - yi + 1); // Y
355         dimensions->InsertNextValue (zf - zi + 1); // Z
356         for (int i = xi; i <= xf; i++)
357            xspacing->InsertNextValue (i);
358         for (int j = yi; j <= yf; j++)
359            yspacing->InsertNextValue (j);
360         for (int k = zi; k <= zf; k++)
361            zspacing->InsertNextValue (k);
362      }
363
364      void vtkLSCEReader::CreateSimpleGrid (int xi, int xf,
365                                            int yi, int yf,
366                                            int zi, int zf,
367                                            vtkPoints   * points,
368                                            vtkIntArray * dimensions)
369      {
370         dimensions->InsertNextValue (xf - xi + 1); // X
371         dimensions->InsertNextValue (yf - yi + 1); // Y
372         dimensions->InsertNextValue (zf - zi + 1); // Z
373         for (int i = xi; i <= xf; i++)
374            for (int j = yi; j <= yf; j++)
375               for (int k = zi; k <= zf; k++)
376                  points->InsertNextPoint(i, j, k);
377      }
378
379      void vtkLSCEReader::CreateSimpleGrid (int xi, int xf,
380                                            int yi, int yf,
381                                            int zi, int zf,
382                                            vtkPoints    * points,
383                                            vtkCellArray * cells,
384                                            vtkIntArray  * dimensions)
385      {
386         this->CreateSimpleGrid (xi, xf, yi, yf, zi, zf, points, dimensions);
387         int dimX = dimensions->GetValue(0),
388             dimY = dimensions->GetValue(1),
389             dimZ = dimensions->GetValue(2);
390         this->GetRectilinearConnectivity(dimZ, dimY, dimX, cells);
391      }
392
393      //----------------------------------------------------------------
394      void vtkLSCEReader::GetSpacings(const vtkStdString & coordinate,
395                                      bool bounds, vtkFloatArray * spacing)
396      {
397         std::size_t size = (Reader->getDimensions(&coordinate).begin())->second;
398         ARRAY_CREATE(data_arr, float, 1, [1]);
399         if (Reader->hasVariable(coordinate))
400         {
401            if (bounds)
402            {
403               if (Reader->hasBounds(coordinate))
404               { // CellData, variable de coordonnée avec attributes bounds.
405                  vtkStdString boundid = Reader->getBoundsId(coordinate);
406                  float f;
407                  Reader->readData(*data_arr, boundid);
408                  spacing->InsertNextValue (f = (*data_arr)[0]);
409                  for (std::size_t j = 1; j < (2 * size); j++) // << Super laid
410                     if (f != (*data_arr)[j])
411                        spacing->InsertNextValue (f = (*data_arr)[j]);
412               }
413               else
414               { // CellData, variable de coordonnée sans attributes bounds.
415                  Reader->readData(*data_arr, coordinate);
416                  spacing->InsertNextValue ((*data_arr)[0] - ((*data_arr)[1] - (*data_arr)[0])/2);
417                  for (std::size_t j = 0; j < (size-1); j++)
418                     spacing->InsertNextValue ((*data_arr)[j] + ((*data_arr)[j+1]-(*data_arr)[j])/2);
419                  spacing->InsertNextValue ((*data_arr)[size-2] + ((*data_arr)[size-1] - (*data_arr)[size-2])/2);
420               }
421            }
422            else
423            { // PointData, variable de coordonnée.
424               Reader->readData(*data_arr, coordinate);
425               for (std::size_t j = 0; j < (size-1); j++)
426                  spacing->InsertNextValue ((*data_arr)[j]);
427            }
428         }
429         else
430         {
431            if (bounds) // CellData, dimension de coordonnée.
432               for (float i = -0.5; i < ((float)size + 1.); i = i + 1.)
433                  spacing->InsertNextValue (i);
434            else // PointData, dimension de coordonnée.
435               for (float j = 0.; j < (float)size; j = j + 1.)
436                  spacing->InsertNextValue (j);
437         }
438      }
439      //----------------------------------------------------------------
440
441      void vtkLSCEReader::AddPoint(vtkPoints * points, float *  value, bool proj)
442      {
443         if (proj)
444         {
445            double h = 1.0;
446            double cartesianCoord[3];
447            value[0] =  0.017453292519943295 * value[0];
448            value[1] =  0.017453292519943295 * value[1];
449
450            cartesianCoord[0] = h * cos(value[0]) * cos(value[1]);
451            cartesianCoord[1] = h * sin(value[0]) * cos(value[1]);
452            cartesianCoord[2] = h * sin(value[1]);
453            points->InsertNextPoint (cartesianCoord);
454         }
455         else
456         {
457            points->InsertNextPoint (value);
458         }
459      }
460
461      //----------------------------------------------------------------
462     
463      void vtkLSCEReader::GetDimensions(const vtkStdString & xcoordinate,
464                                        const vtkStdString & ycoordinate,
465                                        const vtkStdString & zcoordinate,
466                                        vtkIntArray * dimensions, bool bounds)
467      {
468         int a = (bounds) ? 1 : 0;
469         dimensions->InsertNextValue
470            ((Reader->getDimensions(&xcoordinate).begin())->second + a);
471         dimensions->InsertNextValue
472            ((Reader->getDimensions(&ycoordinate).begin())->second + a);
473         if (zcoordinate.size() > 0)
474         {
475            dimensions->InsertNextValue
476               ((Reader->getDimensions(&zcoordinate).begin())->second + a);
477         }
478         else
479         {
480            dimensions->InsertNextValue(1);
481         }
482      }
483     
484      void vtkLSCEReader::GetDimensions(const vtkStdString & xcoordinate,
485                                        const vtkStdString & zcoordinate,
486                                        vtkIntArray * dimensions, bool bounds)
487      {
488         int a = (bounds) ? 1 : 0;
489         dimensions->InsertNextValue
490            (Reader->getDimensions(&xcoordinate)[Reader->getLonCoordName(xcoordinate)] + a);
491         dimensions->InsertNextValue
492            (Reader->getDimensions(&xcoordinate)[Reader->getLatCoordName(xcoordinate)] + a);
493         if (zcoordinate.size() > 0)
494         {
495            dimensions->InsertNextValue
496               ((Reader->getDimensions(&zcoordinate).begin())->second + a);
497         }
498         else
499         {
500            dimensions->InsertNextValue(1);
501         }
502      }
503
504      //----------------------------------------------------------------
505      void vtkLSCEReader::GetPoints(const vtkStdString & xcoordinate,
506                                    const vtkStdString & ycoordinate,
507                                    const vtkStdString & zcoordinate,
508                                    bool bounds, bool proj,
509                                    vtkPoints * points, vtkIntArray * dimensions)
510      {
511         float value[3];
512         bool is2D = true;
513         ARRAY_CREATE(xvalue, float, 1, [1]);
514         ARRAY_CREATE(yvalue, float, 1, [1]);
515         vtkSmartPointer<vtkFloatArray> zspacing = vtkSmartPointer<vtkFloatArray>::New();
516         if (zcoordinate.size() > 0)
517         {
518            GetSpacings(zcoordinate, bounds, zspacing);
519            is2D = false;
520         }
521         else
522            zspacing->InsertNextValue(0);
523
524         dimensions->InsertNextValue
525            (Reader->getDimensions(&xcoordinate)[Reader->getLonCoordName(xcoordinate)]);
526         dimensions->InsertNextValue
527            (Reader->getDimensions(&xcoordinate)[Reader->getLatCoordName(xcoordinate)]);
528         dimensions->InsertNextValue(zspacing->GetNumberOfTuples());
529
530         if (!bounds)
531         {
532            Reader->readData(*xvalue, xcoordinate);
533            Reader->readData(*yvalue, ycoordinate);
534
535            for (int i = 0; i < dimensions->GetValue(0); i++)
536               for (int j = 0; j < dimensions->GetValue(1); j++)
537                  for (int k = 0; k < dimensions->GetValue(2); k++)
538                  {
539                     value[0] = (*xvalue)[i*dimensions->GetValue(1) + j];
540                     value[1] = (*yvalue)[i*dimensions->GetValue(1) + j];
541                     value[2] = zspacing->GetValue(k);
542                     AddPoint(points, value, proj);
543                  }
544            return;
545         }
546
547         dimensions->SetValue(0, dimensions->GetValue(0)+1);
548         dimensions->SetValue(1, dimensions->GetValue(1)+1);
549
550         if (Reader->hasBounds(xcoordinate))
551         {
552            vtkStdString boundid = Reader->getBoundsId(xcoordinate);
553            Reader->readData(*xvalue, boundid);
554         }
555         if (Reader->hasBounds(ycoordinate))
556         {
557            vtkStdString boundid = Reader->getBoundsId(ycoordinate);
558            Reader->readData(*yvalue, boundid);
559         }
560         int ydim = dimensions->GetValue(0) - 1 ,
561             xdim = dimensions->GetValue(1) - 1;
562
563         for (int i = 0; i <= xdim; i++)
564            for (int j = 0; j <= ydim; j++)
565               for (int k = 0; k < dimensions->GetValue(2); k++)
566               {
567                  if ((j != ydim) && (i != xdim))
568                  {
569                     value[0] = (*xvalue)[4 * (i * ydim + j) + 0];
570                     value[1] = (*yvalue)[4 * (i * ydim + j) + 0];
571                     value[2] = zspacing->GetValue(k);
572                     AddPoint(points, value, proj);
573
574                     continue;
575                  }
576                  else if ((j == ydim) && (i != xdim))
577                  {
578                     value[0] = (*xvalue)[4 * (i * ydim + j - 1) + 1];
579                     value[1] = (*yvalue)[4 * (i * ydim + j - 1) + 1];
580                     value[2] = zspacing->GetValue(k);
581                     AddPoint(points, value, proj);
582
583                     continue;
584                  }
585                  else if ((j != ydim) && (i == xdim))
586                  {
587                     value[0] = (*xvalue)[4 * ((i-1) * ydim + j) + 3];
588                     value[1] = (*yvalue)[4 * ((i-1) * ydim + j) + 3];
589                     value[2] = zspacing->GetValue(k);
590                     AddPoint(points, value, proj);
591
592                     continue;
593                  }
594                  else if ((j == ydim) && (i == xdim))
595                  {
596                     value[0] = (*xvalue)[4 * ((i-1) * ydim + j - 1) + 2];
597                     value[1] = (*yvalue)[4 * ((i-1) * ydim + j - 1) + 2];
598                     value[2] = zspacing->GetValue(k);
599                     AddPoint(points, value, proj);
600
601                     continue;
602                  }
603               }
604      }
605
606      //----------------------------------------------------------------
607
608      void vtkLSCEReader::GetCellsAndPoints(const vtkStdString & xcoordinate,
609                                            const vtkStdString & ycoordinate,
610                                            const vtkStdString & zcoordinate,
611                                            bool bounds, bool proj, std::size_t nbvertex,
612                                            vtkCellArray * cells, vtkPoints * points,
613                                            vtkIntArray * dimensions)
614      {
615         float value[3];
616         double h = 1.0;
617         double cartesianCoord[3];
618         vtkIdType pts[nbvertex] ;
619         bool is2D = true;
620         ARRAY_CREATE(xvalue, float, 1, [1]);
621         ARRAY_CREATE(yvalue, float, 1, [1]);
622         vtkSmartPointer<vtkFloatArray> zspacing = vtkSmartPointer<vtkFloatArray>::New();
623         if (zcoordinate.size() > 0)
624         {
625            GetSpacings(zcoordinate, bounds, zspacing);
626            is2D = false;
627         }
628         else
629            zspacing->InsertNextValue(0);
630
631         std::size_t size = (*Reader->getDimensions(&xcoordinate).begin()).second;
632         if (Reader->hasBounds(xcoordinate))
633         {
634            vtkStdString boundid = Reader->getBoundsId(xcoordinate);
635            Reader->readData(*xvalue, boundid);
636         }
637         if (Reader->hasBounds(ycoordinate))
638         {
639            vtkStdString boundid = Reader->getBoundsId(ycoordinate);
640            Reader->readData(*yvalue, boundid);
641         }
642
643         for (std::size_t u = 0; u < size; u++)
644         {
645            for (int v = 0; v < zspacing->GetNumberOfTuples(); v++)
646            {
647               for (std::size_t w = 0; w < nbvertex; w++)
648               {
649                  value[0] = (*xvalue)[nbvertex * u + w];
650                  value[1] = (*yvalue)[nbvertex * u + w];
651                  value[2] = zspacing->GetValue(v);
652                  pts[w] = nbvertex * u + w;
653
654                  if (proj)
655                  {
656                     value[0] = 0.017453292519943295 * value[0];
657                     value[1] = 0.017453292519943295 * value[1];
658
659                     cartesianCoord[0] = h * cos(value[0]) * cos(value[1]);
660                     cartesianCoord[1] = h * sin(value[0]) * cos(value[1]);
661                     cartesianCoord[2] = h * sin(value[1]);
662                     points->InsertPoint (pts[w], cartesianCoord);
663                  }
664                  else
665                  {
666                     points->InsertPoint (pts[w], value);
667                  }
668               }
669               cells->InsertNextCell(nbvertex,  pts);
670            }
671         }
672      }
673
674      //----------------------------------------------------------------
675
676      void vtkLSCEReader::AddScalarData(vtkDataSet * output,
677                                        const vtkStdString & varname,
678                                        std::size_t record, bool bounds)
679      {
680         vtkSmartPointer<vtkDoubleArray> data = vtkDoubleArray::New();
681         data->SetName(varname.c_str());
682         ARRAY_CREATE(data_arr, double, 1, [1]);
683         Reader->readData(*data_arr, varname);
684         Reader->replaceMissingValue(varname, *data_arr, vtkMath::Nan());
685         
686         
687         data->SetNumberOfValues (data_arr->size());
688         for (std::size_t i = 0; i < data_arr->size(); i++)
689            data->SetValue (i, (*data_arr)[i]);
690
691         if (bounds)
692         {
693            output->GetCellData()->AddArray(data);
694            output->GetCellData()->SetActiveScalars (varname.c_str());
695         }
696         else
697         {
698            output->GetPointData()->AddArray(data);
699            output->GetPointData()->SetActiveScalars (varname.c_str());
700         }
701      }
702
703      //----------------------------------------------------------------
704     
705      int vtkLSCEReader::RequestData
706                           (vtkInformation       *  vtkNotUsed(request),
707                            vtkInformationVector ** vtkNotUsed(inputVector),
708                            vtkInformationVector *  outputVector)
709      {
710         vtkInformation *  outInfo = outputVector->GetInformationObject(0);
711         vtkDataSet     *  output  = vtkDataSet::GetData(outInfo);
712         
713         if (!output)
714         {
715            switch (this->CurGridType)
716            {
717               case(RECTILINEAR)  :
718                  output = vtkRectilinearGrid::New();
719                  break;
720               case(CURVILINEAR)  :
721                  output = vtkStructuredGrid::New();
722                  break;
723               default :
724                  output = vtkUnstructuredGrid::New();
725                  break;
726            }
727            output->SetPipelineInformation(outInfo);
728            output->Delete();
729         }
730
731         vtkUnstructuredGrid * uoutput  = vtkUnstructuredGrid::SafeDownCast(output);
732         vtkRectilinearGrid  * routput  = vtkRectilinearGrid::SafeDownCast(output);
733         vtkStructuredGrid   * soutput  = vtkStructuredGrid::SafeDownCast(output);
734
735         if (routput && !this->VarNames.empty())
736         { // Grille rectiliniéaire
737            vtkSmartPointer<vtkFloatArray>
738                           xspacing = vtkSmartPointer<vtkFloatArray>::New(),
739                           yspacing = vtkSmartPointer<vtkFloatArray>::New(),
740                           zspacing = vtkSmartPointer<vtkFloatArray>::New();
741            vtkSmartPointer<vtkIntArray> dims = vtkSmartPointer<vtkIntArray>::New();
742
743            //CreateSimpleGrid (-1, 10, -1, 10, -1, 10, xspacing, yspacing, zspacing, dims);
744            GetSpacings(Reader->getLonCoordName(*VarNames.begin()), true, xspacing);
745            GetSpacings(Reader->getLatCoordName(*VarNames.begin()), true, yspacing);
746            GetSpacings(Reader->getVertCoordName(*VarNames.begin()), true, zspacing);
747            /*std::cout << zspacing->GetNumberOfTuples() << std::endl;*/
748
749            dims->InsertNextValue(xspacing->GetNumberOfTuples());
750            dims->InsertNextValue(yspacing->GetNumberOfTuples());
751            dims->InsertNextValue(zspacing->GetNumberOfTuples());
752
753            AddScalarData(routput, *VarNames.begin(), 0, true);
754            CreateRectilinearGrid(routput, outInfo, xspacing, yspacing, zspacing, dims);
755         }
756
757         if (soutput && !this->VarNames.empty())
758         { // Grille structurée
759             
760            vtkSmartPointer<vtkPoints>   pts  = vtkSmartPointer<vtkPoints>::New();
761            vtkSmartPointer<vtkIntArray> dims = vtkSmartPointer<vtkIntArray>::New();
762           
763            //CreateSimpleGrid (-1, 10, -1, 10, -1, 10, pts, dims);
764            GetPoints(Reader->getLonCoordName(*VarNames.begin()),
765                      Reader->getLatCoordName(*VarNames.begin()),
766                      Reader->getVertCoordName(*VarNames.begin()), true, true, pts, dims);
767
768            AddScalarData(soutput, *VarNames.begin(), 0, true);
769            CreateStructuredGrid(soutput, outInfo, pts, dims);
770         }
771
772         if (uoutput  && !this->VarNames.empty())
773         { // Grille non-structurée
774
775            vtkSmartPointer<vtkPoints>    pts   = vtkSmartPointer<vtkPoints>::New();
776            vtkSmartPointer<vtkIntArray>  dims  = vtkSmartPointer<vtkIntArray>::New();
777            vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
778            // VTK_QUAD, VTK_HEXAHEDRON, VTK_POLYGON
779            //CreateSimpleGrid (0, 12, 0, 12, 0, 12, pts, cells, dims);
780            //std::cout << Reader->getVertCoordName(*VarNames.begin()) << std::endl;
781           
782            std::cout << Reader->getNbVertex(*VarNames.begin()) << std::endl;
783           
784            GetCellsAndPoints(Reader->getLonCoordName(*VarNames.begin()),
785                              Reader->getLatCoordName(*VarNames.begin()),
786                              Reader->getVertCoordName(*VarNames.begin()), true, true,
787                              Reader->getNbVertex(*VarNames.begin()), cells, pts, dims);
788           
789            AddScalarData(uoutput, *VarNames.begin(), 0, true);
790            CreateUnstructuredGrid(uoutput, outInfo, pts, cells, VTK_POLYGON);
791
792
793         }
794         return (1);
795      }
796     
797      int vtkLSCEReader::RequestInformation
798                           (vtkInformation       *  vtkNotUsed(request),
799                            vtkInformationVector ** vtkNotUsed(inputVector),
800                            vtkInformationVector *  outputVector)
801      {
802         vtkInformation *  outInfo = outputVector->GetInformationObject(0);
803         vtkDataSet     *  output  = vtkDataSet::GetData(outInfo);
804
805         if (true)
806         {
807            switch (this->CurGridType)
808            {
809               case(RECTILINEAR)  :
810                  output = vtkRectilinearGrid::New();
811                  break;
812               case(CURVILINEAR)  :
813                  output = vtkStructuredGrid::New();
814                  break;
815               default :
816                  output = vtkUnstructuredGrid::New();
817                  break;
818            }
819            output->SetPipelineInformation(outInfo);
820            output->Delete();
821         }
822
823         vtkRectilinearGrid  * routput  = vtkRectilinearGrid::SafeDownCast(output);
824         vtkStructuredGrid   * soutput  = vtkStructuredGrid::SafeDownCast(output);
825         
826         if (routput && !this->VarNames.empty())
827         { // Grille rectiliniéaire
828            vtkSmartPointer<vtkIntArray> dims = vtkSmartPointer<vtkIntArray>::New();
829            this->GetDimensions(Reader->getLonCoordName(*VarNames.begin()),
830                                Reader->getLatCoordName(*VarNames.begin()),
831                                Reader->getVertCoordName(*VarNames.begin()),
832                                dims, true);
833           
834            int extent[6] = { 0, dims->GetValue(0) - 1
835                            , 0, dims->GetValue(1) - 1
836                            , 0, dims->GetValue(2) - 1};
837            outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),extent,6);
838         }
839         
840         if (soutput && !this->VarNames.empty())
841         { // Grille structurée
842           
843            vtkSmartPointer<vtkIntArray> dims = vtkSmartPointer<vtkIntArray>::New();
844            this->GetDimensions(Reader->getLonCoordName(*VarNames.begin()),
845                                Reader->getVertCoordName(*VarNames.begin()),
846                                dims, true);
847           
848            int extent[6] = { 0, dims->GetValue(0) - 1
849                            , 0, dims->GetValue(1) - 1
850                            , 0, dims->GetValue(2) - 1};
851            outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),extent,6);
852         }
853           
854         return (1);
855      }
856
857      ///---------------------------------------------------------------
858
859#ifndef LSCE_EXPORTS
860   } // namespace vtk
861} // namespace xmlioserver
862#endif //LSCE_EXPORTS
Note: See TracBrowser for help on using the repository browser.