FreeCAD

Форк
0
/
SoFCVectorizeU3DAction.cpp 
408 строк · 11.3 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net>     *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24

25
#ifndef _PreComp_
26
# include <qglobal.h>
27
# include <iomanip>
28
# include <ios>
29
# include <Inventor/SbBSPTree.h>
30
#endif
31

32
#include <Base/FileInfo.h>
33
#include <Base/Tools.h>
34

35
#include "SoFCVectorizeU3DAction.h"
36

37

38
using namespace Gui;
39

40
class SoVectorizeItem {
41
public:
42
    SoVectorizeItem() {
43
        this->type = UNDEFINED;
44
        this->depth = 0.0f;
45
    }
46
    // quick and easy type system
47
    enum Type {
48
        UNDEFINED,
49
        LINE,
50
        TRIANGLE,
51
        TEXT,
52
        POINT,
53
        IMAGE
54
    };
55
    int type;
56
    float depth; // for depth sorting
57
};
58

59
class SoVectorizePoint : public SoVectorizeItem {
60
public:
61
    SoVectorizePoint() {
62
        this->type = POINT;
63
        this->vidx = 0;
64
        this->size = 1.0f;
65
        this->col = 0;
66
    }
67
    int vidx;       // index to BSPtree coordinate
68
    float size;     // Coin size (pixels)
69
    uint32_t col;
70
};
71

72
class SoVectorizeTriangle : public SoVectorizeItem {
73
public:
74
    SoVectorizeTriangle() {
75
        this->type = TRIANGLE;
76
    }
77
    int vidx[3];      // indices to BSPtree coordinates
78
    uint32_t col[3];
79
};
80

81
class SoVectorizeLine : public SoVectorizeItem {
82
public:
83
    SoVectorizeLine() {
84
        this->type = LINE;
85
        vidx[0] = 0;
86
        vidx[1] = 0;
87
        col[0] = 0;
88
        col[1] = 0;
89
        this->pattern = 0xffff;
90
        this->width = 1.0f;
91
    }
92
    int vidx[2];       // indices to BSPtree coordinates
93
    uint32_t col[2];
94
    uint16_t pattern;  // Coin line pattern
95
    float width;       // Coin line width (pixels)
96
};
97

98
class SoVectorizeText : public SoVectorizeItem {
99
public:
100
    SoVectorizeText() {
101
        this->type = TEXT;
102
        this->fontsize = 10;
103
        this->col = 0;
104
        this->justification = LEFT;
105
    }
106

107
    enum Justification {
108
        LEFT,
109
        RIGHT,
110
        CENTER
111
    };
112

113
    SbName fontname;
114
    float fontsize;    // size in normalized coordinates
115
    SbString string;
116
    SbVec2f pos;       // pos in normalized coordinates
117
    uint32_t col;
118
    Justification justification;
119
};
120

121
class SoVectorizeImage : public SoVectorizeItem {
122
public:
123
    SoVectorizeImage() {
124
        this->type = IMAGE;
125
        this->image.data = nullptr;
126
        this->image.nc = 0;
127
    }
128

129
    SbVec2f pos;        // pos in normalized coordinates
130
    SbVec2f size;       // size in normalized coordinates
131

132
    struct Image {
133
        const unsigned char * data;
134
        SbVec2s size;
135
        int nc;
136
    } image;
137
};
138

139
// ----------------------------------------------------------------
140

141
SoU3DVectorOutput::SoU3DVectorOutput() = default;
142

143
SoU3DVectorOutput::~SoU3DVectorOutput()
144
{
145
    closeFile();
146
}
147

148
SbBool SoU3DVectorOutput::openFile (const char *filename)
149
{
150
    Base::FileInfo fi(filename);
151
#ifdef _MSC_VER
152
    this->file.open(fi.toStdWString().c_str(), std::ios::out | std::ios::binary);
153
#else
154
    this->file.open(fi.filePath().c_str(), std::ios::out | std::ios::binary);
155
#endif
156

157
    return this->file.is_open();
158
}
159

160
void SoU3DVectorOutput::closeFile ()
161
{
162
    if (this->file.is_open())
163
        this->file.close();
164
}
165

166
std::fstream& SoU3DVectorOutput::getFileStream()
167
{
168
    return this->file;
169
}
170

171
// ----------------------------------------------------------------
172

173
namespace Gui {
174
class SoFCVectorizeU3DActionP
175
{
176
public:
177
    explicit SoFCVectorizeU3DActionP(SoFCVectorizeU3DAction * p) {
178
        this->publ = p;
179
    }
180

181
    void printCircle(const SbVec3f & v, const SbColor & c, const float radius) const;
182
    void printSquare(const SbVec3f & v, const SbColor & c, const float size) const;
183
    void printTriangle(const SbVec3f * v, const SbColor * c) const;
184
    void printTriangle(const SoVectorizeTriangle * item) const;
185
    void printLine(const SoVectorizeLine * item) const;
186
    void printPoint(const SoVectorizePoint * item) const;
187
    void printText(const SoVectorizeText * item) const;
188
    void printImage(const SoVectorizeImage * item) const;
189

190
private:
191
    SoFCVectorizeU3DAction * publ;
192
};
193
}
194

195
void SoFCVectorizeU3DActionP::printText(const SoVectorizeText * item) const
196
{
197
    //SbVec2f mul = publ->getRotatedViewportSize();
198
    //SbVec2f add = publ->getRotatedViewportStartpos();
199
    //float posx = item->pos[0]*mul[0]+add[0];
200
    //float posy = item->pos[1]*mul[1]+add[1];
201

202
    //std::ostream& str = publ->getU3DOutput()->getFileStream();
203
    // todo
204
    Q_UNUSED(item);
205
}
206

207
void SoFCVectorizeU3DActionP::printTriangle(const SoVectorizeTriangle * item) const
208
{
209
    SbVec2f mul = publ->getRotatedViewportSize();
210
    SbVec2f add = publ->getRotatedViewportStartpos();
211

212
    const SbBSPTree & bsp = publ->getBSPTree();
213

214
    SbVec3f v[3];
215
    SbColor c[3];
216
    float t[3];
217

218
    for (int i = 0; i < 3; i++) {
219
        v[i] = bsp.getPoint(item->vidx[i]);
220
        v[i][0] = (v[i][0] * mul[0]) + add[0];
221
        v[i][1] = ((1.0f-v[i][1]) * mul[1]) + add[1];
222
        c[i].setPackedValue(item->col[i], t[i]);
223
    }
224
    this->printTriangle((SbVec3f*)v, (SbColor*)c);
225
}
226

227
void SoFCVectorizeU3DActionP::printTriangle(const SbVec3f * v, const SbColor * c) const
228
{
229
    if (v[0] == v[1] || v[1] == v[2] || v[0] == v[2])
230
        return;
231
    //uint32_t cc = c->getPackedValue();
232

233
    //std::ostream& str = publ->getU3DOutput()->getFileStream();
234
    // todo
235
    Q_UNUSED(c);
236
}
237

238
void SoFCVectorizeU3DActionP::printCircle(const SbVec3f & v, const SbColor & c, const float radius) const
239
{
240
    // todo
241
    Q_UNUSED(v);
242
    Q_UNUSED(c);
243
    Q_UNUSED(radius);
244
}
245

246
void SoFCVectorizeU3DActionP::printSquare(const SbVec3f & v, const SbColor & c, const float size) const
247
{
248
    // todo
249
    Q_UNUSED(v);
250
    Q_UNUSED(c);
251
    Q_UNUSED(size);
252
}
253

254
void SoFCVectorizeU3DActionP::printLine(const SoVectorizeLine * item) const
255
{
256
    SbVec2f mul = publ->getRotatedViewportSize();
257
    SbVec2f add = publ->getRotatedViewportStartpos();
258

259
    const SbBSPTree & bsp = publ->getBSPTree();
260

261
    SbVec3f v[2];
262
    SbColor c[2];
263
    float t[2];
264

265
    for (int i = 0; i < 2; i++) {
266
        v[i] = bsp.getPoint(item->vidx[i]);
267
        v[i][0] = (v[i][0] * mul[0]) + add[0];
268
        v[i][1] = ((1.0f-v[i][1]) * mul[1]) + add[1];
269
        c[i].setPackedValue(item->col[i], t[i]);
270
    }
271
    //uint32_t cc = c->getPackedValue();
272

273
    //std::ostream& str = publ->getU3DOutput()->getFileStream();
274
    // todo
275
    Q_UNUSED(item);
276
}
277

278
void SoFCVectorizeU3DActionP::printPoint(const SoVectorizePoint * item) const
279
{
280
    // todo
281
    Q_UNUSED(item);
282
}
283

284
void SoFCVectorizeU3DActionP::printImage(const SoVectorizeImage * item) const
285
{
286
    // todo
287
    Q_UNUSED(item);
288
}
289

290
// -------------------------------------------------------
291

292
SO_ACTION_SOURCE(SoFCVectorizeU3DAction)
293

294
void SoFCVectorizeU3DAction::initClass()
295
{
296
    SO_ACTION_INIT_CLASS(SoFCVectorizeU3DAction, SoVectorizeAction);
297
    //SO_ACTION_ADD_METHOD(SoNode, SoFCVectorizeU3DAction::actionMethod);
298
}
299

300
SoFCVectorizeU3DAction::SoFCVectorizeU3DAction()
301
{
302
    SO_ACTION_CONSTRUCTOR(SoFCVectorizeU3DAction);
303
    this->setOutput(new SoU3DVectorOutput);
304
    this->p = new SoFCVectorizeU3DActionP(this);
305
}
306

307
SoFCVectorizeU3DAction::~SoFCVectorizeU3DAction()
308
{
309
    delete this->p;
310
}
311

312
SoU3DVectorOutput *
313
SoFCVectorizeU3DAction::getU3DOutput() const
314
{
315
    return static_cast<SoU3DVectorOutput*>(SoVectorizeAction::getOutput());
316
}
317

318
void
319
SoFCVectorizeU3DAction::actionMethod(SoAction * a, SoNode * n)
320
{
321
    Q_UNUSED(a);
322
    Q_UNUSED(n);
323
}
324

325
void SoFCVectorizeU3DAction::beginTraversal(SoNode * node)
326
{
327
    inherited::beginTraversal(node);
328
}
329

330
void SoFCVectorizeU3DAction::endTraversal(SoNode * node)
331
{
332
    inherited::endTraversal(node);
333
}
334

335
void SoFCVectorizeU3DAction::printHeader() const
336
{
337
    std::ostream& str = this->getU3DOutput()->getFileStream();
338
    str << "FILE_FORMAT \"IDTF\"" << std::endl
339
        << "FORMAT_VERSION 100" << std::endl;
340

341
    str << Base::tabs(0) << "NODE \"MODEL\" {" << std::endl;
342
    str << Base::tabs(1) << "NODE_NAME \"FreeCAD\"" << std::endl;
343
    str << Base::tabs(1) << "PARENT_LIST {" << std::endl;
344
    str << Base::tabs(2) << "PARENT_COUNT 1" << std::endl;
345
    str << Base::tabs(2) << "PARENT 0 {" << std::endl;
346
    str << Base::tabs(3) << "PARENT_NAME \"<NULL>\"" << std::endl;
347
    str << Base::tabs(3) << "PARENT_TM {" << std::endl;
348
    str << Base::tabs(4) << "1.000000 0.000000 0.000000 0.000000" << std::endl;
349
    str << Base::tabs(4) << "0.000000 1.000000 0.000000 0.000000" << std::endl;
350
    str << Base::tabs(4) << "0.000000 0.000000 1.000000 0.000000" << std::endl;
351
    str << Base::tabs(4) << "0.000000 0.000000 0.000000 1.000000" << std::endl;
352
    str << Base::tabs(3) << "}" << std::endl;
353
    str << Base::tabs(2) << "}" << std::endl;
354
    str << Base::tabs(1) << "}" << std::endl;
355
    str << Base::tabs(1) << "RESOURCE_NAME \"FreeCAD\"" << std::endl;
356
    str << Base::tabs(0) << "}" << std::endl;
357
}
358

359
void SoFCVectorizeU3DAction::printFooter() const
360
{
361
}
362

363
void SoFCVectorizeU3DAction::printViewport() const
364
{
365
}
366

367
void SoFCVectorizeU3DAction::printBackground() const
368
{
369
    //SbVec2f mul = getRotatedViewportSize();
370
    //SbVec2f add = getRotatedViewportStartpos();
371

372
    //float x[2],y[2];
373
    //x[0] = add[0];
374
    //x[1] = mul[0] - add[0];
375
    //y[0] = add[1];
376
    //y[1] = mul[1] - add[1];
377

378
    //SbColor bg;
379
    //(void)this->getBackgroundColor(bg);
380
    //uint32_t cc = bg.getPackedValue();
381

382
    //std::ostream& str = this->getU3DOutput()->getFileStream();
383
    // todo
384
}
385

386
void SoFCVectorizeU3DAction::printItem(const SoVectorizeItem * item) const
387
{
388
    switch (item->type) {
389
    case SoVectorizeItem::TRIANGLE:
390
        this->p->printTriangle(static_cast<const SoVectorizeTriangle*>(item));
391
        break;
392
    case SoVectorizeItem::LINE:
393
        this->p->printLine(static_cast<const SoVectorizeLine*>(item));
394
        break;
395
    case SoVectorizeItem::POINT:
396
        this->p->printPoint(static_cast<const SoVectorizePoint*>(item));
397
        break;
398
    case SoVectorizeItem::TEXT:
399
        this->p->printText(static_cast<const SoVectorizeText*>(item));
400
        break;
401
    case SoVectorizeItem::IMAGE:
402
        this->p->printImage(static_cast<const SoVectorizeImage*>(item));
403
        break;
404
    default:
405
        assert(0 && "unsupported item");
406
        break;
407
    }
408
}
409

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.