SoFCVectorizeU3DAction.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <iomanip>
00027 # include <ios>
00028 #endif
00029 #include <Inventor/SbBasic.h>
00030 #include <Inventor/SbBSPTree.h>
00031 
00032 #include <Base/FileInfo.h>
00033 #include <Base/Tools.h>
00034 #include "SoFCVectorizeU3DAction.h"
00035 
00036 using namespace Gui;
00037 
00038 class SoVectorizeItem {
00039 public:
00040     SoVectorizeItem() {
00041         this->type = UNDEFINED;
00042         this->depth = 0.0f;
00043     }
00044     // quick and easy type system
00045     enum Type {
00046         UNDEFINED,
00047         LINE,
00048         TRIANGLE,
00049         TEXT,
00050         POINT,
00051         IMAGE
00052     };
00053     int type;
00054     float depth; // for depth sorting
00055 };
00056 
00057 class SoVectorizePoint : public SoVectorizeItem {
00058 public:
00059     SoVectorizePoint(void) {
00060         this->type = POINT;
00061         this->size = 1.0f;
00062     }
00063     int vidx;       // index to BSPtree coordinate
00064     float size;     // Coin size (pixels)
00065     uint32_t col;
00066 };
00067 
00068 class SoVectorizeTriangle : public SoVectorizeItem {
00069 public:
00070     SoVectorizeTriangle(void) {
00071         this->type = TRIANGLE;
00072     }
00073     int vidx[3];      // indices to BSPtree coordinates
00074     uint32_t col[3];
00075 };
00076 
00077 class SoVectorizeLine : public SoVectorizeItem {
00078 public:
00079     SoVectorizeLine(void) {
00080         this->type = LINE;
00081         this->pattern = 0xffff;
00082         this->width = 1.0f;
00083     }
00084     int vidx[2];       // indices to BSPtree coordinates
00085     uint32_t col[2];
00086     uint16_t pattern;  // Coin line pattern
00087     float width;       // Coin line width (pixels)
00088 };
00089 
00090 class SoVectorizeText : public SoVectorizeItem {
00091 public:
00092     SoVectorizeText(void) {
00093         this->type = TEXT;
00094     }
00095 
00096     enum Justification {
00097         LEFT,
00098         RIGHT,
00099         CENTER
00100     };
00101 
00102     SbName fontname;
00103     float fontsize;    // size in normalized coordinates
00104     SbString string;
00105     SbVec2f pos;       // pos in normalized coordinates
00106     uint32_t col;
00107     Justification justification;
00108 };
00109 
00110 class SoVectorizeImage : public SoVectorizeItem {
00111 public:
00112     SoVectorizeImage(void) {
00113         this->type = IMAGE;
00114     }
00115 
00116     SbVec2f pos;        // pos in normalized coordinates
00117     SbVec2f size;       // size in normalized coordinates
00118 
00119     struct Image {
00120         const unsigned char * data;
00121         SbVec2s size;
00122         int nc;
00123     } image;
00124 };
00125 
00126 // ----------------------------------------------------------------
00127 
00128 SoU3DVectorOutput::SoU3DVectorOutput()
00129 {
00130 }
00131 
00132 SoU3DVectorOutput::~SoU3DVectorOutput()
00133 {
00134     closeFile();
00135 }
00136 
00137 SbBool SoU3DVectorOutput::openFile (const char *filename)
00138 {
00139     Base::FileInfo fi(filename);
00140 #ifdef _MSC_VER
00141     this->file.open(fi.toStdWString().c_str(), std::ios::out | std::ios::binary);
00142 #else
00143     this->file.open(fi.filePath().c_str(), std::ios::out | std::ios::binary);
00144 #endif
00145 
00146     return this->file.is_open();
00147 }
00148 
00149 void SoU3DVectorOutput::closeFile (void)
00150 {
00151     if (this->file.is_open())
00152         this->file.close();
00153 }
00154 
00155 std::fstream& SoU3DVectorOutput::getFileStream()
00156 {
00157     return this->file;
00158 }
00159 
00160 // ----------------------------------------------------------------
00161 
00162 namespace Gui {
00163 class SoFCVectorizeU3DActionP
00164 {
00165 public:
00166     SoFCVectorizeU3DActionP(SoFCVectorizeU3DAction * p) {
00167         this->publ = p;
00168     }
00169 
00170     void printCircle(const SbVec3f & v, const SbColor & c, const float radius) const;
00171     void printSquare(const SbVec3f & v, const SbColor & c, const float size) const;
00172     void printTriangle(const SbVec3f * v, const SbColor * c) const;
00173     void printTriangle(const SoVectorizeTriangle * item) const;
00174     void printLine(const SoVectorizeLine * item) const;
00175     void printPoint(const SoVectorizePoint * item) const;
00176     void printText(const SoVectorizeText * item) const;
00177     void printImage(const SoVectorizeImage * item) const;
00178 
00179 private:
00180     SoFCVectorizeU3DAction * publ;
00181 };
00182 }
00183 
00184 void SoFCVectorizeU3DActionP::printText(const SoVectorizeText * item) const
00185 {
00186     //SbVec2f mul = publ->getRotatedViewportSize();
00187     //SbVec2f add = publ->getRotatedViewportStartpos();
00188     //float posx = item->pos[0]*mul[0]+add[0];
00189     //float posy = item->pos[1]*mul[1]+add[1];
00190 
00191     //std::ostream& str = publ->getU3DOutput()->getFileStream();
00192     // todo
00193 }
00194 
00195 void SoFCVectorizeU3DActionP::printTriangle(const SoVectorizeTriangle * item) const
00196 {
00197     SbVec2f mul = publ->getRotatedViewportSize();
00198     SbVec2f add = publ->getRotatedViewportStartpos();
00199 
00200     const SbBSPTree & bsp = publ->getBSPTree();
00201 
00202     SbVec3f v[3];
00203     SbColor c[3];
00204     float t[3];
00205 
00206     for (int i = 0; i < 3; i++) {
00207         v[i] = bsp.getPoint(item->vidx[i]);
00208         v[i][0] = (v[i][0] * mul[0]) + add[0];
00209         v[i][1] = ((1.0f-v[i][1]) * mul[1]) + add[1];
00210         c[i].setPackedValue(item->col[i], t[i]);
00211     }
00212     this->printTriangle((SbVec3f*)v, (SbColor*)c);
00213 }
00214 
00215 void SoFCVectorizeU3DActionP::printTriangle(const SbVec3f * v, const SbColor * c) const
00216 {
00217     if (v[0] == v[1] || v[1] == v[2] || v[0] == v[2]) return;
00218     //uint32_t cc = c->getPackedValue();
00219 
00220     //std::ostream& str = publ->getU3DOutput()->getFileStream();
00221     // todo
00222 }
00223 
00224 void SoFCVectorizeU3DActionP::printCircle(const SbVec3f & v, const SbColor & c, const float radius) const
00225 {
00226     // todo
00227 }
00228 
00229 void SoFCVectorizeU3DActionP::printSquare(const SbVec3f & v, const SbColor & c, const float size) const
00230 {
00231     // todo
00232 }
00233 
00234 void SoFCVectorizeU3DActionP::printLine(const SoVectorizeLine * item) const
00235 {
00236     SbVec2f mul = publ->getRotatedViewportSize();
00237     SbVec2f add = publ->getRotatedViewportStartpos();
00238 
00239     const SbBSPTree & bsp = publ->getBSPTree();
00240 
00241     SbVec3f v[2];
00242     SbColor c[2];
00243     float t[2];
00244 
00245     for (int i = 0; i < 2; i++) {
00246         v[i] = bsp.getPoint(item->vidx[i]);
00247         v[i][0] = (v[i][0] * mul[0]) + add[0];
00248         v[i][1] = ((1.0f-v[i][1]) * mul[1]) + add[1];
00249         c[i].setPackedValue(item->col[i], t[i]);
00250     }
00251     //uint32_t cc = c->getPackedValue();
00252 
00253     //std::ostream& str = publ->getU3DOutput()->getFileStream();
00254     // todo
00255 }
00256 
00257 void SoFCVectorizeU3DActionP::printPoint(const SoVectorizePoint * item) const
00258 {
00259     // todo
00260 }
00261 
00262 void SoFCVectorizeU3DActionP::printImage(const SoVectorizeImage * item) const
00263 {
00264     // todo
00265 }
00266 
00267 // -------------------------------------------------------
00268 
00269 SO_ACTION_SOURCE(SoFCVectorizeU3DAction);
00270 
00271 void SoFCVectorizeU3DAction::initClass(void)
00272 {
00273     SO_ACTION_INIT_CLASS(SoFCVectorizeU3DAction, SoVectorizeAction);
00274     //SO_ACTION_ADD_METHOD(SoNode, SoFCVectorizeU3DAction::actionMethod);
00275 }
00276 
00277 SoFCVectorizeU3DAction::SoFCVectorizeU3DAction()
00278 {
00279     SO_ACTION_CONSTRUCTOR(SoFCVectorizeU3DAction);
00280     this->setOutput(new SoU3DVectorOutput);
00281     this->p = new SoFCVectorizeU3DActionP(this);
00282 }
00283 
00284 SoFCVectorizeU3DAction::~SoFCVectorizeU3DAction()
00285 {
00286     delete this->p;
00287 }
00288 
00289 SoU3DVectorOutput *
00290 SoFCVectorizeU3DAction::getU3DOutput(void) const
00291 {
00292     return static_cast<SoU3DVectorOutput*>(SoVectorizeAction::getOutput());
00293 }
00294 
00295 void
00296 SoFCVectorizeU3DAction::actionMethod(SoAction * a, SoNode * n)
00297 {
00298 }
00299 
00300 void SoFCVectorizeU3DAction::beginTraversal(SoNode * node)
00301 {
00302     inherited::beginTraversal(node);
00303 }
00304 
00305 void SoFCVectorizeU3DAction::endTraversal(SoNode * node)
00306 {
00307     inherited::endTraversal(node);
00308 }
00309 
00310 void SoFCVectorizeU3DAction::printHeader(void) const
00311 {
00312     std::ostream& str = this->getU3DOutput()->getFileStream();
00313     str << "FILE_FORMAT \"IDTF\"" << std::endl
00314         << "FORMAT_VERSION 100" << std::endl;
00315 
00316     str << Base::tabs(0) << "NODE \"MODEL\" {" << std::endl;
00317     str << Base::tabs(1) << "NODE_NAME \"FreeCAD\"" << std::endl;
00318     str << Base::tabs(1) << "PARENT_LIST {" << std::endl;
00319     str << Base::tabs(2) << "PARENT_COUNT 1" << std::endl;
00320     str << Base::tabs(2) << "PARENT 0 {" << std::endl;
00321     str << Base::tabs(3) << "PARENT_NAME \"<NULL>\"" << std::endl;
00322     str << Base::tabs(3) << "PARENT_TM {" << std::endl;
00323     str << Base::tabs(4) << "1.000000 0.000000 0.000000 0.000000" << std::endl;
00324     str << Base::tabs(4) << "0.000000 1.000000 0.000000 0.000000" << std::endl;
00325     str << Base::tabs(4) << "0.000000 0.000000 1.000000 0.000000" << std::endl;
00326     str << Base::tabs(4) << "0.000000 0.000000 0.000000 1.000000" << std::endl;
00327     str << Base::tabs(3) << "}" << std::endl;
00328     str << Base::tabs(2) << "}" << std::endl;
00329     str << Base::tabs(1) << "}" << std::endl;
00330     str << Base::tabs(1) << "RESOURCE_NAME \"FreeCAD\"" << std::endl;
00331     str << Base::tabs(0) << "}" << std::endl;
00332 }
00333 
00334 void SoFCVectorizeU3DAction::printFooter(void) const
00335 {
00336 }
00337 
00338 void SoFCVectorizeU3DAction::printViewport(void) const
00339 {
00340 }
00341 
00342 void SoFCVectorizeU3DAction::printBackground(void) const
00343 {
00344     //SbVec2f mul = getRotatedViewportSize();
00345     //SbVec2f add = getRotatedViewportStartpos();
00346 
00347     //float x[2],y[2];
00348     //x[0] = add[0];
00349     //x[1] = mul[0] - add[0];
00350     //y[0] = add[1];
00351     //y[1] = mul[1] - add[1];
00352 
00353     //SbColor bg;
00354     //(void)this->getBackgroundColor(bg);
00355     //uint32_t cc = bg.getPackedValue();
00356 
00357     //std::ostream& str = this->getU3DOutput()->getFileStream();
00358     // todo
00359 }
00360 
00361 void SoFCVectorizeU3DAction::printItem(const SoVectorizeItem * item) const
00362 {
00363     switch (item->type) {
00364     case SoVectorizeItem::TRIANGLE:
00365         this->p->printTriangle(static_cast<const SoVectorizeTriangle*>(item));
00366         break;
00367     case SoVectorizeItem::LINE:
00368         this->p->printLine(static_cast<const SoVectorizeLine*>(item));
00369         break;
00370     case SoVectorizeItem::POINT:
00371         this->p->printPoint(static_cast<const SoVectorizePoint*>(item));
00372         break;
00373     case SoVectorizeItem::TEXT:
00374         this->p->printText(static_cast<const SoVectorizeText*>(item));
00375         break;
00376     case SoVectorizeItem::IMAGE:
00377         this->p->printImage(static_cast<const SoVectorizeImage*>(item));
00378         break;
00379     default:
00380         assert(0 && "unsupported item");
00381         break;
00382     }
00383 }

Generated on Wed Nov 23 19:00:40 2011 for FreeCAD by  doxygen 1.6.1