Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JGetSmallestDistance3D.cc File Reference

Example program to find smallest distance between two points. More...

#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#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"

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Detailed Description

Example program to find smallest distance between two points.

Author
mdejong

Definition in file JGetSmallestDistance3D.cc.

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 25 of file JGetSmallestDistance3D.cc.

26 {
27  using namespace std;
28 
29  string inputFile;
30  string outputFile;
31  int numberOfPoints;
32  UInt_t seed;
33  int debug;
34 
35  try {
36 
37  JParser<> zap("Example program to find smallest distance between two points.");
38 
39  zap['f'] = make_field(inputFile) = "";
40  zap['o'] = make_field(outputFile) = "";
41  zap['n'] = make_field(numberOfPoints) = 0;
42  zap['S'] = make_field(seed) = 0;
43  zap['d'] = make_field(debug) = 0;
44 
45  zap(argc, argv);
46  }
47  catch(const exception &error) {
48  FATAL(error.what() << endl);
49  }
50 
51  gRandom->SetSeed(seed);
52 
53  using namespace JPP;
54 
55 
56  vector<JVector3D> buffer;
57 
58  typedef vector<JVector3D>::const_iterator const_iterator;
59 
60 
61  if (inputFile != "") {
62 
63  ifstream in(inputFile.c_str());
64 
65  for (double x, y, z; in >> x >> y >> z; ) {
66  buffer.push_back(JVector3D(x,y,z));
67  }
68 
69  in.close();
70 
71  } else if (numberOfPoints > 1) {
72 
73  NOTICE("Seed: " << gRandom->GetSeed() << endl);
74 
75  for (int i = 0; i != numberOfPoints; ++i) {
76 
77  buffer.push_back(JVector3D(gRandom->Uniform(-1.0, +1.0),
78  gRandom->Uniform(-1.0, +1.0),
79  gRandom->Uniform(-1.0, +1.0)));
80  }
81 
82  if (outputFile != "") {
83 
84  ofstream out(outputFile.c_str(), ios::out);
85 
86  for (const_iterator i = buffer.begin(); i != buffer.end(); ++i)
87  out << setw(7) << i->getX() << ' '
88  << setw(7) << i->getX() << ' '
89  << setw(7) << i->getZ() << endl;
90 
91  out.close();
92  }
93  }
94 
95  if (buffer.size() < 2) {
96  FATAL("Not enough points." << endl);
97  }
98 
99  for (const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
100  DEBUG(i->getX() << ' ' << i->getY() << ' ' << i->getZ() << endl);
101  }
102 
103 
104  {
105  JTimer timer("classic");
106 
107  timer.start();
108 
109  double dmin = numeric_limits<double>::max();
110 
111  for (const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
112  for (const_iterator j = i; ++j != buffer.end(); ) {
113 
114  const double d = i->getDistance(*j);
115 
116  if (d < dmin) {
117  dmin = d;
118  }
119  }
120  }
121 
122  timer.stop();
123 
124  cout << "Minimal distance " << SCIENTIFIC(12,5) << dmin << endl;
125  timer.print(cout);
126  }
127 
128 
129  JTimer timer("O(n log(n))");
130 
131  timer.start();
132 
133  const double dmin = getSmallestDistance3D(buffer.begin(), buffer.end());
134 
135  timer.stop();
136 
137  cout << "Minimal distance " << SCIENTIFIC(12,5) << dmin << endl;
138  timer.print(cout);
139 }
Utility class to parse command line options.
Definition: JParser.hh:1500
static const JSmallestDistance3D getSmallestDistance3D
Function object for smallest distance determination.
string outputFile
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
#define NOTICE(A)
Definition: JMessage.hh:64
int debug
debug level
Definition: JSirene.cc:63
#define FATAL(A)
Definition: JMessage.hh:67
then JMuonMCEvt f $INPUT_FILE o $INTERMEDIATE_FILE d
Definition: JMuonPath.sh:45
int numberOfPoints
Definition: JResultPDF.cc:22
int j
Definition: JPolint.hh:666
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:38
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:484
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62