int main(int argc, char *argv[]) { // FILE *f = fopen("sierp.txt","w"); CarpetPrint(stdout, Sierpinski(3)); // fclose(f); return 0; }
Carpet Sierpinski( int n ) { Carpet carpet; Carpet subCarpet; int row,col, rb; int spc_rqrd; subCarpet = (n > 1) ? Sierpinski(n-1) : &single; carpet = malloc(sizeof(struct sCarpet)); carpet->dim = 3*subCarpet->dim; spc_rqrd = (2*subCarpet->dim) * (carpet->dim); carpet->data = malloc(spc_rqrd*sizeof(char)); carpet->rows = malloc( carpet->dim*sizeof(char *)); for (row=0; row<subCarpet->dim; row++) { carpet->rows[row] = carpet->data + row*carpet->dim; rb = row+subCarpet->dim; carpet->rows[rb] = carpet->data + rb*carpet->dim; rb = row+2*subCarpet->dim; carpet->rows[rb] = carpet->data + row*carpet->dim; } for (col=0; col < 3; col++) { /* 2 rows of tiles to copy - third group points to same data a first */ for (row=0; row < 2; row++) TileCarpet( carpet, row, col, subCarpet ); } if (subCarpet != &single ) { free(subCarpet->rows); free(subCarpet->data); free(subCarpet); } return carpet; }
void CmdMengerSponge::activated(int iMsg) { bool ok; int level = QInputDialog::getInteger(Gui::getMainWindow(), QString::fromAscii("Menger sponge"), QString::fromAscii("Recursion depth:"), 3, 1, 5, 1, &ok); if (!ok) return; int ret = QMessageBox::question(Gui::getMainWindow(), QString::fromAscii("Parallel"), QString::fromAscii("Do you want to run this in a thread pool?"), QMessageBox::Yes|QMessageBox::No); bool parallel=(ret == QMessageBox::Yes); float x0=0,y0=0,z0=0; globalBox = Mesh::MeshObject::createCube(1,1,1); MeshObjectRef mesh; if (parallel) mesh = makeParallelMengerSponge(level,x0,y0,z0); else mesh = Sierpinski(level,x0,y0,z0); MeshCore::MeshKernel& kernel = mesh->getKernel(); // remove duplicated points MeshCore::MeshFixDuplicatePoints(kernel).Fixup(); // remove internal facets MeshCore::MeshEvalInternalFacets eval(kernel); eval.Evaluate(); kernel.DeleteFacets(eval.GetIndices()); // repair neighbourhood kernel.RebuildNeighbours(); App::Document* doc = App::GetApplication().newDocument(); Mesh::Feature* feature = static_cast<Mesh::Feature*>(doc->addObject("Mesh::Feature","MengerSponge")); feature->Mesh.setValue(*mesh); feature->purgeTouched(); }
MeshObjectRef Sierpinski(int level, float x0, float y0, float z0) { float boxnums = std::pow(3.0f,level); float thirds = boxnums / 3; float twothirds = thirds * 2; QList<float> rangerx, rangery, rangerz; if (level == 0) { rangerx << x0; rangery << y0; rangerz << z0; } else { rangerx << x0 << (x0 + thirds) << (x0 + twothirds); rangery << y0 << (y0 + thirds) << (y0 + twothirds); rangerz << z0 << (z0 + thirds) << (z0 + twothirds); } int block = 1; QList<int> skip; skip << 5 << 11 << 13 << 14 << 15 << 17 << 23; MeshObjectRef mesh = new Mesh::MeshObject(); for (QList<float>::iterator i = rangerx.begin(); i != rangerx.end(); ++i) { for (QList<float>::iterator j = rangery.begin(); j != rangery.end(); ++j) { for (QList<float>::iterator k = rangerz.begin(); k != rangerz.end(); ++k) { if (!skip.contains(block)) { if (level > 0) mesh->addMesh(*Sierpinski(level-1,*i,*j,*k)); else mesh->addMesh(*PlaceBox(*i,*j,*k)); } block++; } } } return mesh; }
MeshObjectRef runSierpinski(const Param& p) { return Sierpinski(p.level,p.x,p.y,p.z); }