Add local average base normals

The base normal of each node will be averaged by sum of all the neighbors' base normal.
master
Jeremy Hu 2019-07-22 19:10:37 +09:30
parent fd7dbb2024
commit 3e82ee597e
2 changed files with 22 additions and 1 deletions

View File

@ -304,6 +304,7 @@ bool Builder::build()
} }
unifyBaseNormals(); unifyBaseNormals();
localAverageBaseNormals();
for (const auto &nodeIndex: m_sortedNodeIndices) { for (const auto &nodeIndex: m_sortedNodeIndices) {
if (!generateCutsForNode(nodeIndex)) if (!generateCutsForNode(nodeIndex))
@ -318,6 +319,25 @@ bool Builder::build()
return succeed; return succeed;
} }
void Builder::localAverageBaseNormals()
{
std::vector<QVector3D> localAverageNormals;
for (size_t nodeIndex = 0; nodeIndex < m_nodes.size(); ++nodeIndex) {
const auto &node = m_nodes[nodeIndex];
QVector3D sumOfNormals = node.baseNormal;
for (const auto &edgeIndex: node.edges) {
const auto &edge = m_edges[edgeIndex];
size_t neighborIndex = edge.neiborOf(nodeIndex);
const auto &neighbor = m_nodes[neighborIndex];
sumOfNormals += neighbor.baseNormal;
}
localAverageNormals.push_back(sumOfNormals.normalized());
}
for (size_t nodeIndex = 0; nodeIndex < m_nodes.size(); ++nodeIndex) {
m_nodes[nodeIndex].baseNormal = localAverageNormals[nodeIndex];
}
}
bool Builder::validateNormal(const QVector3D &normal) bool Builder::validateNormal(const QVector3D &normal)
{ {
if (normal.isNull()) { if (normal.isNull()) {

View File

@ -83,7 +83,7 @@ private:
std::vector<size_t> nodes; std::vector<size_t> nodes;
std::vector<std::pair<std::vector<size_t>, QVector3D>> cuts; std::vector<std::pair<std::vector<size_t>, QVector3D>> cuts;
size_t neiborOf(size_t nodeIndex) size_t neiborOf(size_t nodeIndex) const
{ {
const auto &otherIndex = nodes[0]; const auto &otherIndex = nodes[0];
if (otherIndex == nodeIndex) if (otherIndex == nodeIndex)
@ -131,6 +131,7 @@ private:
QVector3D revisedBaseNormalAcordingToCutNormal(const QVector3D &baseNormal, const QVector3D &cutNormal); QVector3D revisedBaseNormalAcordingToCutNormal(const QVector3D &baseNormal, const QVector3D &cutNormal);
void resolveInitialTraverseDirectionRecursively(size_t nodeIndex, const QVector3D *from, std::set<size_t> *visited); void resolveInitialTraverseDirectionRecursively(size_t nodeIndex, const QVector3D *from, std::set<size_t> *visited);
void unifyBaseNormals(); void unifyBaseNormals();
void localAverageBaseNormals();
void resolveTraverseDirection(size_t nodeIndex); void resolveTraverseDirection(size_t nodeIndex);
bool generateCutsForNode(size_t nodeIndex); bool generateCutsForNode(size_t nodeIndex);
bool tryWrapMultipleBranchesForNode(size_t nodeIndex, std::vector<float> &offsets, bool &offsetsChanged); bool tryWrapMultipleBranchesForNode(size_t nodeIndex, std::vector<float> &offsets, bool &offsetsChanged);