-
Notifications
You must be signed in to change notification settings - Fork 0
/
board_vis.cpp
150 lines (126 loc) · 4.83 KB
/
board_vis.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <fstream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/opencv.hpp"
#define ROI_SZ 100
#define ROI_CANVAS ROI_SZ*0.35
struct Cell
{
int idx;
char val;
char type;
};
void storeCell(std::string word, std::map<int,Cell>& board_colors)
{
Cell cell;
cell.idx = (int)word[word.length()-4]-(int)48;
if(isdigit(word[word.length()-5]))
{
cell.idx = cell.idx + ((int)word[word.length()-5] -(int)48)*10;
cell.type = word[word.length()-7];
}
else cell.type = word[word.length()-6];
cell.val = word[word.length()-2];
board_colors.insert(std::make_pair(cell.idx,cell));
}
// gringo file.lp | clasp 1 >> asp_solver.ou
void load(std::map<int,Cell>& board_colors)
{
std::ifstream ans_set;
ans_set.open( "asp_solver.out", std::ifstream::in);
if(ans_set.is_open())
{
bool next = false;
while(!ans_set.eof())
{
std::string tmp_ln;
getline(ans_set,tmp_ln);
std::stringstream iss(tmp_ln);
std::string word_tmp;
while(iss >> word_tmp)
{
if("Answer:" == word_tmp)
{
next = true;
break;
}
if("SATISFIABLE" == word_tmp || "UNSATISFIABLE" == word_tmp)
{
next = false;
break;
}
if(next) storeCell(word_tmp, board_colors);
else continue;
}
}
}
ans_set.close();
}
void number(cv::Mat img, int _num, cv::Point2f _p, cv::Scalar color, cv::Point2f displacement = cv::Point2i())
{
std::stringstream ss;
ss << _num;
std::string num = ss.str();
float fontScale = 0.5f;
int thickness = 1;
cv::putText(img,num,_p+displacement,cv::FONT_HERSHEY_SCRIPT_SIMPLEX,fontScale,color,thickness);
}
int main()
{
std::map<int,Cell> board_colors;
load(board_colors);
cv::Mat board;
if(board_colors.size() != 0)
{
int board_side = sqrt(board_colors.size());
board = cv::Mat::zeros(board_side*ROI_SZ,board_side*ROI_SZ,CV_8UC3);
std::map<int,Cell>::const_iterator drawer = board_colors.begin();
for(; drawer != board_colors.end(); ++drawer)
{
int r = (drawer->first-1)/board_side;
int c = (drawer->first-1)%board_side;
if(drawer->second.type == 't')
{
for(unsigned int r_roi = r*ROI_SZ+ROI_CANVAS; r_roi < r*ROI_SZ + ROI_SZ-ROI_CANVAS; ++r_roi)
{
for(unsigned int c_roi = c*ROI_SZ+ROI_CANVAS; c_roi < c*ROI_SZ + ROI_SZ-ROI_CANVAS; ++c_roi)
{
if((drawer->second.val) == 'r') board.at<cv::Vec3b>(r_roi,c_roi) = cv::Vec3b(51,51,255);
else if((drawer->second.val) == 'g') board.at<cv::Vec3b>(r_roi,c_roi) = cv::Vec3b(51,255,51);
else if((drawer->second.val) == 'b') board.at<cv::Vec3b>(r_roi,c_roi) = cv::Vec3b(255,153,51);
else if((drawer->second.val) == 'y') board.at<cv::Vec3b>(r_roi,c_roi) = cv::Vec3b(51,255,255);
else if((drawer->second.val) == 'p') board.at<cv::Vec3b>(r_roi,c_roi) = cv::Vec3b(255,51,153);
}
}
}
else if(drawer->second.type == 'e')
{
cv::Scalar color = cv::Scalar::all(0);
if((drawer->second.val) == 'r') color = cv::Scalar(51,51,255);
else if((drawer->second.val) == 'g') color = cv::Scalar(51,255,51);
else if((drawer->second.val) == 'b') color = cv::Scalar(255,153,51);
else if((drawer->second.val) == 'y') color = cv::Scalar(51,255,255);
else if((drawer->second.val) == 'p') color = cv::Scalar(255,51,153);
cv::circle( board, cv::Point2i(c*ROI_SZ+ROI_SZ/2, r*ROI_SZ+ROI_SZ/2), ROI_SZ/3, color, CV_FILLED);
}
number(board, drawer->first, cv::Point2i(c*ROI_SZ+ROI_SZ/2, r*ROI_SZ+ROI_SZ/2), cv::Scalar::all(255), cv::Point2i(-ROI_SZ/2 +10, -ROI_SZ/2 +20));
}
for(unsigned int ln = ROI_SZ; ln < board_side*ROI_SZ; ln+=ROI_SZ)
{
cv::line( board, cv::Point2i(0,ln), cv::Point2i(board_side*ROI_SZ,ln), cv::Scalar::all(255), 2, 1);
cv::line( board, cv::Point2i(ln,0), cv::Point2i(ln,board_side*ROI_SZ), cv::Scalar::all(255), 2, 1);
}
}
else
{
int rows = 280;
int cols = 340;
board = cv::Mat::zeros(rows,cols,CV_8UC3);
cv::putText(board,"UNSATISFIABLE",cv::Point2i(rows/2-30,cols/2-50),CV_FONT_NORMAL,0.5f,cv::Scalar::all(255),1);
}
cv::imshow("Board", board);
cv::waitKey(0);
return 0;
}