imstkAssimpMeshIO.cpp 4.94 KB
Newer Older
1 2
/*=========================================================================

3
   Library: iMSTK
4

5 6
   Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
   & Imaging in Medicine, Rensselaer Polytechnic Institute.
7

8 9 10
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
11

12
      http://www.apache.org/licenses/LICENSE-2.0.txt
13

14 15 16 17 18
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
19 20 21 22 23 24 25 26 27 28 29 30 31 32

=========================================================================*/

#include "imstkAssimpMeshIO.h"

#include "g3log/g3log.hpp"

namespace imstk
{
std::shared_ptr<SurfaceMesh>
AssimpMeshIO::read(const std::string& filePath, MeshFileType type)
{
    switch (type)
    {
33 34 35 36
    case MeshFileType::OBJ:
    case MeshFileType::DAE:
    case MeshFileType::FBX:
    case MeshFileType::_3DS:
37 38
        return AssimpMeshIO::readMeshData(filePath);
        break;
39
    default:
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
        LOG(WARNING) << "AssimpMeshIO::read error: file type not supported";
        return nullptr;
        break;
    }
}

std::shared_ptr<SurfaceMesh>
AssimpMeshIO::readMeshData(const std::string& filePath)
{
    // Importer mesh(es) and apply some clean-up operations
    Assimp::Importer importer;
    auto scene = importer.ReadFile(filePath,
        aiPostProcessSteps::aiProcess_GenSmoothNormals |
        aiPostProcessSteps::aiProcess_CalcTangentSpace |
        aiPostProcessSteps::aiProcess_JoinIdenticalVertices |
        aiPostProcessSteps::aiProcess_Triangulate);

    // Check if there is actually a mesh or if the file can be read
    if (!scene || !scene->HasMeshes())
    {
        LOG(WARNING) << "AssimpMeshIO::readMeshData error: could not read with reader.";
        return nullptr;
    }

    // Get first mesh
    auto importedMesh = scene->mMeshes[0];

    // Build SurfaceMesh
    auto mesh = std::make_shared<SurfaceMesh>();

    // Get mesh information
    auto numVertices = importedMesh->mNumVertices;
    auto numTriangles = importedMesh->mNumFaces;

    if (numVertices == 0)
    {
        LOG(WARNING) << "AssimpMeshIO::readMeshData error: mesh has no vertices.";
        return nullptr;
    }

    // Vertex positions
    StdVectorOfVec3d positions(numVertices);
    for (unsigned int i = 0; i < numVertices; i++)
    {
        auto positionX = importedMesh->mVertices[i].x;
        auto positionY = importedMesh->mVertices[i].y;
        auto positionZ = importedMesh->mVertices[i].z;
        positions[i] = Vec3d(positionX, positionY, positionZ);
    }

    // Triangles
    std::vector<SurfaceMesh::TriangleArray> triangles(numTriangles);
    for (unsigned int i = 0; i < numTriangles; i++)
    {
        auto triangle = importedMesh->mFaces[i];
        auto indices = triangle.mIndices;
        triangles[i][0] = indices[0];
        triangles[i][1] = indices[1];
        triangles[i][2] = indices[2];
    }

    mesh->initialize(positions, triangles, false);

    // Vertex normals, tangents, and bitangents
    StdVectorOfVec3d normals(numVertices);
    StdVectorOfVec3d tangents(numVertices);
    StdVectorOfVec3d bitangents(numVertices);

    if (importedMesh->HasNormals())
    {
        for (unsigned int i = 0; i < numVertices; i++)
        {
            auto normalX = importedMesh->mNormals[i].x;
            auto normalY = importedMesh->mNormals[i].y;
            auto normalZ = importedMesh->mNormals[i].z;
            normals[i] = Vec3d(normalX, normalY, normalZ);
        }
    }

119
    if (importedMesh->HasTangentsAndBitangents() && importedMesh->HasTextureCoords(0))
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    {
        for (unsigned int i = 0; i < numVertices; i++)
        {
            auto tangentX = importedMesh->mTangents[i].x;
            auto tangentY = importedMesh->mTangents[i].y;
            auto tangentZ = importedMesh->mTangents[i].z;
            tangents[i] = Vec3d(tangentX, tangentY, tangentZ);

            auto bitangentX = importedMesh->mBitangents[i].x;
            auto bitangentY = importedMesh->mBitangents[i].y;
            auto bitangentZ = importedMesh->mBitangents[i].z;
            bitangents[i] = Vec3d(bitangentX, bitangentY, bitangentZ);
        }
    }

    mesh->setVertexNormals(normals);
    mesh->setVertexTangents(tangents);
    mesh->setVertexBitangents(bitangents);

139
    // UV coordinates
140 141 142 143 144 145 146 147 148 149 150
    StdVectorOfVectorf UVs(numVertices);
    if (importedMesh->HasTextureCoords(0))
    {
        auto texcoords = importedMesh->mTextureCoords[0];
        for (unsigned int i = 0; i < numVertices; i++)
        {
            Vectorf UV(2);
            UV[0] = texcoords[i].x;
            UV[1] = texcoords[i].y;
            UVs[i] = UV;
        }
151 152
        mesh->setDefaultTCoords("tCoords");
        mesh->setPointDataArray("tCoords",UVs);
153 154 155 156 157
    }

    return mesh;
}
}