| 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 |
|
| 24 |
#include <qglobal.h> |
| 25 |
#include <iomanip> |
| 26 |
#include <ios> |
| 27 |
#include <Inventor/SbBSPTree.h> |
| 28 |
|
| 29 |
|
| 30 |
#include <Base/FileInfo.h> |
| 31 |
#include <Base/Tools.h> |
| 32 |
|
| 33 |
#include "SoFCVectorizeU3DAction.h" |
| 34 |
|
| 35 |
|
| 36 |
using namespace Gui; |
| 37 |
|
| 38 |
class SoVectorizeItem |
| 39 |
{ |
| 40 |
public: |
| 41 |
SoVectorizeItem() |
| 42 |
{ |
| 43 |
this->type = UNDEFINED; |
| 44 |
this->depth = 0.0f; |
| 45 |
} |
| 46 |
// quick and easy type system |
| 47 |
enum Type |
| 48 |
{ |
| 49 |
UNDEFINED, |
| 50 |
LINE, |
| 51 |
TRIANGLE, |
| 52 |
TEXT, |
| 53 |
POINT, |
| 54 |
IMAGE |
| 55 |
}; |
| 56 |
int type; |
| 57 |
float depth; // for depth sorting |
| 58 |
}; |
| 59 |
|
| 60 |
class SoVectorizePoint: public SoVectorizeItem |
| 61 |
{ |
| 62 |
public: |
| 63 |
SoVectorizePoint() |
| 64 |
{ |
| 65 |
this->type = POINT; |
| 66 |
this->vidx = 0; |
| 67 |
this->size = 1.0f; |
| 68 |
this->col = 0; |
| 69 |
} |
| 70 |
int vidx; // index to BSPtree coordinate |
| 71 |
float size; // Coin size (pixels) |
| 72 |
uint32_t col; |
| 73 |
}; |
| 74 |
|
| 75 |
class SoVectorizeTriangle: public SoVectorizeItem |
| 76 |
{ |
| 77 |
public: |
| 78 |
SoVectorizeTriangle() |
| 79 |
{ |
| 80 |
this->type = TRIANGLE; |
| 81 |
} |
| 82 |
int vidx[3]; // indices to BSPtree coordinates |
| 83 |
uint32_t col[3]; |
| 84 |
}; |
| 85 |
|
| 86 |
class SoVectorizeLine: public SoVectorizeItem |
| 87 |
{ |
| 88 |
public: |
| 89 |
SoVectorizeLine() |
| 90 |
{ |
| 91 |
this->type = LINE; |
| 92 |
vidx[0] = 0; |
| 93 |
vidx[1] = 0; |
| 94 |
col[0] = 0; |
| 95 |
col[1] = 0; |
| 96 |
this->pattern = 0xffff; |
| 97 |
this->width = 1.0f; |
| 98 |
} |
| 99 |
int vidx[2]; // indices to BSPtree coordinates |
| 100 |
uint32_t col[2]; |
| 101 |
uint16_t pattern; // Coin line pattern |
| 102 |
float width; // Coin line width (pixels) |
| 103 |
}; |
| 104 |
|
| 105 |
class SoVectorizeText: public SoVectorizeItem |
| 106 |
{ |
| 107 |
public: |
| 108 |
SoVectorizeText() |
| 109 |
{ |
| 110 |
this->type = TEXT; |
| 111 |
this->fontsize = 10; |
| 112 |
this->col = 0; |
| 113 |
this->justification = LEFT; |
| 114 |
} |
| 115 |
|
| 116 |
enum Justification |
| 117 |
{ |
| 118 |
LEFT, |
| 119 |
RIGHT, |
| 120 |
CENTER |
| 121 |
}; |
| 122 |
|
| 123 |
SbName fontname; |
| 124 |
float fontsize; // size in normalized coordinates |
| 125 |
SbString string; |
| 126 |
SbVec2f pos; // pos in normalized coordinates |
| 127 |
uint32_t col; |
| 128 |
Justification justification; |
| 129 |
}; |
| 130 |
|
| 131 |
class SoVectorizeImage: public SoVectorizeItem |
| 132 |
{ |
| 133 |
public: |
| 134 |
SoVectorizeImage() |
| 135 |
{ |
| 136 |
this->type = IMAGE; |
| 137 |
this->image.data = nullptr; |
| 138 |
this->image.nc = 0; |
| 139 |
} |
| 140 |
|
| 141 |
SbVec2f pos; // pos in normalized coordinates |
| 142 |
SbVec2f size; // size in normalized coordinates |
| 143 |
|
| 144 |
struct Image |
| 145 |
{ |
| 146 |
const unsigned char* data; |
| 147 |
SbVec2s size; |
| 148 |
int nc; |
| 149 |
} image; |
| 150 |
}; |
| 151 |
|
| 152 |
// ---------------------------------------------------------------- |
| 153 |
|
| 154 |
SoU3DVectorOutput::SoU3DVectorOutput() = default; |
| 155 |
|
| 156 |
SoU3DVectorOutput::~SoU3DVectorOutput() |
| 157 |
{ |
| 158 |
closeFile(); |
| 159 |
} |
| 160 |
|
| 161 |
SbBool SoU3DVectorOutput::openFile(const char* filename) |
| 162 |
{ |
| 163 |
Base::FileInfo fi(filename); |
| 164 |
#ifdef _MSC_VER |
| 165 |
this->file.open(fi.toStdWString().c_str(), std::ios::out | std::ios::binary); |
| 166 |
#else |
| 167 |
this->file.open(fi.filePath().c_str(), std::ios::out | std::ios::binary); |
| 168 |
#endif |
| 169 |
|
| 170 |
return this->file.is_open(); |
| 171 |
} |
| 172 |
|
| 173 |
void SoU3DVectorOutput::closeFile() |
| 174 |
{ |
| 175 |
if (this->file.is_open()) { |
| 176 |
this->file.close(); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
std::fstream& SoU3DVectorOutput::getFileStream() |
| 181 |
{ |
| 182 |
return this->file; |
| 183 |
} |
| 184 |
|
| 185 |
// ---------------------------------------------------------------- |
| 186 |
|
| 187 |
namespace Gui |
| 188 |
{ |
| 189 |
class SoFCVectorizeU3DActionP |
| 190 |
{ |
| 191 |
public: |
| 192 |
explicit SoFCVectorizeU3DActionP(SoFCVectorizeU3DAction* p) |
| 193 |
{ |
| 194 |
this->publ = p; |
| 195 |
} |
| 196 |
|
| 197 |
void printCircle(const SbVec3f& v, const SbColor& c, const float radius) const; |
| 198 |
void printSquare(const SbVec3f& v, const SbColor& c, const float size) const; |
| 199 |
void printTriangle(const SbVec3f* v, const SbColor* c) const; |
| 200 |
void printTriangle(const SoVectorizeTriangle* item) const; |
| 201 |
void printLine(const SoVectorizeLine* item) const; |
| 202 |
void printPoint(const SoVectorizePoint* item) const; |
| 203 |
void printText(const SoVectorizeText* item) const; |
| 204 |
void printImage(const SoVectorizeImage* item) const; |
| 205 |
|
| 206 |
private: |
| 207 |
SoFCVectorizeU3DAction* publ; |
| 208 |
}; |
| 209 |
} // namespace Gui |
| 210 |
|
| 211 |
void SoFCVectorizeU3DActionP::printText(const SoVectorizeText* item) const |
| 212 |
{ |
| 213 |
// SbVec2f mul = publ->getRotatedViewportSize(); |
| 214 |
// SbVec2f add = publ->getRotatedViewportStartpos(); |
| 215 |
// float posx = item->pos[0]*mul[0]+add[0]; |
| 216 |
// float posy = item->pos[1]*mul[1]+add[1]; |
| 217 |
|
| 218 |
// std::ostream& str = publ->getU3DOutput()->getFileStream(); |
| 219 |
// todo |
| 220 |
Q_UNUSED(item); |
| 221 |
} |
| 222 |
|
| 223 |
void SoFCVectorizeU3DActionP::printTriangle(const SoVectorizeTriangle* item) const |
| 224 |
{ |
| 225 |
SbVec2f mul = publ->getRotatedViewportSize(); |
| 226 |
SbVec2f add = publ->getRotatedViewportStartpos(); |
| 227 |
|
| 228 |
const SbBSPTree& bsp = publ->getBSPTree(); |
| 229 |
|
| 230 |
SbVec3f v[3]; |
| 231 |
SbColor c[3]; |
| 232 |
float t[3]; |
| 233 |
|
| 234 |
for (int i = 0; i < 3; i++) { |
| 235 |
v[i] = bsp.getPoint(item->vidx[i]); |
| 236 |
v[i][0] = (v[i][0] * mul[0]) + add[0]; |
| 237 |
v[i][1] = ((1.0f - v[i][1]) * mul[1]) + add[1]; |
| 238 |
c[i].setPackedValue(item->col[i], t[i]); |
| 239 |
} |
| 240 |
this->printTriangle((SbVec3f*)v, (SbColor*)c); |
| 241 |
} |
| 242 |
|
| 243 |
void SoFCVectorizeU3DActionP::printTriangle(const SbVec3f* v, const SbColor* c) const |
| 244 |
{ |
| 245 |
if (v[0] == v[1] || v[1] == v[2] || v[0] == v[2]) { |
| 246 |
return; |
| 247 |
} |
| 248 |
// uint32_t cc = c->getPackedValue(); |
| 249 |
|
| 250 |
// std::ostream& str = publ->getU3DOutput()->getFileStream(); |
| 251 |
// todo |
| 252 |
Q_UNUSED(c); |
| 253 |
} |
| 254 |
|
| 255 |
void SoFCVectorizeU3DActionP::printCircle(const SbVec3f& v, const SbColor& c, const float radius) const |
| 256 |
{ |
| 257 |
// todo |
| 258 |
Q_UNUSED(v); |
| 259 |
Q_UNUSED(c); |
| 260 |
Q_UNUSED(radius); |
| 261 |
} |
| 262 |
|
| 263 |
void SoFCVectorizeU3DActionP::printSquare(const SbVec3f& v, const SbColor& c, const float size) const |
| 264 |
{ |
| 265 |
// todo |
| 266 |
Q_UNUSED(v); |
| 267 |
Q_UNUSED(c); |
| 268 |
Q_UNUSED(size); |
| 269 |
} |
| 270 |
|
| 271 |
void SoFCVectorizeU3DActionP::printLine(const SoVectorizeLine* item) const |
| 272 |
{ |
| 273 |
SbVec2f mul = publ->getRotatedViewportSize(); |
| 274 |
SbVec2f add = publ->getRotatedViewportStartpos(); |
| 275 |
|
| 276 |
const SbBSPTree& bsp = publ->getBSPTree(); |
| 277 |
|
| 278 |
SbVec3f v[2]; |
| 279 |
SbColor c[2]; |
| 280 |
float t[2]; |
| 281 |
|
| 282 |
for (int i = 0; i < 2; i++) { |
| 283 |
v[i] = bsp.getPoint(item->vidx[i]); |
| 284 |
v[i][0] = (v[i][0] * mul[0]) + add[0]; |
| 285 |
v[i][1] = ((1.0f - v[i][1]) * mul[1]) + add[1]; |
| 286 |
c[i].setPackedValue(item->col[i], t[i]); |
| 287 |
} |
| 288 |
// uint32_t cc = c->getPackedValue(); |
| 289 |
|
| 290 |
// std::ostream& str = publ->getU3DOutput()->getFileStream(); |
| 291 |
// todo |
| 292 |
Q_UNUSED(item); |
| 293 |
} |
| 294 |
|
| 295 |
void SoFCVectorizeU3DActionP::printPoint(const SoVectorizePoint* item) const |
| 296 |
{ |
| 297 |
// todo |
| 298 |
Q_UNUSED(item); |
| 299 |
} |
| 300 |
|
| 301 |
void SoFCVectorizeU3DActionP::printImage(const SoVectorizeImage* item) const |
| 302 |
{ |
| 303 |
// todo |
| 304 |
Q_UNUSED(item); |
| 305 |
} |
| 306 |
|
| 307 |
// ------------------------------------------------------- |
| 308 |
|
| 309 |
SO_ACTION_SOURCE(SoFCVectorizeU3DAction) |
| 310 |
|
| 311 |
void SoFCVectorizeU3DAction::initClass() |
| 312 |
{ |
| 313 |
SO_ACTION_INIT_CLASS(SoFCVectorizeU3DAction, SoVectorizeAction); |
| 314 |
// SO_ACTION_ADD_METHOD(SoNode, SoFCVectorizeU3DAction::actionMethod); |
| 315 |
} |
| 316 |
|
| 317 |
SoFCVectorizeU3DAction::SoFCVectorizeU3DAction() |
| 318 |
{ |
| 319 |
SO_ACTION_CONSTRUCTOR(SoFCVectorizeU3DAction); |
| 320 |
this->setOutput(new SoU3DVectorOutput); |
| 321 |
this->p = new SoFCVectorizeU3DActionP(this); |
| 322 |
} |
| 323 |
|
| 324 |
SoFCVectorizeU3DAction::~SoFCVectorizeU3DAction() |
| 325 |
{ |
| 326 |
delete this->p; |
| 327 |
} |
| 328 |
|
| 329 |
SoU3DVectorOutput* SoFCVectorizeU3DAction::getU3DOutput() const |
| 330 |
{ |
| 331 |
return static_cast<SoU3DVectorOutput*>(SoVectorizeAction::getOutput()); |
| 332 |
} |
| 333 |
|
| 334 |
void SoFCVectorizeU3DAction::actionMethod(SoAction* a, SoNode* n) |
| 335 |
{ |
| 336 |
Q_UNUSED(a); |
| 337 |
Q_UNUSED(n); |
| 338 |
} |
| 339 |
|
| 340 |
void SoFCVectorizeU3DAction::beginTraversal(SoNode* node) |
| 341 |
{ |
| 342 |
inherited::beginTraversal(node); |
| 343 |
} |
| 344 |
|
| 345 |
void SoFCVectorizeU3DAction::endTraversal(SoNode* node) |
| 346 |
{ |
| 347 |
inherited::endTraversal(node); |
| 348 |
} |
| 349 |
|
| 350 |
void SoFCVectorizeU3DAction::printHeader() const |
| 351 |
{ |
| 352 |
std::ostream& str = this->getU3DOutput()->getFileStream(); |
| 353 |
str << "FILE_FORMAT \"IDTF\"" << std::endl << "FORMAT_VERSION 100" << std::endl; |
| 354 |
|
| 355 |
str << Base::tabs(0) << "NODE \"MODEL\" {" << std::endl; |
| 356 |
str << Base::tabs(1) << "NODE_NAME \"FreeCAD\"" << std::endl; |
| 357 |
str << Base::tabs(1) << "PARENT_LIST {" << std::endl; |
| 358 |
str << Base::tabs(2) << "PARENT_COUNT 1" << std::endl; |
| 359 |
str << Base::tabs(2) << "PARENT 0 {" << std::endl; |
| 360 |
str << Base::tabs(3) << "PARENT_NAME \"<NULL>\"" << std::endl; |
| 361 |
str << Base::tabs(3) << "PARENT_TM {" << std::endl; |
| 362 |
str << Base::tabs(4) << "1.000000 0.000000 0.000000 0.000000" << std::endl; |
| 363 |
str << Base::tabs(4) << "0.000000 1.000000 0.000000 0.000000" << std::endl; |
| 364 |
str << Base::tabs(4) << "0.000000 0.000000 1.000000 0.000000" << std::endl; |
| 365 |
str << Base::tabs(4) << "0.000000 0.000000 0.000000 1.000000" << std::endl; |
| 366 |
str << Base::tabs(3) << "}" << std::endl; |
| 367 |
str << Base::tabs(2) << "}" << std::endl; |
| 368 |
str << Base::tabs(1) << "}" << std::endl; |
| 369 |
str << Base::tabs(1) << "RESOURCE_NAME \"FreeCAD\"" << std::endl; |
| 370 |
str << Base::tabs(0) << "}" << std::endl; |
| 371 |
} |
| 372 |
|
| 373 |
void SoFCVectorizeU3DAction::printFooter() const |
| 374 |
{} |
| 375 |
|
| 376 |
void SoFCVectorizeU3DAction::printViewport() const |
| 377 |
{} |
| 378 |
|
| 379 |
void SoFCVectorizeU3DAction::printBackground() const |
| 380 |
{ |
| 381 |
// SbVec2f mul = getRotatedViewportSize(); |
| 382 |
// SbVec2f add = getRotatedViewportStartpos(); |
| 383 |
|
| 384 |
// float x[2],y[2]; |
| 385 |
// x[0] = add[0]; |
| 386 |
// x[1] = mul[0] - add[0]; |
| 387 |
// y[0] = add[1]; |
| 388 |
// y[1] = mul[1] - add[1]; |
| 389 |
|
| 390 |
// SbColor bg; |
| 391 |
//(void)this->getBackgroundColor(bg); |
| 392 |
// uint32_t cc = bg.getPackedValue(); |
| 393 |
|
| 394 |
// std::ostream& str = this->getU3DOutput()->getFileStream(); |
| 395 |
// todo |
| 396 |
} |
| 397 |
|
| 398 |
void SoFCVectorizeU3DAction::printItem(const SoVectorizeItem* item) const |
| 399 |
{ |
| 400 |
switch (item->type) { |
| 401 |
case SoVectorizeItem::TRIANGLE: |
| 402 |
this->p->printTriangle(static_cast<const SoVectorizeTriangle*>(item)); |
| 403 |
break; |
| 404 |
case SoVectorizeItem::LINE: |
| 405 |
this->p->printLine(static_cast<const SoVectorizeLine*>(item)); |
| 406 |
break; |
| 407 |
case SoVectorizeItem::POINT: |
| 408 |
this->p->printPoint(static_cast<const SoVectorizePoint*>(item)); |
| 409 |
break; |
| 410 |
case SoVectorizeItem::TEXT: |
| 411 |
this->p->printText(static_cast<const SoVectorizeText*>(item)); |
| 412 |
break; |
| 413 |
case SoVectorizeItem::IMAGE: |
| 414 |
this->p->printImage(static_cast<const SoVectorizeImage*>(item)); |
| 415 |
break; |
| 416 |
default: |
| 417 |
assert(0 && "unsupported item"); |
| 418 |
break; |
| 419 |
} |
| 420 |
} |