MorphoGraphX
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Insert.hpp
Go to the documentation of this file.
1 #ifndef INSERT_HPP
2 #define INSERT_HPP
3 
8 #include <Config.hpp>
9 
10 #include <Information.hpp>
11 #include <StaticAssert.hpp>
12 #include <VVGraph.hpp>
13 
14 #include <utility>
15 
16 namespace mgx {
17 namespace util {
18 
48 template <class vvgraph, bool do_checks = true>
49 class Insert
50 #ifdef JUST_FOR_DOXYGEN
51 {
52 };
53 #else
54 ;
55 #endif
56 
57 template <class vvgraph> class Insert<vvgraph, true> {
58 public:
59  typedef typename vvgraph::vertex_t vertex;
60 
61  vertex operator()(const vertex& a, const vertex& b, vvgraph& S) const
62  {
63  if(a.isNull() || b.isNull()) {
64  return vertex(0);
65  }
66 
67  unsigned int check = 0;
68  if(S.edge(a, b))
69  check++;
70  if(S.edge(b, a))
71  check++;
72 
73  switch(check) {
74  case 0:
75  Information::err << "Warning: Attempt to insert a vertex between vertices that have no relation." << endl;
76  return vertex(0);
77  break;
78  case 1:
79  Information::err << "Warning: Attempt to insert a vertex between vertices that have an assymetric relation."
80  << endl;
81  return vertex(0);
82  break;
83  default:
84  break;
85  }
86 
87  vertex x;
88  S.insert(x);
89 
90  S.replace(a, b, x);
91  S.replace(b, a, x);
92 
93  S.insertEdge(x, a);
94  S.insertEdge(x, b);
95 
96  return x;
97  }
98 };
99 
100 template <class vvgraph> class Insert<vvgraph, false> {
101 public:
102  typedef typename vvgraph::vertex_t vertex;
103 
104  const vertex& operator()(const vertex& a, const vertex& b, vvgraph& S) const
105  {
106  vertex x;
107  const vertex& r = *S.insert(x);
108 
109  S.replace(a, b, x);
110  S.replace(b, a, x);
111 
112  S.insertEdge(x, a);
113  S.insertEdge(x, b);
114 
115  return r;
116  }
117 };
118 
119 template <typename VertexContent, typename EdgeContent, typename Alloc>
120 const typename graph::VVGraph<VertexContent, EdgeContent, Alloc>::vertex_t&
121 insert(const typename graph::VVGraph<VertexContent, EdgeContent, Alloc>::vertex_t& a,
122  const typename graph::VVGraph<VertexContent, EdgeContent, Alloc>::vertex_t& b,
123  graph::VVGraph<VertexContent, EdgeContent, Alloc>&S)
124 {
125  typedef Insert<graph::VVGraph<VertexContent, EdgeContent, Alloc> > Insert;
126  static const Insert fct = Insert();
127  return fct(a, b, S);
128 }
129 
134 template <class Graph>
135 typename Graph::edge_t insertAfter(const typename Graph::vertex_t& v, const typename Graph::vertex_t& ref,
136  const typename Graph::vertex_t& nv, Graph& S)
137 {
138  if(ref)
139  return S.spliceAfter(v, ref, nv);
140  return S.insertEdge(v, nv);
141 }
142 
147 template <class Graph>
148 typename Graph::edge_t insertBefore(const typename Graph::vertex_t& v, const typename Graph::vertex_t& ref,
149  const typename Graph::vertex_t& nv, Graph& S)
150 {
151  if(ref)
152  return S.spliceBefore(v, ref, nv);
153  return S.insertEdge(v, nv);
154 }
155 } // namespace util
156 } // namespace mgx
157 #endif // INSERT_HPP
edge_t insertEdge(const vertex_t &src, const vertex_t &tgt)
Insert a new edge in the graph, without ordering.
Definition: VVGraph.hpp:2035
edge_t replace(const vertex_t &v, const vertex_t &neighbor, const vertex_t &new_neighbor)
Replace a vertex by another in a neighborhood.
Definition: VVGraph.hpp:1894
vvgraph::vertex_t vertex
Type of a vertex.
Definition: Mesh.hpp:39
Insert a new vertex on an edge.
Definition: Insert.hpp:49
Vertex of a vv graph.
Definition: Vertex.hpp:57
Define the STATIC_ASSERT macro.
Contain the definition of the VVGraph template class.
bool isNull() const
Test if a vertex is a null vertex.
Definition: Vertex.hpp:274
iterator insert(const vertex_t &v)
Insert a new vertex in the graph.
Definition: VVGraph.hpp:1585
edge_t edge(const vertex_t &src, const vertex_t &tgt)
Returns the edge from src to tgt.
Definition: VVGraph.hpp:1954