void MeshElementRemovalDialog::on_materialIDCheckBox_toggled(bool is_checked) { materialListWidget->setEnabled(is_checked); if (is_checked && (_currentIndex != _matIDIndex)) { materialListWidget->clear(); _matIDIndex = _currentIndex; auto mesh = _project.getMesh(meshNameComboBox->currentText().toStdString()); auto const opt_mat_ids = mesh->getProperties().getPropertyVector<int>("MaterialIDs"); if (!opt_mat_ids) { INFO("Properties \"MaterialIDs\" not found in the mesh \"%s\".", mesh->getName().c_str()); return; } auto const& mat_ids = opt_mat_ids.get(); if (mat_ids.size() != mesh->getNElements()) { INFO("Size mismatch: Properties \"MaterialIDs\" contains %u values," " the mesh \"%s\" contains %u elements.", mat_ids.size(), mesh->getName().c_str(), mesh->getNElements()); return; } auto max_material = std::max_element(mat_ids.cbegin(), mat_ids.cend()); for (unsigned i=0; i <= static_cast<unsigned>(*max_material); ++i) materialListWidget->addItem(QString::number(i)); } }
struct slName *randomBigBedIds(char *table, struct sqlConnection *conn, int count) /* Return some arbitrary IDs from a bigBed file. */ { /* Figure out bigBed file name and open it. Get contents for first chromosome as an example. */ struct slName *idList = NULL; char *fileName = bigBedFileName(table, conn); struct bbiFile *bbi = bigBedFileOpen(fileName); struct bbiChromInfo *chromList = bbiChromList(bbi); struct lm *lm = lmInit(0); int orderedCount = count * 4; if (orderedCount < 100) orderedCount = 100; struct bigBedInterval *iv, *ivList = getNElements(bbi, chromList, lm, orderedCount); shuffleList(&ivList); // Make a list of item names from intervals. int outCount = 0; for (iv = ivList; iv != NULL && outCount < count; iv = iv->next) { char *row[bbi->fieldCount]; char startBuf[16], endBuf[16]; bigBedIntervalToRow(iv, chromList->name, startBuf, endBuf, row, bbi->fieldCount); if (idList == NULL || differentString(row[3], idList->name)) { slAddHead(&idList, slNameNew(row[3])); outCount++; } } lmCleanup(&lm); bbiFileClose(&bbi); freeMem(fileName); return idList; }
void MeshElementRemovalDialog::on_meshNameComboBox_currentIndexChanged(int idx) { Q_UNUSED(idx); this->_currentIndex = this->meshNameComboBox->currentIndex(); this->newMeshNameEdit->setText(this->meshNameComboBox->currentText() + "_new"); this->elementTypeListWidget->clearSelection(); this->materialListWidget->clearSelection(); if (this->boundingBoxCheckBox->isChecked()) this->on_boundingBoxCheckBox_toggled(true); auto mesh = _project.getMesh(meshNameComboBox->currentText().toStdString()); auto materialIds = mesh->getProperties().getPropertyVector<int>("MaterialIDs"); if (materialIds) { if (materialIds->size() != mesh->getNElements()) { ERR ("Incorrect mesh structure: Number of Material IDs does not match number of mesh elements."); OGSError::box("Incorrect mesh structure detected.\n Number of Material IDs does not match number of mesh elements"); QMetaObject::invokeMethod(this, "close", Qt::QueuedConnection); } materialIDCheckBox->setEnabled(true); if (materialIDCheckBox->isChecked()) on_materialIDCheckBox_toggled(true); } else materialIDCheckBox->setEnabled(false); }
TEST(MeshLib, RemoveElements) { auto mesh = std::unique_ptr<MeshLib::Mesh>{ MeshLib::MeshGenerator::generateLineMesh(1.0, 9)}; std::vector<std::size_t> removed_ele_ids; for (std::size_t i=0; i<5; i++) removed_ele_ids.push_back(i); auto new_mesh = std::unique_ptr<MeshLib::Mesh>{ MeshLib::removeElements(*mesh, removed_ele_ids, "")}; ASSERT_EQ(5u, new_mesh->getNNodes()); ASSERT_EQ(5u, new_mesh->getNBaseNodes()); ASSERT_EQ(4u, new_mesh->getNElements()); for (std::size_t i=0; i<new_mesh->getNNodes(); i++) ASSERT_TRUE(*mesh->getNode(5+i) == *new_mesh->getNode(i)); }
TEST_F(TestVtkMeshConverter, Conversion) { auto mesh = std::unique_ptr<MeshLib::Mesh>( MeshLib::VtkMeshConverter::convertUnstructuredGrid(vtu)); ASSERT_EQ(mesh->getNNodes(), vtu->GetNumberOfPoints()); ASSERT_EQ(mesh->getNElements(), vtu->GetNumberOfCells()); ASSERT_EQ(mesh->getElement(0)->getCellType(), MeshLib::CellType::HEX8); auto meshProperties = mesh->getProperties(); // MaterialIDs are converted to an int property auto materialIds = meshProperties.getPropertyVector<int>("MaterialIDs"); ASSERT_TRUE(static_cast<bool>(materialIds)); auto vtkMaterialIds = vtu->GetCellData()->GetArray("MaterialIDs"); ASSERT_EQ((*materialIds).size(), vtkMaterialIds->GetNumberOfTuples()); for(std::size_t i = 0; i < (*materialIds).size(); i++) ASSERT_EQ((*materialIds)[i], vtkMaterialIds->GetTuple1(i)); }
void showSchemaBigBed(char *table, struct trackDb *tdb) /* Show schema on bigBed. */ { /* Figure out bigBed file name and open it. Get contents for first chromosome as an example. */ struct sqlConnection *conn = NULL; if (!trackHubDatabase(database)) conn = hAllocConn(database); char *fileName = bigBedFileName(table, conn); struct bbiFile *bbi = bigBedFileOpen(fileName); struct bbiChromInfo *chromList = bbiChromList(bbi); struct lm *lm = lmInit(0); struct bigBedInterval *ivList = getNElements(bbi, chromList, lm, 10); /* Get description of columns, making it up from BED records if need be. */ struct asObject *as = bigBedAsOrDefault(bbi); hPrintf("<B>Database:</B> %s", database); hPrintf(" <B>Primary Table:</B> %s<br>", table); hPrintf("<B>Big Bed File:</B> %s", fileName); if (bbi->version >= 2) { hPrintf("<BR><B>Item Count:</B> "); printLongWithCommas(stdout, bigBedItemCount(bbi)); } hPrintf("<BR>\n"); hPrintf("<B>Format description:</B> %s<BR>", as->comment); /* Put up table that describes fields. */ hTableStart(); hPrintf("<TR><TH>field</TH>"); if (ivList != NULL) hPrintf("<TH>example</TH>"); hPrintf("<TH>description</TH> "); puts("</TR>\n"); struct asColumn *col; int colCount = 0; char *row[bbi->fieldCount]; char startBuf[16], endBuf[16]; if (ivList != NULL) { char *dupeRest = lmCloneString(lm, ivList->rest); /* Manage rest-stomping side-effect */ bigBedIntervalToRow(ivList, chromList->name, startBuf, endBuf, row, bbi->fieldCount); ivList->rest = dupeRest; } for (col = as->columnList; col != NULL; col = col->next) { hPrintf("<TR><TD><TT>%s</TT></TD>", col->name); if (ivList != NULL) hPrintf("<TD>%s</TD>", row[colCount]); hPrintf("<TD>%s</TD></TR>", col->comment); ++colCount; } /* If more fields than descriptions put up minimally helpful info (at least has example). */ for ( ; colCount < bbi->fieldCount; ++colCount) { hPrintf("<TR><TD><TT>column%d</TT></TD>", colCount+1); if (ivList != NULL) hPrintf("<TD>%s</TD>", row[colCount]); hPrintf("<TD>n/a</TD></TR>\n"); } hTableEnd(); if (ivList != NULL) { /* Put up another section with sample rows. */ webNewSection("Sample Rows"); hTableStart(); /* Print field names as column headers for example */ hPrintf("<TR>"); int colIx = 0; for (col = as->columnList; col != NULL; col = col->next) { hPrintf("<TH>%s</TH>", col->name); ++colIx; } for (; colIx < colCount; ++colIx) hPrintf("<TH>column%d</TH>", colIx+1); hPrintf("</TR>\n"); /* Print sample lines. */ struct bigBedInterval *iv; for (iv=ivList; iv != NULL; iv = iv->next) { bigBedIntervalToRow(iv, chromList->name, startBuf, endBuf, row, bbi->fieldCount); hPrintf("<TR>"); for (colIx=0; colIx<colCount; ++colIx) { writeHtmlCell(row[colIx]); } hPrintf("</TR>\n"); } hTableEnd(); } printTrackHtml(tdb); /* Clean up and go home. */ lmCleanup(&lm); bbiFileClose(&bbi); freeMem(fileName); hFreeConn(&conn); }
// Writes the mesh into a vtk file, reads the file back and converts it into a // OGS mesh TEST_F(InSituMesh, MappedMeshSourceRoundtrip) { // TODO Add more comparison criteria ASSERT_TRUE(mesh != nullptr); std::string test_data_file(BaseLib::BuildInfo::tests_tmp_path + "/MappedMeshSourceRoundtrip.vtu"); // -- Test VtkMappedMeshSource, i.e. OGS mesh to VTK mesh vtkNew<InSituLib::VtkMappedMeshSource> vtkSource; vtkSource->SetMesh(mesh); vtkSource->Update(); vtkUnstructuredGrid* output = vtkSource->GetOutput(); // Point and cell numbers ASSERT_EQ((subdivisions+1)*(subdivisions+1)*(subdivisions+1), output->GetNumberOfPoints()); ASSERT_EQ(subdivisions*subdivisions*subdivisions, output->GetNumberOfCells()); // Point data arrays vtkDataArray* pointDoubleArray = output->GetPointData()->GetScalars("PointDoubleProperty"); ASSERT_EQ(pointDoubleArray->GetSize(), mesh->getNNodes()); ASSERT_EQ(pointDoubleArray->GetComponent(0, 0), 1.0); double* range = pointDoubleArray->GetRange(0); ASSERT_EQ(range[0], 1.0); ASSERT_EQ(range[1], 1.0 + mesh->getNNodes() - 1.0); vtkDataArray* pointIntArray = output->GetPointData()->GetScalars("PointIntProperty"); ASSERT_EQ(pointIntArray->GetSize(), mesh->getNNodes()); ASSERT_EQ(pointIntArray->GetComponent(0, 0), 1.0); range = pointIntArray->GetRange(0); ASSERT_EQ(range[0], 1.0); ASSERT_EQ(range[1], 1 + mesh->getNNodes() - 1); vtkDataArray* pointUnsignedArray = output->GetPointData()->GetScalars("PointUnsignedProperty"); ASSERT_EQ(pointUnsignedArray->GetSize(), mesh->getNNodes()); ASSERT_EQ(pointUnsignedArray->GetComponent(0, 0), 1.0); range = pointUnsignedArray->GetRange(0); ASSERT_EQ(range[0], 1.0); ASSERT_EQ(range[1], 1 + mesh->getNNodes() - 1); // Cell data arrays vtkDataArray* cellDoubleArray = output->GetCellData()->GetScalars("CellDoubleProperty"); ASSERT_EQ(cellDoubleArray->GetSize(), mesh->getNElements()); ASSERT_EQ(cellDoubleArray->GetComponent(0, 0), 1.0); range = cellDoubleArray->GetRange(0); ASSERT_EQ(range[0], 1.0); ASSERT_EQ(range[1], 1.0 + mesh->getNElements() - 1.0); vtkDataArray* cellIntArray = output->GetCellData()->GetScalars("CellIntProperty"); ASSERT_EQ(cellIntArray->GetSize(), mesh->getNElements()); ASSERT_EQ(cellIntArray->GetComponent(0, 0), 1.0); range = cellIntArray->GetRange(0); ASSERT_EQ(range[0], 1.0); ASSERT_EQ(range[1], 1 + mesh->getNElements() - 1); vtkDataArray* cellUnsignedArray = output->GetCellData()->GetScalars("CellUnsignedProperty"); ASSERT_EQ(cellUnsignedArray->GetSize(), mesh->getNElements()); ASSERT_EQ(cellUnsignedArray->GetComponent(0, 0), 1.0); range = cellUnsignedArray->GetRange(0); ASSERT_EQ(range[0], 1.0); ASSERT_EQ(range[1], 1 + mesh->getNElements() - 1); // -- Write VTK mesh to file (in all combinations of binary, appended and compressed) // atm vtkXMLWriter::Appended does not work, see http://www.paraview.org/Bug/view.php?id=13382 for(int dataMode : { vtkXMLWriter::Ascii, vtkXMLWriter::Binary }) { for(bool compressed : { true, false }) { if(dataMode == vtkXMLWriter::Ascii && compressed) continue; FileIO::VtuInterface vtuInterface(mesh, dataMode, compressed); ASSERT_TRUE(vtuInterface.writeToFile(test_data_file)); // -- Read back VTK mesh vtkSmartPointer<vtkXMLUnstructuredGridReader> reader = vtkSmartPointer<vtkXMLUnstructuredGridReader>::New(); reader->SetFileName(test_data_file.c_str()); reader->Update(); vtkUnstructuredGrid *vtkMesh = reader->GetOutput(); // Both VTK meshes should be identical ASSERT_EQ(vtkMesh->GetNumberOfPoints(), output->GetNumberOfPoints()); ASSERT_EQ(vtkMesh->GetNumberOfCells(), output->GetNumberOfCells()); ASSERT_EQ(vtkMesh->GetPointData()->GetScalars("PointDoubleProperty")->GetNumberOfTuples(), pointDoubleArray->GetNumberOfTuples()); ASSERT_EQ(vtkMesh->GetPointData()->GetScalars("PointIntProperty")->GetNumberOfTuples(), pointIntArray->GetNumberOfTuples()); ASSERT_EQ(vtkMesh->GetPointData()->GetScalars("PointUnsignedProperty")->GetNumberOfTuples(), pointUnsignedArray->GetNumberOfTuples()); ASSERT_EQ(vtkMesh->GetCellData()->GetScalars("CellDoubleProperty")->GetNumberOfTuples(), cellDoubleArray->GetNumberOfTuples()); ASSERT_EQ(vtkMesh->GetCellData()->GetScalars("CellIntProperty")->GetNumberOfTuples(), cellIntArray->GetNumberOfTuples()); ASSERT_EQ(vtkMesh->GetCellData()->GetScalars("CellUnsignedProperty")->GetNumberOfTuples(), cellUnsignedArray->GetNumberOfTuples()); // Both OGS meshes should be identical auto newMesh = std::unique_ptr<MeshLib::Mesh>{MeshLib::VtkMeshConverter::convertUnstructuredGrid(vtkMesh)}; ASSERT_EQ(mesh->getNNodes(), newMesh->getNNodes()); ASSERT_EQ(mesh->getNElements(), newMesh->getNElements()); // Both properties should be identical auto meshProperties = mesh->getProperties(); auto newMeshProperties = newMesh->getProperties(); ASSERT_EQ(newMeshProperties.hasPropertyVector("PointDoubleProperty"), meshProperties.hasPropertyVector("PointDoubleProperty")); ASSERT_EQ(newMeshProperties.hasPropertyVector("PointIntProperty"), meshProperties.hasPropertyVector("PointIntProperty")); ASSERT_EQ(newMeshProperties.hasPropertyVector("PointUnsignedProperty"), meshProperties.hasPropertyVector("PointUnsignedProperty")); ASSERT_EQ(newMeshProperties.hasPropertyVector("CellDoubleProperty"), meshProperties.hasPropertyVector("CellDoubleProperty")); ASSERT_EQ(newMeshProperties.hasPropertyVector("CellIntProperty"), meshProperties.hasPropertyVector("CellIntProperty")); ASSERT_EQ(newMeshProperties.hasPropertyVector("CellUnsignedProperty"), meshProperties.hasPropertyVector("CellUnsignedProperty")); ASSERT_EQ(newMeshProperties.hasPropertyVector("MaterialIDs"), meshProperties.hasPropertyVector("MaterialIDs")); // Check some properties on equality auto doubleProps = meshProperties.getPropertyVector<double>("PointDoubleProperty"); auto newDoubleProps = newMeshProperties.getPropertyVector<double>("PointDoubleProperty"); ASSERT_EQ((*newDoubleProps).getTupleSize(), (*doubleProps).getTupleSize()); ASSERT_EQ((*newDoubleProps).getNumberOfTuples(), (*doubleProps).getNumberOfTuples()); ASSERT_EQ((*newDoubleProps).size(), (*doubleProps).size()); for(std::size_t i = 0; i < (*doubleProps).size(); i++) ASSERT_EQ((*newDoubleProps)[i], (*doubleProps)[i]); auto unsignedProps = meshProperties.getPropertyVector<unsigned>("CellUnsignedProperty"); auto newUnsignedIds = newMeshProperties.getPropertyVector<unsigned>("CellUnsignedProperty"); ASSERT_EQ((*newUnsignedIds).getTupleSize(), (*unsignedProps).getTupleSize()); ASSERT_EQ((*newUnsignedIds).getNumberOfTuples(), (*unsignedProps).getNumberOfTuples()); ASSERT_EQ((*newUnsignedIds).size(), (*unsignedProps).size()); for(std::size_t i = 0; i < (*unsignedProps).size(); i++) ASSERT_EQ((*newUnsignedIds)[i], (*unsignedProps)[i]); auto materialIds = meshProperties.getPropertyVector<int>("MaterialIDs"); auto newMaterialIds = newMeshProperties.getPropertyVector<int>("MaterialIDs"); ASSERT_EQ((*newMaterialIds).getTupleSize(), (*materialIds).getTupleSize()); ASSERT_EQ((*newMaterialIds).getNumberOfTuples(), (*materialIds).getNumberOfTuples()); ASSERT_EQ((*newMaterialIds).size(), (*materialIds).size()); for(std::size_t i = 0; i < (*materialIds).size(); i++) ASSERT_EQ((*newMaterialIds)[i], (*materialIds)[i]); } } }
int main (int argc, char* argv[]) { ApplicationsLib::LogogSetup logog_setup; TCLAP::CmdLine cmd("Converting meshes in gmsh file format (ASCII, version 2.2) to a vtk unstructured grid file (new OGS file format) or to the old OGS file format - see options.", ' ', "0.1"); TCLAP::ValueArg<std::string> ogs_mesh_arg( "o", "out", "filename for output mesh (if extension is msh, old OGS fileformat is written)", true, "", "filename as string"); cmd.add(ogs_mesh_arg); TCLAP::ValueArg<std::string> gmsh_mesh_arg( "i", "in", "gmsh input file", true, "", "filename as string"); cmd.add(gmsh_mesh_arg); TCLAP::SwitchArg exclude_lines_arg("e", "exclude-lines", "if set, lines will not be written to the ogs mesh"); cmd.add(exclude_lines_arg); cmd.parse(argc, argv); // *** read mesh INFO("Reading %s.", gmsh_mesh_arg.getValue().c_str()); #ifndef WIN32 BaseLib::MemWatch mem_watch; unsigned long mem_without_mesh (mem_watch.getVirtMemUsage()); #endif BaseLib::RunTime run_time; run_time.start(); MeshLib::Mesh * mesh(FileIO::GMSHInterface::readGMSHMesh(gmsh_mesh_arg.getValue())); if (mesh == nullptr) { INFO("Could not read mesh from %s.", gmsh_mesh_arg.getValue().c_str()); return -1; } #ifndef WIN32 unsigned long mem_with_mesh (mem_watch.getVirtMemUsage()); INFO("Mem for mesh: %i MB", (mem_with_mesh - mem_without_mesh)/(1024*1024)); #endif INFO("Time for reading: %f seconds.", run_time.elapsed()); INFO("Read %d nodes and %d elements.", mesh->getNNodes(), mesh->getNElements()); // *** remove line elements on request if (exclude_lines_arg.getValue()) { auto ex = MeshLib::ElementSearch(*mesh); ex.searchByElementType(MeshLib::MeshElemType::LINE); auto m = MeshLib::removeElements(*mesh, ex.getSearchedElementIDs(), mesh->getName()+"-withoutLines"); if (m != nullptr) { INFO("Removed %d lines.", mesh->getNElements() - m->getNElements()); std::swap(m, mesh); delete m; } else { INFO("Mesh does not contain any lines."); } } // *** write mesh in new format MeshLib::IO::writeMeshToFile(*mesh, ogs_mesh_arg.getValue()); delete mesh; }