From 3067946b4da266f7392d85253e1805280658fe36 Mon Sep 17 00:00:00 2001 From: Shai Seger Date: Mon, 2 Sep 2024 19:31:08 +0300 Subject: [PATCH] [CAM SIMULATOR] Bug fixes for Issues #16073 and #16052 (#16118) * Bug fixes for Issues #16073 and #16052 * Fix tool rendering when tool position is not reset. Issue #16180 * Fix some lint warnings --- src/Mod/CAM/Path/Main/Gui/SimulatorGL.py | 5 +- .../PathSimulator/AppGL/DlgCAMSimulator.cpp | 94 +++++++------------ .../CAM/PathSimulator/AppGL/DlgCAMSimulator.h | 2 +- src/Mod/CAM/PathSimulator/AppGL/GlUtils.h | 10 +- .../PathSimulator/AppGL/MillPathSegment.cpp | 2 +- .../PathSimulator/AppGL/MillSimulation.cpp | 44 ++++++--- .../CAM/PathSimulator/AppGL/MillSimulation.h | 4 +- .../CAM/PathSimulator/AppGL/SimDisplay.cpp | 4 +- src/Mod/CAM/PathSimulator/AppGL/SimDisplay.h | 2 +- 9 files changed, 85 insertions(+), 82 deletions(-) diff --git a/src/Mod/CAM/Path/Main/Gui/SimulatorGL.py b/src/Mod/CAM/Path/Main/Gui/SimulatorGL.py index 5b8cf7dbb3..be36a4e5af 100644 --- a/src/Mod/CAM/Path/Main/Gui/SimulatorGL.py +++ b/src/Mod/CAM/Path/Main/Gui/SimulatorGL.py @@ -33,7 +33,7 @@ import Path.Main.Job as PathJob from PathScripts import PathUtils import CAMSimulator -from FreeCAD import Vector +from FreeCAD import Vector, Placement, Rotation # lazily loaded modules @@ -167,7 +167,10 @@ class CAMSimulation: """ Get the edge profile of a tool solid. Basically locating the side edge that OCC creates on any revolved object """ + originalPlacement = tool.Placement + tool.Placement = Placement(Vector(0,0,0), Rotation(Vector(0,0,1),0), Vector(0,0,0)) shape = tool.Shape + tool.Placement = originalPlacement sideEdgeList = [] for _i, edge in enumerate(shape.Edges): if not edge.isClosed(): diff --git a/src/Mod/CAM/PathSimulator/AppGL/DlgCAMSimulator.cpp b/src/Mod/CAM/PathSimulator/AppGL/DlgCAMSimulator.cpp index 55e81b9521..4b1a61ac1a 100644 --- a/src/Mod/CAM/PathSimulator/AppGL/DlgCAMSimulator.cpp +++ b/src/Mod/CAM/PathSimulator/AppGL/DlgCAMSimulator.cpp @@ -36,30 +36,27 @@ using namespace MillSim; namespace CAMSimulator { +static const float MouseScrollDelta = 120.0f; + DlgCAMSimulator::DlgCAMSimulator(QWindow* parent) - : QWindow(parent) -{ + : QWindow(parent) { setSurfaceType(QWindow::OpenGLSurface); mMillSimulator = new MillSimulation(); } -void DlgCAMSimulator::render(QPainter* painter) -{ +void DlgCAMSimulator::render(QPainter* painter) { Q_UNUSED(painter); } -void DlgCAMSimulator::render() -{ +void DlgCAMSimulator::render() { mMillSimulator->ProcessSim((unsigned int)(QDateTime::currentMSecsSinceEpoch())); } -void DlgCAMSimulator::renderLater() -{ +void DlgCAMSimulator::renderLater() { requestUpdate(); } -bool DlgCAMSimulator::event(QEvent* event) -{ +bool DlgCAMSimulator::event(QEvent* event) { switch (event->type()) { case QEvent::UpdateRequest: renderNow(); @@ -70,8 +67,7 @@ bool DlgCAMSimulator::event(QEvent* event) return QWindow::event(event); } -void DlgCAMSimulator::exposeEvent(QExposeEvent* event) -{ +void DlgCAMSimulator::exposeEvent(QExposeEvent* event) { Q_UNUSED(event); if (isExposed()) { @@ -79,40 +75,36 @@ void DlgCAMSimulator::exposeEvent(QExposeEvent* event) } } -void DlgCAMSimulator::mouseMoveEvent(QMouseEvent* ev) -{ - mMillSimulator->MouseMove(ev->x(), ev->y()); +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; + mMillSimulator->MouseMove(ev->x(), ev->y(), modifiers); } -void DlgCAMSimulator::mousePressEvent(QMouseEvent* ev) -{ +void DlgCAMSimulator::mousePressEvent(QMouseEvent* ev) { mMillSimulator->MousePress(ev->button(), true, ev->x(), ev->y()); } -void DlgCAMSimulator::mouseReleaseEvent(QMouseEvent* ev) -{ +void DlgCAMSimulator::mouseReleaseEvent(QMouseEvent* ev) { mMillSimulator->MousePress(ev->button(), false, ev->x(), ev->y()); } -void DlgCAMSimulator::wheelEvent(QWheelEvent* ev) -{ - mMillSimulator->MouseScroll((float)ev->angleDelta().y() / 120.0f); +void DlgCAMSimulator::wheelEvent(QWheelEvent* ev) { + mMillSimulator->MouseScroll((float)ev->angleDelta().y() / MouseScrollDelta); } -void DlgCAMSimulator::resetSimulation() -{ +void DlgCAMSimulator::resetSimulation() { } -void DlgCAMSimulator::addGcodeCommand(const char* cmd) -{ +void DlgCAMSimulator::addGcodeCommand(const char* cmd) { mMillSimulator->AddGcodeLine(cmd); } -void DlgCAMSimulator::addTool(const std::vector toolProfilePoints, +void DlgCAMSimulator::addTool(const std::vector& toolProfilePoints, int toolNumber, float diameter, - float resolution) -{ + float resolution) { Q_UNUSED(resolution) std::string toolCmd = "T" + std::to_string(toolNumber); mMillSimulator->AddGcodeLine(toolCmd.c_str()); @@ -121,8 +113,7 @@ void DlgCAMSimulator::addTool(const std::vector toolProfilePoints, } } -void DlgCAMSimulator::hideEvent(QHideEvent* ev) -{ +void DlgCAMSimulator::hideEvent(QHideEvent* ev) { mMillSimulator->Clear(); doGlCleanup(); mAnimating = false; @@ -131,8 +122,7 @@ void DlgCAMSimulator::hideEvent(QHideEvent* ev) mInstance = nullptr; } -void DlgCAMSimulator::resizeEvent(QResizeEvent* event) -{ +void DlgCAMSimulator::resizeEvent(QResizeEvent* event) { if (!mContext) { return; } @@ -143,14 +133,13 @@ void DlgCAMSimulator::resizeEvent(QResizeEvent* event) mMillSimulator->UpdateWindowScale(newWidth, newHeight); } const qreal retinaScale = devicePixelRatio(); - glViewport(0, 0, newWidth * retinaScale, newHeight * retinaScale); + glViewport(0, 0, (int)(newWidth * retinaScale), (int)(newHeight * retinaScale)); } void DlgCAMSimulator::GetMeshData(const Part::TopoShape& tshape, float resolution, std::vector& verts, - std::vector& indices) -{ + std::vector& indices) { std::vector normalCount; int nVerts = 0; for (auto& shape : tshape.getSubTopoShapes(TopAbs_FACE)) { @@ -194,8 +183,7 @@ void DlgCAMSimulator::GetMeshData(const Part::TopoShape& tshape, } } -void DlgCAMSimulator::startSimulation(const Part::TopoShape& stock, float quality) -{ +void DlgCAMSimulator::startSimulation(const Part::TopoShape& stock, float quality) { mQuality = quality; mNeedsInitialize = true; show(); @@ -204,8 +192,7 @@ void DlgCAMSimulator::startSimulation(const Part::TopoShape& stock, float qualit setAnimating(true); } -void DlgCAMSimulator::initialize() -{ +void DlgCAMSimulator::initialize() { mMillSimulator->InitSimulation(mQuality); const qreal retinaScale = devicePixelRatio(); @@ -213,8 +200,7 @@ void DlgCAMSimulator::initialize() glEnable(GL_MULTISAMPLE); } -void DlgCAMSimulator::checkInitialization() -{ +void DlgCAMSimulator::checkInitialization() { if (!mContext) { mLastContext = QOpenGLContext::currentContext(); mContext = new QOpenGLContext(this); @@ -237,8 +223,7 @@ void DlgCAMSimulator::checkInitialization() } } -void DlgCAMSimulator::doGlCleanup() -{ +void DlgCAMSimulator::doGlCleanup() { if (mLastContext != nullptr) { mLastContext->makeCurrent(this); } @@ -248,8 +233,7 @@ void DlgCAMSimulator::doGlCleanup() } } -void DlgCAMSimulator::renderNow() -{ +void DlgCAMSimulator::renderNow() { static unsigned int lastTime = 0; static int frameCount = 0; static int fps = 0; @@ -276,8 +260,7 @@ void DlgCAMSimulator::renderNow() (void)fps; } -void DlgCAMSimulator::setAnimating(bool animating) -{ +void DlgCAMSimulator::setAnimating(bool animating) { mAnimating = animating; if (animating) { @@ -285,8 +268,7 @@ void DlgCAMSimulator::setAnimating(bool animating) } } -DlgCAMSimulator* DlgCAMSimulator::GetInstance() -{ +DlgCAMSimulator* DlgCAMSimulator::GetInstance() { if (mInstance == nullptr) { QSurfaceFormat format; format.setVersion(4, 1); // Request OpenGL 4.1 - for MacOS @@ -305,16 +287,14 @@ DlgCAMSimulator* DlgCAMSimulator::GetInstance() return mInstance; } -void DlgCAMSimulator::SetStockShape(const Part::TopoShape& shape, float resolution) -{ +void DlgCAMSimulator::SetStockShape(const Part::TopoShape& shape, float resolution) { std::vector verts; std::vector indices; GetMeshData(shape, resolution, verts, indices); mMillSimulator->SetArbitraryStock(verts, indices); } -void DlgCAMSimulator::SetBaseShape(const Part::TopoShape& tshape, float resolution) -{ +void DlgCAMSimulator::SetBaseShape(const Part::TopoShape& tshape, float resolution) { std::vector verts; std::vector indices; GetMeshData(tshape, resolution, verts, indices); @@ -332,12 +312,10 @@ SimStock::SimStock(float px, float py, float pz, float lx, float ly, float lz, f , mPz(pz + 0.005 * lz) , mLx(lx) , mLy(ly) - , mLz(1.01 * lz) -{ + , mLz(1.01 * lz) { (void)res; } -SimStock::~SimStock() -{} +SimStock::~SimStock() {} } // namespace CAMSimulator diff --git a/src/Mod/CAM/PathSimulator/AppGL/DlgCAMSimulator.h b/src/Mod/CAM/PathSimulator/AppGL/DlgCAMSimulator.h index 50dda37f0c..6e62f6194d 100644 --- a/src/Mod/CAM/PathSimulator/AppGL/DlgCAMSimulator.h +++ b/src/Mod/CAM/PathSimulator/AppGL/DlgCAMSimulator.h @@ -75,7 +75,7 @@ public: void startSimulation(const Part::TopoShape& stock, float quality); void resetSimulation(); void addGcodeCommand(const char* cmd); - void addTool(const std::vector toolProfilePoints, + void addTool(const std::vector& toolProfilePoints, int toolNumber, float diameter, float resolution); diff --git a/src/Mod/CAM/PathSimulator/AppGL/GlUtils.h b/src/Mod/CAM/PathSimulator/AppGL/GlUtils.h index 558501d1a5..6a168abffb 100644 --- a/src/Mod/CAM/PathSimulator/AppGL/GlUtils.h +++ b/src/Mod/CAM/PathSimulator/AppGL/GlUtils.h @@ -32,9 +32,13 @@ constexpr auto EPSILON = 0.00001f; #define EQ_FLOAT(x, y) (fabs((x) - (y)) < EPSILON) -#define MS_MOUSE_LEFT 1 -#define MS_MOUSE_RIGHT 2 -#define MS_MOUSE_MID 4 +#define MS_MOUSE_LEFT 0x01 +#define MS_MOUSE_RIGHT 0x02 +#define MS_MOUSE_MID 0x04 +#define MS_KBD_SHIFT 0x08 +#define MS_KBD_CONTROL 0x10 +#define MS_KBD_ALT 0x20 + #define GL(x) \ { \ GLClearError(); \ diff --git a/src/Mod/CAM/PathSimulator/AppGL/MillPathSegment.cpp b/src/Mod/CAM/PathSimulator/AppGL/MillPathSegment.cpp index 310363b959..1f76aa6767 100644 --- a/src/Mod/CAM/PathSimulator/AppGL/MillPathSegment.cpp +++ b/src/Mod/CAM/PathSimulator/AppGL/MillPathSegment.cpp @@ -49,7 +49,7 @@ bool IsArcMotion(MillMotion* m) if (m->cmd != eRotateCCW && m->cmd != eRotateCW) { return false; } - return fabs(m->i > EPSILON) || fabs(m->j) > EPSILON; + return fabs(m->i) > EPSILON || fabs(m->j) > EPSILON; } float MillPathSegment::mResolution = 1; diff --git a/src/Mod/CAM/PathSimulator/AppGL/MillSimulation.cpp b/src/Mod/CAM/PathSimulator/AppGL/MillSimulation.cpp index 09f9bc5ca5..7824a3a59e 100644 --- a/src/Mod/CAM/PathSimulator/AppGL/MillSimulation.cpp +++ b/src/Mod/CAM/PathSimulator/AppGL/MillSimulation.cpp @@ -25,6 +25,8 @@ #include #include +#define DRAG_ZOOM_FACTOR 10 + namespace MillSim { @@ -530,23 +532,33 @@ void MillSimulation::SetBaseObject(std::vector& verts, std::vector 0) { + if (modifiers != mLastModifiers) { + mLastMouseX = px; + mLastMouseY = py; + mLastModifiers = modifiers; + } + + int buttons = mMouseButtonState | modifiers; + if (buttons > 0) { int dx = px - mLastMouseX; int dy = py - mLastMouseY; if (dx != 0 || dy != 0) { - MouseDrag(mMouseButtonState, dx, dy); + MouseDrag(buttons, dx, dy); mLastMouseX = px; mLastMouseY = py; } @@ -558,15 +570,7 @@ void MillSimulation::MouseMove(int px, int py) void MillSimulation::MouseScroll(float dy) { - float f = simDisplay.GetEyeFactor(); - f += 0.05f * dy; - if (f > 0.6f) { - f = 0.6f; - } - else if (f < 0.05f) { - f = 0.05f; - } - simDisplay.UpdateEyeFactor(f); + Zoom(-0.02f * dy); } @@ -591,6 +595,18 @@ void MillSimulation::MousePress(int button, bool isPressed, int px, int py) guiDisplay.MousePressed(button, isPressed, mSimPlaying); } +void MillSimulation::Zoom(float factor) +{ + factor += simDisplay.GetEyeFactor(); + if (factor > 0.6f) { + factor = 0.6f; + } + else if (factor < 0.01f) { + factor = 0.01f; + } + simDisplay.UpdateEyeFactor(factor); +} + void MillSimulation::UpdateWindowScale(int width, int height) { if (width == gWindowSizeW && height == gWindowSizeH) { diff --git a/src/Mod/CAM/PathSimulator/AppGL/MillSimulation.h b/src/Mod/CAM/PathSimulator/AppGL/MillSimulation.h index 42c9df4f62..9a3911591d 100644 --- a/src/Mod/CAM/PathSimulator/AppGL/MillSimulation.h +++ b/src/Mod/CAM/PathSimulator/AppGL/MillSimulation.h @@ -74,10 +74,11 @@ public: void SetArbitraryStock(std::vector& verts, std::vector& indices); void SetBaseObject(std::vector& verts, std::vector& indices); void MouseDrag(int buttons, int dx, int dy); - void MouseMove(int px, int py); + void MouseMove(int px, int py, int modifiers); void MouseScroll(float dy); void MouseHover(int px, int py); void MousePress(int button, bool isPressed, int px, int py); + void Zoom(float factor); void UpdateWindowScale(int width, int height); @@ -133,6 +134,7 @@ protected: int mLastMouseX = 0, mLastMouseY = 0; int mMouseButtonState = 0; + int mLastModifiers = 0; bool mIsInStock = false; bool mSimPlaying = false; diff --git a/src/Mod/CAM/PathSimulator/AppGL/SimDisplay.cpp b/src/Mod/CAM/PathSimulator/AppGL/SimDisplay.cpp index 6940650032..08d94f24dd 100644 --- a/src/Mod/CAM/PathSimulator/AppGL/SimDisplay.cpp +++ b/src/Mod/CAM/PathSimulator/AppGL/SimDisplay.cpp @@ -363,10 +363,10 @@ void SimDisplay::RenderLightObject() void SimDisplay::ScaleViewToStock(StockObject* obj) { mMaxStockDim = fmaxf(obj->size[0], obj->size[1]); - maxFar = mMaxStockDim * 4; + maxFar = mMaxStockDim * 16; UpdateProjection(); vec3_set(eye, 0, 0, 0); - UpdateEyeFactor(0.4f); + UpdateEyeFactor(0.1f); vec3_set(lightPos, obj->position[0], obj->position[1], obj->position[2] + mMaxStockDim / 3); mlightObject.SetPosition(lightPos); } diff --git a/src/Mod/CAM/PathSimulator/AppGL/SimDisplay.h b/src/Mod/CAM/PathSimulator/AppGL/SimDisplay.h index ecf1e7c22c..29a2ff78f1 100644 --- a/src/Mod/CAM/PathSimulator/AppGL/SimDisplay.h +++ b/src/Mod/CAM/PathSimulator/AppGL/SimDisplay.h @@ -97,7 +97,7 @@ protected: vec3 pathLineColorPassed = {0.9f, 0.3f, 0.3f}; vec3 eye = {0, 100, 40}; - vec3 target = {0, 0, -10}; + vec3 target = {0, 0, 0}; vec3 upvec = {0, 0, 1}; mat4x4 mMatLookAt;