home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / PRPoly.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  9.1 KB  |  328 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                PRPoly.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef PRPOLY_H
  13. #include "PRPoly.h"
  14. #endif
  15.  
  16. #ifndef FWODEXCE_H
  17. #include "FWODExce.h"
  18. #endif
  19.  
  20. #ifndef FWPOINT_H
  21. #include "FWPoint.h"
  22. #endif
  23.  
  24. #ifndef FWPRIDEB_H
  25. #include "FWPriDeb.h"
  26. #endif
  27.  
  28. #ifndef FWMEMMGR_H
  29. #include "FWMemMgr.h"
  30. #endif
  31.  
  32. #ifndef FWSTRMRW_H
  33. #include "FWStrmRW.h"
  34. #endif
  35.  
  36. #ifndef FWEXCEPT_H
  37. #include "FWExcept.h"
  38. #endif
  39.  
  40. //========================================================================================
  41. //    RunTime Info
  42. //========================================================================================
  43.  
  44. #ifdef FW_BUILD_MAC
  45. #pragma segment FWGraphics_Polygon
  46. #endif
  47.  
  48. FW_DEFINE_AUTO(FW_CPrivPolygonRep)
  49.  
  50. //========================================================================================
  51. //    class FW_CPrivPolygonRep
  52. //========================================================================================
  53.  
  54. //----------------------------------------------------------------------------------------
  55. //    FW_CPrivPolygonRep::FW_CPrivPolygonRep
  56. //----------------------------------------------------------------------------------------
  57.  
  58. FW_CPrivPolygonRep::FW_CPrivPolygonRep(long count, const FW_SPoint* points) :
  59.     fCount(count),
  60.     fPoints(NULL),
  61.     fValidBounds(FALSE)
  62. {
  63.     FW_ASSERT(fCount != 0);
  64.     FW_ASSERT(points != NULL);
  65.     
  66.     long memorySize = count * sizeof(FW_SPoint);    
  67.     fPoints = (FW_SPoint*)FW_CMemoryManager::AllocateBlock(memorySize);
  68.     
  69.     FW_CMemoryManager::CopyMemory(points, fPoints, memorySize);
  70.         
  71.     FW_END_CONSTRUCTOR
  72. }
  73.  
  74. //----------------------------------------------------------------------------------------
  75. //    FW_CPrivPolygonRep::FW_CPrivPolygonRep
  76. //----------------------------------------------------------------------------------------
  77.  
  78. FW_CPrivPolygonRep::FW_CPrivPolygonRep(FW_CReadableStream& stream) :
  79.     fCount(0),
  80.     fPoints(NULL),
  81.     fValidBounds(FALSE)
  82. {
  83.     stream >> fCount;
  84.     
  85.     long memorySize = fCount * sizeof(FW_SPoint);    
  86.     fPoints = (FW_SPoint*) FW_CMemoryManager::AllocateBlock(memorySize);
  87.     
  88.     for (long i = 0; i < fCount; i++)
  89.         stream >> fPoints[i];
  90.         
  91.     FW_END_CONSTRUCTOR
  92. }
  93.  
  94. //----------------------------------------------------------------------------------------
  95. //    FW_CPrivPolygonRep::FW_CPrivPolygonRep
  96. //----------------------------------------------------------------------------------------
  97.  
  98. FW_CPrivPolygonRep::FW_CPrivPolygonRep(const FW_CPrivPolygonRep& poly) :
  99.     fCount(poly.GetCount()),
  100.     fPoints(NULL),
  101.     fValidBounds(FALSE)
  102. {
  103.     long memorySize = fCount * sizeof(FW_SPoint);    
  104.     fPoints = (FW_SPoint*) FW_CMemoryManager::AllocateBlock(memorySize);
  105.     
  106.     FW_CMemoryManager::CopyMemory(poly.GetPoints(), fPoints, memorySize);
  107.         
  108.     FW_END_CONSTRUCTOR
  109. }
  110.  
  111. //----------------------------------------------------------------------------------------
  112. //    FW_CPrivPolygonRep::~FW_CPrivPolygonRep
  113. //----------------------------------------------------------------------------------------
  114.  
  115. FW_CPrivPolygonRep::~FW_CPrivPolygonRep()
  116. {
  117.     FW_START_DESTRUCTOR
  118.     FW_CMemoryManager::FreeBlock(fPoints);    // FreeBlock tests for NULL
  119. }
  120.  
  121. //----------------------------------------------------------------------------------------
  122. //    FW_CPrivPolygonRep::Transform
  123. //----------------------------------------------------------------------------------------
  124.  
  125. void FW_CPrivPolygonRep::Transform(Environment *ev, ODTransform* transform)
  126. {
  127.     FW_TransformPoints(ev, transform, fPoints, fCount);
  128.     fValidBounds = FALSE;
  129. }
  130.  
  131. //----------------------------------------------------------------------------------------
  132. //    FW_CPrivPolygonRep::InverseTransform
  133. //----------------------------------------------------------------------------------------
  134.  
  135. void FW_CPrivPolygonRep::InverseTransform(Environment *ev, ODTransform* transform)
  136. {
  137.     FW_InverseTransformPoints(ev, transform, fPoints, fCount);
  138.     fValidBounds = FALSE;
  139. }
  140.  
  141. //----------------------------------------------------------------------------------------
  142. //    FW_CPrivPolygonRep::Move
  143. //----------------------------------------------------------------------------------------
  144.  
  145. void FW_CPrivPolygonRep::Move(FW_Fixed deltaX, FW_Fixed deltaY)
  146. {
  147.     for(long i = 0; i < fCount; i++)
  148.     {
  149.         fPoints[i].x += deltaX;
  150.         fPoints[i].y += deltaY;
  151.     }
  152.     fValidBounds = FALSE;
  153. }
  154.  
  155. //----------------------------------------------------------------------------------------
  156. //    FW_CPrivPolygonRep::MoveTo
  157. //----------------------------------------------------------------------------------------
  158.  
  159. void FW_CPrivPolygonRep::MoveTo(FW_Fixed x, FW_Fixed y)
  160. {
  161.     FW_SRect bounds;
  162.     GetBounds(bounds);
  163.     
  164.     Move(x - bounds.left, y - bounds.top);
  165.     
  166.     fValidBounds = FALSE;
  167. }
  168.  
  169. //----------------------------------------------------------------------------------------
  170. //    FW_CPrivPolygonRep::Inset
  171. //----------------------------------------------------------------------------------------
  172.  
  173. void FW_CPrivPolygonRep::Inset(FW_Fixed x, FW_Fixed y)
  174. {
  175.     // Find the geometric center of the polygon
  176.     FW_Wide xCenterWide = FW_IntToWide(0);
  177.     FW_Wide yCenterWide = xCenterWide;
  178.     
  179.     long i;
  180.     for(i = 0; i < fCount; i ++)
  181.     {
  182.         xCenterWide += FW_FixedToWide(fPoints[i].x);
  183.         yCenterWide += FW_FixedToWide(fPoints[i].y);
  184.     }
  185.     
  186.     FW_Fixed xCenter = xCenterWide / FW_IntToFixed(fCount);
  187.     FW_Fixed yCenter = yCenterWide / FW_IntToFixed(fCount);
  188.  
  189.     // Now offset all points
  190.     for(i = 0; i < fCount; i ++)
  191.     {
  192.         if(fPoints[i].x < xCenter)
  193.         {
  194.             if((fPoints[i].x += x) > xCenter)
  195.                 fPoints[i].x = xCenter;
  196.         }
  197.         else if(fPoints[i].x > xCenter)
  198.         {
  199.             if((fPoints[i].x -= x) < xCenter)
  200.                 fPoints[i].x = xCenter;
  201.         }
  202.  
  203.         if(fPoints[i].y < yCenter)
  204.         {
  205.             if((fPoints[i].y += y) > yCenter)
  206.                 fPoints[i].y = yCenter;
  207.         }
  208.         else if(fPoints[i].y > yCenter)
  209.         {
  210.             if((fPoints[i].y -= y) < yCenter)
  211.                 fPoints[i].y = yCenter;
  212.         }
  213.     }
  214.     fValidBounds = FALSE;
  215. }
  216.  
  217. //----------------------------------------------------------------------------------------
  218. //    FW_CPrivPolygonRep::GetBounds
  219. //----------------------------------------------------------------------------------------
  220.  
  221. void FW_CPrivPolygonRep::GetBounds(FW_SRect& rect) const
  222. {
  223.     if (fValidBounds)
  224.     {
  225.         rect = fBounds;
  226.     }
  227.     else
  228.     {
  229.         rect.left    =
  230.         rect.right    =    fPoints[0].x;
  231.         rect.top    =
  232.         rect.bottom    =    fPoints[0].y;
  233.     
  234.         for(long i = 1; i < fCount; i ++)
  235.         {
  236.             if(fPoints[i].x < rect.left)
  237.                 rect.left = fPoints[i].x;
  238.             else if(fPoints[i].x > rect.right)
  239.                 rect.right = fPoints[i].x;
  240.     
  241.             if(fPoints[i].y < rect.top)
  242.                 rect.top = fPoints[i].y;
  243.             else if(fPoints[i].y > rect.bottom)
  244.                 rect.bottom = fPoints[i].y;
  245.         }
  246.         
  247.         ((FW_CPrivPolygonRep*)this)->fBounds = rect;
  248.         ((FW_CPrivPolygonRep*)this)->fValidBounds = TRUE;
  249.     }
  250. }
  251.  
  252. //----------------------------------------------------------------------------------------
  253. //    FW_CPrivPolygonRep::SetPoints
  254. //----------------------------------------------------------------------------------------
  255.  
  256. void FW_CPrivPolygonRep::SetPoints(const FW_SPoint* points,
  257.                                long startIndex,
  258.                                long pointCount)
  259. {
  260.     FW_ASSERT(startIndex + pointCount <= fCount);
  261.     FW_CMemoryManager::CopyMemory(points, fPoints + startIndex, pointCount * sizeof(FW_CPoint));
  262.     fValidBounds = FALSE;
  263. }
  264.  
  265. //----------------------------------------------------------------------------------------
  266. //    FW_CPrivPolygonRep::GetPoints
  267. //----------------------------------------------------------------------------------------
  268.  
  269. void FW_CPrivPolygonRep::GetPoints(FW_SPoint* points,
  270.                                long startIndex,
  271.                                long pointCount) const
  272. {
  273.     FW_ASSERT(startIndex + pointCount <= fCount);
  274.     FW_CMemoryManager::CopyMemory(fPoints + startIndex, points, pointCount * sizeof(FW_CPoint));
  275. }
  276.  
  277. //----------------------------------------------------------------------------------------
  278. //    FW_CPrivPolygonRep::GetCount
  279. //----------------------------------------------------------------------------------------
  280.  
  281. long FW_CPrivPolygonRep::GetCount() const
  282. {
  283.     return fCount;
  284. }
  285.  
  286. //----------------------------------------------------------------------------------------
  287. //    FW_CPrivPolygonRep::GetPoints
  288. //----------------------------------------------------------------------------------------
  289.  
  290. FW_SPoint* FW_CPrivPolygonRep::GetPoints() const
  291. {
  292.     ((FW_CPrivPolygonRep*)this)->fValidBounds = FALSE;        // I don't what what they are going to do with those points
  293.     return fPoints;
  294. }
  295.  
  296. //----------------------------------------------------------------------------------------
  297. //    FW_CPrivPolygonRep::Flatten
  298. //----------------------------------------------------------------------------------------
  299.  
  300. void FW_CPrivPolygonRep::Write(FW_CWritableStream& stream) const
  301. {
  302.     stream << fCount;
  303.     for (long i = 0; i< fCount; i++)
  304.         stream << fPoints[i];
  305. }
  306.  
  307. //----------------------------------------------------------------------------------------
  308. //    FW_CPrivPolygonRep::IsEqual
  309. //----------------------------------------------------------------------------------------
  310.  
  311. FW_Boolean FW_CPrivPolygonRep::IsEqual(const FW_CPrivPolygonRep* other) const
  312. {
  313.     if (other == this)
  314.         return TRUE;
  315.         
  316.     if(fCount == other->fCount)
  317.     {
  318.         for (long i = 0; i< fCount; i++)
  319.         {
  320.             if (other->fPoints[i] != fPoints[i])
  321.                 return FALSE;
  322.         }
  323.         return TRUE;
  324.     }
  325.     
  326.     return FALSE;
  327. }
  328.