POV-Ray : Newsgroups : povray.windows : Surface Coefficients??? : Re: Surface Coefficients??? Server Time
28 Jul 2024 18:14:19 EDT (-0400)
  Re: Surface Coefficients???  
From: Ken Cecka
Date: 11 Feb 1998 14:38:52
Message: <34e20b29.0@news.povray.org>
I can't claim authorship of this, but here's a perl script which will take a
fairly standard infix notation equation (as opposed to postfix/referse
polish or prefix), and spit out the pov code for you.  Someone posted it on
CGRR way back when and I've kept a copy handy.

An example session is as follows:

bash-2.01$ echo "x^2*y^2*z^2+x^2*z^2+x*y*z=0"|poveq
poly {
    6, <0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>
}

I know it's kind of finicky about spaces and such, but usually if you play
arround with it, you can get what you want.  The source file is listed
below.

Ken

----cut here---
#!/usr/bin/perl

while(<>)
{
    chop;
    $eq .= $_;
}

foreach $comp (split('\s*\+\s*',$eq))
{
    $comp =~ s/\s+//;
    $x=0; $y=0; $z=0; $coef=1;
    foreach $factor (split('\*',$comp))
    {
        if ($factor =~ /^[xyz]/)


            ($var,$exp) = ($factor =~ /^([xyz])\^?(\d+)?/);
            $exp = 1 if ($exp =~ /^$/);
            eval "\$$var = $exp";
        } else {
            $coef = $factor;
        }
    }
    $equation{$x,$y,$z} = $coef;
    $power = $x+$y+$z if ($x+$y+$z > $power);
}

print "poly {\n    $power, <";

$first = 1;
for($x=$power;$x>=0;$x--)
{
    for($y=$power-$x;$y>=0;$y--)
    {
        for($z=$power-$x-$y;$z>=0;$z--)
        {
            #print "\n$x,$y,$z\n";
            print ", " if (!$first);
            $first = 0;
            if ($equation{$x,$y,$z})
            {
                print "$equation{$x,$y,$z}";
            } else {
                print "0";
            }
        }
    }
}
print ">\n}\n";

------cut here-------



Ronald L. Parker wrote in message <34e### [at] 100233>...
>On 11 Feb 98 15:12:17 GMT, "Tristan Wibberley"
><tri### [at] compaqcom> wrote:
>
>>Look at how the 4th order goes
>>extend to 5th
>>
>>The numbers give which power of each of x, y and z are in each term.
>>To extend further, count down in base n (eg 6th order use base 6) from n00
>>to get each term.
>
>Not quite.  For example, there is no 440 term.  Only terms
>of order 6 or less are included.  Also, of course, you must
>include 600, 060, and 006, which are not legal numbers in
>base 6.  So the real answer is: count down (base 10) from
>777, and only include numbers where the sum of the digits
>is equal to or less than the order of the polynomial you're
>generating.  Of course, there are easier and more efficient
>ways to do it.
>
>Here's a perl program that will generate the terms in
>the correct order, suitable for including in a POV file.
>to use it on Unix, just call it "coeffs", make it mode 700,
>and type "coeffs 6 >coeffs.out" to create a file called
>"coeffs.out" containing the definition for a poly of order
>6.  Under Windows, you'll have to type
>"perl coeffs 6 >coeffs.out" and make sure perl.exe is in
>your path somewhere.  To find a recent port of Perl for
>your machine, see http://cpan.perl.org/ports/
>
>Programmers in other languages, feel free to borrow this
>and rewrite it for the Perl-deprived.  Windows and Mac
>users, hop over to povray.binaries.utilities and grab
>the template file I'll post over there shortly.
>
>----------- cut here ------>8==========
>#!/usr/bin/perl -w
>
>use strict;
>
>my $order = $ARGV[0];
>print "poly { $order, <\n";
>for ( my $x=$order; $x >= 0; $x-- ) {
>  for ( my $y=$order-$x; $y >= 0; $y-- ) {
>    for ( my $z=$order-$y-$x; $z >= 0; $z-- ) {
>      print '  0';
>      if ($x+$y+$z) {
>        print ', /* ';
>        print 'x' if ($x);
>        print $x if ($x>1);
>        print 'y' if ($y);
>        print $y if ($y>1);
>        print 'z' if ($z);
>        print $z if ($z>1);
>        print ' */';
>      }
>    print "\n";
>    }
>  }
>}
>
>print "> }\n";
>----------- cut here ------>8==========
>
>Here's a sample of the output, for order 3:
>
>poly { 3, <
>  0, /* x3 */
>  0, /* x2y */
>  0, /* x2z */
>  0, /* x2 */
>  0, /* xy2 */
>  0, /* xyz */
>  0, /* xy */
>  0, /* xz2 */
>  0, /* xz */
>  0, /* x */
>  0, /* y3 */
>  0, /* y2z */
>  0, /* y2 */
>  0, /* yz2 */
>  0, /* yz */
>  0, /* y */
>  0, /* z3 */
>  0, /* z2 */
>  0, /* z */
>  0
>> }
>


Post a reply to this message

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