Back to index

salome-smesh  6.5.0
UNV2420_Structure.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
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 GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "UNV2420_Structure.hxx"
00024 #include "UNV_Utilities.hxx"
00025 
00026 #include <fstream>
00027 #include <cstdio>
00028 #include <cmath>
00029 
00030 using namespace std;
00031 using namespace UNV;
00032 using namespace UNV2420;
00033 
00034 static string _label_dataset = "2420";
00035 
00036 void UNV2420::Read(std::ifstream& in_stream,
00037                    std::string&   part_name, // can re-store a mesh name
00038                    TDataSet&      theDataSet)
00039 {
00040   if(!in_stream.good())
00041     EXCEPTION(runtime_error,"ERROR: Input file not good.");
00042 
00043   /*
00044    * adjust the \p istream to our
00045    * position
00046    */
00047   if(!beginning_of_dataset(in_stream,_label_dataset))
00048     return;
00049 
00050   string num_buf;
00051   int part_uid;
00052 
00053   in_stream >> part_uid; // Record 1
00054   part_name = read_line( in_stream );  // Record 2
00055 
00056   while ( !in_stream.eof() )
00057   {
00058     TRecord aRec;
00059 
00060     // Record 3
00061     in_stream >> aRec.coord_sys_label;
00062     if ( aRec.coord_sys_label == -1 ) // end of dataset is reached
00063       break;
00064     in_stream >> aRec.coord_sys_type;
00065     in_stream >> aRec.coord_sys_color;
00066 
00067     aRec.coord_sys_name = read_line( in_stream ); // Record 4
00068 
00069     // Records 5-8: rows of Transformation Matrix
00070     for ( int row = 0; row < 4; ++row )
00071       for ( int i = 0; i < 3; i++ )
00072       {
00073         in_stream >> num_buf;
00074         aRec.matrix[row][i] = D_to_e(num_buf);
00075       }
00076     // Store a CS data only if it requires conversion into the global Cartesian CS
00077     if ( aRec.coord_sys_type != 0 || !aRec.isIdentityMatrix() ) // 0 - Cartesian CS
00078       theDataSet.push_back( aRec );
00079   }
00080 }
00081 
00082 
00083 void UNV2420::Write(std::ofstream&     out_stream,
00084                     const std::string& part_name)
00085 //                    const TDataSet& theDataSet)
00086 {
00087   if(!out_stream.good())
00088     EXCEPTION(runtime_error,"ERROR: Output file not good.");
00089   
00090   out_stream<<"    -1"  << endl;
00091   out_stream<<"  "<<_label_dataset << endl;
00092 
00093   out_stream<<"         1"                     << endl; // R1: Part UID
00094   if ( part_name.empty() )
00095     out_stream<<"SMESH_Mesh"                   << endl; // R2: Part Name
00096   else
00097     out_stream<< part_name                     << endl;
00098   out_stream<<"         1         0         0" << endl; // R3: Label, Type, Color
00099 
00100   out_stream<<"Global Cartesian Coordinate System" << endl; // R4: Name
00101   out_stream<<"    1.0000000000000000E+0    0.0000000000000000E+0    0.0000000000000000E+0" << endl;
00102   out_stream<<"    0.0000000000000000E+0    1.0000000000000000E+0    0.0000000000000000E+0" << endl;
00103   out_stream<<"    0.0000000000000000E+0    0.0000000000000000E+0    1.0000000000000000E+0" << endl;
00104   out_stream<<"    0.0000000000000000E+0    0.0000000000000000E+0    0.0000000000000000E+0" << endl;
00105 
00106   out_stream<<"    -1"  << endl;
00107 }
00108 
00109 
00110 bool UNV2420::TRecord::isIdentityMatrix() const
00111 {
00112   bool isIdentity = true;
00113   for ( int row = 0; row < 4 && isIdentity; ++row )
00114     for ( int i = 0; i < 3; i++ )
00115     {
00116       if ( matrix[row][i] != ( row==i ? 1. : 0. ))
00117       {
00118         isIdentity = false;
00119         break;
00120       }
00121     }
00122   return isIdentity;
00123 }
00124 
00125 void UNV2420::TRecord::ApplyMatrix( double* c ) const
00126 {
00127   const double x = matrix[0][0] * c[0] + matrix[0][1] * c[1] + matrix[0][2] * c[2];
00128   const double y = matrix[1][0] * c[0] + matrix[1][1] * c[1] + matrix[1][2] * c[2];
00129   const double z = matrix[2][0] * c[0] + matrix[2][1] * c[1] + matrix[2][2] * c[2];
00130   c[0] = x + matrix[3][0];
00131   c[1] = y + matrix[3][1];
00132   c[2] = z + matrix[3][2];
00133 }
00134 
00135 void UNV2420::TRecord::FromCylindricalCS( double* coords )
00136 {
00137   const double x = coords[0] * cos( coords[1] );
00138   const double y = coords[0] * sin( coords[1] );
00139   coords[0] = x;
00140   coords[1] = y;
00141 }
00142 
00143 void UNV2420::TRecord::FromSphericalCS  ( double* coords )
00144 {
00145   const double sin2 = sin( coords[2] );
00146   const double x = coords[0] * cos( coords[1] ) * sin2;
00147   const double y = coords[0] * sin( coords[1] ) * sin2;
00148   const double z = coords[0] * cos( coords[2] );
00149   coords[0] = x;
00150   coords[1] = y;
00151   coords[2] = z;
00152 }