File size: 11,077 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************
 *   Copyright (c) 2024 Shai Seger <shaise at gmail>                       *
 *                                                                         *
 *   This file is part of the FreeCAD CAx development system.              *
 *                                                                         *
 *   This library is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU Library General Public           *
 *   License as published by the Free Software Foundation; either          *
 *   version 2 of the License, or (at your option) any later version.      *
 *                                                                         *
 *   This library  is distributed in the hope that it will be useful,      *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU Library General Public License for more details.                  *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this library; see the file COPYING.LIB. If not,    *
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
 *   Suite 330, Boston, MA  02111-1307, USA                                *
 *                                                                         *
 ***************************************************************************/


#include "DlgCAMSimulator.h"
#include "MillSimulation.h"
#include "Gui/View3DInventorViewer.h"
#include <Mod/Part/App/BRepMesh.h>
#include <QDateTime>
#include <QSurfaceFormat>
#include <QPoint>
#include <App/Document.h>

QOpenGLContext* gOpenGlContext;

using namespace MillSim;

namespace CAMSimulator
{

static const float MouseScrollDelta = 120.0F;

DlgCAMSimulator::DlgCAMSimulator(QWindow* parent)
    : QWindow(parent)
{
    setSurfaceType(QWindow::OpenGLSurface);
    mMillSimulator = new MillSimulation();
}

DlgCAMSimulator::~DlgCAMSimulator()
{
    delete mMillSimulator;
}

void DlgCAMSimulator::render(QPainter* painter)
{
    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;
    }
    return QWindow::event(event);
}

void DlgCAMSimulator::exposeEvent(QExposeEvent* event)
{
    Q_UNUSED(event);

    if (isExposed()) {
        renderNow();
    }
}

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
    mMillSimulator->MouseMove(pnt.x(), pnt.y(), modifiers);
}

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

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

void DlgCAMSimulator::resetSimulation()
{}

void DlgCAMSimulator::addGcodeCommand(const char* cmd)
{
    mMillSimulator->AddGcodeLine(cmd);
}

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

void DlgCAMSimulator::hideEvent(QHideEvent* ev)
{
    mMillSimulator->Clear();
    doGlCleanup();
    mAnimating = false;
    QWindow::hideEvent(ev);
    close();
    mInstance = nullptr;
}

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)) {
        std::vector<Base::Vector3d> points;
        std::vector<Data::ComplexGeoData::Facet> facets;
        shape.getFaces(points, facets, resolution);

        std::vector<Base::Vector3d> normals(points.size());
        std::vector<int> normalCount(points.size());

        // 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);

            // calculate normal
            Base::Vector3d vAB = points[face.I2] - points[face.I1];
            Base::Vector3d vAC = points[face.I3] - points[face.I1];
            Base::Vector3d vNorm = vAB.Cross(vAC).Normalize();

            normals[face.I1] += vNorm;
            normals[face.I2] += vNorm;
            normals[face.I3] += vNorm;

            normalCount[face.I1]++;
            normalCount[face.I2]++;
            normalCount[face.I3]++;
        }

        // copy points and set normals
        for (unsigned int i = 0; i < points.size(); i++) {
            Base::Vector3d& point = points[i];
            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));
        }

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

    mContext->makeCurrent(this);

    if (mNeedsInitialize) {
        initializeOpenGLFunctions();
        initialize();
        mNeedsInitialize = false;
    }
}

void DlgCAMSimulator::doGlCleanup()
{
    if (mLastContext != nullptr) {
        mLastContext->makeCurrent(this);
    }
    if (mContext != nullptr) {
        mContext->deleteLater();
        mContext = nullptr;
    }
}

void DlgCAMSimulator::renderNow()
{
    static unsigned int lastTime = 0;
    static int frameCount = 0;
    static int fps = 0;
    if (!isExposed()) {
        return;
    }

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

    if (mAnimating) {
        renderLater();
    }
    (void)fps;
}

void DlgCAMSimulator::setAnimating(bool animating)
{
    mAnimating = animating;

    if (animating) {
        renderLater();
    }
}

DlgCAMSimulator* DlgCAMSimulator::GetInstance()
{
    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);
}

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)
    , mPz(pz + 0.005 * lz)
    , mLx(lx)
    , mLy(ly)
    , mLz(1.01 * lz)
{
    (void)res;
}

SimStock::~SimStock()
{}

}  // namespace CAMSimulator