CAM: use QOpenGLWidget for new simulator
This commit is contained in:
@@ -38,12 +38,12 @@ TYPESYSTEM_SOURCE(CAMSimulator::CAMSim, Base::BaseClass);
|
||||
|
||||
void CAMSim::BeginSimulation(const Part::TopoShape& stock, float quality)
|
||||
{
|
||||
DlgCAMSimulator::GetInstance()->startSimulation(stock, quality);
|
||||
DlgCAMSimulator::instance()->startSimulation(stock, quality);
|
||||
}
|
||||
|
||||
void CAMSimulator::CAMSim::resetSimulation()
|
||||
{
|
||||
DlgCAMSimulator::GetInstance()->resetSimulation();
|
||||
DlgCAMSimulator::instance()->resetSimulation();
|
||||
}
|
||||
|
||||
void CAMSim::addTool(
|
||||
@@ -53,7 +53,7 @@ void CAMSim::addTool(
|
||||
float resolution
|
||||
)
|
||||
{
|
||||
DlgCAMSimulator::GetInstance()->addTool(toolProfilePoints, toolNumber, diameter, resolution);
|
||||
DlgCAMSimulator::instance()->addTool(toolProfilePoints, toolNumber, diameter, resolution);
|
||||
}
|
||||
|
||||
void CAMSimulator::CAMSim::SetBaseShape(const Part::TopoShape& baseShape, float resolution)
|
||||
@@ -62,11 +62,11 @@ void CAMSimulator::CAMSim::SetBaseShape(const Part::TopoShape& baseShape, float
|
||||
return;
|
||||
}
|
||||
|
||||
DlgCAMSimulator::GetInstance()->SetBaseShape(baseShape, resolution);
|
||||
DlgCAMSimulator::instance()->setBaseShape(baseShape, resolution);
|
||||
}
|
||||
|
||||
void CAMSim::AddCommand(Command* cmd)
|
||||
{
|
||||
std::string gline = cmd->toGCode();
|
||||
DlgCAMSimulator::GetInstance()->addGcodeCommand(gline.c_str());
|
||||
DlgCAMSimulator::instance()->addGcodeCommand(gline.c_str());
|
||||
}
|
||||
|
||||
@@ -32,8 +32,6 @@
|
||||
#include <QPoint>
|
||||
#include <App/Document.h>
|
||||
|
||||
QOpenGLContext* gOpenGlContext;
|
||||
|
||||
using namespace MillSim;
|
||||
|
||||
namespace CAMSimulator
|
||||
@@ -41,99 +39,80 @@ namespace CAMSimulator
|
||||
|
||||
static const float MouseScrollDelta = 120.0F;
|
||||
|
||||
DlgCAMSimulator::DlgCAMSimulator(QWindow* parent)
|
||||
: QWindow(parent)
|
||||
DlgCAMSimulator::DlgCAMSimulator(QWidget* parent)
|
||||
: QOpenGLWidget(parent)
|
||||
{
|
||||
setSurfaceType(QWindow::OpenGLSurface);
|
||||
mMillSimulator = new MillSimulation();
|
||||
QSurfaceFormat format;
|
||||
format.setVersion(4, 1); // Request OpenGL 4.1 - for MacOS
|
||||
format.setProfile(QSurfaceFormat::CoreProfile); // Use the core profile = for MacOS
|
||||
int samples = Gui::View3DInventorViewer::getNumSamples();
|
||||
if (samples > 1) {
|
||||
format.setSamples(samples);
|
||||
}
|
||||
format.setSwapInterval(2);
|
||||
format.setDepthBufferSize(24);
|
||||
format.setStencilBufferSize(8);
|
||||
setFormat(format);
|
||||
|
||||
setMouseTracking(true);
|
||||
|
||||
mMillSimulator.reset(new MillSimulation());
|
||||
}
|
||||
|
||||
DlgCAMSimulator::~DlgCAMSimulator()
|
||||
{
|
||||
delete mMillSimulator;
|
||||
// nothing to do but need to keep this destructor because of forward declared MillSimulation
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::render(QPainter* painter)
|
||||
DlgCAMSimulator* DlgCAMSimulator::instance()
|
||||
{
|
||||
Q_UNUSED(painter);
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::render()
|
||||
{
|
||||
mMillSimulator->ProcessSim((unsigned int)(QDateTime::currentMSecsSinceEpoch()));
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::renderLater()
|
||||
{
|
||||
requestUpdate();
|
||||
}
|
||||
|
||||
bool DlgCAMSimulator::event(QEvent* event)
|
||||
{
|
||||
switch (event->type()) {
|
||||
case QEvent::UpdateRequest:
|
||||
renderNow();
|
||||
return true;
|
||||
default:
|
||||
break;
|
||||
if (mInstance == nullptr) {
|
||||
mInstance = new DlgCAMSimulator();
|
||||
mInstance->resize(MillSim::gWindowSizeW, MillSim::gWindowSizeH);
|
||||
mInstance->setWindowModality(Qt::ApplicationModal);
|
||||
mInstance->setMinimumWidth(700);
|
||||
mInstance->setMinimumHeight(400);
|
||||
}
|
||||
return QWindow::event(event);
|
||||
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::exposeEvent(QExposeEvent* event)
|
||||
void DlgCAMSimulator::setAnimating(bool animating)
|
||||
{
|
||||
Q_UNUSED(event);
|
||||
mAnimating = animating;
|
||||
|
||||
if (isExposed()) {
|
||||
renderNow();
|
||||
if (mAnimating) {
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::mouseMoveEvent(QMouseEvent* ev)
|
||||
void DlgCAMSimulator::startSimulation(const Part::TopoShape& stock, float quality)
|
||||
{
|
||||
int modifiers = (ev->modifiers() & Qt::ShiftModifier) != 0 ? MS_KBD_SHIFT : 0;
|
||||
modifiers |= (ev->modifiers() & Qt::ControlModifier) != 0 ? MS_KBD_CONTROL : 0;
|
||||
modifiers |= (ev->modifiers() & Qt::AltModifier) != 0 ? MS_KBD_ALT : 0;
|
||||
App::Document* doc = App::GetApplication().getActiveDocument();
|
||||
setWindowTitle(tr("%1 - New CAM Simulator").arg(QString::fromUtf8(doc->getName())));
|
||||
|
||||
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
||||
QPoint pnt = ev->pos();
|
||||
#else
|
||||
QPoint pnt = ev->position().toPoint();
|
||||
#endif
|
||||
mMillSimulator->MouseMove(pnt.x(), pnt.y(), modifiers);
|
||||
}
|
||||
mQuality = quality;
|
||||
mNeedsInitialize = true;
|
||||
|
||||
void DlgCAMSimulator::mousePressEvent(QMouseEvent* ev)
|
||||
{
|
||||
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
||||
QPoint pnt = ev->pos();
|
||||
#else
|
||||
QPoint pnt = ev->position().toPoint();
|
||||
#endif
|
||||
mMillSimulator->MousePress(ev->button(), true, pnt.x(), pnt.y());
|
||||
}
|
||||
setStockShape(stock, 1);
|
||||
|
||||
void DlgCAMSimulator::mouseReleaseEvent(QMouseEvent* ev)
|
||||
{
|
||||
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
||||
QPoint pnt = ev->pos();
|
||||
#else
|
||||
QPoint pnt = ev->position().toPoint();
|
||||
#endif
|
||||
mMillSimulator->MousePress(ev->button(), false, pnt.x(), pnt.y());
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::wheelEvent(QWheelEvent* ev)
|
||||
{
|
||||
mMillSimulator->MouseScroll((float)ev->angleDelta().y() / MouseScrollDelta);
|
||||
show();
|
||||
setAnimating(true);
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::resetSimulation()
|
||||
{}
|
||||
{
|
||||
mNeedsClear = true;
|
||||
|
||||
mGCode.clear();
|
||||
mTools.clear();
|
||||
mStock = {};
|
||||
mBase = {};
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::addGcodeCommand(const char* cmd)
|
||||
{
|
||||
mMillSimulator->AddGcodeLine(cmd);
|
||||
mGCode.push_back(cmd);
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::addTool(
|
||||
@@ -144,45 +123,16 @@ void DlgCAMSimulator::addTool(
|
||||
)
|
||||
{
|
||||
Q_UNUSED(resolution)
|
||||
|
||||
std::string toolCmd = "T" + std::to_string(toolNumber);
|
||||
mMillSimulator->AddGcodeLine(toolCmd.c_str());
|
||||
if (!mMillSimulator->ToolExists(toolNumber)) {
|
||||
mMillSimulator->AddTool(toolProfilePoints, toolNumber, diameter);
|
||||
}
|
||||
addGcodeCommand(toolCmd.c_str());
|
||||
mTools.emplace_back(toolProfilePoints, toolNumber, diameter);
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::hideEvent(QHideEvent* ev)
|
||||
static SimShape getMeshData(const Part::TopoShape& tshape, float resolution)
|
||||
{
|
||||
mMillSimulator->Clear();
|
||||
doGlCleanup();
|
||||
mAnimating = false;
|
||||
QWindow::hideEvent(ev);
|
||||
close();
|
||||
mInstance = nullptr;
|
||||
}
|
||||
SimShape ret;
|
||||
|
||||
void DlgCAMSimulator::resizeEvent(QResizeEvent* event)
|
||||
{
|
||||
if (!mContext) {
|
||||
return;
|
||||
}
|
||||
QSize newSize = event->size();
|
||||
int newWidth = newSize.width();
|
||||
int newHeight = newSize.height();
|
||||
if (mMillSimulator != nullptr) {
|
||||
mMillSimulator->UpdateWindowScale(newWidth, newHeight);
|
||||
}
|
||||
const qreal retinaScale = devicePixelRatio();
|
||||
glViewport(0, 0, (int)(newWidth * retinaScale), (int)(newHeight * retinaScale));
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::GetMeshData(
|
||||
const Part::TopoShape& tshape,
|
||||
float resolution,
|
||||
std::vector<Vertex>& verts,
|
||||
std::vector<GLushort>& indices
|
||||
)
|
||||
{
|
||||
std::vector<int> normalCount;
|
||||
int nVerts = 0;
|
||||
for (auto& shape : tshape.getSubTopoShapes(TopAbs_FACE)) {
|
||||
@@ -195,9 +145,9 @@ void DlgCAMSimulator::GetMeshData(
|
||||
|
||||
// copy triangle indices and calculate normals
|
||||
for (auto face : facets) {
|
||||
indices.push_back(face.I1 + nVerts);
|
||||
indices.push_back(face.I2 + nVerts);
|
||||
indices.push_back(face.I3 + nVerts);
|
||||
ret.indices.push_back(face.I1 + nVerts);
|
||||
ret.indices.push_back(face.I2 + nVerts);
|
||||
ret.indices.push_back(face.I3 + nVerts);
|
||||
|
||||
// calculate normal
|
||||
Base::Vector3d vAB = points[face.I2] - points[face.I1];
|
||||
@@ -219,143 +169,145 @@ void DlgCAMSimulator::GetMeshData(
|
||||
Base::Vector3d& normal = normals[i];
|
||||
int count = normalCount[i];
|
||||
normal /= count;
|
||||
verts.push_back(Vertex(point.x, point.y, point.z, normal.x, normal.y, normal.z));
|
||||
ret.verts.push_back(Vertex(point.x, point.y, point.z, normal.x, normal.y, normal.z));
|
||||
}
|
||||
|
||||
nVerts = verts.size();
|
||||
}
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::startSimulation(const Part::TopoShape& stock, float quality)
|
||||
{
|
||||
mQuality = quality;
|
||||
mNeedsInitialize = true;
|
||||
show();
|
||||
checkInitialization();
|
||||
SetStockShape(stock, 1);
|
||||
setAnimating(true);
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::initialize()
|
||||
{
|
||||
const qreal retinaScale = devicePixelRatio();
|
||||
mMillSimulator->InitSimulation(mQuality, retinaScale);
|
||||
|
||||
glViewport(0, 0, (int)(width() * retinaScale), (int)(height() * retinaScale));
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::checkInitialization()
|
||||
{
|
||||
if (!mContext) {
|
||||
mLastContext = QOpenGLContext::currentContext();
|
||||
mContext = new QOpenGLContext(this);
|
||||
mContext->setFormat(requestedFormat());
|
||||
mContext->create();
|
||||
QSurfaceFormat format;
|
||||
format.setSamples(16);
|
||||
format.setSwapInterval(2);
|
||||
mContext->setFormat(format);
|
||||
gOpenGlContext = mContext;
|
||||
mNeedsInitialize = true;
|
||||
nVerts = ret.verts.size();
|
||||
}
|
||||
|
||||
mContext->makeCurrent(this);
|
||||
ret.needsUpdate = true;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::setStockShape(const Part::TopoShape& shape, float resolution)
|
||||
{
|
||||
mStock = getMeshData(shape, resolution);
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::setBaseShape(const Part::TopoShape& tshape, float resolution)
|
||||
{
|
||||
mBase = getMeshData(tshape, resolution);
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::mouseMoveEvent(QMouseEvent* ev)
|
||||
{
|
||||
int modifiers = (ev->modifiers() & Qt::ShiftModifier) != 0 ? MS_KBD_SHIFT : 0;
|
||||
modifiers |= (ev->modifiers() & Qt::ControlModifier) != 0 ? MS_KBD_CONTROL : 0;
|
||||
modifiers |= (ev->modifiers() & Qt::AltModifier) != 0 ? MS_KBD_ALT : 0;
|
||||
|
||||
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
||||
QPoint pnt = ev->pos();
|
||||
#else
|
||||
QPoint pnt = ev->position().toPoint();
|
||||
#endif
|
||||
|
||||
const qreal ratio = devicePixelRatio();
|
||||
mMillSimulator->MouseMove(pnt.x() * ratio, pnt.y() * ratio, modifiers);
|
||||
|
||||
update();
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::mousePressEvent(QMouseEvent* ev)
|
||||
{
|
||||
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
||||
QPoint pnt = ev->pos();
|
||||
#else
|
||||
QPoint pnt = ev->position().toPoint();
|
||||
#endif
|
||||
|
||||
const qreal ratio = devicePixelRatio();
|
||||
mMillSimulator->MousePress(ev->button(), true, pnt.x() * ratio, pnt.y() * ratio);
|
||||
|
||||
update();
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::mouseReleaseEvent(QMouseEvent* ev)
|
||||
{
|
||||
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
||||
QPoint pnt = ev->pos();
|
||||
#else
|
||||
QPoint pnt = ev->position().toPoint();
|
||||
#endif
|
||||
|
||||
const qreal ratio = devicePixelRatio();
|
||||
mMillSimulator->MousePress(ev->button(), false, pnt.x() * ratio, pnt.y() * ratio);
|
||||
|
||||
update();
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::wheelEvent(QWheelEvent* ev)
|
||||
{
|
||||
mMillSimulator->MouseScroll((float)ev->angleDelta().y() / MouseScrollDelta);
|
||||
|
||||
update();
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::updateResources()
|
||||
{
|
||||
// clear simulator
|
||||
|
||||
if (mNeedsClear) {
|
||||
mMillSimulator->Clear();
|
||||
mNeedsClear = false;
|
||||
}
|
||||
|
||||
// update gcode
|
||||
|
||||
for (const auto& cmd : mGCode) {
|
||||
mMillSimulator->AddGcodeLine(cmd.c_str());
|
||||
}
|
||||
|
||||
mGCode.clear();
|
||||
|
||||
// update tools
|
||||
|
||||
for (const auto& tool : mTools) {
|
||||
if (!mMillSimulator->ToolExists(tool.id)) {
|
||||
mMillSimulator->AddTool(tool.profile, tool.id, tool.diameter);
|
||||
}
|
||||
}
|
||||
|
||||
mTools.clear();
|
||||
|
||||
// initialize simulator
|
||||
|
||||
if (mNeedsInitialize) {
|
||||
initializeOpenGLFunctions();
|
||||
initialize();
|
||||
mMillSimulator->InitSimulation(mQuality);
|
||||
mNeedsInitialize = false;
|
||||
}
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::doGlCleanup()
|
||||
{
|
||||
if (mLastContext != nullptr) {
|
||||
mLastContext->makeCurrent(this);
|
||||
// update stock and base
|
||||
|
||||
if (mStock.needsUpdate) {
|
||||
mMillSimulator->SetArbitraryStock(mStock.verts, mStock.indices);
|
||||
mStock = {};
|
||||
}
|
||||
if (mContext != nullptr) {
|
||||
mContext->deleteLater();
|
||||
mContext = nullptr;
|
||||
|
||||
if (mBase.needsUpdate) {
|
||||
mMillSimulator->SetBaseObject(mBase.verts, mBase.indices);
|
||||
mBase = {};
|
||||
}
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::renderNow()
|
||||
void DlgCAMSimulator::initializeGL()
|
||||
{
|
||||
static unsigned int lastTime = 0;
|
||||
static int frameCount = 0;
|
||||
static int fps = 0;
|
||||
if (!isExposed()) {
|
||||
return;
|
||||
}
|
||||
initializeOpenGLFunctions();
|
||||
}
|
||||
|
||||
checkInitialization();
|
||||
|
||||
frameCount++;
|
||||
unsigned int curtime = QDateTime::currentMSecsSinceEpoch();
|
||||
unsigned int timediff = curtime - lastTime;
|
||||
if (timediff > 10000) {
|
||||
fps = frameCount * 1000 / timediff; // for debug only. not used otherwise.
|
||||
lastTime = curtime;
|
||||
frameCount = 0;
|
||||
}
|
||||
render();
|
||||
mContext->swapBuffers(this);
|
||||
void DlgCAMSimulator::paintGL()
|
||||
{
|
||||
updateResources();
|
||||
mMillSimulator->ProcessSim((unsigned int)(QDateTime::currentMSecsSinceEpoch()));
|
||||
|
||||
if (mAnimating) {
|
||||
renderLater();
|
||||
}
|
||||
(void)fps;
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::setAnimating(bool animating)
|
||||
{
|
||||
mAnimating = animating;
|
||||
|
||||
if (animating) {
|
||||
renderLater();
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
DlgCAMSimulator* DlgCAMSimulator::GetInstance()
|
||||
void DlgCAMSimulator::resizeGL(int w, int h)
|
||||
{
|
||||
if (mInstance == nullptr) {
|
||||
QSurfaceFormat format;
|
||||
format.setVersion(4, 1); // Request OpenGL 4.1 - for MacOS
|
||||
format.setProfile(QSurfaceFormat::CoreProfile); // Use the core profile = for MacOS
|
||||
int samples = Gui::View3DInventorViewer::getNumSamples();
|
||||
if (samples > 1) {
|
||||
format.setSamples(samples);
|
||||
}
|
||||
format.setSwapInterval(2);
|
||||
format.setDepthBufferSize(24);
|
||||
format.setStencilBufferSize(8);
|
||||
mInstance = new DlgCAMSimulator();
|
||||
mInstance->setFormat(format);
|
||||
mInstance->resize(MillSim::gWindowSizeW, MillSim::gWindowSizeH);
|
||||
mInstance->setModality(Qt::ApplicationModal);
|
||||
mInstance->setMinimumWidth(700);
|
||||
mInstance->setMinimumHeight(400);
|
||||
|
||||
App::Document* doc = App::GetApplication().getActiveDocument();
|
||||
mInstance->setTitle(tr("%1 - New CAM Simulator").arg(QString::fromUtf8(doc->getName())));
|
||||
}
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::SetStockShape(const Part::TopoShape& shape, float resolution)
|
||||
{
|
||||
std::vector<Vertex> verts;
|
||||
std::vector<GLushort> indices;
|
||||
GetMeshData(shape, resolution, verts, indices);
|
||||
mMillSimulator->SetArbitraryStock(verts, indices);
|
||||
}
|
||||
|
||||
void DlgCAMSimulator::SetBaseShape(const Part::TopoShape& tshape, float resolution)
|
||||
{
|
||||
std::vector<Vertex> verts;
|
||||
std::vector<GLushort> indices;
|
||||
GetMeshData(tshape, resolution, verts, indices);
|
||||
mMillSimulator->SetBaseObject(verts, indices);
|
||||
const qreal ratio = devicePixelRatio();
|
||||
mMillSimulator->UpdateWindowScale(w * ratio, h * ratio);
|
||||
}
|
||||
|
||||
DlgCAMSimulator* DlgCAMSimulator::mInstance = nullptr;
|
||||
@@ -363,6 +315,7 @@ DlgCAMSimulator* DlgCAMSimulator::mInstance = nullptr;
|
||||
//************************************************************************************************************
|
||||
// stock
|
||||
//************************************************************************************************************
|
||||
|
||||
SimStock::SimStock(float px, float py, float pz, float lx, float ly, float lz, float res)
|
||||
: mPx(px)
|
||||
, mPy(py)
|
||||
@@ -374,7 +327,4 @@ SimStock::SimStock(float px, float py, float pz, float lx, float ly, float lz, f
|
||||
(void)res;
|
||||
}
|
||||
|
||||
SimStock::~SimStock()
|
||||
{}
|
||||
|
||||
} // namespace CAMSimulator
|
||||
|
||||
@@ -29,8 +29,11 @@
|
||||
# pragma warning(disable : 4251)
|
||||
#endif
|
||||
|
||||
#include <queue>
|
||||
#include <functional>
|
||||
|
||||
#include <Mod/Part/App/TopoShape.h>
|
||||
#include <QWindow>
|
||||
#include <QOpenGLWidget>
|
||||
#include <QOpenGLExtraFunctions>
|
||||
#include <QPainter>
|
||||
#include <QExposeEvent>
|
||||
@@ -52,34 +55,43 @@ struct SimStock
|
||||
{
|
||||
public:
|
||||
SimStock(float px, float py, float pz, float lx, float ly, float lz, float res);
|
||||
~SimStock();
|
||||
|
||||
public:
|
||||
float mPx, mPy, mPz; // stock zero position
|
||||
float mLx, mLy, mLz; // stock dimensions
|
||||
};
|
||||
|
||||
class DlgCAMSimulator: public QWindow, public QOpenGLExtraFunctions
|
||||
struct SimShape
|
||||
{
|
||||
public:
|
||||
std::vector<MillSim::Vertex> verts;
|
||||
std::vector<GLushort> indices;
|
||||
bool needsUpdate = false;
|
||||
};
|
||||
|
||||
struct SimTool
|
||||
{
|
||||
public:
|
||||
std::vector<float> profile;
|
||||
int id;
|
||||
float diameter;
|
||||
float resolution;
|
||||
};
|
||||
|
||||
class DlgCAMSimulator: public QOpenGLWidget, public QOpenGLExtraFunctions
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit DlgCAMSimulator(QWindow* parent = nullptr);
|
||||
explicit DlgCAMSimulator(QWidget* parent = nullptr);
|
||||
~DlgCAMSimulator() override;
|
||||
|
||||
virtual void render(QPainter* painter);
|
||||
virtual void render();
|
||||
virtual void initialize();
|
||||
static DlgCAMSimulator* instance();
|
||||
|
||||
void setAnimating(bool animating);
|
||||
static DlgCAMSimulator* GetInstance();
|
||||
void SetStockShape(const Part::TopoShape& tshape, float resolution);
|
||||
void SetBaseShape(const Part::TopoShape& tshape, float resolution);
|
||||
|
||||
public: // slots:
|
||||
void renderLater();
|
||||
void renderNow();
|
||||
void startSimulation(const Part::TopoShape& stock, float quality);
|
||||
void resetSimulation();
|
||||
|
||||
void addGcodeCommand(const char* cmd);
|
||||
void addTool(
|
||||
const std::vector<float>& toolProfilePoints,
|
||||
@@ -88,37 +100,36 @@ public: // slots:
|
||||
float resolution
|
||||
);
|
||||
|
||||
void setStockShape(const Part::TopoShape& tshape, float resolution);
|
||||
void setBaseShape(const Part::TopoShape& tshape, float resolution);
|
||||
|
||||
protected:
|
||||
bool event(QEvent* event) override;
|
||||
void checkInitialization();
|
||||
void doGlCleanup();
|
||||
void exposeEvent(QExposeEvent* event) override;
|
||||
void mouseMoveEvent(QMouseEvent* ev) override;
|
||||
void mousePressEvent(QMouseEvent* ev) override;
|
||||
void mouseReleaseEvent(QMouseEvent* ev) override;
|
||||
void wheelEvent(QWheelEvent* ev) override;
|
||||
void hideEvent(QHideEvent* ev) override;
|
||||
void resizeEvent(QResizeEvent* event) override;
|
||||
void GetMeshData(
|
||||
const Part::TopoShape& tshape,
|
||||
float resolution,
|
||||
std::vector<MillSim::Vertex>& verts,
|
||||
std::vector<GLushort>& indices
|
||||
);
|
||||
|
||||
void updateResources();
|
||||
|
||||
void initializeGL() override;
|
||||
void paintGL() override;
|
||||
void resizeGL(int w, int h) override;
|
||||
|
||||
private:
|
||||
bool mAnimating = false;
|
||||
bool mNeedsInitialize = false;
|
||||
bool mNeedsClear = false;
|
||||
|
||||
QOpenGLContext* mContext = nullptr;
|
||||
QOpenGLContext* mLastContext = nullptr;
|
||||
MillSim::MillSimulation* mMillSimulator = nullptr;
|
||||
std::unique_ptr<MillSim::MillSimulation> mMillSimulator;
|
||||
static DlgCAMSimulator* mInstance;
|
||||
float mQuality = 10;
|
||||
};
|
||||
|
||||
std::vector<std::string> mGCode;
|
||||
std::vector<SimTool> mTools;
|
||||
SimShape mStock;
|
||||
SimShape mBase;
|
||||
};
|
||||
|
||||
} // namespace CAMSimulator
|
||||
|
||||
|
||||
#endif // PATHSIMULATOR_PathSim_H
|
||||
|
||||
@@ -313,7 +313,7 @@ void GuiDisplay::MouseCursorPos(int x, int y)
|
||||
if (mMouseOverItem != prevMouseOver) {
|
||||
if (mMouseOverItem != nullptr && !mMouseOverItem->toolTip.isEmpty()) {
|
||||
QPoint pos(x, y);
|
||||
QPoint globPos = CAMSimulator::DlgCAMSimulator::GetInstance()->mapToGlobal(pos);
|
||||
QPoint globPos = CAMSimulator::DlgCAMSimulator::instance()->mapToGlobal(pos);
|
||||
QToolTip::showText(globPos, mMouseOverItem->toolTip);
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -89,7 +89,7 @@ void MillSimulation::SimNext()
|
||||
}
|
||||
}
|
||||
|
||||
void MillSimulation::InitSimulation(float quality, qreal devicePixelRatio)
|
||||
void MillSimulation::InitSimulation(float quality)
|
||||
{
|
||||
ClearMillPathSegments();
|
||||
millPathLine.Clear();
|
||||
@@ -121,7 +121,7 @@ void MillSimulation::InitSimulation(float quality, qreal devicePixelRatio)
|
||||
}
|
||||
mNPathSteps = (int)MillPathSegments.size();
|
||||
millPathLine.GenerateModel();
|
||||
InitDisplay(quality, devicePixelRatio);
|
||||
InitDisplay(quality);
|
||||
}
|
||||
|
||||
EndMill* MillSimulation::GetTool(int toolId)
|
||||
@@ -529,7 +529,7 @@ void MillSimulation::HandleGuiAction(eGuiItems actionItem, bool checked)
|
||||
}
|
||||
|
||||
|
||||
void MillSimulation::InitDisplay(float quality, qreal devicePixelRatio)
|
||||
void MillSimulation::InitDisplay(float quality)
|
||||
{
|
||||
// generate tools
|
||||
for (unsigned int i = 0; i < mToolTable.size(); i++) {
|
||||
@@ -537,7 +537,7 @@ void MillSimulation::InitDisplay(float quality, qreal devicePixelRatio)
|
||||
}
|
||||
|
||||
// init 3d display
|
||||
simDisplay.InitGL(devicePixelRatio);
|
||||
simDisplay.InitGL();
|
||||
|
||||
// init gui elements
|
||||
guiDisplay.InitGui();
|
||||
@@ -549,13 +549,16 @@ void MillSimulation::SetBoxStock(float x, float y, float z, float l, float w, fl
|
||||
simDisplay.ScaleViewToStock(&mStockObject);
|
||||
}
|
||||
|
||||
void MillSimulation::SetArbitraryStock(std::vector<Vertex>& verts, std::vector<GLushort>& indices)
|
||||
void MillSimulation::SetArbitraryStock(
|
||||
const std::vector<Vertex>& verts,
|
||||
const std::vector<GLushort>& indices
|
||||
)
|
||||
{
|
||||
mStockObject.GenerateSolid(verts, indices);
|
||||
simDisplay.ScaleViewToStock(&mStockObject);
|
||||
}
|
||||
|
||||
void MillSimulation::SetBaseObject(std::vector<Vertex>& verts, std::vector<GLushort>& indices)
|
||||
void MillSimulation::SetBaseObject(const std::vector<Vertex>& verts, const std::vector<GLushort>& indices)
|
||||
{
|
||||
mBaseShape.GenerateSolid(verts, indices);
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ public:
|
||||
void ClearMillPathSegments();
|
||||
void Clear();
|
||||
void SimNext();
|
||||
void InitSimulation(float quality, qreal devicePixelRatio);
|
||||
void InitSimulation(float quality);
|
||||
void AddTool(EndMill* tool);
|
||||
void AddTool(const std::vector<float>& toolProfile, int toolid, float diameter);
|
||||
bool ToolExists(int toolid)
|
||||
@@ -73,8 +73,8 @@ public:
|
||||
bool AddGcodeLine(const char* line);
|
||||
void SetSimulationStage(float stage);
|
||||
void SetBoxStock(float x, float y, float z, float l, float w, float h);
|
||||
void SetArbitraryStock(std::vector<Vertex>& verts, std::vector<GLushort>& indices);
|
||||
void SetBaseObject(std::vector<Vertex>& verts, std::vector<GLushort>& indices);
|
||||
void SetArbitraryStock(const std::vector<Vertex>& verts, const std::vector<GLushort>& indices);
|
||||
void SetBaseObject(const std::vector<Vertex>& verts, const std::vector<GLushort>& indices);
|
||||
void MouseDrag(int buttons, int dx, int dy);
|
||||
void MouseMove(int px, int py, int modifiers);
|
||||
void MouseScroll(float dy);
|
||||
@@ -85,7 +85,7 @@ public:
|
||||
|
||||
|
||||
protected:
|
||||
void InitDisplay(float quality, qreal devicePixelRatio);
|
||||
void InitDisplay(float quality);
|
||||
void GlsimStart();
|
||||
void GlsimToolStep1(void);
|
||||
void GlsimToolStep2(void);
|
||||
|
||||
@@ -26,8 +26,7 @@
|
||||
#define __openglwrapper_h__
|
||||
|
||||
#include "DlgCAMSimulator.h"
|
||||
extern QOpenGLContext* gOpenGlContext;
|
||||
#define gSimWindow CAMSimulator::DlgCAMSimulator::GetInstance()
|
||||
#define gSimWindow CAMSimulator::DlgCAMSimulator::instance()
|
||||
#define glClearColor gSimWindow->glClearColor
|
||||
#define glBlendFunc gSimWindow->glBlendFunc
|
||||
#define glClear gSimWindow->glClear
|
||||
|
||||
@@ -240,7 +240,7 @@ SimDisplay::~SimDisplay()
|
||||
CleanGL();
|
||||
}
|
||||
|
||||
void SimDisplay::InitGL(qreal devicePixelRatio)
|
||||
void SimDisplay::InitGL()
|
||||
{
|
||||
if (displayInitiated) {
|
||||
return;
|
||||
@@ -249,9 +249,8 @@ void SimDisplay::InitGL(qreal devicePixelRatio)
|
||||
// setup light object
|
||||
mlightObject.GenerateBoxStock(-0.5f, -0.5f, -0.5f, 1, 1, 1);
|
||||
|
||||
mDevicePixelRatio = devicePixelRatio;
|
||||
mWidth = (int)(gWindowSizeW * mDevicePixelRatio);
|
||||
mHeight = (int)(gWindowSizeH * mDevicePixelRatio);
|
||||
mWidth = gWindowSizeW;
|
||||
mHeight = gWindowSizeH;
|
||||
InitShaders();
|
||||
CreateDisplayFbos();
|
||||
CreateSsaoFbos();
|
||||
@@ -551,8 +550,8 @@ void SimDisplay::UpdateEyeFactor(float factor)
|
||||
|
||||
void SimDisplay::UpdateWindowScale()
|
||||
{
|
||||
mWidth = (int)(gWindowSizeW * mDevicePixelRatio);
|
||||
mHeight = (int)(gWindowSizeH * mDevicePixelRatio);
|
||||
mWidth = gWindowSizeW;
|
||||
mHeight = gWindowSizeH;
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
|
||||
CleanFbos();
|
||||
CreateDisplayFbos();
|
||||
|
||||
@@ -47,7 +47,7 @@ class SimDisplay
|
||||
{
|
||||
public:
|
||||
~SimDisplay();
|
||||
void InitGL(qreal devicePixelRatio);
|
||||
void InitGL();
|
||||
void CleanGL();
|
||||
void CleanFbos();
|
||||
void PrepareDisplay(vec3 objCenter);
|
||||
@@ -108,7 +108,6 @@ protected:
|
||||
mat4x4 mMatLookAt;
|
||||
StockObject mlightObject;
|
||||
|
||||
qreal mDevicePixelRatio;
|
||||
int mWidth;
|
||||
int mHeight;
|
||||
|
||||
|
||||
@@ -327,7 +327,7 @@ void Shape::ExtrudeProfileLinear(
|
||||
SetModelData(vbuffer, ibuffer);
|
||||
}
|
||||
|
||||
void Shape::GenerateModel(float* vbuffer, GLushort* ibuffer, int numVerts, int nIndices)
|
||||
void Shape::GenerateModel(const float* vbuffer, const GLushort* ibuffer, int numVerts, int nIndices)
|
||||
{
|
||||
// GLuint vbo, ibo, vao;
|
||||
|
||||
@@ -354,9 +354,9 @@ void Shape::GenerateModel(float* vbuffer, GLushort* ibuffer, int numVerts, int n
|
||||
numIndices = nIndices;
|
||||
}
|
||||
|
||||
void MillSim::Shape::SetModelData(std::vector<Vertex>& vbuffer, std::vector<GLushort>& ibuffer)
|
||||
void MillSim::Shape::SetModelData(const std::vector<Vertex>& vbuffer, const std::vector<GLushort>& ibuffer)
|
||||
{
|
||||
GenerateModel((float*)vbuffer.data(), ibuffer.data(), (int)vbuffer.size(), (int)ibuffer.size());
|
||||
GenerateModel((const float*)vbuffer.data(), ibuffer.data(), (int)vbuffer.size(), (int)ibuffer.size());
|
||||
}
|
||||
|
||||
void Shape::Render()
|
||||
|
||||
@@ -85,7 +85,7 @@ public:
|
||||
void Render();
|
||||
void Render(mat4x4 modelMat, mat4x4 normallMat);
|
||||
void FreeResources();
|
||||
void SetModelData(std::vector<Vertex>& vbuffer, std::vector<GLushort>& ibuffer);
|
||||
void SetModelData(const std::vector<Vertex>& vbuffer, const std::vector<GLushort>& ibuffer);
|
||||
void RotateProfile(
|
||||
float* profPoints,
|
||||
int nPoints,
|
||||
@@ -120,7 +120,7 @@ public:
|
||||
static int lastNumSlices;
|
||||
|
||||
protected:
|
||||
void GenerateModel(float* vbuffer, GLushort* ibuffer, int numVerts, int numIndices);
|
||||
void GenerateModel(const float* vbuffer, const GLushort* ibuffer, int numVerts, int numIndices);
|
||||
void CalculateExtrudeBufferSizes(
|
||||
int nProfilePoints,
|
||||
bool capStart,
|
||||
|
||||
@@ -55,14 +55,14 @@ void SolidObject::render()
|
||||
shape.Render(mModelMat, mModelMat); // model is not rotated hence both are identity matrix
|
||||
}
|
||||
|
||||
void SolidObject::GenerateSolid(std::vector<Vertex>& verts, std::vector<GLushort>& indices)
|
||||
void SolidObject::GenerateSolid(const std::vector<Vertex>& verts, const std::vector<GLushort>& indices)
|
||||
{
|
||||
shape.SetModelData(verts, indices);
|
||||
|
||||
// calculate object's bounding box:
|
||||
float x = 999999.0f, y = 999999.0f, z = 999999.0f;
|
||||
float l = -999999.0f, w = -999999.0f, h = -999999.0f;
|
||||
for (auto& vert : verts) {
|
||||
for (const auto& vert : verts) {
|
||||
x = std::fminf(x, vert.x);
|
||||
y = std::fminf(y, vert.y);
|
||||
z = std::fminf(z, vert.z);
|
||||
|
||||
@@ -41,7 +41,7 @@ public:
|
||||
/// Calls the display list.
|
||||
virtual void render();
|
||||
Shape shape;
|
||||
void GenerateSolid(std::vector<Vertex>& verts, std::vector<GLushort>& indices);
|
||||
void GenerateSolid(const std::vector<Vertex>& verts, const std::vector<GLushort>& indices);
|
||||
vec3 center = {};
|
||||
vec3 size = {};
|
||||
vec3 position = {};
|
||||
|
||||
Reference in New Issue
Block a user