STKUNIT_UNIT_TEST(function, stringFunction_vector_valued) { EXCEPTWATCH; double x = 1.234; double y = 5.678; double z = 3.456; double t = 0; bool didCatch = false; try { StringFunction sfv0("v[0]=x; v[1]=y; v[2]=z; x", Name("sfv"), Dimensions(1,4), Dimensions(1,3) ); eval_vec3_print(1,2,3,0, sfv0); } catch (...) { didCatch = true; std::cout << "TEST::function::stringFunctionVector: expected to catch this since dom/codomain dimensions should be rank-1" << std::endl; } STKUNIT_EXPECT_TRUE(didCatch); StringFunction sfv("v[0]=x*y*z; v[1]=y; v[2]=z; x", Name("sfv"), Dimensions(3), Dimensions(3) ); eval_vec3_print(1.234, 2.345e-3, 3.456e+5, 0., sfv); MDArray vec = eval_vec3(x, y, z, t, sfv); std::cout << " x = " << x << " y = " << y << " z = " << z << " val = " << (vec[0]*vec[1]*vec[2]) << std::endl; STKUNIT_EXPECT_DOUBLE_EQ(vec[0], x*y*z); STKUNIT_EXPECT_DOUBLE_EQ(vec[1], y); STKUNIT_EXPECT_DOUBLE_EQ(vec[2], z); }
void vtkSlicer::RectilinearGridExecute(void) { vtkRectilinearGrid *rg = (vtkRectilinearGrid *) GetInput(); int pt_dims[3]; rg->GetDimensions(pt_dims); if (pt_dims[0] <= 1 || pt_dims[1] <= 1 || pt_dims[2] <= 1) { GeneralExecute(); return; } vtkIdType nCells = rg->GetNumberOfCells(); vtkCellData *inCD = rg->GetCellData(); vtkPointData *inPD = rg->GetPointData(); vtkPolyData *output = GetOutput(); vtkIdType ptSizeGuess = (this->CellList == NULL ? (int) pow(float(nCells), 0.6667f) * 5 + 100 : CellListSize*5 + 100); vtkSurfaceFromVolume sfv(ptSizeGuess); int tx = rg->GetXCoordinates()->GetDataType(); int ty = rg->GetYCoordinates()->GetDataType(); int tz = rg->GetZCoordinates()->GetDataType(); bool same = tx == ty && ty == tz; if(same && tx == VTK_FLOAT) { vtkStructuredCreateTriangles<float, RectSliceFunction<float> >( sfv, this->CellList, this->CellListSize, nCells, pt_dims, RectSliceFunction<float>( rg->GetXCoordinates(), rg->GetYCoordinates(), rg->GetZCoordinates(), this->Origin, this->Normal) ); } else if(same && tx == VTK_DOUBLE) { vtkStructuredCreateTriangles<double, RectSliceFunction<double> >( sfv, this->CellList, this->CellListSize, nCells, pt_dims, RectSliceFunction<double>( rg->GetXCoordinates(), rg->GetYCoordinates(), rg->GetZCoordinates(), this->Origin, this->Normal) ); } else { vtkStructuredCreateTriangles<double, GeneralRectSliceFunction >( sfv, this->CellList, this->CellListSize, nCells, pt_dims, GeneralRectSliceFunction( rg->GetXCoordinates(), rg->GetYCoordinates(), rg->GetZCoordinates(), this->Origin, this->Normal) ); } sfv.ConstructPolyData(inPD, inCD, output, pt_dims, rg->GetXCoordinates(), rg->GetYCoordinates(), rg->GetZCoordinates()); }
common::sfv_t make_sfv(const string& repr) { vector<string> elems = split(repr, ' '); common::sfv_t sfv(elems.size()); for (size_t i = 0; i < elems.size(); ++i) { vector<string> parts = split(elems[i], ':'); sfv[i] = make_pair(parts[0], pfi::lang::lexical_cast<float>(parts[1])); } return sfv; }
void ToolsWidget::Prepare(ContainerWindow &parent, const PixelRect &rc) { ScriptFileVisitor sfv(list); Directory::VisitFiles(Path(_T("/mnt/onboard/XCSoarData/kobo/scripts")), sfv); unsigned len = list.size(); if (len > 0) std::sort(list.begin(), list.end()); unsigned max_script_buttons = std::min(len, MAX_SCRIPTS); for (unsigned i = 0; i < max_script_buttons; i++) AddButton(list[i].name, *this, i); }
void vtkSlicer::StructuredGridExecute(void) { vtkStructuredGrid *sg = (vtkStructuredGrid *) GetInput(); int pt_dims[3]; sg->GetDimensions(pt_dims); if (pt_dims[0] <= 1 || pt_dims[1] <= 1 || pt_dims[2] <= 1) { GeneralExecute(); return; } vtkIdType nCells = sg->GetNumberOfCells(); vtkPoints *inPts = sg->GetPoints(); vtkCellData *inCD = sg->GetCellData(); vtkPointData *inPD = sg->GetPointData(); vtkPolyData *output = GetOutput(); vtkIdType ptSizeGuess = (this->CellList == NULL ? (int) pow(float(nCells), 0.6667f) * 5 + 100 : CellListSize*5 + 100); vtkSurfaceFromVolume sfv(ptSizeGuess); if(inPts->GetDataType() == VTK_FLOAT) { vtkStructuredCreateTriangles<float, SliceFunction<float> >( sfv, this->CellList, this->CellListSize, nCells, pt_dims, SliceFunction<float>(pt_dims, inPts, this->Origin, this->Normal) ); } else if(inPts->GetDataType() == VTK_DOUBLE) { vtkStructuredCreateTriangles<double, SliceFunction<double> >( sfv, this->CellList, this->CellListSize, nCells, pt_dims, SliceFunction<double>(pt_dims, inPts, this->Origin, this->Normal) ); } else { vtkStructuredCreateTriangles<double, GeneralSliceFunction >( sfv, this->CellList, this->CellListSize, nCells, pt_dims, GeneralSliceFunction(pt_dims, inPts, this->Origin, this->Normal) ); } sfv.ConstructPolyData(inPD, inCD, output, inPts); }
/** Download finished! */ void DownloadManager::handleEndData(UserConnection* aSource) { dcassert(aSource->getState() == UserConnection::STATE_DONE); Download* d = aSource->getDownload(); dcassert(d != NULL); if(d->isSet(Download::FLAG_TREE_DOWNLOAD)) { d->getFile()->flush(); delete d->getFile(); d->setFile(NULL); Download* old = d->getOldDownload(); size_t bl = 1024; while(bl * old->getTigerTree().getLeaves().size() < old->getSize()) bl *= 2; old->getTigerTree().setBlockSize(bl); dcassert(old->getSize() != -1); old->getTigerTree().setFileSize(old->getSize()); old->getTigerTree().calcRoot(); if(!(*old->getTTH() == old->getTigerTree().getRoot())) { // This tree is for a different file, remove from queue... fire(DownloadManagerListener::Failed(), old, STRING(INVALID_TREE)); string target = old->getTarget(); aSource->setDownload(NULL); removeDownload(old, true); QueueManager::getInstance()->removeSource(target, aSource->getUser(), QueueItem::Source::FLAG_BAD_TREE, false); checkDownloads(aSource); return; } d->getOldDownload()->setTreeValid(true); HashManager::getInstance()->addTree(old->getTarget(), old->getTigerTree()); aSource->setDownload(d->getOldDownload()); delete d; // Ok, now we can continue to the actual file... checkDownloads(aSource); return; } u_int32_t crc = 0; bool hasCrc = (d->getCrcCalc() != NULL); // First, finish writing the file (flushing the buffers and closing the file...) try { d->getFile()->flush(); if(hasCrc) crc = d->getCrcCalc()->getFilter().getValue(); delete d->getFile(); d->setFile(NULL); d->setCrcCalc(NULL); // Check if we're anti-fragging... if(d->isSet(Download::FLAG_ANTI_FRAG)) { // Ok, rename the file to what we expect it to be... try { const string& tgt = d->getTempTarget().empty() ? d->getTarget() : d->getTempTarget(); File::renameFile(d->getDownloadTarget(), tgt); d->unsetFlag(Download::FLAG_ANTI_FRAG); } catch(const FileException& e) { dcdebug("AntiFrag: %s\n", e.getError().c_str()); // Now what? } } } catch(const FileException& e) { fire(DownloadManagerListener::Failed(), d, e.getError()); aSource->setDownload(NULL); removeDownload(d, true); removeConnection(aSource); return; } dcassert(d->getPos() == d->getSize()); dcdebug("Download finished: %s, size " I64_FMT ", downloaded " I64_FMT "\n", d->getTarget().c_str(), d->getSize(), d->getTotal()); // Check if we have some crc:s... if(BOOLSETTING(SFV_CHECK)) { SFVReader sfv(d->getTarget()); if(sfv.hasCRC()) { bool crcMatch; string tgt = d->getDownloadTarget(); if(hasCrc) { crcMatch = (crc == sfv.getCRC()); } else { // More complicated, we have to reread the file try { File ff(tgt, File::READ, File::OPEN); CalcInputStream<CRC32Filter, false> f(&ff); const size_t BUF_SIZE = 16 * 65536; AutoArray<u_int8_t> b(BUF_SIZE); size_t n = BUF_SIZE; while(f.read((u_int8_t*)b, n) > 0) ; // Keep on looping... crcMatch = (f.getFilter().getValue() == sfv.getCRC()); } catch (FileException&) { // Nope; read failed... goto noCRC; } } if(!crcMatch) { File::deleteFile(tgt); dcdebug("DownloadManager: CRC32 mismatch for %s\n", d->getTarget().c_str()); LogManager::getInstance()->message(STRING(SFV_INCONSISTENCY) + " (" + STRING(FILE) + ": " + d->getTarget() + ")"); fire(DownloadManagerListener::Failed(), d, STRING(SFV_INCONSISTENCY)); string target = d->getTarget(); aSource->setDownload(NULL); removeDownload(d, true); QueueManager::getInstance()->removeSource(target, aSource->getUser(), QueueItem::Source::FLAG_CRC_WARN, false); checkDownloads(aSource); return; } d->setFlag(Download::FLAG_CRC32_OK); dcdebug("DownloadManager: CRC32 match for %s\n", d->getTarget().c_str()); } } noCRC: if(BOOLSETTING(LOG_DOWNLOADS) && (BOOLSETTING(LOG_FILELIST_TRANSFERS) || !d->isSet(Download::FLAG_USER_LIST))) { StringMap params; params["target"] = d->getTarget(); params["user"] = aSource->getUser()->getNick(); params["hub"] = aSource->getUser()->getLastHubName(); params["hubip"] = aSource->getUser()->getLastHubAddress(); params["size"] = Util::toString(d->getSize()); params["sizeshort"] = Util::formatBytes(d->getSize()); params["chunksize"] = Util::toString(d->getTotal()); params["chunksizeshort"] = Util::formatBytes(d->getTotal()); params["actualsize"] = Util::toString(d->getActual()); params["actualsizeshort"] = Util::formatBytes(d->getActual()); params["speed"] = Util::formatBytes(d->getAverageSpeed()) + "/s"; params["time"] = Util::formatSeconds((GET_TICK() - d->getStart()) / 1000); params["sfv"] = Util::toString(d->isSet(Download::FLAG_CRC32_OK) ? 1 : 0); LOG(DOWNLOAD_AREA, Util::formatParams(SETTING(LOG_FORMAT_POST_DOWNLOAD), params)); } // Check if we need to move the file if( !d->getTempTarget().empty() && (Util::stricmp(d->getTarget().c_str(), d->getTempTarget().c_str()) != 0) ) { try { File::ensureDirectory(d->getTarget()); if(File::getSize(d->getTempTarget()) > MOVER_LIMIT) { mover.moveFile(d->getTempTarget(), d->getTarget()); } else { File::renameFile(d->getTempTarget(), d->getTarget()); } d->setTempTarget(Util::emptyString); } catch(const FileException&) { try { if(!SETTING(DOWNLOAD_DIRECTORY).empty()) { File::renameFile(d->getTempTarget(), SETTING(DOWNLOAD_DIRECTORY) + d->getTargetFileName()); } else { File::renameFile(d->getTempTarget(), Util::getFilePath(d->getTempTarget()) + d->getTargetFileName()); } } catch(const FileException&) { try { File::renameFile(d->getTempTarget(), Util::getFilePath(d->getTempTarget()) + d->getTargetFileName()); } catch(const FileException&) { // Ignore... } } } } fire(DownloadManagerListener::Complete(), d); aSource->setDownload(NULL); removeDownload(d, true, true); checkDownloads(aSource); }
void vtkSlicer::UnstructuredGridExecute(void) { // The routine here is a bit trickier than for the Rectilinear or // Structured grids. We want to slice an unstructured grid -- but that // could mean any cell type. We only have triangulation tables for // the finite element zoo. So the gameplan is to slice any of the // elements of the finite element zoo. If there are more elements left // over, slice them using the conventional VTK filters. Finally, // append together the slices from the zoo with the slices from the // non-zoo elements. If all the elements are from the zoo, then just // slice them with no appending. vtkUnstructuredGrid *ug = (vtkUnstructuredGrid *) GetInput(); vtkIdType nCells = ug->GetNumberOfCells(); vtkPoints *inPts = ug->GetPoints(); vtkCellData *inCD = ug->GetCellData(); vtkPointData *inPD = ug->GetPointData(); vtkPolyData *output = GetOutput(); vtkIdType ptSizeGuess = (this->CellList == NULL ? (int) pow(float(nCells), 0.6667f) * 5 + 100 : CellListSize*5 + 100); vtkSurfaceFromVolume sfv(ptSizeGuess); vtkUnstructuredGrid *stuff_I_cant_slice = vtkUnstructuredGrid::New(); vtkPolyData *vertices_on_slice = vtkPolyData::New(); double D = Origin[0]*Normal[0] + Origin[1]*Normal[1] + Origin[2]*Normal[2]; vtkIdType nToProcess = (CellList != NULL ? CellListSize : nCells); vtkIdType numIcantSlice = 0; vtkIdType numVertices = 0; for (vtkIdType i = 0 ; i < nToProcess ; i++) { vtkIdType cellId = (CellList != NULL ? CellList[i] : i); int cellType = ug->GetCellType(cellId); vtkIdType npts; vtkIdType *pts; ug->GetCellPoints(cellId, npts, pts); const int *triangulation_table = NULL; const int *vertices_from_edges = NULL; int tt_step = 0; bool canSlice = false; bool isVertex = false; switch (cellType) { case VTK_TETRA: triangulation_table = (const int *) tetTriangulationTable; vertices_from_edges = (const int *) tetVerticesFromEdges; tt_step = 7; canSlice = true; break; case VTK_PYRAMID: triangulation_table = (const int *) pyramidTriangulationTable; vertices_from_edges = (const int *) pyramidVerticesFromEdges; tt_step = 13; canSlice = true; break; case VTK_WEDGE: triangulation_table = (const int *) wedgeTriangulationTable; vertices_from_edges = (const int *) wedgeVerticesFromEdges; tt_step = 13; canSlice = true; break; case VTK_HEXAHEDRON: triangulation_table = (const int *) hexTriangulationTable; vertices_from_edges = (const int *) hexVerticesFromEdges; tt_step = 16; canSlice = true; break; case VTK_VERTEX: isVertex = true; break; default: canSlice = false; break; } if (canSlice) { int tmp[3] = {0,0,0}; if(inPts->GetDataType() == VTK_FLOAT) { vtkUnstructuredCreateTriangles<float, SliceFunction<float> >( sfv, cellId, pts, npts, triangulation_table, vertices_from_edges, tt_step, SliceFunction<float>(tmp, inPts, this->Origin, this->Normal) ); } else if(inPts->GetDataType() == VTK_DOUBLE) { vtkUnstructuredCreateTriangles<double, SliceFunction<double> >( sfv, cellId, pts, npts, triangulation_table, vertices_from_edges, tt_step, SliceFunction<double>(tmp, inPts, this->Origin, this->Normal) ); } else { vtkUnstructuredCreateTriangles<double, GeneralSliceFunction >( sfv, cellId, pts, npts, triangulation_table, vertices_from_edges, tt_step, GeneralSliceFunction(tmp, inPts, this->Origin, this->Normal) ); } } else if (isVertex) { // // Determine if the vertex is even on the plane. // const double *pt = inPts->GetPoint(pts[0]); double dist_from_plane = Normal[0]*pt[0] + Normal[1]*pt[1] + Normal[2]*pt[2] - D; if (fabs(dist_from_plane) < 1e-12) { if (numVertices == 0) { vertices_on_slice->SetPoints(ug->GetPoints()); vertices_on_slice->GetPointData()->ShallowCopy( ug->GetPointData()); vertices_on_slice->Allocate(nCells); vertices_on_slice->GetCellData()-> CopyAllocate(ug->GetCellData(), nCells); } vertices_on_slice->InsertNextCell(VTK_VERTEX, 1, pts); vertices_on_slice->GetCellData()-> CopyData(ug->GetCellData(), cellId, numVertices); numVertices++; } } else { if (numIcantSlice == 0) { stuff_I_cant_slice->SetPoints(ug->GetPoints()); stuff_I_cant_slice->GetPointData()->ShallowCopy( ug->GetPointData()); stuff_I_cant_slice->Allocate(nCells); stuff_I_cant_slice->GetCellData()-> CopyAllocate(ug->GetCellData(), nCells); } if(cellType == VTK_POLYHEDRON) { vtkIdType nFaces, *facePtIds; ug->GetFaceStream(cellId, nFaces, facePtIds); stuff_I_cant_slice->InsertNextCell(cellType, npts, pts, nFaces, facePtIds); } else stuff_I_cant_slice->InsertNextCell(cellType, npts, pts); stuff_I_cant_slice->GetCellData()-> CopyData(ug->GetCellData(), cellId, numIcantSlice); numIcantSlice++; } } if ((numIcantSlice > 0) || (numVertices > 0)) { vtkAppendPolyData *appender = vtkAppendPolyData::New(); if (numIcantSlice > 0) { vtkPolyData *not_from_zoo = vtkPolyData::New(); SliceDataset(stuff_I_cant_slice, not_from_zoo, true); appender->AddInput(not_from_zoo); not_from_zoo->Delete(); } if (numVertices > 0) { appender->AddInput(vertices_on_slice); } vtkPolyData *just_from_zoo = vtkPolyData::New(); sfv.ConstructPolyData(inPD, inCD, just_from_zoo, inPts); appender->AddInput(just_from_zoo); just_from_zoo->Delete(); appender->GetOutput()->Update(); output->ShallowCopy(appender->GetOutput()); appender->Delete(); } else { sfv.ConstructPolyData(inPD, inCD, output, inPts); } stuff_I_cant_slice->Delete(); vertices_on_slice->Delete(); }