#include "anglePreserveCheck.h" int main( int argc, char * argv [] ) { if( argc < 2 ) { std::cerr << "Missing arguments" << std::endl; std::cerr << "Usage: originMesh.vtk resultMesh.vtk" << std::endl; return -1; } vtkPolyData *polyData1 = readDataToPolyData( argv[1] ); vtkPolyData *polyData2 = readDataToPolyData( argv[2] ); //Begin convert from vtkPolyData to ITKMesh MeshType::Pointer mesh1 = vtkPolyDataToITKMesh(polyData1); MeshType::Pointer mesh2 = vtkPolyDataToITKMesh(polyData2); // vnl_sparse_matrix D(mesh->GetNumberOfPoints(), // mesh->GetNumberOfPoints()); // vnl_vector bR(mesh->GetNumberOfPoints(), 0); // vnl_vector bI(mesh->GetNumberOfPoints(), 0); anglePreserveCheck( mesh1, mesh2 ); // std::cerr<<(clock() - time)/CLOCKS_PER_SEC<SetPoints(points); points->Delete(); //Copy all cells into the vtkPolyData structure //Creat vtkCellArray into which the cells are copied vtkCellArray* triangle = vtkCellArray::New(); CellIterator cellIt = mesh->GetCells()->Begin(); CellIterator cellItEnd = mesh->GetCells()->End(); for (int it = 0; cellIt != cellItEnd; ++it, ++cellIt) { CellType * cellptr = cellIt.Value(); // LineType * line = dynamic_cast( cellptr ); // std::cout << line->GetNumberOfPoints() << std::endl; // std::cout << cellptr->GetNumberOfPoints() << std::endl; PointIdIterator pntIdIter = cellptr->PointIdsBegin(); PointIdIterator pntIdEnd = cellptr->PointIdsEnd(); vtkIdList* pts = vtkIdList::New(); for (int it1 = 0; pntIdIter != pntIdEnd; ++it1, ++pntIdIter) { pts->InsertNextId( *pntIdIter ); // std::cout<<" "<InsertNextCell(pts); } newPolyData->SetPolys(triangle); triangle->Delete(); // return the vtkUnstructuredGrid return newPolyData; } void Display(vtkPolyData* polyData) { vtkPolyDataMapper* mapper = vtkPolyDataMapper::New(); mapper->SetInput(polyData); vtkActor* actor = vtkActor::New(); actor->SetMapper(mapper); // vtkCamera *camera = vtkCamera::New(); // camera->SetPosition(1,1,1); // camera->SetFocalPoint(0,0,0); vtkRenderer* ren = vtkRenderer::New(); ren->AddActor(actor); //ren->SetActiveCamera(camera); //ren->ResetCamera(); ren->SetBackground(1,1,1); vtkRenderWindow* renWin = vtkRenderWindow::New(); renWin->AddRenderer(ren); renWin->SetSize(300,300); vtkRenderWindowInteractor* inter = vtkRenderWindowInteractor::New(); inter->SetRenderWindow(renWin); renWin->Render(); inter->Start(); } void anglePreserveCheck(MeshType::Pointer mesh1, MeshType::Pointer mesh2) { std::cerr<<"Checking number of nodes..."; if ( mesh1->GetNumberOfPoints() == mesh2->GetNumberOfPoints() ) { std::cerr<<"Same!"<GetNumberOfCells() == mesh2->GetNumberOfCells() ) { std::cerr<<"Same!"<GetNumberOfPoints(); int numOfCells = mesh1->GetNumberOfCells(); // 1. store the points coordinates: pointXYZ std::vector< std::vector > pointXYZ1( numOfPoints, std::vector(3, 0) ); std::vector< std::vector > pointXYZ2( numOfPoints, std::vector(3, 0) ); PointIterator pntIterator1 = mesh1->GetPoints()->Begin(); PointIterator pntIterator2 = mesh2->GetPoints()->Begin(); for ( int it = 0; it < numOfPoints; ++it, ++pntIterator1, ++pntIterator2) { ItkPoint pnt1 = pntIterator1.Value(); ItkPoint pnt2 = pntIterator2.Value(); pointXYZ1[it][0] = pnt1[0]; pointXYZ1[it][1] = pnt1[1]; pointXYZ1[it][2] = pnt1[2]; pointXYZ2[it][0] = pnt2[0]; pointXYZ2[it][1] = pnt2[1]; pointXYZ2[it][2] = pnt2[2]; } // 2. store the relationship from point to cell, i.e. for each // point, which cells contain it? For each point in the mesh, // generate a vector, storing the id number of triangles containing // this point. The vectors of all the points form a vector: // pointCell // 3. store the relationship from cell to point, i.e. for each cell, // which points does it contains? store in vector: cellPoint std::vector< std::vector > pointCell( numOfPoints ); std::vector< std::vector > cellPoint( numOfCells, std::vector(3, 0) ); CellIterator cellIt = mesh1->GetCells()->Begin(); for ( int itCell = 0; itCell < numOfCells; ++itCell, ++cellIt) { CellType * cellptr = cellIt.Value(); // cellptr will point to each cell in the mesh // std::cout << cellptr->GetNumberOfPoints() << std::endl; PointIdIterator pntIdIter = cellptr->PointIdsBegin(); //pntIdIter will point to each point in the current cell PointIdIterator pntIdEnd = cellptr->PointIdsEnd(); for (int itPntInCell = 0; pntIdIter != pntIdEnd; ++pntIdIter, ++itPntInCell) { pointCell[ *pntIdIter ].push_back(itCell); cellPoint[itCell][itPntInCell] = *pntIdIter; } } std::vector< std::vector > angleRatioDiff( numOfPoints ); // Each line of angleRatioDiff vector corresponds to each vertex in the mesh. // Since there are several angles emanating from this vertex, say, N angles from one given vertex. // We have N-1 ratios: angle_(i)/angle_(i-1) i=1, ..., N // The angle preserving means that those ratios are preserved during the mapping. ofstream myfile; myfile.open ("angleRatioDiff.dat"); // This file will store the angleRatioDiff vector. for ( int itPnt = 0; itPnt < numOfPoints; ++itPnt) { std::vector< int > cellsOfThisPoint = pointCell[ itPnt ]; // Then, iterate in this list, comput each angle emanating from this point. std::vector< double > anglesFromThisPoint1; std::vector< double > anglesFromThisPoint2; // This is to store angles emanating from this point. Its size is same as cellsOfThisPoint std::vector< double > anglesRatiosFromThisPoint1; std::vector< double > anglesRatiosFromThisPoint2; // Store the ratio of successive angles. Its size is size of cellsOfThisPoint-1 std::vector< int >::iterator itCellsOfThisPoint = cellsOfThisPoint.begin(); std::vector< int >::iterator itCellsOfThisPointEnd = cellsOfThisPoint.end(); for (; itCellsOfThisPoint != itCellsOfThisPointEnd; ++itCellsOfThisPoint) { // Current cell is triangle ABC, fix A as the current vertex. int pointIdA = itPnt; int pointIdB; if ( itPnt!= cellPoint[ *itCellsOfThisPoint ][0] ) { pointIdB = cellPoint[ *itCellsOfThisPoint ][0]; } else { pointIdB = cellPoint[ *itCellsOfThisPoint ][1]; } int pointIdC; if ( itPnt!= cellPoint[ *itCellsOfThisPoint ][2] ) { pointIdC = cellPoint[ *itCellsOfThisPoint ][2]; } else { pointIdC = cellPoint[ *itCellsOfThisPoint ][1]; } double Ax1 = pointXYZ1[ pointIdA ][0]; double Ay1 = pointXYZ1[ pointIdA ][1]; double Az1 = pointXYZ1[ pointIdA ][2]; double Bx1 = pointXYZ1[ pointIdB ][0]; double By1 = pointXYZ1[ pointIdB ][1]; double Bz1 = pointXYZ1[ pointIdB ][2]; double Cx1 = pointXYZ1[ pointIdC ][0]; double Cy1 = pointXYZ1[ pointIdC ][1]; double Cz1 = pointXYZ1[ pointIdC ][2]; double lAB1 = sqrt( (Ax1 - Bx1)*(Ax1 - Bx1) + (Ay1 - By1)*(Ay1 - By1) + (Az1 - Bz1)*(Az1 - Bz1) ); double lAC1 = sqrt( (Ax1 - Cx1)*(Ax1 - Cx1) + (Ay1 - Cy1)*(Ay1 - Cy1) + (Az1 - Cz1)*(Az1 - Cz1) ); double lBC1 = sqrt( (Cx1 - Bx1)*(Cx1 - Bx1) + (Cy1 - By1)*(Cy1 - By1) + (Cz1 - Bz1)*(Cz1 - Bz1) ); double Ax2 = pointXYZ2[ pointIdA ][0]; double Ay2 = pointXYZ2[ pointIdA ][1]; double Az2 = pointXYZ2[ pointIdA ][2]; double Bx2 = pointXYZ2[ pointIdB ][0]; double By2 = pointXYZ2[ pointIdB ][1]; double Bz2 = pointXYZ2[ pointIdB ][2]; double Cx2 = pointXYZ2[ pointIdC ][0]; double Cy2 = pointXYZ2[ pointIdC ][1]; double Cz2 = pointXYZ2[ pointIdC ][2]; double lAB2 = sqrt( (Ax2 - Bx2)*(Ax2 - Bx2) + (Ay2 - By2)*(Ay2 - By2) + (Az2 - Bz2)*(Az2 - Bz2) ); double lAC2 = sqrt( (Ax2 - Cx2)*(Ax2 - Cx2) + (Ay2 - Cy2)*(Ay2 - Cy2) + (Az2 - Cz2)*(Az2 - Cz2) ); double lBC2 = sqrt( (Cx2 - Bx2)*(Cx2 - Bx2) + (Cy2 - By2)*(Cy2 - By2) + (Cz2 - Bz2)*(Cz2 - Bz2) ); // Now we know angleA is what we want. It can be obtained by cos law. double angleA1 = acos( (lAB1*lAB1 + lAC1*lAC1 - lBC1*lBC1)/(2*lAB1*lAC1) ); double angleA2 = acos( (lAB2*lAB2 + lAC2*lAC2 - lBC2*lBC2)/(2*lAB2*lAC2) ); anglesFromThisPoint1.push_back(180*angleA1/3.1415926); anglesFromThisPoint2.push_back(180*angleA2/3.1415926); } // for itCellsOfThisPoint, traverse all cells connected with current vertex. // std::cerr<< anglesFromThisPoint1.size()<::iterator anglesFromThisPointIt1 = anglesFromThisPoint1.begin(); std::vector< double >::iterator anglesFromThisPointIt2 = anglesFromThisPoint2.begin(); std::vector< double >::iterator anglesFromThisPointItEnd1 = anglesFromThisPoint1.end(); // // std::vector< double >::iterator anglesRatiosFromThisPointIt1 = anglesRatiosFromThisPoint1.begin(); // std::vector< double >::iterator anglesRatiosFromThisPointIt2 = anglesRatiosFromThisPoint2.begin(); for (; anglesFromThisPointIt1 != anglesFromThisPointItEnd1; ++anglesFromThisPointIt1, ++anglesFromThisPointIt2) { anglesRatiosFromThisPoint1.push_back(*anglesFromThisPointIt1/sumAngle1); anglesRatiosFromThisPoint2.push_back(*anglesFromThisPointIt2/sumAngle2); myfile << (*anglesFromThisPointIt1/sumAngle1)/(*anglesFromThisPointIt2/sumAngle2) <<" "; //<< lBC1/lBC2 <<" "<< lAC1/lAC2 << std::endl; } myfile << std::endl; // std::cerr<<"In cell "<