Skip to content

Commit 4417485

Browse files
authored
Merge pull request #77 from yungyuc/feature/axis-mark
Add a simple axis marker in the view widget
2 parents 7fc445e + bd7c113 commit 4417485

File tree

6 files changed

+241
-11
lines changed

6 files changed

+241
-11
lines changed

cpp/binary/viewer/viewer.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,6 @@
3131

3232
#include <modmesh/view/view.hpp>
3333

34-
int main(int argc, char ** argv)
35-
{
36-
using namespace modmesh;
37-
38-
RApplication app(argc, argv);
39-
app.main()->resize(1000, 600);
40-
41-
return app.exec();
42-
}
43-
4434
namespace modmesh
4535
{
4636

@@ -50,6 +40,19 @@ namespace python
5040
namespace detail
5141
{
5242

43+
static void show_mark()
44+
{
45+
RScene * scene = RApplication::instance()->main()->viewer()->scene();
46+
for (Qt3DCore::QNode * child : scene->childNodes())
47+
{
48+
if (typeid(*child) == typeid(RAxisMark))
49+
{
50+
child->deleteLater();
51+
}
52+
}
53+
new RAxisMark(scene);
54+
}
55+
5356
static void update_appmesh(std::shared_ptr<StaticMesh> const & mesh)
5457
{
5558
RScene * scene = RApplication::instance()->main()->viewer()->scene();
@@ -77,6 +80,7 @@ PYBIND11_EMBEDDED_MODULE(_modmesh_view, mod)
7780

7881
mod
7982
.def("show", &modmesh::python::detail::update_appmesh, py::arg("mesh"))
83+
.def("showMark", &modmesh::python::detail::show_mark)
8084
//
8185
;
8286

@@ -85,4 +89,16 @@ PYBIND11_EMBEDDED_MODULE(_modmesh_view, mod)
8589
mod.attr("app") = py::cast(RApplication::instance());
8690
}
8791

92+
int main(int argc, char ** argv)
93+
{
94+
using namespace modmesh;
95+
96+
RApplication app(argc, argv);
97+
app.main()->resize(1000, 600);
98+
99+
python::detail::show_mark();
100+
101+
return app.exec();
102+
}
103+
88104
// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:

cpp/modmesh/view/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ cmake_minimum_required(VERSION 3.16)
66
set(MODMESH_VIEW_HEADERS
77
${CMAKE_CURRENT_SOURCE_DIR}/R3DWidget.hpp
88
${CMAKE_CURRENT_SOURCE_DIR}/RApplication.hpp
9+
${CMAKE_CURRENT_SOURCE_DIR}/RAxisMark.hpp
910
${CMAKE_CURRENT_SOURCE_DIR}/RMainWindow.hpp
1011
${CMAKE_CURRENT_SOURCE_DIR}/RPythonText.hpp
1112
${CMAKE_CURRENT_SOURCE_DIR}/RStaticMesh.hpp
@@ -16,6 +17,7 @@ set(MODMESH_VIEW_HEADERS
1617
set(MODMESH_VIEW_SOURCES
1718
${CMAKE_CURRENT_SOURCE_DIR}/R3DWidget.cpp
1819
${CMAKE_CURRENT_SOURCE_DIR}/RApplication.cpp
20+
${CMAKE_CURRENT_SOURCE_DIR}/RAxisMark.cpp
1921
${CMAKE_CURRENT_SOURCE_DIR}/RMainWindow.cpp
2022
${CMAKE_CURRENT_SOURCE_DIR}/RPythonText.cpp
2123
${CMAKE_CURRENT_SOURCE_DIR}/RStaticMesh.cpp

cpp/modmesh/view/RAxisMark.cpp

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (c) 2022, Yung-Yu Chen <[email protected]>
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
*
7+
* - Redistributions of source code must retain the above copyright notice,
8+
* this list of conditions and the following disclaimer.
9+
* - Redistributions in binary form must reproduce the above copyright notice,
10+
* this list of conditions and the following disclaimer in the documentation
11+
* and/or other materials provided with the distribution.
12+
* - Neither the name of the copyright holder nor the names of its contributors
13+
* may be used to endorse or promote products derived from this software
14+
* without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
#include <modmesh/view/common_detail.hpp> // Must be the first include.
30+
#include <modmesh/view/RAxisMark.hpp>
31+
32+
#include <modmesh/modmesh.hpp>
33+
34+
#include <Qt>
35+
#include <QWidget>
36+
#include <Qt3DWindow>
37+
38+
#include <QByteArray>
39+
#include <QGeometryRenderer>
40+
41+
#include <Qt3DCore/QBuffer>
42+
#include <Qt3DCore/QEntity>
43+
#include <Qt3DCore/QGeometry>
44+
#include <Qt3DCore/QAttribute>
45+
#include <Qt3DCore/QTransform>
46+
47+
#include <Qt3DExtras/QDiffuseSpecularMaterial>
48+
49+
namespace modmesh
50+
{
51+
52+
RLine::RLine(QVector3D const & v0, QVector3D const & v1, QColor const & color, Qt3DCore::QNode * parent)
53+
: Qt3DCore::QEntity(parent)
54+
, m_geometry(new Qt3DCore::QGeometry(this))
55+
, m_renderer(new Qt3DRender::QGeometryRenderer())
56+
, m_material(new Qt3DExtras::QDiffuseSpecularMaterial())
57+
{
58+
{
59+
auto * buf = new Qt3DCore::QBuffer(m_geometry);
60+
{
61+
QByteArray barray;
62+
barray.resize(3 * 2 * sizeof(float));
63+
float * ptr = reinterpret_cast<float *>(barray.data());
64+
ptr[0] = v0.x();
65+
ptr[1] = v0.y();
66+
ptr[2] = v0.z();
67+
ptr[3] = v1.x();
68+
ptr[4] = v1.y();
69+
ptr[5] = v1.z();
70+
buf->setData(barray);
71+
}
72+
73+
{
74+
auto * vertices = new Qt3DCore::QAttribute(m_geometry);
75+
vertices->setName(Qt3DCore::QAttribute::defaultPositionAttributeName());
76+
vertices->setAttributeType(Qt3DCore::QAttribute::VertexAttribute);
77+
vertices->setVertexBaseType(Qt3DCore::QAttribute::Float);
78+
vertices->setVertexSize(3);
79+
vertices->setBuffer(buf);
80+
vertices->setByteStride(3 * sizeof(float));
81+
vertices->setCount(2);
82+
m_geometry->addAttribute(vertices);
83+
}
84+
}
85+
86+
{
87+
auto * buf = new Qt3DCore::QBuffer(m_geometry);
88+
{
89+
QByteArray barray;
90+
barray.resize(2 * sizeof(uint32_t));
91+
auto * indices = reinterpret_cast<uint32_t *>(barray.data());
92+
indices[0] = 0;
93+
indices[1] = 1;
94+
buf->setData(barray);
95+
}
96+
97+
{
98+
auto * indices = new Qt3DCore::QAttribute(m_geometry);
99+
indices->setVertexBaseType(Qt3DCore::QAttribute::UnsignedInt);
100+
indices->setAttributeType(Qt3DCore::QAttribute::IndexAttribute);
101+
indices->setBuffer(buf);
102+
indices->setCount(2);
103+
m_geometry->addAttribute(indices);
104+
}
105+
}
106+
107+
m_renderer->setGeometry(m_geometry);
108+
m_renderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);
109+
addComponent(m_renderer);
110+
addComponent(m_material);
111+
m_material->setAmbient(color);
112+
}
113+
114+
RAxisMark::RAxisMark(Qt3DCore::QNode * parent)
115+
: Qt3DCore::QEntity(parent)
116+
, m_xmark(new RLine(QVector3D(0, 0, 0), QVector3D(1, 0, 0), QColor(255, 0, 0, 255), this))
117+
, m_ymark(new RLine(QVector3D(0, 0, 0), QVector3D(0, 1, 0), QColor(0, 255, 0, 255), this))
118+
, m_zmark(new RLine(QVector3D(0, 0, 0), QVector3D(0, 0, 1), QColor(0, 0, 255, 255), this))
119+
{
120+
}
121+
122+
} /* end namespace modmesh */
123+
124+
// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:

cpp/modmesh/view/RAxisMark.hpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#pragma once
2+
3+
/*
4+
* Copyright (c) 2022, Yung-Yu Chen <[email protected]>
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* - Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
* - Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
* - Neither the name of the copyright holder nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
16+
* without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
#include <modmesh/view/common_detail.hpp> // Must be the first include.
32+
33+
#include <modmesh/modmesh.hpp>
34+
35+
#include <Qt>
36+
#include <QWidget>
37+
#include <Qt3DWindow>
38+
39+
#include <QByteArray>
40+
#include <QGeometryRenderer>
41+
42+
#include <Qt3DCore/QBuffer>
43+
#include <Qt3DCore/QEntity>
44+
#include <Qt3DCore/QGeometry>
45+
#include <Qt3DCore/QAttribute>
46+
#include <Qt3DCore/QTransform>
47+
48+
#include <Qt3DExtras/QDiffuseSpecularMaterial>
49+
50+
namespace modmesh
51+
{
52+
53+
class RLine
54+
: public Qt3DCore::QEntity
55+
{
56+
57+
public:
58+
59+
RLine(QVector3D const & v0, QVector3D const & v1, QColor const & color, Qt3DCore::QNode * parent = nullptr);
60+
61+
private:
62+
63+
Qt3DCore::QGeometry * m_geometry = nullptr;
64+
Qt3DRender::QGeometryRenderer * m_renderer = nullptr;
65+
Qt3DExtras::QDiffuseSpecularMaterial * m_material = nullptr;
66+
67+
}; /* end class RLine */
68+
69+
class RAxisMark
70+
: public Qt3DCore::QEntity
71+
{
72+
73+
public:
74+
75+
RAxisMark(Qt3DCore::QNode * parent = nullptr);
76+
77+
private:
78+
79+
RLine * m_xmark = nullptr;
80+
RLine * m_ymark = nullptr;
81+
RLine * m_zmark = nullptr;
82+
83+
}; /* end class RAxisMark */
84+
85+
} /* end namespace modmesh */
86+
87+
// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:

cpp/modmesh/view/RPythonText.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def make_3d():
8787
8888
mh = make_2d()
8989
mm.view.show(mh)
90-
mm.view.show(mh)
90+
9191
print("position:", mm.view.app.viewer.position)
9292
print("up_vector:", mm.view.app.viewer.up_vector)
9393
print("view_center:", mm.view.app.viewer.view_center)

cpp/modmesh/view/view.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <modmesh/view/RMainWindow.hpp>
3636
#include <modmesh/view/RPythonText.hpp>
3737
#include <modmesh/view/RStaticMesh.hpp>
38+
#include <modmesh/view/RAxisMark.hpp>
3839

3940
namespace modmesh
4041
{

0 commit comments

Comments
 (0)