1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
#include <iostream>
#include <memory>
#include <set>
#include <cmath>
#include <fstream>
#include <algorithm>
struct Coordinates
{
double _x;
double _y;
double _z;
Coordinates() {}
Coordinates (double x, double y, double z) : _x(x), _y(y), _z(z){};
};
std::istream& operator >> (std::istream& is, Coordinates& rhs)
{
is >> rhs._x >> rhs._y >> rhs._z;
return is;
}
std::ostream& operator << (std::ostream& os, const Coordinates& p)
{
os << "(" << p._x << ", " << p._y << ", " << p._z << ")";
return os;
}
double distance(const Coordinates& lhs, const Coordinates& rhs);
template <typename T, int a, int b, int c>
struct SimplicalComplex;
template <typename T, int a, int b, int c>
struct Point
{
double _P_x;
double _P_y;
double _P_z;
std::unique_ptr<SimplicalComplex<T, a, b, c>> _simplices;
Point(){};
Point (double x, double y, double z) : _P_x(x), _P_y(y), _P_z(z){}
template <typename T1, int a1, int b1, int c1>
struct DistanceCompare
{
T _origin = T(a1, b1, c1);
bool operator()(const Coordinates& lhs, const Coordinates& rhs)const
{
return distance(lhs, _origin) < distance (rhs, _origin);
}
};
std::multiset<Coordinates, DistanceCompare<T, a, b, c>> neighborhood;
bool operator < (const Point<T, a, b, c>& rhs)const
{
if(_P_x < rhs._P_x){return *this;}
else if ((_P_x == rhs._P_x) && (_P_y < rhs._P_y)){return *this;}
else if ((_P_x == rhs._P_x) && (_P_y == rhs._P_y) && (_P_z < rhs._P_y)){return *this;}
}
bool operator == (const Point<T, a, b, c>& rhs)const
{
if((_P_x == rhs._P_x) && (_P_y == rhs._P_y) && (_P_z == rhs._P_y)) {return true;}
else {return false;}
}
void fromCoordinates (const Coordinates& rhs)
{
_P_x = rhs._x; _P_y = rhs._y; _P_z = rhs._z;
}
};
template <typename T, int a, int b, int c>
std::ostream& operator << (std::ostream& os, const Point<T, a, b, c>& rhs)
{
os << "(" << rhs._P_x << ", " << rhs._P_y << ", " << rhs._P_z << ")";
return os;
}
template <typename T, int a, int b, int c>
struct Edge
{
Point<T, a, b, c> _less;
Point<T, a, b, c> _more;
bool operator < (const Edge<T, a, b, c>& rhs) const
{
if(_less < rhs._less){return *this;}
else if((_less == rhs._less) && (_more < rhs._more)){return * this;}
}
};
template <typename T, int a, int b, int c>
struct Triangle
{
Point <T, a, b, c> _low;
Point <T, a, b, c> _mid;
Point <T, a, b, c> _high;
bool operator < (const Triangle<T, a, b, c>& rhs) const
{
if(_low < rhs._low){return *this;}
else if((_low == rhs._low) && (_mid < rhs._mid)){return * this;}
else if ((_low == rhs._low) && (_mid == rhs._mid) && (_high < rhs._high)){return *this;}
}
};
template <typename T, int a, int b, int c>
struct SimplicalComplex
{
std::set<Edge<T, a, b, c>> _edges;
std::set<Triangle<T, a, b, c>> _triangles;
};
int main()
{
using Origin = Point<Coordinates, 0, 0, 0>;//to save some typing
Origin p(0, 0, 0);
Coordinates p1(0, 0, 0);
std::vector<Coordinates> vec;
std::ifstream infile("D:\\input.txt");
while(infile)
{
Coordinates temp;
infile >> temp;//read the file into Coordinates, can convert to type Origin later if reqd (see below)
if(infile)
{
vec.push_back(std::move(temp));
}
}
Origin temp;
std::vector<Origin> neighbors;//you can convert the Coordinates to Point<Coordinates, 0, 0, 0> if you wish ...
for (auto& elem: vec)
{
temp.fromCoordinates(elem);
neighbors.push_back(std::move(temp));
}
for (auto& elem: vec)
{
p.neighborhood.insert(elem);// ... or insert the Coordinates directly into the neighborhood of p
}
for (auto& elem: p.neighborhood)
{
std::cout << elem << '\n';
}
//to access the SimplicalComplex of p, use:
//p._simplices->SimplicalComplex<Point, 0, 0, 0>::_edges //
//p._simplices->SimplicalComplex<Point, 0, 0, 0>::_triangles //
}
double distance(const Coordinates& lhs, const Coordinates& rhs)
{
return std::pow(lhs._x - rhs._x, 2) + std::pow(lhs._y - rhs._y, 2) + std::pow(lhs._z - rhs._z, 2);
}
//tested with the following data file: http://pastie.org/10985735
|