1*ce9c7ef7SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ce9c7ef7SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ce9c7ef7SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ce9c7ef7SAndrew Rist  * distributed with this work for additional information
6*ce9c7ef7SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ce9c7ef7SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ce9c7ef7SAndrew Rist  * "License"); you may not use this file except in compliance
9*ce9c7ef7SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ce9c7ef7SAndrew Rist  *
11*ce9c7ef7SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ce9c7ef7SAndrew Rist  *
13*ce9c7ef7SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ce9c7ef7SAndrew Rist  * software distributed under the License is distributed on an
15*ce9c7ef7SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ce9c7ef7SAndrew Rist  * KIND, either express or implied.  See the License for the
17*ce9c7ef7SAndrew Rist  * specific language governing permissions and limitations
18*ce9c7ef7SAndrew Rist  * under the License.
19*ce9c7ef7SAndrew Rist  *
20*ce9c7ef7SAndrew Rist  *************************************************************/
21*ce9c7ef7SAndrew Rist 
22*ce9c7ef7SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _HOMMATRIX_TEMPLATE_HXX
25cdf0e10cSrcweir #define _HOMMATRIX_TEMPLATE_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <sal/types.h>
28cdf0e10cSrcweir #include <basegfx/numeric/ftools.hxx>
29cdf0e10cSrcweir #include <math.h>
30cdf0e10cSrcweir #include <string.h>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir namespace basegfx
33cdf0e10cSrcweir {
34cdf0e10cSrcweir     namespace internal
35cdf0e10cSrcweir     {
36cdf0e10cSrcweir 
implGetDefaultValue(sal_uInt16 nRow,sal_uInt16 nColumn)37cdf0e10cSrcweir         inline double implGetDefaultValue(sal_uInt16 nRow, sal_uInt16 nColumn)
38cdf0e10cSrcweir         {
39cdf0e10cSrcweir             if(nRow == nColumn)
40cdf0e10cSrcweir                 return 1.0;
41cdf0e10cSrcweir             return 0.0;
42cdf0e10cSrcweir         }
43cdf0e10cSrcweir 
44cdf0e10cSrcweir         template < unsigned int _RowSize > class ImplMatLine
45cdf0e10cSrcweir         {
46cdf0e10cSrcweir             enum { RowSize = _RowSize };
47cdf0e10cSrcweir 
48cdf0e10cSrcweir             double											mfValue[RowSize];
49cdf0e10cSrcweir 
50cdf0e10cSrcweir         public:
ImplMatLine()51cdf0e10cSrcweir             ImplMatLine()
52cdf0e10cSrcweir             {
53cdf0e10cSrcweir             }
54cdf0e10cSrcweir 
ImplMatLine(sal_uInt16 nRow,ImplMatLine<RowSize> * pToBeCopied=0L)55cdf0e10cSrcweir             ImplMatLine(sal_uInt16 nRow, ImplMatLine< RowSize >* pToBeCopied = 0L)
56cdf0e10cSrcweir             {
57cdf0e10cSrcweir                 if(pToBeCopied)
58cdf0e10cSrcweir                 {
59cdf0e10cSrcweir                     memcpy(&mfValue, pToBeCopied, sizeof(double) * RowSize);
60cdf0e10cSrcweir                 }
61cdf0e10cSrcweir                 else
62cdf0e10cSrcweir                 {
63cdf0e10cSrcweir                     for(sal_uInt16 a(0); a < RowSize; a++)
64cdf0e10cSrcweir                     {
65cdf0e10cSrcweir                         mfValue[a] = implGetDefaultValue(nRow, a);
66cdf0e10cSrcweir                     }
67cdf0e10cSrcweir                 }
68cdf0e10cSrcweir             }
69cdf0e10cSrcweir 
get(sal_uInt16 nColumn) const70cdf0e10cSrcweir             double get(sal_uInt16 nColumn) const
71cdf0e10cSrcweir             {
72cdf0e10cSrcweir                 return mfValue[nColumn];
73cdf0e10cSrcweir             }
74cdf0e10cSrcweir 
set(sal_uInt16 nColumn,const double & rValue)75cdf0e10cSrcweir             void set(sal_uInt16 nColumn, const double& rValue)
76cdf0e10cSrcweir             {
77cdf0e10cSrcweir                 mfValue[nColumn] = rValue;
78cdf0e10cSrcweir             }
79cdf0e10cSrcweir         };
80cdf0e10cSrcweir 
81cdf0e10cSrcweir         template < unsigned int _RowSize > class ImplHomMatrixTemplate
82cdf0e10cSrcweir         {
83cdf0e10cSrcweir             enum { RowSize = _RowSize };
84cdf0e10cSrcweir 
85cdf0e10cSrcweir             ImplMatLine< RowSize >							maLine[RowSize - 1];
86cdf0e10cSrcweir             ImplMatLine< RowSize >*							mpLine;
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         public:
89cdf0e10cSrcweir             // Is last line used?
isLastLineDefault() const90cdf0e10cSrcweir             bool isLastLineDefault() const
91cdf0e10cSrcweir             {
92cdf0e10cSrcweir                 if(!mpLine)
93cdf0e10cSrcweir                     return true;
94cdf0e10cSrcweir 
95cdf0e10cSrcweir                 for(sal_uInt16 a(0); a < RowSize; a++)
96cdf0e10cSrcweir                 {
97cdf0e10cSrcweir                     const double fDefault(implGetDefaultValue((RowSize - 1), a));
98cdf0e10cSrcweir                     const double fLineValue(mpLine->get(a));
99cdf0e10cSrcweir 
100cdf0e10cSrcweir                     if(!::basegfx::fTools::equal(fDefault, fLineValue))
101cdf0e10cSrcweir                     {
102cdf0e10cSrcweir                         return false;
103cdf0e10cSrcweir                     }
104cdf0e10cSrcweir                 }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir                 // reset last line, it equals default
107cdf0e10cSrcweir                 delete ((ImplHomMatrixTemplate< RowSize >*)this)->mpLine;
108cdf0e10cSrcweir                 ((ImplHomMatrixTemplate< RowSize >*)this)->mpLine = 0L;
109cdf0e10cSrcweir 
110cdf0e10cSrcweir                 return true;
111cdf0e10cSrcweir             }
112cdf0e10cSrcweir 
ImplHomMatrixTemplate()113cdf0e10cSrcweir             ImplHomMatrixTemplate()
114cdf0e10cSrcweir                 :	mpLine(0L)
115cdf0e10cSrcweir             {
116cdf0e10cSrcweir                 // complete initialization with identity matrix, all lines
117cdf0e10cSrcweir                 // were initialized with a trailing 1 followed by 0's.
118cdf0e10cSrcweir                 for(sal_uInt16 a(0); a < RowSize-1; a++)
119cdf0e10cSrcweir                 {
120cdf0e10cSrcweir                     for(sal_uInt16 b(0); b < RowSize; b++)
121cdf0e10cSrcweir                         maLine[a].set(b, implGetDefaultValue(a, b) );
122cdf0e10cSrcweir                 }
123cdf0e10cSrcweir             }
124cdf0e10cSrcweir 
ImplHomMatrixTemplate(const ImplHomMatrixTemplate & rToBeCopied)125cdf0e10cSrcweir             ImplHomMatrixTemplate(const ImplHomMatrixTemplate& rToBeCopied)
126cdf0e10cSrcweir                 :	mpLine(0L)
127cdf0e10cSrcweir             {
128cdf0e10cSrcweir                 // complete initialization using copy
129cdf0e10cSrcweir                 for(sal_uInt16 a(0); a < (RowSize - 1); a++)
130cdf0e10cSrcweir                 {
131cdf0e10cSrcweir                     memcpy(&maLine[a], &rToBeCopied.maLine[a], sizeof(ImplMatLine< RowSize >));
132cdf0e10cSrcweir                 }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir                 if(rToBeCopied.mpLine)
135cdf0e10cSrcweir                 {
136cdf0e10cSrcweir                     mpLine = new ImplMatLine< RowSize >((RowSize - 1), rToBeCopied.mpLine);
137cdf0e10cSrcweir                 }
138cdf0e10cSrcweir             }
139cdf0e10cSrcweir 
~ImplHomMatrixTemplate()140cdf0e10cSrcweir             ~ImplHomMatrixTemplate()
141cdf0e10cSrcweir             {
142cdf0e10cSrcweir                 if(mpLine)
143cdf0e10cSrcweir                 {
144cdf0e10cSrcweir                     delete mpLine;
145cdf0e10cSrcweir                 }
146cdf0e10cSrcweir             }
147cdf0e10cSrcweir 
getEdgeLength() const148cdf0e10cSrcweir             sal_uInt16 getEdgeLength() const { return RowSize; }
149cdf0e10cSrcweir 
get(sal_uInt16 nRow,sal_uInt16 nColumn) const150cdf0e10cSrcweir             double get(sal_uInt16 nRow, sal_uInt16 nColumn) const
151cdf0e10cSrcweir             {
152cdf0e10cSrcweir                 if(nRow < (RowSize - 1))
153cdf0e10cSrcweir                 {
154cdf0e10cSrcweir                     return maLine[nRow].get(nColumn);
155cdf0e10cSrcweir                 }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir                 if(mpLine)
158cdf0e10cSrcweir                 {
159cdf0e10cSrcweir                     return mpLine->get(nColumn);
160cdf0e10cSrcweir                 }
161cdf0e10cSrcweir 
162cdf0e10cSrcweir                 return implGetDefaultValue((RowSize - 1), nColumn);
163cdf0e10cSrcweir             }
164cdf0e10cSrcweir 
set(sal_uInt16 nRow,sal_uInt16 nColumn,const double & rValue)165cdf0e10cSrcweir             void set(sal_uInt16 nRow, sal_uInt16 nColumn, const double& rValue)
166cdf0e10cSrcweir             {
167cdf0e10cSrcweir                 if(nRow < (RowSize - 1))
168cdf0e10cSrcweir                 {
169cdf0e10cSrcweir                     maLine[nRow].set(nColumn, rValue);
170cdf0e10cSrcweir                 }
171cdf0e10cSrcweir                 else if(mpLine)
172cdf0e10cSrcweir                 {
173cdf0e10cSrcweir                     mpLine->set(nColumn, rValue);
174cdf0e10cSrcweir                 }
175cdf0e10cSrcweir                 else
176cdf0e10cSrcweir                 {
177cdf0e10cSrcweir                     const double fDefault(implGetDefaultValue((RowSize - 1), nColumn));
178cdf0e10cSrcweir 
179cdf0e10cSrcweir                     if(!::basegfx::fTools::equal(fDefault, rValue))
180cdf0e10cSrcweir                     {
181cdf0e10cSrcweir                         mpLine = new ImplMatLine< RowSize >((RowSize - 1), 0L);
182cdf0e10cSrcweir                         mpLine->set(nColumn, rValue);
183cdf0e10cSrcweir                     }
184cdf0e10cSrcweir                 }
185cdf0e10cSrcweir             }
186cdf0e10cSrcweir 
testLastLine()187cdf0e10cSrcweir             void testLastLine()
188cdf0e10cSrcweir             {
189cdf0e10cSrcweir                 if(mpLine)
190cdf0e10cSrcweir                 {
191cdf0e10cSrcweir                     bool bNecessary(false);
192cdf0e10cSrcweir 
193cdf0e10cSrcweir                     for(sal_uInt16 a(0);!bNecessary && a < RowSize; a++)
194cdf0e10cSrcweir                     {
195cdf0e10cSrcweir                         const double fDefault(implGetDefaultValue((RowSize - 1), a));
196cdf0e10cSrcweir                         const double fLineValue(mpLine->get(a));
197cdf0e10cSrcweir 
198cdf0e10cSrcweir                         if(!::basegfx::fTools::equal(fDefault, fLineValue))
199cdf0e10cSrcweir                         {
200cdf0e10cSrcweir                             bNecessary = true;
201cdf0e10cSrcweir                         }
202cdf0e10cSrcweir                     }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir                     if(!bNecessary)
205cdf0e10cSrcweir                     {
206cdf0e10cSrcweir                         delete mpLine;
207cdf0e10cSrcweir                         mpLine = 0L;
208cdf0e10cSrcweir                     }
209cdf0e10cSrcweir                 }
210cdf0e10cSrcweir             }
211cdf0e10cSrcweir 
212cdf0e10cSrcweir             // Left-upper decompositon
ludcmp(sal_uInt16 nIndex[],sal_Int16 & nParity)213cdf0e10cSrcweir             bool ludcmp(sal_uInt16 nIndex[], sal_Int16& nParity)
214cdf0e10cSrcweir             {
215cdf0e10cSrcweir                 double fBig, fSum, fDum;
216cdf0e10cSrcweir                 double fStorage[RowSize];
217cdf0e10cSrcweir                 sal_uInt16 a, b, c;
218cdf0e10cSrcweir 
219cdf0e10cSrcweir                 // #i30874# Initialize nAMax (compiler warns)
220cdf0e10cSrcweir                 sal_uInt16 nAMax = 0;
221cdf0e10cSrcweir 
222cdf0e10cSrcweir                 nParity = 1;
223cdf0e10cSrcweir 
224cdf0e10cSrcweir                 // Calc the max of each line. If a line is empty,
225cdf0e10cSrcweir                 // stop immediately since matrix is not invertible then.
226cdf0e10cSrcweir                 for(a = 0; a < RowSize; a++)
227cdf0e10cSrcweir                 {
228cdf0e10cSrcweir                     fBig = 0.0;
229cdf0e10cSrcweir 
230cdf0e10cSrcweir                     for(b = 0; b < RowSize; b++)
231cdf0e10cSrcweir                     {
232cdf0e10cSrcweir                         double fTemp(fabs(get(a, b)));
233cdf0e10cSrcweir 
234cdf0e10cSrcweir                         if(::basegfx::fTools::more(fTemp, fBig))
235cdf0e10cSrcweir                         {
236cdf0e10cSrcweir                             fBig = fTemp;
237cdf0e10cSrcweir                         }
238cdf0e10cSrcweir                     }
239cdf0e10cSrcweir 
240cdf0e10cSrcweir                     if(::basegfx::fTools::equalZero(fBig))
241cdf0e10cSrcweir                     {
242cdf0e10cSrcweir                         return false;
243cdf0e10cSrcweir                     }
244cdf0e10cSrcweir 
245cdf0e10cSrcweir                     fStorage[a] = 1.0 / fBig;
246cdf0e10cSrcweir                 }
247cdf0e10cSrcweir 
248cdf0e10cSrcweir                 // start normalizing
249cdf0e10cSrcweir                 for(b = 0; b < RowSize; b++)
250cdf0e10cSrcweir                 {
251cdf0e10cSrcweir                     for(a = 0; a < b; a++)
252cdf0e10cSrcweir                     {
253cdf0e10cSrcweir                         fSum = get(a, b);
254cdf0e10cSrcweir 
255cdf0e10cSrcweir                         for(c = 0; c < a; c++)
256cdf0e10cSrcweir                         {
257cdf0e10cSrcweir                             fSum -= get(a, c) * get(c, b);
258cdf0e10cSrcweir                         }
259cdf0e10cSrcweir 
260cdf0e10cSrcweir                         set(a, b, fSum);
261cdf0e10cSrcweir                     }
262cdf0e10cSrcweir 
263cdf0e10cSrcweir                     fBig = 0.0;
264cdf0e10cSrcweir 
265cdf0e10cSrcweir                     for(a = b; a < RowSize; a++)
266cdf0e10cSrcweir                     {
267cdf0e10cSrcweir                         fSum = get(a, b);
268cdf0e10cSrcweir 
269cdf0e10cSrcweir                         for(c = 0; c < b; c++)
270cdf0e10cSrcweir                         {
271cdf0e10cSrcweir                             fSum -= get(a, c) * get(c, b);
272cdf0e10cSrcweir                         }
273cdf0e10cSrcweir 
274cdf0e10cSrcweir                         set(a, b, fSum);
275cdf0e10cSrcweir                         fDum = fStorage[a] * fabs(fSum);
276cdf0e10cSrcweir 
277cdf0e10cSrcweir                         if(::basegfx::fTools::moreOrEqual(fDum, fBig))
278cdf0e10cSrcweir                         {
279cdf0e10cSrcweir                             fBig = fDum;
280cdf0e10cSrcweir                             nAMax = a;
281cdf0e10cSrcweir                         }
282cdf0e10cSrcweir                     }
283cdf0e10cSrcweir 
284cdf0e10cSrcweir                     if(b != nAMax)
285cdf0e10cSrcweir                     {
286cdf0e10cSrcweir                         for(c = 0; c < RowSize; c++)
287cdf0e10cSrcweir                         {
288cdf0e10cSrcweir                             fDum = get(nAMax, c);
289cdf0e10cSrcweir                             set(nAMax, c, get(b, c));
290cdf0e10cSrcweir                             set(b, c, fDum);
291cdf0e10cSrcweir                         }
292cdf0e10cSrcweir 
293cdf0e10cSrcweir                         nParity = -nParity;
294cdf0e10cSrcweir                         fStorage[nAMax] = fStorage[b];
295cdf0e10cSrcweir                     }
296cdf0e10cSrcweir 
297cdf0e10cSrcweir                     nIndex[b] = nAMax;
298cdf0e10cSrcweir 
299cdf0e10cSrcweir                     // here the failure of precision occurs
300cdf0e10cSrcweir                     const double fValBB(fabs(get(b, b)));
301cdf0e10cSrcweir 
302cdf0e10cSrcweir                     if(::basegfx::fTools::equalZero(fValBB))
303cdf0e10cSrcweir                     {
304cdf0e10cSrcweir                         return false;
305cdf0e10cSrcweir                     }
306cdf0e10cSrcweir 
307cdf0e10cSrcweir                     if(b != (RowSize - 1))
308cdf0e10cSrcweir                     {
309cdf0e10cSrcweir                         fDum = 1.0 / get(b, b);
310cdf0e10cSrcweir 
311cdf0e10cSrcweir                         for(a = b + 1; a < RowSize; a++)
312cdf0e10cSrcweir                         {
313cdf0e10cSrcweir                             set(a, b, get(a, b) * fDum);
314cdf0e10cSrcweir                         }
315cdf0e10cSrcweir                     }
316cdf0e10cSrcweir                 }
317cdf0e10cSrcweir 
318cdf0e10cSrcweir                 return true;
319cdf0e10cSrcweir             }
320cdf0e10cSrcweir 
lubksb(const sal_uInt16 nIndex[],double fRow[]) const321cdf0e10cSrcweir             void lubksb(const sal_uInt16 nIndex[], double fRow[]) const
322cdf0e10cSrcweir             {
323cdf0e10cSrcweir                 sal_uInt16 b, ip;
324cdf0e10cSrcweir                 sal_Int16 a, a2 = -1;
325cdf0e10cSrcweir                 double fSum;
326cdf0e10cSrcweir 
327cdf0e10cSrcweir                 for(a = 0; a < RowSize; a++)
328cdf0e10cSrcweir                 {
329cdf0e10cSrcweir                     ip = nIndex[a];
330cdf0e10cSrcweir                     fSum = fRow[ip];
331cdf0e10cSrcweir                     fRow[ip] = fRow[a];
332cdf0e10cSrcweir 
333cdf0e10cSrcweir                     if(a2 >= 0)
334cdf0e10cSrcweir                     {
335cdf0e10cSrcweir                         for(b = a2; b < a; b++)
336cdf0e10cSrcweir                         {
337cdf0e10cSrcweir                             fSum -= get(a, b) * fRow[b];
338cdf0e10cSrcweir                         }
339cdf0e10cSrcweir                     }
340cdf0e10cSrcweir                     else if(!::basegfx::fTools::equalZero(fSum))
341cdf0e10cSrcweir                     {
342cdf0e10cSrcweir                         a2 = a;
343cdf0e10cSrcweir                     }
344cdf0e10cSrcweir 
345cdf0e10cSrcweir                     fRow[a] = fSum;
346cdf0e10cSrcweir                 }
347cdf0e10cSrcweir 
348cdf0e10cSrcweir                 for(a = (RowSize - 1); a >= 0; a--)
349cdf0e10cSrcweir                 {
350cdf0e10cSrcweir                     fSum = fRow[a];
351cdf0e10cSrcweir 
352cdf0e10cSrcweir                     for(b = a + 1; b < RowSize; b++)
353cdf0e10cSrcweir                     {
354cdf0e10cSrcweir                         fSum -= get(a, b) * fRow[b];
355cdf0e10cSrcweir                     }
356cdf0e10cSrcweir 
357cdf0e10cSrcweir                     const double fValueAA(get(a, a));
358cdf0e10cSrcweir 
359cdf0e10cSrcweir                     if(!::basegfx::fTools::equalZero(fValueAA))
360cdf0e10cSrcweir                     {
361cdf0e10cSrcweir                         fRow[a] = fSum / get(a, a);
362cdf0e10cSrcweir                     }
363cdf0e10cSrcweir                 }
364cdf0e10cSrcweir             }
365cdf0e10cSrcweir 
isIdentity() const366cdf0e10cSrcweir             bool isIdentity() const
367cdf0e10cSrcweir             {
368cdf0e10cSrcweir                 // last line needs no testing if not existing
369cdf0e10cSrcweir                 const sal_uInt16 nMaxLine(
370cdf0e10cSrcweir                     sal::static_int_cast<sal_uInt16>(mpLine ? RowSize : (RowSize - 1)) );
371cdf0e10cSrcweir 
372cdf0e10cSrcweir                 for(sal_uInt16 a(0); a < nMaxLine; a++)
373cdf0e10cSrcweir                 {
374cdf0e10cSrcweir                     for(sal_uInt16 b(0); b < RowSize; b++)
375cdf0e10cSrcweir                     {
376cdf0e10cSrcweir                         const double fDefault(implGetDefaultValue(a, b));
377cdf0e10cSrcweir                         const double fValueAB(get(a, b));
378cdf0e10cSrcweir 
379cdf0e10cSrcweir                         if(!::basegfx::fTools::equal(fDefault, fValueAB))
380cdf0e10cSrcweir                         {
381cdf0e10cSrcweir                             return false;
382cdf0e10cSrcweir                         }
383cdf0e10cSrcweir                     }
384cdf0e10cSrcweir                 }
385cdf0e10cSrcweir 
386cdf0e10cSrcweir                 return true;
387cdf0e10cSrcweir             }
388cdf0e10cSrcweir 
isInvertible() const389cdf0e10cSrcweir             bool isInvertible() const
390cdf0e10cSrcweir             {
391cdf0e10cSrcweir                 ImplHomMatrixTemplate aWork(*this);
392cdf0e10cSrcweir                 sal_uInt16 nIndex[RowSize];
393cdf0e10cSrcweir                 sal_Int16 nParity;
394cdf0e10cSrcweir 
395cdf0e10cSrcweir                 return aWork.ludcmp(nIndex, nParity);
396cdf0e10cSrcweir             }
397cdf0e10cSrcweir 
isNormalized() const398cdf0e10cSrcweir             bool isNormalized() const
399cdf0e10cSrcweir             {
400cdf0e10cSrcweir                 if(!mpLine)
401cdf0e10cSrcweir                     return true;
402cdf0e10cSrcweir 
403cdf0e10cSrcweir                 const double fHomValue(get((RowSize - 1), (RowSize - 1)));
404cdf0e10cSrcweir 
405cdf0e10cSrcweir                 if(::basegfx::fTools::equalZero(fHomValue))
406cdf0e10cSrcweir                 {
407cdf0e10cSrcweir                     return true;
408cdf0e10cSrcweir                 }
409cdf0e10cSrcweir 
410cdf0e10cSrcweir                 const double fOne(1.0);
411cdf0e10cSrcweir 
412cdf0e10cSrcweir                 if(::basegfx::fTools::equal(fOne, fHomValue))
413cdf0e10cSrcweir                 {
414cdf0e10cSrcweir                     return true;
415cdf0e10cSrcweir                 }
416cdf0e10cSrcweir 
417cdf0e10cSrcweir                 return false;
418cdf0e10cSrcweir             }
419cdf0e10cSrcweir 
doInvert(const ImplHomMatrixTemplate & rWork,const sal_uInt16 nIndex[])420cdf0e10cSrcweir             void doInvert(const ImplHomMatrixTemplate& rWork, const sal_uInt16 nIndex[])
421cdf0e10cSrcweir             {
422cdf0e10cSrcweir                 double fArray[RowSize];
423cdf0e10cSrcweir 
424cdf0e10cSrcweir                 for(sal_uInt16 a(0); a < RowSize; a++)
425cdf0e10cSrcweir                 {
426cdf0e10cSrcweir                     // prepare line
427cdf0e10cSrcweir 		    sal_uInt16 b;
428cdf0e10cSrcweir                     for( b = 0; b < RowSize; b++)
429cdf0e10cSrcweir                     {
430cdf0e10cSrcweir                         fArray[b] = implGetDefaultValue(a, b);
431cdf0e10cSrcweir                     }
432cdf0e10cSrcweir 
433cdf0e10cSrcweir                     // expand line
434cdf0e10cSrcweir                     rWork.lubksb(nIndex, fArray);
435cdf0e10cSrcweir 
436cdf0e10cSrcweir                     // copy line transposed to this matrix
437cdf0e10cSrcweir                     for( b = 0; b < RowSize; b++)
438cdf0e10cSrcweir                     {
439cdf0e10cSrcweir                         set(b, a, fArray[b]);
440cdf0e10cSrcweir                     }
441cdf0e10cSrcweir                 }
442cdf0e10cSrcweir 
443cdf0e10cSrcweir                 // evtl. get rid of last matrix line
444cdf0e10cSrcweir                 testLastLine();
445cdf0e10cSrcweir             }
446cdf0e10cSrcweir 
doNormalize()447cdf0e10cSrcweir             void doNormalize()
448cdf0e10cSrcweir             {
449cdf0e10cSrcweir                 if(mpLine)
450cdf0e10cSrcweir                 {
451cdf0e10cSrcweir                     const double fHomValue(get((RowSize - 1), (RowSize - 1)));
452cdf0e10cSrcweir 
453cdf0e10cSrcweir                     for(sal_uInt16 a(0); a < RowSize; a++)
454cdf0e10cSrcweir                     {
455cdf0e10cSrcweir                         for(sal_uInt16 b(0); b < RowSize; b++)
456cdf0e10cSrcweir                         {
457cdf0e10cSrcweir                             set(a, b, get(a, b) / fHomValue);
458cdf0e10cSrcweir                         }
459cdf0e10cSrcweir                     }
460cdf0e10cSrcweir 
461cdf0e10cSrcweir                     // evtl. get rid of last matrix line
462cdf0e10cSrcweir                     testLastLine();
463cdf0e10cSrcweir                 }
464cdf0e10cSrcweir             }
465cdf0e10cSrcweir 
doDeterminant() const466cdf0e10cSrcweir             double doDeterminant() const
467cdf0e10cSrcweir             {
468cdf0e10cSrcweir                 ImplHomMatrixTemplate aWork(*this);
469cdf0e10cSrcweir                 sal_uInt16 nIndex[RowSize];
470cdf0e10cSrcweir                 sal_Int16 nParity;
471cdf0e10cSrcweir                 double fRetval(0.0);
472cdf0e10cSrcweir 
473cdf0e10cSrcweir                 if(aWork.ludcmp(nIndex, nParity))
474cdf0e10cSrcweir                 {
475cdf0e10cSrcweir                     fRetval = (double)nParity;
476cdf0e10cSrcweir 
477cdf0e10cSrcweir                     // last line needs no multiply if not existing; default value would be 1.
478cdf0e10cSrcweir                     const sal_uInt16 nMaxLine(
479cdf0e10cSrcweir                         sal::static_int_cast<sal_uInt16>(aWork.mpLine ? RowSize : (RowSize - 1)) );
480cdf0e10cSrcweir 
481cdf0e10cSrcweir                     for(sal_uInt16 a(0); a < nMaxLine; a++)
482cdf0e10cSrcweir                     {
483cdf0e10cSrcweir                         fRetval *= aWork.get(a, a);
484cdf0e10cSrcweir                     }
485cdf0e10cSrcweir                 }
486cdf0e10cSrcweir 
487cdf0e10cSrcweir                 return fRetval;
488cdf0e10cSrcweir             }
489cdf0e10cSrcweir 
doTrace() const490cdf0e10cSrcweir             double doTrace() const
491cdf0e10cSrcweir             {
492cdf0e10cSrcweir                 double fTrace = (mpLine) ? 0.0 : 1.0;
493cdf0e10cSrcweir                 const sal_uInt16 nMaxLine(
494cdf0e10cSrcweir                     sal::static_int_cast<sal_uInt16>(mpLine ? RowSize : (RowSize - 1)) );
495cdf0e10cSrcweir 
496cdf0e10cSrcweir                 for(sal_uInt16 a(0); a < nMaxLine; a++)
497cdf0e10cSrcweir                 {
498cdf0e10cSrcweir                     fTrace += get(a, a);
499cdf0e10cSrcweir                 }
500cdf0e10cSrcweir 
501cdf0e10cSrcweir                 return fTrace;
502cdf0e10cSrcweir             }
503cdf0e10cSrcweir 
doTranspose()504cdf0e10cSrcweir             void doTranspose()
505cdf0e10cSrcweir             {
506cdf0e10cSrcweir                 for(sal_uInt16 a(0); a < (RowSize - 1); a++)
507cdf0e10cSrcweir                 {
508cdf0e10cSrcweir                     for(sal_uInt16 b(a + 1); b < RowSize; b++)
509cdf0e10cSrcweir                     {
510cdf0e10cSrcweir                         const double fTemp(get(a, b));
511cdf0e10cSrcweir                         set(a, b, get(b, a));
512cdf0e10cSrcweir                         set(b, a, fTemp);
513cdf0e10cSrcweir                     }
514cdf0e10cSrcweir                 }
515cdf0e10cSrcweir 
516cdf0e10cSrcweir                 testLastLine();
517cdf0e10cSrcweir             }
518cdf0e10cSrcweir 
doAddMatrix(const ImplHomMatrixTemplate & rMat)519cdf0e10cSrcweir             void doAddMatrix(const ImplHomMatrixTemplate& rMat)
520cdf0e10cSrcweir             {
521cdf0e10cSrcweir                 for(sal_uInt16 a(0); a < RowSize; a++)
522cdf0e10cSrcweir                 {
523cdf0e10cSrcweir                     for(sal_uInt16 b(0); b < RowSize; b++)
524cdf0e10cSrcweir                     {
525cdf0e10cSrcweir                         set(a, b, get(a, b) + rMat.get(a, b));
526cdf0e10cSrcweir                     }
527cdf0e10cSrcweir                 }
528cdf0e10cSrcweir 
529cdf0e10cSrcweir                 testLastLine();
530cdf0e10cSrcweir             }
531cdf0e10cSrcweir 
doSubMatrix(const ImplHomMatrixTemplate & rMat)532cdf0e10cSrcweir             void doSubMatrix(const ImplHomMatrixTemplate& rMat)
533cdf0e10cSrcweir             {
534cdf0e10cSrcweir                 for(sal_uInt16 a(0); a < RowSize; a++)
535cdf0e10cSrcweir                 {
536cdf0e10cSrcweir                     for(sal_uInt16 b(0); b < RowSize; b++)
537cdf0e10cSrcweir                     {
538cdf0e10cSrcweir                         set(a, b, get(a, b) - rMat.get(a, b));
539cdf0e10cSrcweir                     }
540cdf0e10cSrcweir                 }
541cdf0e10cSrcweir 
542cdf0e10cSrcweir                 testLastLine();
543cdf0e10cSrcweir             }
544cdf0e10cSrcweir 
doMulMatrix(const double & rfValue)545cdf0e10cSrcweir             void doMulMatrix(const double& rfValue)
546cdf0e10cSrcweir             {
547cdf0e10cSrcweir                 for(sal_uInt16 a(0); a < RowSize; a++)
548cdf0e10cSrcweir                 {
549cdf0e10cSrcweir                     for(sal_uInt16 b(0); b < RowSize; b++)
550cdf0e10cSrcweir                     {
551cdf0e10cSrcweir                         set(a, b, get(a, b) * rfValue);
552cdf0e10cSrcweir                     }
553cdf0e10cSrcweir                 }
554cdf0e10cSrcweir 
555cdf0e10cSrcweir                 testLastLine();
556cdf0e10cSrcweir             }
557cdf0e10cSrcweir 
doMulMatrix(const ImplHomMatrixTemplate & rMat)558cdf0e10cSrcweir             void doMulMatrix(const ImplHomMatrixTemplate& rMat)
559cdf0e10cSrcweir             {
560cdf0e10cSrcweir                 // create a copy as source for the original values
561cdf0e10cSrcweir                 const ImplHomMatrixTemplate aCopy(*this);
562cdf0e10cSrcweir 
563cdf0e10cSrcweir                 // TODO: maybe optimize cases where last line is [0 0 1].
564cdf0e10cSrcweir 
565cdf0e10cSrcweir                 double fValue(0.0);
566cdf0e10cSrcweir 
567cdf0e10cSrcweir                 for(sal_uInt16 a(0); a < RowSize; ++a)
568cdf0e10cSrcweir                 {
569cdf0e10cSrcweir                     for(sal_uInt16 b(0); b < RowSize; ++b)
570cdf0e10cSrcweir                     {
571cdf0e10cSrcweir                         fValue = 0.0;
572cdf0e10cSrcweir 
573cdf0e10cSrcweir                         for(sal_uInt16 c(0); c < RowSize; ++c)
574cdf0e10cSrcweir                             fValue += aCopy.get(c, b) * rMat.get(a, c);
575cdf0e10cSrcweir 
576cdf0e10cSrcweir                         set(a, b, fValue);
577cdf0e10cSrcweir                     }
578cdf0e10cSrcweir                 }
579cdf0e10cSrcweir 
580cdf0e10cSrcweir                 testLastLine();
581cdf0e10cSrcweir             }
582cdf0e10cSrcweir 
isEqual(const ImplHomMatrixTemplate & rMat) const583cdf0e10cSrcweir             bool isEqual(const ImplHomMatrixTemplate& rMat) const
584cdf0e10cSrcweir             {
585cdf0e10cSrcweir                 const sal_uInt16 nMaxLine(
586cdf0e10cSrcweir                     sal::static_int_cast<sal_uInt16>((mpLine || rMat.mpLine) ? RowSize : (RowSize - 1)) );
587cdf0e10cSrcweir 
588cdf0e10cSrcweir                 for(sal_uInt16 a(0); a < nMaxLine; a++)
589cdf0e10cSrcweir                 {
590cdf0e10cSrcweir                     for(sal_uInt16 b(0); b < RowSize; b++)
591cdf0e10cSrcweir                     {
592cdf0e10cSrcweir                         const double fValueA(get(a, b));
593cdf0e10cSrcweir                         const double fValueB(rMat.get(a, b));
594cdf0e10cSrcweir 
595cdf0e10cSrcweir                         if(!::basegfx::fTools::equal(fValueA, fValueB))
596cdf0e10cSrcweir                         {
597cdf0e10cSrcweir                             return false;
598cdf0e10cSrcweir                         }
599cdf0e10cSrcweir                     }
600cdf0e10cSrcweir                 }
601cdf0e10cSrcweir 
602cdf0e10cSrcweir                 return true;
603cdf0e10cSrcweir             }
604cdf0e10cSrcweir         };
605cdf0e10cSrcweir 
606cdf0e10cSrcweir     } // namespace internal
607cdf0e10cSrcweir } // namespace basegfx
608cdf0e10cSrcweir 
609cdf0e10cSrcweir #endif /* _HOMMATRIX_TEMPLATE_HXX */
610