00001 // $Id: xmatrix.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 */ 00101 00102 #include "camtypes.h" 00103 //#include "xmatrix.h" - in camtypes.h [AUTOMATICALLY REMOVED] 00104 //#include "matrix.h" - in camtypes.h [AUTOMATICALLY REMOVED] 00105 00106 DECLARE_SOURCE("$Revision: 1282 $"); 00107 00108 // (this source was based on the 1.5 revision of matrix.cpp done by Mario) 00109 00110 // this is used to show the fact that the final parameter to XMatrixCalc is in fact modified 00111 // by the call. It is done this way so that 32-bit implementations may replace the final 00112 // paraemter and simply use the return result of the function. 00113 #define RESULT 00114 00115 #define TORADIANS(x) ((x)/180.0*PI) 00116 00117 00118 /******************************************************************************************** 00119 00120 > XMatrix::XMatrix () 00121 00122 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00123 Created: 30/5/93 00124 Inputs: None 00125 Outputs: None 00126 Returns: None. 00127 Purpose: Default initialisation - sets up the identity matrix. 00128 Errors: None. 00129 00130 ********************************************************************************************/ 00131 /* 00132 Technical notes: 00133 00134 ********************************************************************************************/ 00135 00136 XMatrix::XMatrix() 00137 { 00138 a = (FIXED16) 1; 00139 b = (FIXED16) 0; 00140 c = (FIXED16) 0; 00141 d = (FIXED16) 1; 00142 e = 0; 00143 f = 0; 00144 } 00145 00146 /******************************************************************************************** 00147 00148 > XMatrix::XMatrix ( const Matrix& mat) 00149 00150 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00151 Created: 30/6/93 00152 Inputs: Matrix (32-bit flavour) 00153 Outputs: None 00154 Returns: None. 00155 Purpose: Default initialisation - copies elements from 32- to 64-bit matrix 00156 Errors: None. 00157 00158 ********************************************************************************************/ 00159 00160 XMatrix::XMatrix( const Matrix& mat32) 00161 { 00162 a = mat32.a; 00163 b = mat32.b; 00164 c = mat32.c; 00165 d = mat32.d; 00166 e = mat32.e; 00167 f = mat32.f; 00168 } 00169 00170 00171 /******************************************************************************************** 00172 00173 > XMatrix::XMatrix (const FIXED16& xScale, const FIXED16& yScale) 00174 00175 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00176 Created: 30/5/93 00177 Inputs: Two fixed16s representing the x and y scale factors. 00178 Outputs: None 00179 Returns: None. 00180 Purpose: Default initialisation - sets up the scale matrix. 00181 Errors: None. 00182 00183 ********************************************************************************************/ 00184 /* 00185 Technical notes: 00186 00187 ********************************************************************************************/ 00188 00189 XMatrix::XMatrix(const FIXED16& xScale, const FIXED16& yScale) 00190 { 00191 a = xScale; 00192 b = (FIXED16) 0; 00193 c = (FIXED16) 0; 00194 d = yScale; 00195 e = 0; 00196 f = 0; 00197 } 00198 00199 /******************************************************************************************** 00200 00201 > XMatrix::XMatrix (const ANGLE& theta) 00202 00203 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00204 Created: 30/5/93 00205 Inputs: An angle representing the degree of rotation. 00206 Outputs: None 00207 Returns: None. 00208 Purpose: Default initialisation - sets up the rotation matrix. 00209 Errors: None. 00210 00211 ********************************************************************************************/ 00212 /* 00213 Technical notes: 00214 00215 ********************************************************************************************/ 00216 00217 XMatrix::XMatrix(const ANGLE& theta) 00218 { 00219 #if 0 00220 a = b = c = d = theta; 00221 00222 a.Cos(); 00223 b.Sin(); 00224 c = -(c.Sin()); 00225 d.Cos(); 00226 #else 00227 double thetaradians = TORADIANS(theta.MakeDouble()); 00228 FIXED16 costheta = cos(thetaradians); 00229 FIXED16 sintheta = sin(thetaradians); 00230 00231 a = d = costheta; 00232 b = sintheta; 00233 c = -sintheta; 00234 #endif 00235 00236 e = 0; 00237 f = 0; 00238 } 00239 00240 /******************************************************************************************** 00241 00242 > XMatrix::XMatrix (const WorkCoord& disp) 00243 00244 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00245 Created: 30/5/93 00246 Inputs: Displacement of translation. 00247 Outputs: None 00248 Returns: None. 00249 Purpose: Default initialisation - Sets up a translate matrix. 00250 Errors: None. 00251 00252 ********************************************************************************************/ 00253 /* 00254 Technical notes: 00255 00256 ********************************************************************************************/ 00257 00258 XMatrix::XMatrix(const WorkCoord& disp) 00259 { 00260 a = (FIXED16) 1; 00261 b = (FIXED16) 0; 00262 c = (FIXED16) 0; 00263 d = (FIXED16) 1; 00264 e = disp.x; 00265 f = disp.y; 00266 } 00267 00268 /******************************************************************************************** 00269 00270 > XMatrix::XMatrix (const XLONG x, const XLONG y) 00271 00272 > XMatrix::XMatrix (const INT32 x, const INT32 y) 00273 00274 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00275 Created: 30/5/93 00276 Inputs: Displacement of translation. 00277 Outputs: None 00278 Returns: None. 00279 Purpose: Default initialisation - Sets up a translate matrix. 00280 Errors: None. 00281 00282 ********************************************************************************************/ 00283 00284 XMatrix::XMatrix(const XLONG x, const XLONG y) 00285 { 00286 a = (FIXED16) 1; 00287 b = (FIXED16) 0; 00288 c = (FIXED16) 0; 00289 d = (FIXED16) 1; 00290 e = x; 00291 f = y; 00292 } 00293 00294 00295 XMatrix::XMatrix(const INT32 x, const INT32 y) 00296 { 00297 a = (FIXED16) 1; 00298 b = (FIXED16) 0; 00299 c = (FIXED16) 0; 00300 d = (FIXED16) 1; 00301 e = x; 00302 f = y; 00303 } 00304 00305 /******************************************************************************************** 00306 00307 > XMatrix::XMatrix (const FIXED16& ca, 00308 const FIXED16& cb, 00309 const FIXED16& cc, 00310 const FIXED16& cd, 00311 const XLONG ce, 00312 const XLONG cf 00313 ) 00314 00315 Author: Phil_Martin (Xara Group Ltd) <camelotdev@xara.com> 00316 Created: 21/7/93 00317 Inputs: All six parameters which make up an Xmatrix 00318 Outputs: None 00319 Returns: None. 00320 Purpose: Initialise a whole XMatrix. 00321 Errors: None. 00322 00323 ********************************************************************************************/ 00324 00325 XMatrix::XMatrix( const FIXED16& ca, 00326 const FIXED16& cb, 00327 const FIXED16& cc, 00328 const FIXED16& cd, 00329 const XLONG ce, 00330 const XLONG cf 00331 ) 00332 { 00333 a = ca; 00334 b = cb; 00335 c = cc; 00336 d = cd; 00337 e = ce; 00338 f = cf; 00339 } 00340 00341 /******************************************************************************************** 00342 00343 > void XMatrix::transform(WorkCoord pts[], UINT32 count) 00344 00345 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00346 Created: 30/5/93 00347 Inputs: result - points to be translated. 00348 count - number of points. 00349 Outputs: None 00350 Returns: None. 00351 Purpose: Transforms a list of points. 00352 Errors: None. 00353 00354 ********************************************************************************************/ 00355 /* 00356 Technical notes: 00357 00358 ********************************************************************************************/ 00359 00360 void XMatrix::transform(WorkCoord pts[], UINT32 count ) 00361 { 00362 UINT32 i; 00363 XLONG tx; // Holds INPUT value of pts[i].x for use in second MatrixCalc 00364 00365 for (i = 0; i < count; i++) 00366 { 00367 tx = pts[i].x; 00368 XMatrixCalc(a, tx, c, pts[i].y, RESULT pts[i].x); 00369 pts[i].x += e; 00370 XMatrixCalc(b, tx, d, pts[i].y, RESULT pts[i].y); 00371 pts[i].y += f; 00372 } 00373 } 00374 00375 /******************************************************************************************** 00376 00377 > void XMatrix::transform(WorkCoord pts[], const WorkCoord input[], UINT32 count) 00378 00379 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00380 Created: 30/5/93 00381 Inputs: input - points to be translated. 00382 count - number of points. 00383 Outputs: result points translated. 00384 Returns: None. 00385 Purpose: Transforms a list of points. 00386 Errors: None. 00387 00388 ********************************************************************************************/ 00389 /* 00390 Technical notes: 00391 00392 ********************************************************************************************/ 00393 00394 void XMatrix::transform(WorkCoord pts[], const WorkCoord input[], UINT32 count) 00395 { 00396 UINT32 i; 00397 00398 for (i = 0; i < count; i++) 00399 { 00400 XMatrixCalc(a, input[i].x, c, input[i].y, RESULT pts[i].x); 00401 pts[i].x += e; 00402 XMatrixCalc(b, input[i].x, d, input[i].y, RESULT pts[i].y); 00403 pts[i].y += f; 00404 } 00405 } 00406 00407 /******************************************************************************************** 00408 00409 > XMatrix& XMatrix::operator=(const Matrix& rhs) 00410 00411 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00412 Created: 30/5/93 00413 Inputs: rhs - matrix to be assigned. 00414 Outputs: None. 00415 Returns: None. 00416 Purpose: Matrix assignment. 00417 Errors: None. 00418 00419 ********************************************************************************************/ 00420 /* 00421 Technical notes: 00422 00423 ********************************************************************************************/ 00424 00425 XMatrix& XMatrix::operator=(const XMatrix& rhs) 00426 { 00427 this->a = rhs.a; 00428 this->b = rhs.b; 00429 this->c = rhs.c; 00430 this->d = rhs.d; 00431 this->e = rhs.e; 00432 this->f = rhs.f; 00433 00434 return *this; 00435 } 00436 00437 /******************************************************************************************** 00438 00439 > XMatrix XMatrix::operator*(const XMatrix& operand1, const XMatrix& operand2) 00440 00441 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00442 Created: 30/5/93 00443 Inputs: Two matrices to be multiplied. 00444 Outputs: None. 00445 Friend: Matrix 00446 Returns: result of multiplication. 00447 Purpose: Matrix multiplication. Cannot be used in a nested form as the result is 00448 contained in a static local (e.g. a = (b*c)*(d*e) will fail). 00449 Errors: None. 00450 00451 ********************************************************************************************/ 00452 00453 00454 XMatrix operator*(const XMatrix& op1, const XMatrix& op2) 00455 { 00456 static XMatrix result; 00457 00458 result.a = ((op1.a * op2.a) + (op1.b * op2.c)); 00459 result.b = ((op1.a * op2.b) + (op1.b * op2.d)); 00460 result.c = ((op1.c * op2.a) + (op1.d * op2.c)); 00461 result.d = ((op1.c * op2.b) + (op1.d * op2.d)); 00462 00463 XMatrixCalc(op2.a, op1.e, op2.c, op1.f, RESULT result.e); 00464 result.e += op2.e; 00465 XMatrixCalc(op2.b, op1.e, op2.d, op1.f, RESULT result.f); 00466 result.f += op2.f; 00467 00468 return result; 00469 } 00470 00471 /******************************************************************************************** 00472 00473 > XMatrix& XMatrix::operator*=(const XMatrix& op) 00474 00475 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00476 Created: 30/5/93 00477 Inputs: Matrix to be multiplied. 00478 Outputs: None. 00479 Friend: Matrix 00480 Returns: result of multiplication. 00481 Purpose: Overloads the *= operator for Matrices. 00482 Errors: None. 00483 00484 ********************************************************************************************/ 00485 00486 00487 XMatrix& XMatrix::operator*=(const XMatrix& op) 00488 { 00489 XMatrix temp; 00490 00491 temp.a = ((a * op.a) + (b * op.c)); 00492 temp.b = ((a * op.b) + (b * op.d)); 00493 temp.c = ((c * op.a) + (d * op.c)); 00494 temp.d = ((c * op.b) + (d * op.d)); 00495 XMatrixCalc(op.a, e, op.c, f, RESULT temp.e); 00496 temp.e += op.e; 00497 XMatrixCalc(op.b, e, op.d, f, RESULT temp.f); 00498 temp.f += op.f; 00499 00500 *this = temp; 00501 00502 return *this; 00503 } 00504 00505 00506 00507 /******************************************************************************************** 00508 00509 > void XMatrix::transform(WorkCoord* pt) 00510 00511 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00512 Created: 30/5/93 00513 Inputs: result - points to be transformed. 00514 Outputs: None 00515 Returns: None. 00516 Purpose: Transforms a single point. 00517 Errors: None. 00518 00519 ********************************************************************************************/ 00520 00521 void XMatrix::transform(WorkCoord* pt) 00522 { 00523 XLONG tx = pt->x; // Holds INPUT value of x for use in second MatrixCalc 00524 00525 XMatrixCalc(a, tx, c, pt->y, RESULT pt->x); 00526 pt->x += e; 00527 XMatrixCalc(b, tx, d, pt->y, RESULT pt->y); 00528 pt->y += f; 00529 }