read_points() does not need ambient dimension and ignores lines starting with '#' dev
authorDmitriy Morozov <dmitriy@mrzv.org>
Thu, 14 May 2009 13:09:06 -0700
branchdev
changeset 135 fc8ebbae62e2
parent 134 c270826fd4a8
child 136 beff535b53ff
read_points() does not need ambient dimension and ignores lines starting with '#'
examples/cohomology/rips-pairwise-cohomology.cpp
examples/consistency/rips-consistency-zigzag.cpp
examples/rips/rips-image-zigzag.cpp
examples/rips/rips-pairwise.cpp
examples/rips/rips-zigzag.cpp
include/geometry/l2distance.h
--- a/examples/cohomology/rips-pairwise-cohomology.cpp	Mon May 11 12:45:49 2009 -0700
+++ b/examples/cohomology/rips-pairwise-cohomology.cpp	Thu May 14 13:09:06 2009 -0700
@@ -26,18 +26,18 @@
                      Smplx::VertexComparison>                       Complex;
 typedef     std::vector<Smplx>                                      SimplexVector;
 
-void        program_options(int argc, char* argv[], std::string& infilename, Dimension& ambient, Dimension& skeleton, DistanceType& max_distance);
+void        program_options(int argc, char* argv[], std::string& infilename, Dimension& skeleton, DistanceType& max_distance);
 
 int main(int argc, char* argv[])
 {
-    Dimension               ambient, skeleton;
+    Dimension               skeleton;
     DistanceType            max_distance;
     std::string             infilename;
 
-    program_options(argc, argv, infilename, ambient, skeleton, max_distance);
+    program_options(argc, argv, infilename, skeleton, max_distance);
 
     PointContainer          points;
-    read_points(infilename, points, ambient);
+    read_points(infilename, points);
 
     PairDistances           distances(points);
     Generator               rips(distances);
@@ -68,7 +68,7 @@
     persistence_timer.check("Persistence timer");
 }
 
-void        program_options(int argc, char* argv[], std::string& infilename, Dimension& ambient, Dimension& skeleton, DistanceType& max_distance)
+void        program_options(int argc, char* argv[], std::string& infilename, Dimension& skeleton, DistanceType& max_distance)
 {
     namespace po = boost::program_options;
 
@@ -79,7 +79,6 @@
     po::options_description visible("Allowed options", 100);
     visible.add_options()
         ("help,h",                                                                                  "produce help message")
-        ("ambient-dimsnion,a",  po::value<Dimension>(&ambient)->default_value(3),                   "The ambient dimension of the point set")
         ("skeleton-dimsnion,s", po::value<Dimension>(&skeleton)->default_value(2),                  "Dimension of the Rips complex we want to compute")
         ("max-distance,m",      po::value<DistanceType>(&max_distance)->default_value(Infinity),    "Maximum value for the Rips complex construction");
 
--- a/examples/consistency/rips-consistency-zigzag.cpp	Mon May 11 12:45:49 2009 -0700
+++ b/examples/consistency/rips-consistency-zigzag.cpp	Thu May 14 13:09:06 2009 -0700
@@ -71,7 +71,6 @@
 void        make_boundary(const Smplx& s, Complex& c, const Zigzag& zz, Boundary& b);
 void        process_command_line_options(int           argc,
                                          char*         argv[],
-                                         unsigned&     ambient_dimension,
                                          unsigned&     skeleton_dimension,
                                          float&        epsilon,
                                          std::string&  points_filename,
@@ -98,16 +97,15 @@
     SetTrigger(cOperations, cComplexSize);
 #endif
 
-    unsigned        ambient_dimension;
     unsigned        skeleton_dimension;
     float           epsilon;
     std::string     points_filename, subsample_filename, outfilename;
-    process_command_line_options(argc, argv, ambient_dimension, skeleton_dimension, epsilon, 
+    process_command_line_options(argc, argv, skeleton_dimension, epsilon, 
                                              points_filename, subsample_filename, outfilename);
 
     // Read in points
     PointContainer      points;
-    read_points(points_filename, points, ambient_dimension);
+    read_points(points_filename, points);
     
     // Read in subsamples
     std::ifstream subsample_in(subsample_filename.c_str());
@@ -285,7 +283,6 @@
 
 void        process_command_line_options(int           argc,
                                          char*         argv[],
-                                         unsigned&     ambient_dimension,
                                          unsigned&     skeleton_dimension,
                                          float&        epsilon,
                                          std::string&  points_filename,
@@ -303,7 +300,6 @@
     po::options_description visible("Allowed options", 100);
     visible.add_options()
         ("help,h",                                                                              "produce help message")
-        ("ambient-dimsnion,a",  po::value<unsigned>(&ambient_dimension)->default_value(3),      "The ambient dimension of the point set")
         ("skeleton-dimsnion,s", po::value<unsigned>(&skeleton_dimension)->default_value(2),     "Dimension of the Rips complex we want to compute")
         ("epsilon,e",           po::value<float>(&epsilon)->default_value(0),                   "epsilon to use when computing the Rips complex");
 #if LOGGING
--- a/examples/rips/rips-image-zigzag.cpp	Mon May 11 12:45:49 2009 -0700
+++ b/examples/rips/rips-image-zigzag.cpp	Thu May 14 13:09:06 2009 -0700
@@ -7,6 +7,8 @@
 #include <utilities/memory.h>       // for report_memory()
 #include <utilities/timer.h>
 
+#include <geometry/l2distance.h>    // for L2Distance and read_points()
+
 #include <map>
 #include <cmath>
 #include <fstream>
@@ -23,21 +25,6 @@
 static Counter*  cOperations =                      GetCounter("rips/operations");
 #endif // COUNTERS
 
-typedef     std::vector<double>                                     Point;
-typedef     std::vector<Point>                                      PointContainer;
-struct L2Distance:
-    public std::binary_function<const Point&, const Point&, double>
-{
-    result_type     operator()(const Point& p1, const Point& p2) const
-    {
-        AssertMsg(p1.size() == p2.size(), "Points must be in the same dimension (in L2Distance)");
-        result_type sum = 0;
-        for (size_t i = 0; i < p1.size(); ++i)
-            sum += (p1[i] - p2[i])*(p1[i] - p2[i]);
-
-        return sqrt(sum);
-    }
-};
 typedef     PairwiseDistances<PointContainer, L2Distance>           PairDistances;
 typedef     PairDistances::DistanceType                             DistanceType;
 
@@ -78,7 +65,6 @@
 std::ostream&   operator<<(std::ostream& out, const BirthInfo& bi);
 void        process_command_line_options(int           argc,
                                          char*         argv[],
-                                         unsigned&     ambient_dimension,
                                          unsigned&     skeleton_dimension,
                                          float&        from_multiplier, 
                                          float&        to_multiplier,
@@ -101,25 +87,14 @@
     SetTrigger(cOperations, cComplexSize);
 #endif
 
-    unsigned        ambient_dimension;
     unsigned        skeleton_dimension;
     float           from_multiplier, to_multiplier;
     std::string     infilename, outfilename;
-    process_command_line_options(argc, argv, ambient_dimension, skeleton_dimension, from_multiplier, to_multiplier, infilename, outfilename);
+    process_command_line_options(argc, argv, skeleton_dimension, from_multiplier, to_multiplier, infilename, outfilename);
 
     // Read in points
-    std::ifstream in(infilename.c_str());
     PointContainer      points;
-    while(in)
-    {
-        points.push_back(Point());
-        for (unsigned i = 0; i < ambient_dimension; ++i)
-        {
-            DistanceType    x;
-            in >> x;
-            points.back().push_back(x);
-        }
-    }
+    read_points(infilename, points);
     
     // Create output file
     std::ofstream out(outfilename.c_str());
@@ -422,7 +397,6 @@
 
 void        process_command_line_options(int           argc, 
                                          char*         argv[],
-                                         unsigned&     ambient_dimension,
                                          unsigned&     skeleton_dimension,
                                          float&        from_multiplier, 
                                          float&        to_multiplier,
@@ -439,7 +413,6 @@
     po::options_description visible("Allowed options", 100);
     visible.add_options()
         ("help,h",                                                                              "produce help message")
-        ("ambient-dimsnion,a",  po::value<unsigned>(&ambient_dimension)->default_value(3),      "The ambient dimension of the point set")
         ("skeleton-dimsnion,s", po::value<unsigned>(&skeleton_dimension)->default_value(2),     "Dimension of the Rips complex we want to compute")
         ("from,f",              po::value<float>(&from_multiplier)->default_value(4),           "From multiplier for the epsilon (distance to next maxmin point) when computing the Rips complex")
         ("to,t",                po::value<float>(&to_multiplier)->default_value(16),            "To multiplier for the epsilon (distance to next maxmin point) when computing the Rips complex");
--- a/examples/rips/rips-pairwise.cpp	Mon May 11 12:45:49 2009 -0700
+++ b/examples/rips/rips-pairwise.cpp	Thu May 14 13:09:06 2009 -0700
@@ -26,18 +26,18 @@
 typedef         DynamicPersistenceChains<>                              Persistence;
 typedef         PersistenceDiagram<>                                    PDgm;
 
-void            program_options(int argc, char* argv[], std::string& infilename, Dimension& ambient, Dimension& skeleton, DistanceType& max_distance);
+void            program_options(int argc, char* argv[], std::string& infilename, Dimension& skeleton, DistanceType& max_distance);
 
 int main(int argc, char* argv[])
 {
-    Dimension               ambient, skeleton;
+    Dimension               skeleton;
     DistanceType            max_distance;
     std::string             infilename;
 
-    program_options(argc, argv, infilename, ambient, skeleton, max_distance);
+    program_options(argc, argv, infilename, skeleton, max_distance);
 
     PointContainer          points;
-    read_points(infilename, points, ambient);
+    read_points(infilename, points);
 
     PairDistances           distances(points);
     Generator               rips(distances);
@@ -95,7 +95,7 @@
     persistence_timer.check("Persistence timer");
 }
 
-void        program_options(int argc, char* argv[], std::string& infilename, Dimension& ambient, Dimension& skeleton, DistanceType& max_distance)
+void        program_options(int argc, char* argv[], std::string& infilename, Dimension& skeleton, DistanceType& max_distance)
 {
     namespace po = boost::program_options;
 
@@ -106,7 +106,6 @@
     po::options_description visible("Allowed options", 100);
     visible.add_options()
         ("help,h",                                                                                  "produce help message")
-        ("ambient-dimsnion,a",  po::value<Dimension>(&ambient)->default_value(3),                   "The ambient dimension of the point set")
         ("skeleton-dimsnion,s", po::value<Dimension>(&skeleton)->default_value(2),                  "Dimension of the Rips complex we want to compute")
         ("max-distance,m",      po::value<DistanceType>(&max_distance)->default_value(Infinity),    "Maximum value for the Rips complex construction");
 
--- a/examples/rips/rips-zigzag.cpp	Mon May 11 12:45:49 2009 -0700
+++ b/examples/rips/rips-zigzag.cpp	Thu May 14 13:09:06 2009 -0700
@@ -58,12 +58,11 @@
 };
 
 // Forward declarations of auxilliary functions
-void        report_death(std::ofstream& out, Death d, DistanceType epsilon, Dimension skeleton_dimension);
+void        report_death(std::ostream& out, Death d, DistanceType epsilon, Dimension skeleton_dimension);
 void        make_boundary(const Smplx& s, Complex& c, const Zigzag& zz, Boundary& b);
 std::ostream&   operator<<(std::ostream& out, const BirthInfo& bi);
 void        process_command_line_options(int           argc,
                                          char*         argv[],
-                                         unsigned&     ambient_dimension,
                                          unsigned&     skeleton_dimension,
                                          float&        multiplier,
                                          std::string&  infilename,
@@ -85,15 +84,14 @@
     SetTrigger(cOperations, cComplexSize);
 #endif
 
-    unsigned        ambient_dimension;
     unsigned        skeleton_dimension;
     float           multiplier;
     std::string     infilename, outfilename;
-    process_command_line_options(argc, argv, ambient_dimension, skeleton_dimension, multiplier, infilename, outfilename);
+    process_command_line_options(argc, argv, skeleton_dimension, multiplier, infilename, outfilename);
 
     // Read in points
     PointContainer      points;
-    read_points(infilename, points, ambient_dimension);
+    read_points(infilename, points);
     
     // Create output file
     std::ofstream out(outfilename.c_str());
@@ -281,7 +279,7 @@
 
 
             
-void        report_death(std::ofstream& out, Death d, DistanceType epsilon, Dimension skeleton_dimension)
+void        report_death(std::ostream& out, Death d, DistanceType epsilon, Dimension skeleton_dimension)
 {
     if (d && ((d->distance - epsilon) != 0) && (d->dimension < skeleton_dimension))
         out << d->dimension << " " << d->distance << " " << epsilon << std::endl;
@@ -302,7 +300,6 @@
 
 void        process_command_line_options(int           argc, 
                                          char*         argv[],
-                                         unsigned&     ambient_dimension,
                                          unsigned&     skeleton_dimension,
                                          float&        multiplier,
                                          std::string&  infilename,
@@ -318,7 +315,6 @@
     po::options_description visible("Allowed options", 100);
     visible.add_options()
         ("help,h",                                                                              "produce help message")
-        ("ambient-dimsnion,a",  po::value<unsigned>(&ambient_dimension)->default_value(3),      "The ambient dimension of the point set")
         ("skeleton-dimsnion,s", po::value<unsigned>(&skeleton_dimension)->default_value(2),     "Dimension of the Rips complex we want to compute")
         ("multiplier,m",        po::value<float>(&multiplier)->default_value(6),                "Multiplier for the epsilon (distance to next maxmin point) when computing the Rips complex");
 #if LOGGING
--- a/include/geometry/l2distance.h	Mon May 11 12:45:49 2009 -0700
+++ b/include/geometry/l2distance.h	Thu May 14 13:09:06 2009 -0700
@@ -7,6 +7,8 @@
 #include <fstream>
 #include <functional>
 #include <cmath>
+#include <string>
+#include <sstream>
 
 
 typedef     std::vector<double>                                     Point;
@@ -26,19 +28,18 @@
     }
 };
 
-void    read_points(const std::string& infilename, PointContainer& points, Dimension ambient)
+void    read_points(const std::string& infilename, PointContainer& points)
 {
     std::ifstream in(infilename.c_str());
-    while(in)
+    std::string   line;
+    while(std::getline(in, line))
     {
+        if (line[0] == '#') continue;               // comment line in the file
+        std::stringstream linestream(line);
+        double x;
         points.push_back(Point());
-        for (unsigned i = 0; i < ambient; ++i)
-        {
-            double      x;
-            in >> x;
-            if (!in) { points.pop_back(); break; }
+        while (linestream >> x)
             points.back().push_back(x);
-        }
     }
 }