14 #define TRG_SHORT_NAMES
15 #define TRGCDC_SHORT_NAMES
18 #include "trg/trg/Debug.h"
19 #include "trg/cdc/Circle.h"
20 #include "trg/cdc/PeakFinder.h"
21 #include "trg/cdc/HoughPlaneMulti2.h"
22 #include "trg/cdc/HoughTransformationCircle.h"
39 TRGCDCPeakFinder::version(
void)
const
41 return string(
"TRGCDCPeakFinder 5.05");
44 TRGCDCPeakFinder::TRGCDCPeakFinder(
const string& name)
60 unsigned nCells = hp.nX() * hp.nY();
61 static unsigned* candidates =
62 (
unsigned*) malloc(nCells *
sizeof(
unsigned));
64 for (
unsigned j = 0; j < hp.nY(); j++) {
66 if ((hp.name()) ==
"circle hough minus")
67 for (
unsigned i = 0; i < hp.nX(); i++) {
69 const unsigned n = hp.entry(i, j);
70 if (n < threshold)
continue;
71 candidates[nActive] = hp.serialId(i, j);
75 for (
unsigned z = hp.nX(); z > 0 ; --z) {
79 const unsigned n = hp.entry(i, j);
80 if (n < threshold)
continue;
81 candidates[nActive] = hp.serialId(i, j);
87 cout <<
TRGDebug::tab() <<
"Active cells=" << nActive << endl;
90 const unsigned used = nCells;
91 for (
unsigned i = 0; i < nActive; i++) {
92 if (candidates[i] == used)
continue;
93 const unsigned id0 = candidates[i];
97 vector<unsigned>* region =
new vector<unsigned>;
98 region->push_back(id0);
103 for (
unsigned j = 0; j < nActive; j++) {
104 if (candidates[j] == used)
continue;
105 const unsigned id1 = candidates[j];
114 for (
unsigned k = 0; k < unsigned(region->size()); k++) {
115 unsigned id2 = (* region)[k];
119 int difx = abs(
int(x1) -
int(x2));
120 int dify = abs(
int(y1) -
int(y2));
121 if (difx > (
int) hp.nX() / 2) difx = hp.nX() - difx;
122 if (dify > (
int) hp.nY() / 2) dify = hp.nY() - dify;
126 <<
":difx=" << difx <<
",dify=" << dify;
127 if ((difx < 2) && (dify < 3))
128 cout <<
" ... connected" << endl;
133 if ((difx < 2) && (dify < 3)) {
134 region->push_back(id1);
135 candidates[j] = used;
141 hp.setRegion(region);
145 cout <<
TRGDebug::tab() <<
"Regions=" << hp.regions().size() << endl;
159 bool rlrel(vector<unsigned> a, vector<unsigned> b)
163 if (b[1] == 0 && b[3] == 0) {
167 if (a[2] == 0 && a[4] == 0) {
185 bool udrel(vector<unsigned> a, vector<unsigned> b)
189 if (a[1] == 0 && a[2] == 0 && a[3] == 0 && a[4] == 0) {
193 if (a[3] == 0 && a[4] == 0 && b[1] == 0 && b[2] == 0) {
197 if (b[1] == 0 && b[2] == 0 && b[3] == 0 && b[4] == 0) {
214 bool mirel(vector<unsigned> a, vector<unsigned> b)
218 if (a[4] == 0 && b[1] == 0) {
222 if (a[2] == 0 && a[4] == 0) {
226 if (b[1] == 0 && b[3] == 0) {
246 if (a[0] == 1 && a[1] == 0 && a[2] == 0 && a[3] == 0 && a[4] == 0 && a[5] == 0) {center = 0;}
252 if (a[0] == 1 && a[1] == 1 && a[2] == 0 && a[3] == 0 && a[4] == 0 && a[5] == 0) {center = 0;}
253 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 0 && a[4] == 0 && a[5] == 0) {center = 0;}
254 if (a[0] == 1 && a[1] == 0 && a[2] == 0 && a[3] == 1 && a[4] == 0 && a[5] == 0) {center = 0;}
260 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 0 && a[4] == 0 && a[5] == 0) {center = 0;}
261 if (a[0] == 1 && a[1] == 1 && a[2] == 0 && a[3] == 1 && a[4] == 0 && a[5] == 0) {center = 1;}
262 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 1 && a[4] == 0 && a[5] == 0) {center = 2;}
263 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 0 && a[4] == 1 && a[5] == 0) {center = 2;}
264 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 0 && a[4] == 0 && a[5] == 1) {center = 2;}
265 if (a[0] == 1 && a[1] == 0 && a[2] == 0 && a[3] == 1 && a[4] == 1 && a[5] == 0) {center = 3;}
266 if (a[0] == 1 && a[1] == 0 && a[2] == 0 && a[3] == 1 && a[4] == 0 && a[5] == 1) {center = 3;}
272 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 1 && a[4] == 0 && a[5] == 0) {center = 0;}
273 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 0 && a[4] == 1 && a[5] == 0) {center = 2;}
274 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 0 && a[4] == 0 && a[5] == 1) {center = 2;}
275 if (a[0] == 1 && a[1] == 1 && a[2] == 0 && a[3] == 1 && a[4] == 1 && a[5] == 0) {center = 3;}
276 if (a[0] == 1 && a[1] == 1 && a[2] == 0 && a[3] == 1 && a[4] == 0 && a[5] == 1) {center = 3;}
277 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 1 && a[4] == 1 && a[5] == 0) {center = 2;}
278 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 1 && a[4] == 0 && a[5] == 1) {center = 2;}
279 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 0 && a[4] == 1 && a[5] == 1) {center = 2;}
280 if (a[0] == 1 && a[1] == 0 && a[2] == 0 && a[3] == 1 && a[4] == 1 && a[5] == 1) {center = 3;}
286 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 1 && a[4] == 1 && a[5] == 0) {center = 2;}
287 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 1 && a[4] == 0 && a[5] == 1) {center = 2;}
288 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 0 && a[4] == 1 && a[5] == 1) {center = 2;}
289 if (a[0] == 1 && a[1] == 1 && a[2] == 0 && a[3] == 1 && a[4] == 1 && a[5] == 1) {center = 3;}
290 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 1 && a[4] == 1 && a[5] == 1) {center = 2;}
296 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 1 && a[4] == 1 && a[5] == 1) {center = 2;}
308 unsigned short center = 0;
309 for (
unsigned short k = 1; k < a.size(); k++) {
310 if (a[k] == 1) {hits++;}
317 if (a[1] == 1 && a[2] == 0 && a[3] == 0 && a[4] == 0) {center = 1;}
318 if (a[1] == 0 && a[2] == 1 && a[3] == 0 && a[4] == 0) {center = 2;}
319 if (a[1] == 0 && a[2] == 0 && a[3] == 1 && a[4] == 0) {center = 3;}
320 if (a[1] == 0 && a[2] == 0 && a[3] == 0 && a[4] == 1) {center = 4;}
328 if (a[1] == 1 && a[2] == 1 && a[3] == 0 && a[4] == 0) {center = 1;}
329 if (a[1] == 1 && a[2] == 0 && a[3] == 1 && a[4] == 0) {center = 1;}
330 if (a[1] == 1 && a[2] == 0 && a[3] == 0 && a[4] == 1) {center = 1;}
331 if (a[1] == 0 && a[2] == 1 && a[3] == 1 && a[4] == 0) {center = 2;}
332 if (a[1] == 0 && a[2] == 1 && a[3] == 0 && a[4] == 1) {center = 2;}
333 if (a[1] == 0 && a[2] == 0 && a[3] == 1 && a[4] == 1) {center = 3;}
341 if (a[1] == 1 && a[2] == 1 && a[3] == 1 && a[4] == 0) {center = 1;}
342 if (a[1] == 1 && a[2] == 1 && a[3] == 0 && a[4] == 1) {center = 2;}
343 if (a[1] == 1 && a[2] == 0 && a[3] == 1 && a[4] == 1) {center = 3;}
344 if (a[1] == 0 && a[2] == 1 && a[3] == 1 && a[4] == 1) {center = 4;}
353 if (a[1] == 1 && a[2] == 1 && a[3] == 1 && a[4] == 1) {center = 1;}
362 const unsigned threshold,
363 const bool centerIsPeak,
364 vector<unsigned>& peakSerialIds)
const
370 <<
",plane name=[" << hp.name() <<
"]" << endl;
376 const vector<vector<unsigned> *>&
regions = hp.regions();
377 for (
unsigned i = 0; i < (unsigned)
regions.size(); i++) {
380 cout <<
TRGDebug::tab() <<
"region " << i <<
" contents" << endl;
383 const vector<unsigned>& r = *
regions[i];
384 unsigned minX = hp.nX();
386 unsigned minY = hp.nY();
388 for (
unsigned j = 0; j < (unsigned) r.size(); j++) {
389 const unsigned s = r[j];
393 if (x < minX) minX = x;
394 if (x > maxX) maxX = x;
395 if (y < minY) minY = y;
396 if (y > maxY) maxY = y;
401 const unsigned cX = minX + (maxX - minX) / 2;
402 const unsigned cY = minY + (maxY - minY) / 2;
405 unsigned ncX = hp.nX() * hp.nY();
408 cout <<
TRGDebug::tab() <<
"center of region:x=" << cX <<
",y="
411 if (! centerIsPeak) {
414 cout <<
TRGDebug::tab() <<
"Searching a cell closest to the "
415 <<
"region center" << endl;
418 float minDiff2 = float(hp.nX() * hp.nX() + hp.nY() * hp.nY());
419 for (
unsigned j = 0; j < (unsigned) r.size(); j++) {
420 const unsigned s = r[j];
425 const float diff2 = (float(x) - float(cX)) *
426 (
float(x) - float(cX))
427 + (
float(y) - float(cY)) *
428 (
float(y) - float(cY));
430 if (diff2 < minDiff2) {
437 <<
":diff2=" << diff2 << endl;
443 for (
unsigned j = 0; j < (unsigned) r.size(); j++) {
444 const unsigned s = r[j];
445 const float entry = hp.entry(s);
458 const unsigned serialId = hp.serialId(ncX, ncY);
459 peakSerialIds.push_back(serialId);
462 cout <<
TRGDebug::tab() <<
"region " << i <<
" final center:x="
463 << ncX <<
",y=" << ncY << endl
465 << hp.position(ncX, ncY).x() <<
",y="
466 << hp.position(ncX, ncY).y() << endl;
471 cout <<
TRGDebug::tab() << peakSerialIds.size() <<
" peak(s)"
472 <<
" found in total" << endl;
480 const unsigned threshold,
481 vector<vector<unsigned>>& peaks)
const
484 const string sn =
"Peak Finding";
489 <<
",plane name=[" << hp.name() <<
"]" << endl;
501 const unsigned threshold,
502 vector<vector<unsigned>>& peak_xy)
const
504 const string sn =
"p1p2";
507 unsigned nCells = hp.nX() * hp.nY();
508 unsigned nX2 = hp.nX() / 2;
509 unsigned nY2 = hp.nY() / 2;
512 static unsigned* candidates = (
unsigned*) malloc(nCells *
sizeof(
unsigned));
513 unsigned nActive = 0;
514 for (
unsigned j = 0; j < hp.nY(); ++j) {
516 if ((hp.name()) ==
"circle hough minus") {
517 for (
unsigned i = 0; i < hp.nX(); ++i) {
519 const unsigned n = hp.entry(i, j);
520 if (n < threshold)
continue;
521 candidates[nActive] = hp.serialId(i, j);
525 for (
unsigned i = 0; i < hp.nX(); ++i) {
527 const unsigned n = hp.entry(hp.nX() - i - 1, j);
528 if (n < threshold)
continue;
529 candidates[nActive] = hp.serialId(hp.nX() - i - 1, j);
536 vector<vector<unsigned>> p1m;
537 unsigned short no = 0;
542 for (
unsigned n = 0; n < nY2; n++) {
543 for (
unsigned m = 0; m < nX2; m++) {
551 for (
unsigned j = 0; j < 2; j++) {
553 for (
unsigned k = 0; k < 2; k++) {
556 if ((hp.name()) ==
"circle hough plus") {
557 xx = hp.nX() - xx - 1;
562 unsigned short t = 0;
563 for (
unsigned i = 0; i < nActive; i++) {
564 unsigned id1 = candidates[i];
568 if (xx == x1 && yy == y1) {
600 vector<unsigned> p0(5, 0);
601 vector<vector<unsigned>> op2;
604 for (
unsigned short i = 0; i < p1m.size(); i++) {
605 unsigned short j = p1m[i][0];
606 unsigned short a = 0;
615 if ((j % nX2) == 1) {
621 for (
unsigned k = 0; k < p1m.size(); k++) {
622 if (a == p1m[k][0]) {
625 if (!
rlrel(p1m[k], p1m[i])) {
643 for (
unsigned k = 0; k < p1m.size(); k++) {
644 if (a == p1m[k][0]) {
647 if (!
udrel(p1m[k], p1m[i])) {
664 if ((j % nX2) == 1) {
670 for (
unsigned k = 0; k < p1m.size(); k++) {
671 if (a == p1m[k][0]) {
674 if (!
mirel(p1m[k], p1m[i])) {
698 vector<unsigned> p2v;
699 for (
unsigned ip2 = 0; ip2 < 3; ++ip2) {
700 for (
unsigned jp2 = 0; jp2 < 2; ++jp2) {
701 p2v.push_back(j + jp2 + ip2 * nX2);
703 if ((j % nX2) == 0) {
704 p2v[ip2 * 3 + 1] -= nX2;
714 for (
unsigned short imp2 = 0; imp2 < p2v.size(); imp2++) {
715 unsigned short p2v_i = p2v[imp2];
717 for (
unsigned short jmp2 = 0; jmp2 < p1m.size(); jmp2++) {
718 unsigned short p1m_no = p1m[jmp2][0];
720 if (p2v_i != p1m_no) {
721 if (jmp2 == (p1m.size() - 1)) {
727 op2.push_back(p1m[jmp2]);
736 vector<vector<unsigned>> final_op2;
737 vector<unsigned> p2_state;
739 final_op2.push_back(op2[0]);
740 p2_state.push_back(1);
742 if (
rlrel(op2[0], op2[1])) {
743 final_op2.push_back(op2[1]);
744 p2_state.push_back(1);
746 final_op2.push_back(p0);
747 p2_state.push_back(0);
750 if (
udrel(op2[0], op2[2])) {
751 final_op2.push_back(op2[2]);
752 p2_state.push_back(1);
754 final_op2.push_back(p0);
755 p2_state.push_back(0);
758 if (
mirel(op2[0], op2[3]) ||
udrel(op2[1], op2[3]) ||
rlrel(op2[2], op2[3])) {
759 final_op2.push_back(op2[3]);
760 p2_state.push_back(1);
762 final_op2.push_back(p0);
763 p2_state.push_back(0);
766 if (
udrel(op2[2], op2[4])) {
767 final_op2.push_back(op2[4]);
768 p2_state.push_back(1);
770 final_op2.push_back(p0);
771 p2_state.push_back(0);
774 if (
mirel(op2[2], op2[5]) ||
udrel(op2[3], op2[5]) ||
rlrel(op2[4], op2[5])) {
775 final_op2.push_back(op2[5]);
776 p2_state.push_back(1);
778 final_op2.push_back(p0);
779 p2_state.push_back(0);
788 unsigned short fcpi = 0;
789 unsigned short fcpn = 0;
790 unsigned short fcpx = 0;
791 unsigned short fcpxs = 0;
792 unsigned short fcpy = 0;
793 unsigned short fcpys = 0;
801 fcpn = op2[
FindCP1(p2_state)][0];
809 fcpx = ((fcpn - 1) % nX2) * 2 + fcpxs;
811 if ((hp.name()) ==
"circle hough plus") {
812 fcpx = hp.nX() - fcpx - 1;
819 fcpy = fcpy + ((fcpn - 1) / nX2) * 2 + fcpys;
825 peak_xy.push_back(p);
839 cout <<
TRGDebug::tab() <<
"total peaks=" << peak_xy.size() << endl;