]> SALOME platform Git repositories - samples/sierpinsky.git/blob - src/Sierpinsky/SIERPINSKY_Gen_i.cxx
Salome HOME
CoTech decision: move MEDWrapper from MED to SMESH
[samples/sierpinsky.git] / src / Sierpinsky / SIERPINSKY_Gen_i.cxx
1 // Copyright (C) 2005-2013  OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 ///////////////////////////////////////////////////////////
21 // File    : SIERPINSKY_Gen_i.cxx
22 // Author  : Vadim SANDLER (OCN)
23 // Created : 13/07/05
24 ///////////////////////////////////////////////////////////
25 //
26
27 #include "SIERPINSKY_Gen_i.hxx"
28 #include "SIERPINSKY_version.h"
29 #include <MED_Factory.hxx>
30 #include <gd.h>
31
32 using namespace MED;
33
34 /*!
35  * Engine factory
36  */
37 extern "C"
38 {
39   PortableServer::ObjectId * SIERPINSKYEngine_factory( CORBA::ORB_ptr            orb,
40                                                        PortableServer::POA_ptr   poa, 
41                                                        PortableServer::ObjectId* contId,
42                                                        const char*               instanceName,
43                                                        const char*               interfaceName )
44 {
45   SIERPINSKY_Gen_i* anEngine = new SIERPINSKY_Gen_i( orb, poa, contId, instanceName, interfaceName );
46   return anEngine->getId() ;
47 }
48 }
49
50 /*!
51  * Default constructor
52  */
53 SIERPINSKY_Gen_i::SIERPINSKY_Gen_i()
54 {
55 }
56
57 /*!
58  * Constructor
59  */
60 SIERPINSKY_Gen_i::SIERPINSKY_Gen_i( CORBA::ORB_ptr            orb,
61                                     PortableServer::POA_ptr   poa,
62                                     PortableServer::ObjectId* contId, 
63                                     const char*               instanceName, 
64                                     const char*               interfaceName ) 
65 : Engines_Component_i( orb, poa, contId, instanceName, interfaceName ) 
66 {
67   // activate servant
68   _thisObj = this;
69   _id = poa->activate_object(_thisObj);
70   // set default values
71   Reset();
72 }
73
74 /*!
75  * Destructor
76  */
77 SIERPINSKY_Gen_i::~SIERPINSKY_Gen_i()
78 {
79   myPoints.clear();
80 }
81   
82 /*!
83  * Initializes engine with three reference points
84  */
85 void SIERPINSKY_Gen_i::Init( CORBA::Double theX1, CORBA::Double theY1, 
86                              CORBA::Double theX2, CORBA::Double theY2, 
87                              CORBA::Double theX3, CORBA::Double theY3 )
88 {
89   myRefPoints[0] = MyPoint( theX1, theY1 );
90   myRefPoints[1] = MyPoint( theX2, theY2 );
91   myRefPoints[2] = MyPoint( theX3, theY3 );
92   myPoints.clear();
93 }
94   
95 /*!
96  * Initializes engine with three default reference points: (0.5, 1), (0, 0), (1, 0)
97  */
98 void SIERPINSKY_Gen_i::Reset()
99 {
100   myRefPoints[0] = MyPoint( 0.5, 1.0 );
101   myRefPoints[1] = MyPoint( 0.0, 0.0 );
102   myRefPoints[2] = MyPoint( 1.0, 0.0 );
103   myPoints.clear();
104 }
105   
106 /*!
107  * Generates next iteration point
108  */
109 void SIERPINSKY_Gen_i::NextPoint( CORBA::Double  theX,     CORBA::Double  theY, 
110                                   CORBA::Long    theIter, 
111                                   CORBA::Double& theNextX, CORBA::Double& theNextY )
112 {
113   double x = theIter < 1 || theIter > 3 ? theX : ( theX + myRefPoints[ theIter-1 ].myX ) / 2;
114   double y = theIter < 1 || theIter > 3 ? theY : ( theY + myRefPoints[ theIter-1 ].myY ) / 2;
115   myPoints.push_back( MyPoint( x, y ) );
116   theNextX = x;
117   theNextY = y;
118 }
119   
120 /*!
121  * Exports data to the JPEG image
122  */
123 CORBA::Boolean SIERPINSKY_Gen_i::ExportToJPEG( const char* theFileName, CORBA::Long theSize )
124 {
125   if ( theSize <= 0 ) return false;
126
127   // open file
128   FILE* fileDescriptor = fopen( theFileName, "wb" );
129   if ( !fileDescriptor ) return false;
130
131   // create an image
132   gdImagePtr image = gdImageCreate( theSize, theSize );
133   int white = gdImageColorAllocate( image, 255, 255, 255 );
134   int black = gdImageColorAllocate( image,   0,   0,   0 );
135
136   gdImageRectangle( image, 0, 0, theSize-1, theSize-1, white );
137
138   // draw points
139   std::list<MyPoint>::const_iterator iter;
140   for ( iter = myPoints.begin(); iter != myPoints.end(); ++iter ) {
141     gdImageSetPixel( image, (int)( (*iter).myX * theSize ), theSize - (int)( (*iter).myY * theSize ), black );
142   }
143
144   // export jpeg image
145   gdImageJpeg( image, fileDescriptor, 95 );
146   fclose( fileDescriptor );
147   gdImageDestroy( image );
148   
149   return true;
150 }
151   
152 /*!
153  * Exports data to the MED file
154  */
155 CORBA::Boolean SIERPINSKY_Gen_i::ExportToMED( const char* theFileName, CORBA::Double theSize )
156 {
157   TErr anError;
158   
159   // if file already exists - remove it (MED cannot overwrite files correctly)
160   FILE* aFile = fopen( theFileName, "rb" );
161   if ( aFile ) {
162     fclose( aFile );
163     if ( remove( theFileName ) ) return false; // can't remove file
164   }
165   
166   // create MED 2.2 file
167   PWrapper aMed = CrWrapper( theFileName, MED::eV2_2 );
168
169   // create 2D mesh
170   PMeshInfo aMesh = aMed->CrMeshInfo( 2, 2, "Sierpinsky" );
171   aMed->SetMeshInfo( aMesh, &anError );
172   if ( anError < 0 ) return false;
173
174   // create nodes
175   TFloatVector nodes;
176   TIntVector   connect;
177   std::list<MyPoint>::const_iterator iter;
178   int ind = 1;
179   for ( iter = myPoints.begin(); iter != myPoints.end(); ++iter ) {
180     nodes.push_back( (*iter).myX * theSize );
181     nodes.push_back( (*iter).myY * theSize );
182     connect.push_back( ind++ );
183   }
184   PNodeInfo aNodes = aMed->CrNodeInfo( aMesh, nodes, MED::eFULL_INTERLACE, MED::eCART, TStringVector(2), TStringVector(2), TIntVector( myPoints.size() ), TIntVector() );
185   aMed->SetNodeInfo( aNodes, &anError );
186   if ( anError < 0 ) return false;
187
188   PCellInfo aCells = aMed->CrCellInfo( aMesh, MED::eMAILLE, MED::ePOINT1, connect, eNOD, TIntVector( myPoints.size() ), TIntVector( myPoints.size() ) );
189   aMed->SetCellInfo( aCells, &anError );
190   if ( anError < 0 ) return false;
191
192   return true;
193 }
194
195 // Version information
196 char* SIERPINSKY_Gen_i::getVersion()
197 {
198 #if SIERPINSKY_DEVELOPMENT
199   return CORBA::string_dup(SIERPINSKY_VERSION_STR"dev");
200 #else
201   return CORBA::string_dup(SIERPINSKY_VERSION_STR);
202 #endif
203 }