#include "vtkVectorVisuManager.h" #include #include #include #include #include #include #include #include #include #include #include #include vtkCxxRevisionMacro(vtkVectorVisuManager, "$Revision: 1.21 $"); vtkStandardNewMacro(vtkVectorVisuManager); void vtkVectorVisuManagerCallback::Execute ( vtkObject *caller, unsigned long, void* ) { // get the box widget vtkBoxWidget *widget = reinterpret_cast(caller); // get the inputed vtkPolyData vtkUnstructuredGrid* input = (vtkUnstructuredGrid*)widget->GetInput(); if( !input ) { std::cout<<"ouch !..."<GetPolyData(myBox); // myBox contains 15 points and points 8 to 13 // define the bounding box double xmin, xmax, ymin, ymax, zmin, zmax; double* pt = myBox->GetPoint(8); xmin = pt[0]; pt = myBox->GetPoint(9); xmax = pt[0]; pt = myBox->GetPoint(10); ymin = pt[1]; pt = myBox->GetPoint(11); ymax = pt[1]; pt = myBox->GetPoint(12); zmin = pt[2]; pt = myBox->GetPoint(13); zmax = pt[2]; this->Box->SetBounds(xmin, xmax, ymin, ymax, zmin, zmax); // this->VectorLimiter->SetVOI ((int)xmin, (int)xmax, (int)ymin, (int)ymax, (int)zmin, (int)zmax); this->VectorLimiter->Update(); myBox->Delete(); } vtkVectorVisuManager::vtkVectorVisuManager() { this->Input = 0; this->RWin = 0; this->BoxWidget = vtkBoxWidget::New(); this->Callback = vtkVectorVisuManagerCallback::New(); this->Mapper = vtkDataSetMapper::New(); this->Actor = vtkActor::New(); this->VectorFilter = vtkGlyph3D::New(); this->VectorFilter->SetScaleModeToScaleByVector(); this->VectorFilter->SetColorModeToColorByScalar(); this->VectorFilter->SetScaleFactor (5.0); //this->VectorFilter->GeneratePointIdsOn(); this->RenderingMode = RENDER_IS_POLYLINES; this->BoxWidget->RotationEnabledOff(); this->BoxWidget->SetPlaceFactor (1.0); this->BoxWidget->AddObserver (vtkCommand::InteractionEvent, this->Callback); this->BoxWidgetVisibility = true; this->BoxWidget->GetOutlineProperty()->SetColor (0.3,0.3,1.0); this->BoxWidget->GetHandleProperty()->SetColor (0.8,0.8,1.0); //this->BoxWidget->GetSelectedOutlineProperty()->SetColor (0.7,0.7,1.0); // link the limiters this->Actor->SetMapper (this->Mapper); this->Actor->GetProperty()->SetLineWidth (3.0); } vtkVectorVisuManager::~vtkVectorVisuManager() { if( this->RWin && this->RWin->GetRenderWindow() && this->RWin->GetRenderWindow()->GetRenderers()) { this->RWin->GetRenderWindow()->GetRenderers()->InitTraversal(); vtkRenderer* first_renderer = this->RWin->GetRenderWindow()->GetRenderers()->GetNextItem(); if (first_renderer) { first_renderer->RemoveActor(this->Actor); } this->RWin->Delete(); } this->BoxWidget->Delete(); this->Callback->Delete(); this->Mapper->Delete(); this->Actor->Delete(); this->VectorFilter->Delete(); } void vtkVectorVisuManager::ProvideColorsToVectors (void) { if(!this->Input) return; bool is_in_points = true; vtkDoubleArray* vectors = vtkDoubleArray::SafeDownCast (this->Input->GetPointData()->GetVectors()); if (!vectors) { vectors = vtkDoubleArray::SafeDownCast (this->Input->GetCellData()->GetVectors()); is_in_points = false; } if (!vectors) return; vtkUnsignedCharArray* colors = vtkUnsignedCharArray::New(); colors->SetNumberOfComponents (3); colors->SetName("colors"); int npts = 0; if (is_in_points) npts = this->Input->GetNumberOfPoints(); else npts = this->Input->GetNumberOfCells(); for( int i=0; iGetTupleValue (i, vector); double norm = sqrt (vector[0]*vector[0]+ vector[1]*vector[1]+ vector[2]*vector[2]); vector[0] /= norm; vector[1] /= norm; vector[2] /= norm; unsigned char color[3]; for( unsigned int j=0; j<3; j++) color[j] = (unsigned char)( fabs (vector[j])*255.0 ); colors->InsertNextTupleValue( color ); } if (is_in_points) this->Input->GetPointData()->SetScalars ( colors ); else this->Input->GetCellData()->SetScalars ( colors ); colors->Delete(); } void vtkVectorVisuManager::NormalizeVectors (void) { bool is_in_points = true; vtkDoubleArray* vectors = vtkDoubleArray::SafeDownCast (this->Input->GetPointData()->GetVectors()); if (!vectors) { vectors = vtkDoubleArray::SafeDownCast (this->Input->GetCellData()->GetVectors()); is_in_points = false; } if (!vectors) return; int npts = 0; if (is_in_points) npts = this->Input->GetNumberOfPoints(); else npts = this->Input->GetNumberOfCells(); for( int i=0; iGetTupleValue (i, vector); double norm = sqrt (vector[0]*vector[0]+ vector[1]*vector[1]+ vector[2]*vector[2]); v2[0] = vector[0] / norm; v2[1] = vector[1] / norm; v2[2] = vector[2] / norm; vectors->SetTupleValue (i, v2); } } void vtkVectorVisuManager::SetInput(vtkDataSet* input) { if( !input ) return; if( !input->GetNumberOfPoints() ) return; this->Initialize(); this->Input = input; this->ParseVectorFromCellsToPoints(); this->ProvideColorsToVectors(); this->NormalizeVectors(); this->Callback->GetVectorLimiter()->SetInput ( this->GetInput() ); this->BoxWidget->SetInput ( this->GetInput() ); this->BoxWidget->PlaceWidget(); this->Callback->Execute (this->BoxWidget, 0, NULL); // this->Mapper->SetInput (this->Callback->GetOutput()); this->VectorFilter->SetInput(this->Callback->GetOutput()); this->VectorFilter->Update(); this->Mapper->SetInput (this->VectorFilter->GetOutput()); //this->VectorFilter->SetInput(this->Input); // add the actor to the rwin // if( this->RWin ) // { // this->RWin->GetRenderWindow()->GetRenderers()->InitTraversal(); // vtkRenderer* first_renderer = this->RWin->GetRenderWindow()->GetRenderers()->GetNextItem(); // if (first_renderer) // first_renderer->AddActor(this->Actor); // } } void vtkVectorVisuManager::SwapInputOutput() { // vtkUnstructuredGrid* auxPoly = vtkUnstructuredGrid::New(); // vtkUnstructuredGrid* cbkPoly = vtkUnstructuredGrid::SafeDownCast(this->Callback->GetOutput()); // if (cbkPoly) // auxPoly->SetPoints( cbkPoly->GetPoints() ); // auxPoly->GetPointData()->SetVectors ( this->Callback->GetOutput()->GetPointData()->GetVectors() ); // this->BoxWidget->SetInput ( auxPoly ); // this->Callback->GetVectorLimiter()->SetInput ( auxPoly ); // auxPoly->Delete(); } void vtkVectorVisuManager::Reset() { if( !this->GetInput() ) return; this->BoxWidget->SetInput ( this->GetInput() ); this->Callback->GetVectorLimiter()->SetInput ( this->Callback->GetVectorLimiter()->GetOutput() ); this->Callback->Execute (this->BoxWidget, 0, NULL); } void vtkVectorVisuManager::SetVisibility (bool isVisible) { this->Actor->SetVisibility (isVisible); if (isVisible) this->SetBoxWidget (this->BoxWidgetVisibility); } void vtkVectorVisuManager::Initialize (void) { if (this->Callback->GetVectorLimiter()->GetOutput()) this->Callback->GetVectorLimiter()->GetOutput()->Initialize(); this->Callback->GetVectorLimiter()->RemoveAllInputs(); this->VectorFilter->GetOutput()->Initialize(); this->Input=0; } void vtkVectorVisuManager::ParseVectorFromCellsToPoints (void) { vtkDoubleArray* vectors = vtkDoubleArray::SafeDownCast (this->Input->GetPointData()->GetVectors()); if (vectors) return; vectors = vtkDoubleArray::SafeDownCast (this->Input->GetCellData()->GetVectors()); if (!vectors) return; vtkDoubleArray* newvectors = vtkDoubleArray::New(); newvectors->SetNumberOfComponents (3); std::ostringstream os; if (vectors->GetName()) os << vectors->GetName(); os << "(copy)"; newvectors->SetName(os.str().c_str()); int npts = this->Input->GetNumberOfPoints(); for( int pointid=0; pointidInput->GetPointCells (pointid, list); double vector[3] = {0.0, 0.0, 0.0}; for (int j=0; jGetNumberOfIds(); j++) { int cellid = list->GetId (j); double t_vector[3]; vectors->GetTupleValue (cellid, t_vector); vector[0]+=t_vector[0]; vector[1]+=t_vector[1]; vector[2]+=t_vector[2]; } // std::cout<<"vector is " // <InsertNextTupleValue( vector ); list->Delete(); } this->Input->GetPointData()->SetVectors (newvectors); newvectors->Delete(); }