1
/***************************************************************************
2
* Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
29
# include <Inventor/SbBSPTree.h>
32
#include <Base/FileInfo.h>
33
#include <Base/Tools.h>
35
#include "SoFCVectorizeU3DAction.h"
40
class SoVectorizeItem {
43
this->type = UNDEFINED;
46
// quick and easy type system
56
float depth; // for depth sorting
59
class SoVectorizePoint : public SoVectorizeItem {
67
int vidx; // index to BSPtree coordinate
68
float size; // Coin size (pixels)
72
class SoVectorizeTriangle : public SoVectorizeItem {
74
SoVectorizeTriangle() {
75
this->type = TRIANGLE;
77
int vidx[3]; // indices to BSPtree coordinates
81
class SoVectorizeLine : public SoVectorizeItem {
89
this->pattern = 0xffff;
92
int vidx[2]; // indices to BSPtree coordinates
94
uint16_t pattern; // Coin line pattern
95
float width; // Coin line width (pixels)
98
class SoVectorizeText : public SoVectorizeItem {
104
this->justification = LEFT;
114
float fontsize; // size in normalized coordinates
116
SbVec2f pos; // pos in normalized coordinates
118
Justification justification;
121
class SoVectorizeImage : public SoVectorizeItem {
125
this->image.data = nullptr;
129
SbVec2f pos; // pos in normalized coordinates
130
SbVec2f size; // size in normalized coordinates
133
const unsigned char * data;
139
// ----------------------------------------------------------------
141
SoU3DVectorOutput::SoU3DVectorOutput() = default;
143
SoU3DVectorOutput::~SoU3DVectorOutput()
148
SbBool SoU3DVectorOutput::openFile (const char *filename)
150
Base::FileInfo fi(filename);
152
this->file.open(fi.toStdWString().c_str(), std::ios::out | std::ios::binary);
154
this->file.open(fi.filePath().c_str(), std::ios::out | std::ios::binary);
157
return this->file.is_open();
160
void SoU3DVectorOutput::closeFile ()
162
if (this->file.is_open())
166
std::fstream& SoU3DVectorOutput::getFileStream()
171
// ----------------------------------------------------------------
174
class SoFCVectorizeU3DActionP
177
explicit SoFCVectorizeU3DActionP(SoFCVectorizeU3DAction * p) {
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;
191
SoFCVectorizeU3DAction * publ;
195
void SoFCVectorizeU3DActionP::printText(const SoVectorizeText * item) const
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];
202
//std::ostream& str = publ->getU3DOutput()->getFileStream();
207
void SoFCVectorizeU3DActionP::printTriangle(const SoVectorizeTriangle * item) const
209
SbVec2f mul = publ->getRotatedViewportSize();
210
SbVec2f add = publ->getRotatedViewportStartpos();
212
const SbBSPTree & bsp = publ->getBSPTree();
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]);
224
this->printTriangle((SbVec3f*)v, (SbColor*)c);
227
void SoFCVectorizeU3DActionP::printTriangle(const SbVec3f * v, const SbColor * c) const
229
if (v[0] == v[1] || v[1] == v[2] || v[0] == v[2])
231
//uint32_t cc = c->getPackedValue();
233
//std::ostream& str = publ->getU3DOutput()->getFileStream();
238
void SoFCVectorizeU3DActionP::printCircle(const SbVec3f & v, const SbColor & c, const float radius) const
246
void SoFCVectorizeU3DActionP::printSquare(const SbVec3f & v, const SbColor & c, const float size) const
254
void SoFCVectorizeU3DActionP::printLine(const SoVectorizeLine * item) const
256
SbVec2f mul = publ->getRotatedViewportSize();
257
SbVec2f add = publ->getRotatedViewportStartpos();
259
const SbBSPTree & bsp = publ->getBSPTree();
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]);
271
//uint32_t cc = c->getPackedValue();
273
//std::ostream& str = publ->getU3DOutput()->getFileStream();
278
void SoFCVectorizeU3DActionP::printPoint(const SoVectorizePoint * item) const
284
void SoFCVectorizeU3DActionP::printImage(const SoVectorizeImage * item) const
290
// -------------------------------------------------------
292
SO_ACTION_SOURCE(SoFCVectorizeU3DAction)
294
void SoFCVectorizeU3DAction::initClass()
296
SO_ACTION_INIT_CLASS(SoFCVectorizeU3DAction, SoVectorizeAction);
297
//SO_ACTION_ADD_METHOD(SoNode, SoFCVectorizeU3DAction::actionMethod);
300
SoFCVectorizeU3DAction::SoFCVectorizeU3DAction()
302
SO_ACTION_CONSTRUCTOR(SoFCVectorizeU3DAction);
303
this->setOutput(new SoU3DVectorOutput);
304
this->p = new SoFCVectorizeU3DActionP(this);
307
SoFCVectorizeU3DAction::~SoFCVectorizeU3DAction()
313
SoFCVectorizeU3DAction::getU3DOutput() const
315
return static_cast<SoU3DVectorOutput*>(SoVectorizeAction::getOutput());
319
SoFCVectorizeU3DAction::actionMethod(SoAction * a, SoNode * n)
325
void SoFCVectorizeU3DAction::beginTraversal(SoNode * node)
327
inherited::beginTraversal(node);
330
void SoFCVectorizeU3DAction::endTraversal(SoNode * node)
332
inherited::endTraversal(node);
335
void SoFCVectorizeU3DAction::printHeader() const
337
std::ostream& str = this->getU3DOutput()->getFileStream();
338
str << "FILE_FORMAT \"IDTF\"" << std::endl
339
<< "FORMAT_VERSION 100" << std::endl;
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;
359
void SoFCVectorizeU3DAction::printFooter() const
363
void SoFCVectorizeU3DAction::printViewport() const
367
void SoFCVectorizeU3DAction::printBackground() const
369
//SbVec2f mul = getRotatedViewportSize();
370
//SbVec2f add = getRotatedViewportStartpos();
374
//x[1] = mul[0] - add[0];
376
//y[1] = mul[1] - add[1];
379
//(void)this->getBackgroundColor(bg);
380
//uint32_t cc = bg.getPackedValue();
382
//std::ostream& str = this->getU3DOutput()->getFileStream();
386
void SoFCVectorizeU3DAction::printItem(const SoVectorizeItem * item) const
388
switch (item->type) {
389
case SoVectorizeItem::TRIANGLE:
390
this->p->printTriangle(static_cast<const SoVectorizeTriangle*>(item));
392
case SoVectorizeItem::LINE:
393
this->p->printLine(static_cast<const SoVectorizeLine*>(item));
395
case SoVectorizeItem::POINT:
396
this->p->printPoint(static_cast<const SoVectorizePoint*>(item));
398
case SoVectorizeItem::TEXT:
399
this->p->printText(static_cast<const SoVectorizeText*>(item));
401
case SoVectorizeItem::IMAGE:
402
this->p->printImage(static_cast<const SoVectorizeImage*>(item));
405
assert(0 && "unsupported item");