@@ -264,27 +264,39 @@ bool img_load(ImageBuf& outBuf, const std::string& inputFileName, bool external_
264264}
265265
266266template <class Rtype >
267- static bool normalize_impl (ImageBuf& R, const ImageBuf& A, float inCenter, float outCenter, float scale, ROI roi, int nthreads)
267+ static bool recalc_normal_impl (ImageBuf& R, const ImageBuf& A, uint channel, int sign , float inCenter, float outCenter, float scale, ROI roi, int nthreads)
268268{
269269 ImageBufAlgo::parallel_image (roi, nthreads, [&](ROI roi) {
270270 ImageBuf::ConstIterator<Rtype> a (A, roi);
271271 for (ImageBuf::Iterator<Rtype> r (R, roi); !r.done (); ++r, ++a)
272272 {
273- float x = a[0 ];
274- float y = a[1 ];
275- float z = a[2 ];
276273
277- x = x - inCenter;
278- y = y - inCenter;
279- z = z - inCenter;
274+ // axis = sqrt(1 - (x^2 + y^2))
275+ // axs = sqrt(1 - (x*x + y*y + z*z))
280276
281- float length = std::hypot (x, y, z);
277+ float t[3 ];
278+
279+ t[0 ] = channel == 0 ? 0 .0f : (a[0 ] - inCenter) / scale;
280+ t[1 ] = channel == 1 ? 0 .0f : (a[1 ] - inCenter) / scale;
281+ t[2 ] = channel == 2 ? 0 .0f : (a[2 ] - inCenter) / scale;
282+
283+ bool isZero = (abs (t[0 ]) + abs (t[1 ]) + abs (t[2 ])) == 0 .0f ;
284+
285+ float recomp = 0 .0f ;
286+
287+ if (!isZero) {
288+ recomp = sqrt (1 .0f - (t[0 ] * t[0 ] + t[1 ] * t[1 ] + t[2 ] * t[2 ]));
289+ }
290+
291+ t[channel] = sign * recomp;
292+
293+ float length = sqrt (t[0 ] * t[0 ] + t[1 ] * t[1 ] + t[2 ] * t[2 ]);
282294
283295 float s = (length > 0 .0f ) ? scale / length : scale;
284296
285- r[0 ] = x * s + outCenter;
286- r[1 ] = y * s + outCenter;
287- r[2 ] = z * s + outCenter;
297+ r[0 ] = t[ 0 ] * s + outCenter;
298+ r[1 ] = t[ 1 ] * s + outCenter;
299+ r[2 ] = t[ 2 ] * s + outCenter;
288300
289301 if (A.spec ().nchannels == 4 ) {
290302 r[3 ] = a[3 ];
@@ -294,42 +306,21 @@ static bool normalize_impl(ImageBuf& R, const ImageBuf& A, float inCenter, float
294306 return true ;
295307}
296308
297- bool normalize (ImageBuf& dst, const ImageBuf& A, float inCenter, float outCenter, float scale, ROI roi, int nthreads)
309+ bool recalc_normal (ImageBuf& dst, const ImageBuf& A, uint channel, int sign , float inCenter, float outCenter, float scale, ROI roi, int nthreads)
298310{
299311 if (!ImageBufAlgo::IBAprep (roi, &dst, &A))
300312 return false ;
301313 bool ok;
302- // OIIO_DISPATCH_COMMON_TYPES(ok, "normalize", normalize_impl, dst.spec().format, dst, A, fullRange, roi, nthreads);
303- switch (dst.spec ().format .basetype ) {
304- case TypeDesc::FLOAT:
305- ok = normalize_impl<float >(dst, A, inCenter, outCenter, scale, roi, nthreads);
306- break ;
307- case TypeDesc::UINT8:
308- ok = normalize_impl<unsigned char >(dst, A, inCenter, outCenter, scale, roi, nthreads);
309- break ;
310- case TypeDesc::HALF: // should be half but it give me an error
311- ok = normalize_impl<float >(dst, A, inCenter, outCenter, scale, roi, nthreads);
312- break ;
313- case TypeDesc::UINT16:
314- ok = normalize_impl<unsigned short >(dst, A, inCenter, outCenter, scale, roi, nthreads);
315- break ;
316- default :
317- {
318- ImageBuf Rtmp;
319- if ((dst).initialized ()) Rtmp.copy (dst, TypeDesc::FLOAT);
320- ok = normalize_impl<float >(Rtmp, A, inCenter, outCenter, scale, roi, nthreads);
321- if (ok) (dst).copy (Rtmp);
322- else (dst).errorfmt (" {}" , Rtmp.geterror ());
323- }
324- };
314+ OIIO_DISPATCH_COMMON_TYPES (ok, " recalc_normal" , recalc_normal_impl, dst.spec ().format , dst, A,
315+ channel, sign, inCenter, outCenter, scale, roi, nthreads);
325316 return ok;
326317}
327318
328- ImageBuf normalize (const ImageBuf& A, float inCenter, float outCenter, float scale, ROI roi, int nthreads)
319+ ImageBuf recalc_normal (const ImageBuf& A, uint channel, int sign , float inCenter, float outCenter, float scale, ROI roi, int nthreads)
329320{
330321 ImageBuf result;
331- bool ok = normalize (result, A, inCenter, outCenter, scale, roi, nthreads);
322+ bool ok = recalc_normal (result, A, channel, sign , inCenter, outCenter, scale, roi, nthreads);
332323 if (!ok && !result.has_error ())
333- result.errorfmt (" normalize error" );
324+ result.errorfmt (" recalculation error" );
334325 return result;
335326}
0 commit comments