POV-Ray : Newsgroups : povray.newusers : Using conditional expression in functions. How to write step functions? : Re: Using conditional expression in functions. How to write step functions? Server Time
8 Jul 2024 00:27:32 EDT (-0400)
  Re: Using conditional expression in functions. How to write step functions?  
From: Christian Froeschlin
Date: 18 Dec 2009 18:06:40
Message: <4b2c0b00$1@news.povray.org>
Evgenij Gagauz wrote:

> Unfortunatly the best way is express it with the something like case statement,
> not with the "select" function.

I just hacked something up which might do what you want,
with the following limitations

1. No nesting of fswitch blocks
2. Quotes required around all subexpressions (except plain numbers)

Basically, it is a set of macros which will write an include
file containing the nested select statements.

#macro fswitch(CONTROL)
#declare CASE_CONTROL = CONTROL;
#declare CASE_COUNTER = 0;
#fopen   CASE_FILE "switch_tmp.inc" write
#end

#macro fcase(CASE,VALUE)
#declare CASE_COUNTER = CASE_COUNTER + 1;
#write(CASE_FILE,"select((",CASE_CONTROL,")-(",CASE,"),",VALUE,",\n")
#end

#macro fdefault(VALUE)
#write(CASE_FILE,VALUE)
#while(CASE_COUNTER > 0)
   #write(CASE_FILE,")")
   #declare CASE_COUNTER = CASE_COUNTER - 1;
#end
#fclose CASE_FILE
#include "switch_tmp.inc"
#end

function(a,b)
{
   fswitch("a")
   fcase(0.1,"1")
   fcase(0.5,"b")
   fdefault("0")
}


Post a reply to this message

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