36#ifndef _CHOMP_HOMOLOGY_HOMTOOLS_H_
37#define _CHOMP_HOMOLOGY_HOMTOOLS_H_
59 std::ifstream in (name);
65 sout <<
"Scanning the file '" << name <<
"' for cubes... ";
72 typename tCube::CoordType c [tCube::MaxDim];
83 else if ((dim != d) && !warned)
85 sout <<
"\nWARNING: Not all the cubes have "
86 "the same dimension.\n";
92 int_t n = tCube::PointBase::number (c, d);
93 if ((n != count) && !warned)
96 sout <<
"\nWARNING: Some cubes are "
107 sout << count <<
" cubes analyzed.\n";
116template <
class settype>
117void readtheset (
const char *name, settype &s,
const char *pluralname,
125 sout <<
"Reading " << pluralname;
127 sout <<
" to " << what;
128 sout <<
" from '" << name <<
"'... ";
131 std::ifstream in (name);
138 ((in. peek () <
'0') || (in. peek () >
'9')) &&
139 (in. peek () !=
'-'))
146 int_t prev = s. size ();
148 sout << (s. size () - prev) <<
" " << pluralname <<
" read.\n";
154template <
class cell,
class eucl
idom>
158 readtheset (name, s, cell::pluralname (), what);
163template <
class element>
167 readtheset (name, s, element::pluralname (), what);
180template <
class element>
185 sout <<
"Reading the domain of the map from '" << name <<
"'... ";
186 std::ifstream in (name);
189 int_t prev = cub. size ();
191 sout << (cub. size () - prev) <<
" " << element::pluralname () <<
197template <
class element>
202 sout <<
"Reading the image of the map from '" << name <<
"'... ";
203 std::ifstream in (name);
206 int_t prev = cub. size ();
208 sout << (cub. size () - prev) <<
" " << element::pluralname () <<
215template<
class element>
222 sout <<
"Reading the image of " << domname <<
" by the map '" <<
224 std::ifstream in (filename);
227 int_t prev = cub. size ();
229 sout << (cub. size () - prev) <<
" " << element::pluralname () <<
235template <
class element>
239 const char *Xname,
const char *purpose = 0)
241 if (!mapname || (Xcubes. empty () && Acubes. empty ()))
243 sout <<
"Reading the map on " << Xname <<
" from '" << mapname;
245 sout <<
"' " << purpose <<
"... ";
248 std::ifstream in (mapname);
252 if (Fcubmap. getdomain (). size () !=
253 Xcubes. size () + Acubes. size ())
255 sout <<
"\nWARNING: The map is not defined "
256 "on some cubes in " << Xname <<
".\n";
264template <
class element>
268 const char *purpose = NULL)
282template <
class settype>
283void savetheset (
const char *name,
const settype &s,
const char *pluralname,
284 const char *what,
const char *filecomment = 0)
291 sout <<
"Saving " << pluralname;
293 sout <<
" in " << what;
294 sout <<
" to '" << name <<
"'... ";
297 std::ofstream out (name);
309 sout << s. size () <<
" " << pluralname <<
" written.\n";
315template <
class cell,
class eucl
idom>
317 const char *what,
const char *filecomment = 0)
319 savetheset (name, s, cell::pluralname (), what, filecomment);
324template <
class element>
326 const char *what,
const char *filecomment = 0)
328 savetheset (name, s, element::pluralname (), what, filecomment);
335 const char *what,
const char *filecomment = 0)
348template <
class cubsettype>
350 const cubsettype &Bcubes,
const char *Xname,
const char *YBname)
352 sout <<
"Verifying if " << Xname <<
" is contained in " <<
355 for (
int_t i = 0; !failed && (i < Xcubes. size ()); ++ i)
357 if (!Ycubes. check (Xcubes [i]) &&
358 !Bcubes. check (Xcubes [i]))
364 sout <<
"Failed.\nWARNING: The set " << Xname <<
365 " is NOT contained in " << YBname <<
".\n";
373template <
class cubsettype>
375 const cubsettype &Ycubes,
const char *Xname,
const char *Yname)
383template <
class maptype,
class cubsettype>
385 const cubsettype &Ycubes,
const cubsettype &Bcubes,
386 const char *Xname,
const char *Yname)
388 sout <<
"Verifying if the image of " << Xname <<
389 " is contained in " << Yname <<
"... ";
391 for (
int_t i = 0; !failed && (i < Xcubes. size ()); ++ i)
393 if (!Fcubmap. getdomain (). check (Xcubes [i]))
395 const cubsettype &cset = Fcubmap (Xcubes [i]);
396 for (
int_t j = 0; !failed && (j < cset. size ()); ++ j)
398 if (!Ycubes. check (cset [j]) &&
399 !Bcubes. check (cset [j]))
406 sout <<
"Failed.\nWARNING: The image of " << Xname <<
407 " is NOT contained in " << Yname <<
".\n";
415template <
class maptype,
class cubsettype>
417 const cubsettype &Xcubes,
const cubsettype &Ycubes,
418 const char *Xname,
const char *Yname)
427template <
class maptype,
class cubsettype>
429 const cubsettype &Ycubes,
const char *Aname,
const char *Yname)
431 sout <<
"Verifying if the image of " << Aname <<
432 " is disjoint from " << Yname <<
"... ";
434 for (
int_t i = 0; !failed && (i < Acubes. size ()); ++ i)
436 if (!Fcubmap. getdomain (). check (Acubes [i]))
438 const cubsettype &cset = Fcubmap (Acubes [i]);
439 for (
int_t j = 0; !failed && (j < cset. size ()); ++ j)
440 if (Ycubes. check (cset [j]))
444 sout <<
"Failed.\nSERIOUS WARNING: The image of " << Aname <<
445 " is NOT disjoint from " << Yname <<
".\n";
453template <
class tCell,
class tCube,
class tCoef>
457 sout <<
"Verifying if the map on " << Xname <<
" is acyclic... ";
463 for (
int_t d = 0; !failed && (d <= dom. dim ()); ++ d)
467 for (
int_t i = 0; !failed && (i < qset. size ()); ++ i)
470 if (counter && !(counter % 373))
471 scon << std::setw (10) << counter <<
472 "\b\b\b\b\b\b\b\b\b\b";
477 if (img. size () == 1)
486 sout <<
"Failed.\n*** WARNING: The map on " << Xname <<
487 " is NOT acyclic. ***\n"
488 "*** The result of the computations "
489 "may be totally wrong. ***\n";
491 sout <<
"Passed. \n";
502template <
class cubsettype>
504 const char *Xname,
const char *Aname,
505 const cubsettype *keepcubes = 0)
508 if (Acubes. empty ())
512 sout <<
"Restricting " << Aname <<
" to the neighbors of " <<
513 Xname <<
"\\" << Aname <<
"... ";
516 int_t prev = Acubes. size ();
519 if (Xcubes. empty ())
527 for (
int_t i = 0; i < Acubes. size (); ++ i)
529 if (keepcubes && keepcubes -> check (Acubes [i]))
533 sseq <<
'0' << Acubes [i] <<
'\n';
534 Acubes. removenum (i);
540 sout << (prev - Acubes. size ()) <<
" cubes removed, " <<
541 Acubes. size () <<
" left.\n";
547template <
class cubsettype>
549 const char *Xname,
const char *Aname)
551 if (Xcubes. empty () || Acubes. empty ())
553 sout <<
"Computing " << Xname <<
"\\" << Aname <<
"... ";
554 int_t prev = Xcubes. size ();
555 Xcubes. remove (Acubes);
556 sout << (prev - Xcubes. size ()) <<
" cubes removed from " <<
557 Xname <<
", " << Xcubes. size () <<
" left.\n";
562template <
class cell,
class eucl
idom>
565 const char *Xname,
const char *Aname)
567 if (X. empty () || A. empty ())
569 sout <<
"Computing " << Xname <<
"\\" << Aname <<
"... ";
570 int_t prev = X. size ();
572 sout << (prev - X. size ()) <<
' ' << cell::pluralname () <<
573 " removed from " << Xname <<
", " << X. size () <<
579template <
class cubsettype>
581 const char *Xname,
const char *Aname)
583 if (Xcubes. empty () || Acubes. empty ())
585 sout <<
"Expanding " << Aname <<
" in " << Xname <<
"... ";
587 sout << count <<
" cubes moved to " << Aname <<
", " <<
588 Xcubes. size () <<
" left in " << Xname <<
"\\" << Aname <<
594template <
class cubsettype,
class maptype>
595void expandAinX (cubsettype &Xcubes, cubsettype &Acubes, cubsettype &Ycubes,
596 cubsettype &Bcubes,
const maptype &Fcubmap,
597 const char *Xname,
const char *Aname,
const char *Bname,
598 bool indexmap,
bool checkacyclic)
600 if (Xcubes. empty () || Acubes. empty ())
602 sout <<
"Expanding " << Aname <<
" in " << Xname <<
"... ";
603 int_t prevB = Bcubes. size ();
604 int_t prevY = Ycubes. size ();
606 Fcubmap, indexmap, checkacyclic);
607 sout << count <<
" moved to " << Aname <<
", " << Xcubes. size () <<
608 " left in " << Xname <<
"\\" << Aname <<
", " <<
609 (Bcubes. size () - prevB) <<
" added to " << Bname <<
".\n";
610 if (prevY - Ycubes. size () != Bcubes. size () - prevB)
611 sout <<
"WARNING: The image of " << Xname <<
"\\" << Aname <<
612 " was not contained in Y. "
613 "The result can be wrong!\n";
618template <
class cubsettype>
620 const cubsettype &Xkeepcubes,
const char *Xname,
const char *Aname)
622 if (Xcubes. empty ())
624 sout <<
"Reducing full-dim cubes from ";
625 if (!Acubes. empty ())
626 sout <<
'(' << Xname <<
',' << Aname <<
")... ";
628 sout << Xname <<
"... ";
630 sout << count <<
" removed, " <<
631 (Xcubes. size () + Acubes. size ()) <<
" left.\n";
637template <
class maptype,
class cubsettype>
638void reducepair (cubsettype &Xcubes, cubsettype &Acubes, maptype &Fcubmap,
639 const cubsettype &Xkeepcubes,
const char *Xname,
const char *Aname)
641 if (Xcubes. empty ())
643 sout <<
"Reducing cubes from ";
644 if (!Acubes. empty ())
645 sout <<
'(' << Xname <<
',' << Aname <<
") [acyclic]... ";
647 sout << Xname <<
" [acyclic]... ";
649 sout << count <<
" removed, " <<
650 (Xcubes. size () + Acubes. size ()) <<
" left.\n";
657template <
class maptype,
class cubsettype>
658void addmapimg (
const maptype &Fcubmap,
const maptype &FcubmapA,
659 const cubsettype &Xcubes,
const cubsettype &Acubes,
660 cubsettype &Ykeepcubes,
bool indexmap)
662 if (Fcubmap. getdomain (). empty () &&
663 FcubmapA. getdomain (). empty ())
667 sout <<
"Computing the image of the map... ";
668 int_t prev = Ykeepcubes. size ();
669 const cubsettype &Fdomain = Fcubmap. getdomain ();
670 if (!Fdomain. empty ())
672 if (Fdomain. size () == Xcubes. size ())
676 int_t n = Xcubes. size ();
677 for (
int_t i = 0; i < n; ++ i)
678 Ykeepcubes. add (Fcubmap (Xcubes [i]));
681 const cubsettype &FdomainA = FcubmapA. getdomain ();
682 if (!FdomainA. empty ())
684 if (FdomainA. size () == Acubes. size ())
688 int_t n = Acubes. size ();
689 for (
int_t i = 0; i < n; ++ i)
690 Ykeepcubes. add (FcubmapA (Acubes [i]));
695 sout <<
"and of the inclusion... ";
696 Ykeepcubes. add (Xcubes);
697 Ykeepcubes. add (Acubes);
699 sout << (Ykeepcubes. size () - prev) <<
" cubes.\n";
705template <
class maptype,
class cubsettype>
707 const cubsettype &Xcubes,
const cubsettype &Acubes,
708 cubsettype &Ykeepcubes,
bool indexmap)
710 addmapimg (Fcubmap, Fcubmap, Xcubes, Acubes, Ykeepcubes, indexmap);
715template <
class tCubes,
class tCell,
class tCoef>
717 const char *Xname,
bool deletecubes =
true)
720 if (Xcubes. empty ())
724 int_t prev = Xcompl. size ();
725 sout <<
"Transforming " << Xname <<
" into cells... ";
726 for (
int_t i = 0; i < Xcubes. size (); ++ i)
727 Xcompl. add (tCell (Xcubes [i]));
728 sout << (Xcompl. size () - prev) <<
" cells added.\n";
741template <
class cell,
class eucl
idom>
744 const char *Xname,
const char *Aname,
bool addbd =
true,
745 bool addcob =
false,
bool disjoint =
true,
const int *level = NULL)
748 sout <<
"Collapsing faces in " << Xname;
749 if (!Acompl. empty ())
750 sout <<
" and " << Aname;
755 addbd, addcob, disjoint, level);
758 sout << (count << 1) <<
" removed, " <<
759 Xcompl. size () <<
" left.\n";
762 if (!Acompl. empty ())
763 sout <<
"There are " << Acompl. size () <<
" faces "
764 "of dimension up to " << Acompl. dim () <<
765 " left in " << Aname <<
".\n";
772template <
class cell,
class eucl
idom>
775 const char *Xname,
const char *Aname,
bool addbd =
true,
776 bool addcob =
false,
bool disjoint =
true,
const int *level = NULL)
779 collapse (Xcompl, Acompl, empty, Xname, Aname,
780 addbd, addcob, disjoint, level);
785template <
class cell,
class eucl
idom>
788 const char *Xname,
bool addbd =
true,
bool addcob =
false,
789 bool disjoint =
true,
const int *level = NULL)
792 collapse (Xcompl, empty, Xkeepcompl, Xname,
"",
793 addbd, addcob, disjoint, level);
799template <
class cell,
class eucl
idom>
801 const char *Xname,
bool addbd =
true,
bool addcob =
false,
802 bool disjoint =
true,
const int *level = NULL)
805 collapse (Xcompl, empty, empty, Xname,
"",
806 addbd, addcob, disjoint, level);
812template <
class cell,
class eucl
idom>
814 int dim,
const char *name)
816 if (Acompl. dim () <= dim)
820 sout <<
"Adding to " << name <<
" boundaries of high-dim " <<
821 cell::pluralname () <<
"... ";
823 for (
int i = Acompl. dim (); i > dim; -- i)
827 Acompl. removeall (i);
829 sout <<
' ' << howmany <<
" added.\n";
834template <
class cell,
class eucl
idom>
837 int minlevel,
bool bothsets,
const char *Xname,
const char *Aname)
840 if (Xcompl. empty ())
844 sout <<
"Adding boundaries of " << cell::pluralname () <<
" in ";
845 if (!Acompl. empty ())
846 sout << Xname <<
" and " << Aname <<
"... ";
848 sout << Xname <<
"... ";
852 for (
int i = Xcompl. dim (); (i >= minlevel) && i; -- i)
857 if (Acompl. dim () >= i)
866 sout << howmany <<
' ' << cell::pluralname () <<
" added.\n";
890template <
class tCube>
892 int mingray = 0,
int maxgray = 128)
896 bmp. invertedpicture ();
899 if (bmp. open (bmpname) < 0)
904 for (c [1] = 0; c [1] < bmp. picture_height (); ++ (c [1]))
906 for (c [0] = 0; c [0] < bmp. picture_width (); ++ (c [0]))
908 long color = bmp. getpixel (c [0], c [1]);
909 long gray = (77 * ((color & 0xFF0000) >> 16) +
910 154 * ((color & 0xFF00) >> 8) +
911 25 * (color & 0xFF)) >> 8;
912 if ((gray >= mingray) && (gray <= maxgray))
913 cset. add (tCube (c, 2));
This file contains the definition of a class which can be used as a simple interface to read and writ...
The class 'bmpfile' is an elementary interface that can be used to read or create and write images in...
The class that defines a geometric complex - a set of cells (cubes, simplices, etc).
This is a template for a set of objects of the given type.
This class represents a multivalued map whose domain is a geometric complex.
This class defines a multivalued map.
This class defines a hypercube in R^n with edges parallel to the axes and with size 1 in each directi...
static const char * pluralname()
Returns the plural name of the objects represented by this class.
This file contains an interface to procedures for full cubical reduction.
This file contains some procedures defined for cubical maps.
int int_t
Index type for indexing arrays, counting cubes, etc.
outputstream sseq
An auxiliary stream which captures sequences of processed data.
void readmapdomain(const char *name, hashedset< element > &cub)
Reads the domain of a cubical multivalued map from the given file.
void saveelements(const char *name, const hashedset< element > &s, const char *what, const char *filecomment=0)
Uses the general procedure "savetheset" to save a set of elements.
int_t getneighbors(const tCube &q, BitField *bits, const tCubeSet1 &theset, tCubeSet2 *neighbors, int_t limit)
Gets neighbors of the given cube from the given set and indicates them in the bit field provided.
bool acyclic(int dim, BitField &b)
Checks whether this cube's nieghbors form an acyclic set.
bool checkimagecontained(const maptype &Fcubmap, const cubsettype &Xcubes, const cubsettype &Ycubes, const cubsettype &Bcubes, const char *Xname, const char *Yname)
Checks if the image of X by F is contained in the union of Y and B.
void ignoreline(std::istream &in)
Ignores the input characters until the end of a line, including this end of the line.
outputstream sout
A replacement for standard output stream, with optional logging and other features provided by the cl...
int closingparenthesis(int ch)
Returns the matching closing parenthesis for the given opening one or EOF if none.
void readmaprestriction(mvmap< element, element > &Fcubmap, const char *mapname, const hashedset< element > &Xcubes, const hashedset< element > &Acubes, const char *Xname, const char *purpose=0)
Reads the restriction of a multivalued map to the union of two sets.
void fileerror(const char *filename, const char *what="open")
Throws a message about the inability to do something with a file.
void reducepair(cubsettype &Xcubes, cubsettype &Acubes, const cubsettype &Xkeepcubes, const char *Xname, const char *Aname)
Reduces the pair of sets of cubes. Keeps the given cubes untouched.
int ReadBitmapFile(const char *bmpname, hashedset< tCube > &cset, int mingray=0, int maxgray=128)
Reads the squares from a bitmap file to the set of cubes.
void cubes2cells(tCubes &Xcubes, gcomplex< tCell, tCoef > &Xcompl, const char *Xname, bool deletecubes=true)
Transforms cubes to full-dimensional cells.
void addboundaries(gcomplex< cell, euclidom > &Xcompl, gcomplex< cell, euclidom > &Acompl, int minlevel, bool bothsets, const char *Xname, const char *Aname)
Adds boundaries to the geometric complex X or to both X and A.
bool checkinclusion(const cubsettype &Xcubes, const cubsettype &Ycubes, const cubsettype &Bcubes, const char *Xname, const char *YBname)
Checks if X is a subset of the union of Y and B.
std::istream & readimage(std::istream &in, hashedset< tCube > &img)
Reads the image of a multivalued cubical map.
void readtheset(const char *name, settype &s, const char *pluralname, const char *what)
Reads a given set from the file and shows appropriate messages.
void collapse(gcomplex< cell, euclidom > &Xcompl, gcomplex< cell, euclidom > &Acompl, gcomplex< cell, euclidom > &Xkeepcompl, const char *Xname, const char *Aname, bool addbd=true, bool addcob=false, bool disjoint=true, const int *level=NULL)
Collapses a pair of geometric complexes.
int_t cubexpand(hashedset< tCube > &cset, hashedset< tCube > &other, bool quiet=false)
Expands the set 'other' towards 'cset' without changing the homology of (cset + other,...
bool checkacyclicmap(const mvcellmap< tCell, tCoef, tCube > &Fcellcubmap, const char *Xname)
Checks if the image of each element of the domain of this map is acyclic.
void savetheset(const char *name, const settype &s, const char *pluralname, const char *what, const char *filecomment=0)
Saves a given set to a file and shows appropriate messages.
short int coordinate
The default type of coordinates.
int readcoordinates(std::istream &in, coordtype *coord, int maxdim, int closing)
Reads the coordinates of a point.
void readcells(const char *name, gcomplex< cell, euclidom > &s, const char *what)
Uses the general procedure "readtheset" to read a geometric complex.
void expandAinX(cubsettype &Xcubes, cubsettype &Acubes, const char *Xname, const char *Aname)
Expands the other element of the pair into the main portion of the set.
std::istream & readselective(std::istream &in, const hashedset< tCube > &dom1, const hashedset< tCube > &dom2, mvmap< tCube, tCube > &m)
Reads the restriction of a multivalued map to the given pair of sets.
void readmapimage(const char *name, hashedset< element > &cub)
Reads the domain of a cubical multivalued map from the given file.
hashedset< imgelement > & retrieveimage(const mvmap< domelement, imgelement > &m, hashedset< imgelement > &img)
Adds images of all the elements from the domain of the map to 'img'.
bool checkimagedisjoint(const maptype &Fcubmap, const cubsettype &Acubes, const cubsettype &Ycubes, const char *Aname, const char *Yname)
Checks if the image of A by F is disjoint from Y (actually, from Y\B).
void decreasedimension(gcomplex< cell, euclidom > &Acompl, int dim, const char *name)
Decreases the dimension of the geometric complex by adding boundary cells to all the cells on higher ...
outputstream scon
The console output stream to which one should put all the junk that spoils the log file,...
void readelements(const char *name, hashedset< element > &s, const char *what)
Uses the general procedure "readtheset" to read a set of elements.
void ignorecomments(std::istream &in)
Ignores white characters (spaces, tabulators, CRs and LFs), as well as comments from the input text f...
void restrictAtoneighbors(const cubsettype &Xcubes, cubsettype &Acubes, const char *Xname, const char *Aname, const cubsettype *keepcubes=0)
Restricts the set of cubes 'Acubes' to these cubes which are neighbors of any of the cubes in 'Xcubes...
void removeAfromX(cubsettype &Xcubes, const cubsettype &Acubes, const char *Xname, const char *Aname)
Removes 'Acubes' from 'Xcubes' and shows messages.
void addmapimg(const maptype &Fcubmap, const maptype &FcubmapA, const cubsettype &Xcubes, const cubsettype &Acubes, cubsettype &Ykeepcubes, bool indexmap)
Adds the images of both maps to the set of cubes to be kept.
void savecells(const char *name, const gcomplex< cell, euclidom > &s, const char *what, const char *filecomment=0)
Uses the general procedure "savetheset" to save a geometric complex.
std::istream & readdomain(std::istream &in, hashedset< tCube > &dom)
Reads the domain of a multivalued cubical map.
int_t cubreduce(hashedset< tCube > &cset, hashedset< tCube > &other, mvmap< tCube, tCube > &cubmap, const hashedset< tCube > &keep)
Reduces a pair of sets of cubes for relative homology computation.
void scancubes(const char *name)
Reads all the cubes from the given file and ignores them.
This namespace contains the entire CHomP library interface.
This file contains the definition of a class which represents a simplex.