void operator()( HDS& hds) { CGAL::Polyhedron_incremental_builder_3<HDS> B( hds, true); //start surface, number of halfedges is unknown (at least to me) B.begin_surface( vtkMesh->GetNumberOfVerts(),vtkMesh->GetNumberOfPolys(),0); typedef typename HDS::Vertex Vertex; typedef typename Vertex::Point Point; //add vertices to polyhedron for(vtkIdType i = 0; i < vtkMesh->GetNumberOfPoints(); i++) { double p[3]; vtkMesh->GetPoint(i,p); B.add_vertex(Point(p[0],p[1],p[2])); } //add faces to polyhedron vtkIdType npts, *pts; vtkMesh->GetPolys()->InitTraversal(); while(vtkMesh->GetPolys()->GetNextCell(npts,pts)) { B.begin_facet(); B.add_vertex_to_facet(pts[0]); B.add_vertex_to_facet(pts[1]); B.add_vertex_to_facet(pts[2]); B.end_facet(); } B.end_surface(); }
bool mitk::Equal(vtkPolyData &leftHandSide, vtkPolyData &rightHandSide, mitk::ScalarType eps, bool verbose) { bool noDifferenceFound = true; if (!mitk::Equal(leftHandSide.GetNumberOfCells(), rightHandSide.GetNumberOfCells(), eps, verbose)) { if (verbose) MITK_INFO << "[Equal( vtkPolyData*, vtkPolyData* )] Number of cells not equal"; noDifferenceFound = false; } if (!mitk::Equal(leftHandSide.GetNumberOfVerts(), rightHandSide.GetNumberOfVerts(), eps, verbose)) { if (verbose) MITK_INFO << "[Equal( vtkPolyData*, vtkPolyData* )] Number of vertices not equal"; noDifferenceFound = false; } if (!mitk::Equal(leftHandSide.GetNumberOfLines(), rightHandSide.GetNumberOfLines(), eps, verbose)) { if (verbose) MITK_INFO << "[Equal( vtkPolyData*, vtkPolyData* )] Number of lines not equal"; noDifferenceFound = false; } if (!mitk::Equal(leftHandSide.GetNumberOfPolys(), rightHandSide.GetNumberOfPolys(), eps, verbose)) { if (verbose) MITK_INFO << "[Equal( vtkPolyData*, vtkPolyData* )] Number of polys not equal"; noDifferenceFound = false; } if (!mitk::Equal(leftHandSide.GetNumberOfStrips(), rightHandSide.GetNumberOfStrips(), eps, verbose)) { if (verbose) MITK_INFO << "[Equal( vtkPolyData*, vtkPolyData* )] Number of strips not equal"; noDifferenceFound = false; } { unsigned int numberOfPointsRight = rightHandSide.GetPoints()->GetNumberOfPoints(); unsigned int numberOfPointsLeft = leftHandSide.GetPoints()->GetNumberOfPoints(); if (!mitk::Equal(numberOfPointsLeft, numberOfPointsRight, eps, verbose)) { if (verbose) MITK_INFO << "[Equal( vtkPolyData*, vtkPolyData* )] Number of points not equal"; noDifferenceFound = false; } else { for (unsigned int i(0); i < numberOfPointsRight; i++) { bool pointFound = false; double pointOne[3]; rightHandSide.GetPoints()->GetPoint(i, pointOne); for (unsigned int j(0); j < numberOfPointsLeft; j++) { double pointTwo[3]; leftHandSide.GetPoints()->GetPoint(j, pointTwo); double x = pointOne[0] - pointTwo[0]; double y = pointOne[1] - pointTwo[1]; double z = pointOne[2] - pointTwo[2]; double distance = x * x + y * y + z * z; if (distance < eps) { pointFound = true; break; } } if (!pointFound) { if (verbose) { MITK_INFO << "[Equal( vtkPolyData*, vtkPolyData* )] Right hand side point with id " << i << " and coordinates ( " << std::setprecision(12) << pointOne[0] << " ; " << pointOne[1] << " ; " << pointOne[2] << " ) could not be found in left hand side with epsilon " << eps << "."; } noDifferenceFound = false; break; } } } } return noDifferenceFound; }
void vtkHighlightActor2D::RegenerateHighlight() { // Count the number of hot points that we need to display. int i, numHotPoints = 0; for(i = 0; i < numTools; ++i) { if(tools[i]->IsEnabled()) numHotPoints += (int)tools[i]->HotPoints().size(); } // Allocate some temporary arrays that we'll use to construct the // highlightData object. int numPts = 4 * numHotPoints; vtkPoints *pts = vtkPoints::New(); pts->SetNumberOfPoints(numPts); vtkCellArray *lines = vtkCellArray::New(); lines->Allocate(lines->EstimateSize(numPts, 2)); vtkUnsignedCharArray *colors = vtkUnsignedCharArray::New(); colors->SetNumberOfComponents(3); colors->SetNumberOfTuples(numPts); // Store the colors and points in the polydata. if(highlightData != NULL) highlightData->Delete(); highlightData = vtkPolyData::New(); highlightData->Initialize(); highlightData->SetPoints(pts); highlightData->SetLines(lines); highlightData->GetCellData()->SetScalars(colors); pts->Delete(); lines->Delete(); colors->Delete(); // Add a cell to the polyData for each active hotpoint. int ptIndex = 0; int cellIndex = 0; int winWidth = helperRenderer->GetSize()[0]; int winHeight = helperRenderer->GetSize()[1]; double v[4]; double coord[3]; vtkIdType ptIds[4]; coord[2] = 0.; #define SQRT_OF_2 1.4142136 for(i = 0; i < numTools; ++i) { if(tools[i]->IsEnabled() && tools[i]->ShowsHotPointHighlights()) { const HotPointVector &hpts = tools[i]->HotPoints(); for(size_t j = 0; j < hpts.size(); ++j) { // Use the background renderer to compute the normalized // device coordinate of the hotpoint from the world space // coordinate. helperRenderer->SetWorldPoint(hpts[j].pt.x, hpts[j].pt.y, hpts[j].pt.z, 1.0); helperRenderer->WorldToDisplay(); helperRenderer->GetDisplayPoint(v); double dX = double((hpts[j].radius * winWidth) / SQRT_OF_2); double dY = double((hpts[j].radius * winHeight) / SQRT_OF_2); if (hpts[j].shape == 0) // square { coord[0] = v[0] + dX; coord[1] = v[1] + dY; pts->SetPoint(ptIndex, coord); coord[0] = v[0] + dX; coord[1] = v[1] - dY; pts->SetPoint(ptIndex + 1, coord); coord[0] = v[0] - dX; coord[1] = v[1] - dY; pts->SetPoint(ptIndex + 2, coord); coord[0] = v[0] - dX; coord[1] = v[1] + dY; pts->SetPoint(ptIndex + 3, coord); } else if (hpts[j].shape == 1) // tri up { // Yeah, we're making it have four points. It's easier. coord[0] = v[0] + dX; coord[1] = v[1] + dY; pts->SetPoint(ptIndex, coord); coord[0] = v[0]; coord[1] = v[1]; pts->SetPoint(ptIndex + 1, coord); coord[0] = v[0] - dX; coord[1] = v[1] + dY; pts->SetPoint(ptIndex + 2, coord); coord[0] = v[0]; coord[1] = v[1] + dY; pts->SetPoint(ptIndex + 3, coord); } else if (hpts[j].shape == 2) // tri down { // Yeah, we're making it have four points. It's easier. coord[0] = v[0] - dX; coord[1] = v[1] - dY; pts->SetPoint(ptIndex, coord); coord[0] = v[0]; coord[1] = v[1]; pts->SetPoint(ptIndex + 1, coord); coord[0] = v[0] + dX; coord[1] = v[1] - dY; pts->SetPoint(ptIndex + 2, coord); coord[0] = v[0]; coord[1] = v[1] - dY; pts->SetPoint(ptIndex + 3, coord); } for(int k = 0; k < 4; ++k) { ptIds[0] = ptIndex + k; ptIds[1] = (k < 3) ? (ptIndex + k + 1) : ptIndex; lines->InsertNextCell(2, ptIds); // Add the color. unsigned char *rgb = colors->GetPointer(cellIndex * 3); rgb[0] = 255; rgb[1] = 0; rgb[2] = 0; ++cellIndex; } ptIndex += 4; } } } if(highlightMapper != NULL) highlightMapper->Delete(); highlightMapper = vtkPolyDataMapper2D::New(); highlightMapper->SetInputData(highlightData); if(highlightActor != NULL) highlightActor->Delete(); highlightActor = vtkActor2D::New(); highlightActor->GetProperty()->SetLineWidth(2.); highlightActor->SetMapper(highlightMapper); highlightActor->PickableOff(); }
void CameraDolly::moveToPoly(vtkPolyData & polyData, vtkIdType index, IndexType indexType, bool overTime) { if (!m_renderer) { return; } double selectionPoint[3], selectionNormal[3]; if (indexType == IndexType::cells) { vtkCell * cell = polyData.GetCell(index); assert(cell); if (!cell) { qWarning() << "[CameraDolly] Cell not found in data set: " + QString::number(index); return; } auto cellPointIds = vtkSmartPointer<vtkIdTypeArray>::New(); cellPointIds->SetArray(cell->GetPointIds()->GetPointer(0), cell->GetNumberOfPoints(), true); vtkPolygon::ComputeCentroid(cellPointIds, polyData.GetPoints(), selectionPoint); vtkPolygon::ComputeNormal(cell->GetPoints(), selectionNormal); } else { polyData.GetPoint(index, selectionPoint); auto normals = polyData.GetPointData()->GetNormals(); if (!normals) { qWarning() << "[CameraDolly] No point normals found in poly data set"; return; } normals->GetTuple(index, selectionNormal); } vtkCamera & camera = *m_renderer->GetActiveCamera(); double objectCenter[3]; polyData.GetCenter(objectCenter); double startingFocalPoint[3], targetFocalPoint[3]; double startingAzimuth, targetAzimuth; double startingElevation, targetElevation; camera.GetFocalPoint(startingFocalPoint); for (size_t i = 0; i < 3; ++i) // look at center of the object { targetFocalPoint[i] = objectCenter[i]; } startingAzimuth = TerrainCamera::getAzimuth(camera); startingElevation = TerrainCamera::getVerticalElevation(camera); // compute target camera position to find azimuth and elevation for the transition double startingPosition[3]; camera.GetPosition(startingPosition); double targetPositionXY[2]; double objectToEye[3]; vtkMath::Subtract(startingPosition, objectCenter, objectToEye); double viewDistanceXY = vtkMath::Normalize2D(objectToEye); double selectionCenterXY[2] = { selectionPoint[0], selectionPoint[1] }; double norm_objectToSelectionXY[2]; norm_objectToSelectionXY[0] = selectionCenterXY[0] - objectCenter[0]; norm_objectToSelectionXY[1] = selectionCenterXY[1] - objectCenter[1]; double selectionRadiusXY = vtkMath::Normalize2D(norm_objectToSelectionXY); // make sure to move outside of the selection if (viewDistanceXY < selectionRadiusXY) { viewDistanceXY = selectionRadiusXY * 1.5; } // choose nearest viewpoint for flat surfaces const double flat_threshold = 30; double inclination = std::acos(selectionNormal[2]) * 180.0 / vtkMath::Pi(); if (inclination < flat_threshold) { targetPositionXY[0] = objectCenter[0] + viewDistanceXY * norm_objectToSelectionXY[0]; targetPositionXY[1] = objectCenter[1] + viewDistanceXY * norm_objectToSelectionXY[1]; } // or use the hill's normal else { double selectionNormalXY[2] = { selectionNormal[0], selectionNormal[1] }; double l; if ((l = std::sqrt((selectionNormalXY[0] * selectionNormalXY[0] + selectionNormalXY[1] * selectionNormalXY[1]))) != 0.0) { selectionNormalXY[0] /= l; selectionNormalXY[1] /= l; } // get a point in front of the selected cell double selectionFrontXY[2] = { selectionCenterXY[0] + selectionNormalXY[0], selectionCenterXY[1] + selectionNormalXY[1] }; double intersections[4]; // our focal point (center of view circle) is the object center // so assume a circle center of (0,0) in the calculations bool intersects = circleLineIntersection(viewDistanceXY, selectionCenterXY, selectionFrontXY, &intersections[0], &intersections[2]); // ignore for now if (!intersects) { targetPositionXY[0] = startingPosition[0]; targetPositionXY[1] = startingPosition[1]; } else { bool towardsPositive = selectionFrontXY[0] > selectionCenterXY[0] || (selectionFrontXY[0] == selectionCenterXY[0] && selectionFrontXY[1] >= selectionCenterXY[1]); int intersectionIndex; if (towardsPositive == (intersections[0] > intersections[2] || (intersections[0] == intersections[2] && intersections[1] >= intersections[3]))) { intersectionIndex = 0; } else { intersectionIndex = 2; } targetPositionXY[0] = intersections[intersectionIndex]; targetPositionXY[1] = intersections[intersectionIndex + 1]; } } auto targetCamera = vtkSmartPointer<vtkCamera>::New(); targetCamera->SetPosition(targetPositionXY[0], targetPositionXY[1], startingPosition[2]); targetCamera->SetFocalPoint(targetFocalPoint); targetCamera->SetViewUp(camera.GetViewUp()); targetAzimuth = TerrainCamera::getAzimuth(*targetCamera); targetElevation = TerrainCamera::getVerticalElevation(*targetCamera); if (overTime) { const double flyTimeSec = 0.5; const int flyDeadlineMSec = 1000; const int NumberOfFlyFrames = 15; double stepFactor = 1.0 / (NumberOfFlyFrames + 1); // transition of position, focal point, azimuth and elevation double stepFocalPoint[3], stepPosition[3], stepAzimuth, stepElevation; vtkMath::Subtract(targetFocalPoint, startingFocalPoint, stepFocalPoint); vtkMath::MultiplyScalar(stepFocalPoint, stepFactor); vtkMath::Subtract(targetCamera->GetPosition(), startingPosition, stepPosition); vtkMath::MultiplyScalar(stepPosition, stepFactor); stepAzimuth = (targetAzimuth - startingAzimuth) * stepFactor; stepElevation = (targetElevation - startingElevation) * stepFactor; double intermediateFocal[3]{ startingFocalPoint[0], startingFocalPoint[1], startingFocalPoint[2] }; double intermediatePosition[3]{ startingPosition[0], startingPosition[1], startingPosition[2] }; double intermediateAzimuth = startingAzimuth; double intermediateElevation = startingElevation; QTime startingTime = QTime::currentTime(); QTime deadline = startingTime.addMSecs(flyDeadlineMSec); long sleepMSec = long(flyTimeSec * 1000.0 * stepFactor); QTime renderTime; int i = 0; for (; i < NumberOfFlyFrames; ++i) { renderTime.start(); vtkMath::Add(intermediateFocal, stepFocalPoint, intermediateFocal); vtkMath::Add(intermediatePosition, stepPosition, intermediatePosition); intermediateAzimuth += stepAzimuth; intermediateElevation += stepElevation; camera.SetFocalPoint(intermediateFocal); camera.SetPosition(intermediatePosition); TerrainCamera::setAzimuth(camera, intermediateAzimuth); TerrainCamera::setVerticalElevation(camera, intermediateElevation); m_renderer->ResetCameraClippingRange(); m_renderer->GetRenderWindow()->InvokeEvent(vtkCommandExt::ForceRepaintEvent); if (QTime::currentTime() > deadline) { break; } QThread::msleep((unsigned long)std::max(0l, sleepMSec - renderTime.elapsed())); } } // in any case, jump to target position camera.SetFocalPoint(targetFocalPoint); camera.SetPosition(targetCamera->GetPosition()); TerrainCamera::setAzimuth(camera, targetAzimuth); TerrainCamera::setVerticalElevation(camera, targetElevation); m_renderer->ResetCameraClippingRange(); m_renderer->GetRenderWindow()->Render(); }