POV-Ray : Newsgroups : povray.general : #switch #case in function? : #switch #case in function? Server Time
27 Jul 2024 08:02:18 EDT (-0400)
  #switch #case in function?  
From: Ilya Razmanov
Date: 16 Jun 2024 06:13:59
Message: <666ebae7$1@news.povray.org>
Yet another stupid question from me: can I use "case" operator withing 
function? I can't figure it out.

Details:

I already used functions in my POVmosaic for mapping some object 
attributes to source pixel brightness, and surely implemented some 
transfer functions. And surely I want map in my img2mesh as well since 
sometimes I need some map adjustment. Currently I do it with Photoshop 
by editing source PNG heightfield with Photoshop "Curves/Map", saving 
resulting PNG, passing through img2mesh, rendering mesh thus obtained, 
realizing I need slightly changing map so editing it with Photoshop 
"Curves/Map" again etc. etc., so surely I want to transfer map from 
Photoshop into POVRay so it works in a non-destructive manner right in 
the final scene.

Currently my maps look like this:

//    Map functions
#declare map_1 = function(c) {c}               // Direct input
#declare map_2 = function(c) {1.0 - c}         // Negative input
#declare map_3 = function(c) {pow(c,(1/1.5))}  // Gamma 1.5
#declare map_4 = function(c) {      // Piecewise rescaling example start
     (c < 0.7) * ((c - 0) / (0.7 - 0) * (1 - 0) + 0) +       // rescale 
0-0.7 to 0-1
     (c >= 0.7) * ((c - 0.7) / (1 - 0.7) * (0.5 - 1) + 1)    // rescale 
0.7-1 to 1-0.5
}  // Piecewise example end

as you can see, map_4 is piecewise, one piece scaling up, another one 
scaling with inversion (sorry for all the +0 and -0, I put them to make 
things look "generalized" to try to prevent me forgetting what happens 
here. I'm still not sure that it doesn't contain errors already, need 
more tests), where "piecewise" is achieved by multiplying bools on cases 
and then adding all this together. It works but looks silly, and looking 
silly increase its chances to make me mess something up.

Is there any more elegant way to do piecewise functions in POVRay? I 
know that "if" and "case" exist in some form, but from examples I can 
see I don't get whether I can put them within functions, or not.

-- 
Ilyich the Toad
https://dnyarri.github.io/


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.