Skip to content
Snippets Groups Projects
Commit 7a385f6c authored by Norihiro Watanabe's avatar Norihiro Watanabe Committed by Norihiro Watanabe
Browse files

[Mesh] add ConvertToLinearMesh

parent a26f38ab
No related branches found
No related tags found
Loading
/**
* \copyright
* Copyright (c) 2012-2016, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*/
#include "ConvertToLinearMesh.h"
#include "MeshLib/Elements/Element.h"
#include "MeshLib/Elements/Line.h"
#include "MeshLib/Elements/Quad.h"
#include "MeshLib/Elements/Utils.h"
#include "MeshLib/Mesh.h"
#include "MeshLib/MeshEditing/DuplicateMeshComponents.h"
#include "MeshLib/Node.h"
#include "MeshLib/Properties.h"
#include "MeshLib/PropertyVector.h"
namespace MeshLib
{
namespace
{
template <typename T_ELEMENT>
T_ELEMENT* createLinearElement(MeshLib::Element const* e,
std::vector<MeshLib::Node*> const& vec_new_nodes)
{
auto const n_base_nodes = T_ELEMENT::n_base_nodes;
MeshLib::Node** nodes = new MeshLib::Node*[n_base_nodes];
for (unsigned i=0; i<e->getNumberOfBaseNodes(); i++)
nodes[i] = const_cast<MeshLib::Node*>(vec_new_nodes[e->getNode(i)->getID()]);
return new T_ELEMENT(nodes);
}
} // unnamed namespace
std::unique_ptr<MeshLib::Mesh> convertToLinearMesh(MeshLib::Mesh const& org_mesh, std::string const& new_mesh_name)
{
std::vector<MeshLib::Node*> vec_new_nodes = MeshLib::copyNodeVector(MeshLib::getBaseNodes(org_mesh.getElements()));
// create new elements with the quadratic nodes
std::vector<MeshLib::Element*> vec_new_eles;
for (MeshLib::Element const* e : org_mesh.getElements())
{
if (e->getCellType() == MeshLib::CellType::LINE3)
{
vec_new_eles.push_back(createLinearElement<MeshLib::Line>(
e, vec_new_nodes));
}
else if (e->getCellType() == MeshLib::CellType::QUAD8)
{
vec_new_eles.push_back(createLinearElement<MeshLib::Quad>(
e, vec_new_nodes));
}
else
{
OGS_FATAL("Mesh element type %s is not supported", MeshLib::CellType2String(e->getCellType()).c_str());
}
}
std::unique_ptr<MeshLib::Mesh> new_mesh(
new MeshLib::Mesh(new_mesh_name, vec_new_nodes, vec_new_eles,
org_mesh.getProperties().excludeCopyProperties(
std::vector<MeshLib::MeshItemType>(
1, MeshLib::MeshItemType::Node))));
MeshLib::Properties const& src_properties = org_mesh.getProperties();
for (auto name : src_properties.getPropertyVectorNames())
{
auto const* src_prop = src_properties.getPropertyVector<double>(name);
if (!src_prop)
continue;
if (src_prop->getMeshItemType() != MeshLib::MeshItemType::Node)
continue;
auto const n_src_comp = src_prop->getNumberOfComponents();
// convert 2D vector to 3D. Otherwise Paraview Calculator filter does not recognize
// it as a vector
auto const n_dest_comp = (n_src_comp==2) ? 3 : n_src_comp;
auto new_prop =
new_mesh->getProperties().createNewPropertyVector<double>(
name, MeshLib::MeshItemType::Node, n_dest_comp);
new_prop->resize(new_mesh->getNumberOfNodes() * n_dest_comp);
// copy existing
for (unsigned i=0; i<org_mesh.getNumberOfBaseNodes(); i++)
{
for (unsigned j=0; j<n_src_comp; j++)
(*new_prop)[i*n_dest_comp+j] = (*src_prop)[i*n_src_comp+j];
// set zero for components not existing in the original
for (unsigned j=n_src_comp; j<n_dest_comp; j++)
(*new_prop)[i*n_dest_comp+j] = 0;
}
}
return new_mesh;
}
} // end namespace MeshLib
/**
* \copyright
* Copyright (c) 2012-2016, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*/
#ifndef MESHLIB_CONVERRTTOLINEARMESH_H_
#define MESHLIB_CONVERRTTOLINEARMESH_H_
#include <memory>
#include <string>
namespace MeshLib
{
class Mesh;
std::unique_ptr<MeshLib::Mesh> convertToLinearMesh(
const MeshLib::Mesh& mesh, const std::string& new_mesh_name);
} // end namespace MeshLib
#endif //MESHLIB_CONVERRTTOLINEARMESH_H_
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment