/** * @brief MapManager::setupRobotManagerThread esegue il setup del thread su cui gira la parte di controllo del robot */ void MapManager::setupRobotManagerThread() { //Creo l'istanza del thread secondario // QThread* robotManagerThread = new QThread(this); robotManagerThread = new QThread(this); // Sposto l'oggetto che controlla il robot nell'altro thread robotManager.moveToThread(robotManagerThread); //Connetto il signal che dichiara l'inizio dell'esecuzione del thread con lo slot che si occupa dell'inizializzazione della classe QObject::connect(robotManagerThread, SIGNAL(started()), &robotManager, SLOT(init())); // Connetto i signal della classe del robot a quelli della GUI, in modo da poterli usare da QML QObject::connect(&robotManager, SIGNAL(pointFound(QVariant,QVariant)), this, SIGNAL(pointFound(QVariant,QVariant))); QObject::connect(&robotManager, SIGNAL(victimFound(QVariant)), this, SIGNAL(victimFound(QVariant))); QObject::connect(&robotManager, SIGNAL(robotTurn(QVariant)), this, SIGNAL(robotTurn(QVariant))); QObject::connect(&robotManager, SIGNAL(generateStep()), this, SIGNAL(generateStep())); // Dato che uso signal che passano paremtri Mat di opencv, devo esplori al meta system delle Qt qRegisterMetaType< cv::Mat >("cv::Mat"); // Connetto i signal che inviano immagini da mostrare ai rispettivi slot QObject::connect(&robotManager, SIGNAL(newCameraImage(cv::Mat)), this, SLOT(printImage(cv::Mat))); QObject::connect(&robotManager, SIGNAL(victimImageFound(cv::Mat)), this, SLOT(printVictimImage(cv::Mat))); // Segnalo di terminare il thread alla chiusura della GUI (anche se non sembra far nulla) QObject::connect(this, SIGNAL(destroyed()), robotManagerThread, SLOT(quit())); // Avvio il thread robotManagerThread->start(); }
void Maze::generate() { // set starting (exit) point int randX = (int)ofRandom(NUM_CELLS_X); int randY = (int)ofRandom(NUM_CELLS_Y); currentCell = cells[randX][randY]; currentCell->visit(); currentCell->mazeExit = true; cellStack.push(currentCell); exitCell = currentCell; finishedGenerating = false; // generate the maze while (!finishedGenerating) { generateStep(); } buildModel(); // create moving balls with paths for (int i=0; i<settings->numberOfBalls; i++) { int sx = (int)ofRandom(60, NUM_CELLS_X-60); int sy = (int)ofRandom(10, 500); ofxSimpleSpline* spline = createSimpleSpline(sx, sy, 100); balls.push_back(new MovingBall(spline, settings)); } }
std::vector<std::unique_ptr<State>> Model::generateParticles( BeliefNode *previousBelief, Action const &action, Observation const &obs, long nParticles) { std::vector<std::unique_ptr<State>> particles; ObservationMapping *obsMap = previousBelief->getMapping()->getActionNode(action)->getMapping(); BeliefNode *childNode = obsMap->getBelief(obs); double startTime = tapir::clock_ms(); double endTime = startTime + 10000.0; // Use rejection sampling to generate the required number of particles. while ((long)particles.size() < nParticles) { if (tapir::clock_ms() >= endTime) { cout << "WARNING: Timeout while replenishing particles" << endl; return particles; } // Sample a state uniformly at random std::unique_ptr<State> state = sampleStateUninformed(); // Now generate a step in the model, and compare the observation to the actual observation. // Note that this comparison is done implicitly via the observation mapping, to ensure // that approximate observations are treated cleanly. StepResult result = generateStep(*state, action); if (obsMap->getBelief(*result.observation) == childNode) { particles.push_back(std::move(result.nextState)); } } return particles; }
char *LSys::generateStep(char *current, int n) { if(n <= 0) return current; int currentlength = strlen(current); int nextlength = 12 * currentlength; char *result = new char[nextlength]; memset(result, '\0', nextlength); char *result_idx = result; int currentlen = strlen(current); //For each character in the string for(int i = 0; i < currentlen; i++) { bool happened = false; //Try and apply all rules for(int j = 0; j < m_rules->length(); j++) { m_rules->at(j)->apply(current[i], &result_idx, happened); } //If no rule happened, copy the current character if(!happened) { *result_idx = current[i]; result_idx += 1; } } //Recur delete[] current; return generateStep(result, n-1); }
char *LSys::generate(int n, char *initial) { char *input = new char[strlen(initial) + 1]; memset(input, '\0', strlen(initial) + 1); memcpy(input, initial, strlen(initial)); return generateStep(input, n); }
int main() { Parms_t input; /* input.width = 10; input.height = 10; input.fullscreen = 0;//SDL_WINDOW_FULLSCREEN; input.windowH = 200; input.windowW = 200; input.animationDelay = 0; input.framesDrop = 1; input.startPoint.x = 1; input.startPoint.y = 1; input.exitPoint.x = 7; input.exitPoint.y = 7; */ Parms_t last; bool quit = false; Data_t d; RenderData_t rd; srand(clock()); //инициализация SDL2 SDL_Init(SDL_INIT_VIDEO); input = getInput(1, ALL, input); SDL_Window *window = SDL_CreateWindow("Labytinth", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, input.windowW, input.windowH, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | input.fullscreen | SDL_WINDOW_BORDERLESS); SDL_GL_CreateContext(window); SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); SDL_Event event; //OpenGL 4.1 glMatrixMode(GL_PROJECTION|GL_MODELVIEW); glLoadIdentity(); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glOrtho(-input.windowW/2,input.windowW/2,input.windowH/2,-input.windowH/2,0,1); glClearColor( 1, 1, 1, 0 ); SDL_GL_SetSwapInterval( 1 ); //vsync uint8_t *data; bool flagChanged = 0; Action_t flagAction = NOTHING; bool flagGenerated = 0; bool flagGeneratorData = 0; bool flagSolverData = 0; uint16_t i = 0; glClear(GL_COLOR_BUFFER_BIT); SDL_GL_SwapWindow(window); while(!quit){ while(SDL_PollEvent(&event)){ if(event.type == SDL_QUIT) quit = true; else if (event.type == SDL_KEYDOWN){ switch (event.key.keysym.sym){ case SDLK_g: flagAction = GENERATE; break; case SDLK_s: flagAction = SOLVE; break; case SDLK_TAB: flagAction = STOP; break; case SDLK_ESCAPE: quit = 1; break; case SDLK_UP: input.framesDrop++; break; case SDLK_RIGHT: input.animationDelay++; case SDLK_DOWN: input.framesDrop = (input.framesDrop - 1 > 0) ? input.framesDrop - 1 : 1; break; case SDLK_LEFT: input.animationDelay = (input.animationDelay - 1 >= 0) ? input.animationDelay - 1 : 0; break; } } } if(flagAction == GENERATE){ if(!flagGeneratorData){ d = initGeneratorData(input.width, input.height, input.startPoint); rd = initRenderData(d, input.windowW, input.windowH); flagGeneratorData = true; flagGenerated = false; flagSolverData = false; glVertexPointer(2, GL_FLOAT, sizeof(Vertex_t), rd.vertices); glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(VertexColor_t), rd.verticesColor); } for(i = 0; i < input.framesDrop; i++){ if(d.unvisitedNum != 0){ generateStep(&d); } else{ flagGeneratorData = false; flagGenerated = true; flagAction = NOTHING; break; } } d.maze[d.startPoint.y][d.startPoint.x] = CURRENT; renderMatrix(d.maze, rd, GENERATE); d.maze[d.startPoint.y][d.startPoint.x] = GENVISITED; flagChanged = true; } else if(flagAction == SOLVE && flagGenerated){ if(!flagSolverData){ d = initSeekerData(d, input.startPoint, input.exitPoint); rd = clearSeekerColorArray(d.maze, rd); flagSolverData = true; } for(i = 0; i < input.framesDrop; i++){ if((d.startPoint.x != d.exitPoint.x || d.startPoint.y != d.exitPoint.y) && d.error != 1){ seekStep(&d); } else if(d.error){ printf("ERROR: Lanyrinth cannot be solved! :C\n"); } else{ flagSolverData = false; flagAction = NOTHING; setMode(d.exitPoint, d.maze, WAY); break; } } setMode(d.startPoint, d.maze, CURRENT); renderMatrix(d.maze, rd, SOLVE); setMode(d.startPoint, d.maze, WAY); flagChanged = true; } else if(flagAction == STOP){ last = input; input = getInput(0, GENERATE, last); if((last.width != input.width) || (last.height != input.height)){ flagGenerated = false; flagGeneratorData = false; flagSolverData = false; } flagAction = NOTHING; flagChanged = true; } else if(flagAction == OUTPUT){ data = malloc(input.windowW * input.windowH * 4 * sizeof(uint8_t)); glReadBuffer(GL_FRONT); glReadPixels(0, 0, input.windowW , input.windowH, GL_RGBA, GL_UNSIGNED_BYTE, data); flagAction = NOTHING; } if(flagAction != NOTHING || flagChanged){ SDL_Delay(input.animationDelay); SDL_GL_SwapWindow(window); flagChanged = false; } } //cleanup wipe(d.stack); return 0; }