#include "vertix.h" #include #include #include #include "pose_body.h" Polygon workspace; time_gap_t global_time_gap = { .in = 0, .size = 0, .periods = NULL, }; void dump_polygon_to_file(char *filename) { FILE *fp = fopen(filename, "w+"); if (fp == NULL) { printf("failed to open fp\n"); return; } char s[100]; snprintf(s, 100, "rect: ["); fwrite(s, strlen(s), 1, fp); for (int i = 0; i < workspace.numVertix; i++) { if (i != workspace.numVertix - 1) { snprintf(s, 100, "(%f, %f), ", workspace.vertix[i].x, workspace.vertix[i].y); } else { snprintf(s, 100, "(%f, %f)", workspace.vertix[i].x, workspace.vertix[i].y); } fwrite(s, strlen(s), 1, fp); } const char *suffix = "]\n"; fwrite(suffix, strlen(suffix), 1, fp); fclose(fp); } void init_polygon(float *datas, int point_size) { if (point_size == 0) { workspace.numVertix = 0; #ifdef DUMP_POLYGON_INFO char filename[128]; snprintf(filename, 128, "/usr/data/camera/%s/dump_rect.txt", CameraID); dump_polygon_to_file(filename); #endif return; } if (point_size % 2 != 0) { printf("point size error"); exit(0); } workspace.numVertix = point_size / 2; workspace.vertix = (Point *)malloc(sizeof(Point) * point_size); for (int i = 0; i < point_size / 2; i++) { workspace.vertix[i] = Point{ x : datas[i * 2], y : datas[i * 2 + 1], }; } #ifdef DUMP_POLYGON_INFO char filename[128]; snprintf(filename, 128, "/usr/data/camera/%s/dump_rect.txt", CameraID); dump_polygon_to_file(filename); #endif } void dump_box_info(bool is_in, int left, int top, int right, int bottom) { char filename[128]; snprintf(filename, 128, "/usr/data/camera/%s/dump_rect.txt", CameraID); FILE *fp = fopen(filename, "a+"); if (fp == NULL) { printf("failed to open fp\n"); return; } float y = (top + bottom) / 2.0; float x = (left + right) / 2.0; char s[] = {0}; if (is_in) { snprintf(s, 1024, "in: [%d, %d, %d, %d](%f, %f)\n", left, top, right, bottom, x, y); } else { snprintf(s, 1024, "not in: [%d, %d, %d, %d](%f, %f)\n", left, top, right, bottom, x, y); } fwrite(s, strlen(s), 1, fp); fclose(fp); } bool is_point_in_workspace(int left, int top, int right, int bottom) { float y = (top + bottom) / 2.0; float x = (left + right) / 2.0; int n = workspace.numVertix; if (n == 0) { // all area #ifdef DUMP_POLYGON_INFO dump_box_info(true, left, top, right, bottom); #endif printf("all workspace\n"); return true; } int i, j; bool inside = false; for (i = 0, j = n - 1; i < n; j = i++) { if ((workspace.vertix[i].y > y) != (workspace.vertix[j].y > y)) { // this point is between the Point[j] and Point[i] in y axis float deltax = (workspace.vertix[j].x - workspace.vertix[i].x); float deltay = (workspace.vertix[j].y - workspace.vertix[i].y); float ratio = deltax / deltay; if (x < (ratio * (y - workspace.vertix[i].y) + workspace.vertix[i].x)) { inside = !inside; } } } #ifdef DUMP_POLYGON_INFO dump_box_info(inside, left, top, right, bottom); #endif return inside; } // 获取今天的秒数 int get_today_seconds() { time_t current_time; struct tm time_info; int total_seconds; time(¤t_time); localtime_r(¤t_time, &time_info); total_seconds = time_info.tm_hour * 3600 + time_info.tm_min * 60 + time_info.tm_sec; return total_seconds; } int should_do_judge_according_time() { int now = get_today_seconds(); int ok = false; if (global_time_gap.in) { for (int i = 0; i < global_time_gap.size; i++) { if (global_time_gap.periods[i].from <= now && global_time_gap.periods[i].to >= now) { return 1; } } return 0; } else { // not in for (int i = 0; i < global_time_gap.size; i++) { if (global_time_gap.periods[i].from <= now && global_time_gap.periods[i].to >= now) { return 0; } } return 1; } } int get_today_seconds_from_string(const char *str) { char *token; char *rest = (char*)str; int hour = 0, minute = 0, second = 0; char *origin = (char*)malloc(sizeof(char)*(strlen(str)+1)); strncpy(origin, str, strlen(str)); origin[strlen(str)] = '\0'; token = strtok_r(origin, ":", &rest); if (token != NULL) { hour = atoi(token); } else { free(origin); return -1; } token = strtok_r(NULL, ":", &rest); if (token != NULL) { minute = atoi(token); } token = strtok_r(NULL, ":", &rest); if (token != NULL) { second = atoi(token); } free(origin); return hour * 3600 + minute * 60 + second; } time_period_t* get_time_period_from_string(char *str) { char *token; char *rest = str; char *origin = (char*)malloc(sizeof(char)*(strlen(str)+1)); strncpy(origin, str, strlen(str)); origin[strlen(str)] = '\0'; printf("\norigin=%s\n", origin); token = strtok_r(origin, "-", &rest); printf("token1 = %s\n", token); if (token == NULL) { printf("token1 is null\n"); free(origin); return NULL; } printf("start_token = %s\n", token); int start = get_today_seconds_from_string(token); if (start < 0) { free(origin); return NULL; } printf("rest = \"%s\"", rest); if (strlen(rest) == 0) { token = (char*)"23:59:59"; } else { token = rest; } printf("end_token = %s\n", token); int end = get_today_seconds_from_string(token); printf("start = %d, end = %d\n", start, end); free(origin); time_period_t *result = (time_period_t*)malloc(sizeof(time_period_t)); result->from = start; result->to = end; return result; } int get_all_time_period_from_arguments(const char *str) { if (strlen(str) == 0) { return -1; } char in = 1; char *origin = (char*)malloc(sizeof(char)*(strlen(str)+1)); strncpy(origin, str, strlen(str)); origin[strlen(str)] = '\0'; int offset = 0; if (str[0] == '!') { offset = 1; in = 0; } char *rest; char *info; char *start = origin+offset; printf(in ? "in" : "not in "); global_time_gap.in = in; global_time_gap.periods = (time_period_t*) malloc(sizeof(time_period_t)*64); int size = 0; while ((info = strtok_r(start, ",", &rest)) != NULL) { time_period_t* period = get_time_period_from_string(info); if (period == NULL) { printf("failed to parse gap: %s\n", info); return -2; } global_time_gap.periods[size].from = period->from; global_time_gap.periods[size].to = period->to; free(period); size++; if (size >= 64) { printf("too many periods specified: > 64\n"); free(global_time_gap.periods); free(origin); return -3; } start = NULL; } global_time_gap.size = size; free(origin); return 0; }