// select all visible nodes UInt32 RenderAction::selectVisibles(void) { if(getFrustumCulling() == false) return getNNodes(); useNodeList(); Color3f col; UInt32 count = 0; for(UInt32 i = 0; i < getNNodes(); i++) { if(isVisible(getNode(i))) { col.setValuesRGB(0,1,0); addNode(getNode(i)); ++count; } else { col.setValuesRGB(1,0,0); } if(getVolumeDrawing()) { dropVolume(this, getNode(i), col); } } return count; }
bool ModelContainer::writeFile(const char *filename) { bool result = false; unsigned int flags=0; unsigned int size; FILE *wf =fopen(filename,"wb"); if(wf) { fwrite(VMAP_MAGIC,1,8,wf); result = true; if(result && fwrite("CTREE01",8,1,wf) != 1) result = false; if(result && fwrite(&flags,sizeof(unsigned int),1,wf) != 1) result = false; if(result && fwrite("POS ",4,1,wf) != 1) result = false; size = sizeof(float)*3; if(result && fwrite(&size,4,1,wf) != 1) result = false; Vector3 basePos = getBasePosition(); if(result && fwrite(&basePos,sizeof(float),3,wf) != 3) result = false; if(result && fwrite("BOX ",4,1,wf) != 1) result = false; size = sizeof(float)*6; if(result && fwrite(&size,4,1,wf) != 1) result = false; Vector3 low = iBox.low(); if(result && fwrite(&low,sizeof(float),3,wf) != 3) result = false; Vector3 high = iBox.high(); if(result && fwrite(&high,sizeof(float),3,wf) != 3) result = false; if(result && fwrite("NODE",4,1,wf) != 1) result = false; size = sizeof(unsigned int)+ sizeof(TreeNode)*getNNodes(); if(result && fwrite(&size,4,1,wf) != 1) result = false; unsigned int val = getNNodes(); if(result && fwrite(&val,sizeof(unsigned int),1,wf) != 1) result = false; if(result && fwrite(getTreeNodes(),sizeof(TreeNode),getNNodes(),wf) != getNNodes()) result = false; if(result && fwrite("TRIB",4,1,wf) != 1) result = false; size = sizeof(unsigned int)+ sizeof(TriangleBox)*getNTriangles(); if(result && fwrite(&size,4,1,wf) != 1) result = false; val = getNTriangles(); if(result && fwrite(&val,sizeof(unsigned int),1,wf) != 1) result = false; if(result && fwrite(getTriangles(),sizeof(TriangleBox),getNTriangles(),wf) != getNTriangles()) result = false; if(result && fwrite("SUBM",4,1,wf) != 1) result = false; size = sizeof(unsigned int)+ sizeof(SubModel)*iNSubModel; if(result && fwrite(&size,4,1,wf) != 1) result = false; if(result && fwrite(&iNSubModel,sizeof(unsigned int),1,wf) != 1) result = false; if(result && fwrite(iSubModel,sizeof(SubModel),iNSubModel,wf) != iNSubModel) result = false; fclose(wf); } return(result); }
UInt32 AdapterDrawAction::selectVisiblesGlobal (void) { if (!getFrustumCulling()) { return getNNodes(); } GLint l = 0; bool d = getVolumeDrawing(); if ( d ) { l = glIsEnabled(GL_LIGHTING); glDisable(GL_LIGHTING); } useNodeList(); UInt32 count = 0; for ( UInt32 i = 0; i < getNNodes(); ++i ) { if ( isVisible( getNode(i).getCPtr() ) ) { addNode( getNode(i) ); ++count; } if (d) { OSGCache::CacheData& data = OSGCache::the()[getNode(i)]; OSGCache::CacheData::AdapterContainer& all = data.getAdapter(BVolAdapterBase::getAdapterId()); // for BVolHierarchyBase there are more than one leaf nodes, then render // for SingleHierarchyBase there are more than one inner nodes, then skip if (all.size() > 1 && ((BVolAdapterBase*)all[0])->isInner()) { continue; } glColor3f(0,1,0); glPushMatrix(); glLoadIdentity(); getCamera()->setup(this, *getViewport()); //const Matrix& matrixI = data.getToWorldMatrix(); //glMultMatrixf(matrixI.getValues()); OSGCache::CacheData::AdapterContainer::const_iterator iter=all.begin(); for (; iter != all.end(); ++iter) { ((BVolAdapterBase*)*iter)->getBoundingVolume().drawWireframe(); } glPopMatrix(); } } if (l) { glEnable(GL_LIGHTING); } return count; }
unsigned Element::getNodeIndex(unsigned i) const { if (i<getNNodes()) return _nodes[i]->getID(); std::cerr << "Error in MeshLib::Element::getNodeIndex() - Index does not exist." << std::endl; return std::numeric_limits<unsigned>::max(); }
const Node* Element::getNode(unsigned i) const { if (i < getNNodes()) return _nodes[i]; std::cerr << "Error in MeshLib::Element::getNode() - Index " << i << " in " << MshElemType2String(getGeoType()) << " does not exist." << std::endl; return NULL; }
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)); }
bool SubModel::operator==(const SubModel& pSm2) const { bool result = false; if(getNNodes() == pSm2.getNNodes() && getNTriangles() == pSm2.getNTriangles() && getBasePosition() == pSm2.getBasePosition() && getNodesPos() == pSm2.getNodesPos() && getTrianglesPos() == pSm2.getTrianglesPos()) { result = true; } return result; }
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)); }
bool Tree::checkValid(bool verbose) { int node_count = 0; int tip_count = 0; if (root_node->anc != nullptr) { std::cout << "Invalid tree: Root node doesn't have null ancestor pointer\n"; return false; } bool status = checkTreeNode(root_node, node_count, tip_count, verbose); if (!status) { return status; } if (tip_count != getNTips()) { std::cout << "Invalid tree: Could not access all tips.\n"; return false; } if (node_count != getNNodes()) { std::cout << "Invalid tree: Could not access all nodes.\n"; return false; } return status; }
//void Tree::addTipNextTo(int id, int sibling_id) { // /* Check that parameters are valid */ // if (id > 0) { // if (id <= max_id) { // if (getNode(id, root_node) != nullptr) { // throw std::logic_error("Cannot add node with duplicate ID"); // } // } // } // else { // id = ++max_id; // } // auto sibling = getNode(sibling_id, root_node); // if (sibling != nullptr) { // throw std::logic_error("Cannot find sibling node"); // } // // addTip(id, sibling, nullptr); //} // void Tree::addTipFrom(int id, int anc_id) { /* Check that parameters are valid */ if (id > 0) { if (id <= max_id) { if (getNode(id, root_node) != nullptr) { throw std::logic_error("Cannot add node with duplicate ID"); } } } else { id = ++max_id; } /* anc_id of 0 indicates insertion of a root node into an empty tree */ if (anc_id == 0) { if (getNNodes() != 0) { throw std::logic_error("Cannot add root to non-empty tree"); } root_node = std::make_shared<Node>(id); } else { // auto ancestor = getNode(anc_id, ) } }
bool ModelContainer::readFile(const char *filename) { bool result = false; unsigned int flags; unsigned int size; char ident[8]; char chunk[4]; unsigned int ival; FILE *rf = fopen(filename, "rb"); if(rf) { free(); result = true; char magic[8]; // Ignore the added magic header fread(magic,1,8,rf); if(strncmp(VMAP_MAGIC,magic,8)) result = false; if(result && fread(ident,8,1,rf) != 1) result = false; if(result && fread(&flags,sizeof(unsigned int),1,rf) != 1) result = false; //POS if(result && fread(chunk,4,1,rf) != 1) result = false; if(result && fread(&size,4,1,rf) != 1) result = false; Vector3 basePos; if(result && fread(&basePos,sizeof(float),3,rf) != 3) result = false; setBasePosition(basePos); //---- Box if(result && fread(chunk,4,1,rf) != 1) result = false; if(result && fread(&size,4,1,rf) != 1) result = false; Vector3 low,high; if(result && fread(&low,sizeof(float),3,rf) != 3) result = false; if(result && fread(&high,sizeof(float),3,rf) != 3) result = false; setBounds(low, high); //---- TreeNodes if(result && fread(chunk,4,1,rf) != 1) result = false; if(result && fread(&size,4,1,rf) != 1) result = false; if(result && fread(&ival,sizeof(unsigned int),1,rf) != 1) result = false; if(result) setNNodes(ival); if(result) setTreeNodeArray(new TreeNode[getNNodes()]); if(result && fread(getTreeNodes(),sizeof(TreeNode),getNNodes(),rf) != getNNodes()) result = false; //---- TriangleBoxes if(result && fread(chunk,4,1,rf) != 1) result = false; if(result && fread(&size,4,1,rf) != 1) result = false; if(result && fread(&ival,sizeof(unsigned int),1,rf) != 1) result = false; setNTriangles(ival); if(result) setTriangleArray(new TriangleBox[getNTriangles()]); if(result && fread(getTriangles(),sizeof(TriangleBox),getNTriangles(),rf) != getNTriangles()) result = false; //---- SubModel if(result && fread(chunk,4,1,rf) != 1) result = false; if(result && fread(&size,4,1,rf) != 1) result = false; if(result && fread(&iNSubModel,sizeof(unsigned int),1,rf) != 1) result = false; if(result) iSubModel = new SubModel[iNSubModel]; if(result) { for (unsigned int i=0; i<iNSubModel && result; ++i) { unsigned char readBuffer[52]; // this is the size of SubModel on 32 bit systems if(fread(readBuffer,sizeof(readBuffer),1,rf) != 1) result = false; iSubModel[i].initFromBinBlock(readBuffer); iSubModel[i].setTriangleArray(getTriangles()); iSubModel[i].setTreeNodeArray(getTreeNodes()); } } fclose(rf); } return result; }
// 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]); } } }
void Element::setNode(unsigned idx, Node* node) { if (idx < getNNodes()) _nodes[idx] = node; }