SpiecsEngine
 
Loading...
Searching...
No Matches

◆ MergeByDistance()

bool Spices::MeshProcessor::MergeByDistance ( MeshPack * meshPack,
std::vector< glm::uvec3 > & primVertices,
scl::kd_tree< 6 > & kdTree,
float maxDistance,
float maxUVDistance )
staticprivate

Merge Vertex by Distance.

Parameters
[in]meshPackMeshPack.
[in]kdTreekd_tree.
[in]maxDistanceallowed merge by vertex position.
[in]maxUVDistanceallowed merge by vertex uv.
Returns
Returns Merged Vertex Map.

Get boundary points.

Find merged vertices.

Find near vertex.

Only allow near merge.

Can merge if this vertex is in stableboundary.

Attention
This situation not allowed merge to line and point here.

Can merge when target is not in stableboundary if this vertex is in boundary.

Attention
boundarypoint can only be merged when connected.

Can merge when target is not in boundary if this vertex is not in boundary.

Copy primVertices and merge.

Remove lines and poins merged from triangles;

Get boundary points.

Find merged vertices.

Find near vertex.

Only allow near merge.

Can merge if this vertex is in stableboundary.

Attention
This situation not allowed merge to line and point here.

Can merge when target is not in stableboundary if this vertex is in boundary.

Attention
boundarypoint can only be merged when connected.

Can merge when target is not in boundary if this vertex is not in boundary.

Copy primVertices and merge.

Remove lines and poins merged from triangles;

Definition at line 504 of file MeshProcessor.cpp.

511 {
513
514 auto& positions = *meshPack->m_MeshResource.positions.attributes;
515 auto& texCoords = *meshPack->m_MeshResource.texCoords.attributes;
516 auto& vertices = *meshPack->m_MeshResource.vertices .attributes;
517
521 std::unordered_map<uint32_t, bool> boundaryPoints;
522 std::unordered_map<uint32_t, bool> stableBoundaryPoints;
523 std::unordered_map<uint32_t, EdgePoint> boundaryEdgePoints;
524 std::unordered_map<uint32_t, std::unordered_map<uint32_t, bool>> pointConnect;
526 meshPack ,
527 primVertices ,
528 boundaryPoints ,
529 stableBoundaryPoints ,
530 boundaryEdgePoints ,
531 pointConnect
532 );
533
534 std::unordered_map<uint32_t, uint32_t> primVerticesMap;
535 auto addToMap = [&](const uint32_t& k, const uint32_t& v) {
536 if (primVerticesMap.find(k) == primVerticesMap.end())
537 {
538 primVerticesMap[k] = v;
539 }
540 };
541
545 for (int i = 0; i < primVertices.size(); i++)
546 {
547 auto& primVertex = primVertices[i];
548
549 std::array<uint32_t, 3> primVertexArray = { primVertex.x, primVertex.y, primVertex.z };
550
551 for (int j = 0; j < 3; j++)
552 {
553 const glm::uvec4& vertex = vertices[primVertexArray[j]];
554
558 scl::kd_tree<6>::item item = {
559 positions[vertex.x].x,
560 positions[vertex.x].y,
561 positions[vertex.x].z,
562 texCoords[vertex.w].x,
563 texCoords[vertex.w].y,
564 (float)primVertexArray[j]
565 };
566 auto rangeVts = kdTree.range_search(item, {
567 maxDistance ,
568 maxDistance ,
569 maxDistance ,
570 maxUVDistance ,
571 maxUVDistance ,
572 (float)UINT32_MAX
573 });
574
578 std::vector<scl::kd_tree<6>::item> nearVts;
579 for (auto& vt : rangeVts)
580 {
581 uint32_t pt = vertices[(uint32_t)vt[5]].x;
582
583 if (pointConnect[vertex.x].find(pt) != pointConnect[vertex.x].end())
584 {
585 nearVts.push_back(vt);
586 }
587 }
588
593 if (stableBoundaryPoints.find(vertex.x) != stableBoundaryPoints.end())
594 {
595 std::unordered_map<uint32_t, bool> mergedVertex;
596
597 for (auto& rangeVt : nearVts)
598 {
599 uint32_t primVertexIndex = (uint32_t)rangeVt[5];
600 mergedVertex[vertices[primVertexIndex].x] = true;
601 }
602
603 bool canBeTriangle = true;
604 for (int k = 0; k < primVertexArray.size(); k++)
605 {
606 if (mergedVertex.find(vertices[primVertexArray[k]].x) != mergedVertex.end())
607 {
608 canBeTriangle = false;
609 break;
610 }
611 }
612
613 if(canBeTriangle)
614 {
615 for (auto& rangeVt : nearVts)
616 {
617 uint32_t primVertexIndex = (uint32_t)rangeVt[5];
618 addToMap(primVertexIndex, primVertexArray[j]);
619 }
620 }
621 else
622 {
623 addToMap(primVertexArray[j], primVertexArray[j]);
624 }
625 }
626
631 else if (boundaryPoints.find(vertex.x) != boundaryPoints.end())
632 {
633 for (auto& rangeVt : nearVts)
634 {
635 uint32_t primVertexIndex = (uint32_t)rangeVt[5];
636 uint32_t pt = vertices[primVertexIndex].x;
637
638 if (boundaryPoints.find(pt) == boundaryPoints.end())
639 {
640 addToMap(primVertexIndex, primVertexArray[j]);
641 }
642 else if (stableBoundaryPoints.find(pt) == stableBoundaryPoints.end())
643 {
644 if (boundaryEdgePoints[vertex.x].next == pt ||
645 boundaryEdgePoints[vertex.x].prev == pt ||
646 boundaryEdgePoints[vertex.x].self == pt
647 )
648 {
649 addToMap(primVertexIndex, primVertexArray[j]);
650 }
651 }
652 }
653 }
654
658 else
659 {
660 for (auto& rangeVt : nearVts)
661 {
662 uint32_t primVertexIndex = (uint32_t)rangeVt[5];
663 uint32_t pt = vertices[primVertexIndex].x;
664
665 if (boundaryPoints.find(pt) == boundaryPoints.end())
666 {
667 addToMap(primVertexIndex, primVertexArray[j]);
668 }
669 }
670 }
671 }
672 }
673
677 std::vector<glm::uvec3> tempPrimVertices = primVertices;
678 for (int i = 0; i < primVertices.size(); i++)
679 {
680 auto primVertex = primVertices[i];
681
682 if(primVerticesMap.find(primVertex.x) != primVerticesMap.end()) tempPrimVertices[i].x = primVerticesMap[primVertex.x];
683 if(primVerticesMap.find(primVertex.y) != primVerticesMap.end()) tempPrimVertices[i].y = primVerticesMap[primVertex.y];
684 if(primVerticesMap.find(primVertex.z) != primVerticesMap.end()) tempPrimVertices[i].z = primVerticesMap[primVertex.z];
685 }
686
690 primVertices.clear();
691 for (auto& primVertex : tempPrimVertices)
692 {
693 std::set<uint32_t> set;
694
695 set.insert(primVertex.x);
696 set.insert(primVertex.y);
697 set.insert(primVertex.z);
698
699 if (set.size() < 3) continue;
700
701 primVertices.push_back(primVertex);
702 }
703
704 return true;
705 }
#define SPICES_PROFILE_ZONE
static bool FindBoundaryPoints(MeshPack *meshPack, const std::vector< glm::uvec3 > &primVertices, std::unordered_map< uint32_t, bool > &boundaryPoints, std::unordered_map< uint32_t, bool > &stableBoundaryPoints, std::unordered_map< uint32_t, EdgePoint > &boundaryEdgePoints, std::unordered_map< uint32_t, std::unordered_map< uint32_t, bool > > &pointConnect)
Find Points located in Boundaries.
std::array< float, K > item
using item instead of point in k d.
Definition KDTree.h:33
auto range_search(const item &point, const item &condition) const -> std::vector< item >
Search for all points within given range. Start at the root. If the current node is within the range,...
Definition KDTree.h:660