POV-Ray : Newsgroups : povray.general : help w/simple macro Server Time
1 Nov 2024 18:18:06 EDT (-0400)
  help w/simple macro (Message 1 to 8 of 8)  
From: ross litscher
Subject: help w/simple macro
Date: 19 May 2000 16:36:30
Message: <3925A6B6.CB89CD94@osu.edu>
Hi, ive recently been trying to ween myself off modellers and do some
hand coding. So I made a simple macro as practice, well.. it's short so
here it is. (my_spike is just a cone really)

#macro Make_Grid (spacing, size)

	#local grid = 0;
	#while (grid < size)
	
		#local zee = spacing * grid;
		#local copy = 0;
	
		#while (copy < size)
		object {
			my_spike
			translate <spacing*copy, 0, zee> 
			}
		#local copy = copy + 1;
		#end	

	#local grid = grid + 1;
	#end
	//A cheap fix for it not putting a cone at <0,0,0>
	object { my_spike }
#end

then i do:

#declare Grid_1 = Make_Grid(.5, 2)

and this renders and all. But how do I do anything with Grid_1 now? Say
I want to 
rotate it 45*x and translate it 3*y. 

Am I doing things at all close to correct? Thanks for the help...


Ross


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: help w/simple macro
Date: 19 May 2000 17:44:50
Message: <3925B53F.DD41BC62@hotmail.com>
ross litscher wrote:
> 
> Hi, ive recently been trying to ween myself off modellers and do some
> hand coding. So I made a simple macro as practice, well.. it's short so
> here it is. (my_spike is just a cone really)
> 
> #macro Make_Grid (spacing, size)
> 
>         #local grid = 0;
>         #while (grid < size)
> 
>                 #local zee = spacing * grid;
>                 #local copy = 0;
> 
>                 #while (copy < size)
>                 object {
>                         my_spike
>                         translate <spacing*copy, 0, zee>
>                         }
>                 #local copy = copy + 1;
>                 #end
> 
>         #local grid = grid + 1;
>         #end
>         //A cheap fix for it not putting a cone at <0,0,0>
>         object { my_spike }
> #end
> 
> then i do:
> 
> #declare Grid_1 = Make_Grid(.5, 2)
> 
> and this renders and all. But how do I do anything with Grid_1 now? Say
> I want to
> rotate it 45*x and translate it 3*y.
> 
> Am I doing things at all close to correct? Thanks for the help...
> 
> Ross


I have not tried your code, 
but as far as I can see it looks OK.

Now you just have to change your last #declare to this

#declare Grid_1 = 
union {
  Make_Grid(.5, 2)
  rotate 45*x
  translate 3*y
}



You could also have done this:

#macro Make_Grid(spacing, size)

  union {
    // place the rest of your 
    // macro code here
  }

#end // macro Make_Grid


And then the last declare could/should 
be done like this:

#declare Grid_1 = 
object {
  Make_Grid(.5, 2)
  rotate 45*x
  translate 3*y
}




And (of coarse) you can use other CSG operators instead of union


Tor Olav
--
mailto:tor### [at] hotmailcom
http://www.crosswinds.net/~tok/tokrays.html


Post a reply to this message

From: ross litscher
Subject: Re: help w/simple macro
Date: 19 May 2000 18:03:59
Message: <3925BB39.44CB3208@osu.edu>
Tor Olav Kristensen wrote:

> I have not tried your code,
> but as far as I can see it looks OK.
> 
> Now you just have to change your last #declare to this
> 
> #declare Grid_1 =
> union {
>   Make_Grid(.5, 2)
>   rotate 45*x
>   translate 3*y
> }
> 
> Tor Olav



Hey, thanks! I'm off to try it out.

Ross


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: help w/simple macro
Date: 19 May 2000 18:47:18
Message: <3925C3DF.4A4F0913@hotmail.com>
Tor Olav Kristensen wrote:
> 
> Now you just have to change your last #declare to this
> 
> #declare Grid_1 =
> union {
>   Make_Grid(.5, 2)
>   rotate 45*x
>   translate 3*y
> }
> 

Now I have read your question thoroughly.
(as I should have done in the first place :)

And I have also tested your code.

Below you find the code for my new suggestion.
The "big" difference is the passing of the 
spike shape trough a variable; "Thing" to 
the macro.

(POV-Ray does not force you to do this, but
I think this is good programming practice.)


#macro Make_Grid(Thing, spacing, size)

  #local xCnt = 0;
  #while (xCnt < size)
    #local zCnt = 0;
    #while (zCnt < size)
      object {
        Thing
        translate spacing*<xCnt, 0, zCnt> 
      }
      #local zCnt = zCnt + 1;
    #end    
    #local xCnt = xCnt + 1;
  #end

#end


#declare Spike = cone { <0, 0, 0>, 0.2, <0, 1, 0>, 0 }

#declare Grid_1 = 
union {
  Make_Grid(Spike, 0.5, 6)
  pigment { color Yellow }
}

object {
  Grid_1
  rotate 45*x
  translate 3*y
}


Tor Olav
--
mailto:tor### [at] hotmailcom
http://www.crosswinds.net/~tok/tokrays.html


PS:
You can also have the macro apply changes 
to the Thing object with #declare.
E.g.: In the macro you can say 

#declare Thing = object { Thing scale <1, 2, 1> }

And then after you have called the macro
the Spike will be twice as high as before.


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: help w/simple macro
Date: 19 May 2000 19:14:05
Message: <3925CA25.AA9536C4@hotmail.com>
Tor Olav Kristensen wrote:
> 
> Below you find the code for my new suggestion.
> The "big" difference is the passing of the
> spike shape trough a variable; "Thing" to
> the macro.
> 
> (POV-Ray does not force you to do this, but
> I think this is good programming practice.)
> 

I forgot to mention that this enables you to 
have the macro accept other shapes too.

Now you can do things like this:


#declare Spike = cone { <0, 0, 0>, 0.2, <0, 1, 0>, 0 }

#declare Grid_1 = 
union {
  Make_Grid(Spike, 0.5, 6)
}

#declare Grid_2 = 
union {
  Make_Grid(sphere { y*0.8, 0.3 }, 0.5, 6)
}


difference {
  object { Grid_1 }
  object { Grid_2 }
  rotate 45*x
  translate 3*y
  pigment { color Red }
}


Tor Olav
--
mailto:tor### [at] hotmailcom
http://www.crosswinds.net/~tok/tokrays.html


Post a reply to this message

From: ross litscher
Subject: Re: help w/simple macro
Date: 19 May 2000 21:20:47
Message: <3925E95A.4157ED94@osu.edu>
Tor Olav Kristensen wrote:
>
> Now I have read your question thoroughly.
> (as I should have done in the first place :)
> 
> And I have also tested your code.
> 
> Below you find the code for my new suggestion.
> The "big" difference is the passing of the
> spike shape trough a variable; "Thing" to
> the macro.
> 
> (POV-Ray does not force you to do this, but
> I think this is good programming practice.)
> 

I agree. I had thought about it since your first post. The one thing
that is not entirely intuitive to me is this part 

#declare Grid_1 = 
union {
  Make_Grid(Spike, 0.5, 6)
  pigment { color Yellow }
}

object {
  Grid_1
  rotate 45*x
  translate 3*y
}


Now if I wanted two "Grids" I'd have to do another #declare Grid_2...
etc., and another object {Grid_2 ...}. Now this is how I thought it
would work. especially after reading (and misunderstanding, i presume)
the section in the povray docs about macros as functions that return
some value.

#declare Grid_1 = object{ Make_Grid(Spike, 0.5, 6) }

and then I hoped to be able to do something like 

object {
	Grid_1
	rotate 45*x
	translate 3*y
	}

I'm pretty sure I tried this, and I'm pretty sure it didn't do what I
expected. What I expected was what your initial solution did.

But anyway, this is all probably just a result of me not knowing the
language completely. 


Thanks for your help,
Ross


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: help w/simple macro
Date: 20 May 2000 21:15:04
Message: <392737B3.F4C86EF1@hotmail.com>
ross litscher wrote:

8< .... snip ...

> The one thing
> that is not entirely intuitive to me is this part
> 
> #declare Grid_1 =
> union {
>   Make_Grid(Spike, 0.5, 6)
>   pigment { color Yellow }
> }
> 
> object {
>   Grid_1
>   rotate 45*x
>   translate 3*y
> }
> 
> Now if I wanted two "Grids" I'd have to do another #declare Grid_2...
> etc., and another object {Grid_2 ...}. Now this is how I thought it
> would work. especially after reading (and misunderstanding, i presume)
> the section in the povray docs about macros as functions that return
> some value.
> 
> #declare Grid_1 = object{ Make_Grid(Spike, 0.5, 6) }
> 
> and then I hoped to be able to do something like
> 
> object {
>         Grid_1
>         rotate 45*x
>         translate 3*y
>         }
> 
> I'm pretty sure I tried this, and I'm pretty sure it didn't do what I
> expected. What I expected was what your initial solution did.
> 
> But anyway, this is all probably just a result of me not knowing the
> language completely.


You have to be aware that in POV-Ray macros does not behave
like functions in other languages. 

(Someone please correct me if I'm wrong about the following:)

Macros in POV-Ray does not return function values. 

They just return text that is to be pasted in where the 
macro is called.

So the macro you posted; 

#macro Make_Grid (spacing, size)

  #local grid = 0;
  #while (grid < size)
    #local zee = spacing * grid;
    #local copy = 0;
    #while (copy < size)
      object {
        my_spike
        translate <spacing*copy, 0, zee> 
      }
      #local copy = copy + 1;
    #end
    #local grid = grid + 1;
  #end
  //A cheap fix for it not putting a cone at <0,0,0>
  object { my_spike }

#end

just generated a lot of "spike" objects. And these were
pasted in to the part of the code that called the macro:
#declare Grid_1 = Make_Grid(.5, 2)


Like this:
(The carriage returns, spaces and the extra .0 are 
of coarse not correct.)

#declare Grid_1 = object {
                    my_spike
                    translate <0.0, 0, 0.0> 
                  }
                  object {
                    my_spike
                    translate <0.5, 0, 0.0>
                  }
                  object {
                    my_spike
                    translate <0.0, 0, 0.5>
                  }
                  object {
                    my_spike
                    translate <0.5, 0, 0.5>
                  }
                  object {
                    my_spike
                  }

As you see the first spike object is assigned to 
the Grid_1 variable and the others are just left 
to be rendered were they now are.

(This is why there was missing a cone at <0, 0, 0>
and I believe that this lead you to do the "cheap fix":
To add a last cone at this position (origo). :)

Now the Grid_1 variable contains a single cone 
object and that object is rotated and translated 
when one does this:

object {
  Grid_1
  rotate 45*x
  translate 3*y
}

So then one ends up with 5 cones:

1 rotated and translated 
4 in the xz-plane. 


Then I came and suggested to put the code that 
called the macro into a union operator:

#declare Grid_1 = 
union {
  Make_Grid(.5, 2)
  rotate 45*x
  translate 3*y
}


This time the result of calling the macro 
is this:

#declare Grid_1 = 
union {
  object {
    my_spike
    translate <0.0, 0, 0.0>
  }
  object {
    my_spike
    translate <0.5, 0, 0.0>
  }
  object {
    my_spike
    translate <0.0, 0, 0.5>
  }
  object {
    my_spike
    translate <0.5, 0, 0.5>
  }
  object {
    my_spike
  }
  rotate 45*x
  translate 3*y
}

The Grid_1 variable now contains a union of all 
the 5 cone objects. And note that now they are 
all rotated and translated away from the xz-plane
together.

Now all that is needed to make this new object
visible is to just write the name of the variable 
that holds it.

Like this:

Grid_1


If you want to do further "operations" on this 
object, then just put the name of the variable
inside an object declaration and apply more 
"operations" to it.

E.g.:

object {
  Grid_1
  pigment { color Yellow }
}


or:


difference {
  object { Grid_1 translate  y/16 }
  object { Grid_1 translate -y/16 }
  pigment { color Red }
}
  

The fact that macros returns text instead of
function values can be exploited in other 
ways than I have shown here.

E.g.:
One cane make macros that passes arrays
of 2D points to a lathe shape.

Instead of having to type in all the points 
manually inside the shape statement, the 
macro reads them out of an array (that is 
passed to the macro) and pastes them into 
the brackets of a lathe statement with all 
the necessary commas.

I can show you examples of how to do 
things like this with macros if you are 
interested.


Hope this helped.


Regards,

Tor Olav
--
mailto:tor### [at] hotmailcom
http://www.crosswinds.net/~tok/tokrays.html


Post a reply to this message

From: ross litscher
Subject: Re: help w/simple macro
Date: 21 May 2000 23:21:24
Message: <3928A8A2.639439E3@osu.edu>
Tor Olav Kristensen wrote:

::snip::

> 
> I can show you examples of how to do
> things like this with macros if you are
> interested.
> 
> Hope this helped.
> 
> Regards,
> 
> Tor Olav
> --
> mailto:tor### [at] hotmailcom
> http://www.crosswinds.net/~tok/tokrays.html


wow. your help has been greatly appreciated to say the least. Thankyou.
I'm going to have to read all your posts now for every other topic :)


Of course i'm interested! But i'll just keep messing around with things
for now. 

thanks again,
ross


Post a reply to this message

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