26 #include <igtl_util.h> 27 #include <igtlPolyDataMessage.h> 30 #include <vtkPolyData.h> 31 #include <vtkIntArray.h> 32 #include <vtkMatrix4x4.h> 33 #include <vtkObjectFactory.h> 34 #include <vtkSmartPointer.h> 35 #include <vtkVertex.h> 36 #include <vtkCellArray.h> 37 #include <vtkPolyLine.h> 38 #include <vtkPolygon.h> 39 #include <vtkTriangleStrip.h> 40 #include <vtkFloatArray.h> 41 #include <vtkDataSetAttributes.h> 42 #include <vtkPointData.h> 43 #include <vtkCellData.h> 56 igtl::PolyDataMessage::Pointer retval = igtl::PolyDataMessage::New();
62 polyData = this->encodeCoordinateSystem(in, externalSpace);
63 this->encode_vtkPolyData(polyData, retval);
71 polyData = this->decodeCoordinateSystem(polyData, externalSpace);
73 QString deviceName = in->GetDeviceName();
76 retval->setVtkPolyData(polyData);
77 retval->setAcquisitionTime(timestamp);
91 vtkPolyDataPtr IGTLinkConversionPolyData::encodeCoordinateSystem(
MeshPtr mesh, PATIENT_COORDINATE_SYSTEM externalSpace)
101 vtkPolyDataPtr IGTLinkConversionPolyData::decode_vtkPolyData(igtl::PolyDataMessage* msg)
106 igtl::PolyDataMessage* polyDataMsg = msg;
108 vtkSmartPointer<vtkPolyData> poly = vtkSmartPointer<vtkPolyData>::New();
111 igtl::PolyDataPointArray::Pointer pointsArray = polyDataMsg->GetPoints();
112 int npoints = pointsArray->GetNumberOfPoints();
115 vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
116 for (
int i = 0; i < npoints; i ++)
118 igtlFloat32 point[3];
119 pointsArray->GetPoint(i, point);
120 points->InsertNextPoint(point);
122 poly->SetPoints(points);
130 igtl::PolyDataCellArray::Pointer verticesArray = polyDataMsg->GetVertices();
131 int nvertices = verticesArray.IsNotNull() ? verticesArray->GetNumberOfCells() : 0;
134 vtkSmartPointer<vtkCellArray> vertCells = vtkSmartPointer<vtkCellArray>::New();
135 for (
int i = 0; i < nvertices; i ++)
137 vtkSmartPointer<vtkVertex> vertex = vtkSmartPointer<vtkVertex>::New();
139 std::list<igtlUint32> cell;
140 verticesArray->GetCell(i, cell);
143 std::list<igtlUint32>::iterator iter;
145 vertex->GetPointIds()->SetId(i, *iter);
147 vertCells->InsertNextCell(vertex);
149 poly->SetVerts(vertCells);
153 igtl::PolyDataCellArray::Pointer linesArray = polyDataMsg->GetLines();
154 int nlines = linesArray.IsNotNull() ? linesArray->GetNumberOfCells() : 0;
157 vtkSmartPointer<vtkCellArray> lineCells = vtkSmartPointer<vtkCellArray>::New();
158 for(
int i = 0; i < nlines; i++)
160 vtkSmartPointer<vtkPolyLine> polyLine = vtkSmartPointer<vtkPolyLine>::New();
162 std::list<igtlUint32> cell;
163 linesArray->GetCell(i, cell);
164 polyLine->GetPointIds()->SetNumberOfIds(cell.size());
165 std::list<igtlUint32>::iterator iter;
167 for (iter = cell.begin(); iter != cell.end(); iter ++)
169 polyLine->GetPointIds()->SetId(j, *iter);
172 lineCells->InsertNextCell(polyLine);
174 poly->SetLines(lineCells);
178 igtl::PolyDataCellArray::Pointer polygonsArray = polyDataMsg->GetPolygons();
179 int npolygons =polygonsArray.IsNotNull() ? polygonsArray->GetNumberOfCells() : 0;
182 vtkSmartPointer<vtkCellArray> polygonCells = vtkSmartPointer<vtkCellArray>::New();
183 for(
int i = 0; i < npolygons; i++)
185 vtkSmartPointer<vtkPolygon> polygon = vtkSmartPointer<vtkPolygon>::New();
187 std::list<igtlUint32> cell;
188 polygonsArray->GetCell(i, cell);
189 polygon->GetPointIds()->SetNumberOfIds(cell.size());
190 std::list<igtlUint32>::iterator iter;
192 for (iter = cell.begin(); iter != cell.end(); iter ++)
194 polygon->GetPointIds()->SetId(j, *iter);
197 polygonCells->InsertNextCell(polygon);
199 poly->SetPolys(polygonCells);
203 igtl::PolyDataCellArray::Pointer triangleStripsArray = polyDataMsg->GetTriangleStrips();
204 int ntstrips = triangleStripsArray.IsNotNull() ? triangleStripsArray->GetNumberOfCells() : 0;
207 vtkSmartPointer<vtkCellArray> tstripCells = vtkSmartPointer<vtkCellArray>::New();
208 for(
int i = 0; i < ntstrips; i++)
210 vtkSmartPointer<vtkTriangleStrip> tstrip = vtkSmartPointer<vtkTriangleStrip>::New();
212 std::list<igtlUint32> cell;
213 triangleStripsArray->GetCell(i, cell);
214 tstrip->GetPointIds()->SetNumberOfIds(cell.size());
215 std::list<igtlUint32>::iterator iter;
217 for (iter = cell.begin(); iter != cell.end(); iter ++)
219 tstrip->GetPointIds()->SetId(j, *iter);
222 tstripCells->InsertNextCell(tstrip);
224 poly->SetStrips(tstripCells);
228 int nAttributes = polyDataMsg->GetNumberOfAttributes();
229 for (
int i = 0; i < nAttributes; i ++)
231 igtl::PolyDataAttribute::Pointer attribute;
232 attribute = polyDataMsg->GetAttribute(i);
234 vtkSmartPointer<vtkFloatArray> data =
235 vtkSmartPointer<vtkFloatArray>::New();
237 data->SetName(attribute->GetName());
238 int n = attribute->GetSize();
246 switch (attribute->GetType() & 0x0F)
248 case igtl::PolyDataAttribute::POINT_SCALAR:
250 data->SetNumberOfComponents(1);
253 case igtl::PolyDataAttribute::POINT_VECTOR:
254 case igtl::PolyDataAttribute::POINT_NORMAL:
256 data->SetNumberOfComponents(3);
259 case igtl::PolyDataAttribute::POINT_TENSOR:
261 data->SetNumberOfComponents(9);
264 case igtl::PolyDataAttribute::POINT_RGBA:
266 data->SetNumberOfComponents(4);
275 data->SetNumberOfTuples(n);
276 attribute->GetData(static_cast<igtl_float32*>(data->GetPointer(0)));
278 if ((attribute->GetType() & 0xF0) == 0)
280 poly->GetPointData()->AddArray(data);
284 poly->GetCellData()->AddArray(data);
293 void IGTLinkConversionPolyData::encode_vtkPolyData(
vtkPolyDataPtr in, igtl::PolyDataMessage* outMsg)
298 vtkSmartPointer<vtkPolyData> poly = in;
301 vtkSmartPointer<vtkPoints> points = poly->GetPoints();
302 if (points.GetPointer() != NULL)
304 int npoints = points->GetNumberOfPoints();
307 igtl::PolyDataPointArray::Pointer pointArray = igtl::PolyDataPointArray::New();
308 for (
int i = 0; i < npoints; i ++)
310 double *p = points->GetPoint(i);
311 pointArray->AddPoint(static_cast<igtlFloat32>(p[0]),
312 static_cast<igtlFloat32>(p[1]),
313 static_cast<igtlFloat32>(p[2]));
315 outMsg->SetPoints(pointArray);
320 vtkSmartPointer<vtkCellArray> vertCells = poly->GetVerts();
321 if (vertCells.GetPointer() != NULL)
323 igtl::PolyDataCellArray::Pointer verticesArray = igtl::PolyDataCellArray::New();
324 if (this->VTKToIGTLCellArray(vertCells, verticesArray) > 0)
326 outMsg->SetVertices(verticesArray);
331 vtkSmartPointer<vtkCellArray> lineCells = poly->GetLines();
332 if (lineCells.GetPointer() != NULL)
334 igtl::PolyDataCellArray::Pointer linesArray = igtl::PolyDataCellArray::New();
335 if (this->VTKToIGTLCellArray(lineCells, linesArray) > 0)
337 outMsg->SetLines(linesArray);
342 vtkSmartPointer<vtkCellArray> polygonCells = poly->GetPolys();
343 if (polygonCells.GetPointer() != NULL)
345 igtl::PolyDataCellArray::Pointer polygonsArray = igtl::PolyDataCellArray::New();
346 if (this->VTKToIGTLCellArray(polygonCells, polygonsArray) > 0)
348 outMsg->SetPolygons(polygonsArray);
353 vtkSmartPointer<vtkCellArray> triangleStripCells = poly->GetStrips();
354 if (triangleStripCells.GetPointer() != NULL)
356 igtl::PolyDataCellArray::Pointer triangleStripsArray = igtl::PolyDataCellArray::New();
357 if (this->VTKToIGTLCellArray(triangleStripCells, triangleStripsArray) > 0)
359 outMsg->SetTriangleStrips(triangleStripsArray);
364 vtkSmartPointer<vtkPointData> pdata = poly->GetPointData();
365 int nPointAttributes = pdata->GetNumberOfArrays();
366 if (nPointAttributes > 0)
368 for (
int i = 0; i < nPointAttributes; i ++)
370 igtl::PolyDataAttribute::Pointer attribute = igtl::PolyDataAttribute::New();
371 if (this->VTKToIGTLAttribute(pdata, i, attribute) > 0)
373 outMsg->AddAttribute(attribute);
380 vtkSmartPointer<vtkCellData> cdata = poly->GetCellData();
381 int nCellAttributes = cdata->GetNumberOfArrays();
382 if (nCellAttributes > 0)
384 for (
int i = 0; i < nCellAttributes; i ++)
386 igtl::PolyDataAttribute::Pointer attribute = igtl::PolyDataAttribute::New();
387 if (this->VTKToIGTLAttribute(cdata, i, attribute) > 0)
389 outMsg->AddAttribute(attribute);
396 int IGTLinkConversionPolyData::VTKToIGTLCellArray(vtkCellArray* src, igtl::PolyDataCellArray* dest)
400 int ncells = src->GetNumberOfCells();
403 vtkSmartPointer<vtkIdList> idList = vtkSmartPointer<vtkIdList>::New();
404 src->InitTraversal();
405 while (src->GetNextCell(idList))
407 std::list<igtlUint32> cell;
408 int nIds = idList->GetNumberOfIds();
409 for (
int i = 0; i < nIds; i ++)
411 cell.push_back(idList->GetId(i));
427 int IGTLinkConversionPolyData::VTKToIGTLAttribute(vtkDataSetAttributes* src,
int i, igtl::PolyDataAttribute* dest)
431 if ((!src) || (!dest))
437 if (i < 0 || i >= src->GetNumberOfArrays())
447 if (src->IsTypeOf(
"vtkCellData"))
456 vtkSmartPointer<vtkDataArray> array = src->GetArray(i);
457 int ncomps = array->GetNumberOfComponents();
460 dest->SetType(igtl::PolyDataAttribute::POINT_SCALAR | attrTypeBit);
462 else if (ncomps == 3)
465 dest->SetType(igtl::PolyDataAttribute::POINT_NORMAL | attrTypeBit);
467 else if (ncomps == 9)
469 dest->SetType(igtl::PolyDataAttribute::POINT_TENSOR | attrTypeBit);
471 else if (ncomps == 4)
473 dest->SetType(igtl::PolyDataAttribute::POINT_RGBA | attrTypeBit);
475 dest->SetName((array->GetName() ? array->GetName() :
""));
476 int ntuples = array->GetNumberOfTuples();
477 dest->SetSize(ntuples);
479 for (
int j = 0; j < ntuples; j ++)
481 double * tuple = array->GetTuple(j);
483 for (
int k = 0; k < ncomps; k ++)
485 data[k] =
static_cast<igtlFloat32
>(tuple[k]);
487 dest->SetNthData(j, data);
QDateTime decode_timestamp(igtl::MessageBase *msg)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
cxResource_EXPORT Transform3D createTransformFromReferenceToExternal(PATIENT_COORDINATE_SYSTEM external)
cstring_cast_Placeholder cstring_cast(const T &val)
vtkSmartPointer< vtkPolyData > vtkPolyDataPtr
MeshPtr decode(igtl::PolyDataMessage *in, PATIENT_COORDINATE_SYSTEM externalSpace)
igtl::PolyDataMessage::Pointer encode(MeshPtr in, PATIENT_COORDINATE_SYSTEM externalSpace)
void encode_timestamp(QDateTime ts, igtl::MessageBase *msg)
boost::shared_ptr< class Mesh > MeshPtr
Namespace for all CustusX production code.