2016-09-05 6 views
0

以下のMWEを参照すると、PointCellを含むMeshがあります。今度はMETISでメッシュを2つに分割し、2つの新しいメッシュを作成したいと思います。メッシュ内の循環ポインタの方向を考慮すると、新しいメッシュのポイントとセルのポインタを新しいアドレスに簡単に更新できますか?コピー後のポインタのアドレスを更新する

#include <iostream> 
#include <vector> 

struct Point; 
struct Cell; 

struct Mesh 
{ 
    std::vector<Point> point; 
    std::vector<Cell> cell; 
}; 

struct Point 
{ 
    Cell* parent_cell; 
    int partition; 
}; 

struct Cell 
{ 
    std::vector<Point*> vertex; 
    int partition; 
}; 

int main() 
{ 
    Mesh mesh; 

    mesh.cell.resize(2); // create two triangles. 
    mesh.point.resize(4); // 4 points instead of 6 because two of them will be shared. 

    // let vertices know their parent cell.  
    mesh.point[0].parent_cell = &mesh.cell[0]; 
    mesh.point[1].parent_cell = &mesh.cell[0]; mesh.point[1].parent_cell = &mesh.cell[1]; 
    mesh.point[2].parent_cell = &mesh.cell[0]; mesh.point[2].parent_cell = &mesh.cell[1]; 
    mesh.point[3].parent_cell = &mesh.cell[1]; 

    // let cells know their vertices. 
    mesh.cell[0].vertex.push_back(&mesh.point[0]); 
    mesh.cell[0].vertex.push_back(&mesh.point[1]); 
    mesh.cell[0].vertex.push_back(&mesh.point[2]); 
    mesh.cell[1].vertex.push_back(&mesh.point[1]); 
    mesh.cell[1].vertex.push_back(&mesh.point[2]); 
    mesh.cell[1].vertex.push_back(&mesh.point[3]);  

    // partition mesh into two. 
    // give partition number to points. 
    // all but one of the vertices belong to partition 0. 
    mesh.point[0].partition = 0; 
    mesh.point[1].partition = 0; 
    mesh.point[2].partition = 0; 
    mesh.point[3].partition = 1; // only this vertex belongs to partition 1. 
    // give partition number to cells. 
    mesh.cell[0].partition = 0; 
    mesh.cell[1].partition = 1; 

    // create two new meshes. 
    // filter points and cells according to partition number. 
    // but how to update pointers?  

    return 0; 
} 

答えて

0

私はポインタの代わりに他のベクトルにインデックスを格納することをお勧めします。コピー後またはベクタがバッキングメモリを再割り当てした後も有効です。

私のプロジェクトの1つでも同様のことをしましたが、余計なインダイレクションのためにパフォーマンスについて少し心配していました。それはポインタよりも速いことが判明しました!おそらく、8Bポインタを4Bの整数に置き換えてメモリ負荷に保存したからです。


あなたがstd::vectorの要素へのポインタを使う、という場合は、次のようにコピーした後にそれらを再マッピングすることができます

// Mesh copy constructor 
Mesh::Mesh(const Mesh &other) { 
    point = other.point; 
    cell = other.cell; 

    for(Point &p : point) { 
     p.parent_cell = p.parent_cell - other.cell.data() + cell.data(); 
    } 

    for(Cell &c : cell) { 
     for(Point* &p : c.vertex) { 
      p = p - other.point.data() + point.data(); 
     } 
    } 
} 

が、これは脆弱なコードであることを警告すること。構造内の何かが変更され、それに応じてこのメソッドが更新されないと、醜いバグが発生します。

関連する問題