联通三角形分组的C++算法实现

一、问题介绍

        有一个三角形网格,它有不同的三角形组,例如,一组15个相连的三角形,然后是另一组(没有连接到第一个)的25个三角形。连接的三角形组的数量是任意的,组本身可以是任何大小(从1到任何大小)。

二、算法实现思路

        需要为每个三角形顶点分配一个索引,指示它属于哪组连接的三角形。因此,在上面的示例中,将为组成15个三角形的组的顶点提供索引0,为组成25个三角形的组的顶点提供索引1(依此类推)。

三、代码实现如下

#include "stdafx.h"
#include <iostream>     // std::cout
#include <algorithm>    // std::set_difference, std::sort
#include <vector>       // std::vector
#include <set>       // std::vector
#include <cmath>
#include <map>

using namespace std;

// the global vertex indices
int numIndices;
int* indices;

class Triangle
{
public:
    explicit Triangle(int positionIndex_) : added(false), positionIndex(positionIndex_) {}

    int positionIndex; // positinon of the first index of this triangle in the global vert array (which is in  3's)

    // only valid with 0, 1, 2
    int getIndex(int i) { return indices[positionIndex + i];}

    bool isNeighbour(Triangle* other)
    {
        for (int i = 0; i < 3; ++i)
            for (int j = 0; j < 3; ++j)
                if (getIndex(i) == other->getIndex(j))
                    return true;
        return false;
    }

    bool isAdded() const{return added;}
    void setAdded(){ added = true;}

    int getNeighbourCount() const{ return neighbours.size(); }
    Triangle* getNeighbour(int i) const{ return neighbours[i];}
    void AddNeighbour(Triangle* neighbour)
    {
        neighbours.push_back(neighbour);//changed to set
    }

private:
    std::vector<Triangle*> neighbours;//changed to set
    bool added;
};

std::vector<Triangle*> triangles;

void createAllTriangles()
{
    for (int i = 0; i < numIndices; i += 3)
        triangles.push_back(new Triangle(i));

    //must delete all these pointers created with new
}

void setupAllNeighboursA()
{
    std::map<int,std::set<int>> vertex_to_tris;
    for (int i = 0; i < numIndices; i += 3)
    {
        vertex_to_tris[indices[i]].insert(i);
        vertex_to_tris[indices[i+1]].insert(i);
        vertex_to_tris[indices[i+2]].insert(i);
    }

    int n = triangles.size();
    for (int i = 0; i < n; ++i)
    {
        Triangle* t = triangles[i];
        std::set<int> temp_neighbours;
        for (int j = 0; j < 3; ++j)
        {
            int test_index = t->getIndex(j);
            for (std::set<int>::iterator it = vertex_to_tris[test_index].begin(); it != vertex_to_tris[test_index].end(); ++it)
            {
                if (*it != i) temp_neighbours.insert(*it/3);//divide by 3 to get the 'actual' tri index
            }
        }

        for (std::set<int>::iterator it = temp_neighbours.begin(); it != temp_neighbours.end(); ++it)
        {
            Triangle* other = triangles[*it];
            t->AddNeighbour(other);
        }
    }
}

class Island
{
public:
    void recursiveAdd(Triangle* t)
    {
        AddAndSetAdded(t);
        for(int i = 0; i < t->getNeighbourCount(); i++)
            if ( ! t->getNeighbour(i)->isAdded() )
                recursiveAdd(t->getNeighbour(i));
    }
    std::set<Triangle*> children;
private:
    void AddAndSetAdded(Triangle* t)
    {
        t->setAdded();
        children.insert(t);
    }
};

std::vector<Island*> island_list;

void createIslands()
{
    for (int i = 0; i < int(triangles.size()); ++i)
    {
        Triangle* t = triangles[i];
        if( ! t->isAdded() )
        {
            Island* island = new Island;
            island->recursiveAdd(t);
            island_list.push_back(island);
        }
    }
}

int main(int argc, _TCHAR* argv[])
{
    indices = vv;
    numIndices = 73728;
    createAllTriangles();
    setupAllNeighboursA();
    createIslands();

    for (int x = 0; x < int(island_list.size()); x++)
    {
        std::cout << "Island Index: " << x << endl;
        std::cout << island_list[x]->children.size() << endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大王算法

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值