MeshKit  1.0
DualGraph.cpp
Go to the documentation of this file.
00001 #include "meshkit/DualGraph.hpp"
00002 
00003 using namespace Jaal;
00004 
00006 
00007 int DualGraph::build(Mesh *m)
00008 {
00009     mesh = m;
00010     nodes.clear();
00011     edges.clear();
00012 
00013     size_t numfaces = mesh->getSize(2);
00014 
00015     int relexist = mesh->build_relations(0, 2);
00016 
00017     Vertex *dualvtx;
00018 
00019     nodes.resize(numfaces);
00020 
00021     Face *face;
00022     for (size_t iface = 0; iface < numfaces; iface++)
00023     {
00024         face = mesh->getFaceAt(iface);
00025         assert(face);
00026         face->setID(iface);
00027         dualvtx = DualGraph::getNewDualNode(face);
00028         dualvtx->setID(iface);
00029         nodes[iface] = dualvtx;
00030     }
00031 
00032     PNode v0, v1, dv0, dv1;
00033 
00034     FaceSequence neighs;
00035     for (size_t iface = 0; iface < numfaces; iface++)
00036     {
00037         face = mesh->getFaceAt(iface);
00038         int nc = face->getSize(0);
00039         for (int j = 0; j < nc; j++)
00040         {
00041             v0 = face->getNodeAt((j + 0) );
00042             v1 = face->getNodeAt((j + 1) );
00043             Mesh::getRelations112(v0, v1, neighs);
00044             if (neighs.size() == 2)
00045             {
00046                  dv0 = NULL;
00047                  neighs[0]->getAttribute("DualNode", dv0);
00048                  dv1 = NULL;
00049                  neighs[1]->getAttribute("DualNode", dv1);
00050                 addEdge(dv0, dv1);
00051             }
00052             else
00053             {
00054                 dv0 = NULL;
00055                 neighs[0]->getAttribute("DualNode", dv0);
00056                 dv0->setAttribute("Boundary", 1);
00057             }
00058         }
00059     }
00060 
00061     if (!relexist)
00062         mesh->clear_relations(0, 2);
00063 
00064     adjTable[0][0] = 1;
00065     mesh->setDual(1);
00066     return 0;
00067 }
00068 
00070 
00071 void DualGraph::node_node_adjacency_rep()
00072 {
00073     assert( adjTable[1][0]);
00074 
00075     int numedges = edges.size();
00076 
00077     for (int i = 0; i < numedges; i++)
00078     {
00079         Edge *edge = edges[i];
00080         Vertex *v0 = edge->getNodeAt(0);
00081         Vertex *v1 = edge->getNodeAt(1);
00082         addEdge(v0, v1);
00083         delete edge;
00084     }
00085     edges.clear();
00086 
00087     adjTable[1][0] = 0;
00088     adjTable[0][0] = 1;
00089 }
00091 
00092 void DualGraph::edge_rep()
00093 {
00094     NodeSequence vneighs, eConnect(2);
00095 
00096     assert( adjTable[0][0]);
00097 
00098     edges.clear();
00099 
00100     size_t numfaces = mesh->getSize(2);
00101     edges.reserve(2 * numfaces);
00102     for (size_t iface = 0; iface < numfaces; iface++)
00103     {
00104         Vertex *dualvtx = nodes[iface];
00105         dualvtx->getRelations( vneighs );
00106         for (size_t i = 0; i < vneighs.size(); i++)
00107         {
00108             Edge *newedge = new Edge(dualvtx, vneighs[i]);
00109             edges.push_back(newedge);
00110         }
00111         dualvtx->clearRelations(0);
00112     }
00113 
00114     adjTable[1][0] = 1;
00115     adjTable[0][0] = 0;
00116 }
00118 
00119 void DualGraph::writeNodes(const string &fname)
00120 {
00121     string filename = fname + ".node";
00122 
00123     ofstream ofile(filename.c_str(), ios::out);
00124     if (ofile.fail())
00125     {
00126         cout << "Warning: cann't open node file " << filename << endl;
00127         return;
00128     }
00129 
00130     int numnodes = nodes.size();
00131     ofile << numnodes << " 2 0 0" << endl;
00132 
00133     for (int i = 0; i < numnodes; i++)
00134     {
00135         Point3D p3d = nodes[i]->getXYZCoords();
00136         ofile << i << " " << p3d[0] << " " << p3d[1] << " " << p3d[1] << endl;
00137     }
00138 }
00139 
00141 
00142 void DualGraph::writeEdges(const string &fname)
00143 {
00144 
00145     string filename = fname + ".edge";
00146     ofstream ofile(filename.c_str(), ios::out);
00147     if (ofile.fail())
00148     {
00149         cout << "Warning: cann't open node file " << filename << endl;
00150         return;
00151     }
00152 
00153     edge_rep();
00154 
00155     int numedges = edges.size();
00156     ofile << numedges << " 0 " << endl;
00157 
00158     for (int i = 0; i < numedges; i++)
00159     {
00160         int id0 = edges[i]->getNodeAt(0)->getID();
00161         int id1 = edges[i]->getNodeAt(1)->getID();
00162         ofile << i << " " << id0 << " " << id1 << endl;
00163     }
00164 
00165 }
00166 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines