/* * Output a message in the lower left corner of the screen * and wait for carriage return. */ void error(const char *s) { int ch; ++errmsgs; if (!any_display) { /* * Nothing has been displayed yet. Output this message on * error output (file descriptor 2) and don't wait for a * keystroke to continue. * * This has the desirable effect of producing all error * messages on error output if standard output is directed * to a file. It also does the same if we never produce * any real output; for example, if the input file(s) cannot * be opened. If we do eventually produce output, code in * edit() makes sure these messages can be seen before they * are overwritten or scrolled away. */ if (s != NULL) fprintf(stderr, "%s\n", s); return; } lower_left(); clear_eol(); so_enter(); if (s != NULL) printf("%s ", s); fputs(return_to_continue, stdout); so_exit(); if ((ch = getchr()) != '\n') { if (ch == 'q') quit(); cmdstack = ch; } lower_left(); if ((s != NULL ? strlen(s) : 0) + sizeof(return_to_continue) + so_width + se_width + 1 > (size_t)sc_width) /* * Printing the message has probably scrolled the screen. * {{ Unless the terminal doesn't have auto margins, * in which case we just hammered on the right margin. }} */ repaint(); fflush(stdout); }
/* set up the display to start a new multi-character command. */ void start_mca(int action, const char *str) { lower_left(); clear_eol(); fputs(str, stdout); cmd_col = strlen(str); mca = action; }
void screen_controller::drawbox(int hor, int ver, int ul, int ur, int ll, int lr) { draw_vertical(0, 0, maxrow()-1, ver); draw_vertical(0, maxcol(), maxrow()-1, ver); draw_horizontal(0, 0, maxcol()-1, hor); draw_horizontal(maxrow(), 0, maxcol()-1, hor); upper_left(); printf("%d", ul, 0); lower_left(); printf("%d", ll, 0); upper_right(); printf("%d", ur, 0); lower_right(); printf("%d", lr, 0); }
inline bool Intersects(const RectangleInt2D &other) const { FixedPointCoordinate upper_left(other.max_lat, other.min_lon); FixedPointCoordinate upper_right(other.max_lat, other.max_lon); FixedPointCoordinate lower_right(other.min_lat, other.max_lon); FixedPointCoordinate lower_left(other.min_lat, other.min_lon); return (Contains(upper_left) || Contains(upper_right) || Contains(lower_right) || Contains(lower_left)); }
void ierror(char *s) { lower_left(); clear_eol(); so_enter(); printf("%s ... (interrupt to abort)", s); so_exit(); fflush(stdout); }
int prompt(void) { off_t len, pos; /* * if nothing is displayed yet, display starting from line 1; * if search string provided, go there instead. */ if (position(TOP) == NULL_POSITION) { if (forw_line((off_t)0) == NULL_POSITION) return(0); if (!firstsearch || !search(1, firstsearch, 1, 1)) jump_back(1); } else if (screen_trashed) repaint(); /* if no -e flag and we've hit EOF on the last file, quit. */ if ((!quit_at_eof || short_file) && hit_eof && curr_ac + 1 >= ac) quit(); /* select the proper prompt and display it. */ lower_left(); clear_eol(); if (longprompt) { so_enter(); printf("%s:", current_name); if (!ispipe) printf(" file %d/%d", curr_ac + 1, ac); if (linenums) printf(" line %d", currline(BOTTOM)); if ((pos = position(BOTTOM)) != NULL_POSITION) { printf(" byte %qd", pos); if (!ispipe && (len = ch_length())) printf("/%qd pct %qd%%", len, ((100 * pos) / len)); } so_exit(); longprompt = 0; } else { so_enter(); printf("%s", current_name); if (hit_eof) if (next_name) printf(": END (next file: %s)", next_name); else printf(": END"); else if (!ispipe && (pos = position(BOTTOM)) != NULL_POSITION && (len = ch_length())) printf(" (%qd%%)", ((100 * pos) / len)); so_exit(); } return(1); }
/* * Pass the specified command to a shell to be executed. * Like plain "system()", but handles resetting terminal modes, etc. */ void lsystem(const char *cmd) { int inp; char sysbuf[256]; char *shell; /* * Print the command which is to be executed, * unless the command starts with a "-". */ if (cmd[0] == '-') cmd++; else { lower_left(); clear_eol(); printf("!%s\n", cmd); } /* * De-initialize the terminal and take out of raw mode. */ deinit(); fflush(stdout); raw_mode(0); /* * Restore signals to their defaults. */ init_signals(0); /* * Force standard input to be the terminal, "/dev/tty", * even if less's standard input is coming from a pipe. */ inp = dup(0); (void)close(0); if (open(_PATH_TTY, O_RDONLY, 0) < 0) (void)dup(inp); /* * Pass the command to the system to be executed. * If we have a SHELL environment variable, use * <$SHELL -c "command"> instead of just <command>. * If the command is empty, just invoke a shell. */ if ((shell = getenv("SHELL")) != NULL && *shell != '\0') { if (*cmd == '\0') cmd = shell; else { snprintf(sysbuf, sizeof(sysbuf), "%s -c \"%s\"", shell, cmd); cmd = sysbuf; } } if (*cmd == '\0') cmd = "sh"; (void)system(cmd); /* * Restore standard input, reset signals, raw mode, etc. */ (void)close(0); (void)dup(inp); (void)close(inp); init_signals(1); raw_mode(1); init(); screen_trashed = 1; /* * Since we were ignoring window change signals while we executed * the system command, we must assume the window changed. */ windoch(0); }
/* * Main command processor. * Accept and execute commands until a quit command, then return. */ void commands(void) { int c, action; last_mca = 0; nscroll = (sc_height + 1) / 2; for (;;) { mca = 0; number = 0; /* * See if any signals need processing. */ if (sigs) { psignals(); if (quitting) quit(); } /* * Display prompt and accept a character. */ CMD_RESET; if (!prompt()) { next_file(1); continue; } noprefix(); c = getcc(); again: if (sigs) continue; /* * If we are in a multicharacter command, call mca_char. * Otherwise we call cmd_decode to determine the * action to be performed. */ if (mca) switch (mca_char(c)) { case MCA_MORE: /* * Need another character. */ c = getcc(); goto again; case MCA_DONE: /* * Command has been handled by mca_char. * Start clean with a prompt. */ continue; case NO_MCA: /* * Not a multi-char command * (at least, not anymore). */ break; } /* decode the command character and decide what to do. */ switch (action = cmd_decode(c)) { case A_DIGIT: /* first digit of a number */ start_mca(A_DIGIT, ":"); goto again; case A_F_SCREEN: /* forward one screen */ CMD_EXEC; if (number <= 0 && (number = sc_window) <= 0) number = sc_height - 1; forward(number, 1); break; case A_B_SCREEN: /* backward one screen */ CMD_EXEC; if (number <= 0 && (number = sc_window) <= 0) number = sc_height - 1; backward(number, 1); break; case A_F_LINE: /* forward N (default 1) line */ CMD_EXEC; forward(number <= 0 ? 1 : number, 0); break; case A_B_LINE: /* backward N (default 1) line */ CMD_EXEC; backward(number <= 0 ? 1 : number, 0); break; case A_F_SCROLL: /* forward N lines */ CMD_EXEC; if (number > 0) nscroll = number; forward(nscroll, 0); break; case A_B_SCROLL: /* backward N lines */ CMD_EXEC; if (number > 0) nscroll = number; backward(nscroll, 0); break; case A_FREPAINT: /* flush buffers and repaint */ if (!ispipe) { ch_init(0, 0); clr_linenum(); } /* FALLTHROUGH */ case A_REPAINT: /* repaint the screen */ CMD_EXEC; repaint(); break; case A_GOLINE: /* go to line N, default 1 */ CMD_EXEC; if (number <= 0) number = 1; jump_back(number); break; case A_PERCENT: /* go to percent of file */ CMD_EXEC; if (number < 0) number = 0; else if (number > 100) number = 100; jump_percent(number); break; case A_GOEND: /* go to line N, default end */ CMD_EXEC; if (number <= 0) jump_forw(); else jump_back(number); break; case A_STAT: /* print file name, etc. */ longprompt = 1; continue; case A_QUIT: /* exit */ quit(); case A_F_SEARCH: /* search for a pattern */ case A_B_SEARCH: if (number <= 0) number = 1; start_mca(action, (action==A_F_SEARCH) ? "/" : "?"); last_mca = mca; wsearch = 1; c = getcc(); if (c == '!') { /* * Invert the sense of the search; set wsearch * to 0 and get a new character for the start * of the pattern. */ start_mca(action, (action == A_F_SEARCH) ? "!/" : "!?"); wsearch = 0; c = getcc(); } goto again; case A_AGAIN_SEARCH: /* repeat previous search */ if (number <= 0) number = 1; if (wsearch) start_mca(last_mca, (last_mca == A_F_SEARCH) ? "/" : "?"); else start_mca(last_mca, (last_mca == A_F_SEARCH) ? "!/" : "!?"); CMD_EXEC; (void)search(mca == A_F_SEARCH, (char *)NULL, number, wsearch); break; case A_HELP: /* help */ lower_left(); clear_eol(); fputs("help", stdout); CMD_EXEC; help(); break; case A_TAGFILE: /* tag a new file */ CMD_RESET; start_mca(A_TAGFILE, "Tag: "); c = getcc(); goto again; case A_FILE_LIST: /* show list of file names */ CMD_EXEC; showlist(); repaint(); break; case A_EXAMINE: /* edit a new file */ CMD_RESET; start_mca(A_EXAMINE, "Examine: "); c = getcc(); goto again; case A_VISUAL: /* invoke the editor */ if (ispipe) { error("Cannot edit standard input"); break; } CMD_EXEC; editfile(); ch_init(0, 0); clr_linenum(); break; case A_NEXT_FILE: /* examine next file */ if (number <= 0) number = 1; next_file(number); break; case A_PREV_FILE: /* examine previous file */ if (number <= 0) number = 1; prev_file(number); break; case A_SETMARK: /* set a mark */ lower_left(); clear_eol(); start_mca(A_SETMARK, "mark: "); c = getcc(); if (c == erase_char || c == kill_char) break; setmark(c); break; case A_GOMARK: /* go to mark */ lower_left(); clear_eol(); start_mca(A_GOMARK, "goto mark: "); c = getcc(); if (c == erase_char || c == kill_char) break; gomark(c); break; case A_PREFIX: /* * The command is incomplete (more chars are needed). * Display the current char so the user knows what's * going on and get another character. */ if (mca != A_PREFIX) start_mca(A_PREFIX, ""); if (CONTROL_CHAR(c)) { putchar('^'); c = CARAT_CHAR(c); } putchar(c); c = getcc(); goto again; default: putchar('\7'); break; } } }
/* * Move the cursor to lower left before executing a command. * This looks nicer if the command takes a long time before * updating the screen. */ static void cmd_exec() { lower_left(); flush(); }
void TestComputeCoarseElementsForFineElementCentroids() throw(Exception) { TetrahedralMesh<2,2> fine_mesh; fine_mesh.ConstructRegularSlabMesh(0.2, 1.0, 1.0); QuadraticMesh<2> coarse_mesh(1.0, 1.0, 1.0); // 2 triangular elements FineCoarseMeshPair<2> mesh_pair(fine_mesh,coarse_mesh); TS_ASSERT_THROWS_CONTAINS(mesh_pair.ComputeCoarseElementsForFineElementCentroids(true),"Call SetUpBoxesOnCoarseMesh()"); mesh_pair.SetUpBoxesOnCoarseMesh(); mesh_pair.ComputeCoarseElementsForFineElementCentroids(true); //Check that the indices of the coarse mesh elements are as expected unsigned lower_left_element_index=1u; ChastePoint<2> lower_left(0.25, 0.25); TS_ASSERT_EQUALS(coarse_mesh.GetContainingElementIndex(lower_left), lower_left_element_index); unsigned upper_right_element_index=0u; ChastePoint<2> upper_right(0.75, 0.75); TS_ASSERT_EQUALS(coarse_mesh.GetContainingElementIndex(upper_right), upper_right_element_index); TS_ASSERT_EQUALS( mesh_pair.rGetCoarseElementsForFineElementCentroids().size(), fine_mesh.GetNumElements()); for (unsigned i=0; i<fine_mesh.GetNumElements(); i++) { double x = fine_mesh.GetElement(i)->CalculateCentroid()(0); double y = fine_mesh.GetElement(i)->CalculateCentroid()(1); if (x+y < 1.0) { TS_ASSERT_EQUALS(mesh_pair.rGetCoarseElementsForFineElementCentroids()[i], lower_left_element_index); } else { TS_ASSERT_EQUALS(mesh_pair.rGetCoarseElementsForFineElementCentroids()[i], upper_right_element_index); } } // Coverage mesh_pair.DeleteCoarseBoxCollection(); mesh_pair.SetUpBoxesOnCoarseMesh(0.8); // force a point to be found in a neighbouring box mesh_pair.ComputeCoarseElementsForFineElementCentroids(true); mesh_pair.DeleteCoarseBoxCollection(); mesh_pair.SetUpBoxesOnCoarseMesh(0.1); // force a point to be found in a nonlocal box mesh_pair.ComputeCoarseElementsForFineElementCentroids(true); mesh_pair.DeleteCoarseBoxCollection(); mesh_pair.SetUpBoxesOnCoarseMesh(); // back to default /* * Translate the fine mesh in the (-1, -1) direction --> all fine elements * nearest to (not contained in) element 0. We have to make the fine mesh * tiny and then translate a small amount so that it is still in the box * collection for the coarse (normally the two meshes should overlap). */ fine_mesh.Scale(1e-2, 1e-2); fine_mesh.Translate(-1.1e-2, -1.1e-2); mesh_pair.ComputeCoarseElementsForFineElementCentroids(true); TS_ASSERT_EQUALS( mesh_pair.rGetCoarseElementsForFineElementCentroids().size(), fine_mesh.GetNumElements()); for (unsigned i=0; i<fine_mesh.GetNumElements(); i++) { TS_ASSERT_EQUALS( mesh_pair.rGetCoarseElementsForFineElementCentroids()[i], lower_left_element_index); } }
void TestComputeCoarseElementsForFineNodes() throw(Exception) { TetrahedralMesh<2,2> fine_mesh; fine_mesh.ConstructRegularSlabMesh(0.2, 1.0, 1.0); QuadraticMesh<2> coarse_mesh(1.0, 1.0, 1.0); // 2 triangular elements FineCoarseMeshPair<2> mesh_pair(fine_mesh,coarse_mesh); TS_ASSERT_THROWS_CONTAINS(mesh_pair.ComputeCoarseElementsForFineNodes(true),"Call SetUpBoxesOnCoarseMesh()"); mesh_pair.SetUpBoxesOnCoarseMesh(); mesh_pair.ComputeCoarseElementsForFineNodes(true); //Check that the indices of the coarse mesh elements are as expected unsigned lower_left_element_index=1u; ChastePoint<2> lower_left(0.25, 0.25); TS_ASSERT_EQUALS(coarse_mesh.GetContainingElementIndex(lower_left), lower_left_element_index); ChastePoint<2> lower_left1(0.1, 0.25); //Double check that there is a `backslash` ChastePoint<2> lower_left2(0.25, 0.1); TS_ASSERT_EQUALS(coarse_mesh.GetContainingElementIndex(lower_left1), lower_left_element_index); TS_ASSERT_EQUALS(coarse_mesh.GetContainingElementIndex(lower_left2), lower_left_element_index); unsigned upper_right_element_index=0u; ChastePoint<2> upper_right(0.75, 0.75); TS_ASSERT_EQUALS(coarse_mesh.GetContainingElementIndex(upper_right), upper_right_element_index); for (unsigned i=0; i<fine_mesh.GetNumNodes(); i++) { double x = fine_mesh.GetNode(i)->rGetLocation()[0]; double y = fine_mesh.GetNode(i)->rGetLocation()[1]; if ( x+y < 1.0 - 1e-5 ) // x+y < 1 { TS_ASSERT_EQUALS(mesh_pair.rGetCoarseElementsForFineNodes()[i], lower_left_element_index); } else if ( x+y > 1.0 + 1e-5 ) // x+y > 1 { TS_ASSERT_EQUALS(mesh_pair.rGetCoarseElementsForFineNodes()[i], upper_right_element_index); } else // x=1-y, so in both elements, result could be either. However, it should find 0 first { //TS_ASSERT_LESS_THAN(mesh_pair.rGetCoarseElementsForFineNodes()[i], 2u); TS_ASSERT_EQUALS(mesh_pair.rGetCoarseElementsForFineNodes()[i], 0u); } } /* * Translate the fine mesh in the (-1, -1) direction --> all fine nodes * nearest to (not contained in) element 0. We have to make the fine mesh * tiny and then translate a small amount so that it is still in the box * collection for the coarse (normally the two meshes should overlap). */ fine_mesh.Scale(1e-2, 1e-2); fine_mesh.Translate(-1.1e-2, -1.1e-2); mesh_pair.ComputeCoarseElementsForFineNodes(true); for (unsigned i=0; i<fine_mesh.GetNumNodes(); i++) { TS_ASSERT_EQUALS(mesh_pair.rGetCoarseElementsForFineNodes()[i], lower_left_element_index); } // Call again with safeMode=false this time (same results, faster) mesh_pair.rGetCoarseElementsForFineNodes()[0] = 189342958; mesh_pair.ComputeCoarseElementsForFineNodes(false); for (unsigned i=0; i<fine_mesh.GetNumNodes(); i++) { TS_ASSERT_EQUALS(mesh_pair.rGetCoarseElementsForFineNodes()[i], lower_left_element_index); } // Coverage: // call again mesh_pair.SetUpBoxesOnCoarseMesh(); // delete mesh_pair.DeleteCoarseBoxCollection(); }
int main() { try { // Camera settings Camera camera; glm::vec3 vrp(0.0f, 0.0f, 10.0f); glm::vec3 vpn(0.0f, 0.0f, 1.0f); glm::vec3 vup(0.0f, 1.0f, 0.0f); glm::vec3 prp(0.0f, 0.0f, 100.0f); float F = 10.0f; float B = -80.0f; glm::vec2 lower_left(-20.0f, -20.0f); glm::vec2 upper_right(20.0f, 20.0f); camera = Camera(vrp, vpn, vup, prp, lower_left, upper_right, F, B); // Examples int curves = 4; // Amount of examples BezierRow G[curves]; G[0] = BezierRow(glm::vec3(-15.0f, -15.0f, 0.0f), glm::vec3(-10.0f, 25.0f, 0.0f), glm::vec3(10.0f, 25.0f, 0.0f), glm::vec3(15.0f, -15.0f, 0.0f)); G[1] = BezierRow(glm::vec3(-20.0f, 0.0f, 0.0f), glm::vec3(-1.0f, 55.0f, 0.0f), glm::vec3(1.0f, -55.0f, 0.0f), glm::vec3(20.0f, 0.0f, 0.0f)); G[2] = BezierRow(glm::vec3(-1.0f, -5.0f, 0.0f), glm::vec3(-60.0f, 5.0f, 0.0f), glm::vec3(60.0f, 5.0f, 0.0f), glm::vec3(1.0f, -5.0f, 0.0f)); G[3] = BezierRow(glm::vec3(-10.0f, -5.0f, 0.0f), glm::vec3(60.0f, 5.0f, 0.0f), glm::vec3(-60.0f, 5.0f, 0.0f), glm::vec3(10.0f, -5.0f, 0.0f)); int currentfigure = 3; // Set figure between 4 different examples // Decide whether to use sampling or subdivision int decider = 1; int Npoint = 0; std::vector<glm::vec3> points; // Sampling if(decider == 0){ float t = 0.0f; float step = 12.0f; sampling(G[currentfigure], t, step, points); Npoint = step*2; } // Subdivision else if(decider == 1){ DLB /= 8.0f; DRB /= 8.0f; int n = 3; // Amount of curves (smoothness) int npow = pow(2, n+1); // Amount of points subdivide(G[currentfigure], n, points); Npoint = npow; } else{ printf("No method chosen\n"); return 1; } glm::mat4x4 CTM = camera.CurrentTransformationMatrix(); std::cout << "CTM = " << std::endl << CTM << std::endl; // Initialize the graphics InitializeGLFW(); GLFWwindow* Window = CreateWindow(WindowWidth, WindowHeight, WindowTitle.c_str()); InitializeGLEW(); InitializeOpenGL(); glfwSwapBuffers(Window); // Read and Compile the vertex program vertextransform.vert GLuint vertexprogID = CreateGpuProgram("vertextransform.vert", GL_VERTEX_SHADER); // Read and Compile the fragment program linefragment.frag GLuint linefragmentprogID = CreateGpuProgram("linefragment.frag", GL_FRAGMENT_SHADER); // Create a lineshader program and Link it with the vertex and linefragment programs GLuint lineshaderID = CreateShaderProgram(vertexprogID, linefragmentprogID); // Now comes the OpenGL core part // This is where the curve is initialized // User data is in the global variable curveVertices, and the number of entries // is in Ncurvevertices // Make a VertexArrayObject - it is used by the VertexArrayBuffer, and it must be declared! GLuint CurveVertexArrayID; glGenVertexArrays(1, &CurveVertexArrayID); glBindVertexArray(CurveVertexArrayID); // Make a curvevertexbufferObject - it uses the previous VertexArrayBuffer! GLuint curvevertexbuffer; glGenBuffers(1, &curvevertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, curvevertexbuffer); // Give our vertices to OpenGL. glBufferData(GL_ARRAY_BUFFER, Npoint * 3 * sizeof(float), &points[0], GL_STATIC_DRAW); // Validate the shader program ValidateShader(lineshaderID, "Validating the lineshader"); // Get locations of Uniforms GLuint curvevertextransform = glGetUniformLocation(lineshaderID, "CTM"); GLuint curvefragmentcolor = glGetUniformLocation(lineshaderID, "Color"); // Initialize Attributes GLuint curvevertexattribute = glGetAttribLocation(lineshaderID, "VertexPosition"); glVertexAttribPointer(curvevertexattribute, 3, GL_FLOAT, GL_FALSE, 0, 0); // The main loop while (!glfwWindowShouldClose(Window)) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(lineshaderID); glm::mat4x4 CTM = camera.CurrentTransformationMatrix(); glUniformMatrix4fv(curvevertextransform, 1, GL_FALSE, &CTM[0][0]); glUniform3f(curvefragmentcolor, 0.2f, 0.2f, 0.2f); glEnableVertexAttribArray(curvevertexattribute); glBindVertexArray(CurveVertexArrayID); // This is very important! There are two "binds"! glDrawArrays(GL_LINES, 0, Npoint); glDisableVertexAttribArray(curvevertexattribute); glUseProgram(0); glfwSwapBuffers(Window); std::stringstream errormessage; errormessage << "End of loop: " << "assignment5.cpp" << ": " << __LINE__ << ": "; ErrorCheck(errormessage.str()); glfwPollEvents(); } } catch (std::exception const& exception) { std::cerr << "Exception: " << exception.what() << std::endl; } glfwTerminate(); return 0; }