File size: 7,290 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
// SPDX-License-Identifier: LGPL-2.1-or-later

/**************************************************************************
 *   Copyright (c) 2017 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                                *
 *                                                                         *
 ***************************************************************************
 *   Volumetric Path simulation engine                                     *
 ***************************************************************************/

#ifndef PATHSIMULATOR_VolSim_H
#define PATHSIMULATOR_VolSim_H

#include <vector>

#include <Mod/Mesh/App/Mesh.h>
#include <Mod/CAM/App/Command.h>


#define SIM_EPSILON 0.00001
#define SIM_TESSEL_TOP 1
#define SIM_TESSEL_BOT 2
#define SIM_WALK_RES \
    0.6  // step size in pixel units (to make sure all pixels in the path are visited)

struct toolShapePoint
{
    float radiusPos;
    float heightPos;

    struct less_than
    {
        bool operator()(const toolShapePoint& a, const toolShapePoint& b)
        {
            return a.radiusPos < b.radiusPos;
        }
    };
};

struct Point3D
{
    Point3D()
        : x(0)
        , y(0)
        , z(0)
        , sina(0)
        , cosa(0)
    {}
    Point3D(float x, float y, float z)
        : x(x)
        , y(y)
        , z(z)
        , sina(0)
        , cosa(0)
    {}
    explicit Point3D(Base::Vector3d& vec)
        : x(vec[0])
        , y(vec[1])
        , z(vec[2])
        , sina(0)
        , cosa(0)
    {}
    explicit Point3D(Base::Placement& pl)
        : x(pl.getPosition()[0])
        , y(pl.getPosition()[1])
        , z(pl.getPosition()[2])
        , sina(0)
        , cosa(0)
    {}
    inline void set(float px, float py, float pz)
    {
        x = px;
        y = py;
        z = pz;
    }
    inline void Add(Point3D& p)
    {
        x += p.x;
        y += p.y;
        z += p.z;
    }
    inline void Rotate()
    {
        float tx = x;
        x = x * cosa - y * sina;
        y = tx * sina + y * cosa;
    }
    void UpdateCmd(Path::Command& cmd);
    void SetRotationAngle(float angle);
    void SetRotationAngleRad(float angle);
    float x, y, z;
    float sina, cosa;
};

// some vector manipulations
inline static Point3D operator+(const Point3D& a, const Point3D& b)
{
    return Point3D(a.x + b.x, a.y + b.y, a.z + b.z);
}
inline static Point3D operator-(const Point3D& a, const Point3D& b)
{
    return Point3D(a.x - b.x, a.y - b.y, a.z - b.z);
}
inline static Point3D operator*(const Point3D& a, double b)
{
    return Point3D(a.x * b, a.y * b, a.z * b);
}
inline static Point3D operator/(const Point3D& a, double b)
{
    return a * (1.0f / b);
}
inline static double dot(const Point3D& a, const Point3D& b)
{
    return a.x * b.x + a.y * b.y + a.z * b.z;
}
inline static double length(const Point3D& a)
{
    return sqrtf(dot(a, a));
}
inline static Point3D unit(const Point3D& a)
{
    return a / length(a);
}


struct Triangle3D
{
    Triangle3D()
    {}
    Triangle3D(Point3D& p1, Point3D& p2, Point3D& p3)
    {
        points[0] = p1;
        points[1] = p2;
        points[2] = p3;
    }
    Point3D points[3];
};

struct cLineSegment
{
    cLineSegment()
        : len(0)
        , lenXY(0)
    {}
    cLineSegment(Point3D& p1, Point3D& p2)
    {
        SetPoints(p1, p2);
    }
    void SetPoints(Point3D& p1, Point3D& p2);
    void PointAt(float dist, Point3D& retp);
    Point3D pStart;
    Point3D pDir;
    Point3D pDirXY;
    float len;
    float lenXY;
};

class cSimTool
{
public:
    cSimTool(const TopoDS_Shape& toolShape, float res);
    ~cSimTool()
    {}

    float GetToolProfileAt(float pos);
    bool isInside(const TopoDS_Shape& toolShape, Base::Vector3d pnt, float res);

    /* m_toolShape has to be populated with linearly increased
       radiusPos to get the tool profile at given position */
    std::vector<toolShapePoint> m_toolShape;
    float radius;
    float length;
};

template<class T>
class Array2D
{
public:
    Array2D()
        : data(nullptr)
        , height(0)
    {}

    ~Array2D()
    {
        if (data) {
            delete[] data;
        }
    }

    void Init(int x, int y)
    {
        data = new T[x * y];
        height = y;
    }

    T* operator[](int i)
    {
        return data + i * height;
    }

private:
    T* data;
    int height;
};

class cStock
{
public:
    cStock(float px, float py, float pz, float lx, float ly, float lz, float res);
    ~cStock();
    void Tessellate(Mesh::MeshObject& meshOuter, Mesh::MeshObject& meshInner);
    void CreatePocket(float x, float y, float rad, float height);
    void ApplyLinearTool(Point3D& p1, Point3D& p2, cSimTool& tool);
    void ApplyCircularTool(Point3D& p1, Point3D& p2, Point3D& cent, cSimTool& tool, bool isCCW);
    inline Point3D ToInner(Point3D& p)
    {
        return Point3D((p.x - m_px) / m_res, (p.y - m_py) / m_res, p.z);
    }

private:
    float FindRectTop(int& xp, int& yp, int& x_size, int& y_size, bool scanHoriz);
    void FindRectBot(int& xp, int& yp, int& x_size, int& y_size, bool scanHoriz);
    void SetFacetPoints(MeshCore::MeshGeomFacet& facet, Point3D& p1, Point3D& p2, Point3D& p3);
    void AddQuad(
        Point3D& p1,
        Point3D& p2,
        Point3D& p3,
        Point3D& p4,
        std::vector<MeshCore::MeshGeomFacet>& facets
    );
    int TesselTop(int x, int y);
    int TesselBot(int x, int y);
    int TesselSidesX(int yp);
    int TesselSidesY(int xp);
    Array2D<float> m_stock;
    Array2D<char> m_attr;
    float m_px, m_py, m_pz;  // stock zero position
    float m_lx, m_ly, m_lz;  // stock dimensions
    float m_res;             // resoulution
    float m_plane;           // stock plane height
    int m_x, m_y;            // stock array size
    std::vector<MeshCore::MeshGeomFacet> facetsOuter;
    std::vector<MeshCore::MeshGeomFacet> facetsInner;
};

class cVolSim
{
public:
    cVolSim();
    ~cVolSim();
    void CreateStock();

private:
    cStock* stock;
};

#endif  // PATHSIMULATOR_VolSim_H