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