Fit is canonically formulated as: out = newmin + ((value-oldmin)/(oldmax-oldmin)*(newmax-newmin)) I.e., subtract the old offset, descale into the [0,1] range, scale into the new range, and add the new offset We algebraiclly manipulate the terms into y = mx + b form as: m = (newmax-newmin)/(oldmax-oldmin) b = (newmin*oldmax - newmax*oldmin) / (oldmax-oldmin) */
| 160 | b = (newmin*oldmax - newmax*oldmin) / (oldmax-oldmin) |
| 161 | */ |
| 162 | void MatrixTransform::Fit(double * m44, double * offset4, |
| 163 | const double * oldmin4, const double * oldmax4, |
| 164 | const double * newmin4, const double * newmax4) |
| 165 | { |
| 166 | if(!oldmin4 || !oldmax4) return; |
| 167 | if(!newmin4 || !newmax4) return; |
| 168 | |
| 169 | if(m44) memset(m44, 0, 16*sizeof(double)); |
| 170 | if(offset4) memset(offset4, 0, 4*sizeof(double)); |
| 171 | |
| 172 | for(int i=0; i<4; ++i) |
| 173 | { |
| 174 | double denom = oldmax4[i] - oldmin4[i]; |
| 175 | if(IsScalarEqualToZero(denom)) |
| 176 | { |
| 177 | std::ostringstream os; |
| 178 | os << "Cannot create Fit operator. "; |
| 179 | os << "Max value equals min value '"; |
| 180 | os << oldmax4[i] << "' in channel index "; |
| 181 | os << i << "."; |
| 182 | throw Exception(os.str().c_str()); |
| 183 | } |
| 184 | |
| 185 | if(m44) m44[5*i] = (newmax4[i]-newmin4[i]) / denom; |
| 186 | if(offset4) offset4[i] = (newmin4[i]*oldmax4[i] - newmax4[i]*oldmin4[i]) / denom; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | void MatrixTransform::Identity(double * m44, double * offset4) |
| 191 | { |