examples/triangle/triangle.cpp
author Dmitriy Morozov <dmitriy@mrzv.org>
Fri May 11 17:06:55 2012 -0700 (2 weeks ago)
branchdev
changeset 251 870865d25958
parent 100884f70adc576
permissions -rw-r--r--
Merge
     1 #include <utilities/log.h>
     2 
     3 #include "topology/simplex.h"
     4 #include "topology/filtration.h"
     5 #include "topology/static-persistence.h"
     6 #include "topology/dynamic-persistence.h"
     7 #include "topology/persistence-diagram.h"
     8 #include <utilities/indirect.h>
     9 
    10 #include <vector>
    11 #include <map>
    12 #include <iostream>
    13 
    14 
    15 #if 1
    16 #include <fstream>
    17 #include <boost/archive/text_oarchive.hpp>
    18 #include <boost/archive/text_iarchive.hpp>
    19 #include <boost/serialization/vector.hpp>
    20 #endif
    21 
    22 typedef         unsigned                                            Vertex;
    23 typedef         Simplex<Vertex, double>                             Smplx;
    24 typedef         Filtration<Smplx>                                   Fltr;
    25 // typedef         StaticPersistence<>                                 Persistence;
    26 typedef         DynamicPersistenceTrails<>                          Persistence;
    27 typedef         PersistenceDiagram<>                                PDgm;
    28 typedef         OffsetBeginMap<Persistence, Fltr, 
    29                                Persistence::iterator, 
    30                                Fltr::Index>                         PersistenceFiltrationMap;
    31 typedef         OffsetBeginMap<Fltr, Persistence,
    32                                Fltr::Index, 
    33                                Persistence::iterator>               FiltrationPersistenceMap;
    34 
    35 // Transposes elements of the filtration together with the 
    36 struct FiltrationTranspositionVisitor: public Persistence::TranspositionVisitor
    37 {
    38     typedef     Persistence::iterator                               iterator;
    39 
    40                 FiltrationTranspositionVisitor(const Persistence& p,
    41                                                Fltr& f):
    42                     p_(p), f_(f)                                    {}                                               
    43     void        transpose(iterator i)                               { f_.transpose(f_.begin() + (i - p_.begin())); }
    44 
    45     const Persistence&  p_;
    46     Fltr&               f_;
    47 };
    48 
    49 void fillTriangleSimplices(Fltr& c)
    50 {
    51     typedef std::vector<Vertex> VertexVector;
    52     VertexVector vertices(4);
    53     vertices[0] = 0; vertices[1] = 1; vertices[2] = 2; 
    54     vertices[3] = 0;
    55         
    56     VertexVector::const_iterator bg = vertices.begin();
    57     VertexVector::const_iterator end = vertices.end();
    58     c.push_back(Smplx(bg,     bg + 1, 0));                 // 0 = A
    59     c.push_back(Smplx(bg + 1, bg + 2, 1));                 // 1 = B
    60     c.push_back(Smplx(bg + 2, bg + 3, 2));                 // 2 = C
    61     c.push_back(Smplx(bg,     bg + 2, 2.5));               // AB
    62     c.push_back(Smplx(bg + 1, bg + 3, 2.9));               // BC
    63     c.push_back(Smplx(bg + 2, end,    3.5));               // CA
    64     c.push_back(Smplx(bg,     bg + 3, 5));                 // ABC
    65 }
    66 
    67 int main(int argc, char** argv)
    68 {
    69 #ifdef LOGGING
    70     rlog::RLogInit(argc, argv);
    71 
    72     stdoutLog.subscribeTo(RLOG_CHANNEL("topology/persistence"));
    73     //stdoutLog.subscribeTo(RLOG_CHANNEL("topology/chain"));
    74     //stdoutLog.subscribeTo(RLOG_CHANNEL("topology/vineyard"));
    75 #endif
    76 
    77     Fltr f;
    78     fillTriangleSimplices(f);
    79     std::cout << "Simplices filled" << std::endl;
    80     for (Fltr::Index cur = f.begin(); cur != f.end(); ++cur)
    81         std::cout << "  " << *cur << std::endl;
    82 
    83 #if 1           // testing serialization of the Filtration (really Simplex)
    84   {  
    85     std::ofstream ofs("complex");
    86     boost::archive::text_oarchive oa(ofs);
    87     oa << f;
    88     f.clear();
    89   }  
    90 
    91   {
    92     std::ifstream ifs("complex");
    93     boost::archive::text_iarchive ia(ifs);
    94     ia >> f;
    95   }  
    96 #endif
    97 
    98     f.sort(Smplx::DataComparison());
    99     std::cout << "Filtration initialized" << std::endl;
   100     std::cout << f << std::endl;
   101 
   102     Persistence p(f);
   103     std::cout << "Persistence initialized" << std::endl;
   104 
   105     p.pair_simplices();
   106     std::cout << "Simplices paired" << std::endl;
   107 
   108     Persistence::SimplexMap<Fltr>   m = p.make_simplex_map(f);
   109     std::map<Dimension, PDgm> dgms;
   110     init_diagrams(dgms, p.begin(), p.end(), 
   111                   evaluate_through_map(m, Smplx::DataEvaluator()),
   112                   evaluate_through_map(m, Smplx::DimensionExtractor()));
   113 
   114     std::cout << 0 << std::endl << dgms[0] << std::endl;
   115     std::cout << 1 << std::endl << dgms[1] << std::endl;
   116 
   117     PersistenceFiltrationMap                                    pfmap(p, f);
   118     DimensionFunctor<PersistenceFiltrationMap, Fltr>            dim(pfmap, f);
   119 
   120     // Transpositions
   121     FiltrationPersistenceMap        fpmap(f, p);
   122     FiltrationTranspositionVisitor  visitor(p, f);
   123     Smplx A;  A.add(0);
   124     std::cout << A << std::endl;
   125     std::cout << "Transposing A: " << p.transpose(fpmap[f.find(A)], dim, visitor) << std::endl;     // 1.2 unpaired
   126 
   127     Smplx BC; BC.add(1); BC.add(2);
   128     Smplx AB; AB.add(0); AB.add(1);
   129     std::cout << BC << std::endl;
   130     std::cout << p.transpose(fpmap[f.find(BC)], dim, visitor) << std::endl;         // 3.1
   131     // p.transpose(fpmap[f.find(BC)], dim, visitor);
   132     std::cout << AB << std::endl;
   133     std::cout << p.transpose(fpmap[f.find(AB)], dim, visitor) << std::endl;         // 2.1
   134     // p.transpose(fpmap[f.find(AB)], dim, visitor);
   135 
   136     std::cout << p.transpose(p.begin(), dim, visitor) << std::endl;         // transposition case 1.2 special
   137     std::cout << p.transpose(boost::next(p.begin()), dim, visitor) << std::endl;
   138     std::cout << p.transpose(boost::next(p.begin(),3), dim, visitor) << std::endl;
   139 }
   140