#include #include #include #include #include #include "TRandom3.h" #include "JGeometry3D/JVector3D.hh" #include "JGeometry3D/JGeometry3DToolkit.hh" #include "Jeep/JPrint.hh" #include "Jeep/JTimer.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" /** * \file * * Example program to find smallest distance between two points. * \author mdejong */ int main(int argc, char* argv[]) { using namespace std; string inputFile; string outputFile; int numberOfPoints; UInt_t seed; int debug; try { JParser<> zap("Example program to find smallest distance between two points."); zap['f'] = make_field(inputFile) = ""; zap['o'] = make_field(outputFile) = ""; zap['n'] = make_field(numberOfPoints) = 0; zap['S'] = make_field(seed) = 0; zap['d'] = make_field(debug) = 0; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } gRandom->SetSeed(seed); using namespace JPP; vector buffer; typedef vector::const_iterator const_iterator; if (inputFile != "") { ifstream in(inputFile.c_str()); for (double x, y, z; in >> x >> y >> z; ) { buffer.push_back(JVector3D(x,y,z)); } in.close(); } else if (numberOfPoints > 1) { NOTICE("Seed: " << gRandom->GetSeed() << endl); for (int i = 0; i != numberOfPoints; ++i) { buffer.push_back(JVector3D(gRandom->Uniform(-1.0, +1.0), gRandom->Uniform(-1.0, +1.0), gRandom->Uniform(-1.0, +1.0))); } if (outputFile != "") { ofstream out(outputFile.c_str(), ios::out); for (const_iterator i = buffer.begin(); i != buffer.end(); ++i) out << setw(7) << i->getX() << ' ' << setw(7) << i->getX() << ' ' << setw(7) << i->getZ() << endl; out.close(); } } if (buffer.size() < 2) { FATAL("Not enough points." << endl); } for (const_iterator i = buffer.begin(); i != buffer.end(); ++i) { DEBUG(i->getX() << ' ' << i->getY() << ' ' << i->getZ() << endl); } { JTimer timer("classic"); timer.start(); double dmin = numeric_limits::max(); for (const_iterator i = buffer.begin(); i != buffer.end(); ++i) { for (const_iterator j = i; ++j != buffer.end(); ) { const double d = i->getDistance(*j); if (d < dmin) { dmin = d; } } } timer.stop(); cout << "Minimal distance " << SCIENTIFIC(12,5) << dmin << endl; timer.print(cout); } JTimer timer("O(n log(n))"); timer.start(); const double dmin = getSmallestDistance3D(buffer.begin(), buffer.end()); timer.stop(); cout << "Minimal distance " << SCIENTIFIC(12,5) << dmin << endl; timer.print(cout); }