示例#1
0
vtkSmartPointer<vtkArrayType> FlowAnalysis::vtkMakeArray(const vtkSmartPointer<vtkUniformGrid>& grid, const string& name, size_t numComponents, bool fillZero){
	auto arr=vtkSmartPointer<vtkArrayType>::New();
	arr->SetNumberOfComponents(numComponents);
	arr->SetNumberOfTuples(boxCells.prod());
	arr->SetName(name.c_str());
	if(cellData) grid->GetCellData()->AddArray(arr);
	else grid->GetPointData()->AddArray(arr);
	if(fillZero){ for(int _i=0; _i<(int)numComponents; _i++) arr->FillComponent(_i,0.); }
	return arr;
}
void VTKEulerMeshSnapshotWriter::dumpMeshSpecificData(EulerMesh* mesh, vtkSmartPointer<vtkStructuredGrid>& grid, vtkSmartPointer<vtkPoints>& points) const
{
    auto nodeDims = mesh->getNodeDimensions();
    auto cellDims = mesh->getDimensions();

    auto cellStatus = vtkSmartPointer<vtkIntArray>::New();
    cellStatus->SetName("cellStatus");

    auto cellError = vtkSmartPointer<vtkIntArray>::New();
    cellError->SetName("cellError");
    for (uint k = 0; k < nodeDims.z; k++)
        for (uint j = 0; j < nodeDims.y; j++)
            for (uint i = 0; i < nodeDims.x; i++)
            {
                auto& node = mesh->getNodeByEulerMeshIndex(vector3u(i, j, k));
                points->InsertNextPoint(node.coords.x, node.coords.y, node.coords.z);
            }
    for (uint k = 0; k < cellDims.z; k++)
        for (uint j = 0; j < cellDims.y; j++)
            for (uint i = 0; i < cellDims.x; i++)
            {
                cellStatus->InsertNextValue(mesh->getCellStatus(vector3u(i, j, k)));
                char flag = 0;

                for (uint p = 0; p <= 1; p++)
                    for (uint q = 0; q <= 1; q++)
                        for (uint s = 0; s <= 1; s++)
                            if ((flag = mesh->getNodeByEulerMeshIndex(vector3u(i+p, j+q, k+s)).getErrorFlags()))
                                break;

                cellError->InsertNextValue(flag);
            }

    grid->GetCellData()->AddArray(cellStatus);
    grid->GetCellData()->AddArray(cellError);

    grid->SetDimensions(nodeDims.x, nodeDims.y, nodeDims.z);
}
示例#3
0
void
pcl::io::pointCloudTovtkPolyData(const pcl::PCLPointCloud2Ptr& cloud, vtkSmartPointer<vtkPolyData>& poly_data)
{
  if (!poly_data.GetPointer())
    poly_data = vtkSmartPointer<vtkPolyData>::New (); // OR poly_data->Reset();


  // Add Points
  size_t x_idx = pcl::getFieldIndex (*cloud, std::string ("x") );
  vtkSmartPointer<vtkPoints> cloud_points = vtkSmartPointer<vtkPoints>::New ();
  vtkSmartPointer<vtkCellArray> cloud_vertices = vtkSmartPointer<vtkCellArray>::New ();

  vtkIdType pid[1];
  for (size_t point_idx = 0; point_idx < cloud->width * cloud->height; point_idx ++)
  {
    float point[3];

    int point_offset = (int (point_idx) * cloud->point_step);
    int offset = point_offset + cloud->fields[x_idx].offset;
    memcpy (&point, &cloud->data[offset], sizeof (float)*3);

    pid[0] = cloud_points->InsertNextPoint (point);
    cloud_vertices->InsertNextCell (1, pid);
  }

  //set the points and vertices we created as the geometry and topology of the polydata
  poly_data->SetPoints (cloud_points);
  poly_data->SetVerts (cloud_vertices);

  // Add RGB
  int rgb_idx = pcl::getFieldIndex (*cloud, "rgb");
  if (rgb_idx != -1)
  {
    //std::cout << "Adding rgb" << std::endl;
    vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New ();

    colors->SetNumberOfComponents (3);
    colors->SetName ("rgb");

    for (size_t point_idx = 0; point_idx < cloud->width * cloud->height; point_idx ++)
    {
      unsigned char bgr[3];

      int point_offset = (int (point_idx) * cloud->point_step);
      int offset = point_offset + cloud->fields[rgb_idx].offset;
      memcpy (&bgr, &cloud->data[offset], sizeof (unsigned char)*3);

      colors->InsertNextTuple3(bgr[2], bgr[1], bgr[0]);
    }

    poly_data->GetCellData()->SetScalars(colors);
  }

  // Add Intensity
  int intensity_idx = pcl::getFieldIndex (*cloud, "intensity");
  if (intensity_idx != -1)
  {
    //std::cout << "Adding intensity" << std::endl;
    vtkSmartPointer<vtkFloatArray> cloud_intensity = vtkSmartPointer<vtkFloatArray>::New ();
    cloud_intensity->SetNumberOfComponents (1);
    cloud_intensity->SetName("intensity");

    for (size_t point_idx = 0; point_idx < cloud->width * cloud->height; point_idx ++)
    {
      float intensity;

      int point_offset = (int (point_idx) * cloud->point_step);
      int offset = point_offset + cloud->fields[intensity_idx].offset;
      memcpy (&intensity, &cloud->data[offset], sizeof(float));

      cloud_intensity->InsertNextValue(intensity);
    }

    poly_data->GetCellData()->AddArray(cloud_intensity);
    if (rgb_idx == -1)
      poly_data->GetCellData()->SetActiveAttribute("intensity", vtkDataSetAttributes::SCALARS);
  }

  // Add Normals
  int normal_x_idx = pcl::getFieldIndex (*cloud, std::string ("normal_x") );
  if (normal_x_idx != -1)
  {
    //std::cout << "Adding normals" << std::endl;
    vtkSmartPointer<vtkFloatArray> normals = vtkSmartPointer<vtkFloatArray>::New();
    normals->SetNumberOfComponents(3); //3d normals (ie x,y,z)
    normals->SetName("normals");

    for (size_t point_idx = 0; point_idx < cloud->width * cloud->height; point_idx ++)
    {
      float normal[3];

      int point_offset = (int (point_idx) * cloud->point_step);
      int offset = point_offset + cloud->fields[normal_x_idx].offset;
      memcpy (&normal, &cloud->data[offset], sizeof (float)*3);

      normals->InsertNextTuple(normal);
    }

    poly_data->GetCellData()->SetNormals(normals);
    //poly_data->GetCellData()->SetActiveAttribute("normals", vtkDataSetAttributes::SCALARS);
  }
}
示例#4
0
void
ExporterVTK<MeshType,N>::saveElementData( typename timeset_type::step_ptrtype step, Iterator __evar, Iterator __evaren, vtkSmartPointer<vtkout_type> out ) const
{
    while ( __evar != __evaren )
    {
        if ( !__evar->second.worldComm().isActive() ) return;

        auto mesh = step->mesh();

        auto r = elements( step->mesh() );
        auto elt_st = r.template get<1>();
        auto elt_en = r.template get<2>();

        if ( !__evar->second.areGlobalValuesUpdated() )
            __evar->second.updateGlobalValues();

        //size_type ncells = __evar->second.size()/__evar->second.nComponents;
        size_type ncells = std::distance( elt_st, elt_en );

        uint16_type nComponents = __evar->second.nComponents;

        if ( __evar->second.is_vectorial )
            nComponents = 3;

        size_type __field_size = nComponents * ncells;
        std::vector<float> __field( __field_size );
        __field.clear();

        DVLOG(2) << "[saveElement] firstLocalIndex = " << __evar->second.firstLocalIndex() << "\n";
        DVLOG(2) << "[saveElement] lastLocalIndex = " << __evar->second.lastLocalIndex() << "\n";
        DVLOG(2) << "[saveElement] field.size = " << __field_size << "\n";

        vtkSmartPointer<vtkFloatArray> da = vtkSmartPointer<vtkFloatArray>::New();
        da->SetName(__evar->first.c_str());

        /* set array parameters */
        da->SetNumberOfComponents(nComponents);
        da->SetNumberOfTuples(ncells);

        /*
           std::cout << this->worldComm().rank() << " nbElts:" << ncells << " nComp:" << nComponents
           << " __evar->second.nComponents:" << __evar->second.nComponents << std::endl;
        */
        /*
           std::cout << this->worldComm().rank() << " marker=" << *mit << " nbElts:" << ncells << " nComp:" << nComponents
           << " __evar->second.nComponents:" << __evar->second.nComponents << std::endl;
           */

        float * array = new float[nComponents];

        size_type e = 0;
        for ( auto elt_it = elt_st ; elt_it != elt_en; ++elt_it, ++e )
        {
            auto const& elt = boost::unwrap_ref( *elt_it );
            DVLOG(2) << "pid : " << this->worldComm().globalRank()
                     << " elt_it :  " << elt.id()
                     << " e : " << e << "\n";

            for ( int c = 0; c < nComponents; ++c )
            {
                size_type global_node_id = e*ncells+c ;

                if ( c < __evar->second.nComponents )
                {
                    size_type dof_id = boost::get<0>( __evar->second.functionSpace()->dof()->localToGlobal( elt.id(),0, c ) );

                    DVLOG(2) << "c : " << c
                             << " gdofid: " << global_node_id
                             << " dofid : " << dof_id
                             << " f.size : " <<  __field.size()
                             << " e.size : " <<  __evar->second.size()
                             << "\n";

                    //__field[global_node_id] = __evar->second.globalValue( dof_id );
                    array[c] =  __evar->second.globalValue( dof_id );

#if 1
                    //__field[global_node_id] = __evar->second.globalValue(dof_id);
                    DVLOG(2) << "c : " << c
                             << " gdofid: " << global_node_id
                             << " dofid : " << dof_id
                             << " field :  " << __field[global_node_id]
                             << " evar: " << __evar->second.globalValue( dof_id ) << "\n";
#endif
                }

                else
                {
                    //__field[global_node_id] = 0;
                    array[c] = 0.0;
                }
            }
            da->SetTuple(e, array);
        }

        delete[] array;

        /* add data array into the vtk object */
        out->GetCellData()->AddArray(da);

        /* Set the first scalar/vector/tensor data, we process as active */
        if( __evar->second.is_scalar && !(out->GetCellData()->GetScalars()))
        {
            out->GetCellData()->SetActiveScalars(da->GetName());
        }
        if( __evar->second.is_vectorial && !(out->GetCellData()->GetVectors()))
        {
            out->GetCellData()->SetActiveVectors(da->GetName());
        }
        if( __evar->second.is_tensor2 && !(out->GetCellData()->GetTensors()))
        {
            out->GetCellData()->SetActiveTensors(da->GetName());
        }

        DVLOG(2) << "[ExporterVTK::saveElement] saving " << __evar->first << "done\n";
        ++__evar;
    }
}
//keyword "cellvars" in OOFEM input file
void
VTKXMLExportModule :: exportCellVarAs(InternalStateType type, int region,
#ifdef __VTK_MODULE
    vtkSmartPointer<vtkUnstructuredGrid> &stream,
#else
    FILE *stream,
#endif
    TimeStep *tStep)
{
    Domain *d = emodel->giveDomain(1);
    int ielem, nelem = d->giveNumberOfElements();
    int pos;
    Element *elem;
    FloatMatrix mtrx(3, 3);
    IntegrationRule *iRule;
    GaussPoint *gp;
    FloatArray answer, temp;
    double gptot;
    int ncomponents = 1;

#ifdef __VTK_MODULE
    vtkSmartPointer<vtkDoubleArray> cellVarsArray = vtkSmartPointer<vtkDoubleArray>::New();
    cellVarsArray->SetName(__InternalStateTypeToString(type));
#endif

    switch ( type ) {
    case IST_MaterialNumber:
    case IST_ElementNumber:
    case IST_Pressure:
        // if it wasn't for IST_Pressure,
#ifdef __VTK_MODULE
        cellVarsArray->SetNumberOfComponents(1);
        cellVarsArray->SetNumberOfTuples(nelem);
#else
        fprintf( stream, "<DataArray type=\"Float64\" Name=\"%s\" format=\"ascii\">\n", __InternalStateTypeToString(type) );
#endif
        for ( ielem = 1; ielem <= nelem; ielem++ ) {
            elem = d->giveElement(ielem);

            if ( (( region > 0 ) && ( this->smoother->giveElementVirtualRegionNumber(ielem) != region ))
                    || this->isElementComposite(elem) || !elem-> isActivated(tStep) ) { // composite cells exported individually
                continue;
            }

#ifdef __PARALLEL_MODE
            if ( elem->giveParallelMode() != Element_local ) {
                continue;
            }

#endif
            if ( type == IST_MaterialNumber ) {
#ifdef __VTK_MODULE
                cellVarsArray->SetTuple1(ielem-1, elem->giveMaterial()->giveNumber() ); // Should be integer..
#else
                fprintf( stream, "%d ", elem->giveMaterial()->giveNumber() );
#endif
            } else if ( type == IST_ElementNumber ) {
#ifdef __VTK_MODULE
                cellVarsArray->SetTuple1(ielem-1,  elem->giveNumber() ); // Should be integer..
#else
                fprintf( stream, "%d ", elem->giveNumber() );
#endif
            } else if (type == IST_Pressure) { ///@todo Why this special treatment for pressure? / Mikael
                if (elem->giveNumberOfInternalDofManagers() == 1) {
                    IntArray pmask(1); pmask.at(1) = P_f;
                    elem->giveInternalDofManager(1)->giveUnknownVector (answer, pmask,EID_ConservationEquation, VM_Total, tStep);
#ifdef __VTK_MODULE
                    cellVarsArray->SetTuple1(ielem-1,  answer.at(1) ); // Should be integer..
#else
                    fprintf( stream, "%f ", answer.at(1) );
#endif
                }
            }
        }
#ifdef __VTK_MODULE
        stream->GetCellData()->SetActiveScalars(__InternalStateTypeToString(type));
        stream->GetCellData()->SetScalars(cellVarsArray);
#endif
        break;

    case IST_MaterialOrientation_x:
    case IST_MaterialOrientation_y:
    case IST_MaterialOrientation_z:
#ifdef __VTK_MODULE
        cellVarsArray->SetNumberOfComponents(3);
        cellVarsArray->SetNumberOfTuples(nelem);
        ncomponents = 3;
#else
        fprintf( stream, "<DataArray type=\"Float64\" Name=\"%s\" NumberOfComponents=\"3\" format=\"ascii\">\n", __InternalStateTypeToString(type) );
#endif
        if ( type == IST_MaterialOrientation_x ) {
            pos = 1;
        }

        if ( type == IST_MaterialOrientation_y ) {
            pos = 2;
        }

        if ( type == IST_MaterialOrientation_z ) {
            pos = 3;
        }

        for ( ielem = 1; ielem <= nelem; ielem++ ) {
            ///@todo Should no elements be skipped here? / Mikael
            if ( !d->giveElement(ielem)->giveLocalCoordinateSystem(mtrx) ) {
                mtrx.resize(3, 3);
                mtrx.zero();
            }
#ifdef __VTK_MODULE
            cellVarsArray->SetTuple3(ielem-1,  mtrx.at(1, pos), mtrx.at(2,pos), mtrx.at(3,pos) );
#else
            fprintf( stream, "%f %f %f  ", mtrx.at(1, pos), mtrx.at(2, pos), mtrx.at(3, pos) );
#endif
        }

#ifdef __VTK_MODULE
        stream->GetCellData()->SetActiveVectors(__InternalStateTypeToString(type));
        stream->GetCellData()->SetVectors(cellVarsArray);
#endif
        break;

    default:
        bool reshape = false;
        InternalStateValueType vt = giveInternalStateValueType(type);
        if ( vt == ISVT_SCALAR ) {
            ncomponents = 1;
        } else if ( vt == ISVT_VECTOR ) {
            ncomponents = 3;
        } else {
            ncomponents = 9;
            reshape = true;
        }
#ifdef __VTK_MODULE
        cellVarsArray->SetNumberOfComponents(ncomponents);
        cellVarsArray->SetNumberOfTuples(nelem);
#else
        fprintf( stream, "<DataArray type=\"Float64\" Name=\"%s\" NumberOfComponents=\"%d\" format=\"ascii\">\n", __InternalStateTypeToString(type), ncomponents );
#endif

        IntArray redIndx;
        for ( ielem = 1; ielem <= nelem; ielem++ ) {
            elem = d->giveElement(ielem);
            if ( (( region > 0 ) && ( this->smoother->giveElementVirtualRegionNumber(ielem) != region ))
                    || this->isElementComposite(elem) || !elem-> isActivated(tStep) ) { // composite cells exported individually
                continue;
            }
#ifdef __PARALLEL_MODE
            if ( elem->giveParallelMode() != Element_local ) {
                continue;
            }
#endif
            gptot = 0;
            answer.resize(0);
            iRule = elem->giveDefaultIntegrationRulePtr();
            if (iRule) {
                MaterialMode mmode = _Unknown;
                for (int i = 0; i < iRule->getNumberOfIntegrationPoints(); ++i) {
                    gp = iRule->getIntegrationPoint(i);
                    mmode = gp->giveMaterialMode();
                    elem->giveIPValue(temp, gp, type, tStep);
                    gptot += gp->giveWeight();
                    answer.add(gp->giveWeight(), temp);
                }
                answer.times(1./gptot);
                elem->giveMaterial()->giveIntVarCompFullIndx(redIndx, type, mmode);
            }
            // Reshape the Voigt vectors to include all components (duplicated if necessary, VTK insists on 9 components for tensors.)
            if ( reshape && answer.giveSize() != 9) { // If it has 9 components, then it is assumed to be proper already.
                FloatArray tmp = answer;
                this->makeFullForm(answer, tmp, vt, redIndx);
            } else if ( vt == ISVT_VECTOR && answer.giveSize() < 3) {
                answer.setValues(3,
                                 answer.giveSize() > 1 ? answer.at(1) : 0.0,
                                 answer.giveSize() > 2 ? answer.at(2) : 0.0,
                                 0.0);
            } else if ( ncomponents != answer.giveSize() ) { // Trying to gracefully handle bad cases, just output zeros.
                answer.resize(ncomponents);
                answer.zero();
            }
            for (int i = 1; i <= ncomponents; ++i) {
#ifdef __VTK_MODULE
                cellVarsArray->SetComponent(ielem-1, i-1, answer.at(i));
#else
                fprintf( stream, "%e ", answer.at(i) );
#endif
            }
#ifndef __VTK_MODULE
            fprintf( stream, "\n" );
#endif
        }
#ifdef __VTK_MODULE
        if (ncomponents == 1) {
            stream->GetCellData()->SetActiveScalars(__InternalStateTypeToString(type));
            stream->GetCellData()->SetScalars(cellVarsArray);
        } else if (ncomponents == 3) {
            stream->GetCellData()->SetActiveVectors(__InternalStateTypeToString(type));
            stream->GetCellData()->SetVectors(cellVarsArray);
        } else {
            stream->GetCellData()->SetActiveTensors(__InternalStateTypeToString(type));
            stream->GetCellData()->SetTensors(cellVarsArray);
        }
#endif
    }
#ifndef __VTK_MODULE
    fprintf(stream, "</DataArray>\n");
#endif
}