Skip to content

Commit

Permalink
Turn single node to tube
Browse files Browse the repository at this point in the history
  • Loading branch information
huxingyi committed Dec 20, 2022
1 parent 9e5620b commit 4e15cb9
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 231 deletions.
2 changes: 0 additions & 2 deletions application/application.pro
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,6 @@ HEADERS += ../dust3d/base/uuid.h
SOURCES += ../dust3d/base/uuid.cc
HEADERS += ../dust3d/mesh/base_normal.h
SOURCES += ../dust3d/mesh/base_normal.cc
HEADERS += ../dust3d/mesh/box_mesh.h
SOURCES += ../dust3d/mesh/box_mesh.cc
HEADERS += ../dust3d/mesh/centripetal_catmull_rom_spline.h
SOURCES += ../dust3d/mesh/centripetal_catmull_rom_spline.cc
HEADERS += ../dust3d/mesh/solid_mesh.h
Expand Down
148 changes: 0 additions & 148 deletions dust3d/mesh/box_mesh.cc

This file was deleted.

35 changes: 0 additions & 35 deletions dust3d/mesh/box_mesh.h

This file was deleted.

86 changes: 40 additions & 46 deletions dust3d/mesh/mesh_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <dust3d/base/part_target.h>
#include <dust3d/base/snapshot_xml.h>
#include <dust3d/base/string.h>
#include <dust3d/mesh/box_mesh.h>
#include <dust3d/mesh/mesh_generator.h>
#include <dust3d/mesh/mesh_recombiner.h>
#include <dust3d/mesh/rope_mesh.h>
Expand Down Expand Up @@ -650,53 +649,48 @@ std::unique_ptr<MeshState> MeshGenerator::combinePartMesh(const std::string& par
}

if (PartTarget::Model == target) {
if (1 == meshNodes.size()) {
size_t subdivideTimes = subdived ? 1 : 0;
buildBoxMesh(meshNodes[0].origin, meshNodes[0].radius, subdivideTimes, partCache.vertices, partCache.faces);
} else {
std::unique_ptr<TubeMeshBuilder> tubeMeshBuilder;
TubeMeshBuilder::BuildParameters buildParameters;
buildParameters.deformThickness = deformThickness;
buildParameters.deformWidth = deformWidth;
buildParameters.deformUnified = deformUnified;
buildParameters.baseNormalRotation = cutRotation * Math::Pi;
buildParameters.cutFace = cutTemplate;
buildParameters.frontEndRounded = buildParameters.backEndRounded = rounded;
tubeMeshBuilder = std::make_unique<TubeMeshBuilder>(buildParameters, std::move(meshNodes), isCircle);
tubeMeshBuilder->build();
partCache.vertices = tubeMeshBuilder->generatedVertices();
partCache.faces = tubeMeshBuilder->generatedFaces();
if (!__mirrorFromPartId.empty()) {
for (auto& it : partCache.vertices)
it.setX(-it.x());
for (auto& it : partCache.faces)
std::reverse(it.begin(), it.end());
}
const auto& faceUvs = tubeMeshBuilder->generatedFaceUvs();
for (size_t i = 0; i < faceUvs.size(); ++i) {
const auto& uv = faceUvs[i];
const auto& face = partCache.faces[i];
if (3 == face.size()) {
partCache.triangleUvs.insert({ { PositionKey(partCache.vertices[face[0]]),
PositionKey(partCache.vertices[face[1]]),
PositionKey(partCache.vertices[face[2]]) },
{ uv[0], uv[1], uv[2] } });
} else if (4 == face.size()) {
partCache.triangleUvs.insert({ { PositionKey(partCache.vertices[face[0]]),
PositionKey(partCache.vertices[face[1]]),
PositionKey(partCache.vertices[face[2]]) },
{ uv[0], uv[1], uv[2] } });
partCache.triangleUvs.insert({ { PositionKey(partCache.vertices[face[2]]),
PositionKey(partCache.vertices[face[3]]),
PositionKey(partCache.vertices[face[0]]) },
{ uv[2], uv[3], uv[0] } });
}
}
const auto& vertexSources = tubeMeshBuilder->generatedVertexSources();
for (size_t i = 0; i < vertexSources.size(); ++i) {
partCache.positionToNodeIdMap.emplace(std::make_pair(PositionKey(partCache.vertices[i]), vertexSources[i]));
std::unique_ptr<TubeMeshBuilder> tubeMeshBuilder;
TubeMeshBuilder::BuildParameters buildParameters;
buildParameters.deformThickness = deformThickness;
buildParameters.deformWidth = deformWidth;
buildParameters.deformUnified = deformUnified;
buildParameters.baseNormalRotation = cutRotation * Math::Pi;
buildParameters.cutFace = cutTemplate;
buildParameters.frontEndRounded = buildParameters.backEndRounded = rounded;
tubeMeshBuilder = std::make_unique<TubeMeshBuilder>(buildParameters, std::move(meshNodes), isCircle);
tubeMeshBuilder->build();
partCache.vertices = tubeMeshBuilder->generatedVertices();
partCache.faces = tubeMeshBuilder->generatedFaces();
if (!__mirrorFromPartId.empty()) {
for (auto& it : partCache.vertices)
it.setX(-it.x());
for (auto& it : partCache.faces)
std::reverse(it.begin(), it.end());
}
const auto& faceUvs = tubeMeshBuilder->generatedFaceUvs();
for (size_t i = 0; i < faceUvs.size(); ++i) {
const auto& uv = faceUvs[i];
const auto& face = partCache.faces[i];
if (3 == face.size()) {
partCache.triangleUvs.insert({ { PositionKey(partCache.vertices[face[0]]),
PositionKey(partCache.vertices[face[1]]),
PositionKey(partCache.vertices[face[2]]) },
{ uv[0], uv[1], uv[2] } });
} else if (4 == face.size()) {
partCache.triangleUvs.insert({ { PositionKey(partCache.vertices[face[0]]),
PositionKey(partCache.vertices[face[1]]),
PositionKey(partCache.vertices[face[2]]) },
{ uv[0], uv[1], uv[2] } });
partCache.triangleUvs.insert({ { PositionKey(partCache.vertices[face[2]]),
PositionKey(partCache.vertices[face[3]]),
PositionKey(partCache.vertices[face[0]]) },
{ uv[2], uv[3], uv[0] } });
}
}
const auto& vertexSources = tubeMeshBuilder->generatedVertexSources();
for (size_t i = 0; i < vertexSources.size(); ++i) {
partCache.positionToNodeIdMap.emplace(std::make_pair(PositionKey(partCache.vertices[i]), vertexSources[i]));
}
}

bool hasMeshError = false;
Expand Down
43 changes: 43 additions & 0 deletions dust3d/mesh/tube_mesh_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,48 @@ void TubeMeshBuilder::applyRoundEnd()
}
}

void TubeMeshBuilder::turnSingleNodeToTube()
{
if (1 != m_nodes.size())
return;

size_t subdivTimes = ((m_buildParameters.cutFace.size() + 3) / 4) - 1;

if (0 == subdivTimes) {
std::vector<MeshNode> interpolatedNodes;
interpolatedNodes.emplace_back(MeshNode {
m_nodes.front().origin + Vector3(0.0, m_nodes.front().radius, 0.0),
m_nodes.front().radius,
m_nodes.front().sourceId });
interpolatedNodes.emplace_back(MeshNode {
m_nodes.front().origin - Vector3(0.0, m_nodes.front().radius, 0.0),
m_nodes.front().radius,
m_nodes.front().sourceId });
m_nodes = std::move(interpolatedNodes);
return;
}

std::vector<MeshNode> interpolatedNodes;
double stepY = 0.45;
for (auto y = stepY; y < 1.0; y += stepY) {
auto moveY = m_nodes.front().radius * y;
interpolatedNodes.emplace_back(MeshNode {
m_nodes.front().origin + Vector3(0.0, 0.0, moveY),
std::sqrt(std::pow(m_nodes.front().radius, 2.0) - std::pow(moveY, 2.0)),
m_nodes.front().sourceId });
}
std::reverse(interpolatedNodes.begin(), interpolatedNodes.end());
interpolatedNodes.push_back(m_nodes.front());
for (auto y = stepY; y < 1.0; y += stepY) {
auto moveY = m_nodes.front().radius * y;
interpolatedNodes.emplace_back(MeshNode {
m_nodes.front().origin - Vector3(0.0, 0.0, moveY),
std::sqrt(std::pow(m_nodes.front().radius, 2.0) - std::pow(moveY, 2.0)),
m_nodes.front().sourceId });
}
m_nodes = std::move(interpolatedNodes);
}

void TubeMeshBuilder::applyInterpolation()
{
if (!m_buildParameters.interpolationEnabled)
Expand Down Expand Up @@ -116,6 +158,7 @@ void TubeMeshBuilder::applyInterpolation()

void TubeMeshBuilder::preprocessNodes()
{
turnSingleNodeToTube();
applyInterpolation();
applyRoundEnd();
}
Expand Down
1 change: 1 addition & 0 deletions dust3d/mesh/tube_mesh_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class TubeMeshBuilder {
std::vector<Vector3> buildCutFaceVertices(const Vector3& origin,
double radius,
const Vector3& forwardDirection);
void turnSingleNodeToTube();
void applyRoundEnd();
void applyInterpolation();
void addCap(const std::vector<size_t>& section, double ringV, double centerV, bool reverseU);
Expand Down

0 comments on commit 4e15cb9

Please sign in to comment.