GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
vtkPolyDataMySQLTrackReader.cxx
Go to the documentation of this file.
1 /*=========================================================================
2  Authors: The GoFigure Dev. Team.
3  at Megason Lab, Systems biology, Harvard Medical school, 2009-11
4 
5  Copyright (c) 2009-11, President and Fellows of Harvard College.
6  All rights reserved.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10 
11  Redistributions of source code must retain the above copyright notice,
12  this list of conditions and the following disclaimer.
13  Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16  Neither the name of the President and Fellows of Harvard College
17  nor the names of its contributors may be used to endorse or promote
18  products derived from this software without specific prior written
19  permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 =========================================================================*/
34 
36 
37 #include "vtkObjectFactory.h"
38 #include "vtkPolyLine.h"
39 #include "vtkCellArray.h"
40 #include "vtkPolyData.h"
41 #include "vtkIntArray.h"
42 #include "vtkDoubleArray.h"
43 #include "vtkPointData.h"
44 
45 #include <sstream>
46 
49 
50 //--------------------------------------------------------------------------
52 {
53 }
54 
55 //--------------------------------------------------------------------------
56 
57 //--------------------------------------------------------------------------
60 {
61 }
62 
63 //--------------------------------------------------------------------------
64 
65 //--------------------------------------------------------------------------
66 vtkSmartPointer< vtkPolyData >
67 vtkPolyDataMySQLTrackReader::GetPolyData(const std::string & iString)
68 {
69  std::stringstream str(iString);
70 
71  // Might not be useful
72  vtkIdType N;
73 
74  str >> N;
75 
76  if ( N != 0 )
77  {
78  std::map< unsigned int, double * > orderedPoints = GetMap(iString);
79 
80  vtkSmartPointer< vtkIntArray > temporalArray = vtkSmartPointer< vtkIntArray >::New();
81  temporalArray->SetNumberOfComponents(1);
82  temporalArray->SetName("TemporalInformation");
83 
84  // read map and fill points
85  vtkSmartPointer< vtkPoints > points = vtkSmartPointer< vtkPoints >::New();
86  std::map< unsigned int, double * >::iterator it = orderedPoints.begin();
87 
88  while ( it != orderedPoints.end() )
89  {
90  temporalArray->InsertNextValue(it->first);
91  points->InsertNextPoint(it->second);
92  ++it;
93  }
94 
95  // Clean the map
96  it = orderedPoints.begin();
97  while ( it != orderedPoints.end() )
98  {
99  delete[] it->second;
100  ++it;
101  }
102  orderedPoints.clear();
103 
104  // Create a line from points
105  vtkSmartPointer< vtkPolyLine > polyLine =
106  vtkSmartPointer< vtkPolyLine >::New();
107 
108  polyLine->GetPointIds()->SetNumberOfIds( points->GetNumberOfPoints() );
109  for ( vtkIdType i = 0; i < points->GetNumberOfPoints(); i++ )
110  {
111  polyLine->GetPointIds()->SetId(i, i);
112  }
113 
114  //Create a cell array to store the lines in and add the lines to it
115  vtkSmartPointer< vtkCellArray > cells =
116  vtkSmartPointer< vtkCellArray >::New();
117  cells->InsertNextCell(polyLine);
118 
119  //Create a polydata to store everything in
120  vtkSmartPointer< vtkPolyData > polyData =
121  vtkSmartPointer< vtkPolyData >::New();
122 
123  //add the points to the dataset
124  polyData->SetPoints(points);
125 
126  //add the lines to the dataset
127  polyData->SetLines(cells);
128 
129  //add the temporal information
130  polyData->GetPointData()->AddArray(temporalArray);
131 
132  vtkSmartPointer< vtkDoubleArray > speedArray =
133  vtkSmartPointer< vtkDoubleArray >::New();
134  speedArray->SetNumberOfComponents(1);
135  speedArray->SetName("SpeedInformation");
136  polyData->GetPointData()->AddArray(speedArray);
137 
138  return polyData;
139  }
140 
141  return NULL;
142 }
143 
144 //--------------------------------------------------------------------------
145 
146 //--------------------------------------------------------------------------
147 std::map< unsigned int, double * >
148 vtkPolyDataMySQLTrackReader::GetMap(const std::string & iString)
149 {
150  std::stringstream str(iString);
151 
152  std::map< unsigned int, double * > orderedPoints;
153 
154  // Might not be useful
155  vtkIdType N;
156  str >> N;
157 
158  if ( N != 0 )
159  {
160  double * pt = NULL;
161  unsigned int time = 0;
162 
163  // fill a map so the points will be ordered automatically
164  for ( vtkIdType i = 0; i < N; i++ )
165  {
166  pt = new double[3];
167  str >> pt[0] >> pt[1] >> pt[2];
168  str >> time;
169  orderedPoints.insert( std::pair< unsigned int, double * >(time, pt) );
170  }
171  }
172  return orderedPoints;
173 }
vtkSmartPointer< vtkPolyData > GetPolyData(const std::string &iString)
Reads a string and convert it into a track polydata.
vtkCxxRevisionMacro(vtkPolyDataMySQLTrackReader,"$Revision$")
std::map< unsigned int, double * > GetMap(const std::string &iString)
vtkStandardNewMacro(vtkPolyDataMySQLTrackReader)