Link to home
Start Free TrialLog in
Avatar of yot
yot

asked on

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 )
Avatar of mitchell042997
mitchell042997

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.
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
Avatar of yot

ASKER

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.
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
****************************************
*   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
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
Avatar of yot

ASKER

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 ???

Avatar of yot

ASKER

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 ???

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.
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.
ASKER CERTIFIED SOLUTION
Avatar of inter
inter
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yot

ASKER

Ok, i think i got da main idea !