POV-Ray : Newsgroups : povray.advanced-users : Vector handling : Re: Vector handling Server Time
7 Jun 2025 13:04:39 EDT (-0400)
  Re: Vector handling  
From: Tor Olav Kristensen
Date: 3 Jun 2025 23:30:00
Message: <web.683fbcb92f29ff143a63f5189db30a9@news.povray.org>
"Bald Eagle" <cre### [at] netscapenet> wrote:
> In light of the fact that I was able to solve the scalar / vector typing
> problem,  I wanted to do a test of the macro with a full complement of vectors
> that are composed of all permutations of negative, zero, and positive values, so
> that I can test anything that might trigger some edge case and cause the macro
> to fail and misidentify an argument.
>
> To do that, I wrote a macro to generate all of the permutations for any given
> vector size, however it quickly became apparent that actually outputting vectors
> of any given size is a challenge.
>
> There's no way to really assign a value to the individual vector component
> Vector.z, so I'm coming up against an interesting limitation of SDL.  It's not a
> trivial task to take an array of values and convert it to a vector.
> Nor can I (that I know) diminish the size of a vector from 5 to 3.
>
> (Can we have 1-component vectors?)
>
> I don't want to use ParseString () to generate 4,000 vectors.
> I also don't want to individually verify every test case, so I'll probably make
> the output of the macro a scalar/vector plus the type (1, 2, 3, 4, 5) and have
> the identification macro compare, and sum any errors.
>
> Can anyone suggest a robust method for composing vectors of sizes 2 through 5
> with a single, general purpose macro?

Hi Bill

Could this be something to work with?

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#version 3.7;

global_settings { assumed_gamma 1.0 }

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#macro PrintSCalarOrVector(Dim, SV)

    #switch (Dim)
        #case (1)
            #debug str(SV, 0, -1)
            #break
        #case (2)
            #debug concat("<", vstr(2, SV, ", ", 0, -1), ">")
            #break
        #case (3)
            #debug concat("<", vstr(3, SV, ", ", 0, -1), ">")
            #break
        #case (4)
            #debug concat("<", vstr(4, SV, ", ", 0, -1), ">")
            #break
        #case (5)
            #debug concat("<", vstr(5, SV, ", ", 0, -1), ">")
            #break
        #else
            #error "Not a valid vector dimension"
    #end // switch

#end // macro PrintSCalarOrVector


#debug "\n"

PrintSCalarOrVector(1, 1.0)
#debug "\n"

PrintSCalarOrVector(2, <2.0, 3.0>)
#debug "\n"

PrintSCalarOrVector(3, <3.0, 4.0, 5.0>)
#debug "\n"

PrintSCalarOrVector(4, <4.0, 5.0, 6.0, 7.0>)
#debug "\n"

PrintSCalarOrVector(5, <5.0, 6.0, 7.0, 8.0, 9.0>)
#debug "\n"


#macro CreateScalarOrVector(ValueArray)

    #local Dim = dimension_size(ValueArray, 1);
    #switch (Dim)
        #case (1)
            #local SV =
                ValueArray[0]
            ;
            #break
        #case (2)
            #local SV =
                <
                    ValueArray[0],
                    ValueArray[1]
                >
             ;
            #break
        #case (3)
            #local SV =
                <
                    ValueArray[0],
                    ValueArray[1],
                    ValueArray[2]
                >
            ;
            #break
        #case (4)
            #local SV =
                <
                    ValueArray[0],
                    ValueArray[1],
                    ValueArray[2],
                    ValueArray[3]
                >
            ;
            #break
        #case (5)
            #local SV =
                <
                    ValueArray[0],
                    ValueArray[1],
                    ValueArray[2],
                    ValueArray[3],
                    ValueArray[4]
                >
            ;
            #break
        #else
            #error "Not a valid vector dimension"
    #end // switch

    SV

#end // macro CreateScalarOrVector


#debug "\n"

#declare VS =
    CreateScalarOrVector(
        array[1] { 1.0 }
    )
;
PrintSCalarOrVector(1, VS)
#debug "\n"

#declare VS =
    CreateScalarOrVector(
        array[2] { 2.0, 3.0 }
    )
;
PrintSCalarOrVector(2, VS)
#debug "\n"

#declare VS =
    CreateScalarOrVector(
        array[3] { 3.0, 4.0, 5.0 }
    )
;
PrintSCalarOrVector(3, VS)
#debug "\n"

#declare VS =
    CreateScalarOrVector(
        array[4] { 4.0, 5.0, 6.0, 7.0 }
    )
;
PrintSCalarOrVector(4, VS)
#debug "\n"

#declare VS =
    CreateScalarOrVector(
        array[5] { 5.0, 6.0, 7.0, 8.0, 9.0 }
    )
;
PrintSCalarOrVector(5, VS)
#debug "\n"

#debug "\n"
#error "No error, just finished"


--
Tor Olav
http://subcube.com
https://github.com/t-o-k


Post a reply to this message

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