Results 1 to 21 of 21

Thread: Radian LookUp Table

  1. #1

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Radian LookUp Table

    When working with quaternions/vectors/matrices/rotation of you require alot cos(angle)/sin(angle).

    Now, it seems best to work in radians all the time and try to keep your numbers in radians; avoid converting into degree's.

    Right now I have a lookup table which looks like this:
    //----------------
    //Generate Vector Circles
    //----------------
    void Camera::GenerateVectorCircle()
    {
    float Theta=0;
    float Pi = 3.14159265358979f;
    float ToRad = Pi / 180;

    for (int i = 0;i < 360;i++)
    {
    Theta = i * ToRad;
    m_VectorCircle[i].u = cosf(Theta);
    m_VectorCircle[i].v = sinf(Theta);
    }
    }


    This table seems to work fine but I'm sure it can't be 100% accurate unless...
    Here is my question: between 0 --> 2pi (thus 0 --> 360)...Is there the same number of value's between 0 --> 2pi and 0 --> 360?

    From a glance, obviously not.
    So I must create a look up table to eliminate the use of sin and cos on a regular basis, but the look up table must have contain values for any angle in rads.

    Is this possible? Between 0 and 2pi there are probably a quaddrillion values I would have to record for cos and sin on each angle.

    So is accuracy lost if I just use that lookup table above and ALWAYS work in degree's?

    Maybe this post is pointless.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  2. #2

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: Radian LookUp Table

    The amount variable isa in degree.
    Problem is..i convert it to rads the divide it by 2 then sin/cos it.
    Because it is being divided by 2 means i cant use degree lookup table or i'll loose accuracy.
    20 / 2 = 10...works
    21 / 2 = 10.5 which could end up as 10 or 11
    // Convert the angle degrees into radians.
    float angle = GET_RADIANS(amount);

    float sine = (float)sin(angle / 2.0f);

    // Create the quaternion.
    x = xAxis * sine;
    y = yAxis * sine;
    z = zAxis * sine;
    w = (float)cos(angle / 2.0f);
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  3. #3
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Radian LookUp Table

    I don't get it. Is this a trick question?
    There are the same number of angles between 0 and 360 as there are between 0 and 2Pi. No matter what units you are working in (degrees/rads) you will still get increments of 1 degree based on how you are making your table. If you want better accuracy, you need to double the array size and

    for (int i = 0;i < 720;i++)
    {
    Theta = i * ToRad * 0.5f;
    m_VectorCircle[i].u = cosf(Theta);
    m_VectorCircle[i].v = sinf(Theta);
    }
    }
    just be sure to work with floats and becareful that (floatX/floatY) * floatY may not equal floatX
    HTH kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  4. #4

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: Radian LookUp Table

    I want to get sin and cos functions out of this function by using a lookup table.

    Do you see the problem yet?

    //-------------------
    //Rotatef -- Thanks to Allen Sherrod for the idea/guideline
    //-------------------
    void Quaternion::Rotatef(float amount, float xAxis, float yAxis, float zAxis)
    {
    // Convert the angle degrees into radians.
    float angle = DEGTORAD(amount);

    // Call this once for optimization, just like using the if statement to determine if
    // we should normalize.
    float sine = sinf(angle / 2.0f);

    // Create the quaternion.
    x = xAxis * sine;
    y = yAxis * sine;
    z = zAxis * sine;
    w = cosf(angle / 2.0f);

    // Normalize the quaternion.
    float length = 1 / (float)sqrt(x * x + y * y + z * z + w * w);
    x *= length;
    y *= length;
    z *= length;
    }
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  5. #5
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Radian LookUp Table

    Do you see the problem yet?
    no sorry
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  6. #6

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: Radian LookUp Table

    You can't have an array which takes floats. There is no such thing as VectorCircle[2.453564];
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  7. #7
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Radian LookUp Table

    o, why didn't you say that to begin with You will obviously need to round the argument first. Then you need to decide whether you want 1 degree of accuracy or 1/2 degree or whatever. You then need to construct your array accordingly. If you want one degree accuracy then you array needs 360 points, if 1/2 degree accuracy, 720 points, etc. Then you need to adjust the argument to the function
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  8. #8

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: Radian LookUp Table

    Yah i guess thats all I can do eh.

    Well thanks for your idea.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  9. #9
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Radian LookUp Table

    Just remember to not make it tooo big, then you will get to many cash misses, and it will be even slower then the sin function...

  10. #10
    Junior Member
    Join Date
    Dec 2004
    Location
    Örebro, Sweden
    Posts
    29

    Re: Radian LookUp Table

    maybe you could make it like, when you want to look up radian [2.453564] you can defins VectorCircle as [2453564] or something? and then divide it with 1 000 000? Or would that make it a toooo big array (well I suppose you don't need so many decimals?)

  11. #11

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: Radian LookUp Table

    This is a great idea and in fact may work to an extent.
    Although, VectorCircle at the least has to be a struct of 2 floats, u and v to store the cos and sin values.
    So its size in bytes would be at least 8...maybe 12 if you wrap it up in a class.

    An array 2453564 big would be:
    2,453,564 * 8 = 19,628,512 bytes = 19mb of ram zapped

    Trimming it down to less decimal points would sure help.

    Truly the problem I have is that I CANNOT run any trig functions in the main game loop...they are WAY to slow.
    So I gotta compile a lookup table. I now loose the extreme precision of radians because I gotta go Pi * Angle/180 -- then the decimal point is cut off because it is stored in an int...
    So I simply do a few things in degree's because of this.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  12. #12
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Radian LookUp Table

    Why not just wrap an array in a function and pass a float to that and do some internal processing.

    Also: Default properties of classes can take floats as arguments (as well as anything else in fact.
    I don't live here any more.

  13. #13

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: Radian LookUp Table

    Yes but:
    Vector *m_VectorCircle
    m_VectorCirlce = new Vector[360]
    m_VectorCircle[2.56] <<-- error. You HAVE to use integers when diving through arrays. You can probably bitshift or increment the points by a float...but that probably won't do you any good.
    delete [] m_VectorCircle
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  14. #14
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Radian LookUp Table

    Maybe I'm a bit easy minded, but:

    I use only degrees in my prog.
    I figured a lookup-table for every tenth of a degree gives enough accuracy.

    So I use something like:
    Create a LookUptable(array) for Sin(0 ...3599) and Cos(0 ..3599). The conversion from Rad is done in there.
    Make a Function that takes degree as input and gives the Sinus/Cosinus as output. This Function uses the lookuptable and takes care of the multiplication from 1/10 degree to 1 degree.

    So I only use a Sinus or Cosinus Function in my program that take degree as input!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  15. #15

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: Radian LookUp Table

    Thats pretty what I'm doing but now I only get a 0-360 range of movement.
    Its not a bad idea.
    I suppose.


    Also to clarify with my last post.
    I believe you overload the [] operator in a class with a float.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  16. #16
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Radian LookUp Table

    Sorry forgot to mention, when calling the sinus or Cosinus function, I take degree as input, but I only use it as an integer! In my application (where I'm using 1/10 degree resolution) I do something like
    VB Code:
    1. Sinus=SinusFromLookTableUpValue (Int(Degree*10))
    the SinusFromLookupTable holds Values for each 1/10 degree.
    That way I do not overload!!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  17. #17

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: Radian LookUp Table

    Thats very intelligent.

    Thanks for all the replies.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  18. #18
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Radian LookUp Table

    Quote Originally Posted by Halsafar
    Thats very intelligent.

    I wish my math-teachers would have said that, back then..........
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  19. #19
    New Member
    Join Date
    Nov 2005
    Posts
    2

    Re: Radian LookUp Table

    Hi folks,

    how can I archieve the same result with asin,acos, atan and atan2 functions?
    I have to use them quite a lot, of course those functions are quite slow...

    Due to the fact VB6 doesn't have for example the atan2 function I look in the web for an alternative and found something with asin and acos.

    Any ideas?

    Thanks in advance!

    Daniel

  20. #20
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Radian LookUp Table

    We could use such LookUp tables since our input-values (the degrees) were equally distributed, so we could use them as the array-counter in the table.
    In your case it would be more complicated.
    If you use an equally distributed counter for the asin ( between -1 and 1 for example 400 values), your accuracy would not be the same for all output values.

    I would use a lookup-table for sin and use it reverse, look thru all values in the table, the desired result is the indexof the closest value.
    But I don't know if that would be faster than calculation!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  21. #21
    New Member
    Join Date
    Nov 2005
    Posts
    2

    Re: Radian LookUp Table

    Okay, I'll try that out. Thanks for your help! Really appreciated!!

    Daniel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width