1 // Copyright (C) 2005-2010 OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 ///////////////////////////////////////////////////////////
21 // File : SIERPINSKY_Gen_i.cxx
22 // Author : Vadim SANDLER (OCN)
24 ///////////////////////////////////////////////////////////
26 #include "SIERPINSKY_Gen_i.hxx"
27 #include <MED_Factory.hxx>
37 PortableServer::ObjectId * SIERPINSKYEngine_factory( CORBA::ORB_ptr orb,
38 PortableServer::POA_ptr poa,
39 PortableServer::ObjectId* contId,
40 const char* instanceName,
41 const char* interfaceName )
43 SIERPINSKY_Gen_i* anEngine = new SIERPINSKY_Gen_i( orb, poa, contId, instanceName, interfaceName );
44 return anEngine->getId() ;
51 SIERPINSKY_Gen_i::SIERPINSKY_Gen_i()
58 SIERPINSKY_Gen_i::SIERPINSKY_Gen_i( CORBA::ORB_ptr orb,
59 PortableServer::POA_ptr poa,
60 PortableServer::ObjectId* contId,
61 const char* instanceName,
62 const char* interfaceName )
63 : Engines_Component_i( orb, poa, contId, instanceName, interfaceName )
67 _id = poa->activate_object(_thisObj);
75 SIERPINSKY_Gen_i::~SIERPINSKY_Gen_i()
81 * Initializes engine with three reference points
83 void SIERPINSKY_Gen_i::Init( CORBA::Double theX1, CORBA::Double theY1,
84 CORBA::Double theX2, CORBA::Double theY2,
85 CORBA::Double theX3, CORBA::Double theY3 )
87 myRefPoints[0] = MyPoint( theX1, theY1 );
88 myRefPoints[1] = MyPoint( theX2, theY2 );
89 myRefPoints[2] = MyPoint( theX3, theY3 );
94 * Initializes engine with three default reference points: (0.5, 1), (0, 0), (1, 0)
96 void SIERPINSKY_Gen_i::Reset()
98 myRefPoints[0] = MyPoint( 0.5, 1.0 );
99 myRefPoints[1] = MyPoint( 0.0, 0.0 );
100 myRefPoints[2] = MyPoint( 1.0, 0.0 );
105 * Generates next iteration point
107 void SIERPINSKY_Gen_i::NextPoint( CORBA::Double theX, CORBA::Double theY,
109 CORBA::Double& theNextX, CORBA::Double& theNextY )
111 double x = theIter < 1 || theIter > 3 ? theX : ( theX + myRefPoints[ theIter-1 ].myX ) / 2;
112 double y = theIter < 1 || theIter > 3 ? theY : ( theY + myRefPoints[ theIter-1 ].myY ) / 2;
113 myPoints.push_back( MyPoint( x, y ) );
119 * Exports data to the JPEG image
121 CORBA::Boolean SIERPINSKY_Gen_i::ExportToJPEG( const char* theFileName, CORBA::Long theSize )
123 if ( theSize <= 0 ) return false;
126 FILE* fileDescriptor = fopen( theFileName, "wb" );
127 if ( !fileDescriptor ) return false;
130 gdImagePtr image = gdImageCreate( theSize, theSize );
131 int white = gdImageColorAllocate( image, 255, 255, 255 );
132 int black = gdImageColorAllocate( image, 0, 0, 0 );
134 gdImageRectangle( image, 0, 0, theSize-1, theSize-1, white );
137 std::list<MyPoint>::const_iterator iter;
138 for ( iter = myPoints.begin(); iter != myPoints.end(); ++iter ) {
139 gdImageSetPixel( image, (int)( (*iter).myX * theSize ), theSize - (int)( (*iter).myY * theSize ), black );
143 gdImageJpeg( image, fileDescriptor, 95 );
144 fclose( fileDescriptor );
145 gdImageDestroy( image );
151 * Exports data to the MED file
153 CORBA::Boolean SIERPINSKY_Gen_i::ExportToMED( const char* theFileName, CORBA::Double theSize )
157 // if file already exists - remove it (MED cannot overwrite files correctly)
158 FILE* aFile = fopen( theFileName, "rb" );
161 if ( remove( theFileName ) ) return false; // can't remove file
164 // create MED 2.2 file
165 PWrapper aMed = CrWrapper( theFileName, MED::eV2_2 );
168 PMeshInfo aMesh = aMed->CrMeshInfo( 2, 2, "Sierpinsky" );
169 aMed->SetMeshInfo( aMesh, &anError );
170 if ( anError < 0 ) return false;
175 std::list<MyPoint>::const_iterator iter;
177 for ( iter = myPoints.begin(); iter != myPoints.end(); ++iter ) {
178 nodes.push_back( (*iter).myX * theSize );
179 nodes.push_back( (*iter).myY * theSize );
180 connect.push_back( ind++ );
182 PNodeInfo aNodes = aMed->CrNodeInfo( aMesh, nodes, MED::eFULL_INTERLACE, MED::eCART, TStringVector(2), TStringVector(2), TIntVector( myPoints.size() ), TIntVector() );
183 aMed->SetNodeInfo( aNodes, &anError );
184 if ( anError < 0 ) return false;
186 PCellInfo aCells = aMed->CrCellInfo( aMesh, MED::eMAILLE, MED::ePOINT1, connect, eNOD, TIntVector( myPoints.size() ), TIntVector( myPoints.size() ) );
187 aMed->SetCellInfo( aCells, &anError );
188 if ( anError < 0 ) return false;