ALBA
albaPolylineGraph.h
Go to the documentation of this file.
1/*=========================================================================
2Program: Multimod Application Framework
3Module: $RCSfile: albaPolylineGraph.h,v $
4Language: C++
5Date: $Date: 2012-04-06 07:54:31 $
6Version: $Revision: 1.1.2.1 $
7Authors: Nigel McFarlane
8==========================================================================
9Copyright (c) 2001/2005
10CINECA - Interuniversity Consortium (www.cineca.it)
11=========================================================================
12Modified by Josef Kohout to strip off ALBA stuff from it making it possible
13to use in VTK filters (=> wxString was replaced by char*)*/
14#ifndef __albaPolylineGraph_h
15#define __albaPolylineGraph_h
16
17//----------------------------------------------------------------------------
18// Include:
19//----------------------------------------------------------------------------
20#include "albaDefines.h"
21#include "vtkPolydata.h"
22#include <ostream>
23#include <vector>
24
25
26//----------------------------------------------------------------------------
27// forward declarations :
28//----------------------------------------------------------------------------
29
30
31
32//----------------------------------------------------------------------------
68//----------------------------------------------------------------------------
69class ALBA_EXPORT albaPolylineGraph
70{
71public:
73 const static vtkIdType m_UndefinedId;
74 const static int m_UndefinedInt;
75 const static char* m_UndefinedName;
76
77public:
78 //-----------------------------------------------------------------------------
79 // public methods
80 //-----------------------------------------------------------------------------
83 int GetNumberOfVertices() const ;
84 int GetNumberOfEdges() const ;
85 int GetNumberOfBranches() const ;
86 vtkIdType GetMaxVertexId() const ;
87 vtkIdType GetMaxEdgeId() const ;
88 vtkIdType GetMaxBranchId() const ;
89
90 void GetVertexCoords(vtkIdType v, double *coords) const ;
91 void SetVertexCoords(vtkIdType v, const double *coords) ;
92
93 const char* GetBranchName(vtkIdType b) const ;
94 void SetBranchName(vtkIdType b, const char* name) ;
95 void UnsetBranchName(vtkIdType b) ;
96 vtkIdType FindBranchName(const char* name) const ;
97
98 double GetBranchLength(vtkIdType b) const;
99 double GetBranchIntervalLength(vtkIdType b, vtkIdType startVertexId, vtkIdType endVertexId) const;
100
101 bool IsEdgeDirected(vtkIdType e) const ;
102 void SetEdgeDirected(vtkIdType e, bool directed) ;
103 void ReverseEdge(vtkIdType e) ;
104
105 double GetEdgeWeight(vtkIdType e) const ;
106 void SetEdgeWeight(vtkIdType e, double wt) ;
107
108 bool AddNewEdge(vtkIdType v0, vtkIdType v1) ;
109
113 void AddNewVertex(double *coords = NULL) ;
114 bool AddNewVertex(vtkIdType v0, double *coords = NULL) ;
115
117 void AddNewBranch(const char* name = NULL) ;
118 bool AddNewBranch(vtkIdType v0, const char* name = NULL) ;
119
125 bool CopyFromPolydata(vtkPolyData *polydata) ;
126
133 bool CopyToPolydata(vtkPolyData *polydata) ;
134
135 /* Get location of edge in output polydata.
136 Returns the cellid and the index on the cell.
137 The mapping is set by CopyToPolydata(). */
138 void GetOutputCellCorrespondingToEdge(vtkIdType edgeid, vtkIdType *cellid, vtkIdType *index) const ;
139
140 /* Get edge id in graph which corresponds to (cellid,index) in the output data
141 The mapping is set by CopyToPolydata(). */
142 vtkIdType GetEdgeCorrespondingToOutputCell(vtkIdType cellid, vtkIdType index) const ;
143
144 /* Get location of branch in output polydata.
145 The mapping is set by CopyToPolydata(). */
146 void GetOutputCellCorrespondingToBranch(vtkIdType branchid, vtkIdType *cellid) const ;
147
148 /* Get branch id in graph corresponding to cell in output polydata.
149 The mapping is set by CopyToPolydata().
150 NB this returns -1 if the cell does not map to a branch. */
151 vtkIdType GetBranchCorrespondingToOutputCell(vtkIdType cellid) const ;
152
156 bool SplitBranchAtEdge(vtkIdType b, vtkIdType e) ;
157
161 bool SplitBranchAtVertex(vtkIdType b, vtkIdType v) ;
162
166 void AddNewVertexToBranch(vtkIdType b, double *coords = NULL) ;
167
171 bool AddExistingVertexToBranch(vtkIdType b, vtkIdType v, vtkIdType e = m_UndefinedId) ;
172
176 bool AddExistingEdgeToBranch(vtkIdType b, vtkIdType e) ;
177
180 void ReverseBranch(vtkIdType b) ;
181
185 bool DeleteEdge(vtkIdType i) ;
186
191 bool DeleteVertex(vtkIdType i) ;
192
195 bool DeleteBranch(vtkIdType i) ;
196
197#pragma region BES April 2008
201 bool MergeBranches(vtkIdType b1, vtkIdType b2);
202
207
211#pragma endregion
212
213 bool IsConnected() const ;
214 bool IsConnectedTree() const ;
215 void Clear() ;
216 bool SelfCheck() const ;
217 void PrintSelf(std::ostream& os, const int tabs) const ;
218
219protected:
221 //void LogMessageF(const char* format, ...);
222
224 //virtual void LogMessage(const char* szMessage) {
225 // //default implementation is to do nothing
226 //}
227public:
228 //----------------------------------------------------------------------------
233 //----------------------------------------------------------------------------
234 class ALBA_EXPORT Vertex{
235 public:
237 Vertex(double *coords) ;
239 int GetDegree() const ;
240 vtkIdType GetEdgeId(int i) const ;
241 void SetEdgeId(int i, vtkIdType e) ;
242 void AddEdgeId(vtkIdType e) ;
243 int FindEdgeId(vtkIdType e) const ;
244 bool DeleteEdgeId(vtkIdType e) ;
245 vtkIdType GetVertexId(int i) const ;
246 void SetVertexId(int i, vtkIdType v) ;
247 void AddVertexId(vtkIdType v) ;
248 int FindVertexId(vtkIdType v) const ;
249 bool DeleteVertexId(vtkIdType v) ;
250 void GetVerticesIdList(vtkIdList *idlist) const ;
251 void GetCoords(double *coords) const ;
252 void SetCoords(const double *coords) ;
253 void Clear() ;
254 bool SelfCheck() const ;
255 void PrintSelf(std::ostream& os, const int tabs) const ;
256 private:
257 double m_Coords[3] ;
258 std::vector<vtkIdType> m_EdgeId ;
259 std::vector<vtkIdType> m_VertexId ;
260 } ;
261
262
263 //----------------------------------------------------------------------------
268 //----------------------------------------------------------------------------
269 class ALBA_EXPORT Edge{
270 public:
271 Edge() ;
272 Edge(vtkIdType v0, vtkIdType v1) ;
273 Edge(const Edge& e) ;
274 ~Edge() ;
275 Edge& operator=(const Edge& e) ;
276 vtkIdType GetVertexId(int i) const ;
277 void SetVertexId(int i, vtkIdType v) ;
278 void SetVertexIds(vtkIdType v0, vtkIdType v1) ;
279 void GetVerticesIdList(vtkIdList *idlist) const ;
280 vtkIdType GetOtherEndVertexId(vtkIdType v) const ;
281 vtkIdType GetBranchId() const ;
282 void SetBranchId(vtkIdType b) ;
283 bool IsVertexPair(vtkIdType v0, vtkIdType v1) const ;
284 bool IsVertexPairOrdered(vtkIdType v0, vtkIdType v1) const ;
285 void SetDirected(bool directed) ;
286 bool IsDirected() const ;
288 double GetWeight() const ;
289 void SetWeight(double w) ;
290 void GetMappingToOutputPolydata(vtkIdType *cellid, vtkIdType *index) const ;
291 void SetMappingToOutputPolydata(vtkIdType cellid, vtkIdType index) ;
292 bool SelfCheck() const ;
293 void PrintSelf(std::ostream& os, const int tabs) const ;
294 private:
295 vtkIdType m_VertexId[2] ;
296 bool m_Directed ;
297 double m_Weight ;
298 vtkIdType m_BranchId ;
299
300 vtkIdType m_OutputPolydataCell ;
301 vtkIdType m_OutputPolydataCellIndex ;
302
303 } ;
304
305
306 //----------------------------------------------------------------------------
311 //----------------------------------------------------------------------------
312 class ALBA_EXPORT Branch{
313 public:
314 Branch(const char* name = NULL) ;
315 explicit Branch(vtkIdType startvertex, const char* name = NULL) ;
316 Branch(const Branch& src);
317
319 int GetNumberOfVertices() const ;
320 int GetNumberOfEdges() const ;
321 void GetName(char* name) const ;
322 const char* GetName() const ;
323 void SetName(const char *name) ;
324 void UnsetName() ;
325 vtkIdType GetEdgeId(int i) const ;
326 void SetEdgeId(int i, vtkIdType e) ;
327 void AddEdgeId(vtkIdType e) ;
328 int FindEdgeId(vtkIdType e) const ;
329 void InsertEdgeId(int i, vtkIdType e) ;
330 vtkIdType GetLastEdgeId() const ;
331 vtkIdType GetVertexId(int i) const ;
332 void SetVertexId(int i, vtkIdType v) ;
333 void AddVertexId(vtkIdType v) ;
334 int FindVertexId(vtkIdType v) const ;
335 void InsertVertexId(int i, vtkIdType v) ;
336 vtkIdType GetLastVertexId() const ;
338 void GetVerticesIdList(vtkIdList *idlist) const ;
340 vtkIdType GetMappingToOutputPolydata() const ;
341 void SetMappingToOutputPolydata(vtkIdType cellid) ;
342 void Clear() ;
343 bool SelfCheck() const ;
344 void PrintSelf(std::ostream& os, const int tabs) const ;
345
346 Branch& operator=(const Branch& src);
347 private:
348 //char* m_Name ; ///< name of branch
349 wxString m_Name;
350 std::vector<vtkIdType> m_VertexId ;
351 std::vector<vtkIdType> m_EdgeId ;
352
353 vtkIdType m_OutputPolydataCell ;
354
355 } ;
356
357 //-----------------------------------------------------------------------------
358 // Public functions which return const pointers to vertices, edges and branches.
359 // These allow read-only access to const methods of the nested classes
360 //-----------------------------------------------------------------------------
361 const Vertex* GetConstVertexPtr(vtkIdType i) const ;
362 const Edge* GetConstEdgePtr(vtkIdType i) const ;
363 const Branch* GetConstBranchPtr(vtkIdType i) const ;
364
365private:
366 //-----------------------------------------------------------------------------
367 // private methods
368 //-----------------------------------------------------------------------------
369 void AllocateVertices(int nv) ;
370 void AllocateEdges(int ne) ;
371 void AllocateBranches(int ne) ;
372
373 bool SwapVertexIndices(vtkIdType i, vtkIdType j) ;
374 bool SwapEdgeIndices(vtkIdType i, vtkIdType j) ;
375 bool SwapBranchIndices(vtkIdType i, vtkIdType j) ;
376
379 bool DeleteLastEdge() ;
380
384 bool DeleteLastVertex() ;
385
387 bool DeleteLastBranch() ;
388
389
390 //-----------------------------------------------------------------------------
391 // Private functions which return non-const pointers to vertices, edges and branches
392 // These give full read and write access to the nested classes,
393 // and can only be used by methods of albaPolylineGraph.
394 //-----------------------------------------------------------------------------
395 Vertex* GetVertexPtr(vtkIdType i) ;
396 Edge* GetEdgePtr(vtkIdType i) ;
397 Branch* GetBranchPtr(vtkIdType i) ;
398
399 //-----------------------------------------------------------------------------
400 // member variables
401 //-----------------------------------------------------------------------------
402 std::vector<Vertex> m_Vertex ;
403 std::vector<Edge> m_Edge ;
404 std::vector<Branch> m_Branch ;
405} ;
406
407#endif
408
Nested Branch class A branch is a connected sequence of vertex id's.
void Clear()
clear data from branch
void PrintSelf(std::ostream &os, const int tabs) const
print self
int FindEdgeId(vtkIdType e) const
find position (0..ne-1) of this edge id in the list, return -1 if invalid
void SetMappingToOutputPolydata(vtkIdType cellid)
set mapping from branch to output cell
Branch(const Branch &src)
int GetNumberOfEdges() const
get number of edges (should be vertices - 1)
void ReverseDirection()
reverse branch direction
void AddVertexId(vtkIdType v)
add new vertex to end of list
void InsertVertexId(int i, vtkIdType v)
insert vertex at position i in list
int FindVertexId(vtkIdType v) const
find position (0..nv-1) of this vertex id in the list, return -1 if invalid
void AddEdgeId(vtkIdType e)
add edge to end of list
void SetEdgeId(int i, vtkIdType e)
set ith edge
void GetName(char *name) const
get name of branch
void DeleteLastVertex()
delete last vertex and edge from end of list
int GetNumberOfVertices() const
get number of vertices
void SetName(const char *name)
set name of branch
void GetVerticesIdList(vtkIdList *idlist) const
return vertices as a vtkIdlist
vtkIdType GetLastEdgeId() const
get id of last edge
Branch(vtkIdType startvertex, const char *name=NULL)
constructor: branch with initial vertex and optional name
vtkIdType GetMappingToOutputPolydata() const
get mapping from branch to output cell
vtkIdType GetLastVertexId() const
get id of last vertex
vtkIdType GetVertexId(int i) const
get index of ith vertex
Branch & operator=(const Branch &src)
const char * GetName() const
get name of branch
void SetVertexId(int i, vtkIdType v)
set ith vertex
void InsertEdgeId(int i, vtkIdType e)
insert edge
vtkIdType GetEdgeId(int i) const
get index of ith edge
Branch(const char *name=NULL)
constructor: empty branch with optional name
void UnsetName()
unset name of branch to undefined
bool SelfCheck() const
check self consistency (empty branches are allowed)
Nested Edge class This is a vertex pair.
bool SelfCheck() const
check self consistency
void SetDirected(bool directed)
set directed property
void ReverseDirection()
exchange vertices 0 and 1
void PrintSelf(std::ostream &os, const int tabs) const
print self
vtkIdType GetVertexId(int i) const
get index of vertex i, where i = 0 or 1
double GetWeight() const
get weight
void SetVertexIds(vtkIdType v0, vtkIdType v1)
set indices of vertices
vtkIdType GetOtherEndVertexId(vtkIdType v) const
get the vertex id at the other end of the edge
vtkIdType GetBranchId() const
get branch which edge belongs to
bool IsVertexPair(vtkIdType v0, vtkIdType v1) const
is the edge equal to these vertices (order doesn't matter)
bool IsVertexPairOrdered(vtkIdType v0, vtkIdType v1) const
is the edge equal to these vertices in this order
void GetMappingToOutputPolydata(vtkIdType *cellid, vtkIdType *index) const
get mapping from edge to location in output
void SetWeight(double w)
set weight
void SetBranchId(vtkIdType b)
set branch which edge belongs to
bool IsDirected() const
is edge directed
void SetVertexId(int i, vtkIdType v)
set index of vertex, where i = 0 or 1
Edge & operator=(const Edge &e)
assignment operator
Edge(vtkIdType v0, vtkIdType v1)
constructor with end vertices
void SetMappingToOutputPolydata(vtkIdType cellid, vtkIdType index)
set mapping from edge to location in output
Edge(const Edge &e)
copy constructor (because array member doesn't deep copy)
void GetVerticesIdList(vtkIdList *idlist) const
return vertices as a vtkIdlist
‍** Logs messages. Formatted *‍/
bool DeleteVertexId(vtkIdType v)
delete this vertex and assoc. edge from the vertex
void AddVertexId(vtkIdType v)
add new vertex id to end of list
vtkIdType GetEdgeId(int i) const
get index of ith edge
int GetDegree() const
return degree of vertex
bool DeleteEdgeId(vtkIdType e)
delete this edge and assoc. vertex from the vertex
int FindEdgeId(vtkIdType e) const
find position (0..deg-1) of this edge id in the list, return -1 if invalid
vtkIdType GetVertexId(int i) const
get index of ith vertex neighbour
void AddEdgeId(vtkIdType e)
add new edge id to end of list
bool SelfCheck() const
check self consistency
Vertex(double *coords)
constructor
void Clear()
clear data from vertex
void SetEdgeId(int i, vtkIdType e)
set ith edge
void SetCoords(const double *coords)
set coords of vertex
void GetCoords(double *coords) const
get coords of vertex
void GetVerticesIdList(vtkIdList *idlist) const
return vertices as a vtkIdlist
void PrintSelf(std::ostream &os, const int tabs) const
print self
int FindVertexId(vtkIdType v) const
find position (0..deg-1) of this vertex id in the list, return undefinedInt if invalid
void SetVertexId(int i, vtkIdType v)
set ith vertex neighbour
albaPolylineGraph class
void GetOutputCellCorrespondingToEdge(vtkIdType edgeid, vtkIdType *cellid, vtkIdType *index) const
void SetVertexCoords(vtkIdType v, const double *coords)
set coords of vertex
int GetNumberOfBranches() const
get no. of branches
vtkIdType GetMaxEdgeId() const
get index of last edge in graph
bool IsConnectedTree() const
is graph a connected tree
bool CopyFromPolydata(vtkPolyData *polydata)
Read graph from vtkPolyData (polyline only).
bool IsEdgeDirected(vtkIdType e) const
get directed property of edge
void MergeSimpleJoinedBranches()
Merges all branches in the graph connected in vertices of order 2 Be careful: this leads to reindex...
void AddNewVertex(double *coords=NULL)
Add new vertex to graph.
bool IsConnected() const
is graph connected
bool SplitBranchAtVertex(vtkIdType b, vtkIdType v)
Split branch at vertex v.
vtkIdType GetEdgeCorrespondingToOutputCell(vtkIdType cellid, vtkIdType index) const
int GetNumberOfVertices() const
get no. of vertices
int GetNumberOfEdges() const
get no. of edges
bool SelfCheck() const
check internal consistency
void Clear()
clear graph
bool AddNewVertex(vtkIdType v0, double *coords=NULL)
add new vertex, connecting to existing vertex
void AddNewBranch(const char *name=NULL)
Add new branch to graph.
vtkIdType GetMaxVertexId() const
get index of last vertex in graph
double GetBranchIntervalLength(vtkIdType b, vtkIdType startVertexId, vtkIdType endVertexId) const
const Edge * GetConstEdgePtr(vtkIdType i) const
return read-only pointer to edge, return NULL if index out of range
bool AddExistingEdgeToBranch(vtkIdType b, vtkIdType e)
Add existing edge to end of branch.
bool AddNewBranch(vtkIdType v0, const char *name=NULL)
add new branch with start vertex and optional name
void GetOutputCellCorrespondingToBranch(vtkIdType branchid, vtkIdType *cellid) const
void SetBranchName(vtkIdType b, const char *name)
set name of branch
const Branch * GetConstBranchPtr(vtkIdType i) const
return read-only pointer to branch, return NULL if index out of range
bool AddNewEdge(vtkIdType v0, vtkIdType v1)
Add new edge between existing vertices.
bool SplitBranchAtEdge(vtkIdType b, vtkIdType e)
Split branch by finding and removing edge.
albaPolylineGraph()
constructor: empty graph
bool MergeBranches(vtkIdType b1, vtkIdType b2)
Merges two branches.
void ReverseEdge(vtkIdType e)
reverse direction of edge (swap end vertices)
const char * GetBranchName(vtkIdType b) const
get name of branch
bool DeleteVertex(vtkIdType i)
Delete vertex from the graph.
bool DeleteEdge(vtkIdType i)
Delete edge from graph If the edge was a member of a branch, the branch will be split.
void SetEdgeWeight(vtkIdType e, double wt)
set weight of edge
void AddNewVertexToBranch(vtkIdType b, double *coords=NULL)
Add new vertex to end of branch.
void SetEdgeDirected(vtkIdType e, bool directed)
set directed property of edge
bool DeleteBranch(vtkIdType i)
Delete branch from the graph.
void GetVertexCoords(vtkIdType v, double *coords) const
get coords of vertex
bool AddExistingVertexToBranch(vtkIdType b, vtkIdType v, vtkIdType e=m_UndefinedId)
Add existing vertex to end of branch.
static const char * m_UndefinedName
void PrintSelf(std::ostream &os, const int tabs) const
print self
~albaPolylineGraph()
destructor
bool MergeSimpleJoinedBranchesAtVertex(vtkIdType v)
Merges two branches connected at the given vertex.
void UnsetBranchName(vtkIdType b)
unset name of branch to undefined
vtkIdType GetMaxBranchId() const
get index of last branch in graph
bool CopyToPolydata(vtkPolyData *polydata)
Write polyline graph to vtkPolyData.
vtkIdType GetBranchCorrespondingToOutputCell(vtkIdType cellid) const
const Vertex * GetConstVertexPtr(vtkIdType i) const
return read-only pointer to vertex, return NULL if index out of range
static const int m_UndefinedInt
double GetBranchLength(vtkIdType b) const
return the length of the branch
void ReverseBranch(vtkIdType b)
Reverse direction of branch This is useful if you want to add items at the front end of the branch.
double GetEdgeWeight(vtkIdType e) const
get weight of edge
vtkIdType FindBranchName(const char *name) const
find branch with name
static const vtkIdType m_UndefinedId
consts for undefined index values