1 ///////////////////////////////////////////////////////////
2 // File : SIERPINSKY_Gen_i.cxx
3 // Author : Vadim SANDLER (OCN)
5 // Copyright (C) 2005 Open CASCADE
6 ///////////////////////////////////////////////////////////
8 #include "SIERPINSKY_Gen_i.hxx"
9 #include <MED_Factory.hxx>
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 )
25 SIERPINSKY_Gen_i* anEngine = new SIERPINSKY_Gen_i( orb, poa, contId, instanceName, interfaceName );
26 return anEngine->getId() ;
33 SIERPINSKY_Gen_i::SIERPINSKY_Gen_i()
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 )
49 _id = poa->activate_object(_thisObj);
57 SIERPINSKY_Gen_i::~SIERPINSKY_Gen_i()
63 * Initializes engine with three reference points
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 )
69 myRefPoints[0] = MyPoint( theX1, theY1 );
70 myRefPoints[1] = MyPoint( theX2, theY2 );
71 myRefPoints[2] = MyPoint( theX3, theY3 );
76 * Initializes engine with three default reference points: (0.5, 1), (0, 0), (1, 0)
78 void SIERPINSKY_Gen_i::Reset()
80 myRefPoints[0] = MyPoint( 0.5, 1.0 );
81 myRefPoints[1] = MyPoint( 0.0, 0.0 );
82 myRefPoints[2] = MyPoint( 1.0, 0.0 );
87 * Generates next iteration point
89 void SIERPINSKY_Gen_i::NextPoint( CORBA::Double theX, CORBA::Double theY,
91 CORBA::Double& theNextX, CORBA::Double& theNextY )
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 ) );
101 * Exports data to the JPEG image
103 CORBA::Boolean SIERPINSKY_Gen_i::ExportToJPEG( const char* theFileName, CORBA::Long theSize )
105 if ( theSize <= 0 ) return false;
108 FILE* fileDescriptor = fopen( theFileName, "wb" );
109 if ( !fileDescriptor ) return false;
112 gdImagePtr image = gdImageCreate( theSize, theSize );
113 int white = gdImageColorAllocate( image, 255, 255, 255 );
114 int black = gdImageColorAllocate( image, 0, 0, 0 );
116 gdImageRectangle( image, 0, 0, theSize-1, theSize-1, white );
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 );
125 gdImageJpeg( image, fileDescriptor, 95 );
126 fclose( fileDescriptor );
127 gdImageDestroy( image );
133 * Exports data to the MED file
135 CORBA::Boolean SIERPINSKY_Gen_i::ExportToMED( const char* theFileName, CORBA::Double theSize )
139 // if file already exists - remove it (MED cannot overwrite files correctly)
140 FILE* aFile = fopen( theFileName, "rb" );
143 if ( remove( theFileName ) ) return false; // can't remove file
146 // create MED 2.2 file
147 PWrapper aMed = CrWrapper( theFileName, MED::eV2_2 );
150 PMeshInfo aMesh = aMed->CrMeshInfo( 2, "Sierpinsky" );
151 aMed->SetMeshInfo( aMesh, &anError );
152 if ( anError < 0 ) return false;
157 std::list<MyPoint>::const_iterator iter;
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++ );
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;
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;