xref: /aoo42x/main/vcl/aqua/source/gdi/salmathutils.cxx (revision 9f62ea84)
1*9f62ea84SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9f62ea84SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9f62ea84SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9f62ea84SAndrew Rist  * distributed with this work for additional information
6*9f62ea84SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9f62ea84SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9f62ea84SAndrew Rist  * "License"); you may not use this file except in compliance
9*9f62ea84SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9f62ea84SAndrew Rist  *
11*9f62ea84SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9f62ea84SAndrew Rist  *
13*9f62ea84SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9f62ea84SAndrew Rist  * software distributed under the License is distributed on an
15*9f62ea84SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9f62ea84SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9f62ea84SAndrew Rist  * specific language governing permissions and limitations
18*9f62ea84SAndrew Rist  * under the License.
19*9f62ea84SAndrew Rist  *
20*9f62ea84SAndrew Rist  *************************************************************/
21*9f62ea84SAndrew Rist 
22*9f62ea84SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_vcl.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "aqua/salmathutils.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <stdlib.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir // =======================================================================
32cdf0e10cSrcweir 
33cdf0e10cSrcweir // =======================================================================
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #define Swap( x, y ) { x ^= y; y ^= x; x ^= y; }
36cdf0e10cSrcweir 
37cdf0e10cSrcweir // =======================================================================
38cdf0e10cSrcweir 
39cdf0e10cSrcweir // =======================================================================
40cdf0e10cSrcweir 
41cdf0e10cSrcweir // Storage free swapping using XOR
42cdf0e10cSrcweir 
43cdf0e10cSrcweir void CSwap ( char &rX, char &rY )
44cdf0e10cSrcweir {
45cdf0e10cSrcweir 	Swap( rX, rY );
46cdf0e10cSrcweir } // CSwap
47cdf0e10cSrcweir 
48cdf0e10cSrcweir // -----------------------------------------------------------------------
49cdf0e10cSrcweir 
50cdf0e10cSrcweir // Storage free swapping using XOR
51cdf0e10cSrcweir 
52cdf0e10cSrcweir void UCSwap ( unsigned char &rX, unsigned char &rY )
53cdf0e10cSrcweir {
54cdf0e10cSrcweir 	Swap( rX, rY );
55cdf0e10cSrcweir } // UCSwap
56cdf0e10cSrcweir 
57cdf0e10cSrcweir // -----------------------------------------------------------------------
58cdf0e10cSrcweir 
59cdf0e10cSrcweir // Storage free swapping using XOR
60cdf0e10cSrcweir 
61cdf0e10cSrcweir void SSwap ( short &rX, short &rY )
62cdf0e10cSrcweir {
63cdf0e10cSrcweir 	Swap( rX, rY );
64cdf0e10cSrcweir } // SSwap
65cdf0e10cSrcweir 
66cdf0e10cSrcweir // -----------------------------------------------------------------------
67cdf0e10cSrcweir 
68cdf0e10cSrcweir // Storage free swapping using XOR
69cdf0e10cSrcweir 
70cdf0e10cSrcweir void USSwap ( unsigned short &rX, unsigned short &rY )
71cdf0e10cSrcweir {
72cdf0e10cSrcweir 	Swap( rX, rY );
73cdf0e10cSrcweir } // USSwap
74cdf0e10cSrcweir 
75cdf0e10cSrcweir // -----------------------------------------------------------------------
76cdf0e10cSrcweir 
77cdf0e10cSrcweir // Storage free swapping using XOR
78cdf0e10cSrcweir 
79cdf0e10cSrcweir void LSwap ( long &rX, long &rY )
80cdf0e10cSrcweir {
81cdf0e10cSrcweir 	Swap( rX, rY );
82cdf0e10cSrcweir } // LSwap
83cdf0e10cSrcweir 
84cdf0e10cSrcweir // -----------------------------------------------------------------------
85cdf0e10cSrcweir 
86cdf0e10cSrcweir // Storage free swapping using XOR
87cdf0e10cSrcweir 
88cdf0e10cSrcweir void ULSwap ( unsigned long &rX, unsigned long &rY )
89cdf0e10cSrcweir {
90cdf0e10cSrcweir 	Swap( rX, rY );
91cdf0e10cSrcweir } // ULSwap
92cdf0e10cSrcweir 
93cdf0e10cSrcweir // =======================================================================
94cdf0e10cSrcweir 
95cdf0e10cSrcweir // =======================================================================
96cdf0e10cSrcweir 
97cdf0e10cSrcweir // -----------------------------------------------------------------------
98cdf0e10cSrcweir //
99cdf0e10cSrcweir // This way of measuring distance is also called the "Manhattan distance."
100cdf0e10cSrcweir // Manhattan distance takes advantage of the fact that the sum of the
101cdf0e10cSrcweir // lengths of the three components of a 3D vector is a rough approxima-
102cdf0e10cSrcweir // tion of the vector's length.
103cdf0e10cSrcweir //
104cdf0e10cSrcweir // -----------------------------------------------------------------------
105cdf0e10cSrcweir 
106cdf0e10cSrcweir unsigned long  Euclidian2Norm ( const LRectCoorVector  pVec )
107cdf0e10cSrcweir {
108cdf0e10cSrcweir 	unsigned long  ndist = 0;
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 	if ( pVec )
111cdf0e10cSrcweir 	{
112cdf0e10cSrcweir 		long           nDX  = 0;
113cdf0e10cSrcweir 		long           nDY  = 0;
114cdf0e10cSrcweir 		long           nDZ  = 0;
115cdf0e10cSrcweir 		unsigned long  nMax = 0;
116cdf0e10cSrcweir 		unsigned long  nMed = 0;
117cdf0e10cSrcweir 		unsigned long  nMin = 0;
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 		// Find |x'-x|, |y'-y|, and |z'-z| from (x,y,z) and (x',y',z')
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 		nDX = pVec[1].x - pVec[0].x;
122cdf0e10cSrcweir 		nDY = pVec[1].y - pVec[0].y;
123cdf0e10cSrcweir 		nDZ = pVec[1].z - pVec[0].z;
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 		nMax = (unsigned long)abs( nDX );
126cdf0e10cSrcweir 		nMed = (unsigned long)abs( nDY );
127cdf0e10cSrcweir 		nMin = (unsigned long)abs( nDZ );
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 		// Sort them (3 compares, 0-3 swaps)
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 		if ( nMax < nMed )
132cdf0e10cSrcweir 		{
133cdf0e10cSrcweir 			Swap( nMax, nMed );
134cdf0e10cSrcweir 		} // if
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 		if ( nMax < nMin )
137cdf0e10cSrcweir 		{
138cdf0e10cSrcweir 			Swap( nMax, nMin );
139cdf0e10cSrcweir 		} // if
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 		// Approximate Euclidian distance:
142cdf0e10cSrcweir 		//
143cdf0e10cSrcweir 		//    d = max + (11/32)*med + (1/4)*min
144cdf0e10cSrcweir 		//
145cdf0e10cSrcweir 		// with +/- 8% error, where the exact formulae for d is
146cdf0e10cSrcweir 		//
147cdf0e10cSrcweir 		//  || (x',y',z') - (x,y,z) || = { |x'-x|^2 + |y'-y|^2 + |z'-z|^2 }^(1/2)
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 		ndist = nMax + ( nMin >> 2UL )
150cdf0e10cSrcweir 		             + ( ( ( nMed << 3UL ) + ( nMed << 1UL ) + nMed ) >> 5UL );
151cdf0e10cSrcweir 	} // if
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 	return ndist;
154cdf0e10cSrcweir } // RGBDistance
155cdf0e10cSrcweir 
156cdf0e10cSrcweir // =======================================================================
157cdf0e10cSrcweir 
158cdf0e10cSrcweir // =======================================================================
159cdf0e10cSrcweir 
160