13 #define TRG_SHORT_NAMES
14 #define TRGCDC_SHORT_NAMES
17 #include "trg/trg/Debug.h"
18 #include "trg/cdc/Circle.h"
19 #include "trg/cdc/PeakFinder.h"
20 #include "trg/cdc/HoughPlaneMulti2.h"
21 #include "trg/cdc/HoughTransformationCircle.h"
38 TRGCDCPeakFinder::version(
void)
const
40 return string(
"TRGCDCPeakFinder 5.05");
43 TRGCDCPeakFinder::TRGCDCPeakFinder(
const string& name)
59 unsigned nCells = hp.nX() * hp.nY();
60 static unsigned* candidates =
61 (
unsigned*) malloc(nCells *
sizeof(
unsigned));
63 for (
unsigned j = 0; j < hp.nY(); j++) {
65 if ((hp.name()) ==
"circle hough minus")
66 for (
unsigned i = 0; i < hp.nX(); i++) {
68 const unsigned n = hp.entry(i, j);
69 if (n < threshold)
continue;
70 candidates[nActive] = hp.serialId(i, j);
74 for (
unsigned z = hp.nX(); z > 0 ; --z) {
78 const unsigned n = hp.entry(i, j);
79 if (n < threshold)
continue;
80 candidates[nActive] = hp.serialId(i, j);
86 cout <<
TRGDebug::tab() <<
"Active cells=" << nActive << endl;
89 const unsigned used = nCells;
90 for (
unsigned i = 0; i < nActive; i++) {
91 if (candidates[i] == used)
continue;
92 const unsigned id0 = candidates[i];
96 vector<unsigned>* region =
new vector<unsigned>;
97 region->push_back(id0);
102 for (
unsigned j = 0; j < nActive; j++) {
103 if (candidates[j] == used)
continue;
104 const unsigned id1 = candidates[j];
113 for (
unsigned k = 0; k < unsigned(region->size()); k++) {
114 unsigned id2 = (* region)[k];
118 int difx = abs(
int(x1) -
int(x2));
119 int dify = abs(
int(y1) -
int(y2));
120 if (difx > (
int) hp.nX() / 2) difx = hp.nX() - difx;
121 if (dify > (
int) hp.nY() / 2) dify = hp.nY() - dify;
125 <<
":difx=" << difx <<
",dify=" << dify;
126 if ((difx < 2) && (dify < 3))
127 cout <<
" ... connected" << endl;
132 if ((difx < 2) && (dify < 3)) {
133 region->push_back(id1);
134 candidates[j] = used;
140 hp.setRegion(region);
144 cout <<
TRGDebug::tab() <<
"Regions=" << hp.regions().size() << endl;
158 bool rlrel(vector<unsigned> a, vector<unsigned> b)
162 if (b[1] == 0 && b[3] == 0) {
166 if (a[2] == 0 && a[4] == 0) {
184 bool udrel(vector<unsigned> a, vector<unsigned> b)
188 if (a[1] == 0 && a[2] == 0 && a[3] == 0 && a[4] == 0) {
192 if (a[3] == 0 && a[4] == 0 && b[1] == 0 && b[2] == 0) {
196 if (b[1] == 0 && b[2] == 0 && b[3] == 0 && b[4] == 0) {
213 bool mirel(vector<unsigned> a, vector<unsigned> b)
217 if (a[4] == 0 && b[1] == 0) {
221 if (a[2] == 0 && a[4] == 0) {
225 if (b[1] == 0 && b[3] == 0) {
245 if (a[0] == 1 && a[1] == 0 && a[2] == 0 && a[3] == 0 && a[4] == 0 && a[5] == 0) {center = 0;}
251 if (a[0] == 1 && a[1] == 1 && a[2] == 0 && a[3] == 0 && a[4] == 0 && a[5] == 0) {center = 0;}
252 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 0 && a[4] == 0 && a[5] == 0) {center = 0;}
253 if (a[0] == 1 && a[1] == 0 && a[2] == 0 && a[3] == 1 && a[4] == 0 && a[5] == 0) {center = 0;}
259 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 0 && a[4] == 0 && a[5] == 0) {center = 0;}
260 if (a[0] == 1 && a[1] == 1 && a[2] == 0 && a[3] == 1 && a[4] == 0 && a[5] == 0) {center = 1;}
261 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 1 && a[4] == 0 && a[5] == 0) {center = 2;}
262 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 0 && a[4] == 1 && a[5] == 0) {center = 2;}
263 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 0 && a[4] == 0 && a[5] == 1) {center = 2;}
264 if (a[0] == 1 && a[1] == 0 && a[2] == 0 && a[3] == 1 && a[4] == 1 && a[5] == 0) {center = 3;}
265 if (a[0] == 1 && a[1] == 0 && a[2] == 0 && a[3] == 1 && a[4] == 0 && a[5] == 1) {center = 3;}
271 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 1 && a[4] == 0 && a[5] == 0) {center = 0;}
272 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 0 && a[4] == 1 && a[5] == 0) {center = 2;}
273 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 0 && a[4] == 0 && a[5] == 1) {center = 2;}
274 if (a[0] == 1 && a[1] == 1 && a[2] == 0 && a[3] == 1 && a[4] == 1 && a[5] == 0) {center = 3;}
275 if (a[0] == 1 && a[1] == 1 && a[2] == 0 && a[3] == 1 && a[4] == 0 && a[5] == 1) {center = 3;}
276 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 1 && a[4] == 1 && a[5] == 0) {center = 2;}
277 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 1 && a[4] == 0 && a[5] == 1) {center = 2;}
278 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 0 && a[4] == 1 && a[5] == 1) {center = 2;}
279 if (a[0] == 1 && a[1] == 0 && a[2] == 0 && a[3] == 1 && a[4] == 1 && a[5] == 1) {center = 3;}
285 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 1 && a[4] == 1 && a[5] == 0) {center = 2;}
286 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 1 && a[4] == 0 && a[5] == 1) {center = 2;}
287 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 0 && a[4] == 1 && a[5] == 1) {center = 2;}
288 if (a[0] == 1 && a[1] == 1 && a[2] == 0 && a[3] == 1 && a[4] == 1 && a[5] == 1) {center = 3;}
289 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 1 && a[4] == 1 && a[5] == 1) {center = 2;}
295 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 1 && a[4] == 1 && a[5] == 1) {center = 2;}
307 unsigned short center = 0;
308 for (
unsigned short k = 1; k < a.size(); k++) {
309 if (a[k] == 1) {hits++;}
316 if (a[1] == 1 && a[2] == 0 && a[3] == 0 && a[4] == 0) {center = 1;}
317 if (a[1] == 0 && a[2] == 1 && a[3] == 0 && a[4] == 0) {center = 2;}
318 if (a[1] == 0 && a[2] == 0 && a[3] == 1 && a[4] == 0) {center = 3;}
319 if (a[1] == 0 && a[2] == 0 && a[3] == 0 && a[4] == 1) {center = 4;}
327 if (a[1] == 1 && a[2] == 1 && a[3] == 0 && a[4] == 0) {center = 1;}
328 if (a[1] == 1 && a[2] == 0 && a[3] == 1 && a[4] == 0) {center = 1;}
329 if (a[1] == 1 && a[2] == 0 && a[3] == 0 && a[4] == 1) {center = 1;}
330 if (a[1] == 0 && a[2] == 1 && a[3] == 1 && a[4] == 0) {center = 2;}
331 if (a[1] == 0 && a[2] == 1 && a[3] == 0 && a[4] == 1) {center = 2;}
332 if (a[1] == 0 && a[2] == 0 && a[3] == 1 && a[4] == 1) {center = 3;}
340 if (a[1] == 1 && a[2] == 1 && a[3] == 1 && a[4] == 0) {center = 1;}
341 if (a[1] == 1 && a[2] == 1 && a[3] == 0 && a[4] == 1) {center = 2;}
342 if (a[1] == 1 && a[2] == 0 && a[3] == 1 && a[4] == 1) {center = 3;}
343 if (a[1] == 0 && a[2] == 1 && a[3] == 1 && a[4] == 1) {center = 4;}
352 if (a[1] == 1 && a[2] == 1 && a[3] == 1 && a[4] == 1) {center = 1;}
361 const unsigned threshold,
362 const bool centerIsPeak,
363 vector<unsigned>& peakSerialIds)
const
369 <<
",plane name=[" << hp.name() <<
"]" << endl;
375 const vector<vector<unsigned> *>&
regions = hp.regions();
376 for (
unsigned i = 0; i < (unsigned)
regions.size(); i++) {
379 cout <<
TRGDebug::tab() <<
"region " << i <<
" contents" << endl;
382 const vector<unsigned>& r = *
regions[i];
383 unsigned minX = hp.nX();
385 unsigned minY = hp.nY();
387 for (
unsigned j = 0; j < (unsigned) r.size(); j++) {
388 const unsigned s = r[j];
392 if (x < minX) minX = x;
393 if (x > maxX) maxX = x;
394 if (y < minY) minY = y;
395 if (y > maxY) maxY = y;
400 const unsigned cX = minX + (maxX - minX) / 2;
401 const unsigned cY = minY + (maxY - minY) / 2;
404 unsigned ncX = hp.nX() * hp.nY();
407 cout <<
TRGDebug::tab() <<
"center of region:x=" << cX <<
",y="
410 if (! centerIsPeak) {
413 cout <<
TRGDebug::tab() <<
"Searching a cell closest to the "
414 <<
"region center" << endl;
417 float minDiff2 = float(hp.nX() * hp.nX() + hp.nY() * hp.nY());
418 for (
unsigned j = 0; j < (unsigned) r.size(); j++) {
419 const unsigned s = r[j];
424 const float diff2 = (float(x) - float(cX)) *
425 (
float(x) - float(cX))
426 + (
float(y) - float(cY)) *
427 (
float(y) - float(cY));
429 if (diff2 < minDiff2) {
436 <<
":diff2=" << diff2 << endl;
442 for (
unsigned j = 0; j < (unsigned) r.size(); j++) {
443 const unsigned s = r[j];
444 const float entry = hp.entry(s);
457 const unsigned serialId = hp.serialId(ncX, ncY);
458 peakSerialIds.push_back(serialId);
461 cout <<
TRGDebug::tab() <<
"region " << i <<
" final center:x="
462 << ncX <<
",y=" << ncY << endl
464 << hp.position(ncX, ncY).x() <<
",y="
465 << hp.position(ncX, ncY).y() << endl;
470 cout <<
TRGDebug::tab() << peakSerialIds.size() <<
" peak(s)"
471 <<
" found in total" << endl;
479 const unsigned threshold,
480 vector<vector<unsigned>>& peaks)
const
483 const string sn =
"Peak Finding";
488 <<
",plane name=[" << hp.name() <<
"]" << endl;
500 const unsigned threshold,
501 vector<vector<unsigned>>& peak_xy)
const
503 const string sn =
"p1p2";
506 unsigned nCells = hp.nX() * hp.nY();
507 unsigned nX2 = hp.nX() / 2;
508 unsigned nY2 = hp.nY() / 2;
511 static unsigned* candidates = (
unsigned*) malloc(nCells *
sizeof(
unsigned));
512 unsigned nActive = 0;
513 for (
unsigned j = 0; j < hp.nY(); ++j) {
515 if ((hp.name()) ==
"circle hough minus") {
516 for (
unsigned i = 0; i < hp.nX(); ++i) {
518 const unsigned n = hp.entry(i, j);
519 if (n < threshold)
continue;
520 candidates[nActive] = hp.serialId(i, j);
524 for (
unsigned i = 0; i < hp.nX(); ++i) {
526 const unsigned n = hp.entry(hp.nX() - i - 1, j);
527 if (n < threshold)
continue;
528 candidates[nActive] = hp.serialId(hp.nX() - i - 1, j);
535 vector<vector<unsigned>> p1m;
536 unsigned short no = 0;
541 for (
unsigned n = 0; n < nY2; n++) {
542 for (
unsigned m = 0; m < nX2; m++) {
550 for (
unsigned j = 0; j < 2; j++) {
552 for (
unsigned k = 0; k < 2; k++) {
555 if ((hp.name()) ==
"circle hough plus") {
556 xx = hp.nX() - xx - 1;
561 unsigned short t = 0;
562 for (
unsigned i = 0; i < nActive; i++) {
563 unsigned id1 = candidates[i];
567 if (xx == x1 && yy == y1) {
599 vector<unsigned> p0(5, 0);
600 vector<vector<unsigned>> op2;
603 for (
unsigned short i = 0; i < p1m.size(); i++) {
604 unsigned short j = p1m[i][0];
605 unsigned short a = 0;
614 if ((j % nX2) == 1) {
620 for (
unsigned k = 0; k < p1m.size(); k++) {
621 if (a == p1m[k][0]) {
624 if (!
rlrel(p1m[k], p1m[i])) {
642 for (
unsigned k = 0; k < p1m.size(); k++) {
643 if (a == p1m[k][0]) {
646 if (!
udrel(p1m[k], p1m[i])) {
662 if ((j % nX2) == 1) {
668 for (
unsigned k = 0; k < p1m.size(); k++) {
669 if (a == p1m[k][0]) {
672 if (!
mirel(p1m[k], p1m[i])) {
696 vector<unsigned> p2v;
697 for (
unsigned ip2 = 0; ip2 < 3; ++ip2) {
698 for (
unsigned jp2 = 0; jp2 < 2; ++jp2) {
699 p2v.push_back(j + jp2 + ip2 * nX2);
701 if ((j % nX2) == 0) {
702 p2v[ip2 * 3 + 1] -= nX2;
712 for (
unsigned short imp2 = 0; imp2 < p2v.size(); imp2++) {
713 unsigned short p2v_i = p2v[imp2];
715 for (
unsigned short jmp2 = 0; jmp2 < p1m.size(); jmp2++) {
716 unsigned short p1m_no = p1m[jmp2][0];
718 if (p2v_i != p1m_no) {
719 if (jmp2 == (p1m.size() - 1)) {
725 op2.push_back(p1m[jmp2]);
734 vector<vector<unsigned>> final_op2;
735 vector<unsigned> p2_state;
737 final_op2.push_back(op2[0]);
738 p2_state.push_back(1);
740 if (
rlrel(op2[0], op2[1])) {
741 final_op2.push_back(op2[1]);
742 p2_state.push_back(1);
744 final_op2.push_back(p0);
745 p2_state.push_back(0);
748 if (
udrel(op2[0], op2[2])) {
749 final_op2.push_back(op2[2]);
750 p2_state.push_back(1);
752 final_op2.push_back(p0);
753 p2_state.push_back(0);
756 if (
mirel(op2[0], op2[3]) ||
udrel(op2[1], op2[3]) ||
rlrel(op2[2], op2[3])) {
757 final_op2.push_back(op2[3]);
758 p2_state.push_back(1);
760 final_op2.push_back(p0);
761 p2_state.push_back(0);
764 if (
udrel(op2[2], op2[4])) {
765 final_op2.push_back(op2[4]);
766 p2_state.push_back(1);
768 final_op2.push_back(p0);
769 p2_state.push_back(0);
772 if (
mirel(op2[2], op2[5]) ||
udrel(op2[3], op2[5]) ||
rlrel(op2[4], op2[5])) {
773 final_op2.push_back(op2[5]);
774 p2_state.push_back(1);
776 final_op2.push_back(p0);
777 p2_state.push_back(0);
786 unsigned short fcpi = 0;
787 unsigned short fcpn = 0;
788 unsigned short fcpx = 0;
789 unsigned short fcpxs = 0;
790 unsigned short fcpy = 0;
791 unsigned short fcpys = 0;
799 fcpn = op2[
FindCP1(p2_state)][0];
807 fcpx = ((fcpn - 1) % nX2) * 2 + fcpxs;
809 if ((hp.name()) ==
"circle hough plus") {
810 fcpx = hp.nX() - fcpx - 1;
817 fcpy = fcpy + ((fcpn - 1) / nX2) * 2 + fcpys;
823 peak_xy.push_back(p);
837 cout <<
TRGDebug::tab() <<
"total peaks=" << peak_xy.size() << endl;
static std::string tab(void)
returns tab spaces.
void regions(TRGCDCHoughPlane &hp, const unsigned threshold) const
Makes regions.
virtual ~TRGCDCPeakFinder()
Destructor.
bool rlrel(vector< unsigned > a, vector< unsigned > b)
...rlrel...
unsigned short FindCP1(vector< unsigned > a)
...Find center Pattern1 from Pattern 2
bool udrel(vector< unsigned > a, vector< unsigned > b)
...udrel...
static void enterStage(const std::string &stageName)
Declare that you enter new stage.
void findPeaksTrasan(TRGCDCHoughPlaneMulti2 &hp, const unsigned threshold, const bool centerIsPeak, std::vector< unsigned > &peakSerialIds) const
do peak finding. This is a copy from "trasan".
unsigned FindP1C(vector< unsigned > a)
...Pattern1 Center...
static int level(void)
returns the debug level.
static void leaveStage(const std::string &stageName)
Declare that you leave a stage.
void findPeaks(const TRGCDCHoughPlaneMulti2 &hp, const unsigned threshold, std::vector< std::vector< unsigned >> &peaks) const
do peak finding. Kaiyu's version using p1p2Methode.
bool mirel(vector< unsigned > a, vector< unsigned > b)
...mirel...
void p1p2Methode(const TRGCDCHoughPlane &hp, const unsigned threshold, std::vector< std::vector< unsigned >> &peaks) const
Kaiyu's logic. Finds peaks from nested patterns.
Abstract base class for different kinds of events.