1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
///////////////////////////////////////////////////////////////////////////////
//
//  Description:  Construct Dual Graph of from a Mesh. The dual of a triangle
//  ************  is its centroid.
//  Dual Graph can be represented as Node-Node Adjacency or through explcitly 
//  creating and storing the edges. When Adjacency is set to (1,0)  Edges are created 
//  and stored. When Adjacency is set to (0,0)  Node-Node relationships are stored.
//
//  Chaman Singh Verma
//  Department of Computer Sciences,
//  University of Wisconsin Madison.
//  Date 15th Jan 2010.
//
//  License:  Free to download and modify. 
//
///////////////////////////////////////////////////////////////////////////////

#ifndef DUALGRAPH_H
#define DUALGRAPH_H

#include "meshkit/Mesh.hpp"

namespace Jaal {

class DualGraph
{
public:
  static PNode getNewDualNode(Face *f);

  DualGraph()<--- Member variable 'DualGraph::mesh' is not initialized in the constructor.
  {
    adjTable[0][0] = 1;
  }

  ~DualGraph()
  {
    clear();
  }

  void setAdjTable(int src, int dst)
  {
    if (src == 0 && dst == 0)
    {
      if (adjTable[1][0])
        node_node_adjacency_rep();
      return;
    }

    if (src == 1 && dst == 0)
    {
      if (adjTable[0][0])
        edge_rep();
      return;
    }
  }

  size_t getSize(int d) const
  {
    if (d == 0)
      return nodes.size();
    if (d == 1)
      return edges.size();
    return 0;
  }

  int build(Mesh *m);

  const PNode &getNode(int i) const
  {
    return nodes[i];
  }

  const PEdge &getEdge(int i) const
  {
    assert(adjTable[1][0]);
    return edges[i];
  }

  void saveAs(const string &s)
  {
    writeNodes(s);
    writeEdges(s);
  }

  void clear()
  {
      nodes.clear();
      edges.clear();
  }

  void deleteAll()
  {
    size_t nSize;

    nSize = nodes.size();
    for (size_t i = 0; i < nSize; i++)
      delete nodes[i];
    nodes.clear();

    nSize = edges.size();
    for (size_t i = 0; i < nSize; i++)
      delete edges[i];
    edges.clear();
  }

private:
  char adjTable[4][4];

  Mesh *mesh;

  void node_node_adjacency_rep();
  void edge_rep();

  NodeSequence  nodes;
  EdgeSequence  edges;

  void addEdge(const PNode n1, const PNode n2);
  void getCentroid(int faceid, Point3D &p);
  void writeNodes(const string &s);
  void writeEdges(const string &s);
};

inline
void DualGraph::addEdge(const PNode v1, const PNode v2)
{
  v1->addRelation(v2);
  v2->addRelation(v1);
}

inline 
PNode DualGraph::getNewDualNode(Face *face) 
{
  PNode dualnode = Vertex::newObject();
  Point3D p3d;
  face->getAvgPos( p3d );
  dualnode->setXYZCoords(p3d);

  face->setAttribute("DualNode", dualnode);
  dualnode->setAttribute("PrimalFace", face);

  return dualnode;
}


} // namespace Jaal

#endif