Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a single tetrahedron mesh for testing 3D mesh code #65

Merged
merged 3 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/modmesh/mesh/interior.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ void StaticMeshBase<D, ND>::calc_metric()
{
for (size_t icl = 0 ; icl < ncell() ; ++icl)
{
if ((use_incenter()) && (m_cltpn(icl) == 5))
if ((use_incenter()) && (CellType::TETRAHEDRON == m_cltpn(icl)))
{
real_type voc = 0.0;
{
Expand Down
10 changes: 10 additions & 0 deletions include/modmesh/python/python.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,16 @@ WrapStaticMeshBase
// clang-format on

#undef MM_DECL_ARRAY

this->cls().attr("NONCELLTYPE") = uint8_t(CellType::NONCELLTYPE);
this->cls().attr("POINT") = uint8_t(CellType::POINT);
this->cls().attr("LINE") = uint8_t(CellType::LINE);
this->cls().attr("QUADRILATERAL") = uint8_t(CellType::QUADRILATERAL);
this->cls().attr("TRIANGLE") = uint8_t(CellType::TRIANGLE);
this->cls().attr("HEXAHEDRON") = uint8_t(CellType::HEXAHEDRON);
this->cls().attr("TETRAHEDRON") = uint8_t(CellType::TETRAHEDRON);
this->cls().attr("PRISM") = uint8_t(CellType::PRISM);
this->cls().attr("PYRAMID") = uint8_t(CellType::PYRAMID);
}

}; /* end class WrapStaticMeshBase */
Expand Down
128 changes: 113 additions & 15 deletions src/viewer/modmesh/viewer/PythonInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include <modmesh/viewer/RMainWindow.hpp>
#include <modmesh/viewer/RStaticMesh.hpp>
#include <modmesh/viewer/R3DWidget.hpp>

namespace modmesh
{
Expand Down Expand Up @@ -120,29 +121,126 @@ _set_modmesh_path())"""";
py::exec("modmesh.view = _modmesh_view");
}

namespace python
{

// clang-format off
class
MODMESH_PYTHON_WRAPPER_VISIBILITY
WrapR3DWidget
// clang-format on
: public WrapBase<WrapR3DWidget, R3DWidget>
{

friend root_base_type;

WrapR3DWidget(pybind11::module & mod, char const * pyname, char const * pydoc)
: root_base_type(mod, pyname, pydoc)
{

namespace py = pybind11;

#define DECL_QVECTOR3D_PROPERTY(NAME, GETTER, SETTER) \
.def_property( \
#NAME, \
[](wrapped_type & self) \
{ \
QVector3D const v = self.camera()->GETTER(); \
return py::make_tuple(v.x(), v.y(), v.z()); \
}, \
[](wrapped_type & self, std::vector<double> const & v) \
{ \
double const x = v.at(0); \
double const y = v.at(1); \
double const z = v.at(2); \
self.camera()->SETTER(QVector3D(x, y, z)); \
})

(*this)
DECL_QVECTOR3D_PROPERTY(position, position, setPosition)
DECL_QVECTOR3D_PROPERTY(up_vector, upVector, setUpVector)
DECL_QVECTOR3D_PROPERTY(view_center, viewCenter, setViewCenter)
//
;

#undef DECL_QVECTOR3D_PROPERTY
}

}; /* end class WrapR3DWidget */

class
MODMESH_PYTHON_WRAPPER_VISIBILITY
WrapRApplication
// clang-format on
: public WrapBase<WrapRApplication, RApplication>
{

friend root_base_type;

WrapRApplication(pybind11::module & mod, char const * pyname, char const * pydoc)
: root_base_type(mod, pyname, pydoc)
{

namespace py = pybind11;

(*this)
.def_property_readonly_static(
"instance",
[](py::object const &)
{
return RApplication::instance();
})
.def_property_readonly(
"viewer",
[](wrapped_type & self)
{
return self.main()->viewer();
})
//
;
}

}; /* end class WrapRApplication */

namespace detail
{

template <uint8_t ND>
static void update_appmesh(std::shared_ptr<StaticMesh<ND>> const & mesh)
{
RScene * scene = RApplication::instance()->main()->viewer()->scene();
for (Qt3DCore::QNode * child : scene->childNodes())
{
if (typeid(*child) == typeid(RStaticMesh))
{
child->deleteLater();
}
}
new RStaticMesh(mesh, scene);
}

} /* end namespace detail */

} /* end namespace python */

} /* end namespace modmesh */

PYBIND11_EMBEDDED_MODULE(_modmesh_view, mod)
{
using namespace modmesh;
using namespace modmesh::python;
namespace py = pybind11;

mod
.def(
"show",
[](std::shared_ptr<StaticMesh2d> const & mesh)
{
RApplication * app = RApplication::instance();
RScene * scene = app->main()->viewer()->scene();
for (Qt3DCore::QNode * child : scene->childNodes())
{
if (typeid(*child) == typeid(RStaticMesh<2>))
{
child->deleteLater();
}
}
new RStaticMesh<2>(mesh, scene);
});
.def("show", &modmesh::python::detail::update_appmesh<2>, py::arg("mesh"))
.def("show", &modmesh::python::detail::update_appmesh<3>, py::arg("mesh"))
//
;

WrapR3DWidget::commit(mod, "R3DWidget", "R3DWidget");
WrapRApplication::commit(mod, "RApplication", "RApplication");

mod.attr("app") = py::cast(RApplication::instance());
}

// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:
4 changes: 1 addition & 3 deletions src/viewer/modmesh/viewer/R3DWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
#include <modmesh/viewer/base.hpp> // Must be the first include.
#include <modmesh/viewer/R3DWidget.hpp>

#include <Qt3DRender/QCamera>

namespace modmesh
{

Expand All @@ -45,7 +43,7 @@ R3DWidget::R3DWidget(Qt3DExtras::Qt3DWindow * window, RScene * scene, QWidget *
// Set up the camera.
Qt3DRender::QCamera * camera = m_view->camera();
camera->lens()->setPerspectiveProjection(45.0f, 16.0f / 9.0f, 0.1f, 1000.0f);
camera->setPosition(QVector3D(0, 0, 40.0f));
camera->setPosition(QVector3D(0, 0, 10.0f));
camera->setViewCenter(QVector3D(0, 0, 0));

// Set up the camera control.
Expand Down
16 changes: 12 additions & 4 deletions src/viewer/modmesh/viewer/R3DWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <Qt3DWindow>

#include <Qt3DCore/QEntity>
#include <Qt3DRender/QCamera>

#include <QOrbitCameraController>

Expand All @@ -43,6 +44,12 @@
namespace modmesh
{

class RCameraController
: public Qt3DExtras::QOrbitCameraController
{
using Qt3DExtras::QOrbitCameraController::QOrbitCameraController;
}; /* end class RCameraController */

class RScene
: public Qt3DCore::QEntity
{
Expand All @@ -51,16 +58,16 @@ class RScene

RScene(Qt3DCore::QNode * parent = nullptr)
: Qt3DCore::QEntity(parent)
, m_controller(new Qt3DExtras::QOrbitCameraController(this))
, m_controller(new RCameraController(this))
{
}

Qt3DExtras::QOrbitCameraController const * controller() const { return m_controller; }
Qt3DExtras::QOrbitCameraController * controller() { return m_controller; }
RCameraController const * controller() const { return m_controller; }
RCameraController * controller() { return m_controller; }

private:

Qt3DExtras::QOrbitCameraController * m_controller = nullptr;
RCameraController * m_controller = nullptr;

}; /* end class RScene */

Expand All @@ -83,6 +90,7 @@ class R3DWidget

Qt3DExtras::Qt3DWindow * view() { return m_view; }
RScene * scene() { return m_scene; }
Qt3DRender::QCamera * camera() { return m_view->camera(); }

private:

Expand Down
33 changes: 25 additions & 8 deletions src/viewer/modmesh/viewer/RPythonText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,31 @@ void RPythonText::setUp()

m_text->setPlainText(QString(R""""(# Sample input
import modmesh as mm
mh = mm.StaticMesh2d(nnode=4, nface=0, ncell=3)
mh.ndcrd.ndarray[:, :] = (0, 0), (-1, -1), (1, -1), (0, 1)
mh.cltpn.ndarray[:] = 4
mh.clnds.ndarray[:, :4] = (3, 0, 1, 2), (3, 0, 2, 3), (3, 0, 3, 1)
mh.build_interior()
mh.build_boundary()
mh.build_ghost()
mm.view.show(mh))""""));
ndim = 2
if 2 == ndim:
mh = mm.StaticMesh2d(nnode=4, nface=0, ncell=3)
mh.ndcrd.ndarray[:, :] = (0, 0), (-1, -1), (1, -1), (0, 1)
mh.cltpn.ndarray[:] = modmesh.StaticMesh2d.TRIANGLE
mh.clnds.ndarray[:, :4] = (3, 0, 1, 2), (3, 0, 2, 3), (3, 0, 3, 1)
mh.build_interior()
mh.build_boundary()
mh.build_ghost()
else:
mh = modmesh.StaticMesh3d(nnode=4, nface=4, ncell=1)
mh.ndcrd.ndarray[:, :] = (0, 0, 0), (0, 1, 0), (-1, 1, 0), (0, 1, 1)
mh.cltpn.ndarray[:] = modmesh.StaticMesh3d.TETRAHEDRON
mh.clnds.ndarray[:, :5] = [(4, 0, 1, 2, 3)]
mh.build_interior()
mh.build_boundary()
mh.build_ghost()
#mm.view.app.viewer.up_vector = (0, 1, 0)
#mm.view.app.viewer.position = (-10, -10, -20)
#mm.view.app.viewer.view_center = (0, 0, 0)
mm.view.show(mh)
print("position:", mm.view.app.viewer.position)
print("up_vector:", mm.view.app.viewer.up_vector)
print("view_center:", mm.view.app.viewer.view_center)
)""""));
}

void RPythonText::runPythonCode()
Expand Down
53 changes: 20 additions & 33 deletions src/viewer/modmesh/viewer/RStaticMesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,53 +50,49 @@
namespace modmesh
{

template <uint8_t ND>
class RStaticMesh
: public Qt3DCore::QEntity
{

public:

using mesh_type = StaticMesh<ND>;

RStaticMesh(std::shared_ptr<mesh_type> const & static_mesh, Qt3DCore::QNode * parent = nullptr);

static Qt3DCore::QGeometry * make_geometry(mesh_type const & mh, Qt3DCore::QEntity * parent);
static Qt3DRender::QGeometryRenderer * make_renderer(Qt3DCore::QGeometry * geom);
template <uint8_t ND>
RStaticMesh(std::shared_ptr<StaticMesh<ND>> const & static_mesh, Qt3DCore::QNode * parent = nullptr);

mesh_type const & static_mesh() const { return *m_static_mesh; }
mesh_type & static_mesh() { return *m_static_mesh; }
template <uint8_t ND>
void update_geometry(StaticMesh<ND> const & mh)
{
update_geometry_impl(mh, m_geometry);
}

private:

std::shared_ptr<mesh_type> m_static_mesh = nullptr;
template <uint8_t ND>
static void update_geometry_impl(StaticMesh<ND> const & mh, Qt3DCore::QGeometry * geom);

Qt3DCore::QGeometry * m_geometry = nullptr;
Qt3DRender::QGeometryRenderer * m_renderer = nullptr;
Qt3DRender::QMaterial * m_material = nullptr;

}; /* end class RStaticMesh */

using RStaticMesh2d = RStaticMesh<2>;
using RStaticMesh3d = RStaticMesh<3>;

template <uint8_t ND>
RStaticMesh<ND>::RStaticMesh(std::shared_ptr<mesh_type> const & static_mesh, Qt3DCore::QNode * parent)
: QEntity(parent)
, m_static_mesh(static_mesh)
, m_geometry(make_geometry(*static_mesh, this))
, m_renderer(make_renderer(m_geometry))
RStaticMesh::RStaticMesh(std::shared_ptr<StaticMesh<ND>> const & static_mesh, Qt3DCore::QNode * parent)
: Qt3DCore::QEntity(parent)
, m_geometry(new Qt3DCore::QGeometry(this))
, m_renderer(new Qt3DRender::QGeometryRenderer())
, m_material(new Qt3DExtras::QDiffuseSpecularMaterial())
{
update_geometry(*static_mesh);
m_renderer->setGeometry(m_geometry);
m_renderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);
addComponent(m_renderer);
addComponent(m_material);
}

template <uint8_t ND>
Qt3DCore::QGeometry * RStaticMesh<ND>::make_geometry(mesh_type const & mh, Qt3DCore::QEntity * parent)
void RStaticMesh::update_geometry_impl(StaticMesh<ND> const & mh, Qt3DCore::QGeometry * geom)
{
auto * geom = new Qt3DCore::QGeometry(parent);

auto * buf = new Qt3DCore::QBuffer(geom);
{
QByteArray barray;
Expand All @@ -106,7 +102,7 @@ Qt3DCore::QGeometry * RStaticMesh<ND>::make_geometry(mesh_type const & mh, Qt3DC
{
*ptr++ = mh.ndcrd(ind, 0);
*ptr++ = mh.ndcrd(ind, 1);
*ptr++ = 0;
*ptr++ = (3 == ND) ? mh.ndcrd(ind, 2) : 0;
}
buf->setData(barray);
}
Expand All @@ -122,6 +118,8 @@ Qt3DCore::QGeometry * RStaticMesh<ND>::make_geometry(mesh_type const & mh, Qt3DC

geom->addAttribute(vertices);

// FIXME: This is naive implementation and creates a lot of duplicated
// edges. The StaticMesh should provide a set of unique edges.
uint32_t nend = 0;
for (uint32_t ifc = 0; ifc < mh.nface(); ++ifc)
{
Expand Down Expand Up @@ -150,17 +148,6 @@ Qt3DCore::QGeometry * RStaticMesh<ND>::make_geometry(mesh_type const & mh, Qt3DC
indices->setCount(nend);

geom->addAttribute(indices);

return geom;
}

template <uint8_t ND>
Qt3DRender::QGeometryRenderer * RStaticMesh<ND>::make_renderer(Qt3DCore::QGeometry * geom)
{
auto * renderer = new Qt3DRender::QGeometryRenderer();
renderer->setGeometry(geom);
renderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);
return renderer;
}

} /* end namespace modmesh */
Expand Down
Loading