Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 385
  • Last Modified:

3D rotation

All i need is a FAST procedure that will be given a x,y,z, lengths and : x angle, y angle and z angle and return the resoult of the rotation ( rotate a point X,Y,Z in angle Ax,Ay,Az ). the procedure can use any cos,sin table or matrixes or assembly - just make it fast. If you tell me your real name I'll give you credit for your work when ever i use the procedure.
The procedures declaration should be as simple as that :

Procedure Rotate3D(X,Y,Z:LongInt;AngX,AngY,AngZ:Integer Var resoultX,resoultY,resoultZ:LongInt);

Ofcource the procedure must use an integer values becouse real is way to slow for 3D real time uses ( the X,Y,Z values must be LongInt and not Integer )
0
yot
Asked:
yot
  • 4
  • 4
  • 3
  • +1
1 Solution
 
mitchell042997Commented:
I haven't tried implementing this in code, ao I know not if it is fast or slow, but I have a linear algebra text book which describes linear transformations in some detail.  You can rotate things simply by using linear transformations with sin and cos.  If you are interested in my posting the theory as an answer, I will gladly oblige.
0
 
interCommented:
To make it fast we should use lookup table for cosine and sine so please indicate the accuracy so that I can post the code including the lookup table(for example 0.5 degree rotation accuracy enough?)

Igor
0
 
yotAuthor Commented:
Igor,

0.5 degree would be very good. I'll be glad to have your code when ever you can give it to me !

Yotam.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
interCommented:
Dear zot; here is the story; (Sorry, I know you just one a proc but the word FAST makes things little bit more complicated)

To make things fast we should assume some model for our integer operations. I decided the following, if you need more accuracy let me know after trying:

- Assume we divide the 2*PI = 360 deg into 256 (2^8).
- Also, let our cos and sine values are scalled so that 1.0 coresponds to 16384 = 2^14 (we should do all this stuff to represent the divides as shr)

The test program and your source is

Dear zot; here is the story; (Sorry, I know you just one a proc but the word FAST makes things little bit more complicated)

To make things fast we should assume some model for our integer operations. I decided the following, if you need more accuracy let me know after trying:

- Assume we divide the 2*PI = 360 deg into 256 (2^8).
- Also, let our cos and sine values are scalled so that 1.0 coresponds to 16384 = 2^14 (we should do all this stuff to represent the divides as shr)

The look up table construction is

const
  num_div = 256;  
var
  CosN : array[0..num_div-1] of longint;
  SinN : array[0..num_div-1] of longint; {we may not need sine because Cos(N) = Sin(N+90) but I'd better do not complicate that much}

procedure PrepareLookUp;
var
  i : integer;
  M_PI : real;
begin
  M_PI := Pi; {Never use Pi, you know it is a function}
  for i := 0 to num_div-1 do
  begin
    CosN[i] := Trunc(cos(2*M_PI*i/num_div)*16384); {just scale to num_div}
    SinN[i] := Trunc(sin(2*M_PI*i/num_div)*16384);
  end;
end;

{Now lets rotate fast:

 Note that AngX, AngY, AngZ are the angles scaled to num_div; we should not scale it in the rotation because it is time critical inner loop}
Procedure Rotate3D(X,Y,Z:LongInt;AX,AY,AZ:Integer Var
                  rX,rY,rZ:LongInt);
var
  cx,cy,cz,sx,sy,sz : longint;
begin
  cx := CosN[AX]; cy := CosN[AY]; cz := CosN[AZ];
  sx := SinN[AX]; sy := SinN[AY]; sz := SinN[AZ];
  {rotate around x first}
  rX := X;
  rY := (Y*cx - Z*sx) * shr 14;
  rZ := (Y*sx + Z*cx) * shr 14;
  {rotate result around y then}
  rX := (rX*cy - rZ*sy) * shr 14;
  rZ := (rX*sy + rZ*cy) * shr 14;
  {rotate finaly round z}
  rX := (rX*cz - rY*sz) * shr 14;
  rY := (rX*sz + rY*cz) * shr 14;
end;

It is not very accurate and not too much speedy but the idea is this. If you have time write it in assembler to boostup speed. I test it in BP7.0 real mode so, since the instrucions are 16 bit the BP calls an internal procedure for longmultiplications. As I  said this is the idea. If I have time I try to convert it to assembler. By the way, are you using BP7.0?

Sincerely,
Igor
0
 
interCommented:
****************************************
*   OPPPPPPPSSSS!!!!!!!!!!!!!!
****************************************

This is the one sorry, I do not know what happened

program drot;

const
  num_div = 256;
  scale = 16384;
var
  CosN : array[0..num_div-1] of longint;
  SinN : array[0..num_div-1] of longint; {we may not need sine because Cos(N) = Sin(N+90)
  but I'd better do not complicate that much}

procedure PrepareLookUp;
var
  i : integer;
  M_PI : real;
begin
  M_PI := Pi; {Never use Pi, you know it is a function}
  for i := 0 to num_div-1 do
  begin
    CosN[i] := Trunc(cos(2*M_PI*i/num_div)*scale); {just scale to num_div}
    SinN[i] := Trunc(sin(2*M_PI*i/num_div)*scale);
  end;
end;

{Now lets rotate fast:

 Note that AngX, AngY, AngZ are the angles scaled to num_div; we should not scale it in the rotation because it is
 time critical inner loop}
Procedure Rotate3D(X,Y,Z:LongInt;AX,AY,AZ:Integer; Var
                  rX,rY,rZ:LongInt);
var
  cx,cy,cz,sx,sy,sz : longint;
begin
  cx := CosN[AX]; cy := CosN[AY]; cz := CosN[AZ];
  sx := SinN[AX]; sy := SinN[AY]; sz := SinN[AZ];
  {rotate around x first}
  rX := X;
  rY := (Y*cx - Z*sx) div scale;
  rZ := (Y*sx + Z*cx) div scale;
  {rotate result around y then}
  X := (rX*cy - rZ*sy) div scale;
  Y := rY;
  Z := (rX*sy + rZ*cy) div scale;
  {rotate finaly round z}
  rX := (X*cz - Y*sz) div scale;
  rY := (X*sz + Y*cz) div scale;
  rZ := Z;
end;

var
  X,y,z : longint;
  rX,ry,rz : longint;
begin
  PrepareLookUp;
  x := 100000;y:=0;z:=0;
  Rotate3D(x,y,z,0,32,0,rx,ry,rz);
  WriteLn(rx:6,ry:6,rz:6);
end.

Igor
0
 
andersh012098Commented:
if you want to do something fast you should be using Watcom C. My 3d engine is written using only real numbers and it's way fast.

/Anders
0
 
yotAuthor Commented:
Igor, ( Yes maaaster ... )

If you want me to give you pointz for your answer then -

Please answer my qustion !, i can't give you credit for comments ( adleast i don't think i can ).

   zot ... yot !
* by the way - can i change my f***ing user-name ???

0
 
yotAuthor Commented:
Igor, ( Yes maaaster ... )

If you want me to give you pointz for your answer then -

Please answer my qustion !, i can't give you credit for comments ( adleast i don't think i can ).

   zot ... yot !
* by the way - can i change my f***ing user-name ???

0
 
mitchell042997Commented:
Also if you learn about the math behind the function, you may find ways to speed it up.  Or do other neat tricks with coordinate systems.
0
 
mitchell042997Commented:
Also if you learn about the math behind the function, you may find ways to speed it up.  Or do other neat tricks with coordinate systems.

In fact, I have been told that with eijen vectors, you can do ray tracing ****.  That is cool.  Math is neat.
0
 
interCommented:
Dear yot,

Mitchell is right, the wolf, doom, heretic, etc clone handles the fast thingies by using what is called RAY CASTING.
If you compile the code above in 32 bit compiler the code is speeded upto 10 times (using 32 bit integer multiplications with register) but this is not possible for real mode BP application.
One other thing is you may wonder why we do not use DIV instead of SHR, this is due to the shr shifts negative numbers as unsigned we need ASR (arithmetic shift right) so if you rewrote the code in pure assembler procedure it again speedup 10 times.

Bye,
Igor
0
 
yotAuthor Commented:
Ok, i think i got da main idea !
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now