graph.h

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 3 -*-
00002  *
00003  * Copyright (c), 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 
00044 #include <vector>
00045 
00046 #ifndef __PGRAPHH__
00047 #define __PGRAPHH__
00048 
00049 namespace pandore {
00050    
00051    class Graph2d;
00052    class Graph3d;
00053    
00054    
00055    /* @brief Trait that returns the name of the Pandore type.
00056     *
00057     * TypeName is a trait that returns the name
00058     * of the Pandore type T.
00059     */
00060    template<>
00061    struct TypeName< Graph2d > {
00066          static std::string Name() { return "Graph2d"; }
00067    };
00068    
00069    
00070    /* @brief Trait that returns the name of the Pandore type.
00071     *
00072     * TypeName is a trait that returns the name
00073     * of the Pandore type T.
00074     */
00075    template<> struct TypeName< Graph3d > {
00080          static std::string Name() { return "Graph3d"; }
00081    };
00082    
00090    class GEdge {
00091       private:
00092          
00094          GEdge *next; 
00095          
00097          Long node;
00098          
00100          Long item;
00101          
00102       public:
00104          Double weight;
00105          
00113          GEdge( Long n, GEdge *adj, Double w = 1 ): next(adj), node(n), item(-1), weight(w) {}
00114          
00123          GEdge( Long n, GEdge *adj, Long i, Double w = 1 ): next(adj), node(n), item(i), weight(w) {}
00124          
00128          ~GEdge() { }
00129          
00134          Long Node() const { return node; };
00135          
00140          GEdge* Next() const { return next; };
00141          
00147          GEdge* Next( GEdge* n ) {
00148             return next = n;
00149          }
00150          
00155          Long Item() const {
00156             return item;
00157          }
00158          
00164          Long Item( Long i ) {
00165             return item = i;
00166          }
00167    };
00168    
00177    template< class Point >
00178    class GNode {
00179       private:
00181          GEdge *adjacents;
00183          Long item;
00185          std::vector<GEdge *> etrash;
00186          
00187       public :
00189          Double value;
00191          Point seed;
00192          
00198          GNode( Long i ): adjacents(0), item(i), value(255.0) { }
00199 
00206          GNode( Long i, const Point p ): adjacents(0), item(i), value(255.0), seed(p) { }
00207          
00211          ~GNode();
00212          
00217          GEdge* Neighbours() const {
00218             return adjacents;
00219          }
00220          
00226          GEdge* Search( Long n ) const ;
00227          
00234          GEdge* Search( Long n, Long i ) const ;
00235          
00243          GEdge* Add( Long n, Double w );
00244          
00254          GEdge* Add( Long n, Long i, Double w );
00255          
00261          GEdge* Del( Long n );
00262          
00270          GEdge* Del( Long n, Long i );
00271          
00276          Long Item() const {
00277             return item;
00278          }
00279          
00285          Long Item( Long i ) {
00286             return item = i;
00287          }
00288    };
00289    
00290    /*
00291     * No GRAPH1D: it is not useful... It's my opinion.
00292     */
00293    
00307    class Graph2d: public Pobject {
00308       private :
00309          GNode< Point2d > **tnode;
00310          Long size;
00311          Long ncol;
00312          Long nrow;
00313          bool _directed;
00314          
00315          friend class GEdge;
00316          
00317       public :
00318          
00320          typedef Double ValueType;
00321          
00326          Typobj Type() const { return Po_Graph2d; }
00327          
00332          std::string Name() const { return TypeName< Graph2d >::Name(); }
00333          
00337          bool isDirected() const {
00338             return _directed;
00339          }
00340          
00345          Long Width() const {
00346             return ncol;
00347          }
00348          
00353          Long Height() const {
00354             return nrow;
00355          }
00356          
00361          Long Size() const {
00362             return size;
00363          }
00364          
00369          Dimension2d ImageSize() const {
00370             return Dimension2d(nrow, ncol);
00371          }
00372          
00377          PobjectProps Props() const {
00378             return PobjectProps(0, ncol, nrow, 0, (PColorSpace)0, 0, size, _directed);
00379          }
00380          
00385          Graph2d( bool directed = false): tnode(0), size(0), ncol(0), nrow(0), _directed(directed) { }
00386          
00395          Graph2d( Long s, bool directed = false ): tnode(0), size(0), _directed(directed) {
00396             New(s, 0, 0);
00397          }
00398          
00409          Graph2d( Long s, Long h, Long w, bool directed = false ): tnode(0), size(0), _directed(directed) {
00410             New(s, h, w);
00411          }
00412          
00422          Graph2d( Long s, const Dimension2d &d, bool directed = false ): tnode(0), size(0), _directed(directed) {
00423             New(s, d.h, d.w);
00424          }
00425          
00432          Graph2d( const PobjectProps &p ): tnode(0), size(0) {
00433             _directed = p.directed;
00434             New(p.size, p.nrow, p.ncol);
00435          }
00436          
00440          ~Graph2d() { 
00441             Delete(); 
00442          }
00443          
00452          void New( Long s, Long h, Long w );
00453          
00459          void New( const PobjectProps &p ) {
00460             New(p.size, p.nrow, p.ncol);
00461          }
00462          
00466          void Delete();
00467          
00475          Graph2d& operator=( const Graph2d &src );
00476          
00486          Errc Init( const Reg2d& rgs, const Reg2d& seeds );
00487          
00495          Errc Init( const Reg2d &rgs );
00496          
00501          Pobject *Clone() const;
00502          
00508          GNode<Point2d> *operator[]( Long pos ) {
00509             return tnode[pos];
00510          }
00511          
00517          const GNode<Point2d> *operator[]( Long pos ) const {
00518             return tnode[pos];
00519          }
00520          
00528          Errc Add( Long node, Long item, const Point2d &pt );
00529          
00537          Errc Add( Long node, Long item ) {
00538             return Add(node, item, Point2d(0, 0));
00539          }
00540          
00550          Errc Add( Long node, Long item, Long y, Long x ) {
00551             return Add(node, item, Point2d(y, x));
00552          }
00553          
00560          Errc Del( Long s );
00561          
00574          Errc Link( Long s1, Long s2, Double weight = 1.0F, bool add = false );
00575          
00589          Errc Link( Long s1, Long s2, Long i, Double weight = 1.0F, bool add = false );
00590          
00598          Errc Unlink( Long s1, Long s2 );
00599          
00610          Errc Unlink( Long s1, Long s2, Long i );
00611          
00621          Errc Merge( Long s1, Long s2 );
00622          
00631          Errc Split( Long s1, Long s2 );
00632          
00639          Errc LoadAttributes( FILE *file );
00640          
00646          Errc SaveAttributes( FILE *file ) const;
00647          
00653          Errc LoadData( FILE *file );
00654          
00660          Errc SaveData( FILE *file ) const;
00661          
00670          Pobject *Mask( const Pobject *mask ) ;
00671          
00681          Pobject *UnMask( const Pobject *mask, const Pobject *reference ) ;
00682          
00689          Graph2d( const Graph2d &grs ): Pobject() {
00690             *this = grs;
00691             _directed = grs._directed;
00692          }
00693    };
00694    
00708    class Graph3d: public Pobject {
00709       private :
00710          GNode<Point3d> **tnode;
00711          Long size;
00712          Long ncol;
00713          Long nrow;
00714          Long ndep;
00715          bool _directed;
00716          
00717          friend class GEdge;
00718          
00719       public :
00720          
00722          typedef Double ValueType;
00723          
00728          Typobj Type() const { return Po_Graph3d; }
00729          
00734          std::string Name() const { return TypeName< Graph3d >::Name(); }
00735          
00739          bool isDirected() const {
00740             return _directed;
00741          }
00742          
00747          Long Width() const { 
00748             return ncol;
00749          }
00750          
00755          Long Height() const { 
00756             return nrow; 
00757          }
00758          
00763          Long Depth() const { 
00764             return ndep;
00765          }
00766          
00770          Long Size() const {
00771             return size;
00772          }
00773          
00778          Dimension3d ImageSize() const {
00779             return Dimension3d(ndep, nrow, ncol);
00780          }
00781          
00786          PobjectProps Props() const {
00787             return PobjectProps(0, ncol, nrow, ndep, (PColorSpace)0, 0, size, _directed); 
00788          }
00789          
00794          Graph3d( bool directed = false ): tnode(0), size(0), ncol(0), nrow(0), ndep(0), _directed(directed) {
00795          }
00796          
00805          Graph3d( Long s, bool directed = false ): tnode(0), size(0), _directed(directed) {
00806             New(s, 0, 0, 0);
00807          }
00808          
00820          Graph3d( Long s, Long d, Long h, Long w, bool directed = false ): tnode(0), size(0), _directed(directed) {
00821             New(s, d, h, w);
00822          }
00823          
00833          Graph3d( Long s, const Dimension3d &d, bool directed = false ): tnode(0), size(0), _directed(directed) {
00834             New(s, d.d, d.h, d.w);
00835          }
00836          
00843          Graph3d( const PobjectProps& p ): tnode(0), size(0) {
00844             _directed = p.directed;
00845             New(p.size, p.ndep, p.nrow, p.ncol);
00846          }
00847          
00851          ~Graph3d() {
00852             Delete();
00853          }
00854          
00864          void New( Long s, Long d, Long h, Long w );
00865          
00871          void New( const PobjectProps &p ) {
00872             New(p.size, p.ndep, p.nrow, p.ncol);
00873          }
00874          
00878          void Delete();
00879          
00889          Errc Init( const Reg3d &rgs, const Reg3d &seeds );
00890          
00898          Errc Init( const Reg3d &rgs );
00899          
00907          Graph3d& operator=( const Graph3d &src );
00908          
00913          Pobject *Clone() const;
00914          
00920          GNode<Point3d> *operator[]( Long pos ) {
00921             return tnode[pos];
00922          }
00923          
00929          const GNode<Point3d> *operator[]( Long pos ) const {
00930             return tnode[pos];
00931          }
00932          
00939          Errc Add( Long node, Long item, const Point3d& pt );
00940          
00948          Errc Add( Long node, Long item ) {
00949             return Add(node, item, Point3d(0, 0, 0));
00950          }
00951          
00962          Errc Add( Long node, Long item, Long z, Long y, Long x ) {
00963             return Add(node, item, Point3d(z, y, x));
00964          }
00965          
00972          Errc Del( Long s );
00973          
00986          Errc Link( Long s1, Long s2, Double weight = 1.0F, bool add = false );
00987          
01001          Errc Link( Long s1, Long s2, Long i, Double weight = 1.0F, bool add = false );
01002          
01010          Errc Unlink( Long s1, Long s2 );
01011          
01020          Errc Unlink( Long s1, Long s2, Long i );
01021          
01031          Errc Merge( Long s1, Long s2 );
01032          
01041          Errc Split( Long s1, Long s2 );
01042          
01049          Errc LoadAttributes( FILE *file );
01050          
01056          Errc SaveAttributes( FILE *file ) const;
01057          
01063          Errc LoadData( FILE *file );
01064          
01071          Errc SaveData( FILE *file ) const;
01072          
01080          Pobject *Mask( const Pobject *mask ) ;
01081          
01091          Pobject *UnMask( const Pobject *mask, const Pobject *reference ) ;
01092          
01099          Graph3d( const Graph3d &grs ): Pobject() {
01100             _directed = grs._directed;
01101             *this = grs;
01102          }
01103    };
01104    
01105 } //End of pandore:: namespace
01106 
01107 #endif // __PGRAPHH__

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