POV-Ray : Newsgroups : povray.advanced-users : user defined function Server Time
31 Oct 2024 16:15:28 EDT (-0400)
  user defined function (Message 1 to 10 of 14)  
Goto Latest 10 Messages Next 4 Messages >>>
From: Leo80s
Subject: user defined function
Date: 11 Jan 2006 06:10:01
Message: <web.43c4e70d47b9b0fe72071f7b0@news.povray.org>
Hello everybody,

I need to understand exactly how can I specify in POV-RAY a
user-defined-function...I read many guide and tutorial and I have not
understand if it's possible or not...for example: I need to define a
function that take 3 parameters (a,b,c) and give in output sqrt
(a^2+b^2+c^2)...Can somebody send me an example and tell me exactly where I
have to put this function's code in a povray source file?

Thanks a lot,
Leo


Post a reply to this message

From: kurtz le pirate
Subject: Re: user defined function
Date: 11 Jan 2006 06:25:40
Message: <43c4eb34$1@news.povray.org>

web.43c4e70d47b9b0fe72071f7b0@news.povray.org...
> Hello everybody,
>
> I need to understand exactly how can I specify in POV-RAY a
> user-defined-function...I read many guide and tutorial and I have 
> not
> understand if it's possible or not...for example: I need to define a
> function that take 3 parameters (a,b,c) and give in output sqrt
> (a^2+b^2+c^2)...Can somebody send me an example and tell me exactly 
> where I
> have to put this function's code in a povray source file?

#macro myFunction(a,b,c)
    sqrt(a*a+b*b+c*c)
#end



...
#declare foo=myFunction(3,4,5);

hope that help,
klp


Post a reply to this message

From: Warp
Subject: Re: user defined function
Date: 11 Jan 2006 07:43:21
Message: <43c4fd69@news.povray.org>
Leo80s <nomail@nomail> wrote:
> I need to understand exactly how can I specify in POV-RAY a
> user-defined-function...I read many guide and tutorial and I have not
> understand if it's possible or not...for example: I need to define a
> function that take 3 parameters (a,b,c) and give in output sqrt
> (a^2+b^2+c^2)...Can somebody send me an example and tell me exactly where I
> have to put this function's code in a povray source file?

#declare MyFunction = function(a,b,c) { sqrt(a*a+b*b+c*c) };

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: user defined function
Date: 11 Jan 2006 07:44:06
Message: <43c4fd96@news.povray.org>
kurtz_le_pirate <kur### [at] yahoofr> wrote:
> #macro myFunction(a,b,c)
>     sqrt(a*a+b*b+c*c)
> #end

  There's a difference between macros and user-defined functions.

-- 
                                                          - Warp


Post a reply to this message

From: Leo80s
Subject: Re: user defined function
Date: 11 Jan 2006 11:50:01
Message: <web.43c53669ca20965f443403d30@news.povray.org>
Warp <war### [at] tagpovrayorg> wrote:
> kurtz_le_pirate <kur### [at] yahoofr> wrote:
> > #macro myFunction(a,b,c)
> >     sqrt(a*a+b*b+c*c)
> > #end
>
>   There's a difference between macros and user-defined functions.
>
> --
>                                                           - Warp

Can you explain exactly in what macros and user defined function are
different, lease?
thanks Leo


Post a reply to this message

From: Leo80s
Subject: Re: user defined function
Date: 11 Jan 2006 12:40:00
Message: <web.43c54273ca20965f443403d30@news.povray.org>
Hello , I need an help...

I can't understand the way I could specify the value returned by a function
(or a macro?!)...isn't there something similar a return statement?!?!

for example, how can I write a function that take an integer as parameter
and give in output the value of this integer if it's positive and 0
otherwise???

Leo


Post a reply to this message

From: Mike Williams
Subject: Re: user defined function
Date: 11 Jan 2006 13:25:10
Message: <wT7frDAUxUxDFwYL@econym.demon.co.uk>
Wasn't it Leo80s who wrote:
>
>how can I write a function that take an integer as parameter
>and give in output the value of this integer if it's positive and 0
>otherwise???

#declare myFunction = function(a){select (a, 0, a)}


-- 
Mike Williams
Gentleman of Leisure


Post a reply to this message

From: kurtz le pirate
Subject: Re: user defined function
Date: 11 Jan 2006 13:47:16
Message: <kurtzlepirate-2E0094.19471611012006@news.povray.org>
In article <43c4fd96@news.povray.org>, Warp <war### [at] tagpovrayorg> 
wrote:

> kurtz_le_pirate <kur### [at] yahoofr> wrote:
> > #macro myFunction(a,b,c)
> >     sqrt(a*a+b*b+c*c)
> > #end
> 
>   There's a difference between macros and user-defined functions.

oooops, you'r right... read too quick, answer too quick :))
klp


Post a reply to this message

From: Warp
Subject: Re: user defined function
Date: 11 Jan 2006 19:03:53
Message: <43c59ce6@news.povray.org>
Leo80s <nomail@nomail> wrote:
> Can you explain exactly in what macros and user defined function are
> different, lease?

  Macros are a lot more generic entities than functions. While a macro
can be used as if it was a function (except in certain cases), it can be
used for a lot more. For example, you can make a macro which creates an
object (something which a user-defined function cannot do).
  In a way, you can think about a macro as if it was a subroutine call
in a programming language.

  A user-defined function resembles a lot more a mathematical function:
It takes numerical parameters and it returns a numerical value (based on
mathematical operations performed on these input values), and it basically
cannot do anything else. You cannot use a user-defined function for example
to create an object.

  For example:

#macro CreateSphere(Location, Radius)
  sphere { Location, Radius }
#end

  This is not a mathematical function. It doesn't even return a numerical
value. It just creates a sphere. Macros don't really have a return value
per se.
  Macros work almost like substitution macros in C: It's almost as if
the macro call was substituted by the contents of the macro.

  You can't do that with user-defined functions. These can only be used
for calculating a mathematical operation, and the return value of such
function is the result of the operation in question.

  You can often simulate a function with a macro. For example:

#macro Length(A, B, C)
  sqrt(A*A+B*B+C*C)
#end

  This will work a lot like a function. You can call it like Length(1,2,3)
(you can think about it as if the call in question is substituted by the
contents of the macro).

  That is basically the equivalent of:

#define Length = function(A,B,C) { sqrt(A*A+B*B+C*C) };

  Then what is the difference? Why have user-defined functions at all
since macros can be used for the same thing?

  For one, user-defined functions are a whole lot faster. If you need to
call such a function millions of times, it's a whole lot faster to do it
with a user-defined function than with a macro.

  Secondly, user-defined functions can be used in some render-time
features, more specifically isosurfaces and parametric surfaces, while
macros cannot (macros are purely parse-time entities and they simply
don't exist at render time).

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: user defined function
Date: 11 Jan 2006 19:08:59
Message: <43c59e1a@news.povray.org>
Leo80s <nomail@nomail> wrote:
> I can't understand the way I could specify the value returned by a function
> (or a macro?!)...isn't there something similar a return statement?!?!

  The return value of the function is directly the evaluated expression
inside it. You don't have to "return" it explicitly.
  If you write:

#declare Func = function(A) { A*A };

then the return value of Func will be its parameter squared.

  With macros things get slightly more complicated. If you have just
a simple mathematical expression as the body of the macro, that serves
as its "return value". For example:

#macro Func(A)
  A*A
#end

  If the macro contains control flow commands, you have to use a small
trick to put a "return value". It works for example like this:

#macro Func(A)
  #if(A < 0)
    #local Result = -A;
  #else
    #local Result = A;
  #end
  A
#end

  The value of the last line of this macro is what is ultimately used as
the "substitution" of the macro call, which is practice is its "return
value".

-- 
                                                          - Warp


Post a reply to this message

Goto Latest 10 Messages Next 4 Messages >>>

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