2015-05-19 03:48:29 +00:00
|
|
|
/** Implementation of the BVH loader */
|
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2020-01-20 13:53:12 +00:00
|
|
|
Copyright (c) 2006-2020, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2017-05-09 17:57:36 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the following
|
2015-05-19 03:48:29 +00:00
|
|
|
conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
2015-05-19 03:52:10 +00:00
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
2015-05-19 03:48:29 +00:00
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
2015-05-19 03:52:10 +00:00
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
2015-05-19 03:52:10 +00:00
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
2015-05-19 03:48:29 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_BVH_IMPORTER
|
|
|
|
|
|
|
|
#include "BVHLoader.h"
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/SkeletonMeshBuilder.h>
|
|
|
|
#include <assimp/TinyFormatter.h>
|
2020-05-02 13:14:38 +00:00
|
|
|
#include <assimp/fast_atof.h>
|
2017-02-22 16:20:26 +00:00
|
|
|
#include <assimp/importerdesc.h>
|
2020-05-02 13:14:38 +00:00
|
|
|
#include <assimp/scene.h>
|
|
|
|
#include <assimp/IOSystem.hpp>
|
|
|
|
#include <assimp/Importer.hpp>
|
2018-08-08 22:22:10 +00:00
|
|
|
#include <map>
|
2020-05-02 13:14:38 +00:00
|
|
|
#include <memory>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
using namespace Assimp;
|
2016-04-05 22:03:05 +00:00
|
|
|
using namespace Assimp::Formatter;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
static const aiImporterDesc desc = {
|
2015-05-19 03:57:13 +00:00
|
|
|
"BVH Importer (MoCap)",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
aiImporterFlags_SupportTextFlavour,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
"bvh"
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
2020-08-18 15:32:34 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Aborts the file reading with an exception
|
|
|
|
template<typename... T>
|
|
|
|
AI_WONT_RETURN void BVHLoader::ThrowException(T&&... args) {
|
|
|
|
throw DeadlyImportError(mFileName, ":", mLine, " - ", args...);
|
|
|
|
}
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor to be privately used by Importer
|
2020-05-02 13:14:38 +00:00
|
|
|
BVHLoader::BVHLoader() :
|
|
|
|
mLine(),
|
|
|
|
mAnimTickDuration(),
|
|
|
|
mAnimNumFrames(),
|
|
|
|
noSkeletonMesh() {}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Destructor, private as well
|
2020-05-02 13:14:38 +00:00
|
|
|
BVHLoader::~BVHLoader() {}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
// Returns whether the class can handle the format of the given file.
|
2020-05-02 13:14:38 +00:00
|
|
|
bool BVHLoader::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool cs) const {
|
2015-05-19 03:57:13 +00:00
|
|
|
// check file extension
|
|
|
|
const std::string extension = GetExtension(pFile);
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
if (extension == "bvh")
|
2015-05-19 03:57:13 +00:00
|
|
|
return true;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
if ((!extension.length() || cs) && pIOHandler) {
|
2020-05-02 13:14:38 +00:00
|
|
|
const char *tokens[] = { "HIERARCHY" };
|
|
|
|
return SearchFileHeaderForToken(pIOHandler, pFile, tokens, 1);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
return false;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-05-02 13:14:38 +00:00
|
|
|
void BVHLoader::SetupProperties(const Importer *pImp) {
|
|
|
|
noSkeletonMesh = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_NO_SKELETON_MESHES, 0) != 0;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Loader meta information
|
2020-05-02 13:14:38 +00:00
|
|
|
const aiImporterDesc *BVHLoader::GetInfo() const {
|
2015-05-19 03:57:13 +00:00
|
|
|
return &desc;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
// Imports the given file into the given scene structure.
|
2020-05-02 13:14:38 +00:00
|
|
|
void BVHLoader::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
|
2015-05-19 03:57:13 +00:00
|
|
|
mFileName = pFile;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// read file into memory
|
2020-05-02 13:14:38 +00:00
|
|
|
std::unique_ptr<IOStream> file(pIOHandler->Open(pFile));
|
2020-06-21 10:03:38 +00:00
|
|
|
if (file.get() == nullptr) {
|
2020-08-18 16:35:08 +00:00
|
|
|
throw DeadlyImportError("Failed to open file ", pFile, ".");
|
2020-06-21 10:03:38 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
size_t fileSize = file->FileSize();
|
2020-06-21 10:03:38 +00:00
|
|
|
if (fileSize == 0) {
|
|
|
|
throw DeadlyImportError("File is too small.");
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
mBuffer.resize(fileSize);
|
|
|
|
file->Read(&mBuffer.front(), 1, fileSize);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// start reading
|
|
|
|
mReader = mBuffer.begin();
|
|
|
|
mLine = 1;
|
2020-05-02 13:14:38 +00:00
|
|
|
ReadStructure(pScene);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
if (!noSkeletonMesh) {
|
|
|
|
// build a dummy mesh for the skeleton so that we see something at least
|
2020-05-02 13:14:38 +00:00
|
|
|
SkeletonMeshBuilder meshBuilder(pScene);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// construct an animation from all the motion data we read
|
2020-05-02 13:14:38 +00:00
|
|
|
CreateAnimation(pScene);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the file
|
2020-05-02 13:14:38 +00:00
|
|
|
void BVHLoader::ReadStructure(aiScene *pScene) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// first comes hierarchy
|
|
|
|
std::string header = GetNextToken();
|
2020-05-02 13:14:38 +00:00
|
|
|
if (header != "HIERARCHY")
|
|
|
|
ThrowException("Expected header string \"HIERARCHY\".");
|
|
|
|
ReadHierarchy(pScene);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// then comes the motion data
|
|
|
|
std::string motion = GetNextToken();
|
2020-05-02 13:14:38 +00:00
|
|
|
if (motion != "MOTION")
|
|
|
|
ThrowException("Expected beginning of motion data \"MOTION\".");
|
|
|
|
ReadMotion(pScene);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the hierarchy
|
2020-05-02 13:14:38 +00:00
|
|
|
void BVHLoader::ReadHierarchy(aiScene *pScene) {
|
2015-05-19 03:57:13 +00:00
|
|
|
std::string root = GetNextToken();
|
2020-05-02 13:14:38 +00:00
|
|
|
if (root != "ROOT")
|
|
|
|
ThrowException("Expected root node \"ROOT\".");
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Go read the hierarchy from here
|
|
|
|
pScene->mRootNode = ReadNode();
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads a node and recursively its childs and returns the created node;
|
2020-05-02 13:14:38 +00:00
|
|
|
aiNode *BVHLoader::ReadNode() {
|
2015-05-19 03:57:13 +00:00
|
|
|
// first token is name
|
|
|
|
std::string nodeName = GetNextToken();
|
2020-05-02 13:14:38 +00:00
|
|
|
if (nodeName.empty() || nodeName == "{")
|
2020-08-18 15:32:34 +00:00
|
|
|
ThrowException("Expected node name, but found \"", nodeName, "\".");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// then an opening brace should follow
|
|
|
|
std::string openBrace = GetNextToken();
|
2020-05-02 13:14:38 +00:00
|
|
|
if (openBrace != "{")
|
2020-08-18 15:32:34 +00:00
|
|
|
ThrowException("Expected opening brace \"{\", but found \"", openBrace, "\".");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Create a node
|
2020-05-02 13:14:38 +00:00
|
|
|
aiNode *node = new aiNode(nodeName);
|
|
|
|
std::vector<aiNode *> childNodes;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// and create an bone entry for it
|
2020-05-02 13:14:38 +00:00
|
|
|
mNodes.push_back(Node(node));
|
|
|
|
Node &internNode = mNodes.back();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// now read the node's contents
|
2018-03-20 17:43:54 +00:00
|
|
|
std::string siteToken;
|
2020-05-02 13:14:38 +00:00
|
|
|
while (1) {
|
2015-05-19 03:57:13 +00:00
|
|
|
std::string token = GetNextToken();
|
|
|
|
|
|
|
|
// node offset to parent node
|
2020-05-02 13:14:38 +00:00
|
|
|
if (token == "OFFSET")
|
|
|
|
ReadNodeOffset(node);
|
|
|
|
else if (token == "CHANNELS")
|
|
|
|
ReadNodeChannels(internNode);
|
|
|
|
else if (token == "JOINT") {
|
2015-05-19 03:57:13 +00:00
|
|
|
// child node follows
|
2020-05-02 13:14:38 +00:00
|
|
|
aiNode *child = ReadNode();
|
2015-05-19 03:57:13 +00:00
|
|
|
child->mParent = node;
|
2020-05-02 13:14:38 +00:00
|
|
|
childNodes.push_back(child);
|
|
|
|
} else if (token == "End") {
|
2015-05-19 03:57:13 +00:00
|
|
|
// The real symbol is "End Site". Second part comes in a separate token
|
2018-03-20 17:43:54 +00:00
|
|
|
siteToken.clear();
|
|
|
|
siteToken = GetNextToken();
|
2020-05-02 13:14:38 +00:00
|
|
|
if (siteToken != "Site")
|
2020-08-18 15:32:34 +00:00
|
|
|
ThrowException("Expected \"End Site\" keyword, but found \"", token, " ", siteToken, "\".");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
aiNode *child = ReadEndSite(nodeName);
|
2015-05-19 03:57:13 +00:00
|
|
|
child->mParent = node;
|
2020-05-02 13:14:38 +00:00
|
|
|
childNodes.push_back(child);
|
|
|
|
} else if (token == "}") {
|
2015-05-19 03:57:13 +00:00
|
|
|
// we're done with that part of the hierarchy
|
|
|
|
break;
|
2020-05-02 13:14:38 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// everything else is a parse error
|
2020-08-18 15:32:34 +00:00
|
|
|
ThrowException("Unknown keyword \"", token, "\".");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the child nodes if there are any
|
2020-05-02 13:14:38 +00:00
|
|
|
if (childNodes.size() > 0) {
|
2016-11-18 15:33:58 +00:00
|
|
|
node->mNumChildren = static_cast<unsigned int>(childNodes.size());
|
2020-05-02 13:14:38 +00:00
|
|
|
node->mChildren = new aiNode *[node->mNumChildren];
|
|
|
|
std::copy(childNodes.begin(), childNodes.end(), node->mChildren);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// and return the sub-hierarchy we built here
|
|
|
|
return node;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads an end node and returns the created node.
|
2020-05-02 13:14:38 +00:00
|
|
|
aiNode *BVHLoader::ReadEndSite(const std::string &pParentName) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// check opening brace
|
|
|
|
std::string openBrace = GetNextToken();
|
2020-05-02 13:14:38 +00:00
|
|
|
if (openBrace != "{")
|
2020-08-18 15:32:34 +00:00
|
|
|
ThrowException("Expected opening brace \"{\", but found \"", openBrace, "\".");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Create a node
|
2020-05-02 13:14:38 +00:00
|
|
|
aiNode *node = new aiNode("EndSite_" + pParentName);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// now read the node's contents. Only possible entry is "OFFSET"
|
2018-03-20 17:43:54 +00:00
|
|
|
std::string token;
|
2020-05-02 13:14:38 +00:00
|
|
|
while (1) {
|
2018-03-20 17:43:54 +00:00
|
|
|
token.clear();
|
|
|
|
token = GetNextToken();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// end node's offset
|
2020-05-02 13:14:38 +00:00
|
|
|
if (token == "OFFSET") {
|
|
|
|
ReadNodeOffset(node);
|
|
|
|
} else if (token == "}") {
|
2015-05-19 03:57:13 +00:00
|
|
|
// we're done with the end node
|
|
|
|
break;
|
2018-03-20 17:43:54 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// everything else is a parse error
|
2020-08-18 15:32:34 +00:00
|
|
|
ThrowException("Unknown keyword \"", token, "\".");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// and return the sub-hierarchy we built here
|
|
|
|
return node;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads a node offset for the given node
|
2020-05-02 13:14:38 +00:00
|
|
|
void BVHLoader::ReadNodeOffset(aiNode *pNode) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Offset consists of three floats to read
|
|
|
|
aiVector3D offset;
|
|
|
|
offset.x = GetNextTokenAsFloat();
|
|
|
|
offset.y = GetNextTokenAsFloat();
|
|
|
|
offset.z = GetNextTokenAsFloat();
|
|
|
|
|
|
|
|
// build a transformation matrix from it
|
2020-05-02 13:14:38 +00:00
|
|
|
pNode->mTransformation = aiMatrix4x4(1.0f, 0.0f, 0.0f, offset.x,
|
|
|
|
0.0f, 1.0f, 0.0f, offset.y,
|
|
|
|
0.0f, 0.0f, 1.0f, offset.z,
|
|
|
|
0.0f, 0.0f, 0.0f, 1.0f);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the animation channels for the given node
|
2020-05-02 13:14:38 +00:00
|
|
|
void BVHLoader::ReadNodeChannels(BVHLoader::Node &pNode) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// number of channels. Use the float reader because we're lazy
|
|
|
|
float numChannelsFloat = GetNextTokenAsFloat();
|
2020-05-02 13:14:38 +00:00
|
|
|
unsigned int numChannels = (unsigned int)numChannelsFloat;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
for (unsigned int a = 0; a < numChannels; a++) {
|
2015-05-19 03:57:13 +00:00
|
|
|
std::string channelToken = GetNextToken();
|
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
if (channelToken == "Xposition")
|
|
|
|
pNode.mChannels.push_back(Channel_PositionX);
|
|
|
|
else if (channelToken == "Yposition")
|
|
|
|
pNode.mChannels.push_back(Channel_PositionY);
|
|
|
|
else if (channelToken == "Zposition")
|
|
|
|
pNode.mChannels.push_back(Channel_PositionZ);
|
|
|
|
else if (channelToken == "Xrotation")
|
|
|
|
pNode.mChannels.push_back(Channel_RotationX);
|
|
|
|
else if (channelToken == "Yrotation")
|
|
|
|
pNode.mChannels.push_back(Channel_RotationY);
|
|
|
|
else if (channelToken == "Zrotation")
|
|
|
|
pNode.mChannels.push_back(Channel_RotationZ);
|
2015-05-19 03:57:13 +00:00
|
|
|
else
|
2020-08-18 15:32:34 +00:00
|
|
|
ThrowException("Invalid channel specifier \"", channelToken, "\".");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the motion data
|
2020-05-02 13:14:38 +00:00
|
|
|
void BVHLoader::ReadMotion(aiScene * /*pScene*/) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Read number of frames
|
|
|
|
std::string tokenFrames = GetNextToken();
|
2020-05-02 13:14:38 +00:00
|
|
|
if (tokenFrames != "Frames:")
|
2020-08-18 15:32:34 +00:00
|
|
|
ThrowException("Expected frame count \"Frames:\", but found \"", tokenFrames, "\".");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
float numFramesFloat = GetNextTokenAsFloat();
|
2020-05-02 13:14:38 +00:00
|
|
|
mAnimNumFrames = (unsigned int)numFramesFloat;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Read frame duration
|
|
|
|
std::string tokenDuration1 = GetNextToken();
|
|
|
|
std::string tokenDuration2 = GetNextToken();
|
2020-05-02 13:14:38 +00:00
|
|
|
if (tokenDuration1 != "Frame" || tokenDuration2 != "Time:")
|
2020-08-18 15:32:34 +00:00
|
|
|
ThrowException("Expected frame duration \"Frame Time:\", but found \"", tokenDuration1, " ", tokenDuration2, "\".");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
mAnimTickDuration = GetNextTokenAsFloat();
|
|
|
|
|
|
|
|
// resize value vectors for each node
|
2020-05-02 13:14:38 +00:00
|
|
|
for (std::vector<Node>::iterator it = mNodes.begin(); it != mNodes.end(); ++it)
|
|
|
|
it->mChannelValues.reserve(it->mChannels.size() * mAnimNumFrames);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// now read all the data and store it in the corresponding node's value vector
|
2020-05-02 13:14:38 +00:00
|
|
|
for (unsigned int frame = 0; frame < mAnimNumFrames; ++frame) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// on each line read the values for all nodes
|
2020-05-02 13:14:38 +00:00
|
|
|
for (std::vector<Node>::iterator it = mNodes.begin(); it != mNodes.end(); ++it) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// get as many values as the node has channels
|
2020-05-02 13:14:38 +00:00
|
|
|
for (unsigned int c = 0; c < it->mChannels.size(); ++c)
|
|
|
|
it->mChannelValues.push_back(GetNextTokenAsFloat());
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// after one frame worth of values for all nodes there should be a newline, but we better don't rely on it
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Retrieves the next token
|
2020-05-02 13:14:38 +00:00
|
|
|
std::string BVHLoader::GetNextToken() {
|
2016-04-03 00:38:00 +00:00
|
|
|
// skip any preceding whitespace
|
2020-05-02 13:14:38 +00:00
|
|
|
while (mReader != mBuffer.end()) {
|
|
|
|
if (!isspace(*mReader))
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// count lines
|
2020-05-02 13:14:38 +00:00
|
|
|
if (*mReader == '\n')
|
2015-05-19 03:57:13 +00:00
|
|
|
mLine++;
|
|
|
|
|
|
|
|
++mReader;
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect all chars till the next whitespace. BVH is easy in respect to that.
|
|
|
|
std::string token;
|
2020-05-02 13:14:38 +00:00
|
|
|
while (mReader != mBuffer.end()) {
|
|
|
|
if (isspace(*mReader))
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
token.push_back(*mReader);
|
2015-05-19 03:57:13 +00:00
|
|
|
++mReader;
|
|
|
|
|
|
|
|
// little extra logic to make sure braces are counted correctly
|
2020-05-02 13:14:38 +00:00
|
|
|
if (token == "{" || token == "}")
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// empty token means end of file, which is just fine
|
|
|
|
return token;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the next token as a float
|
2020-05-02 13:14:38 +00:00
|
|
|
float BVHLoader::GetNextTokenAsFloat() {
|
2015-05-19 03:57:13 +00:00
|
|
|
std::string token = GetNextToken();
|
2020-05-02 13:14:38 +00:00
|
|
|
if (token.empty())
|
|
|
|
ThrowException("Unexpected end of file while trying to read a float");
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// check if the float is valid by testing if the atof() function consumed every char of the token
|
2020-05-02 13:14:38 +00:00
|
|
|
const char *ctoken = token.c_str();
|
2015-05-19 03:57:13 +00:00
|
|
|
float result = 0.0f;
|
2020-05-02 13:14:38 +00:00
|
|
|
ctoken = fast_atoreal_move<float>(ctoken, result);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
if (ctoken != token.c_str() + token.length())
|
2020-08-18 15:32:34 +00:00
|
|
|
ThrowException("Expected a floating point number, but found \"", token, "\".");
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
return result;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructs an animation for the motion data and stores it in the given scene
|
2020-05-02 13:14:38 +00:00
|
|
|
void BVHLoader::CreateAnimation(aiScene *pScene) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// create the animation
|
|
|
|
pScene->mNumAnimations = 1;
|
2020-05-02 13:14:38 +00:00
|
|
|
pScene->mAnimations = new aiAnimation *[1];
|
|
|
|
aiAnimation *anim = new aiAnimation;
|
2015-05-19 03:57:13 +00:00
|
|
|
pScene->mAnimations[0] = anim;
|
|
|
|
|
|
|
|
// put down the basic parameters
|
2020-05-02 13:14:38 +00:00
|
|
|
anim->mName.Set("Motion");
|
|
|
|
anim->mTicksPerSecond = 1.0 / double(mAnimTickDuration);
|
|
|
|
anim->mDuration = double(mAnimNumFrames - 1);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// now generate the tracks for all nodes
|
2016-11-18 15:33:58 +00:00
|
|
|
anim->mNumChannels = static_cast<unsigned int>(mNodes.size());
|
2020-05-02 13:14:38 +00:00
|
|
|
anim->mChannels = new aiNodeAnim *[anim->mNumChannels];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// FIX: set the array elements to nullptr to ensure proper deletion if an exception is thrown
|
2020-05-02 13:14:38 +00:00
|
|
|
for (unsigned int i = 0; i < anim->mNumChannels; ++i)
|
2020-06-23 19:05:42 +00:00
|
|
|
anim->mChannels[i] = nullptr;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
for (unsigned int a = 0; a < anim->mNumChannels; a++) {
|
|
|
|
const Node &node = mNodes[a];
|
|
|
|
const std::string nodeName = std::string(node.mNode->mName.data);
|
|
|
|
aiNodeAnim *nodeAnim = new aiNodeAnim;
|
2015-05-19 03:57:13 +00:00
|
|
|
anim->mChannels[a] = nodeAnim;
|
2020-05-02 13:14:38 +00:00
|
|
|
nodeAnim->mNodeName.Set(nodeName);
|
|
|
|
std::map<BVHLoader::ChannelType, int> channelMap;
|
2018-08-08 22:22:10 +00:00
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
//Build map of channels
|
|
|
|
for (unsigned int channel = 0; channel < node.mChannels.size(); ++channel) {
|
|
|
|
channelMap[node.mChannels[channel]] = channel;
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// translational part, if given
|
2020-05-02 13:14:38 +00:00
|
|
|
if (node.mChannels.size() == 6) {
|
2015-05-19 03:57:13 +00:00
|
|
|
nodeAnim->mNumPositionKeys = mAnimNumFrames;
|
|
|
|
nodeAnim->mPositionKeys = new aiVectorKey[mAnimNumFrames];
|
2020-05-02 13:14:38 +00:00
|
|
|
aiVectorKey *poskey = nodeAnim->mPositionKeys;
|
|
|
|
for (unsigned int fr = 0; fr < mAnimNumFrames; ++fr) {
|
|
|
|
poskey->mTime = double(fr);
|
|
|
|
|
|
|
|
// Now compute all translations
|
|
|
|
for (BVHLoader::ChannelType channel = Channel_PositionX; channel <= Channel_PositionZ; channel = (BVHLoader::ChannelType)(channel + 1)) {
|
|
|
|
//Find channel in node
|
|
|
|
std::map<BVHLoader::ChannelType, int>::iterator mapIter = channelMap.find(channel);
|
|
|
|
|
|
|
|
if (mapIter == channelMap.end())
|
2020-08-18 16:35:08 +00:00
|
|
|
throw DeadlyImportError("Missing position channel in node ", nodeName);
|
2020-05-02 13:14:38 +00:00
|
|
|
else {
|
|
|
|
int channelIdx = mapIter->second;
|
|
|
|
switch (channel) {
|
|
|
|
case Channel_PositionX:
|
|
|
|
poskey->mValue.x = node.mChannelValues[fr * node.mChannels.size() + channelIdx];
|
|
|
|
break;
|
|
|
|
case Channel_PositionY:
|
|
|
|
poskey->mValue.y = node.mChannelValues[fr * node.mChannels.size() + channelIdx];
|
|
|
|
break;
|
|
|
|
case Channel_PositionZ:
|
|
|
|
poskey->mValue.z = node.mChannelValues[fr * node.mChannels.size() + channelIdx];
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
++poskey;
|
|
|
|
}
|
2020-05-02 13:14:38 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// if no translation part is given, put a default sequence
|
2020-05-02 13:14:38 +00:00
|
|
|
aiVector3D nodePos(node.mNode->mTransformation.a4, node.mNode->mTransformation.b4, node.mNode->mTransformation.c4);
|
2015-05-19 03:57:13 +00:00
|
|
|
nodeAnim->mNumPositionKeys = 1;
|
|
|
|
nodeAnim->mPositionKeys = new aiVectorKey[1];
|
|
|
|
nodeAnim->mPositionKeys[0].mTime = 0.0;
|
|
|
|
nodeAnim->mPositionKeys[0].mValue = nodePos;
|
|
|
|
}
|
|
|
|
|
|
|
|
// rotation part. Always present. First find value offsets
|
|
|
|
{
|
|
|
|
|
|
|
|
// Then create the number of rotation keys
|
|
|
|
nodeAnim->mNumRotationKeys = mAnimNumFrames;
|
|
|
|
nodeAnim->mRotationKeys = new aiQuatKey[mAnimNumFrames];
|
2020-05-02 13:14:38 +00:00
|
|
|
aiQuatKey *rotkey = nodeAnim->mRotationKeys;
|
|
|
|
for (unsigned int fr = 0; fr < mAnimNumFrames; ++fr) {
|
2015-05-19 03:57:13 +00:00
|
|
|
aiMatrix4x4 temp;
|
|
|
|
aiMatrix3x3 rotMatrix;
|
2020-05-18 15:07:46 +00:00
|
|
|
for (unsigned int channelIdx = 0; channelIdx < node.mChannels.size(); ++ channelIdx) {
|
|
|
|
switch (node.mChannels[channelIdx]) {
|
|
|
|
case Channel_RotationX:
|
|
|
|
{
|
2020-05-02 13:14:38 +00:00
|
|
|
const float angle = node.mChannelValues[fr * node.mChannels.size() + channelIdx] * float(AI_MATH_PI) / 180.0f;
|
2020-05-18 15:07:46 +00:00
|
|
|
aiMatrix4x4::RotationX( angle, temp); rotMatrix *= aiMatrix3x3( temp);
|
2020-05-02 13:14:38 +00:00
|
|
|
}
|
2020-05-18 15:07:46 +00:00
|
|
|
break;
|
|
|
|
case Channel_RotationY:
|
|
|
|
{
|
|
|
|
const float angle = node.mChannelValues[fr * node.mChannels.size() + channelIdx] * float(AI_MATH_PI) / 180.0f;
|
|
|
|
aiMatrix4x4::RotationY( angle, temp); rotMatrix *= aiMatrix3x3( temp);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Channel_RotationZ:
|
|
|
|
{
|
|
|
|
const float angle = node.mChannelValues[fr * node.mChannels.size() + channelIdx] * float(AI_MATH_PI) / 180.0f;
|
|
|
|
aiMatrix4x4::RotationZ( angle, temp); rotMatrix *= aiMatrix3x3( temp);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2018-08-08 22:22:10 +00:00
|
|
|
}
|
2020-05-18 15:07:46 +00:00
|
|
|
}
|
2020-05-02 13:14:38 +00:00
|
|
|
rotkey->mTime = double(fr);
|
|
|
|
rotkey->mValue = aiQuaternion(rotMatrix);
|
2015-05-19 03:57:13 +00:00
|
|
|
++rotkey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// scaling part. Always just a default track
|
|
|
|
{
|
|
|
|
nodeAnim->mNumScalingKeys = 1;
|
|
|
|
nodeAnim->mScalingKeys = new aiVectorKey[1];
|
|
|
|
nodeAnim->mScalingKeys[0].mTime = 0.0;
|
2020-05-02 13:14:38 +00:00
|
|
|
nodeAnim->mScalingKeys[0].mValue.Set(1.0f, 1.0f, 1.0f);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !! ASSIMP_BUILD_NO_BVH_IMPORTER
|