2018-10-26 23:04:45 +00:00
|
|
|
#ifndef DUST3D_RIGGER_H
|
|
|
|
#define DUST3D_RIGGER_H
|
2018-09-14 09:45:05 +00:00
|
|
|
#include <QtGlobal>
|
|
|
|
#include <QVector3D>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QColor>
|
2018-09-18 03:17:35 +00:00
|
|
|
#include <QDebug>
|
2018-11-01 15:24:06 +00:00
|
|
|
#include <QVector2D>
|
2018-09-14 09:45:05 +00:00
|
|
|
#include "meshsplitter.h"
|
2018-10-25 00:19:38 +00:00
|
|
|
#include "bonemark.h"
|
2018-09-14 09:45:05 +00:00
|
|
|
#include "rigtype.h"
|
2018-10-25 00:19:38 +00:00
|
|
|
#include "skeletonside.h"
|
2018-09-14 09:45:05 +00:00
|
|
|
|
2018-10-26 23:04:45 +00:00
|
|
|
class RiggerMark
|
2018-09-14 09:45:05 +00:00
|
|
|
{
|
|
|
|
public:
|
2018-10-25 00:19:38 +00:00
|
|
|
BoneMark boneMark;
|
2018-09-14 09:45:05 +00:00
|
|
|
SkeletonSide boneSide;
|
|
|
|
QVector3D bonePosition;
|
2018-11-01 15:24:06 +00:00
|
|
|
float nodeRadius = 0;
|
2018-09-14 09:45:05 +00:00
|
|
|
std::set<MeshSplitterTriangle> markTriangles;
|
|
|
|
const std::set<MeshSplitterTriangle> &bigGroup() const
|
|
|
|
{
|
2018-11-07 08:02:55 +00:00
|
|
|
return m_isFirstBiggerThenSecond ? m_firstGroup : m_secondGroup;
|
2018-09-14 09:45:05 +00:00
|
|
|
}
|
|
|
|
const std::set<MeshSplitterTriangle> &smallGroup() const
|
|
|
|
{
|
2018-11-07 08:02:55 +00:00
|
|
|
return m_isFirstBiggerThenSecond ? m_secondGroup : m_firstGroup;
|
2018-09-14 09:45:05 +00:00
|
|
|
}
|
2019-10-02 22:51:38 +00:00
|
|
|
bool split(const std::vector<QVector3D> &verticesPositions, const std::set<MeshSplitterTriangle> &input,
|
|
|
|
const std::vector<std::pair<std::pair<size_t, size_t>, std::pair<size_t, size_t>>> &triangleLinks, int expandRound=0)
|
2018-09-14 09:45:05 +00:00
|
|
|
{
|
2018-11-01 15:24:06 +00:00
|
|
|
int totalRound = 1 + expandRound;
|
|
|
|
for (int round = 0; round < totalRound; ++round) {
|
|
|
|
m_firstGroup.clear();
|
|
|
|
m_secondGroup.clear();
|
2019-10-02 22:51:38 +00:00
|
|
|
bool splitResult = MeshSplitter::split(input, triangleLinks, markTriangles, m_firstGroup, m_secondGroup, round > 0);
|
2018-11-07 08:02:55 +00:00
|
|
|
if (splitResult) {
|
|
|
|
sortByDistanceFromOrigin(verticesPositions);
|
2018-11-01 15:24:06 +00:00
|
|
|
return true;
|
2018-11-07 08:02:55 +00:00
|
|
|
}
|
2018-11-01 15:24:06 +00:00
|
|
|
}
|
|
|
|
return false;
|
2018-09-14 09:45:05 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
std::set<MeshSplitterTriangle> m_firstGroup;
|
|
|
|
std::set<MeshSplitterTriangle> m_secondGroup;
|
2018-11-07 08:02:55 +00:00
|
|
|
bool m_isFirstBiggerThenSecond = false;
|
|
|
|
|
|
|
|
float minDistance2FromOrigin(const std::vector<QVector3D> &verticesPositions, const std::set<MeshSplitterTriangle> &triangles)
|
|
|
|
{
|
|
|
|
float minDistance2 = std::numeric_limits<float>::max();
|
|
|
|
for (const auto &triangle: triangles) {
|
|
|
|
for (size_t i = 0; i < 3; ++i) {
|
2018-11-17 23:02:12 +00:00
|
|
|
float distance2 = verticesPositions[triangle.indices[i]].lengthSquared();
|
2018-11-07 08:02:55 +00:00
|
|
|
if (distance2 < minDistance2)
|
|
|
|
minDistance2 = distance2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return minDistance2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sortByDistanceFromOrigin(const std::vector<QVector3D> &verticesPositions)
|
|
|
|
{
|
|
|
|
m_isFirstBiggerThenSecond = minDistance2FromOrigin(verticesPositions, m_firstGroup) <
|
|
|
|
minDistance2FromOrigin(verticesPositions, m_secondGroup);
|
|
|
|
}
|
2018-09-14 09:45:05 +00:00
|
|
|
};
|
|
|
|
|
2018-10-26 23:04:45 +00:00
|
|
|
class RiggerBone
|
2018-09-14 09:45:05 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
QString name;
|
2018-11-01 15:24:06 +00:00
|
|
|
int index = -1;
|
2018-09-14 09:45:05 +00:00
|
|
|
QVector3D headPosition;
|
|
|
|
QVector3D tailPosition;
|
2019-09-21 14:30:27 +00:00
|
|
|
float headRadius = 0.0;
|
|
|
|
float tailRadius = 0.0;
|
2018-09-14 09:45:05 +00:00
|
|
|
QColor color;
|
2018-11-01 15:24:06 +00:00
|
|
|
QVector3D baseNormal;
|
2018-09-14 09:45:05 +00:00
|
|
|
std::vector<int> children;
|
|
|
|
};
|
|
|
|
|
2018-10-26 23:04:45 +00:00
|
|
|
class RiggerVertexWeights
|
2018-09-14 09:45:05 +00:00
|
|
|
{
|
|
|
|
public:
|
2018-11-17 23:02:12 +00:00
|
|
|
int boneIndices[4] = {0, 0, 0, 0};
|
2018-09-14 09:45:05 +00:00
|
|
|
float boneWeights[4] = {0, 0, 0, 0};
|
|
|
|
void addBone(int boneIndex, float distance)
|
|
|
|
{
|
2018-11-17 23:02:12 +00:00
|
|
|
if (m_boneRawIndices.find(boneIndex) != m_boneRawIndices.end())
|
2018-11-01 15:24:06 +00:00
|
|
|
return;
|
2018-11-17 23:02:12 +00:00
|
|
|
m_boneRawIndices.insert(boneIndex);
|
2018-09-14 09:45:05 +00:00
|
|
|
if (qFuzzyIsNull(distance))
|
2018-09-18 03:17:35 +00:00
|
|
|
distance = 0.0001;
|
2018-09-14 09:45:05 +00:00
|
|
|
m_boneRawWeights.push_back(std::make_pair(boneIndex, 1.0 / distance));
|
|
|
|
}
|
|
|
|
void finalizeWeights()
|
|
|
|
{
|
|
|
|
std::sort(m_boneRawWeights.begin(), m_boneRawWeights.end(),
|
|
|
|
[](const std::pair<int, float> &a, const std::pair<int, float> &b) {
|
|
|
|
return a.second > b.second;
|
|
|
|
});
|
|
|
|
float totalDistance = 0;
|
|
|
|
for (size_t i = 0; i < m_boneRawWeights.size() && i < 4; i++) {
|
|
|
|
const auto &item = m_boneRawWeights[i];
|
|
|
|
totalDistance += item.second;
|
|
|
|
}
|
|
|
|
if (totalDistance > 0) {
|
|
|
|
for (size_t i = 0; i < m_boneRawWeights.size() && i < 4; i++) {
|
|
|
|
const auto &item = m_boneRawWeights[i];
|
2018-11-17 23:02:12 +00:00
|
|
|
boneIndices[i] = item.first;
|
2018-09-14 09:45:05 +00:00
|
|
|
boneWeights[i] = item.second / totalDistance;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qDebug() << "totalDistance:" << totalDistance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
2018-11-17 23:02:12 +00:00
|
|
|
std::set<int> m_boneRawIndices;
|
2018-09-14 09:45:05 +00:00
|
|
|
std::vector<std::pair<int, float>> m_boneRawWeights;
|
|
|
|
};
|
|
|
|
|
2018-10-26 23:04:45 +00:00
|
|
|
class Rigger : public QObject
|
2018-09-14 09:45:05 +00:00
|
|
|
{
|
2019-06-27 12:44:33 +00:00
|
|
|
Q_OBJECT
|
2018-09-14 09:45:05 +00:00
|
|
|
public:
|
2018-10-26 23:04:45 +00:00
|
|
|
Rigger(const std::vector<QVector3D> &verticesPositions,
|
2019-10-02 22:51:38 +00:00
|
|
|
const std::set<MeshSplitterTriangle> &inputTriangles,
|
|
|
|
const std::vector<std::pair<std::pair<size_t, size_t>, std::pair<size_t, size_t>>> &triangleLinks);
|
2019-02-18 12:57:18 +00:00
|
|
|
bool addMarkGroup(BoneMark boneMark, SkeletonSide boneSide, QVector3D bonePosition, float nodeRadius, const std::set<MeshSplitterTriangle> &markTriangles);
|
2018-09-14 09:45:05 +00:00
|
|
|
const std::vector<std::pair<QtMsgType, QString>> &messages();
|
2018-10-26 23:04:45 +00:00
|
|
|
const std::vector<RiggerBone> &resultBones();
|
|
|
|
const std::map<int, RiggerVertexWeights> &resultWeights();
|
2018-11-01 15:24:06 +00:00
|
|
|
virtual bool rig() = 0;
|
2018-11-05 15:47:21 +00:00
|
|
|
static QString rootBoneName;
|
2019-06-10 06:42:08 +00:00
|
|
|
//static QString firstSpineBoneName;
|
2018-11-01 15:24:06 +00:00
|
|
|
protected:
|
|
|
|
virtual bool validate() = 0;
|
|
|
|
virtual bool isCutOffSplitter(BoneMark boneMark) = 0;
|
|
|
|
virtual BoneMark translateBoneMark(BoneMark boneMark);
|
2018-09-14 09:45:05 +00:00
|
|
|
void addTrianglesToVertices(const std::set<MeshSplitterTriangle> &triangles, std::set<int> &vertices);
|
|
|
|
bool calculateBodyTriangles(std::set<MeshSplitterTriangle> &bodyTriangles);
|
|
|
|
void resolveBoundingBox(const std::set<int> &vertices, QVector3D &xMin, QVector3D &xMax, QVector3D &yMin, QVector3D &yMax, QVector3D &zMin, QVector3D &zMax);
|
|
|
|
QVector3D findMinX(const std::set<int> &vertices);
|
|
|
|
QVector3D findMaxX(const std::set<int> &vertices);
|
|
|
|
QVector3D findMinY(const std::set<int> &vertices);
|
|
|
|
QVector3D findMaxY(const std::set<int> &vertices);
|
|
|
|
QVector3D findMinZ(const std::set<int> &vertices);
|
|
|
|
QVector3D findMaxZ(const std::set<int> &vertices);
|
2018-11-01 15:24:06 +00:00
|
|
|
QVector3D averagePosition(const std::set<int> &vertices);
|
2018-09-14 09:45:05 +00:00
|
|
|
void splitVerticesByY(const std::set<int> &vertices, float y, std::set<int> &greaterEqualThanVertices, std::set<int> &lessThanVertices);
|
|
|
|
void splitVerticesByX(const std::set<int> &vertices, float x, std::set<int> &greaterEqualThanVertices, std::set<int> &lessThanVertices);
|
|
|
|
void splitVerticesByZ(const std::set<int> &vertices, float z, std::set<int> &greaterEqualThanVertices, std::set<int> &lessThanVertices);
|
2018-11-01 15:24:06 +00:00
|
|
|
void splitVerticesByPlane(const std::set<int> &vertices, const QVector3D &pointOnPlane, const QVector3D &planeNormal, std::set<int> &frontOrCoincidentVertices, std::set<int> &backVertices);
|
2018-09-14 09:45:05 +00:00
|
|
|
void addVerticesToWeights(const std::set<int> &vertices, int boneIndex);
|
|
|
|
std::vector<std::pair<QtMsgType, QString>> m_messages;
|
|
|
|
std::vector<QVector3D> m_verticesPositions;
|
|
|
|
std::set<MeshSplitterTriangle> m_inputTriangles;
|
2019-10-02 22:51:38 +00:00
|
|
|
std::vector<std::pair<std::pair<size_t, size_t>, std::pair<size_t, size_t>>> m_triangleLinks;
|
2018-10-26 23:04:45 +00:00
|
|
|
std::vector<RiggerMark> m_marks;
|
2018-10-25 00:19:38 +00:00
|
|
|
std::map<std::pair<BoneMark, SkeletonSide>, std::vector<int>> m_marksMap;
|
2018-10-26 23:04:45 +00:00
|
|
|
std::vector<RiggerBone> m_resultBones;
|
|
|
|
std::map<int, RiggerVertexWeights> m_resultWeights;
|
2018-11-06 16:26:19 +00:00
|
|
|
std::vector<QString> m_cutoffErrorItems;
|
|
|
|
std::vector<QString> m_jointErrorItems;
|
2018-11-01 15:24:06 +00:00
|
|
|
static size_t m_maxCutOffSplitterExpandRound;
|
2018-11-06 16:26:19 +00:00
|
|
|
bool m_extraMessagedAdded;
|
2018-09-14 09:45:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|