// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
int BinaryNodesTrianglesReader::read()
{

    SurfaceMeshDataContainer *sm = getSurfaceMeshDataContainer();
    int err = 0;
    setErrorCondition(err);

    std::stringstream s;
    // Open the Nodes file for reading
    FILE* nodesFile = fopen(m_BinaryNodesFile.c_str(), "rb+");
    if(nodesFile == NULL)
    {
        s.str("");
        s << "Error opening nodes file '" << m_BinaryNodesFile << "'";
        setErrorCondition(786);
        //    PipelineMessage em (getHumanLabel(), s.str(), -1);
        //    addErrorMessage(em);
        //    notifyMessage(em);
        return getErrorCondition();
    }
    ScopedFileMonitor nodesMonitor(nodesFile);

    // Calculate how many nodes are in the file based on the file size
    fseek(nodesFile, 0, SEEK_END);
    size_t fLength = ftell(nodesFile);
    size_t nNodes = fLength / SurfaceMesh::NodesFile::ByteCount;
    fseek(nodesFile, 0, SEEK_SET);
    fLength = ftell(nodesFile);
    if(0 != fLength)
    {
        s.str("");
        s << getNameOfClass() << ": Error Could not rewind to beginning of file after nodes count.'" << m_BinaryNodesFile << "'";
        setErrorCondition(787);
        //    PipelineMessage em (getHumanLabel(), s.str(), -1);
        //    addErrorMessage(em);
        //    notifyMessage(em);
        return getErrorCondition();
    }
    s.str("");
    s << "Calc Node Count from Nodes.bin File: " << nNodes;
    notifyStatusMessage(s.str());

    // Open the triangles file for reading
    FILE* triFile = fopen(m_BinaryTrianglesFile.c_str(), "rb+");
    if(triFile == NULL)
    {
        s.str("");
        s << getNameOfClass() << ": Error opening Triangles file '" << m_BinaryTrianglesFile << "'";
        setErrorCondition(788);
        //    PipelineMessage em (getHumanLabel(), s.str(), -1);
        //    addErrorMessage(em);
        //    notifyMessage(em);
        return getErrorCondition();
    }

    ScopedFileMonitor trianglesMonitor(triFile);
    // Calculate how many Triangles are in the file based in the file size
    fseek(triFile, 0, SEEK_END);
    fLength = ftell(triFile);
    size_t nTriangles = fLength / SurfaceMesh::TrianglesFile::ByteCount;
    fseek(triFile, 0, SEEK_SET);
    fLength = ftell(triFile);
    if(0 != fLength)
    {
        s.str("");
        s << getNameOfClass() << ": Error Could not rewind to beginning of file after triangles count.'" << m_BinaryTrianglesFile << "'";
        setErrorCondition(789);
        //    PipelineMessage em (getHumanLabel(), s.str(), -1);
        //    addErrorMessage(em);
        //    notifyMessage(em);
        return getErrorCondition();
    }
    s.str("");
    s << "Calc Triangle Count from Triangles.bin File: " << nTriangles;
    notifyStatusMessage(s.str());

    // Allocate all the nodes
    typedef DREAM3D::SurfaceMesh::Vert_t Vert_t;
    StructArray<Vert_t>::Pointer m_NodeListPtr = StructArray<Vert_t>::CreateArray(nNodes, DREAM3D::VertexData::SurfaceMeshNodes);
    Vert_t* m_NodeList = m_NodeListPtr->GetPointer(0);

    Int8ArrayType::Pointer nodeTypePtr = Int8ArrayType::CreateArray(nNodes, 1, DREAM3D::VertexData::SurfaceMeshNodeType);
    nodeTypePtr->initializeWithZeros();
    int8_t* nodeType = nodeTypePtr->GetPointer(0);

    s.str("");
    s << "Reading Nodes file into Memory";
    notifyStatusMessage(s.str());
    size_t nread = 0;
    SurfaceMesh::NodesFile::NodesFileRecord_t nRecord;

    for (size_t i = 0; i < nNodes; i++)
    {

        nread = fread(&nRecord, SurfaceMesh::NodesFile::ByteCount, 1, nodesFile); // Read one set of positions from the nodes file
        if(nread != 1)
        {
            break;
        }
        DREAM3D::SurfaceMesh::Vert_t& node = m_NodeList[nRecord.nodeId];
        node.pos[0] = nRecord.x;
        node.pos[1] = nRecord.y;
        node.pos[2] = nRecord.z;
        nodeType[nRecord.nodeId] = nRecord.nodeKind;
    }

    s.str("");
    s << "Reading Triangles file into Memory";
    notifyStatusMessage(s.str());

    // Allocate all the Triangle Objects
    typedef DREAM3D::SurfaceMesh::Face_t Face_t;
    StructArray<Face_t>::Pointer m_TriangleListPtr = StructArray<Face_t>::CreateArray(nTriangles, DREAM3D::FaceData::SurfaceMeshFaces);
    Face_t* m_TriangleList = m_TriangleListPtr->GetPointer(0);
    ::memset(m_TriangleList, 0xAB, sizeof(Face_t) * nTriangles);

    DataArray<int32_t>::Pointer faceLabelPtr = DataArray<int32_t>::CreateArray(nTriangles, 2, DREAM3D::FaceData::SurfaceMeshFaceLabels);
    int32_t* faceLabels = faceLabelPtr->GetPointer(0);
    faceLabelPtr->initializeWithZeros();


    SurfaceMesh::TrianglesFile::TrianglesFileRecord_t tRecord;
    for (size_t i = 0; i < nTriangles; i++)
    {
        // Read from the Input Triangles Temp File
        nread = fread(&tRecord, SurfaceMesh::TrianglesFile::ByteCount, 1, triFile);
        if(nread != 1)
        {
            break;
        }

        DREAM3D::SurfaceMesh::Face_t& triangle = m_TriangleList[tRecord.triId];

        triangle.verts[0] = tRecord.nodeId_0;
        triangle.verts[1] = tRecord.nodeId_1;
        triangle.verts[2] = tRecord.nodeId_2;
        faceLabels[tRecord.triId * 2] = tRecord.label_0;
        faceLabels[tRecord.triId * 2 + 1] = tRecord.label_1;
    }

    sm->setVertices(m_NodeListPtr);
    sm->setFaces(m_TriangleListPtr);
    sm->addFaceData(faceLabelPtr->GetName(), faceLabelPtr);
    sm->addVertexData(nodeTypePtr->GetName(), nodeTypePtr);





    // The ScopedFileMonitor classes will take care of closing the files

    return getErrorCondition();
}
Esempio n. 2
0
  void ConvertData(T* ptr, VoxelDataContainer* m, int32_t scalarType, const std::string &name)
  {
    int numberOfComponents = ptr->GetNumberOfComponents();
    int voxels = ptr->GetNumberOfTuples();
    size_t size = ptr->GetSize();


    if (scalarType == Detail::Int8)
    {
      Int8ArrayType::Pointer p = Int8ArrayType::CreateArray(voxels, numberOfComponents, name);
      m->addCellData(p->GetName(), p);
      for(size_t v = 0; v < size; ++v)
      {
        p->SetValue(v, ptr->GetValue(v) );
      }
    }
    else if (scalarType == Detail::UInt8)
    {
      UInt8ArrayType::Pointer p = UInt8ArrayType::CreateArray(voxels, numberOfComponents, name);
      m->addCellData(p->GetName(), p);
      for(size_t v = 0; v < size; ++v)
      {
        p->SetValue(v, ptr->GetValue(v) );
      }
    }
    else if (scalarType == Detail::Int16)
    {
      Int16ArrayType::Pointer p = Int16ArrayType::CreateArray(voxels, numberOfComponents, name);
      m->addCellData(p->GetName(), p);
      for(size_t v = 0; v < size; ++v)
      {
        p->SetValue(v, ptr->GetValue(v) );
      }
    }
    else if (scalarType == Detail::UInt16)
    {
      UInt16ArrayType::Pointer p = UInt16ArrayType::CreateArray(voxels, numberOfComponents, name);
      m->addCellData(p->GetName(), p);
      for(size_t v = 0; v < size; ++v)
      {
        p->SetValue(v, ptr->GetValue(v) );
      }
    }
    else if (scalarType == Detail::Int32)
    {
      Int32ArrayType::Pointer p = Int32ArrayType::CreateArray(voxels, numberOfComponents, name);
      m->addCellData(p->GetName(), p);
      for(size_t v = 0; v < size; ++v)
      {
        p->SetValue(v, ptr->GetValue(v) );
      }
    }
    else if (scalarType == Detail::UInt32)
    {
      UInt32ArrayType::Pointer p = UInt32ArrayType::CreateArray(voxels, numberOfComponents, name);
      m->addCellData(p->GetName(), p);
      for(size_t v = 0; v < size; ++v)
      {
        p->SetValue(v, ptr->GetValue(v) );
      }
    }
    else if (scalarType == Detail::Int64)
    {
      Int64ArrayType::Pointer p = Int64ArrayType::CreateArray(voxels, numberOfComponents, name);
      m->addCellData(p->GetName(), p);
      for(size_t v = 0; v < size; ++v)
      {
        p->SetValue(v, ptr->GetValue(v) );
      }
    }
    else if (scalarType == Detail::UInt64)
    {
      UInt64ArrayType::Pointer p = UInt64ArrayType::CreateArray(voxels, numberOfComponents, name);
      m->addCellData(p->GetName(), p);
      for(size_t v = 0; v < size; ++v)
      {
        p->SetValue(v, ptr->GetValue(v) );
      }
    }
    else if (scalarType == Detail::Float)
    {
      FloatArrayType::Pointer p = FloatArrayType::CreateArray(voxels, numberOfComponents, name);
      m->addCellData(p->GetName(), p);
    }
    else if (scalarType == Detail::Double)
    {
      DoubleArrayType::Pointer p = DoubleArrayType::CreateArray(voxels, numberOfComponents, name);
      m->addCellData(p->GetName(), p);
      for(size_t v = 0; v < size; ++v)
      {
        p->SetValue(v, ptr->GetValue(v) );
      }
    }


  }