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"
40 return string(
"TRGCDCPeakFinder 5.05");
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);
73 for (
unsigned z = hp.nX(); z > 0 ; --z) {
77 const unsigned n = hp.entry(i, j);
78 if (n < threshold)
continue;
79 candidates[nActive] = hp.serialId(i, j);
85 cout <<
TRGDebug::tab() <<
"Active cells=" << nActive << endl;
88 const unsigned used = nCells;
89 for (
unsigned i = 0; i < nActive; i++) {
90 if (candidates[i] == used)
continue;
91 const unsigned id0 = candidates[i];
95 vector<unsigned>* region =
new vector<unsigned>;
96 region->push_back(id0);
101 for (
unsigned j = 0; j < nActive; j++) {
102 if (candidates[j] == used)
continue;
103 const unsigned id1 = candidates[j];
112 for (
unsigned k = 0; k < unsigned(region->size()); k++) {
113 unsigned id2 = (* region)[k];
117 int difx = abs(
int(x1) -
int(x2));
118 int dify = abs(
int(y1) -
int(y2));
119 if (difx > (
int) hp.nX() / 2) difx = hp.nX() - difx;
120 if (dify > (
int) hp.nY() / 2) dify = hp.nY() - dify;
124 <<
":difx=" << difx <<
",dify=" << dify;
125 if ((difx < 2) && (dify < 3))
126 cout <<
" ... connected" << endl;
131 if ((difx < 2) && (dify < 3)) {
132 region->push_back(id1);
133 candidates[j] = used;
139 hp.setRegion(region);
143 cout <<
TRGDebug::tab() <<
"Regions=" << hp.regions().size() << endl;
157 bool rlrel(vector<unsigned> a, vector<unsigned> b)
161 if (b[1] == 0 && b[3] == 0) {
165 if (a[2] == 0 && a[4] == 0) {
183 bool udrel(vector<unsigned> a, vector<unsigned> b)
187 if (a[1] == 0 && a[2] == 0 && a[3] == 0 && a[4] == 0) {
191 if (a[3] == 0 && a[4] == 0 && b[1] == 0 && b[2] == 0) {
195 if (b[1] == 0 && b[2] == 0 && b[3] == 0 && b[4] == 0) {
212 bool mirel(vector<unsigned> a, vector<unsigned> b)
216 if (a[4] == 0 && b[1] == 0) {
220 if (a[2] == 0 && a[4] == 0) {
224 if (b[1] == 0 && b[3] == 0) {
244 if (a[0] == 1 && a[1] == 0 && a[2] == 0 && a[3] == 0 && a[4] == 0 && a[5] == 0) {center = 0;}
250 if (a[0] == 1 && a[1] == 1 && a[2] == 0 && a[3] == 0 && a[4] == 0 && a[5] == 0) {center = 0;}
251 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 0 && a[4] == 0 && a[5] == 0) {center = 0;}
252 if (a[0] == 1 && a[1] == 0 && a[2] == 0 && a[3] == 1 && a[4] == 0 && a[5] == 0) {center = 0;}
258 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 0 && a[4] == 0 && a[5] == 0) {center = 0;}
259 if (a[0] == 1 && a[1] == 1 && a[2] == 0 && a[3] == 1 && a[4] == 0 && a[5] == 0) {center = 1;}
260 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 1 && a[4] == 0 && a[5] == 0) {center = 2;}
261 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 0 && a[4] == 1 && a[5] == 0) {center = 2;}
262 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 0 && a[4] == 0 && a[5] == 1) {center = 2;}
263 if (a[0] == 1 && a[1] == 0 && a[2] == 0 && a[3] == 1 && a[4] == 1 && a[5] == 0) {center = 3;}
264 if (a[0] == 1 && a[1] == 0 && a[2] == 0 && a[3] == 1 && a[4] == 0 && a[5] == 1) {center = 3;}
270 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 1 && a[4] == 0 && a[5] == 0) {center = 0;}
271 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 0 && a[4] == 1 && a[5] == 0) {center = 2;}
272 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 0 && a[4] == 0 && a[5] == 1) {center = 2;}
273 if (a[0] == 1 && a[1] == 1 && a[2] == 0 && a[3] == 1 && a[4] == 1 && a[5] == 0) {center = 3;}
274 if (a[0] == 1 && a[1] == 1 && a[2] == 0 && a[3] == 1 && a[4] == 0 && a[5] == 1) {center = 3;}
275 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 1 && a[4] == 1 && a[5] == 0) {center = 2;}
276 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 1 && a[4] == 0 && a[5] == 1) {center = 2;}
277 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 0 && a[4] == 1 && a[5] == 1) {center = 2;}
278 if (a[0] == 1 && a[1] == 0 && a[2] == 0 && a[3] == 1 && a[4] == 1 && a[5] == 1) {center = 3;}
284 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 1 && a[4] == 1 && a[5] == 0) {center = 2;}
285 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 1 && a[4] == 0 && a[5] == 1) {center = 2;}
286 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 0 && a[4] == 1 && a[5] == 1) {center = 2;}
287 if (a[0] == 1 && a[1] == 1 && a[2] == 0 && a[3] == 1 && a[4] == 1 && a[5] == 1) {center = 3;}
288 if (a[0] == 1 && a[1] == 0 && a[2] == 1 && a[3] == 1 && a[4] == 1 && a[5] == 1) {center = 2;}
294 if (a[0] == 1 && a[1] == 1 && a[2] == 1 && a[3] == 1 && a[4] == 1 && a[5] == 1) {center = 2;}
306 unsigned short center = 0;
307 for (
unsigned short k = 1; k < a.size(); k++) {
308 if (a[k] == 1) {hits++;}
315 if (a[1] == 1 && a[2] == 0 && a[3] == 0 && a[4] == 0) {center = 1;}
316 if (a[1] == 0 && a[2] == 1 && a[3] == 0 && a[4] == 0) {center = 2;}
317 if (a[1] == 0 && a[2] == 0 && a[3] == 1 && a[4] == 0) {center = 3;}
318 if (a[1] == 0 && a[2] == 0 && a[3] == 0 && a[4] == 1) {center = 4;}
326 if (a[1] == 1 && a[2] == 1 && a[3] == 0 && a[4] == 0) {center = 1;}
327 if (a[1] == 1 && a[2] == 0 && a[3] == 1 && a[4] == 0) {center = 1;}
328 if (a[1] == 1 && a[2] == 0 && a[3] == 0 && a[4] == 1) {center = 1;}
329 if (a[1] == 0 && a[2] == 1 && a[3] == 1 && a[4] == 0) {center = 2;}
330 if (a[1] == 0 && a[2] == 1 && a[3] == 0 && a[4] == 1) {center = 2;}
331 if (a[1] == 0 && a[2] == 0 && a[3] == 1 && a[4] == 1) {center = 3;}
339 if (a[1] == 1 && a[2] == 1 && a[3] == 1 && a[4] == 0) {center = 1;}
340 if (a[1] == 1 && a[2] == 1 && a[3] == 0 && a[4] == 1) {center = 2;}
341 if (a[1] == 1 && a[2] == 0 && a[3] == 1 && a[4] == 1) {center = 3;}
342 if (a[1] == 0 && a[2] == 1 && a[3] == 1 && a[4] == 1) {center = 4;}
351 if (a[1] == 1 && a[2] == 1 && a[3] == 1 && a[4] == 1) {center = 1;}
360 const unsigned threshold,
361 const bool centerIsPeak,
362 vector<unsigned>& peakSerialIds)
const
368 <<
",plane name=[" << hp.name() <<
"]" << endl;
374 const vector<vector<unsigned> *>&
regions = hp.regions();
375 for (
unsigned i = 0; i < (unsigned)
regions.size(); i++) {
378 cout <<
TRGDebug::tab() <<
"region " << i <<
" contents" << endl;
381 const vector<unsigned>& r = *
regions[i];
382 unsigned minX = hp.nX();
384 unsigned minY = hp.nY();
386 for (
unsigned j = 0; j < (unsigned) r.size(); j++) {
387 const unsigned s = r[j];
391 if (x < minX) minX = x;
392 if (x > maxX) maxX = x;
393 if (y < minY) minY = y;
394 if (y > maxY) maxY = y;
399 const unsigned cX = minX + (maxX - minX) / 2;
400 const unsigned cY = minY + (maxY - minY) / 2;
403 unsigned ncX = hp.nX() * hp.nY();
406 cout <<
TRGDebug::tab() <<
"center of region:x=" << cX <<
",y="
409 if (! centerIsPeak) {
412 cout <<
TRGDebug::tab() <<
"Searching a cell closest to the "
413 <<
"region center" << endl;
416 float minDiff2 = float(hp.nX() * hp.nX() + hp.nY() * hp.nY());
417 for (
unsigned j = 0; j < (unsigned) r.size(); j++) {
418 const unsigned s = r[j];
423 const float diff2 = (float(x) - float(cX)) *
424 (
float(x) - float(cX))
425 + (
float(y) - float(cY)) *
426 (
float(y) - float(cY));
428 if (diff2 < minDiff2) {
435 <<
":diff2=" << diff2 << endl;
441 for (
unsigned j = 0; j < (unsigned) r.size(); j++) {
442 const unsigned s = r[j];
443 const float entry = hp.entry(s);
456 const unsigned serialId = hp.serialId(ncX, ncY);
457 peakSerialIds.push_back(serialId);
460 cout <<
TRGDebug::tab() <<
"region " << i <<
" final center:x="
461 << ncX <<
",y=" << ncY << endl
463 << hp.position(ncX, ncY).x() <<
",y="
464 << hp.position(ncX, ncY).y() << endl;
469 cout <<
TRGDebug::tab() << peakSerialIds.size() <<
" peak(s)"
470 <<
" found in total" << endl;
478 const unsigned threshold,
479 vector<vector<unsigned>>& peaks)
const
482 const string sn =
"Peak Finding";
487 <<
",plane name=[" << hp.name() <<
"]" << endl;
499 const unsigned threshold,
500 vector<vector<unsigned>>& peak_xy)
const
502 const string sn =
"p1p2";
505 unsigned nCells = hp.nX() * hp.nY();
506 unsigned nX2 = hp.nX() / 2;
507 unsigned nY2 = hp.nY() / 2;
510 static unsigned* candidates = (
unsigned*) malloc(nCells *
sizeof(
unsigned));
511 unsigned nActive = 0;
512 for (
unsigned j = 0; j < hp.nY(); ++j) {
514 if ((hp.name()) ==
"circle hough minus") {
515 for (
unsigned i = 0; i < hp.nX(); ++i) {
517 const unsigned n = hp.entry(i, j);
518 if (n < threshold)
continue;
519 candidates[nActive] = hp.serialId(i, j);
523 for (
unsigned i = 0; i < hp.nX(); ++i) {
525 const unsigned n = hp.entry(hp.nX() - i - 1, j);
526 if (n < threshold)
continue;
527 candidates[nActive] = hp.serialId(hp.nX() - i - 1, j);
534 vector<vector<unsigned>> p1m;
535 unsigned short no = 0;
540 for (
unsigned n = 0; n < nY2; n++) {
541 for (
unsigned m = 0; m < nX2; m++) {
549 for (
unsigned j = 0; j < 2; j++) {
551 for (
unsigned k = 0; k < 2; k++) {
554 if ((hp.name()) ==
"circle hough plus") {
555 xx = hp.nX() - xx - 1;
560 unsigned short t = 0;
561 for (
unsigned i = 0; i < nActive; i++) {
562 unsigned id1 = candidates[i];
566 if (xx == x1 && yy == y1) {
598 vector<unsigned> p0(5, 0);
599 vector<vector<unsigned>> op2;
602 for (
unsigned short i = 0; i < p1m.size(); i++) {
603 unsigned short j = p1m[i][0];
604 unsigned short a = 0;
613 if ((j % nX2) == 1) {
619 for (
unsigned k = 0; k < p1m.size(); k++) {
620 if (a == p1m[k][0]) {
623 if (!
rlrel(p1m[k], p1m[i])) {
641 for (
unsigned k = 0; k < p1m.size(); k++) {
642 if (a == p1m[k][0]) {
645 if (!
udrel(p1m[k], p1m[i])) {
661 if ((j % nX2) == 1) {
667 for (
unsigned k = 0; k < p1m.size(); k++) {
668 if (a == p1m[k][0]) {
671 if (!
mirel(p1m[k], p1m[i])) {
695 vector<unsigned> p2v;
696 for (
unsigned ip2 = 0; ip2 < 3; ++ip2) {
697 for (
unsigned jp2 = 0; jp2 < 2; ++jp2) {
698 p2v.push_back(j + jp2 + ip2 * nX2);
700 if ((j % nX2) == 0) {
701 p2v[ip2 * 3 + 1] -= nX2;
711 for (
unsigned short imp2 = 0; imp2 < p2v.size(); imp2++) {
712 unsigned short p2v_i = p2v[imp2];
714 for (
unsigned short jmp2 = 0; jmp2 < p1m.size(); jmp2++) {
715 unsigned short p1m_no = p1m[jmp2][0];
717 if (p2v_i != p1m_no) {
718 if (jmp2 == (p1m.size() - 1)) {
724 op2.push_back(p1m[jmp2]);
733 vector<vector<unsigned>> final_op2;
734 vector<unsigned> p2_state;
736 final_op2.push_back(op2[0]);
737 p2_state.push_back(1);
739 if (
rlrel(op2[0], op2[1])) {
740 final_op2.push_back(op2[1]);
741 p2_state.push_back(1);
743 final_op2.push_back(p0);
744 p2_state.push_back(0);
747 if (
udrel(op2[0], op2[2])) {
748 final_op2.push_back(op2[2]);
749 p2_state.push_back(1);
751 final_op2.push_back(p0);
752 p2_state.push_back(0);
755 if (
mirel(op2[0], op2[3]) ||
udrel(op2[1], op2[3]) ||
rlrel(op2[2], op2[3])) {
756 final_op2.push_back(op2[3]);
757 p2_state.push_back(1);
759 final_op2.push_back(p0);
760 p2_state.push_back(0);
763 if (
udrel(op2[2], op2[4])) {
764 final_op2.push_back(op2[4]);
765 p2_state.push_back(1);
767 final_op2.push_back(p0);
768 p2_state.push_back(0);
771 if (
mirel(op2[2], op2[5]) ||
udrel(op2[3], op2[5]) ||
rlrel(op2[4], op2[5])) {
772 final_op2.push_back(op2[5]);
773 p2_state.push_back(1);
775 final_op2.push_back(p0);
776 p2_state.push_back(0);
785 unsigned short fcpi = 0;
786 unsigned short fcpn = 0;
787 unsigned short fcpx = 0;
788 unsigned short fcpxs = 0;
789 unsigned short fcpy = 0;
790 unsigned short fcpys = 0;
798 fcpn = op2[
FindCP1(p2_state)][0];
806 fcpx = ((fcpn - 1) % nX2) * 2 + fcpxs;
808 if ((hp.name()) ==
"circle hough plus") {
809 fcpx = hp.nX() - fcpx - 1;
816 fcpy = fcpy + ((fcpn - 1) / nX2) * 2 + fcpys;
822 peak_xy.push_back(p);
836 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.
bool rlrel(vector< unsigned > a, vector< unsigned > b)
...rlrel...
virtual ~TRGCDCPeakFinder()
Destructor.
static void enterStage(const std::string &stageName)
Declare that you enter new 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 udrel(vector< unsigned > a, vector< unsigned > b)
...udrel...
TRGCDCPeakFinder(const std::string &name)
Contructor.
void p1p2Methode(const TRGCDCHoughPlane &hp, const unsigned threshold, std::vector< std::vector< unsigned > > &peaks) const
Kaiyu's logic. Finds peaks from nested patterns.
bool mirel(vector< unsigned > a, vector< unsigned > b)
...mirel...
unsigned short FindCP1(vector< unsigned > a)
...Find center Pattern1 from Pattern 2
void findPeaksTrasan(TRGCDCHoughPlaneMulti2 &hp, const unsigned threshold, const bool centerIsPeak, std::vector< unsigned > &peakSerialIds) const
do peak finding. This is a copy from "trasan".
static int level(void)
returns the debug level.
static void leaveStage(const std::string &stageName)
Declare that you leave a stage.
std::string version(void) const
returns version.
unsigned FindP1C(vector< unsigned > a)
...Pattern1 Center...
Abstract base class for different kinds of events.