00001 #include "StBiTree.h"
00002 #include <iostream>
00003 #include <cmath>
00004 #include "StDraw3D.h"
00005 #include "TLine.h"
00006 #include "TRandom.h"
00007 using namespace std;
00008
00009
00010 float line( float x, float A, float B, float C)
00011 {
00012 float y = abs(B)>0.0001 ? -(A*x + C)/B : 1;
00013 cerr << __FUNCTION__ << " x ="<< x << " y=" << y << endl;
00014 return y;
00015 }
00016 float line( float x, const vector <float> &v)
00017 {
00018 return line(x,v[0],v[1],v[2]);
00019 }
00020
00021 namespace {
00022 void plotplane(StDraw3D &dsp, const vector< float> plane, const vector < float > &point )
00023 {
00024
00025 }
00026 }
00027 void testclass() {
00028
00029 StDraw3D *dsp = new StDraw3D(0);
00030
00031 typedef StBiTree< vector<float> > Node3d;
00032 TRandom r;
00033 #ifdef test3d
00034 Node3d a3;
00035 cout << "Empty:" << a3.IsEmpty() << endl;
00036 vector<float> b(3);
00037 for (int i=0; i<10; ++i) {
00038 b[0] =r.Rndm(); b[1]=r.Rndm(); b[2]=r.Rndm();
00039 a3.push_back(b);
00040 dsp->Point(b[0],b[1],b[2],kWhite,7,1);
00041
00042 }
00043
00044 b[0]= 0.5 ; b[1]= 0.5; b[2]=0.5;
00045 dsp->Point(b[0],b[1],b[2],kVtx);
00046 Node3d &l = *(a3.find(b));
00047 dsp->Point(l.Data()[0],l.Data()[1],l.Data()[2],kVtx);
00048
00049 Node3d *next = l.Parent();
00050 int nCounter = 0;
00051 while (next){
00052 Node3d *thisParent = next->Parent();
00053 if (thisParent) {
00054 Node3d *uncle = thisParent->Branch(next->WhereAmI() == Node3d::kLeft ? Node3d::kRight : Node3d::kLeft);
00055 Node3d &cousin = *uncle->find(b);
00056 dsp->Point(cousin.Data()[0],cousin.Data()[1],cousin.Data()[2],kUsedHit);
00057 dsp->Line(b[0],b[1],b[2],cousin.Data()[0],cousin.Data()[1],cousin.Data()[2],kBlue,0,3);
00058 dsp->Line(l.Data()[0],l.Data()[1],l.Data()[2],cousin.Data()[0],cousin.Data()[1],cousin.Data()[2],kRed,0,3);
00059 nCounter++;
00060 cout << nCounter << ". x =" << cousin.Data()[0]
00061 if (cousin.Data().size() >= 2 ) << "; y =" << cousin.Data()[1]
00062 if (cousin.Data().size() >= 3 ) cout << "; z =" << cousin.Data()[2]
00063 coud << endl;
00064 }
00065 next= thisParent;
00066 }
00067 #else
00068 Node3d a2;
00069 vector<float> b(2);
00070 #if 0
00071 b[0] = 0; b[1]= 0; a2.push_back(b); dsp->Point(b[0],b[1],0,kWhite,7,1);
00072 b[0] = -1; b[1]=-1; a2.push_back(b); dsp->Point(b[0],b[1],0,kWhite,7,1);
00073 b[0] =-0.5; b[1]=-1; a2.push_back(b); dsp->Point(b[0],b[1],0,kWhite,7,1);
00074 b[0] =+0.5; b[1]=-1; a2.push_back(b); dsp->Point(b[0],b[1],0,kWhite,7,1);
00075 b[0] =+1.0; b[1]=-1; a2.push_back(b); dsp->Point(b[0],b[1],0,kWhite,7,1);
00076 a2.Print();
00077 a2.PrintData();
00078 #else
00079 for (int i=0; i<25; ++i) {
00080 b[0] =r.Rndm(); b[1]=r.Rndm();
00081 Node3d *p = a2.push_back(b)->Parent();
00082 dsp->Point(b[0],b[1],0,kYellow,3,1);
00083 if ( p )
00084 {
00085 float x = b[0];
00086 vector<float> pl = p->Data();
00087 cerr << " x0=" << x-0.3 << " y0=" << line(x-0.3,pl)
00088 << " x1=" << x+0.3 << " y1=" << line(x+0.3,pl)
00089 << endl;
00090 TObject *view1 = dsp->Line(x+1,line(x+1,pl),0.0,x-1,line(x-1,pl),0.0,kRed);
00091
00092 vector<float> connection = p->Left()->Data();
00093 connection.push_back(0);
00094 connection.push_back( p->Right()->Data()[0]);
00095 connection.push_back( p->Right()->Data()[1]);
00096 connection.push_back(0);
00097 TObject *view2 = dsp->Line(connection,kBlue);
00098 dsp->Update(true);
00099
00100
00101 }
00102 }
00103 #endif
00104 dsp->Update(true);
00105 dsp->SetDrawOption("{view:all}");
00106
00107 b[0]= 0.8 ; b[1]= 0.8;
00108 dsp->Point(b[0],b[1],0,kVtx);
00109 Node3d &l = *(a2.find(b));
00110 dsp->Point(l.Data()[0],l.Data()[1],0,kVtx);
00111 cout << " Depth = " << l.Depth() << endl;
00112 b = l.Data();
00113 Node3d *next = l.Parent();
00114 int nCounter = 0;
00115 while (next){
00116 Node3d *thisParent = next->Parent();
00117 if (thisParent) {
00118 Node3d *uncle = thisParent->Branch(next->WhereAmI() == Node3d::kLeft ? Node3d::kRight : Node3d::kLeft);
00119 Node3d &cousin = *uncle->find(b);
00120 dsp->Point(cousin.Data()[0],cousin.Data()[1],0,kBlue,4,6);
00121 dsp->Point(cousin.Data()[0],cousin.Data()[1],0,kBlue,3,2);
00122 dsp->Point(cousin.Data()[0],cousin.Data()[1],0.01,kBlue,3,2);
00123 dsp->Point(cousin.Data()[0],cousin.Data()[1],-0.01,kBlue,3,2);
00124 dsp->Update(true);
00125
00126 nCounter++;
00127 cout << nCounter << ". x =" << cousin.Data()[0]
00128 << "; y =" << cousin.Data()[1]
00129 << endl;
00130 }
00131 next= thisParent;
00132 }
00133 #endif
00134
00135
00136
00137
00138
00139
00140
00141 }