[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
This commit is contained in:
Shai Seger
2024-09-02 19:31:08 +03:00
committed by GitHub
parent 5a1275ad60
commit 3067946b4d
9 changed files with 85 additions and 82 deletions

View File

@@ -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():

View File

@@ -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<float> toolProfilePoints,
void DlgCAMSimulator::addTool(const std::vector<float>& 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<float> 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<Vertex>& verts,
std::vector<GLushort>& indices)
{
std::vector<GLushort>& indices) {
std::vector<int> 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<Vertex> verts;
std::vector<GLushort> 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<Vertex> verts;
std::vector<GLushort> 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

View File

@@ -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<float> toolProfilePoints,
void addTool(const std::vector<float>& toolProfilePoints,
int toolNumber,
float diameter,
float resolution);

View File

@@ -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(); \

View File

@@ -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;

View File

@@ -25,6 +25,8 @@
#include <vector>
#include <iostream>
#define DRAG_ZOOM_FACTOR 10
namespace MillSim
{
@@ -530,23 +532,33 @@ void MillSimulation::SetBaseObject(std::vector<Vertex>& verts, std::vector<GLush
void MillSimulation::MouseDrag(int buttons, int dx, int dy)
{
if (buttons == (MS_MOUSE_MID | MS_MOUSE_LEFT)) {
if (buttons == (MS_MOUSE_MID | MS_MOUSE_LEFT) || buttons == MS_KBD_ALT) {
simDisplay.TiltEye((float)dy / 100.0f);
simDisplay.RotateEye((float)dx / 100.0f);
}
else if (buttons == MS_MOUSE_MID) {
else if (buttons == MS_MOUSE_MID || buttons == MS_KBD_SHIFT) {
simDisplay.MoveEye(dx, -dy);
}
else if (buttons == (MS_KBD_CONTROL | MS_KBD_SHIFT)) {
Zoom(0.003 * dy);
}
guiDisplay.MouseDrag(buttons, dx, dy);
}
void MillSimulation::MouseMove(int px, int py)
void MillSimulation::MouseMove(int px, int py, int modifiers)
{
if (mMouseButtonState > 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) {

View File

@@ -74,10 +74,11 @@ public:
void SetArbitraryStock(std::vector<Vertex>& verts, std::vector<GLushort>& indices);
void SetBaseObject(std::vector<Vertex>& verts, std::vector<GLushort>& 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;

View File

@@ -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);
}

View File

@@ -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;