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

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

#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include "TROOT.h"
#include "TRandom3.h"
#include "TH2D.h"
#include "TGraph.h"
#include "TMarker.h"
#include "TCanvas.h"
#include "TApplication.h"
#include "JGeometry2D/JVector2D.hh"
#include "JGeometry2D/JGeometry2DToolkit.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 JGetSmallestDistance2D.cc.

Function Documentation

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

Definition at line 31 of file JGetSmallestDistance2D.cc.

32 {
33  using namespace std;
34 
35  string inputFile;
36  string outputFile;
37  int numberOfPoints;
38  UInt_t seed;
39  int debug;
40 
41  try {
42 
43  JParser<> zap("Example program to find smallest distance between two points.");
44 
45  zap['f'] = make_field(inputFile) = "";
46  zap['o'] = make_field(outputFile) = "";
47  zap['n'] = make_field(numberOfPoints) = 0;
48  zap['S'] = make_field(seed) = 0;
49  zap['d'] = make_field(debug) = 0;
50 
51  zap(argc, argv);
52  }
53  catch(const exception &error) {
54  FATAL(error.what() << endl);
55  }
56 
57  gRandom->SetSeed(seed);
58 
59  using namespace JPP;
60 
61 
62  vector<JVector2D> buffer;
63 
64  typedef vector<JVector2D>::const_iterator const_iterator;
65 
66 
67  if (inputFile != "") {
68 
69  ifstream in(inputFile.c_str());
70 
71  for (double x, y; in >> x >> y; ) {
72  buffer.push_back(JVector2D(x,y));
73  }
74 
75  in.close();
76 
77  } else if (numberOfPoints > 1) {
78 
79  NOTICE("Seed: " << gRandom->GetSeed() << endl);
80 
81  for (int i = 0; i != numberOfPoints; ++i) {
82 
83  buffer.push_back(JVector2D(gRandom->Uniform(-1.0, +1.0),
84  gRandom->Uniform(-1.0, +1.0)));
85  }
86 
87  if (outputFile != "") {
88 
89  ofstream out(outputFile.c_str(), ios::out);
90 
91  for (const_iterator i = buffer.begin(); i != buffer.end(); ++i)
92  out << setw(7) << i->getX() << ' '
93  << setw(7) << i->getY() << endl;
94 
95  out.close();
96  }
97  }
98 
99  if (buffer.size() < 2) {
100  FATAL("Not enough points." << endl);
101  }
102 
103  for (const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
104  DEBUG(i->getX() << ' ' << i->getY() << endl);
105  }
106 
107 
108  {
109  JTimer timer("classic");
110 
111  timer.start();
112 
113  double dmin = numeric_limits<double>::max();
114 
115  for (const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
116  for (const_iterator j = i; ++j != buffer.end(); ) {
117 
118  const double d = i->getDistance(*j);
119 
120  if (d < dmin) {
121  dmin = d;
122  }
123  }
124  }
125 
126  timer.stop();
127 
128  cout << "Minimal distance " << SCIENTIFIC(12,5) << dmin << endl;
129  timer.print(cout);
130  }
131 
132 
133  JTimer timer("O(n log(n))");
134 
135  timer.start();
136 
137  const double dmin = getSmallestDistance2D(buffer.begin(), buffer.end());
138 
139  timer.stop();
140 
141  cout << "Minimal distance " << SCIENTIFIC(12,5) << dmin << endl;
142  timer.print(cout);
143 
144 
145  pair<const_iterator, const_iterator> result = JSmallestDistance2D::getPair(buffer.begin(), buffer.end(), dmin);
146 
147  cout << "pair "
148  << "(" << FIXED(7,5) << result.first ->getX() << "," << FIXED(7,5) << result.first ->getY() << ")" << ' '
149  << "(" << FIXED(7,5) << result.second->getX() << "," << FIXED(7,5) << result.second->getY() << ")" << endl;
150  cout << "Distance " << SCIENTIFIC(12,5) << result.first->getDistance(*result.second) << endl;
151 
152 
153  TApplication* tp = new TApplication("user", NULL, NULL);
154 
155  TCanvas cv("cv", "", 400, 400);
156 
157  cv.SetFillStyle(4000);
158  cv.SetFillColor(0);
159 
160  cv.Divide(1, 1);
161  cv.cd(1);
162 
163 
164  const Int_t MAX_BUFFER_SIZE = buffer.size() + 1;
165 
166  Double_t x[MAX_BUFFER_SIZE];
167  Double_t y[MAX_BUFFER_SIZE];
168 
169  int N = 0;
170 
171  for (const_iterator i = buffer.begin(); i != buffer.end(); ++i, ++N) {
172  x[N] = i->getX();
173  y[N] = i->getY();
174  }
175 
176 
177  Double_t xmin = -1.1;
178  Double_t xmax = +1.1;
179  Double_t ymin = -1.1;
180  Double_t ymax = +1.1;
181 
182  TH2D h2("h2", "", 1000, xmin, xmax, 1000, ymin, ymax);
183 
184  h2.SetStats(kFALSE);
185  h2.Draw();
186 
187 
188  TGraph g(N, x, y);
189 
190  g.SetMarkerStyle(20);
191  g.SetMarkerColor(kBlack);
192  g.SetMarkerSize(0.7);
193  g.Draw("P");
194 
195  TMarker m1(result.first ->getX(), result.first ->getY(), 20);
196  TMarker m2(result.second->getX(), result.second->getY(), 20);
197 
198  m1.SetMarkerColor(kRed);
199  m2.SetMarkerColor(kRed);
200 
201  m1.SetMarkerSize(0.7);
202  m2.SetMarkerSize(0.7);
203 
204  m1.Draw();
205  m2.Draw();
206 
207  cv.Update();
208 
209  tp->Run();
210 }
const double xmax
Definition: JQuadrature.cc:24
Utility class to parse command line options.
Definition: JParser.hh:1514
static const JSmallestDistance2D getSmallestDistance2D
Function object for smallest distance determination.
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
string outputFile
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
#define NOTICE(A)
Definition: JMessage.hh:64
#define FATAL(A)
Definition: JMessage.hh:67
const double xmin
Definition: JQuadrature.cc:23
then usage $script< input file >[option[primary[working directory]]] nWhere option can be N
Definition: JMuonPostfit.sh:40
then JMuonMCEvt f $INPUT_FILE o $INTERMEDIATE_FILE d
Definition: JMuonPath.sh:47
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 JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:48
int numberOfPoints
Definition: JResultPDF.cc:22
int j
Definition: JPolint.hh:792
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:486
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62