Builder3D.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) Juergen Riegel         <juergen.riegel@web.de>          *
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 
00026 #ifndef _PreComp_
00027 # include <assert.h>
00028 # include <string>
00029 #endif
00030 
00031 #include <stdlib.h>
00032 
00034 #include "Builder3D.h"
00035 #include "Exception.h"
00036 #include "Vector3D.h"
00037 #include "Matrix.h"
00038 #include "Console.h"
00039 #include "Tools.h"
00040 
00041 using namespace Base;
00042 
00043 
00044 //**************************************************************************
00045 // Construction/Destruction
00046 
00051 Builder3D::Builder3D()
00052 :bStartEndOpen(false)
00053 {
00054   result << "#Inventor V2.1 ascii " << std::endl << std::endl;
00055   result << "Separator { ";
00056 }
00057 
00062 Builder3D::~Builder3D()
00063 {
00064 }
00065 
00066 
00067 //**************************************************************************
00068 // points handling
00069 
00080 void Builder3D::startPoints(short pointSize, float color_r,float color_g,float color_b)
00081 {
00082   bStartEndOpen = true;
00083   result << "Separator { ";
00084   result <<   "Material { ";
00085   result <<     "diffuseColor " << color_r << " "<< color_g << " "<< color_b  ;
00086   result <<   "} ";
00087   result <<   "MaterialBinding { value PER_PART } ";
00088   result <<   "DrawStyle { pointSize " << pointSize << "} ";
00089   result <<   "Coordinate3 { ";
00090   result <<     "point [ ";
00091 }
00092 
00094 void Builder3D::addPoint(float x, float y, float z)
00095 {
00096   result << x << " " << y << " " << z << ",";
00097 }
00098 
00099 
00101 void Builder3D::addPoint(const Vector3f &vec)
00102 {
00103   addPoint(vec.x,vec.y,vec.z);
00104 }
00109 void Builder3D::endPoints(void)
00110 {
00111   result  <<      "] ";
00112   result  <<     "} ";
00113   result  <<   "PointSet { } ";
00114   result  << "} ";
00115   bStartEndOpen = false;
00116 }
00117 
00118 void Builder3D::addSinglePoint(float x, float y, float z,short pointSize, float color_r,float color_g,float color_b)
00119 {
00120   // addSinglePoint() not between startXXX() and endXXX() allowed
00121   assert( bStartEndOpen == false );
00122 
00123   result << "Separator { ";
00124   result <<   "Material { ";
00125   result <<     "diffuseColor " << color_r << " "<< color_g << " "<< color_b  ;
00126   result <<   "} ";
00127   result <<   "MaterialBinding { value PER_PART } ";
00128   result <<   "DrawStyle { pointSize " << pointSize << "} ";
00129   result <<   "Coordinate3 { ";
00130   result <<     "point [ ";
00131   result << x << " " << y << " " << z << ",";
00132   result <<      "] ";
00133   result <<     "} ";
00134   result <<   "PointSet { } ";
00135   result << "} ";
00136 
00137 }
00138 
00139 void Builder3D::addSinglePoint(const Base::Vector3f &vec, short pointSize, float color_r,float color_g,float color_b)
00140 {
00141   addSinglePoint(vec.x, vec.y , vec.z, pointSize, color_r, color_g, color_b);
00142 }
00143 
00144 //**************************************************************************
00145 // text handling
00146 
00147 
00157 void Builder3D::addText(float pos_x, float pos_y , float pos_z,const char * text, float color_r,float color_g,float color_b)
00158 {
00159   // addSinglePoint() not between startXXX() and endXXX() allowed
00160   assert( bStartEndOpen == false );
00161 
00162   result << "Separator { "  
00163          <<   "Material { diffuseColor " << color_r << " "<< color_g << " "<< color_b << "} " 
00164          <<   "Transform { translation " << pos_x << " "<< pos_y << " "<< pos_z << "} "
00165          <<   "Text2 { string \" " << text << "\" " << "} "
00166          << "} ";
00167 
00168 }
00169 
00170 void Builder3D::addText(const Base::Vector3f &vec,const char * text, float color_r,float color_g,float color_b)
00171 {
00172   addText(vec.x, vec.y , vec.z,text, color_r,color_g,color_b);
00173 }
00174 
00175 void Builder3D::clear ()
00176 {
00177   // Under VC6 string::clear() doesn't exist, under gcc stringstream::str() returns a copy not a reference
00178 #if defined(_MSC_VER) && _MSC_VER >= 1400
00179   result.str().clear();
00180 #endif
00181   result.clear();
00182 }
00183 
00184 //**************************************************************************
00185 // line/arrow handling
00186 
00187 void Builder3D::addSingleLine(Vector3f pt1, Vector3f pt2, short lineSize, float color_r,float color_g,float color_b, unsigned short linePattern)
00188 {
00189   char lp[20];
00190   sprintf(lp, "0x%x", linePattern);
00191   //char lp[20] = "0x";
00192   //itoa(linePattern, buf, 16);
00193   //strcat(lp, buf);
00194 
00195   result << "Separator { "
00196          <<   "Material { diffuseColor " << color_r << " "<< color_g << " "<< color_b << "} " 
00197          <<   "DrawStyle { lineWidth " << lineSize << " linePattern " << lp << " } "
00198          <<   "Coordinate3 { "
00199          <<     "point [ "
00200          <<        pt1.x << " " << pt1.y << " " << pt1.z << ","
00201          <<        pt2.x << " " << pt2.y << " " << pt2.z 
00202          <<     "] "
00203          <<   "} "
00204          <<   "LineSet { } "
00205          << "} ";
00206 }
00207 
00208 void Builder3D::addSingleArrow(Vector3f pt1, Vector3f pt2, short lineSize, float color_r,float color_g,float color_b, unsigned short /*linePattern*/)
00209 {
00210     float l = (pt2 - pt1).Length();
00211     float cl = l / 10.0f;
00212     float cr = cl / 2.0f;
00213 
00214     Vector3f dir = pt2 - pt1;
00215     dir.Normalize();
00216     dir.Scale(l-cl, l-cl, l-cl);
00217     Vector3f pt2s = pt1 + dir;
00218     dir.Normalize();
00219     dir.Scale(l-cl/2.0f, l-cl/2.0f, l-cl/2.0f);
00220     Vector3f cpt = pt1 + dir;
00221 
00222     Vector3f rot = Vector3f(0.0f, 1.0f, 0.0f) % dir;
00223     rot.Normalize();
00224     float a = Vector3f(0.0f, 1.0f, 0.0f).GetAngle(dir);
00225 
00226     result << "Separator { "
00227          <<   "Material { diffuseColor " << color_r << " "<< color_g << " "<< color_b << "} " 
00228          <<   "DrawStyle { lineWidth " << lineSize << "} "
00229          <<   "Coordinate3 { "
00230          <<     "point [ "
00231          <<        pt1.x << " " << pt1.y << " " << pt1.z << ","
00232          <<        pt2s.x << " " << pt2s.y << " " << pt2s.z 
00233          <<     "] "
00234          <<   "} "
00235          <<   "LineSet { } "
00236          <<   "Transform { "
00237          <<     "translation " << cpt.x << " " << cpt.y << " " << cpt.z << " "
00238          <<     "rotation " << rot.x << " " << rot.y << " " << rot.z << " " << a
00239          <<   "} "
00240          <<   "Cone { bottomRadius " << cr << " height " << cl << "} "
00241          << "} ";
00242 
00243 }
00244 
00245 //**************************************************************************
00246 // triangle handling
00247 
00248 void Builder3D::addSingleTriangle(Vector3f pt0, Vector3f pt1, Vector3f pt2, bool filled, short lineSize, float color_r, float color_g, float color_b)
00249 {
00250   std::string fs = "";
00251   if (filled)
00252   {
00253     fs = "IndexedFaceSet { coordIndex[ 0, 1, 2, -1 ] } ";
00254   }
00255 
00256     result << "Separator { "
00257          <<   "Material { diffuseColor " << color_r << " "<< color_g << " "<< color_b << "} " 
00258          <<   "DrawStyle { lineWidth " << lineSize << "} "
00259          <<   "Coordinate3 { "
00260          <<     "point [ "
00261          <<        pt0.x << " " << pt0.y << " " << pt0.z << ","
00262          <<        pt1.x << " " << pt1.y << " " << pt1.z << ","
00263          <<        pt2.x << " " << pt2.y << " " << pt2.z << ","
00264          <<     "] "
00265          <<   "} "
00266          <<   "LineSet { } "
00267          <<   fs
00268          << "} ";
00269 
00270 }
00271 
00272 void Builder3D::addTransformation(const Base::Matrix4D& transform)
00273 {
00274   Base::Vector3f cAxis, cBase;
00275   float fAngle, fTranslation;
00276   transform.toAxisAngle(cBase, cAxis,fAngle,fTranslation);
00277   cBase.x = (float)transform[0][3];
00278   cBase.y = (float)transform[1][3];
00279   cBase.z = (float)transform[2][3];
00280   addTransformation(cBase,cAxis,fAngle);
00281 }
00282 
00283 void Builder3D::addTransformation(const Base::Vector3f& translation, const Base::Vector3f& rotationaxis, float fAngle)
00284 {
00285   result << "Transform {";
00286   result << "  translation " << translation.x << " " << translation.y << " " << translation.z;
00287   result << "  rotation " << rotationaxis.x << " " << rotationaxis.y << " " << rotationaxis.z << " " << fAngle;
00288   result << "}";
00289 }
00290 
00291 //**************************************************************************
00292 // output handling
00293 
00301 void Builder3D::saveToLog(void)
00302 {
00303   result <<   "} ";
00304   // Note: The string can become very long, so that ConsoleSingelton::Log() will internally 
00305   // truncate the string which causes Inventor to fail to interpret the truncated string.
00306   // So, we send the string directly to the observer that handles the Inventor stuff.
00307   //Console().Log("Vdbg: %s \n",result.str().c_str());
00308   ConsoleObserver* obs = Base::Console().Get("StatusBar");
00309   if (obs) obs->Log(result.str().c_str());
00310 }
00311 
00320 void Builder3D::saveToFile(const char* FileName)
00321 {
00322   result <<   "} ";
00323   std::ofstream  file(FileName);
00324   if(!file) 
00325     throw Exception("Builder3D::saveToFile(): Can not open file...");
00326 
00327   file << "#Inventor V2.1 ascii " << std::endl;
00328   file << result.str();
00329 }
00330 
00331 // -----------------------------------------------------------------------------
00332 
00333 InventorBuilder::InventorBuilder(std::ostream& output)
00334   : result(output),bStartEndOpen(false),bClosed(false), indent(0)
00335 {
00336     result << "#Inventor V2.1 ascii " << std::endl << std::endl;
00337     beginSeparator();
00338 }
00339 
00340 InventorBuilder:: ~InventorBuilder()
00341 {
00342     close();
00343 }
00344 
00345 void InventorBuilder::close()
00346 {
00347     if (!bClosed) {
00348         bClosed = true;
00349         endSeparator();
00350     }
00351 }
00352 
00353 void InventorBuilder::beginSeparator()
00354 {
00355     result << Base::blanks(indent) << "Separator { " << std::endl;
00356     indent+=2;
00357 }
00358 
00359 void InventorBuilder::endSeparator()
00360 {
00361     indent-=2;
00362     result << Base::blanks(indent) << "}" << std::endl;
00363 }
00364 
00365 void InventorBuilder::addMaterial(float color_r,float color_g,float color_b)
00366 {
00367     result << Base::blanks(indent) << "Material { " << std::endl;
00368     result << Base::blanks(indent) << "  diffuseColor "
00369            << color_r << " "<< color_g << " "<< color_b << std::endl;
00370     result << Base::blanks(indent) << "} " << std::endl;
00371 }
00372 
00373 void InventorBuilder::addMaterialBinding(const char* bind)
00374 {
00375     result << Base::blanks(indent) << "MaterialBinding { value "
00376            << bind << " } " << std::endl;
00377 }
00378 
00379 void InventorBuilder::addDrawStyle(short pointSize, short lineWidth, unsigned short linePattern, const char* style)
00380 {
00381     result << Base::blanks(indent) << "DrawStyle {" << std::endl
00382            << Base::blanks(indent) << "  style " << style << std::endl
00383            << Base::blanks(indent) << "  pointSize " << pointSize << std::endl
00384            << Base::blanks(indent) << "  lineWidth " << lineWidth << std::endl
00385            << Base::blanks(indent) << "  linePattern " << linePattern << std::endl
00386            << Base::blanks(indent) << "}" << std::endl;
00387 }
00388 
00389 //**************************************************************************
00390 // points handling
00391 
00399 void InventorBuilder::beginPoints()
00400 {
00401     result << Base::blanks(indent) << "Coordinate3 { " << std::endl;
00402     indent += 2;
00403     result << Base::blanks(indent) << "point [ ";
00404     indent += 2;
00405 }
00406 
00408 void InventorBuilder::addPoint(float x, float y, float z)
00409 {
00410     result << Base::blanks(indent) << x << " " << y << " " << z << "," << std::endl;
00411 }
00412 
00414 void InventorBuilder::addPoint(const Vector3f &vec)
00415 {
00416     addPoint(vec.x,vec.y,vec.z);
00417 }
00418 
00423 void InventorBuilder::endPoints(void)
00424 {
00425     indent -= 2;
00426     result << Base::blanks(indent) << "]" << std::endl;
00427     indent -= 2;
00428     result << Base::blanks(indent) << "}" << std::endl;
00429     result << Base::blanks(indent) << "PointSet { } " << std::endl;
00430 }
00431 
00432 //**************************************************************************
00433 // text handling
00434 
00435 
00445 void InventorBuilder::addText(float pos_x, float pos_y , float pos_z,const char * text, float color_r,float color_g,float color_b)
00446 {
00447   // addSinglePoint() not between startXXX() and endXXX() allowed
00448   assert( bStartEndOpen == false );
00449 
00450   result << Base::blanks(indent) << "Separator { "   << std::endl
00451          << Base::blanks(indent) << "  Material { diffuseColor "
00452          << color_r << " "<< color_g << " "<< color_b << "} "  << std::endl
00453          << Base::blanks(indent) << "  Transform { translation "
00454          << pos_x << " "<< pos_y << " "<< pos_z << "} "  << std::endl
00455          << Base::blanks(indent) << "  Text2 { string \" " << text << "\" " << "} " << std::endl
00456          << Base::blanks(indent) << "}" << std::endl;
00457 
00458 }
00459 
00460 void InventorBuilder::addText(const Vector3f &vec,const char * text, float color_r,float color_g,float color_b)
00461 {
00462     addText(vec.x, vec.y , vec.z,text, color_r,color_g,color_b);
00463 }
00464 
00465 //**************************************************************************
00466 // line/arrow handling
00467 
00468 void InventorBuilder::addSingleLine(const Vector3f& pt1, const Vector3f& pt2, short lineSize,
00469                                     float color_r,float color_g,float color_b, unsigned short linePattern)
00470 {
00471   char lp[20];
00472   sprintf(lp, "0x%x", linePattern);
00473   //char lp[20] = "0x";
00474   //itoa(linePattern, buf, 16);
00475   //strcat(lp, buf);
00476 
00477   result << "  Separator { " << std::endl
00478          << "    Material { diffuseColor " << color_r << " "<< color_g << " "<< color_b << "} "  << std::endl
00479          << "    DrawStyle { lineWidth " << lineSize << " linePattern " << lp << " } " << std::endl
00480          << "    Coordinate3 { " << std::endl
00481          << "      point [ "
00482          <<        pt1.x << " " << pt1.y << " " << pt1.z << ","
00483          <<        pt2.x << " " << pt2.y << " " << pt2.z
00484          << " ] " << std::endl
00485          << "    } " << std::endl
00486          << "    LineSet { } " << std::endl
00487          << "  } " << std::endl;
00488 }
00489 
00490 void InventorBuilder::addSingleArrow(const Vector3f& pt1, const Vector3f& pt2, short lineSize,
00491                                      float color_r,float color_g,float color_b, unsigned short /*linePattern*/)
00492 {
00493     float l = (pt2 - pt1).Length();
00494     float cl = l / 10.0f;
00495     float cr = cl / 2.0f;
00496 
00497     Vector3f dir = pt2 - pt1;
00498     dir.Normalize();
00499     dir.Scale(l-cl, l-cl, l-cl);
00500     Vector3f pt2s = pt1 + dir;
00501     dir.Normalize();
00502     dir.Scale(l-cl/2.0f, l-cl/2.0f, l-cl/2.0f);
00503     Vector3f cpt = pt1 + dir;
00504 
00505     Vector3f rot = Vector3f(0.0f, 1.0f, 0.0f) % dir;
00506     rot.Normalize();
00507     float a = Vector3f(0.0f, 1.0f, 0.0f).GetAngle(dir);
00508 
00509     result << Base::blanks(indent) << "Separator { " << std::endl
00510            << Base::blanks(indent) << "  Material { diffuseColor "
00511            << color_r << " "<< color_g << " "<< color_b << "} "  << std::endl
00512            << Base::blanks(indent) << "  DrawStyle { lineWidth "
00513            << lineSize << "} " << std::endl
00514            << Base::blanks(indent) << "  Coordinate3 { " << std::endl
00515            << Base::blanks(indent) << "    point [ "
00516            <<        pt1.x << " " << pt1.y << " " << pt1.z << ","
00517            <<        pt2s.x << " " << pt2s.y << " " << pt2s.z
00518            << " ] " << std::endl
00519            << Base::blanks(indent) << "  } " << std::endl
00520            << Base::blanks(indent) << "  LineSet { } " << std::endl
00521            << Base::blanks(indent) << "  Transform { " << std::endl
00522            << Base::blanks(indent) << "    translation "
00523            << cpt.x << " " << cpt.y << " " << cpt.z << " " << std::endl
00524            << Base::blanks(indent) << "    rotation "
00525            << rot.x << " " << rot.y << " " << rot.z << " " << a << std::endl
00526            << Base::blanks(indent) << "  } " << std::endl
00527            << Base::blanks(indent) << "  Cone { bottomRadius " << cr << " height " << cl << "} " << std::endl
00528            << Base::blanks(indent) << "} " << std::endl;
00529 }
00530 
00534 void InventorBuilder::addLineSet(const std::vector<Vector3f>& points, short lineSize,
00535                                  float color_r,float color_g,float color_b, unsigned short linePattern)
00536 {
00537   char lp[20];
00538   sprintf(lp, "0x%x", linePattern);
00539 
00540   result << "  Separator { " << std::endl
00541          << "    Material { diffuseColor " << color_r << " "<< color_g << " "<< color_b << "} "  << std::endl
00542          << "    DrawStyle { lineWidth " << lineSize << " linePattern " << lp << " } " << std::endl
00543          << "    Coordinate3 { " << std::endl
00544          << "      point [ ";
00545   std::vector<Vector3f>::const_iterator it = points.begin();
00546   if ( it != points.end() )
00547   {
00548     result << it->x << " " << it->y << " " << it->z;
00549     for ( ++it ; it != points.end(); ++it )
00550       result << "," << std::endl << "          " << it->x << " " << it->y << " " << it->z;
00551   }
00552 
00553   result << " ] " << std::endl
00554          << "    } " << std::endl
00555          << "    LineSet { " << std::endl
00556          << "      numVertices [ ";
00557   /*size_t ct = points.size() / 2;
00558   if ( ct > 0 )
00559   {
00560     result << "2";
00561     for (size_t i=1; i<ct; i++)
00562     {
00563       result << ","; 
00564       if (i%16==0)
00565         result << std::endl << "          "; 
00566       result << "2";
00567     }
00568   }*/
00569   result << " -1 ";
00570   result << " ] " << std::endl
00571          << "    } " << std::endl
00572          << "  } " << std::endl;
00573 }
00574 
00575 //**************************************************************************
00576 // triangle handling
00577 
00578 void InventorBuilder::addIndexedFaceSet(const std::vector<Vector3f>& points, const std::vector<int>& indices, float crease)
00579 {
00580     if (points.empty() || indices.size() < 4)
00581         return;
00582     result << "  Separator { " << std::endl
00583            << "    ShapeHints {" << std::endl
00584            << "       creaseAngle " << crease << std::endl
00585            << "    }" << std::endl
00586            << "    Coordinate3 { " << std::endl
00587            << "      point [ ";
00588     std::vector<Vector3f>::const_iterator it_last_p = points.end()-1;
00589     for (std::vector<Vector3f>::const_iterator it = points.begin(); it != points.end(); ++it) {
00590         if (it != it_last_p)
00591             result << it->x << " " << it->y << " " << it->z << "," << std::endl;
00592         else
00593             result << it->x << " " << it->y << " " << it->z << " ] " << std::endl;
00594     }
00595 
00596     result << "    } " << std::endl
00597            << "    IndexedFaceSet { " << std::endl
00598            << "      coordIndex [ ";
00599     std::vector<int>::const_iterator it_last_f = indices.end()-1;
00600     int index=0;
00601     for (std::vector<int>::const_iterator it = indices.begin(); it != indices.end(); ++it) {
00602             if (it != it_last_f)
00603                 result << *it << ", ";
00604             else
00605                 result << *it << " ] ";
00606             if (++index%8==0)
00607                 result << std::endl;
00608     }
00609     result << "    } " << std::endl
00610            << "  } " << std::endl;
00611 }
00612 
00613 void InventorBuilder::addSingleTriangle(const Vector3f& pt0, const Vector3f& pt1, const Vector3f& pt2,
00614                                         bool filled, short lineSize, float color_r, float color_g, float color_b)
00615 {
00616   std::string fs = "";
00617   if (filled)
00618   {
00619     fs = "    FaceSet { } ";
00620   }
00621 
00622     result << "  Separator { " << std::endl
00623            << "    Material { diffuseColor " << color_r << " "<< color_g << " "<< color_b << "} "  << std::endl
00624            << "    DrawStyle { lineWidth " << lineSize << "} " << std::endl
00625            << "    Coordinate3 { " << std::endl
00626            << "      point [ "
00627            <<        pt0.x << " " << pt0.y << " " << pt0.z << ","
00628            <<        pt1.x << " " << pt1.y << " " << pt1.z << ","
00629            <<        pt2.x << " " << pt2.y << " " << pt2.z
00630            << "] " << std::endl
00631            << "    } " << std::endl
00632            << "    IndexedLineSet { coordIndex[ 0, 1, 2, 0, -1 ] } " << std::endl
00633            << fs << std::endl
00634            << "  } " << std::endl;
00635 }
00636 
00637 void InventorBuilder::addSinglePlane(const Vector3f& base, const Vector3f& eX, const Vector3f& eY,
00638                                      float length, float width, bool filled, short lineSize, 
00639                                      float color_r,float color_g,float color_b)
00640 {
00641     Vector3f pt0 = base;
00642     Vector3f pt1 = base + length * eX;
00643     Vector3f pt2 = base + length * eX + width * eY;
00644     Vector3f pt3 = base + width * eY;
00645     std::string fs = "";
00646     if (filled)
00647     {
00648         fs = "    FaceSet { } ";
00649     }
00650 
00651     result << "  Separator { " << std::endl
00652            << "    Material { diffuseColor " << color_r << " "<< color_g << " "<< color_b << "} "  << std::endl
00653            << "    DrawStyle { lineWidth " << lineSize << "} " << std::endl
00654            << "    Coordinate3 { " << std::endl
00655            << "      point [ "
00656            <<        pt0.x << " " << pt0.y << " " << pt0.z << ","
00657            <<        pt1.x << " " << pt1.y << " " << pt1.z << ","
00658            <<        pt2.x << " " << pt2.y << " " << pt2.z << ","
00659            <<        pt3.x << " " << pt3.y << " " << pt3.z
00660            << "] " << std::endl
00661            << "    } " << std::endl
00662            << "    IndexedLineSet { coordIndex[ 0, 1, 2, 3, 0, -1 ] } " << std::endl
00663            << fs << std::endl
00664            << "  } " << std::endl;
00665 }
00666 
00673 void InventorBuilder::addNurbsSurface(const std::vector<Base::Vector3f>& controlPoints,
00674                                       int numUControlPoints, int numVControlPoints,
00675                                       const std::vector<float>& uKnots,
00676                                       const std::vector<float>& vKnots)
00677 {
00678     result << "  Separator { " << std::endl
00679            << "    Coordinate3 { " << std::endl
00680            << "      point [ ";
00681     for (std::vector<Base::Vector3f>::const_iterator it = 
00682         controlPoints.begin(); it != controlPoints.end(); ++it) {
00683         if (it != controlPoints.begin())
00684             result << "," << std::endl << "          ";
00685         result << it->x << " " << it->y << " " << it->z;
00686     }
00687 
00688     result << " ]" << std::endl
00689            << "    }" << std::endl;
00690     result << "    NurbsSurface { " << std::endl
00691            << "      numUControlPoints " << numUControlPoints << std::endl
00692            << "      numVControlPoints " << numVControlPoints << std::endl
00693            << "      uKnotVector [ ";
00694     int index = 0;
00695     for (std::vector<float>::const_iterator it = uKnots.begin(); it != uKnots.end(); ++it) {
00696         result << *it;
00697         index++;
00698         if ((it+1) < uKnots.end()) {
00699             if (index % 4 == 0)
00700                 result << "," << std::endl << "          ";
00701             else
00702                 result << ", ";
00703         }
00704     }
00705     result << " ]" << std::endl
00706            << "      vKnotVector [ ";
00707     for (std::vector<float>::const_iterator it = vKnots.begin(); it != vKnots.end(); ++it) {
00708         result << *it;
00709         index++;
00710         if ((it+1) < vKnots.end()) {
00711             if (index % 4 == 0)
00712                 result << "," << std::endl << "          ";
00713             else
00714                 result << ", ";
00715         }
00716     }
00717     result << " ]" << std::endl
00718            << "    }" << std::endl
00719            << "  }" << std::endl;
00720 }
00721 
00722 void InventorBuilder::addCylinder(float radius, float height)
00723 {
00724     result << Base::blanks(indent) << "Cylinder {\n"
00725            << Base::blanks(indent) << "  radius " << radius << "\n"
00726            << Base::blanks(indent) << "  height " << height << "\n"
00727            << Base::blanks(indent) << "  parts (SIDES | TOP | BOTTOM)\n"
00728            << Base::blanks(indent) << "}\n";
00729 }
00730 
00731 void InventorBuilder::addBoundingBox(const Vector3f& pt1, const Vector3f& pt2, short lineWidth,
00732                                      float color_r,float color_g,float color_b)
00733 {
00734     Base::Vector3f pt[8];
00735     pt[0].Set(pt1.x, pt1.y, pt1.z);
00736     pt[1].Set(pt1.x, pt1.y, pt2.z);
00737     pt[2].Set(pt1.x, pt2.y, pt1.z);
00738     pt[3].Set(pt1.x, pt2.y, pt2.z);
00739     pt[4].Set(pt2.x, pt1.y, pt1.z);
00740     pt[5].Set(pt2.x, pt1.y, pt2.z);
00741     pt[6].Set(pt2.x, pt2.y, pt1.z);
00742     pt[7].Set(pt2.x, pt2.y, pt2.z);
00743 
00744     result << "  Separator { " << std::endl
00745            << "    Material { diffuseColor " << color_r << " "<< color_g << " "<< color_b << "} "  << std::endl
00746            << "    DrawStyle { lineWidth " << lineWidth << "} " << std::endl
00747            << "    Coordinate3 { " << std::endl
00748            << "      point [ "
00749            << "        " << pt[0].x << " " << pt[0].y << " " << pt[0].z << ",\n"
00750            << "        " << pt[1].x << " " << pt[1].y << " " << pt[1].z << ",\n"
00751            << "        " << pt[2].x << " " << pt[2].y << " " << pt[2].z << ",\n"
00752            << "        " << pt[3].x << " " << pt[3].y << " " << pt[3].z << ",\n"
00753            << "        " << pt[4].x << " " << pt[4].y << " " << pt[4].z << ",\n"
00754            << "        " << pt[5].x << " " << pt[5].y << " " << pt[5].z << ",\n"
00755            << "        " << pt[6].x << " " << pt[6].y << " " << pt[6].z << ",\n"
00756            << "        " << pt[7].x << " " << pt[7].y << " " << pt[7].z
00757            << "] " << std::endl
00758            << "    } " << std::endl
00759            << "    IndexedLineSet { coordIndex[ 0, 2, 6, 4, 0, -1\n"
00760               "        1, 5, 7, 3, 1, -1,\n"
00761               "        5, 4, 6, 7, 5, -1,\n"
00762               "        7, 6, 2, 3, 7, -1,\n"
00763               "        3, 2, 0, 1, 3, -1,\n"
00764               "        5, 1, 0, 4, 5, -1 ] } " << std::endl
00765            << "  } " << std::endl;
00766 }
00767 
00768 void InventorBuilder::addTransformation(const Matrix4D& transform)
00769 {
00770     Vector3f cAxis, cBase;
00771     float fAngle, fTranslation;
00772     transform.toAxisAngle(cBase, cAxis,fAngle,fTranslation);
00773     cBase.x = (float)transform[0][3];
00774     cBase.y = (float)transform[1][3];
00775     cBase.z = (float)transform[2][3];
00776     addTransformation(cBase,cAxis,fAngle);
00777 }
00778 
00779 void InventorBuilder::addTransformation(const Vector3f& translation, const Vector3f& rotationaxis, float fAngle)
00780 {
00781     result << Base::blanks(indent) << "Transform {" << std::endl;
00782     result << Base::blanks(indent) << "  translation " 
00783          << translation.x << " " << translation.y << " " << translation.z << std::endl;
00784     result << Base::blanks(indent) << "  rotation " 
00785          << rotationaxis.x << " " << rotationaxis.y << " " << rotationaxis.z 
00786          << " " << fAngle << std::endl;
00787     result << Base::blanks(indent) <<  "}" << std::endl;
00788 }

Generated on Wed Nov 23 18:59:59 2011 for FreeCAD by  doxygen 1.6.1