diff --git a/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp b/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp index 6c76477..4cf06d0 100644 --- a/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp +++ b/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp @@ -70,6 +70,11 @@ float DestructionPower::defaultDestructionPower(int i) //gets next highest valued abstraction layer down from current one (if first, get highest) int DestructionPower::getNextAbstractionLayer(coor newCoordinate, int currentAbstractionLayer) { + if(++currentAbstractionLayer>=DESTRUCTION_COUNT) + return -1; + else + return currentAbstractionLayer; + float currentPower = 1; int nextLayer=-1; float nextLayerPower=0; diff --git a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp index cc9c21c..eb100a8 100644 --- a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp +++ b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp @@ -21,11 +21,14 @@ bool AbstractionLayer_1::PreProcessing(coor mySize, const vector* partAr for(int i = 0; i < mySize.row*mySize.col; i++) { unsigned char poempel = analyse.getTabs(i); + PSum+=PoempelSum(poempel); //preprocess correct check for (int j=0;j<4;j++) { ref_partArray[iterator]->m_a1.m_connections=poempel; + ref_partArray[iterator]->m_a3.SideLength=analyse.getPoempelPosition(i); //do abstraction Layer 3 check shift(poempel,1); + ref_partArray[iterator]->m_a3.shift(j);//rotate information iterator++; } } @@ -428,7 +431,9 @@ bool analyseParts::getImages(){ return false; } mask.setCorners(corners); - mask.setTabs(analyseContour(corners,contours[0])); + vector tmpPoempelPosition={0,0,0,0,0,0,0,0};//TODO somehow make this code section bearable + mask.setTabs(analyseContour(corners,contours[0],tmpPoempelPosition)); + mask.setPoempelPosition(tmpPoempelPosition); mask.setLens(analyseLens(lens, corners)); mask.setMidpoint(calcMidpoint(corners)); masks.push_back(mask); @@ -633,7 +638,8 @@ vector analyseParts::findCorners(vector contour, Point center){ if(DISPLAY) imshow("draw",drawing); return corners; } -unsigned char analyseParts::analyseContour(vector corners, vector contour) { +unsigned char analyseParts::analyseContour(vector corners, vector contour,vector& PoempelPosition){ + vector contour_right; vector contour_top; vector contour_left; @@ -1066,6 +1072,12 @@ unsigned char analyseParts::analyseContour(vector corners, vector max_dist_idx = i; } } + //saves length between Corners and Poempel right + Point diff = corners[2] - contour_right_new[max_dist_idx]; + PoempelPosition[2] = cv::sqrt(diff.x*diff.x + diff.y*diff.y); + diff = corners[0] - contour_right_new[max_dist_idx]; + PoempelPosition[3] = cv::sqrt(diff.x*diff.x + diff.y*diff.y); + /*-------------------------------------*/ unsigned char tabs = 0; int poembel_threshold = 15; @@ -1090,6 +1102,13 @@ unsigned char analyseParts::analyseContour(vector corners, vector max_dist_idx = i; } } + + //saves length between Corners and Poempel + diff = corners[3] - contour_top_new[max_dist_idx]; + PoempelPosition[0] = cv::sqrt(diff.x*diff.x + diff.y*diff.y); + diff = corners[2] - contour_top_new[max_dist_idx]; + PoempelPosition[1] = cv::sqrt(diff.x*diff.x + diff.y*diff.y); + /*-------------------------------------*/ if (ref_top - contour_top_new[max_dist_idx].y <= -poembel_threshold) { @@ -1113,6 +1132,12 @@ unsigned char analyseParts::analyseContour(vector corners, vector max_dist_idx = i; } } + + //saves length between Corners and Poempel + diff = corners[1] - contour_left_new[max_dist_idx]; + PoempelPosition[6] = cv::sqrt(diff.x*diff.x + diff.y*diff.y); + diff = corners[3] - contour_left_new[max_dist_idx]; + PoempelPosition[7] = cv::sqrt(diff.x*diff.x + diff.y*diff.y); /*-------------------------------------*/ if (ref_left - contour_left_new[max_dist_idx].x <= -poembel_threshold) { tabs |= (1 << LEFT); @@ -1124,7 +1149,7 @@ unsigned char analyseParts::analyseContour(vector corners, vector tabs |= (0 << LEFT); } - /*---------Suche Poempel Oben---------------*/ + /*---------Suche Poempel Unten---------------*/ max_dist = 0; dist = 0; max_dist_idx = 0; @@ -1135,6 +1160,10 @@ unsigned char analyseParts::analyseContour(vector corners, vector max_dist_idx = i; } } + diff = corners[0] - contour_bottom_new[max_dist_idx]; + PoempelPosition[4] = cv::sqrt(diff.x*diff.x + diff.y*diff.y); + diff = corners[1] - contour_bottom_new[max_dist_idx]; + PoempelPosition[5] = cv::sqrt(diff.x*diff.x + diff.y*diff.y); /*-------------------------------------*/ if (ref_bottom - contour_bottom_new[max_dist_idx].y <= -poembel_threshold) { tabs |= (2 << BOTTOM); diff --git a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.h b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.h index 32e3ac8..368cb63 100644 --- a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.h +++ b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.h @@ -82,11 +82,13 @@ public: void setLens(vector l ){len = l;} Point getMidpoint(){return midpoint;} void setMidpoint(Point m ){midpoint = m;} - + vector getPoempelPosition(){return PoempelPosition;} + void setPoempelPosition(vector& newPP){PoempelPosition=newPP;} vector getCorners(){return corners;} private: Mat image; + vector PoempelPosition; vector corners; vector> contour; vector hierarchy; @@ -105,12 +107,13 @@ public: vector getHierarchy(int i){if(i>= nr_parts)return masks[nr_parts-1].getHierarchy(); else return masks[i].getHierarchy();} unsigned char getTabs(int i){if(i>= nr_parts)return masks[nr_parts-1].getTabs(); else return masks[i].getTabs();} vector getLen(int i ){return masks[i].getLen();} + vector getPoempelPosition(int i){return masks[i].getPoempelPosition();} vector analyseLens(vector, vector); Point calcMidpoint(vector); Point getMidpoint(int i){return masks[i].getMidpoint();} Point findCenter(Mat); vector findCorners(vector,Point); - unsigned char analyseContour(vector, vector); + unsigned char analyseContour(vector, vector,vector&); Mat makeBorder(Mat&); Mat readImages(int); Mat morphDilateErode(Mat&); diff --git a/Source/functions/AbstractionLayers/Layer3_PoempelPosition/AbstractionLayer_PoempelPosition.cpp b/Source/functions/AbstractionLayers/Layer3_PoempelPosition/AbstractionLayer_PoempelPosition.cpp index 4b18a87..dd2daff 100644 --- a/Source/functions/AbstractionLayers/Layer3_PoempelPosition/AbstractionLayer_PoempelPosition.cpp +++ b/Source/functions/AbstractionLayers/Layer3_PoempelPosition/AbstractionLayer_PoempelPosition.cpp @@ -18,12 +18,7 @@ bool AbstractionLayer_PoempelPosition::EvaluateQuality (const coor constraintCoo for(int i = 0;im_a3.SideLength); - if(value > 0.8)//TODO find threshold - { - qVector[i].first=value; - continue; - } - qVector[i].first=0; + qVector[i].first=value; } } @@ -37,14 +32,54 @@ bool AbstractionLayer_PoempelPosition::RemoveConstraintOnPosition(const coor con m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].SideLength={0,0,0,0,0,0,0,0}; } -float AbstractionLayer_PoempelPosition::PlaceOfPartGood(coor myCoor, vector myPart) +float AbstractionLayer_PoempelPosition::PlaceOfPartGood(coor myCoor, vector myPart) { - //sets coordinates to correct position for layer - + vector comparePosition={0,0,0,0,0,0,0,0}; + float sum=0; //create negativePart, watch out for edges + if(myCoor.col>0 && m_constraintMatrix[myCoor.col-1][myCoor.row].SideLength[3])//get right data from left + { + comparePosition[6]=m_constraintMatrix[myCoor.col-1][myCoor.row].SideLength[3]; + comparePosition[7]=m_constraintMatrix[myCoor.col-1][myCoor.row].SideLength[2]; + //sum absolute difference + sum+=abs(comparePosition[6]-myPart[6]); + sum+=abs(comparePosition[7]-myPart[7]); + } + if(myCoor.row>0 && m_constraintMatrix[myCoor.col][myCoor.row-1].SideLength[5])//get bot data from top + { + comparePosition[0]=m_constraintMatrix[myCoor.col][myCoor.row-1].SideLength[5]; + comparePosition[1]=m_constraintMatrix[myCoor.col][myCoor.row-1].SideLength[4]; + //sum absolute difference + sum+=abs(comparePosition[0]-myPart[0]); + sum+=abs(comparePosition[1]-myPart[1]); - //check vector against negative part + } + if(myCoor.col100) + return 0; + if(sum==0) + return 1; + sum/=100; + return 1-sum; + //check vector against negative part, use sad //return of well it fits within threshold } diff --git a/Source/functions/AbstractionLayers/Layer3_PoempelPosition/AbstractionLayer_PoempelPosition.h b/Source/functions/AbstractionLayers/Layer3_PoempelPosition/AbstractionLayer_PoempelPosition.h index 8db2906..1050e53 100644 --- a/Source/functions/AbstractionLayers/Layer3_PoempelPosition/AbstractionLayer_PoempelPosition.h +++ b/Source/functions/AbstractionLayers/Layer3_PoempelPosition/AbstractionLayer_PoempelPosition.h @@ -16,7 +16,7 @@ public: bool SetConstraintOnPosition( coor constraintCoordinate, AbstractionLayer_PoempelPosition_Properties constraint)final; bool RemoveConstraintOnPosition( coor constraintCoordinate)final; - float PlaceOfPartGood(coor myCoor, vector myPart); + float PlaceOfPartGood(coor myCoor, vector myPart); private: }; \ No newline at end of file diff --git a/Source/functions/AbstractionLayers/Layer3_PoempelPosition/AbstractionLayer_PoempelPosition_Properties.h b/Source/functions/AbstractionLayers/Layer3_PoempelPosition/AbstractionLayer_PoempelPosition_Properties.h index 5664c5a..8f5a8f6 100644 --- a/Source/functions/AbstractionLayers/Layer3_PoempelPosition/AbstractionLayer_PoempelPosition_Properties.h +++ b/Source/functions/AbstractionLayers/Layer3_PoempelPosition/AbstractionLayer_PoempelPosition_Properties.h @@ -10,8 +10,9 @@ public: void shift(int i); private: - vector SideLength; + vector SideLength; friend class AbstractionLayer_PoempelPosition; + friend class AbstractionLayer_1; }; diff --git a/Source/functions/solve/structure.cpp b/Source/functions/solve/structure.cpp index 1070f3a..84db395 100755 --- a/Source/functions/solve/structure.cpp +++ b/Source/functions/solve/structure.cpp @@ -72,6 +72,7 @@ coor calculateNextCoor(vector& log, Puzzle& puzzleMat) void solve(vector& log,Puzzle& puzzleMat) { log.back().abstractionLevel = puzzleMat.dp.getNextAbstractionLayer(log.back().myCoor,log.back().abstractionLevel); //sets in abstractionLevel + cout << "abs: " << log.back().abstractionLevel << endl; //status(log,p_Box,puzzleMat); //TODO!! Add more layers here switch(log.back().abstractionLevel) @@ -80,7 +81,7 @@ void solve(vector& log,Puzzle& puzzleMat) puzzleMat.a1.EvaluateQuality(log.back().myCoor, log.back().PieceCollector); break; case 1://histogram - return; + puzzleMat.a3.EvaluateQuality(log.back().myCoor,log.back().PieceCollector); break; case -1://random setsolution(log,puzzleMat); @@ -109,7 +110,7 @@ void setsolution(vector& log, Puzzle& puzzleMat) //tell log entry that it is set log.back().Set(); puzzleMat.setConstraints(log.back().myCoor,log.back().PieceCollector.begin()->second); - //cout << "set:" << log.back().myCoor.col << "," << log.back().myCoor.row << endl; + cout << "set:" << log.back().myCoor.col << "," << log.back().myCoor.row << endl; //cout << "ID: " << log.back().PieceCollector[0].second->GetPartID() << endl; } @@ -131,10 +132,10 @@ bool backtrack(vector& log, Puzzle& puzzleMat) //remove similar in log - Part myPart = *log.back().PieceCollector[0].second;//tmpsaves bad part - log.back().PieceCollector.erase(log.back().PieceCollector.begin());//removes bad part from log - puzzleMat.removeSimilar(log.back().PieceCollector,myPart); //removes all pieces from log that are similar to bad part - //TODO remove this when further layers are added!!! + Part myPart = *log.back().PieceCollector[0].second;//tmpsaves bad part + log.back().PieceCollector.erase(log.back().PieceCollector.begin());//removes bad part from log + puzzleMat.removeSimilar(log.back().PieceCollector,myPart); //removes all pieces from log that are similar to bad part + //TODO reprogram similar removal to allow multilayer tracking if(log.back().PieceCollector.size()) // this checks if 'removeSimilar' has cleared entire LogElement { if(log.back().PieceCollector.size()==1) @@ -298,12 +299,13 @@ void CalculateNewCombinedQuality(vector& log, qualityVector& qVector, } else { - for (unsigned int i = 0; i < cqVector.size(); i++) { + for (unsigned int i = 0; i < cqVector.size();) { for (unsigned int j = 0; j < qVector.size(); j++) { // search same PuzzlePart of qualityVector and combinedQualityVector + removePart=true; if (cqVector.at(i).second->GetPartID() == qVector.at(j).second->GetPartID() && cqVector.at(i).second->GetNumOfRotations() == qVector.at(j).second->GetNumOfRotations()) { // sum Quality of PieceCollector (qualityVector) to combinedQualityVector - cqVector.at(j).first += qVector.at(i).first; + cqVector.at(i).first += qVector.at(j).first; countSummarizedVectors++; removePart=false; break; // skip remaining for loop => save time! @@ -318,7 +320,7 @@ void CalculateNewCombinedQuality(vector& log, qualityVector& qVector, { swap(cqVector.at(i), cqVector.back()); cqVector.pop_back(); - } + } else i++; }