void subdivision (Point2d* bez, int deg) { //subdivide a bezier curve into two curves Point2d p; p.x = 0; p.y = 0; Point2d** pki = (Point2d **) malloc ((deg+1)*(deg+1)*sizeof(Point2d)); Point2d* bez1 = (Point2d *) malloc ((deg+1)*sizeof(Point2d)); Point2d* bez2 = (Point2d *) malloc ((deg+1)*sizeof(Point2d)); double t; t = 0.5; double error = getError(bez, deg); // if the maximal distance is greater than a threshold, the curve is to be subdivided into two parts if ( error>tessEps){ pki = deCasteljau(deg, t, bez); // de Casteljau algorithm for (int i=0; i<=deg; i++){ bez1[i] = pki[i][0]; // the left sub-curve bez2[i] = pki[deg-i][i]; // the right sub-curve } subdivision(bez1, deg); subdivision(bez2, deg); } // else, draw a line from the first control point and the last one else{ glVertex2f(bez[0].x, bez[0].y); glVertex2f(bez[deg].x, bez[deg].y); return; } }
void subdivision(Vector3D centre, float rayon, float rayonMin) { std::vector<Vector3D> lesCentres; if (rayon > rayonMin) { lesCentres.push_back(Vector3D(centre.x() - rayon / 2, centre.y() - rayon / 2, centre.z() - rayon / 2)); lesCentres.push_back(Vector3D(centre.x() - rayon / 2, centre.y() + rayon / 2, centre.z() - rayon / 2)); lesCentres.push_back(Vector3D(centre.x() + rayon / 2, centre.y() - rayon / 2, centre.z() - rayon / 2)); lesCentres.push_back(Vector3D(centre.x() + rayon / 2, centre.y() + rayon / 2, centre.z() - rayon / 2)); lesCentres.push_back(Vector3D(centre.x() - rayon / 2, centre.y() - rayon / 2, centre.z() + rayon / 2)); lesCentres.push_back(Vector3D(centre.x() - rayon / 2, centre.y() + rayon / 2, centre.z() + rayon / 2)); lesCentres.push_back(Vector3D(centre.x() + rayon / 2, centre.y() - rayon / 2, centre.z() + rayon / 2)); lesCentres.push_back(Vector3D(centre.x() + rayon / 2, centre.y() + rayon / 2, centre.z() + rayon / 2)); for (int i = 0; i < lesCentres.size(); i++) { subdivision(lesCentres.at(i), rayon / 2, rayonMin); } } else { finalSize = rayon; centreCube.push_back(centre); //cube(centre, rayon); } }
//============================================================ void plotBezier(Point2d* bez, int deg) { // TODO: add your own codes glBegin(GL_LINE_STRIP); subdivision(bez, deg); //subdivide a bezier curve into two curves glEnd(); }
void generateSubdivision(Matrix<Cell> &matrix) { MatrixCoord matrixSize = matrix.size(); for (int i=0; i<matrixSize.x; ++i) { for (int j=0; j<matrixSize.y; ++j) { matrix[MatrixCoord(i,j)].up = true; matrix[MatrixCoord(i,j)].down = true; matrix[MatrixCoord(i,j)].right = true; matrix[MatrixCoord(i,j)].left = true; } } for (int i=0; i<matrixSize.x; ++i) { matrix[MatrixCoord(i,0 )].down = false; matrix[MatrixCoord(i,matrixSize.y-1)].up = false; } for (int j=0; j<matrixSize.y; ++j) { matrix[MatrixCoord(0 , j)].left = false; matrix[MatrixCoord(matrixSize.x-1, j)].right = false; } subdivision(matrix, MatrixCoord(0,0), (matrixSize-MatrixCoord(1,1))); }
void wallcycle_print(FILE *fplog, int nnodes, int npme, double realtime, gmx_wallcycle_t wc, double cycles[]) { double c2t,tot,sum; int i,j,npp; char buf[STRLEN]; const char *myline = "-----------------------------------------------------------------------"; if (wc == NULL) { return; } if (npme > 0) { npp = nnodes - npme; } else { npp = nnodes; npme = nnodes; } tot = cycles[ewcRUN]; /* Conversion factor from cycles to seconds */ if (tot > 0) { c2t = nnodes*realtime/tot; } else { c2t = 0; } fprintf(fplog,"\n R E A L C Y C L E A N D T I M E A C C O U N T I N G\n\n"); fprintf(fplog," Computing: Nodes Number G-Cycles Seconds %c\n",'%'); fprintf(fplog,"%s\n",myline); sum = 0; for(i=ewcPPDURINGPME+1; i<ewcNR; i++) { if (!subdivision(i)) { print_cycles(fplog,c2t,wcn[i], (i==ewcPMEMESH || i==ewcPMEWAITCOMM) ? npme : npp, wc->wcc[i].n,cycles[i],tot); sum += cycles[i]; } } if (wc->wcc_all != NULL) { for(i=0; i<ewcNR; i++) { for(j=0; j<ewcNR; j++) { sprintf(buf,"%-9s",wcn[i]); buf[9] = ' '; sprintf(buf+10,"%-9s",wcn[j]); buf[19] = '\0'; print_cycles(fplog,c2t,buf, (i==ewcPMEMESH || i==ewcPMEWAITCOMM) ? npme : npp, wc->wcc_all[i*ewcNR+j].n, wc->wcc_all[i*ewcNR+j].c, tot); } } } print_cycles(fplog,c2t,"Rest",npp,0,tot-sum,tot); fprintf(fplog,"%s\n",myline); print_cycles(fplog,c2t,"Total",nnodes,0,tot,tot); fprintf(fplog,"%s\n",myline); if (wc->wcc[ewcPMEMESH].n > 0) { fprintf(fplog,"%s\n",myline); for(i=ewcPPDURINGPME+1; i<ewcNR; i++) { if (subdivision(i)) { print_cycles(fplog,c2t,wcn[i], (i>=ewcPMEMESH || i<=ewcPME_SOLVE) ? npme : npp, wc->wcc[i].n,cycles[i],tot); } } fprintf(fplog,"%s\n",myline); } if (cycles[ewcMoveE] > tot*0.05) { sprintf(buf, "NOTE: %d %% of the run time was spent communicating energies,\n" " you might want to use the -gcom option of mdrun\n", (int)(100*cycles[ewcMoveE]/tot+0.5)); if (fplog) { fprintf(fplog,"\n%s\n",buf); } /* Only the sim master calls this function, so always print to stderr */ fprintf(stderr,"\n%s\n",buf); } }
void subCompute(TRasterFxPort &m_input, TTile &tile, double frame, const TRenderSettings &ri, TPointD p00, TPointD p01, TPointD p11, TPointD p10, int details, bool wireframe, TDimension m_offScreenSize, bool isCast) { TPixel32 bgColor; TRectD outBBox, inBBox; outBBox = inBBox = TRectD(tile.m_pos, TDimensionD(tile.getRaster()->getLx(), tile.getRaster()->getLy())); m_input->getBBox(frame, inBBox, ri); if (inBBox == TConsts::infiniteRectD) // e' uno zerario inBBox = outBBox; int inBBoxLx = (int)inBBox.getLx() / ri.m_shrinkX; int inBBoxLy = (int)inBBox.getLy() / ri.m_shrinkY; if (inBBox.isEmpty()) return; if (p00 == p01 && p00 == p10 && p00 == p11 && !isCast) // significa che non c'e' deformazione { m_input->compute(tile, frame, ri); return; } TRaster32P rasIn; TPointD rasInPos; if (!wireframe) { if (ri.m_bpp == 64 || ri.m_bpp == 48) { TRaster64P aux = TRaster64P(inBBoxLx, inBBoxLy); rasInPos = TPointD(inBBox.x0 / ri.m_shrinkX, inBBox.y0 / ri.m_shrinkY); TTile tmp(aux, rasInPos); m_input->compute(tmp, frame, ri); rasIn = TRaster32P(inBBoxLx, inBBoxLy); TRop::convert(rasIn, aux); } else { rasInPos = TPointD(inBBox.x0 / ri.m_shrinkX, inBBox.y0 / ri.m_shrinkY); TTile tmp(TRaster32P(inBBoxLx, inBBoxLy), rasInPos); m_input->allocateAndCompute(tmp, rasInPos, TDimension(inBBoxLx, inBBoxLy), TRaster32P(), frame, ri); rasIn = tmp.getRaster(); } } unsigned int texWidth = 2; unsigned int texHeight = 2; while (texWidth < (unsigned int)inBBoxLx) texWidth = texWidth << 1; while (texHeight < (unsigned int)inBBoxLy) texHeight = texHeight << 1; while (texWidth > 1024 || texHeight > 1024) // avevo usato la costante // GL_MAX_TEXTURE_SIZE invece di // 1024, ma non funzionava! { inBBoxLx = inBBoxLx >> 1; inBBoxLy = inBBoxLy >> 1; texWidth = texWidth >> 1; texHeight = texHeight >> 1; } if (rasIn->getLx() != inBBoxLx || rasIn->getLy() != inBBoxLy) { TRaster32P rasOut = TRaster32P(inBBoxLx, inBBoxLy); TRop::resample(rasOut, rasIn, TScale((double)rasOut->getLx() / rasIn->getLx(), (double)rasOut->getLy() / rasIn->getLy())); rasIn = rasOut; } int rasterWidth = tile.getRaster()->getLx() + 2; int rasterHeight = tile.getRaster()->getLy() + 2; assert(rasterWidth > 0); assert(rasterHeight > 0); TRectD clippingRect = TRectD(tile.m_pos, TDimensionD(tile.getRaster()->getLx(), tile.getRaster()->getLy())); #if CREATE_GL_CONTEXT_ONE_TIME int ret = wglMakeCurrent(m_offScreenGL.m_offDC, m_offScreenGL.m_hglRC); assert(ret == TRUE); #else TOfflineGL offScreenRendering(TDimension(rasterWidth, rasterHeight)); //#ifdef _WIN32 offScreenRendering.makeCurrent(); //#else //#if defined(LINUX) || defined(MACOSX) // offScreenRendering.m_offlineGL->makeCurrent(); //#endif #endif checkErrorsByGL // disabilito quello che non mi serve per le texture glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); glDisable(GL_DITHER); glDisable(GL_DEPTH_TEST); glCullFace(GL_FRONT); glDisable(GL_STENCIL_TEST); glDisable(GL_LOGIC_OP); // creo la texture in base all'immagine originale glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); checkErrorsByGL #if !CREATE_GL_CONTEXT_ONE_TIME TRaster32P rasaux; if (!wireframe) { TRaster32P texture(texWidth, texHeight); texture->clear(); rasaux = texture; rasaux->lock(); texture->copy(rasIn); glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); glTexImage2D(GL_TEXTURE_2D, 0, 4, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture->getRawData()); } #else unsigned int texWidth = 1024; unsigned int texHeight = 1024; rasaux = rasIn; rasaux->lock(); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasIn->getLx(), rasIn->getLy(), GL_RGBA, GL_UNSIGNED_BYTE, rasIn->getRawData()); #endif checkErrorsByGL glEnable(GL_TEXTURE_2D); // cfr. help: OpenGL/Programming tip/OpenGL Correctness Tips glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-rasterWidth * 0.5, rasterWidth * 0.5, -rasterHeight * 0.5, rasterHeight * 0.5, -1, 1); glViewport(0, 0, rasterWidth, rasterHeight); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); // do OpenGL draw double lwTex = (double)(inBBoxLx - 1) / (double)(texWidth - 1); double lhTex = (double)(inBBoxLy - 1) / (double)(texHeight - 1); TPointD tex00 = TPointD(0.0, 0.0); TPointD tex10 = TPointD(lwTex, 0.0); TPointD tex11 = TPointD(lwTex, lhTex); TPointD tex01 = TPointD(0.0, lhTex); GLenum polygonStyle; if (wireframe) { polygonStyle = GL_LINE; glDisable(GL_TEXTURE_2D); } else polygonStyle = GL_FILL; checkErrorsByGL p00.x /= ri.m_shrinkX; p00.y /= ri.m_shrinkY; p10.x /= ri.m_shrinkX; p10.y /= ri.m_shrinkY; p11.x /= ri.m_shrinkX; p11.y /= ri.m_shrinkY; p01.x /= ri.m_shrinkX; p01.y /= ri.m_shrinkY; TPointD translate = TPointD(tile.m_pos.x + tile.getRaster()->getLx() * 0.5, tile.m_pos.y + tile.getRaster()->getLy() * 0.5); glTranslated(-translate.x, -translate.y, 0.0); // disegno il poligono double dist_p00_p01 = tdistance2(p00, p01); double dist_p10_p11 = tdistance2(p10, p11); double dist_p01_p11 = tdistance2(p01, p11); double dist_p00_p10 = tdistance2(p00, p10); bool vertical = (dist_p00_p01 == dist_p10_p11); bool horizontal = (dist_p00_p10 == dist_p01_p11); if (vertical && horizontal) details = 1; glPolygonMode(GL_FRONT_AND_BACK, polygonStyle); subdivision(p00, p10, p11, p01, tex00, tex10, tex11, tex01, clippingRect, details); if (!wireframe) { // abilito l'antialiasing delle linee glEnable(GL_LINE_SMOOTH); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); // disegno il bordo del poligono glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glBegin(GL_QUADS); glTexCoord2d(tex00.x, tex00.y); tglVertex(p00); glTexCoord2d(tex10.x, tex10.y); tglVertex(p10); glTexCoord2d(tex11.x, tex11.y); tglVertex(p11); glTexCoord2d(tex01.x, tex01.y); tglVertex(p01); glEnd(); // disabilito l'antialiasing per le linee glDisable(GL_LINE_SMOOTH); glDisable(GL_BLEND); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisable(GL_TEXTURE_2D); } // force to finish glFlush(); // rimetto il disegno dei poligoni a GL_FILL glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // metto il frame buffer nel raster del tile glPixelStorei(GL_UNPACK_ROW_LENGTH, rasterWidth); glPixelStorei(GL_UNPACK_ALIGNMENT, 4); TRaster32P newRas(tile.getRaster()->getLx(), tile.getRaster()->getLy()); newRas->lock(); glReadPixels(1, 1, newRas->getLx(), newRas->getLy(), GL_RGBA, GL_UNSIGNED_BYTE, (void *)newRas->getRawData()); newRas->unlock(); checkErrorsByGL rasaux->unlock(); tile.getRaster()->copy(newRas); }
void subdivision(const TPointD &p00, const TPointD &p10, const TPointD &p11, const TPointD &p01, const TPointD &tex00, const TPointD &tex10, const TPointD &tex11, const TPointD &tex01, const TRectD &clippingRect, int details) { if (details == 1) { glBegin(GL_QUADS); glTexCoord2d(tex00.x, tex00.y); tglVertex(p00); glTexCoord2d(tex10.x, tex10.y); tglVertex(p10); glTexCoord2d(tex11.x, tex11.y); tglVertex(p11); glTexCoord2d(tex01.x, tex01.y); tglVertex(p01); glEnd(); } else { TPointD A = p00; TPointD B = p10; TPointD C = p11; TPointD D = p01; /* * D L2 C * +----------------+----------------+ * | | | * | | | * | | | * | | | * | | | * H1 +----------------+----------------+ H2 * | | M | * | | | * | | | * | | | * | | | * +----------------+----------------+ * A L1 B * */ TPointD M, L1, L2, H1, H2, P1, P2; bool intersection; // M intersection = lineIntersection(A, C, B, D, M); assert(intersection); // P1 (punto di fuga) intersection = lineIntersection(D, C, A, B, P1); if (!intersection) { P1.x = 0.5 * (A.x + D.x); P1.y = 0.5 * (A.y + D.y); } // H1 intersection = lineIntersection(A, D, P1, M, H1); assert(intersection); // H2 intersection = lineIntersection(B, C, P1, M, H2); assert(intersection); // P2 (punto di fuga) intersection = lineIntersection(A, D, B, C, P2); if (!intersection) { P2.x = 0.5 * (A.x + B.x); P2.y = 0.5 * (A.y + B.y); } // L1 intersection = lineIntersection(A, B, P2, M, L1); assert(intersection); // L2 intersection = lineIntersection(D, C, P2, M, L2); assert(intersection); TPointD texA = (tex00 + tex10) * 0.5; TPointD texB = (tex10 + tex11) * 0.5; TPointD texC = (tex11 + tex01) * 0.5; TPointD texD = (tex01 + tex00) * 0.5; TPointD texM = (texA + texC) * 0.5; details--; TRectD r1 = TRectD( std::min({A.x, L1.x, M.x, H1.x}), std::min({A.y, L1.y, M.y, H1.y}), std::max({A.x, L1.x, M.x, H1.x}), std::max({A.y, L1.y, M.y, H1.y})); TRectD r2 = TRectD( std::min({L1.x, B.x, H2.x, M.x}), std::min({L1.y, B.y, H2.y, M.y}), std::max({L1.x, B.x, H2.x, M.x}), std::max({L1.y, B.y, H2.y, M.y})); TRectD r3 = TRectD( std::min({M.x, H2.x, C.x, L2.x}), std::min({M.y, H2.y, C.y, L2.y}), std::max({M.x, H2.x, C.x, L2.x}), std::max({M.y, H2.y, C.y, L2.y})); TRectD r4 = TRectD( std::min({H1.x, M.x, L2.x, D.x}), std::min({H1.y, M.y, L2.y, D.y}), std::max({H1.x, M.x, L2.x, D.x}), std::max({H1.y, M.y, L2.y, D.y})); if (r1.overlaps(clippingRect)) subdivision(A, L1, M, H1, tex00, texA, texM, texD, clippingRect, details); if (r2.overlaps(clippingRect)) subdivision(L1, B, H2, M, texA, tex10, texB, texM, clippingRect, details); if (r3.overlaps(clippingRect)) subdivision(M, H2, C, L2, texM, texB, tex11, texC, clippingRect, details); if (r4.overlaps(clippingRect)) subdivision(H1, M, L2, D, texD, texM, texC, tex01, clippingRect, details); } }
static void subdivision(Matrix<Cell> &matrix, const MatrixCoord &start, const MatrixCoord &end) { MatrixCoord difference = end - start; //std::cout << "box start=" << start << " end=" << end << " difference = " << difference << std::endl; if (difference.x <= 0 && difference.y <= 0) return; // std::cout << generateTileSpace(matrix) << std::endl; // std::cout << matrix << std::endl; int horizontal; if (difference.x == 0) horizontal = true; else if (difference.y == 0) horizontal = false; else horizontal = rand() % 2; //std::cout << ((horizontal) ? "horizontal" : "vertical") << std::endl; if (horizontal) { int bound = start.y + rand()%(difference.y); MatrixCoord start1 = start; MatrixCoord end1 = MatrixCoord(end.x, bound); MatrixCoord start2 = MatrixCoord(start.x, bound+1); MatrixCoord end2 = end; if (!matrix.outOfBounds(end1+MatrixCoord(0,1))) { for (int i=end1.x; i>=start1.x; --i) { matrix[MatrixCoord(i, end1.y )].up = false; matrix[MatrixCoord(i, end1.y+1)].down = false; } int irand = start1.x + rand() % (1+end1.x-start1.x); matrix[MatrixCoord(irand, end1.y )].up = true; matrix[MatrixCoord(irand, end1.y+1)].down = true; } //std::cout << "bound = " << bound << " will enqueue start1=" << start1 << " end1=" << end1 << std::endl; //std::cout << "bound = " << bound << " will enqueue start2=" << start2 << " end2=" << end2 << std::endl; subdivision(matrix, start1, end1); subdivision(matrix, start2, end2); } else { int bound = start.x + rand()%(difference.x); MatrixCoord start1 = start; MatrixCoord end1 = MatrixCoord(bound, end.y); MatrixCoord start2 = MatrixCoord(bound+1, start.y); MatrixCoord end2 = end; if (!matrix.outOfBounds(end1+MatrixCoord(1,0))) { for (int j=end1.y; j>=start1.y; --j) { matrix[MatrixCoord(end1.x , j)].right = false; matrix[MatrixCoord(end1.x+1, j)].left = false; } int jrand = start1.y + rand() % (1+end1.y-start1.y); matrix[MatrixCoord(end1.x , jrand)].right = true; matrix[MatrixCoord(end1.x+1, jrand)].left = true; } //std::cout << "bound = " << bound << " will enqueue start1=" << start1 << " end1=" << end1 << std::endl; //std::cout << "bound = " << bound << " will enqueue start2=" << start2 << " end2=" << end2 << std::endl; subdivision(matrix, start1, end1); subdivision(matrix, start2, end2); } }
int main(int argc, char **argv) { // initialisation des paramètres de GLUT en fonction // des arguments sur la ligne de commande glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); maille = Maillage::lectureOff("C:/Users/etu/Downloads/bunny.off", 10); maille.calculNormalTriangle(); //bunny.Ecriture("bunny.obj"); rayon = 0; rayonX = (maille.xMax - maille.xMin) / maille.getScale(); rayonY = (maille.yMax - maille.yMin) / maille.getScale(); rayonZ = (maille.zMax - maille.zMin) / maille.getScale(); if (rayonX > rayonY) { if (rayonX > rayonZ) rayon = rayonX; else rayon = rayonZ; } else { if (rayonY> rayonZ) rayon = rayonY; else rayon = rayonZ; } std::vector<Vector3D> geometrie = maille.getGeom(); Vector3D centre = maille.getCentreGravite(); double scale = maille.getScale(); for (int i = 0; i < geometrie.size(); i++) { geometrie[i] = (geometrie[i] - centre) / scale; } maille.setGeom(geometrie); subdivision(maille.getCentreGravite(), rayon, 1); simplification(); // définition et création de la fenêtre graphique glutInitWindowSize(WIDTH, HEIGHT); glutInitWindowPosition(0, 0); glutCreateWindow("Primitives graphiques"); // initialisation de OpenGL et de la scène initGL(); init_scene(); // choix des procédures de callback pour // le tracé graphique glutDisplayFunc(&window_display); // le redimensionnement de la fenêtre glutReshapeFunc(&window_reshape); // la gestion des événements clavier glutKeyboardFunc(&window_key); glutIdleFunc(&window_idle); // la boucle prinicipale de gestion des événements utilisateur glutMainLoop(); return 1; }