examples/rips/l2distance.h
author Dmitriy Morozov <dmitriy@mrzv.org>
Tue, 24 Mar 2009 15:17:39 -0700
branchdev
changeset 118 c4e25fb4082c
parent 116 5095771715ab
permissions -rw-r--r--
Added consistency zigzag

#ifndef __L2_DISTANCE_H__
#define __L2_DISTANCE_H__

#include <utilities/types.h>

#include <vector>
#include <fstream>
#include <functional>
#include <cmath>


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): dim1=%d, dim2=%d", p1.size(), p2.size());
        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);
    }
};

void    read_points(const std::string& infilename, PointContainer& points, Dimension ambient)
{
    std::ifstream in(infilename.c_str());
    while(in)
    {
        points.push_back(Point());
        for (unsigned i = 0; i < ambient; ++i)
        {
            double      x;
            in >> x;
            if (!in) { points.pop_back(); break; }
            points.back().push_back(x);
        }
    }
}

#endif // __L2_DISTANCE_H__