userrect.cpp

Go to the documentation of this file.
00001 // $Id: userrect.cpp 1282 2006-06-09 09:46:49Z alex $
00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE
00003 ================================XARAHEADERSTART===========================
00004  
00005                Xara LX, a vector drawing and manipulation program.
00006                     Copyright (C) 1993-2006 Xara Group Ltd.
00007        Copyright on certain contributions may be held in joint with their
00008               respective authors. See AUTHORS file for details.
00009 
00010 LICENSE TO USE AND MODIFY SOFTWARE
00011 ----------------------------------
00012 
00013 This file is part of Xara LX.
00014 
00015 Xara LX is free software; you can redistribute it and/or modify it
00016 under the terms of the GNU General Public License version 2 as published
00017 by the Free Software Foundation.
00018 
00019 Xara LX and its component source files are distributed in the hope
00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the
00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00022 See the GNU General Public License for more details.
00023 
00024 You should have received a copy of the GNU General Public License along
00025 with Xara LX (see the file GPL in the root directory of the
00026 distribution); if not, write to the Free Software Foundation, Inc., 51
00027 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00028 
00029 
00030 ADDITIONAL RIGHTS
00031 -----------------
00032 
00033 Conditional upon your continuing compliance with the GNU General Public
00034 License described above, Xara Group Ltd grants to you certain additional
00035 rights. 
00036 
00037 The additional rights are to use, modify, and distribute the software
00038 together with the wxWidgets library, the wxXtra library, and the "CDraw"
00039 library and any other such library that any version of Xara LX relased
00040 by Xara Group Ltd requires in order to compile and execute, including
00041 the static linking of that library to XaraLX. In the case of the
00042 "CDraw" library, you may satisfy obligation under the GNU General Public
00043 License to provide source code by providing a binary copy of the library
00044 concerned and a copy of the license accompanying it.
00045 
00046 Nothing in this section restricts any of the rights you have under
00047 the GNU General Public License.
00048 
00049 
00050 SCOPE OF LICENSE
00051 ----------------
00052 
00053 This license applies to this program (XaraLX) and its constituent source
00054 files only, and does not necessarily apply to other Xara products which may
00055 in part share the same code base, and are subject to their own licensing
00056 terms.
00057 
00058 This license does not apply to files in the wxXtra directory, which
00059 are built into a separate library, and are subject to the wxWindows
00060 license contained within that directory in the file "WXXTRA-LICENSE".
00061 
00062 This license does not apply to the binary libraries (if any) within
00063 the "libs" directory, which are subject to a separate license contained
00064 within that directory in the file "LIBS-LICENSE".
00065 
00066 
00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS
00068 ----------------------------------------------
00069 
00070 Subject to the terms of the GNU Public License (see above), you are
00071 free to do whatever you like with your modifications. However, you may
00072 (at your option) wish contribute them to Xara's source tree. You can
00073 find details of how to do this at:
00074   http://www.xaraxtreme.org/developers/
00075 
00076 Prior to contributing your modifications, you will need to complete our
00077 contributor agreement. This can be found at:
00078   http://www.xaraxtreme.org/developers/contribute/
00079 
00080 Please note that Xara will not accept modifications which modify any of
00081 the text between the start and end of this header (marked
00082 XARAHEADERSTART and XARAHEADEREND).
00083 
00084 
00085 MARKS
00086 -----
00087 
00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara
00089 designs are registered or unregistered trademarks, design-marks, and/or
00090 service marks of Xara Group Ltd. All rights in these marks are reserved.
00091 
00092 
00093       Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK.
00094                         http://www.xara.com/
00095 
00096 =================================XARAHEADEREND============================
00097  */
00098 
00099 
00100 #include "camtypes.h"
00101 #include "userrect.h"
00102 //#include "ensure.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00103 
00104 DECLARE_SOURCE("$Revision: 1282 $");
00105 
00106 /*
00107   $Log: rect.ctt $
00108 Revision 1.20  1995/07/07  11:35:00  Jason
00109 Fixed ABS macro definitions to be safe
00110 
00111 Revision 1.19  1994/06/28  16:57:47  Rik
00112 Added some code that shows how the hell the IsAdjacent function works
00113 
00114 Revision 1.18  1994/03/03  11:38:12  Tim
00115 Added a MakeEmpty function.
00116 
00117 Revision 1.17  1994/01/20  17:09:23  Simon
00118 Default constructor now creates an empty recatngle
00119 
00120 Revision 1.16  1994/01/12  17:30:17  Rik
00121 Improved region merging and region joining
00122 
00123 Revision 1.15  1993/11/05  14:56:03  Rik
00124 Blobs work, fixed DocRect probs, selecting blobs work
00125 
00126 Revision 1.14  1993/08/12  11:17:48  Will
00127 Global CheckIn for new UserCoord system.
00128 
00129 Revision 1.13  1993/07/29  11:10:02  Will
00130 Fixed problem with fuzzy adjacent test.
00131 
00132 Revision 1.12  1993/07/21  21:08:29  Will
00133 Split rect function was splitting the wrong rect.
00134 
00135 Revision 1.11  1993/07/19  17:02:42  Will
00136 Added some comments.
00137 Added Fuzzy limit passed during IsAdjacent().
00138 
00139 Revision 1.10  1993/07/15  17:04:10  Will
00140 Added SplitRect function for BG rendering.
00141 
00142 Revision 1.9  1993/07/14  17:34:25  Will
00143 Added IsAdjacent function for region merging.
00144 
00145 Revision 1.8  1993/07/12  12:58:11  Tim
00146 Added a ContainsRect function.
00147 
00148 Revision 1.7  1993/06/18  11:06:28  Tim
00149 Made rectangle classes usable by tools by using the CAMAPI macro in the
00150 class declaration.
00151 
00152 Revision 1.6  1993/06/15  16:49:28  Tim
00153 Altered Intersection and Union to return objects rather than object refs.
00154 
00155 Revision 1.5  1993/06/10  10:29:35  Tim
00156 Improved links for ADC generated help file.
00157 
00158 Revision 1.4  1993/06/09  16:40:02  Tim
00159 Moved class comment from .cpp file to .h file to conform to style guide.
00160 
00161 Revision 1.3  1993/06/09  13:52:40  Tim
00162 Revised use and meaning of empty/invalid rectangles.
00163 Expanded comment headers to explain this.
00164 
00165 Revision 1.2  1993/06/08  14:12:06  Tim
00166 Added comments about non-re-entrancy of Union and Intersection.
00167 
00168 */
00169 
00170 
00171 /**********************************************************************************************
00172 
00173 >    BOOL UserRect::IsAdjacent() const
00174 
00175      Author:    Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00176      Created:   14/7/93
00177      Inputs:    The Fuzzy limit in MILLIPOINTS.  
00178      Outputs:   - 
00179      
00180      Returns:   TRUE if the rectangles are adjacent within a Fuzzy limit.
00181                 
00182      Purpose:   To check for adjacent rectangles.
00183 
00184      Errors:    
00185 
00186 **********************************************************************************************/  
00187 
00188 BOOL UserRect::IsAdjacent(const UserRect& Rect, MILLIPOINT Fuzzy) const
00189 {
00190     // Check for an an empty rectangle
00191     if (IsEmpty())
00192         return FALSE;
00193 
00194     // Detect an invalid rectangle
00195     ENSURE(IsValid(), "UserRect::IsAdjacent() was called on\nan invalid rectangle.");  
00196 
00197 #if 0
00198     // This is the expansion of the rampant return statement below
00199 
00200     // Near top or bottom
00201     BOOL NearBot = (ABS(Rect.hi.y - lo.y)) <= Fuzzy;
00202     BOOL NearTop = (ABS(Rect.lo.y - hi.y)) <= Fuzzy;
00203 
00204     // Near left or right
00205     BOOL NearLeft  = (ABS(Rect.hi.x - lo.x)) <= Fuzzy;
00206     BOOL NearRight = (ABS(Rect.lo.x - hi.x)) <= Fuzzy;
00207 
00208     // Overlaps
00209     BOOL OverLeft  = (ABS(Rect.lo.x - lo.x)) <= Fuzzy;
00210     BOOL OverRight = (ABS(Rect.hi.x - hi.x)) <= Fuzzy;
00211     BOOL OverTop   = (ABS(Rect.hi.y - hi.y)) <= Fuzzy;
00212     BOOL OverBot   = (ABS(Rect.lo.y - lo.y)) <= Fuzzy;
00213 
00214     // Adjacent to the top or bottom?
00215     BOOL TopOrBot    = (NearTop || NearBot) && OverLeft && OverRight;
00216     BOOL LeftOrRight = (NearLeft || NearRight) && OverTop && OverBot;
00217 
00218     return (TopOrBot || LeftOrRight);
00219 #endif
00220 
00221     // This is explained more carefully in the section above that is excluded from the build
00222     return (( ((ABS(Rect.lo.x - lo.x))<=Fuzzy) && ((ABS(Rect.hi.x - hi.x))<=Fuzzy) &&
00223              (((ABS(Rect.hi.y - lo.y))<=Fuzzy) || ((ABS(Rect.lo.y - hi.y))<=Fuzzy)) ) ||
00224             ( ((ABS(Rect.lo.y - lo.y))<=Fuzzy) && ((ABS(Rect.hi.y - hi.y))<=Fuzzy) &&
00225              (((ABS(Rect.hi.x - lo.x))<=Fuzzy) || ((ABS(Rect.lo.x - hi.x))<=Fuzzy)) ));
00226 }
00227 
00228 
00229 
00230 /*********************************************************************************************
00231 
00232 >    UserRect UserRect::Union(const UserRect& R)
00233 
00234      Author:    Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
00235      Created:   17/5/93
00236      Inputs:    R - the rectangle to join with the object.
00237      Outputs:   - 
00238      
00239      Returns:   The simple union (bounding box).
00240                 
00241      Purpose:   Compute union of two rectangles.
00242                 
00243                 If one of the rectangles involved is invalid, the other is returned as the
00244                 result.  In the case of both being invalid, one of the invalid rectangles
00245                 is returned as the result.  It is undefined which rectangle is returned in
00246                 this case.
00247 
00248      Errors:    None.
00249 
00250 **********************************************************************************************/  
00251 
00252 UserRect UserRect::Union(const UserRect& R) const
00253 {
00254     // Special cases for invalid rectangles...
00255     if (!IsValid()  ||  IsEmpty())
00256         return R;
00257     if (!R.IsValid()  ||  R.IsEmpty())
00258         return *this;
00259         
00260     // Return the result
00261     UserRect U;
00262     U.lo.x = min(lo.x, R.lo.x);
00263     U.lo.y = min(lo.y, R.lo.y);
00264     U.hi.x = max(hi.x, R.hi.x);
00265     U.hi.y = max(hi.y, R.hi.y);
00266     return U;
00267 }
00268 
00269 
00270 
00271 /*********************************************************************************************
00272 
00273 >    UserRect UserRect::Intersection(const UserRect& R)
00274 
00275      Author:    Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
00276      Created:   17/5/93
00277      Inputs:    R - the rectangle to intersect with.
00278      Outputs:   - 
00279      
00280      Returns:   The intersection.
00281                 
00282      Purpose:   Compute intersection of two rectangles.
00283 
00284                 If one of the rectangles involved is invalid, the other is returned as the
00285                 result.  In the case of both being invalid, one of the invalid rectangles
00286                 is returned as the result.  It is undefined which rectangle is returned in
00287                 this case.
00288 
00289      Errors:    None.
00290 
00291 **********************************************************************************************/  
00292 
00293 UserRect UserRect::Intersection(const UserRect& R) const
00294 {
00295     // Special cases for invalid rectangles...
00296     if (!IsValid())
00297         return R;
00298     if (!R.IsValid())
00299         return *this;
00300     
00301     // Special cases when empty rectangles
00302     if (IsEmpty())
00303         return *this;
00304     if (R.IsEmpty())
00305         return R;
00306 
00307     // Return the result
00308     UserRect I;
00309     I.lo.x = max(lo.x, R.lo.x);
00310     I.lo.y = max(lo.y, R.lo.y);
00311     I.hi.x = min(hi.x, R.hi.x);
00312     I.hi.y = min(hi.y, R.hi.y);
00313     return I;
00314 }
00315 
00316 
00317 
00318 /*********************************************************************************************
00319 
00320 >    INT32 UserRect::SplitRect(const UserRect& R, UserRect* SubRects)
00321 
00322      Author:    Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00323      Created:   14/7/93
00324      Inputs:    R is the rectangle to intersect with, SubRects is an array of UserRect to fill.  
00325      Outputs:   Puts any sub-rects into SubRects. 
00326      
00327      Returns:   The number of subrectangles found. 0 for no intersection.
00328                 
00329      Purpose:   Splits a rectangle into sub-rectangles which all exclude the interecting
00330                 rectangle R.  The number of sub-rects generated depends on how the rectangles
00331                 intersect.
00332                      _______________________         ___________________
00333                     |                       |       |                   |
00334                     | *this    A            |       | *this    A        |
00335                     |_ _ _  ________  _ _ _ |       |_ _ _ _ _  ________|__________
00336                     |      |        |       |       |          |        |          |
00337                     |  B   |   R    |   C   |       |    B     |        |          |
00338                     |_ _ _ |________| _ _ _ |       |__________|________| R        |
00339                     |                       |                  |                   |
00340                     |          D    Index=4 |                  |           Index=2 |
00341                     |_______________________|                  |___________________|
00342      Errors:    
00343 
00344 **********************************************************************************************/  
00345 
00346 INT32 UserRect::SplitRect(const UserRect& R, UserRect* SubRects)
00347 {
00348     if (!this->IsIntersectedWith(R))
00349         return 0;   // No intersection  
00350 
00351     INT32 Index = 0;
00352 
00353     if (R.hi.y < hi.y)
00354         SubRects[Index++] = UserRect(lo.x, R.hi.y, hi.x, hi.y); // Sub-Rect A
00355         
00356     if (R.lo.y > lo.y)
00357         SubRects[Index++] = UserRect(lo.x, lo.y, hi.x, R.lo.y); // Sub-Rect D
00358 
00359     INT32 Ry0 = max(R.lo.y, lo.y);
00360     INT32 Ry1 = min(R.hi.y, hi.y);
00361     
00362     if (R.lo.x > lo.x)
00363         SubRects[Index++] = UserRect(lo.x, Ry0, R.lo.x, Ry1);           // Sub-Rect B       
00364 
00365     if (R.hi.x < hi.x)
00366         SubRects[Index++] = UserRect(R.hi.x, Ry0, hi.x, Ry1);           // Sub-Rect C       
00367 
00368     return Index;
00369 }
00370 
00371 
00372 
00373 /***********************************************************************************************
00374 
00375 >   void UserRect::IncludePoint(const UserCoord& Point)
00376 
00377     Author:     Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
00378     Created:    27/5/93
00379     Inputs:     Point - coordinate to include into this rectangle.
00380     Outputs:    -
00381     Returns:    -
00382     Purpose:    Expand a rectangle so that it includes the given point.
00383     Errors:     An assertion failure if the rectangle is invalid.
00384     SeeAlso:    -
00385 
00386 ***********************************************************************************************/
00387 
00388 void UserRect::IncludePoint(const UserCoord& Point)
00389 {
00390     // Detect an invalid rectangle
00391     ENSURE(IsValid(), "UserRect::IncludePoint() was called on an \ninvalid rectangle.");  
00392 
00393     // Extend lower corner to include point if necessary
00394     lo.x = min(lo.x, Point.x);
00395     lo.y = min(lo.y, Point.y);
00396     
00397     // Extend upper corner to include point if necessary
00398     // (remember upper corner is exclusive)
00399     hi.x = max(hi.x, (INT32)(Point.x + 1L));
00400     hi.y = max(hi.y, (INT32)(Point.y + 1L));
00401 }
00402 
00403 // extra functions specific to the DocRect class
00404 
00405 /*
00406 $Log: userrect.def $
00407 Revision 1.1  1995/09/12  17:11:49  Ed
00408 Initial revision
00409 
00410 */
00411 
00412 DECLARE_SOURCE("$Revision: 1282 $");
00413 
00414 //#include "docrect.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00415 
00416 /********************************************************************************************
00417 >   DocRect UserRect::ToSpread(Spread* pSpread)
00418 
00419     Author:     Ed_Cornes (Xara Group Ltd) <camelotdev@xara.com>
00420     Created:    6/9/95
00421     Inputs:     pSpread - 
00422     Returns:    SpreadRect
00423     Purpose:    Convert a UserRect to a SpreadRect (AKA DocRect)
00424 ********************************************************************************************/
00425 
00426 DocRect UserRect::ToSpread(Spread* pSpread)
00427 {
00428     return DocRect( this->lo.ToSpread(pSpread), this->hi.ToSpread(pSpread) );
00429 }
00430 
00431 

Generated on Sat Nov 10 03:47:18 2007 for Camelot by  doxygen 1.4.4