2019-02-18 12:57:18 +00:00
|
|
|
#include <QVector2D>
|
|
|
|
#include <QDebug>
|
2019-12-14 13:28:14 +00:00
|
|
|
#include "strokemodifier.h"
|
|
|
|
#include "util.h"
|
2019-02-18 12:57:18 +00:00
|
|
|
|
2019-12-14 13:28:14 +00:00
|
|
|
void StrokeModifier::enableIntermediateAddition()
|
2019-07-20 02:42:24 +00:00
|
|
|
{
|
|
|
|
m_intermediateAdditionEnabled = true;
|
|
|
|
}
|
|
|
|
|
2019-12-14 13:28:14 +00:00
|
|
|
size_t StrokeModifier::addNode(const QVector3D &position, float radius, const std::vector<QVector2D> &cutTemplate, float cutRotation)
|
2019-02-18 12:57:18 +00:00
|
|
|
{
|
|
|
|
size_t nodeIndex = m_nodes.size();
|
|
|
|
|
|
|
|
Node node;
|
2019-07-02 21:49:18 +00:00
|
|
|
node.isOriginal = true;
|
2019-02-18 12:57:18 +00:00
|
|
|
node.position = position;
|
|
|
|
node.radius = radius;
|
|
|
|
node.cutTemplate = cutTemplate;
|
2019-07-08 22:36:07 +00:00
|
|
|
node.cutRotation = cutRotation;
|
2019-02-18 12:57:18 +00:00
|
|
|
node.originNodeIndex = nodeIndex;
|
|
|
|
m_nodes.push_back(node);
|
|
|
|
|
|
|
|
return nodeIndex;
|
|
|
|
}
|
|
|
|
|
2019-12-14 13:28:14 +00:00
|
|
|
size_t StrokeModifier::addEdge(size_t firstNodeIndex, size_t secondNodeIndex)
|
2019-02-18 12:57:18 +00:00
|
|
|
{
|
|
|
|
size_t edgeIndex = m_edges.size();
|
|
|
|
|
|
|
|
Edge edge;
|
|
|
|
edge.firstNodeIndex = firstNodeIndex;
|
|
|
|
edge.secondNodeIndex = secondNodeIndex;
|
|
|
|
m_edges.push_back(edge);
|
|
|
|
|
|
|
|
return edgeIndex;
|
|
|
|
}
|
|
|
|
|
2019-12-14 13:28:14 +00:00
|
|
|
void StrokeModifier::createIntermediateNode(const Node &firstNode, const Node &secondNode, float factor, Node *resultNode)
|
2019-02-18 12:57:18 +00:00
|
|
|
{
|
|
|
|
float firstFactor = 1.0 - factor;
|
|
|
|
resultNode->position = firstNode.position * firstFactor + secondNode.position * factor;
|
|
|
|
resultNode->radius = firstNode.radius * firstFactor + secondNode.radius * factor;
|
2019-07-08 22:36:07 +00:00
|
|
|
if (factor <= 0.5) {
|
|
|
|
resultNode->originNodeIndex = firstNode.originNodeIndex;
|
|
|
|
resultNode->nearOriginNodeIndex = firstNode.originNodeIndex;
|
|
|
|
resultNode->farOriginNodeIndex = secondNode.originNodeIndex;
|
|
|
|
resultNode->cutRotation = firstNode.cutRotation;
|
|
|
|
resultNode->cutTemplate = firstNode.cutTemplate;
|
|
|
|
} else {
|
|
|
|
resultNode->originNodeIndex = secondNode.originNodeIndex;
|
|
|
|
resultNode->nearOriginNodeIndex = secondNode.originNodeIndex;
|
|
|
|
resultNode->farOriginNodeIndex = firstNode.originNodeIndex;
|
|
|
|
resultNode->cutRotation = secondNode.cutRotation;
|
|
|
|
resultNode->cutTemplate = secondNode.cutTemplate;
|
2019-02-18 12:57:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 13:28:14 +00:00
|
|
|
void StrokeModifier::subdivide()
|
2019-02-18 12:57:18 +00:00
|
|
|
{
|
|
|
|
for (auto &node: m_nodes) {
|
2019-02-27 12:59:03 +00:00
|
|
|
subdivideFace2D(&node.cutTemplate);
|
2019-02-18 12:57:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 13:28:14 +00:00
|
|
|
float StrokeModifier::averageCutTemplateEdgeLength(const std::vector<QVector2D> &cutTemplate)
|
2019-02-18 12:57:18 +00:00
|
|
|
{
|
|
|
|
if (cutTemplate.empty())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
float sum = 0;
|
|
|
|
for (size_t i = 0; i < cutTemplate.size(); ++i) {
|
|
|
|
size_t j = (i + 1) % cutTemplate.size();
|
|
|
|
sum += (cutTemplate[i] - cutTemplate[j]).length();
|
|
|
|
}
|
|
|
|
return sum / cutTemplate.size();
|
|
|
|
}
|
|
|
|
|
2019-12-14 13:28:14 +00:00
|
|
|
void StrokeModifier::roundEnd()
|
2019-02-18 12:57:18 +00:00
|
|
|
{
|
|
|
|
std::map<size_t, std::vector<size_t>> neighbors;
|
|
|
|
for (const auto &edge: m_edges) {
|
|
|
|
neighbors[edge.firstNodeIndex].push_back(edge.secondNodeIndex);
|
|
|
|
neighbors[edge.secondNodeIndex].push_back(edge.firstNodeIndex);
|
|
|
|
}
|
|
|
|
for (const auto &it: neighbors) {
|
|
|
|
if (1 == it.second.size()) {
|
|
|
|
const Node ¤tNode = m_nodes[it.first];
|
|
|
|
const Node &neighborNode = m_nodes[it.second[0]];
|
|
|
|
Node endNode;
|
|
|
|
endNode.radius = currentNode.radius * 0.5;
|
|
|
|
endNode.position = currentNode.position + (currentNode.position - neighborNode.position).normalized() * endNode.radius;
|
|
|
|
endNode.cutTemplate = currentNode.cutTemplate;
|
2019-07-08 22:36:07 +00:00
|
|
|
endNode.cutRotation = currentNode.cutRotation;
|
2019-02-18 12:57:18 +00:00
|
|
|
endNode.originNodeIndex = currentNode.originNodeIndex;
|
|
|
|
size_t endNodeIndex = m_nodes.size();
|
|
|
|
m_nodes.push_back(endNode);
|
|
|
|
addEdge(endNode.originNodeIndex, endNodeIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 13:28:14 +00:00
|
|
|
void StrokeModifier::createIntermediateCutTemplateEdges(std::vector<QVector2D> &cutTemplate, float averageCutTemplateLength)
|
2019-11-01 12:33:10 +00:00
|
|
|
{
|
|
|
|
std::vector<QVector2D> newCutTemplate;
|
|
|
|
auto pointCount = cutTemplate.size();
|
|
|
|
float targetLength = averageCutTemplateLength * 1.2;
|
|
|
|
for (size_t index = 0; index < pointCount; ++index) {
|
|
|
|
size_t nextIndex = (index + 1) % pointCount;
|
|
|
|
newCutTemplate.push_back(cutTemplate[index]);
|
|
|
|
float oldEdgeLength = (cutTemplate[index] - cutTemplate[nextIndex]).length();
|
|
|
|
if (targetLength >= oldEdgeLength)
|
|
|
|
continue;
|
|
|
|
size_t newInsertNum = oldEdgeLength / targetLength;
|
|
|
|
if (newInsertNum < 1)
|
|
|
|
newInsertNum = 1;
|
|
|
|
if (newInsertNum > 100)
|
|
|
|
continue;
|
|
|
|
float stepFactor = 1.0 / (newInsertNum + 1);
|
|
|
|
float factor = stepFactor;
|
|
|
|
for (size_t i = 0; i < newInsertNum && factor < 1.0; factor += stepFactor, ++i) {
|
|
|
|
float firstFactor = 1.0 - factor;
|
|
|
|
newCutTemplate.push_back(cutTemplate[index] * firstFactor + cutTemplate[nextIndex] * factor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cutTemplate = newCutTemplate;
|
|
|
|
}
|
|
|
|
|
2019-12-14 13:28:14 +00:00
|
|
|
void StrokeModifier::finalize()
|
2019-02-18 12:57:18 +00:00
|
|
|
{
|
2019-07-20 02:42:24 +00:00
|
|
|
if (!m_intermediateAdditionEnabled)
|
|
|
|
return;
|
2019-11-01 12:33:10 +00:00
|
|
|
|
|
|
|
for (auto &node: m_nodes) {
|
|
|
|
node.averageCutTemplateLength = averageCutTemplateEdgeLength(node.cutTemplate);
|
|
|
|
createIntermediateCutTemplateEdges(node.cutTemplate, node.averageCutTemplateLength);
|
|
|
|
}
|
|
|
|
|
2019-02-18 12:57:18 +00:00
|
|
|
auto oldEdges = m_edges;
|
|
|
|
m_edges.clear();
|
|
|
|
for (const auto &edge: oldEdges) {
|
|
|
|
const Node &firstNode = m_nodes[edge.firstNodeIndex];
|
|
|
|
const Node &secondNode = m_nodes[edge.secondNodeIndex];
|
2019-07-11 12:52:44 +00:00
|
|
|
//float edgeLengthThreshold = (firstNode.radius + secondNode.radius) * 0.75;
|
2019-11-01 12:33:10 +00:00
|
|
|
auto firstAverageCutTemplateEdgeLength = firstNode.averageCutTemplateLength * firstNode.radius;
|
|
|
|
auto secondAverageCutTemplateEdgeLength = secondNode.averageCutTemplateLength * secondNode.radius;
|
2019-07-11 11:20:34 +00:00
|
|
|
float targetEdgeLength = (firstAverageCutTemplateEdgeLength + secondAverageCutTemplateEdgeLength) * 0.5;
|
2019-07-11 12:52:44 +00:00
|
|
|
//if (targetEdgeLength < edgeLengthThreshold)
|
|
|
|
// targetEdgeLength = edgeLengthThreshold;
|
2019-02-18 12:57:18 +00:00
|
|
|
float currentEdgeLength = (firstNode.position - secondNode.position).length();
|
|
|
|
if (targetEdgeLength >= currentEdgeLength) {
|
|
|
|
addEdge(edge.firstNodeIndex, edge.secondNodeIndex);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
size_t newInsertNum = currentEdgeLength / targetEdgeLength;
|
|
|
|
if (newInsertNum < 1)
|
|
|
|
newInsertNum = 1;
|
2019-12-24 23:24:49 +00:00
|
|
|
if (newInsertNum > 100) {
|
|
|
|
addEdge(edge.firstNodeIndex, edge.secondNodeIndex);
|
2019-06-10 11:55:09 +00:00
|
|
|
continue;
|
2019-12-24 23:24:49 +00:00
|
|
|
}
|
2019-02-18 12:57:18 +00:00
|
|
|
float stepFactor = 1.0 / (newInsertNum + 1);
|
|
|
|
std::vector<size_t> nodeIndices;
|
|
|
|
nodeIndices.push_back(edge.firstNodeIndex);
|
|
|
|
float factor = stepFactor;
|
|
|
|
for (size_t i = 0; i < newInsertNum && factor < 1.0; factor += stepFactor, ++i) {
|
|
|
|
Node intermediateNode;
|
|
|
|
const Node &firstNode = m_nodes[edge.firstNodeIndex];
|
|
|
|
const Node &secondNode = m_nodes[edge.secondNodeIndex];
|
|
|
|
createIntermediateNode(firstNode, secondNode, factor, &intermediateNode);
|
|
|
|
size_t intermedidateNodeIndex = m_nodes.size();
|
|
|
|
nodeIndices.push_back(intermedidateNodeIndex);
|
|
|
|
m_nodes.push_back(intermediateNode);
|
|
|
|
}
|
|
|
|
nodeIndices.push_back(edge.secondNodeIndex);
|
|
|
|
for (size_t i = 1; i < nodeIndices.size(); ++i) {
|
|
|
|
addEdge(nodeIndices[i - 1], nodeIndices[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 13:28:14 +00:00
|
|
|
const std::vector<StrokeModifier::Node> &StrokeModifier::nodes()
|
2019-02-18 12:57:18 +00:00
|
|
|
{
|
|
|
|
return m_nodes;
|
|
|
|
}
|
|
|
|
|
2019-12-14 13:28:14 +00:00
|
|
|
const std::vector<StrokeModifier::Edge> &StrokeModifier::edges()
|
2019-02-18 12:57:18 +00:00
|
|
|
{
|
|
|
|
return m_edges;
|
|
|
|
}
|
|
|
|
|