1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: salmathutils.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
34 #include "salmathutils.hxx"
38 // =======================================================================
40 // =======================================================================
42 #define Swap( x, y ) { x ^= y; y ^= x; x ^= y; }
44 // =======================================================================
46 // =======================================================================
48 // Storage free swapping using XOR
50 void CSwap ( char &rX
, char &rY
)
55 // -----------------------------------------------------------------------
57 // Storage free swapping using XOR
59 void UCSwap ( unsigned char &rX
, unsigned char &rY
)
64 // -----------------------------------------------------------------------
66 // Storage free swapping using XOR
68 void SSwap ( short &rX
, short &rY
)
73 // -----------------------------------------------------------------------
75 // Storage free swapping using XOR
77 void USSwap ( unsigned short &rX
, unsigned short &rY
)
82 // -----------------------------------------------------------------------
84 // Storage free swapping using XOR
86 void LSwap ( long &rX
, long &rY
)
91 // -----------------------------------------------------------------------
93 // Storage free swapping using XOR
95 void ULSwap ( unsigned long &rX
, unsigned long &rY
)
100 // =======================================================================
102 // =======================================================================
104 // -----------------------------------------------------------------------
106 // This way of measuring distance is also called the "Manhattan distance."
107 // Manhattan distance takes advantage of the fact that the sum of the
108 // lengths of the three components of a 3D vector is a rough approxima-
109 // tion of the vector's length.
111 // -----------------------------------------------------------------------
113 unsigned long Euclidian2Norm ( const LRectCoorVector pVec
)
115 unsigned long ndist
= 0;
122 unsigned long nMax
= 0;
123 unsigned long nMed
= 0;
124 unsigned long nMin
= 0;
126 // Find |x'-x|, |y'-y|, and |z'-z| from (x,y,z) and (x',y',z')
128 nDX
= pVec
[1].x
- pVec
[0].x
;
129 nDY
= pVec
[1].y
- pVec
[0].y
;
130 nDZ
= pVec
[1].z
- pVec
[0].z
;
132 nMax
= (unsigned long)abs( nDX
);
133 nMed
= (unsigned long)abs( nDY
);
134 nMin
= (unsigned long)abs( nDZ
);
136 // Sort them (3 compares, 0-3 swaps)
148 // Approximate Euclidian distance:
150 // d = max + (11/32)*med + (1/4)*min
152 // with +/- 8% error, where the exact formulae for d is
154 // || (x',y',z') - (x,y,z) || = { |x'-x|^2 + |y'-y|^2 + |z'-z|^2 }^(1/2)
156 ndist
= nMax
+ ( nMin
>> 2UL )
157 + ( ( ( nMed
<< 3UL ) + ( nMed
<< 1UL ) + nMed
) >> 5UL );
163 // =======================================================================
165 // =======================================================================