graph.h

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 3 -*-
00002  *
00003  * Copyright (c) 2013, GREYC.
00004  * All rights reserved
00005  *
00006  * You may use this file under the terms of the BSD license as follows:
00007  *
00008  * "Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are
00010  * met:
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above copyright
00014  *     notice, this list of conditions and the following disclaimer in
00015  *     the documentation and/or other materials provided with the
00016  *     distribution.
00017  *   * Neither the name of the GREYC, nor the name of its
00018  *     contributors may be used to endorse or promote products
00019  *     derived from this software without specific prior written
00020  *     permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00025  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00026  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00028  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00029  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00030  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00031  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00032  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
00033  *
00034  *
00035  * For more information, refer to:
00036  * https://clouard.users.greyc.fr/Pandore
00037  */
00038 
00055 #include <vector>
00056 
00057 #ifndef __PGRAPHH__
00058 #define __PGRAPHH__
00059 
00060 namespace pandore {
00061    
00062    class Graph2d;
00063    class Graph3d;
00064    
00065    
00066    /* @brief Trait that returns the name of the Pandore type.
00067     *
00068     * TypeName is a trait that returns the name
00069     * of the Pandore type T.
00070     */
00071    template<>
00072    struct TypeName< Graph2d > {
00077          static std::string Name() { return "Graph2d"; }
00078    };
00079    
00080    
00081    /* @brief Trait that returns the name of the Pandore type.
00082     *
00083     * TypeName is a trait that returns the name
00084     * of the Pandore type T.
00085     */
00086    template<> struct TypeName< Graph3d > {
00091          static std::string Name() { return "Graph3d"; }
00092    };
00093    
00101    class GEdge {
00102       private:
00103          
00105          GEdge *next; 
00106          
00108          Long node;
00109          
00111          Long item;
00112          
00113       public:
00115          Double weight;
00116          
00124          GEdge( Long n, GEdge *adj, Double w = 1 ): next(adj), node(n), item(-1), weight(w) {}
00125          
00134          GEdge( Long n, GEdge *adj, Long i, Double w = 1 ): next(adj), node(n), item(i), weight(w) {}
00135          
00139          ~GEdge() { }
00140          
00145          Long Node() const { return node; };
00146          
00151          GEdge* Next() const { return next; };
00152          
00158          GEdge* Next( GEdge* n ) {
00159             return next = n;
00160          }
00161          
00166          Long Item() const {
00167             return item;
00168          }
00169          
00175          Long Item( Long i ) {
00176             return item = i;
00177          }
00178    };
00179    
00188    template< class Point >
00189    class GNode {
00190       private:
00192          GEdge *adjacents;
00194          Long item;
00196          std::vector<GEdge *> etrash;
00197          
00198       public :
00200          Double value;
00202          Point seed;
00203          
00209          GNode( Long i ): adjacents(0), item(i), value(255.0) { }
00210 
00217          GNode( Long i, const Point p ): adjacents(0), item(i), value(255.0), seed(p) { }
00218          
00222          ~GNode();
00223          
00228          GEdge* Neighbours() const {
00229             return adjacents;
00230          }
00231          
00237          GEdge* Search( Long n ) const ;
00238          
00245          GEdge* Search( Long n, Long i ) const ;
00246          
00254          GEdge* Add( Long n, Double w );
00255          
00265          GEdge* Add( Long n, Long i, Double w );
00266          
00272          GEdge* Del( Long n );
00273          
00281          GEdge* Del( Long n, Long i );
00282          
00287          Long Item() const {
00288             return item;
00289          }
00290          
00296          Long Item( Long i ) {
00297             return item = i;
00298          }
00299    };
00300    
00301    /*
00302     * No GRAPH1D: it is not useful... It's my opinion.
00303     */
00304    
00318    class Graph2d: public Pobject {
00319       private :
00320          GNode< Point2d > **tnode;
00321          Long size;
00322          Long ncol;
00323          Long nrow;
00324          bool _directed;
00325          
00326          friend class GEdge;
00327          
00328       public :
00329          
00331          typedef Double ValueType;
00332          
00337          Typobj Type() const { return Po_Graph2d; }
00338          
00343          std::string Name() const { return TypeName< Graph2d >::Name(); }
00344          
00348          bool isDirected() const {
00349             return _directed;
00350          }
00351          
00356          Long Width() const {
00357             return ncol;
00358          }
00359          
00364          Long Height() const {
00365             return nrow;
00366          }
00367          
00372          Long Size() const {
00373             return size;
00374          }
00375          
00380          Dimension2d ImageSize() const {
00381             return Dimension2d(nrow, ncol);
00382          }
00383          
00388          PobjectProps Props() const {
00389             return PobjectProps(0, ncol, nrow, 0, (PColorSpace)0, 0, size, _directed);
00390          }
00391          
00396          Graph2d( bool directed = false): tnode(0), size(0), ncol(0), nrow(0), _directed(directed) { }
00397          
00406          Graph2d( Long s, bool directed = false ): tnode(0), _directed(directed) {
00407             New(s, 0, 0);
00408          }
00409          
00420          Graph2d( Long s, Long h, Long w, bool directed = false ): tnode(0), _directed(directed) {
00421             New(s, h, w);
00422          }
00423          
00433          Graph2d( Long s, const Dimension2d &d, bool directed = false ): tnode(0), _directed(directed) {
00434             New(s, d.h, d.w);
00435          }
00436          
00443          Graph2d( const PobjectProps &p ): tnode(0) {
00444             _directed = p.directed;
00445             New(p.size, p.nrow, p.ncol);
00446          }
00447          
00451          ~Graph2d() { 
00452             Delete(); 
00453          }
00454          
00463          void New( Long s, Long h, Long w );
00464          
00470          void New( const PobjectProps &p ) {
00471             New(p.size, p.nrow, p.ncol);
00472          }
00473          
00477          void Delete();
00478          
00486          Graph2d& operator=( const Graph2d &src );
00487          
00497          Errc Init( const Reg2d& rgs, const Reg2d& seeds );
00498          
00506          Errc Init( const Reg2d &rgs );
00507          
00512          Pobject *Clone() const;
00513          
00519          GNode<Point2d> *operator[]( Long pos ) {
00520             return tnode[pos];
00521          }
00522          
00528          const GNode<Point2d> *operator[]( Long pos ) const {
00529             return tnode[pos];
00530          }
00531          
00539          Errc Add( Long node, Long item, const Point2d &pt );
00540          
00548          Errc Add( Long node, Long item ) {
00549             return Add(node, item, Point2d(0, 0));
00550          }
00551          
00561          Errc Add( Long node, Long item, Long y, Long x ) {
00562             return Add(node, item, Point2d(y, x));
00563          }
00564          
00571          Errc Del( Long s );
00572          
00585          Errc Link( Long s1, Long s2, Double weight = 1.0F, bool add = false );
00586          
00600          Errc Link( Long s1, Long s2, Long i, Double weight = 1.0F, bool add = false );
00601          
00609          Errc Unlink( Long s1, Long s2 );
00610          
00621          Errc Unlink( Long s1, Long s2, Long i );
00622          
00632          Errc Merge( Long s1, Long s2 );
00633          
00642          Errc Split( Long s1, Long s2 );
00643          
00650          Errc LoadAttributes( FILE *file );
00651          
00657          Errc SaveAttributes( FILE *file ) const;
00658          
00664          Errc LoadData( FILE *file );
00665          
00671          Errc SaveData( FILE *file ) const;
00672          
00681          Pobject *Mask( const Pobject *mask ) ;
00682          
00692          Pobject *UnMask( const Pobject *mask, const Pobject *reference ) ;
00693          
00700          Graph2d( const Graph2d &grs ): Pobject() {
00701             *this = grs;
00702             _directed = grs._directed;
00703          }
00704    };
00705    
00719    class Graph3d: public Pobject {
00720       private :
00721          GNode<Point3d> **tnode;
00722          Long size;
00723          Long ncol;
00724          Long nrow;
00725          Long ndep;
00726          bool _directed;
00727          
00728          friend class GEdge;
00729          
00730       public :
00731          
00733          typedef Double ValueType;
00734          
00739          Typobj Type() const { return Po_Graph3d; }
00740          
00745          std::string Name() const { return TypeName< Graph3d >::Name(); }
00746          
00750          bool isDirected() const {
00751             return _directed;
00752          }
00753          
00758          Long Width() const { 
00759             return ncol;
00760          }
00761          
00766          Long Height() const { 
00767             return nrow; 
00768          }
00769          
00774          Long Depth() const { 
00775             return ndep;
00776          }
00777          
00781          Long Size() const {
00782             return size;
00783          }
00784          
00789          Dimension3d ImageSize() const {
00790             return Dimension3d(ndep, nrow, ncol);
00791          }
00792          
00797          PobjectProps Props() const {
00798             return PobjectProps(0, ncol, nrow, ndep, (PColorSpace)0, 0, size, _directed); 
00799          }
00800          
00805          Graph3d( bool directed = false ): tnode(0), size(0), ncol(0), nrow(0), ndep(0), _directed(directed) {
00806          }
00807          
00816          Graph3d( Long s, bool directed = false ): tnode(0), _directed(directed) {
00817             New(s, 0, 0, 0);
00818          }
00819          
00831          Graph3d( Long s, Long d, Long h, Long w, bool directed = false ): tnode(0), _directed(directed) {
00832             New(s, d, h, w);
00833          }
00834          
00844          Graph3d( Long s, const Dimension3d &d, bool directed = false ): tnode(0), _directed(directed) {
00845             New(s, d.d, d.h, d.w);
00846          }
00847          
00854          Graph3d( const PobjectProps& p ): tnode(0) {
00855             _directed = p.directed;
00856             New(p.size, p.ndep, p.nrow, p.ncol);
00857          }
00858          
00862          ~Graph3d() {
00863             Delete();
00864          }
00865          
00875          void New( Long s, Long d, Long h, Long w );
00876          
00882          void New( const PobjectProps &p ) {
00883             New(p.size, p.ndep, p.nrow, p.ncol);
00884          }
00885          
00889          void Delete();
00890          
00900          Errc Init( const Reg3d &rgs, const Reg3d &seeds );
00901          
00909          Errc Init( const Reg3d &rgs );
00910          
00918          Graph3d& operator=( const Graph3d &src );
00919          
00924          Pobject *Clone() const;
00925          
00931          GNode<Point3d> *operator[]( Long pos ) {
00932             return tnode[pos];
00933          }
00934          
00940          const GNode<Point3d> *operator[]( Long pos ) const {
00941             return tnode[pos];
00942          }
00943          
00950          Errc Add( Long node, Long item, const Point3d& pt );
00951          
00959          Errc Add( Long node, Long item ) {
00960             return Add(node, item, Point3d(0, 0, 0));
00961          }
00962          
00973          Errc Add( Long node, Long item, Long z, Long y, Long x ) {
00974             return Add(node, item, Point3d(z, y, x));
00975          }
00976          
00983          Errc Del( Long s );
00984          
00997          Errc Link( Long s1, Long s2, Double weight = 1.0F, bool add = false );
00998          
01012          Errc Link( Long s1, Long s2, Long i, Double weight = 1.0F, bool add = false );
01013          
01021          Errc Unlink( Long s1, Long s2 );
01022          
01031          Errc Unlink( Long s1, Long s2, Long i );
01032          
01042          Errc Merge( Long s1, Long s2 );
01043          
01052          Errc Split( Long s1, Long s2 );
01053          
01060          Errc LoadAttributes( FILE *file );
01061          
01067          Errc SaveAttributes( FILE *file ) const;
01068          
01074          Errc LoadData( FILE *file );
01075          
01082          Errc SaveData( FILE *file ) const;
01083          
01091          Pobject *Mask( const Pobject *mask ) ;
01092          
01102          Pobject *UnMask( const Pobject *mask, const Pobject *reference ) ;
01103          
01110          Graph3d( const Graph3d &grs ): Pobject() {
01111             _directed = grs._directed;
01112             *this = grs;
01113          }
01114    };
01115    
01116 } //End of pandore:: namespace
01117 
01118 #endif // __PGRAPHH__

The Pantheon project
Image Team GREYC Laboratory
UMR CNRS 6072 - ENSICAEN - University of Caen, France
This page was last modified on