Salome HOME
Adjust to OCC_development_generic_2006 changes
[samples/sierpinsky.git] / src / Sierpinsky / SIERPINSKY_Gen_i.cxx
1 ///////////////////////////////////////////////////////////
2 // File    : SIERPINSKY_Gen_i.cxx
3 // Author  : Vadim SANDLER (OCN)
4 // Created : 13/07/05
5 // Copyright (C) 2005 Open CASCADE
6 ///////////////////////////////////////////////////////////
7
8 #include "SIERPINSKY_Gen_i.hxx"
9 #include <MED_Factory.hxx>
10 #include <gd.h>
11
12 using namespace MED;
13
14 /*!
15  * Engine factory
16  */
17 extern "C"
18 {
19   PortableServer::ObjectId * SIERPINSKYEngine_factory( CORBA::ORB_ptr            orb,
20                                                        PortableServer::POA_ptr   poa, 
21                                                        PortableServer::ObjectId* contId,
22                                                        const char*               instanceName,
23                                                        const char*               interfaceName )
24 {
25   SIERPINSKY_Gen_i* anEngine = new SIERPINSKY_Gen_i( orb, poa, contId, instanceName, interfaceName );
26   return anEngine->getId() ;
27 }
28 }
29
30 /*!
31  * Default constructor
32  */
33 SIERPINSKY_Gen_i::SIERPINSKY_Gen_i()
34 {
35 }
36
37 /*!
38  * Constructor
39  */
40 SIERPINSKY_Gen_i::SIERPINSKY_Gen_i( CORBA::ORB_ptr            orb,
41                                     PortableServer::POA_ptr   poa,
42                                     PortableServer::ObjectId* contId, 
43                                     const char*               instanceName, 
44                                     const char*               interfaceName ) 
45 : Engines_Component_i( orb, poa, contId, instanceName, interfaceName ) 
46 {
47   // activate servant
48   _thisObj = this;
49   _id = poa->activate_object(_thisObj);
50   // set default values
51   Reset();
52 }
53
54 /*!
55  * Destructor
56  */
57 SIERPINSKY_Gen_i::~SIERPINSKY_Gen_i()
58 {
59   myPoints.clear();
60 }
61   
62 /*!
63  * Initializes engine with three reference points
64  */
65 void SIERPINSKY_Gen_i::Init( CORBA::Double theX1, CORBA::Double theY1, 
66                              CORBA::Double theX2, CORBA::Double theY2, 
67                              CORBA::Double theX3, CORBA::Double theY3 )
68 {
69   myRefPoints[0] = MyPoint( theX1, theY1 );
70   myRefPoints[1] = MyPoint( theX2, theY2 );
71   myRefPoints[2] = MyPoint( theX3, theY3 );
72   myPoints.clear();
73 }
74   
75 /*!
76  * Initializes engine with three default reference points: (0.5, 1), (0, 0), (1, 0)
77  */
78 void SIERPINSKY_Gen_i::Reset()
79 {
80   myRefPoints[0] = MyPoint( 0.5, 1.0 );
81   myRefPoints[1] = MyPoint( 0.0, 0.0 );
82   myRefPoints[2] = MyPoint( 1.0, 0.0 );
83   myPoints.clear();
84 }
85   
86 /*!
87  * Generates next iteration point
88  */
89 void SIERPINSKY_Gen_i::NextPoint( CORBA::Double  theX,     CORBA::Double  theY, 
90                                   CORBA::Long    theIter, 
91                                   CORBA::Double& theNextX, CORBA::Double& theNextY )
92 {
93   double x = theIter < 1 || theIter > 3 ? theX : ( theX + myRefPoints[ theIter-1 ].myX ) / 2;
94   double y = theIter < 1 || theIter > 3 ? theY : ( theY + myRefPoints[ theIter-1 ].myY ) / 2;
95   myPoints.push_back( MyPoint( x, y ) );
96   theNextX = x;
97   theNextY = y;
98 }
99   
100 /*!
101  * Exports data to the JPEG image
102  */
103 CORBA::Boolean SIERPINSKY_Gen_i::ExportToJPEG( const char* theFileName, CORBA::Long theSize )
104 {
105   if ( theSize <= 0 ) return false;
106
107   // open file
108   FILE* fileDescriptor = fopen( theFileName, "wb" );
109   if ( !fileDescriptor ) return false;
110
111   // create an image
112   gdImagePtr image = gdImageCreate( theSize, theSize );
113   int white = gdImageColorAllocate( image, 255, 255, 255 );
114   int black = gdImageColorAllocate( image,   0,   0,   0 );
115
116   gdImageRectangle( image, 0, 0, theSize-1, theSize-1, white );
117
118   // draw points
119   std::list<MyPoint>::const_iterator iter;
120   for ( iter = myPoints.begin(); iter != myPoints.end(); ++iter ) {
121     gdImageSetPixel( image, (int)( (*iter).myX * theSize ), theSize - (int)( (*iter).myY * theSize ), black );
122   }
123
124   // export jpeg image
125   gdImageJpeg( image, fileDescriptor, 95 );
126   fclose( fileDescriptor );
127   gdImageDestroy( image );
128   
129   return true;
130 }
131   
132 /*!
133  * Exports data to the MED file
134  */
135 CORBA::Boolean SIERPINSKY_Gen_i::ExportToMED( const char* theFileName, CORBA::Double theSize )
136 {
137   TErr anError;
138   
139   // if file already exists - remove it (MED cannot overwrite files correctly)
140   FILE* aFile = fopen( theFileName, "rb" );
141   if ( aFile ) {
142     fclose( aFile );
143     if ( remove( theFileName ) ) return false; // can't remove file
144   }
145   
146   // create MED 2.2 file
147   PWrapper aMed = CrWrapper( theFileName, MED::eV2_2 );
148
149   // create 2D mesh
150   PMeshInfo aMesh = aMed->CrMeshInfo( 2, "Sierpinsky" );
151   aMed->SetMeshInfo( aMesh, &anError );
152   if ( anError < 0 ) return false;
153
154   // create nodes
155   TFloatVector nodes;
156   TIntVector   connect;
157   std::list<MyPoint>::const_iterator iter;
158   int ind = 1;
159   for ( iter = myPoints.begin(); iter != myPoints.end(); ++iter ) {
160     nodes.push_back( (*iter).myX * theSize );
161     nodes.push_back( (*iter).myY * theSize );
162     connect.push_back( ind++ );
163   }
164   PNodeInfo aNodes = aMed->CrNodeInfo( aMesh, nodes, MED::eFULL_INTERLACE, MED::eCART, TStringVector(2), TStringVector(2), TIntVector( myPoints.size() ), TIntVector() );
165   aMed->SetNodeInfo( aNodes, &anError );
166   if ( anError < 0 ) return false;
167
168   PCellInfo aCells = aMed->CrCellInfo( aMesh, MED::eMAILLE, MED::ePOINT1, connect, eNOD, TIntVector( myPoints.size() ), TIntVector( myPoints.size() ) );
169   aMed->SetCellInfo( aCells, &anError );
170   if ( anError < 0 ) return false;
171
172   return true;
173 }