zone_region.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #include "zone_region.h"
00027 
00028 using namespace NLMISC;
00029 using namespace std;
00030 
00031 namespace NLLIGO
00032 {
00033 
00034 string CZoneRegion::_StringOutOfBound;
00035 
00036 // ***************************************************************************
00037 // SZoneUnit
00038 // ***************************************************************************
00039 
00040 // ---------------------------------------------------------------------------
00041 CZoneRegion::SZoneUnit::SZoneUnit()
00042 {
00043         ZoneName = STRING_UNUSED;
00044         PosX = PosY = 0;
00045         Rot = Flip = 0;
00046         SharingMatNames[0] = STRING_UNUSED;
00047         SharingMatNames[1] = STRING_UNUSED;
00048         SharingMatNames[2] = STRING_UNUSED;
00049         SharingMatNames[3] = STRING_UNUSED;
00050         SharingCutEdges[0] = 0;
00051         SharingCutEdges[1] = 0;
00052         SharingCutEdges[2] = 0;
00053         SharingCutEdges[3] = 0;
00054 }
00055 
00056 // ---------------------------------------------------------------------------
00057 void CZoneRegion::SZoneUnit::serial (NLMISC::IStream &f)
00058 {
00059         f.xmlSerial (ZoneName, "NAME");
00060         f.xmlSerial (PosX, "X");
00061         f.xmlSerial (PosY, "Y");
00062         f.xmlSerial (Rot, "ROT");
00063         f.xmlSerial (Flip, "FLIP");
00064 
00065         for (uint32 i = 0; i < 4; ++i)
00066         {
00067                 f.xmlSerial (SharingMatNames[i], "MAT_NAMES");
00068                 f.xmlSerial (SharingCutEdges[i], "CUR_EDGES");
00069         }
00070 }
00071 
00072 // ---------------------------------------------------------------------------
00073 const CZoneRegion::SZoneUnit& CZoneRegion::SZoneUnit::operator=(const CZoneRegion::SZoneUnit&zu)
00074 {
00075         this->ZoneName  = zu.ZoneName;
00076         this->PosX              = zu.PosX;
00077         this->PosY              = zu.PosY;
00078         this->Rot               = zu.Rot;
00079         this->Flip              = zu.Flip;
00080         for (uint32 i = 0; i < 4; ++i)
00081         {
00082                 this->SharingMatNames[i] = zu.SharingMatNames[i];
00083                 this->SharingCutEdges[i] = zu.SharingCutEdges[i];
00084         }
00085         return *this;
00086 }
00087 
00088 
00089 // ***************************************************************************
00090 // SZoneUnit2
00091 // ***************************************************************************
00092 
00093 // ---------------------------------------------------------------------------
00094 CZoneRegion::SZoneUnit2::SZoneUnit2()
00095 {
00096         DateLow = 0;
00097         DateHigh = 0;
00098 }
00099 
00100 // ---------------------------------------------------------------------------
00101 void CZoneRegion::SZoneUnit2::serial (NLMISC::IStream &f)
00102 {
00103         /*sint32 version =*/ f.serialVersion (0);
00104 
00105         SZoneUnit::serial (f);
00106         f.xmlSerial (DateLow, "LOW");
00107         f.xmlSerial (DateHigh, "HIGH");
00108 }
00109 
00110 // ---------------------------------------------------------------------------
00111 const CZoneRegion::SZoneUnit2& CZoneRegion::SZoneUnit2::operator=(const CZoneRegion::SZoneUnit2&zu)
00112 {
00113         this->ZoneName  = zu.ZoneName;
00114         this->PosX              = zu.PosX;
00115         this->PosY              = zu.PosY;
00116         this->Rot               = zu.Rot;
00117         this->Flip              = zu.Flip;
00118         for (uint32 i = 0; i < 4; ++i)
00119         {
00120                 this->SharingMatNames[i] = zu.SharingMatNames[i];
00121                 this->SharingCutEdges[i] = zu.SharingCutEdges[i];
00122         }
00123         this->DateLow   = zu.DateLow;
00124         this->DateHigh  = zu.DateHigh;
00125         return *this;
00126 }
00127 
00128 // ---------------------------------------------------------------------------
00129 const CZoneRegion::SZoneUnit2& CZoneRegion::SZoneUnit2::operator=(const CZoneRegion::SZoneUnit&zu)
00130 {
00131         this->ZoneName  = zu.ZoneName;
00132         this->PosX              = zu.PosX;
00133         this->PosY              = zu.PosY;
00134         this->Rot               = zu.Rot;
00135         this->Flip              = zu.Flip;
00136         for (uint32 i = 0; i < 4; ++i)
00137         {
00138                 this->SharingMatNames[i] = zu.SharingMatNames[i];
00139                 this->SharingCutEdges[i] = zu.SharingCutEdges[i];
00140         }
00141         this->DateLow   = 0;
00142         this->DateHigh  = 0;
00143         return *this;
00144 }
00145 
00146 // ***************************************************************************
00147 // CZoneRegion
00148 // ***************************************************************************
00149 
00150 // ---------------------------------------------------------------------------
00151 CZoneRegion::CZoneRegion()
00152 {
00153         _StringOutOfBound = STRING_OUT_OF_BOUND;
00154         _MinX = _MinY = 0;
00155         _MaxX = _MaxY = -1;
00156 }
00157 
00158 // ---------------------------------------------------------------------------
00159 void CZoneRegion::serial (NLMISC::IStream &f)
00160 {
00161         f.xmlPush ("LAND");
00162                 
00163                 sint32 version = f.serialVersion (1);
00164                 f.serialCheck ((uint32)'DNAL');
00165                 
00166                 f.xmlSerial (_MinX, "MIN_X");
00167                 f.xmlSerial (_MinY, "MIN_Y");
00168                 f.xmlSerial (_MaxX, "MAX_X");
00169                 f.xmlSerial (_MaxY, "MAX_Y");
00170 
00171                 if (version == 1)
00172                 {
00173                         f.serialCont (_Zones);
00174                 }
00175 
00176                 if (version == 0)
00177                 {
00178                         std::vector<SZoneUnit> vZonesTmp;
00179                         f.serialCont (vZonesTmp);
00180                         _Zones.resize (vZonesTmp.size());
00181                         for (uint32 i = 0; i < vZonesTmp.size(); ++i)
00182                                 _Zones[i] = vZonesTmp[i];
00183                 }
00184 
00185         f.xmlPop ();
00186 }
00187 
00188 // ---------------------------------------------------------------------------
00189 const string &CZoneRegion::getName (sint32 x, sint32 y) const
00190 {
00191         if ((x < _MinX) || (x > _MaxX) ||
00192                 (y < _MinY) || (y > _MaxY))
00193         {
00194                 return _StringOutOfBound;
00195         }
00196         else
00197         {
00198                 return _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].ZoneName;
00199         }
00200 }
00201 
00202 // ---------------------------------------------------------------------------
00203 uint8 CZoneRegion::getPosX (sint32 x, sint32 y) const
00204 {
00205         if ((x < _MinX) || (x > _MaxX) ||
00206                 (y < _MinY) || (y > _MaxY))
00207         {
00208                 return 0;
00209         }
00210         else
00211         {
00212                 return _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].PosX;
00213         }
00214 }
00215 
00216 // ---------------------------------------------------------------------------
00217 uint8 CZoneRegion::getPosY (sint32 x, sint32 y) const
00218 {
00219         if ((x < _MinX) || (x > _MaxX) ||
00220                 (y < _MinY) || (y > _MaxY))
00221         {
00222                 return 0;
00223         }
00224         else
00225         {
00226                 return _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].PosY;
00227         }
00228 }
00229 
00230 // ---------------------------------------------------------------------------
00231 uint8 CZoneRegion::getRot (sint32 x, sint32 y) const
00232 {
00233         if ((x < _MinX) || (x > _MaxX) ||
00234                 (y < _MinY) || (y > _MaxY))
00235         {
00236                 return 0;
00237         }
00238         else
00239         {
00240                 return _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].Rot;
00241         }
00242 }
00243 
00244 // ---------------------------------------------------------------------------
00245 uint8 CZoneRegion::getFlip (sint32 x, sint32 y) const
00246 {
00247         if ((x < _MinX) || (x > _MaxX) ||
00248                 (y < _MinY) || (y > _MaxY))
00249         {
00250                 return 0;
00251         }
00252         else
00253         {
00254                 return _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].Flip;
00255         }
00256 }
00257 
00258 // ---------------------------------------------------------------------------
00259 uint8 CZoneRegion::getCutEdge (sint32 x, sint32 y, uint8 pos) const
00260 {
00261         if ((x < _MinX) || (x > _MaxX) ||
00262                 (y < _MinY) || (y > _MaxY))
00263         {
00264                 return 0;
00265         }
00266         else
00267         {
00268                 return _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].SharingCutEdges[pos];
00269         }
00270 }
00271 
00272 // ---------------------------------------------------------------------------
00273 uint32 CZoneRegion::getDate (sint32 x, sint32 y, uint8 lowOrHigh) const // lowOrHigh == 0 -> low
00274 {
00275         if ((x < _MinX) || (x > _MaxX) ||
00276                 (y < _MinY) || (y > _MaxY))
00277         {
00278                 return 0;
00279         }
00280         else
00281         {
00282                 if (lowOrHigh == 0)
00283                         return _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].DateLow;
00284                 else
00285                         return _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].DateHigh;
00286         }
00287 }
00288 
00289 // ---------------------------------------------------------------------------
00290 void CZoneRegion::resize (sint32 newMinX, sint32 newMaxX, sint32 newMinY, sint32 newMaxY)
00291 {
00292         sint32 i, j;
00293         vector<SZoneUnit2> newZones;
00294         SZoneUnit2 zuTmp;
00295 
00296         newZones.resize ((1+newMaxX-newMinX)*(1+newMaxY-newMinY));
00297         sint32 newStride = 1+newMaxX-newMinX;
00298         sint32 Stride = 1+_MaxX-_MinX;
00299         for (j = newMinY; j <= newMaxY; ++j)
00300         for (i = newMinX; i <= newMaxX; ++i)
00301         {
00302                 if ((i >= _MinX)&&(i <= _MaxX)&&(j >= _MinY)&&(j <= _MaxY))
00303                 {
00304                         newZones[(i-newMinX)+(j-newMinY)*newStride] = _Zones[(i-_MinX)+(j-_MinY)*Stride];
00305                 }
00306                 else
00307                 {
00308                         newZones[(i-newMinX)+(j-newMinY)*newStride] = zuTmp;
00309                 }
00310         }
00311         _MinX = newMinX; _MaxX = newMaxX;
00312         _MinY = newMinY; _MaxY = newMaxY;
00313         _Zones = newZones;
00314 }
00315 
00316 // ---------------------------------------------------------------------------
00317 void CZoneRegion::basicSet (sint32 x, sint32 y, sint32 PosX, sint32 PosY,  const std::string &ZoneName)
00318 {
00319         // Do we need to resize ?
00320         if ((x < _MinX) || (x > _MaxX) ||
00321                 (y < _MinY) || (y > _MaxY))
00322         {
00323                 sint32 newMinX = (x<_MinX?x:_MinX), newMinY = (y<_MinY?y:_MinY);
00324                 sint32 newMaxX = (x>_MaxX?x:_MaxX), newMaxY = (y>_MaxY?y:_MaxY);
00325 
00326                 resize (newMinX, newMaxX, newMinY, newMaxY);
00327         }
00328         sint32 stride = (1+_MaxX-_MinX); // Nb to go to next line
00329 
00330         _Zones[(x-_MinX)+(y-_MinY)*stride].ZoneName = ZoneName;
00331         _Zones[(x-_MinX)+(y-_MinY)*stride].PosX = (uint8)PosX;
00332         _Zones[(x-_MinX)+(y-_MinY)*stride].PosY = (uint8)PosY;
00333 }
00334 
00335 // ---------------------------------------------------------------------------
00336 void SPiece::rotFlip (uint8 rot, uint8 flip)
00337 {
00338         uint8 nTmp;
00339         sint32 i, j;
00340 
00341         if (flip == 1)
00342         {
00343                 for (j = 0; j < h; ++j)
00344                 for (i = 0; i < (w/2); ++i)
00345                 {
00346                         nTmp = Tab[i+j*w];
00347                         Tab[i+j*w] = Tab[(w-1-i)+j*w];
00348                         Tab[(w-1-i)+j*w] = nTmp;
00349                 }
00350         }
00351 
00352         if (rot == 1)
00353         {
00354                 vector<uint8> TabDest;
00355                 TabDest.resize (Tab.size());
00356                 for (j = 0; j < h; ++j)
00357                 for (i = 0; i < w;  ++i)
00358                         TabDest[j+i*h] = Tab[i+(h-1-j)*w];
00359                 Tab = TabDest;
00360                 i = w;
00361                 w = h;
00362                 h = i;
00363         }
00364 
00365         if (rot == 2)
00366         {
00367                 for (j = 0; j < (h/2); ++j)
00368                 for (i = 0; i < w; ++i)
00369                 {
00370                         nTmp = Tab[i+j*w];
00371                         Tab[i+j*w] = Tab[(w-1-i)+(h-1-j)*w];
00372                         Tab[(w-1-i)+(h-1-j)*w] = nTmp;
00373                 }
00374                 if ((h/2)*2 != h)
00375                 {
00376                         j = (h/2);
00377                         for (i = 0; i < (w/2); ++i)
00378                         {
00379                                 nTmp = Tab[i+j*w];
00380                                 Tab[i+j*w] = Tab[(w-1-i)+j*w];
00381                                 Tab[(w-1-i)+j*w] = nTmp;
00382                         }
00383                 }
00384         }
00385 
00386         if (rot == 3)
00387         {
00388                 vector<uint8> TabDest;
00389                 TabDest.resize (Tab.size());
00390                 for (j = 0; j < h; ++j)
00391                 for (i = 0; i < w;  ++i)
00392                         TabDest[j+i*h] = Tab[w-1-i+j*w];
00393                 Tab = TabDest;
00394                 i = w;
00395                 w = h;
00396                 h = i;
00397         }
00398 }
00399 
00400 // ***************************************************************************
00401 
00402 std::string     CZoneRegion::getSharingMatNames (sint32 x, sint32 y, uint edge)
00403 {
00404         if ((x < _MinX) || (x > _MaxX) ||
00405                 (y < _MinY) || (y > _MaxY))
00406         {
00407                 return _StringOutOfBound;
00408         }
00409         else
00410         {
00411                 return _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].SharingMatNames[edge];
00412         }
00413 }
00414 
00415 // ***************************************************************************
00416 
00417 uint8 CZoneRegion::getSharingCutEdges (sint32 x, sint32 y, uint edge)
00418 {
00419         if ((x < _MinX) || (x > _MaxX) ||
00420                 (y < _MinY) || (y > _MaxY))
00421         {
00422                 return 0xff;
00423         }
00424         else
00425         {
00426                 return _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].SharingCutEdges[edge];
00427         }
00428 }
00429 
00430 // ***************************************************************************
00431 
00432 bool CZoneRegion::setName (sint32 x, sint32 y, const std::string &newValue)
00433 {
00434         if ((x < _MinX) || (x > _MaxX) ||
00435                 (y < _MinY) || (y > _MaxY))
00436         {
00437                 return false;
00438         }
00439         else
00440         {
00441                 _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].ZoneName = newValue;
00442                 return true;
00443         }
00444 }
00445 
00446 // ***************************************************************************
00447 
00448 bool CZoneRegion::setPosX (sint32 x, sint32 y, uint8 newValue)
00449 {
00450         if ((x < _MinX) || (x > _MaxX) ||
00451                 (y < _MinY) || (y > _MaxY))
00452         {
00453                 return false;
00454         }
00455         else
00456         {
00457                 _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].PosX = newValue;
00458                 return true;
00459         }
00460 }
00461 
00462 // ***************************************************************************
00463 
00464 bool CZoneRegion::setPosY (sint32 x, sint32 y, uint8 newValue)
00465 {
00466         if ((x < _MinX) || (x > _MaxX) ||
00467                 (y < _MinY) || (y > _MaxY))
00468         {
00469                 return false;
00470         }
00471         else
00472         {
00473                 _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].PosY = newValue;
00474                 return true;
00475         }
00476 }
00477 
00478 // ***************************************************************************
00479 
00480 bool CZoneRegion::setRot (sint32 x, sint32 y, uint8 newValue)
00481 {
00482         if ((x < _MinX) || (x > _MaxX) ||
00483                 (y < _MinY) || (y > _MaxY))
00484         {
00485                 return false;
00486         }
00487         else
00488         {
00489                 _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].Rot = newValue;
00490                 return true;
00491         }
00492 }
00493 
00494 // ***************************************************************************
00495 
00496 bool CZoneRegion::setFlip (sint32 x, sint32 y, uint8 newValue)
00497 {
00498         if ((x < _MinX) || (x > _MaxX) ||
00499                 (y < _MinY) || (y > _MaxY))
00500         {
00501                 return false;
00502         }
00503         else
00504         {
00505                 _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].Flip = newValue;
00506                 return true;
00507         }
00508 }
00509 
00510 // ***************************************************************************
00511 
00512 bool CZoneRegion::setSharingMatNames (sint32 x, sint32 y, uint edge, const std::string &newValue)
00513 {
00514         if ((x < _MinX) || (x > _MaxX) ||
00515                 (y < _MinY) || (y > _MaxY))
00516         {
00517                 return false;
00518         }
00519         else
00520         {
00521                 _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].SharingMatNames[edge] = newValue;
00522                 return true;
00523         }
00524 }
00525 
00526 // ***************************************************************************
00527 
00528 bool CZoneRegion::setSharingCutEdges (sint32 x, sint32 y, uint edge, uint8 newValue)
00529 {
00530         if ((x < _MinX) || (x > _MaxX) ||
00531                 (y < _MinY) || (y > _MaxY))
00532         {
00533                 return false;
00534         }
00535         else
00536         {
00537                 _Zones[(x-_MinX)+(y-_MinY)*(1+_MaxX-_MinX)].SharingCutEdges[edge] = newValue;
00538                 return true;
00539         }
00540 }
00541 
00542 // ***************************************************************************
00543 
00544 
00545 } // namespace NLLIGO

Generated on Tue Mar 16 06:42:08 2004 for NeL by doxygen 1.3.6