POV-Ray : Newsgroups : povray.advanced-users : testing if a provided string matches strings in array : Re: testing if a provided string matches strings in array Server Time
1 Jul 2024 06:03:55 EDT (-0400)
  Re: testing if a provided string matches strings in array  
From: Chris B
Date: 30 Sep 2009 07:41:27
Message: <4ac343e7@news.povray.org>
"Chris B" <nom### [at] nomailcom> wrote in message 
news:4ac32dc1@news.povray.org...
> But I have a more general concern that you're making life far more 
> complicated for yourself than it needs to be..........

I couldn't resist the temptation to fiddle with the code a little. The code 
below does the same thing in a way that I think is far simpler, using a 
single array index for the different arrays. I've split out the Location and 
Radius arrays so that locations can be stored as 2x3D vectors in a 2D array.

Doing this makes the macros you had so simple (2 or 3 lines each) that I've 
combined them into a single macro. I added a Type parameter to illustrate 
how you might be able to make this quite generic, potentially avoiding 
having to have separate macros for different shapes. You may find this 
approach useful when moving on to less primitive object types.

Regards,
Chris B.

#include "colors.inc"

/*+++++++++++++++ Fundamental declarations ++++++++++++++*/
#declare Floor_Surface = 0;

/*+++++++++++++++ Capturing Cylinders +++++++++++++++*/
#declare Component_ArraySize = 2000;
#declare Component_Index = -1;
#declare Component_Objects      = array[Component_ArraySize]    // Cylinder 
objects
#declare Component_Objects_Loc  = array[Component_ArraySize][2] // Locations
#declare Component_Objects_Rad  = array[Component_ArraySize]    // Radii
#declare Component_Objects_Id   = array[Component_ArraySize]    // Id
#declare Component_Objects_Type = array[Component_ArraySize]    // Type

// Save object
#macro Save_Object (Name, Type, Obj, Start, End, Radius)
  #declare Component_Index = Component_Index+1;
  #declare Component_Objects[Component_Index] = Obj;
  #declare Component_Objects_Loc[Component_Index][0] = Start;
  #if (strcmp(Type,"Cylinder")=0 | strcmp(Type,"Cone")=0)
    #declare Component_Objects_Loc[Component_Index][1] = End;
  #end
  #declare Component_Objects_Rad[Component_Index]    = Radius;
  #declare Component_Objects_Id[Component_Index] = Name;
  #declare Component_Objects_Type[Component_Index] = Type;
#end

/*+++++++++++++++ building a Cylinder ++++++++++++++++*/
#macro Make_Cylinder( Name, Radius, Height )
  #local Base_PointX = 0;                //x value at base of cylinder
  #local Base_PointY = Floor_Surface;    //y value at base of cylinder
  #local Base_PointZ = 0;                //z value at base of cylinder
  #local Cap_PointX = 0;                 //x value at top of cylinder
  #local Cap_PointY = Base_PointY + Height; //y value at top of cylinder
  #local Cap_PointZ = 0;                 //z value at top of cylinder
  #local Component_Radius = Radius;       //radius of cylinder
  #local Base_Point = <Base_PointX, Base_PointY, Base_PointZ>;
  #local Cap_Point = <Cap_PointX, Cap_PointY, Cap_PointZ>;
  #local Component_Obj = cylinder{ Base_Point, Cap_Point, Component_Radius }

  Save_Object(Name, "Cylinder", Component_Obj, Base_Point, Cap_Point, 
Radius)
#end

/*+++++++++++++++ Manupulating a Cylinder ++++++++++++++++*/
#macro Put_Cylinder( Name, XPos, YPos, ZPos )
  #local Count = 0;
  #while ( Count < Component_Index)
    #if( strcmp( Component_Objects_Id[ Count ], Name)=0)
      Component_Objects[Count]
      #debug concat("Match: ",Component_Objects_Id[ Count ],"\n")
    #else
      #debug concat("No Match: ",Component_Objects_Id[ Count ],"\n")
    #end
    #declare Count = Count + 1;
  #end
#end

/*++++++++++++++++++++Start test commands+++++++++++++++++++*/
light_source{ <-20, 400, -20> color White }
camera{ location <20, 20, -30> look_at 0 }
plane{ y, 0 pigment{ color Yellow }}


Make_Cylinder( "FirstCyl", 5, 10 ) // create first cylinder
Make_Cylinder( "SecondCyl", 10, 6 ) // create second cylinder... testing
Put_Cylinder( "FirstCyl", 0, 0, 0 ) //put cylinder in scene without 
reference


Post a reply to this message

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