ConvertDyna.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2007                                                    *
00003  *   Joachim Zettler <Joachim.Zettler@gmx.de>                              *
00004  *                                                                         *
00005  *   This file is part of the FreeCAD CAx development system.              *
00006  *                                                                         *
00007  *   This library is free software; you can redistribute it and/or         *
00008  *   modify it under the terms of the GNU Library General Public           *
00009  *   License as published by the Free Software Foundation; either          *
00010  *   version 2 of the License, or (at your option) any later version.      *
00011  *                                                                         *
00012  *   This library  is distributed in the hope that it will be useful,      *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU Library General Public License for more details.                  *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU Library General Public     *
00018  *   License along with this library; see the file COPYING.LIB. If not,    *
00019  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00020  *   Suite 330, Boston, MA  02111-1307, USA                                *
00021  *                                                                         *
00022  ***************************************************************************/
00023 
00024 
00025 /******CONVERTDYNA.CPP******/
00026 
00027 /******MAIN INCLUDES******/
00028 #include "PreCompiled.h"
00029 #include "ConvertDyna.h"
00030 #include <cstdlib>
00031 
00032 ReadDyna::ReadDyna(MeshCore::MeshKernel &m, const char* &inputname)
00033 {
00034     //Open the file and perform standard check for file
00035     std::ifstream inputfile;
00036     inputfile.open(inputname);
00037     std::string line;
00038     if (!inputfile.is_open())  //Exists...?
00039     {
00040         std::cerr << "File not found. Exiting..." << std::endl;
00041         return;
00042     }
00043     getline(inputfile,line);
00044     if (line.find("*KEYWORD") == std::string::npos)  //Really a DYNA file...?
00045     {
00046         std::cerr << "NOT A DYNA FILE\a" << std::endl;
00047         return;
00048     }
00049     while (inputfile) //First find *NODE Keyword to initialize the VERTICES list (it's the main list)
00050     {
00051         getline(inputfile,line);
00052         if (line.find("*NODE") != std::string::npos)
00053         {
00054             std::cout << "*NODE found. Parsing... ";
00055             ReadNode(inputfile);
00056             std::cout << "done\n";
00057             break;
00058         }
00059     }
00060     inputfile.seekg(std::ios::beg);   //bring back the file pointer, you never know where those two other will spawn...
00061     if (inputfile.fail())
00062         inputfile.clear();   //...clear the badbits...
00063     while (inputfile) //and reread.
00064     {
00065         getline(inputfile,line);
00066         if (line.find("*ELEMENT_SHELL_THICKNESS") != std::string::npos)  //For Faces
00067         {
00068             std::cout << "*ELEMENT_SHELL_THICKNESS found. Parsing... ";
00069             ReadShellThickness(inputfile);
00070             std::cout << "done\n";
00071         }
00072         else if (line.find("*CONSTRAINED_ADAPTIVITY") != std::string::npos) //For constraints
00073         {
00074             std::cout << "*CONSTRAINED_ADAPTIVITY found. Parsing... ";
00075             ReadConstraints(inputfile);
00076             std::cout << "done\n";
00077         }
00078     }
00079     inputfile.close();
00080     std::cout << "Triangulating... ";
00081     Convert();
00082     std::cout << "done\nWriting into list... ";
00083     PutInMesh(m);
00084     std::cout << "done\n";
00085 }
00088 void ReadDyna::ReadNode(std::ifstream &inputfile)
00089 {
00090     std::string line;
00091     getline(inputfile, line);
00092     char *cstr;
00093     char Byte[16] = {0};
00094     char Nibble[8] = {0};
00095     int i = 0, j = 0;
00096     while (line.find("*") == std::string::npos) //while next keyword is not yet found...
00097     {
00098         if (line.find("$") != std::string::npos) //comment, ignore
00099         {
00100             getline(inputfile, line);
00101             continue;
00102         }
00103         VERTICES Temp;
00104         Temp.Constrain = false;   //Initialize both flags to false
00105         Temp.Constrained = false;
00106         cstr = new char [line.size()+1];
00107         strcpy(cstr, line.c_str());
00108         for (i = 0; i < 8; i++)
00109         {
00110             Nibble[j] = cstr[i];
00111             j++;
00112         }
00113         Temp.PointIndex = atoi(Nibble);  //Index
00114         j = 0;
00115         for (i = 8; i < 24; i++)
00116         {
00117             Byte[j] = cstr[i];
00118             j++;
00119         }
00120         Temp.Coords.push_back(atof(Byte));  //X Coords
00121         j = 0;
00122 
00123         for (i = 24; i < 40; i++)
00124         {
00125             Byte[j] = cstr[i];
00126             j++;
00127         }
00128         Temp.Coords.push_back(atof(Byte));  //Y Coords
00129         j = 0;
00130 
00131         for (i = 40; i < 56; i++)
00132         {
00133             Byte[j] = cstr[i];
00134             j++;
00135         }
00136         Temp.Coords.push_back(atof(Byte));   //Z Coords
00137         j = 0;
00138 
00139         Pointlist.insert(std::pair<unsigned int, VERTICES>(Temp.PointIndex, Temp));
00140         delete[] cstr;
00141         getline(inputfile, line);
00142     }
00143 }
00150 void ReadDyna::ReadShellThickness(std::ifstream &inputfile)
00151 {
00152     std::map<unsigned int, VERTICES>::iterator pnt_it(Pointlist.begin());
00153     std::string line;
00154     char *c_str;
00155     char Info[8] = {0};
00156     int i = 0, j = 0;
00157     getline(inputfile, line);
00158     while (line.find("*") == std::string::npos)  //While next keyword is not yet found..
00159     {
00160         if (line.find("$") != std::string::npos) //Ignore the comments
00161         {
00162             getline(inputfile, line);
00163             continue;
00164         }
00165         FACE Temp;
00166         c_str = new char [line.size()+1];
00167         strcpy(c_str, line.c_str());
00168 
00169         for (i = 0; i < 8; i++,j++)
00170             Info[j] = c_str[i];
00171         Temp.FaceIndex = atoi(Info);    //Facet Index
00172         j= 0;
00173 
00174         for (i = 16; i < 24; i++,j++)
00175             Info[j] = c_str[i];
00176         Temp.PointIndex.push_back(atoi(Info));   //First Point
00177         j = 0;
00178 
00179         for (i = 24; i < 32; i++,j++)
00180             Info[j] = c_str[i];
00181         Temp.PointIndex.push_back(atoi(Info));   //Second Point
00182         j = 0;
00183 
00184         for (i = 32; i < 40; i++,j++)
00185             Info[j] = c_str[i];
00186         Temp.PointIndex.push_back(atoi(Info));   //Third Point
00187         j = 0;
00188 
00189         for (i = 40; i < 48; i++,j++)
00190             Info[j] = c_str[i];
00191         Temp.PointIndex.push_back(atoi(Info));   //Fourth Point
00192         j = 0;
00193 
00194         getline(inputfile,line);
00195         /*If you need extra info from here, extract it here*/
00196 
00197         for (unsigned int k = 0; k < Temp.PointIndex.size(); k++)
00198         {
00199             pnt_it = Pointlist.find(Temp.PointIndex[k]);
00200             (*pnt_it).second.FacetRef.push_back(Temp.FaceIndex);    //Put into the Facet Reference
00201 
00202         }
00203         Facelist.insert(std::pair<unsigned int,FACE>(Temp.FaceIndex, Temp));
00204         getline(inputfile,line);
00205     }
00206 }
00207 
00210 void ReadDyna::ReadConstraints(std::ifstream &inputfile)
00211 {
00212     std::map<unsigned int, VERTICES>::iterator pnt_it(Pointlist.begin());
00213     std::string line;
00214     char *str;
00215     char Info[10] = {0};
00216     int i = 0, j = 0;
00217     unsigned int First;
00218     unsigned int Second;
00219     unsigned int Third;
00220     getline(inputfile, line);
00221     while (line.find("*") == std::string::npos)  //While... you know the drill
00222     {
00223         if (line.find("$") != std::string::npos) //Ignore comments...
00224         {
00225             getline(inputfile, line);
00226             continue;
00227         }
00228         str = new char [line.size()+1];
00229         strcpy(str, line.c_str());
00230 
00231         for (i = 0; i < 10; i++,j++)
00232             Info[j] = str[i];
00233         First = atoi(Info);   //Constainted Point
00234 
00235         j = 0;
00236 
00237         for (i = 10; i < 20; i++,j++)
00238             Info[j] = str[i];
00239         Second = atoi(Info);  //Constrainer 1
00240 
00241         j = 0;
00242 
00243         for (i = 20; i < 30; i++,j++)
00244             Info[j] = str[i];
00245         Third = atoi(Info);  //Constrainer 2
00246 
00247         j = 0;
00248 
00249         //Set all necessary flags fill the necessary std::vector's
00250         pnt_it = Pointlist.find(First);
00251         (*pnt_it).second.Constrained = true;
00252         (*pnt_it).second.ConstrainedBy.push_back(Second);
00253         (*pnt_it).second.ConstrainedBy.push_back(Third);
00254 
00255         pnt_it = Pointlist.find(Second);
00256         (*pnt_it).second.Constrain = true;
00257         (*pnt_it).second.Constraining.push_back(First);
00258 
00259         pnt_it = Pointlist.find(Third);
00260         (*pnt_it).second.Constrain = true;
00261         (*pnt_it).second.Constraining.push_back(First);
00262 
00263         delete[] str;
00264         getline(inputfile,line);
00265     }
00266 }
00267 
00277 void ReadDyna::Convert()
00278 {
00279     std::map<unsigned int, VERTICES>::iterator pnt_it(Pointlist.begin());
00280     std::map<unsigned int, VERTICES>::iterator constraint_it(Pointlist.begin());
00281     std::map<unsigned int, FACE>::iterator face_it(Facelist.begin());
00282     std::vector<unsigned int>::iterator ver_it;
00283     std::vector<unsigned int>::iterator constrainter_it;
00284     for ( ; face_it != Facelist.end(); face_it++) //For every face...
00285     {
00286         FACE CurFace = (*face_it).second;
00287         std::vector<unsigned int> AdditionalPoint;
00288         std::vector<unsigned int> Constrainter;
00289         unsigned int Doubler = 0;
00290         bool HaveConstraint = false;
00291         bool Side = false;
00292         for (unsigned int i = 0; i < CurFace.PointIndex.size(); i++)//...and for every point in the face...
00293         {
00294             pnt_it = Pointlist.find(CurFace.PointIndex[i]);
00295             if ((*pnt_it).second.Constrain)//...does it constraining any other points?
00296             {
00297                 int NumOfConstraint = 0;
00298                 for (unsigned int j = 0; j < (*pnt_it).second.Constraining.size(); j++) //Yes, so for every point it constraints...
00299                 {
00300                     constraint_it = Pointlist.find((*pnt_it).second.Constraining[j]);
00301                     if ((constrainter_it = find(CurFace.PointIndex.begin(),CurFace.PointIndex.end(),
00302                                                 (*constraint_it).second.PointIndex)) != CurFace.PointIndex.end())  //...does the constraint point also a part of the current face?
00303                     {
00304                         continue; //Yes, so skip it.
00305                     }
00306                     unsigned int ver = (*constraint_it).second.ConstrainedBy[0];  //No, so...
00307                     if (ver == CurFace.PointIndex[i]) //...the point is current point?
00308                         ver = (*constraint_it).second.ConstrainedBy[1]; //Yes, so move to second point in ConstrainedBy
00309                     if ((constrainter_it = find(CurFace.PointIndex.begin(),CurFace.PointIndex.end(),ver))
00310                             != CurFace.PointIndex.end()) //So, the point we are checking, is it a part of the current face?
00311                     {
00312                         HaveConstraint = true;
00313                         NumOfConstraint++; //Yes, so...
00314                         if ((ver_it = find(AdditionalPoint.begin(), AdditionalPoint.end(),(*pnt_it).second.Constraining[j]))
00315                                 == AdditionalPoint.end()) //...have the additional point been added to the list?
00316                         {
00317                             //Yes, push it back, and push the current point index too!!!
00318                             AdditionalPoint.push_back((*pnt_it).second.Constraining[j]);
00319                             Constrainter.push_back(i);
00320                         }
00321                     }
00322                 }
00323                 if (NumOfConstraint == 2) //One point constraining two point...?
00324                 {
00325                     Side = true;
00326                     Doubler = i;   //Set the doubler~
00327                 }
00328             }
00329         }
00330         if (!HaveConstraint) //No Constraints, so...
00331         {
00332             /*  ------
00333                 |    /|
00334                 |   / |
00335              |  /  |
00336              | /   |
00337                 -------
00338              or so...
00339             */
00340             STLINDEX Temp;
00341             Temp.PointIndex.push_back(CurFace.PointIndex[0]);
00342             Temp.PointIndex.push_back(CurFace.PointIndex[1]);
00343             Temp.PointIndex.push_back(CurFace.PointIndex[3]);
00344             Stllist.push_back(Temp);
00345             Temp.PointIndex.clear();
00346 
00347             Temp.PointIndex.push_back(CurFace.PointIndex[1]);
00348             Temp.PointIndex.push_back(CurFace.PointIndex[2]);
00349             Temp.PointIndex.push_back(CurFace.PointIndex[3]);
00350             Stllist.push_back(Temp);
00351 
00352         }
00353         else
00354         {
00355             //Houston, we have some constraints...
00356             switch (AdditionalPoint.size())  //"How much" asks Houston
00357             {
00358             case 1: //1 constrain is the reply
00359             {
00360                 /* ____
00361                    | /|
00362                    |/ |
00363                    |\ |
00364                    | \|
00365                    ----
00366                 */
00367                 STLINDEX Temp;
00368                 int checker;
00369                 if (Constrainter[0] != 0)
00370                 {
00371                     Temp.PointIndex.push_back(CurFace.PointIndex[Constrainter[0]]);
00372                     Temp.PointIndex.push_back(AdditionalPoint[0]);
00373                     Temp.PointIndex.push_back(CurFace.PointIndex[Constrainter[0] - 1]);
00374                     Stllist.push_back(Temp);
00375                     Temp.PointIndex.clear();
00376 
00377                     Temp.PointIndex.push_back(AdditionalPoint[0]);
00378 
00379                     checker = Constrainter[0] - 2;
00380                     if (checker < 0)
00381                         Temp.PointIndex.push_back(CurFace.PointIndex[Constrainter[0]+2]);
00382 
00383 
00384                     else Temp.PointIndex.push_back(CurFace.PointIndex[checker]);
00385 
00386 
00387                     checker = Constrainter[0] - 1;
00388                     if (checker < 0)
00389                         Temp.PointIndex.push_back(CurFace.PointIndex[3]);
00390                     else Temp.PointIndex.push_back(CurFace.PointIndex[checker]);
00391 
00392 
00393                     Stllist.push_back(Temp);
00394                     Temp.PointIndex.clear();
00395 
00396                     Temp.PointIndex.push_back(AdditionalPoint[0]);
00397 
00398                     Temp.PointIndex.push_back(CurFace.PointIndex[Constrainter[0] + 1]);
00399 
00400                     checker = Constrainter[0] + 2;
00401                     if (checker >= 4)
00402                     {
00403                         Temp.PointIndex.push_back(CurFace.PointIndex[Constrainter[0] - 2]);
00404 
00405                     }
00406                     else
00407                     {
00408                         Temp.PointIndex.push_back(CurFace.PointIndex[checker]);
00409 
00410                     }
00411                     Stllist.push_back(Temp);
00412                 }
00413                 else  //Seems Constrainter[0] == 0 have it's own... possibilities
00414                 {
00415 
00416                     Temp.PointIndex.push_back(AdditionalPoint[0]);
00417                     Temp.PointIndex.push_back(CurFace.PointIndex[2]);
00418                     Temp.PointIndex.push_back(CurFace.PointIndex[3]);
00419 
00420                     Stllist.push_back(Temp);
00421                     Temp.PointIndex.clear();
00422 
00423                     Temp.PointIndex.push_back(AdditionalPoint[0]);
00424                     Temp.PointIndex.push_back(CurFace.PointIndex[1]);
00425                     Temp.PointIndex.push_back(CurFace.PointIndex[2]);
00426 
00427                     Stllist.push_back(Temp);
00428                     Temp.PointIndex.clear();
00429 
00430                     Temp.PointIndex.push_back(AdditionalPoint[0]);
00431 
00432                     std::map<unsigned int, VERTICES>::iterator pnt = Pointlist.find(AdditionalPoint[0]);
00433                     std::vector<unsigned int> temp = (*pnt).second.ConstrainedBy;
00434                     std::vector<unsigned int>::iterator vec = find(temp.begin(), temp.end(), CurFace.PointIndex[1]);
00435                     if (vec != temp.end())
00436                     {
00437                         Temp.PointIndex.push_back(CurFace.PointIndex[3]);
00438                         Temp.PointIndex.push_back(CurFace.PointIndex[0]);
00439                         Stllist.push_back(Temp);
00440                     }
00441                     else
00442                     {
00443                         Temp.PointIndex.push_back(CurFace.PointIndex[0]);
00444                         Temp.PointIndex.push_back(CurFace.PointIndex[1]);
00445                         Stllist.push_back(Temp);
00446                     }
00447                 }
00448                 break;
00449             }
00450 
00451             case 2:  //2 Constraints is the reply
00452             {
00453                 /* This one i can't draw... sorry
00454                 */
00455                 STLINDEX temp;
00456                 if (Doubler != 0 && Doubler != 3)
00457                 {
00458                     int checker;
00459                     temp.PointIndex.push_back(AdditionalPoint[1]);
00460                     temp.PointIndex.push_back(AdditionalPoint[0]);
00461                     temp.PointIndex.push_back(CurFace.PointIndex[Doubler]);
00462 
00463                     Stllist.push_back(temp);
00464 
00465 
00466                     checker = Doubler + 2;
00467                     temp.PointIndex[0] = AdditionalPoint[0];
00468                     temp.PointIndex[1] = AdditionalPoint[1];
00469 
00470                     if (checker >= 4)
00471                     {
00472                         temp.PointIndex[2] = CurFace.PointIndex[Doubler-2];
00473 
00474                     }
00475                     else
00476                     {
00477                         temp.PointIndex[2] = CurFace.PointIndex[Doubler+2];
00478 
00479                     }
00480                     Stllist.push_back(temp);
00481 
00482                     checker = Doubler - 2;
00483                     temp.PointIndex[0] = AdditionalPoint[0];
00484 
00485                     if (checker < 0)
00486                         temp.PointIndex[1] = CurFace.PointIndex[Doubler+2];
00487                     else
00488                         temp.PointIndex[1] = CurFace.PointIndex[Doubler-2];
00489 
00490                     temp.PointIndex[2] = CurFace.PointIndex[Doubler-1];
00491 
00492                     Stllist.push_back(temp);
00493 
00494 
00495                     temp.PointIndex[0] = AdditionalPoint[1];
00496 
00497                     checker = Doubler + 1;
00498                     temp.PointIndex[1] = CurFace.PointIndex[Doubler+1];
00499 
00500                     checker = Doubler + 2;
00501                     if (checker >= 4)
00502                         temp.PointIndex[2] = CurFace.PointIndex[Doubler-2];
00503                     else
00504                         temp.PointIndex[2] = CurFace.PointIndex[Doubler+2];
00505 
00506 
00507                     Stllist.push_back(temp);
00508 
00509                 }
00510                 else if (Doubler == 0)
00511                 {
00512                     temp.PointIndex.push_back(AdditionalPoint[0]);
00513                     temp.PointIndex.push_back(AdditionalPoint[1]);
00514                     temp.PointIndex.push_back(CurFace.PointIndex[0]);
00515 
00516                     Stllist.push_back(temp);
00517 
00518                     temp.PointIndex[0] = AdditionalPoint[1];
00519                     temp.PointIndex[1] = AdditionalPoint[0];
00520                     temp.PointIndex[2] = CurFace.PointIndex[2];
00521 
00522                     Stllist.push_back(temp);
00523 
00524                     temp.PointIndex[0] = CurFace.PointIndex[1];
00525                     temp.PointIndex[1] = CurFace.PointIndex[2];
00526                     temp.PointIndex[2] = AdditionalPoint[0];
00527 
00528                     Stllist.push_back(temp);
00529 
00530                     temp.PointIndex[0] = CurFace.PointIndex[2];
00531                     temp.PointIndex[1] = CurFace.PointIndex[3];
00532                     temp.PointIndex[2] = AdditionalPoint[1];
00533 
00534                     Stllist.push_back(temp);
00535                 }
00536                 else
00537                 {
00538                     temp.PointIndex.push_back(AdditionalPoint[0]);
00539                     temp.PointIndex.push_back(AdditionalPoint[1]);
00540                     temp.PointIndex.push_back(CurFace.PointIndex[3]);
00541 
00542                     Stllist.push_back(temp);
00543 
00544                     temp.PointIndex[0] = AdditionalPoint[1];
00545                     temp.PointIndex[1] = AdditionalPoint[0];
00546                     temp.PointIndex[2] = CurFace.PointIndex[1];
00547 
00548                     Stllist.push_back(temp);
00549 
00550                     temp.PointIndex[0] = CurFace.PointIndex[1];
00551                     temp.PointIndex[1] = CurFace.PointIndex[2];
00552                     temp.PointIndex[2] = AdditionalPoint[1];
00553 
00554                     Stllist.push_back(temp);
00555 
00556                     temp.PointIndex[0] = CurFace.PointIndex[0];
00557                     temp.PointIndex[1] = CurFace.PointIndex[1];
00558                     temp.PointIndex[2] = AdditionalPoint[0];
00559 
00560                     Stllist.push_back(temp);
00561 
00562                 }
00563                 break;
00564 
00565             }
00566             case 3:
00567             {
00568                 std::cout << "3 Constraints" << std::endl;
00569                 break;
00570             }
00571             case 4:
00572             {
00573                 std::cout << "4 Constraints" << std::endl;
00574                 break;
00575             }
00576             default: //Have constraints, but no constraining points, or more than 5...? BUGSPAWN
00577             {
00578                 std::cout << AdditionalPoint.size() << std::endl;
00579                 std::cout << "Blah...?" << std::endl;
00580                 break;
00581             }
00582             }
00583         }
00584     }
00585 }
00586 
00589 void ReadDyna::PutInMesh(MeshCore::MeshKernel &mesh)
00590 {
00591     std::map<unsigned int,VERTICES>::iterator pnt_it(Pointlist.begin());
00592     Base::Vector3f Points[3];
00593     MeshCore::MeshBuilder builder(mesh);
00594     builder.Initialize(Stllist.size());
00595     for (unsigned int i = 0; i < Stllist.size();i++)
00596     {
00597         for (unsigned int j = 0; j < 3; j++)
00598         {
00599             pnt_it = Pointlist.find(Stllist[i].PointIndex[j]);
00600             Base::Vector3f Temp((float)(*pnt_it).second.Coords[0],(float)(*pnt_it).second.Coords[1],(float)(*pnt_it).second.Coords[2]);
00601             Points[j] = Temp;
00602         }
00603         MeshCore::MeshGeomFacet Face(Points[0],Points[1],Points[2]);
00604         Face.CalcNormal();
00605         builder.AddFacet(Face);
00606     }
00607     builder.Finish();
00608 }
00609 

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