Polar to Cartesian

Pages: 12
Polar to Cartesian

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

#include <iostream>
#include <fstream>
#include <iomanip>
#include <math.h>
using namespace std;


const double PI = 3.14159265;

float array[31744][2];
float ar[31744];
float ay[31744];
float ax[31744];
float az[31744];
int i;
int j;

int main()
{
    ofstream fout("c:\bust\polar\mattias_xyz.txt");
    ifstream in_file("C:\Bust\Bust_samples\Mattias_Files\mattias_r_y.txt");
    if(!in_file.is_open()) {
        cout<<"File not opened"<<"n";
        return 1;
    }
    for(i=0;i<31743;i++) {
       for(j=0;j<2;j++) {
        in_file >> array[i][j];
       }
    }

    for(i=0; i<31743; i++){
        for(j=0; j<1; j++){
            ar[i] = array[i][j];
        }
    }
    for(i=0; i<31743; i++){
       for(j=1; j<2; j++){
            ay[i] = array[i][j];
        }
    }


double angle = 1.40625;

int k = 123;
    for(i=0; i<31743; i++){

        ax[i] = sin(angle* PI / 180)* ar[i];

    if(i==k){

        angle = angle + 1.40625;
            k = k + 124;
        }
    }
     k = 123;
    for(i=0; i<31743; i++){

        az[i] = cos(angle* PI / 180)* ar[i];

    if(i==k){

        angle = angle + 1.40625;
            k = k + 124;
        }
    }
    for(i=0; i<31743; i++){

    fout<<ax[i]<<" "<<ay[i]<<" "<<az[i]<<"n";
   
    }
    return 0;
}


I post this code so it could be of help to the community

Thank you for being here

Peter
Individual Bust

Last edited on
You use magic numbers (like 31744 and 1.40625).
Hi kbw

256 profiles per scan 124 points per profile was the scan config
laser line triangulation scan of the human head in 5 seconds.

Our next scanner will be 512 points per profile in 3 seconds
for the full 360 rotation and 1 second for the linear scan.

Peter
Individual Bust
peter hurley wrote:
I post this code so it could be of help to the community
But you should really improve your code. As far as I see you need only 1 loop. 'array' ist just superfluous
Hi coder777

I don't think so, I read in array and divide into ar & ay then use ar to produce
ax & az, show me your code :-)

Peter
Individual Bust
256 profiles per scan 124 points per profile was the scan config
laser line triangulation scan of the human head in 5 seconds.

Our next scanner will be 512 points per profile in 3 seconds
for the full 360 rotation and 1 second for the linear scan.

If your magic numbers are derived from something else, that should be clear in the code.
Last edited on
peter hurley wrote:
I don't think so, I read in array and divide into ar & ay then use ar to produce ax & az, show me your code :-)
Ok. But i'm not able to test it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <fstream>
#include <iomanip>
#include <math.h>
using namespace std;


const double PI = 3.14159265;
const double NAngle = 1.40625;
namespace Profile
{
  const int Count = 256;
  const int Points = 124;
}
const int FieldCount = Profile::Count * Profile::Points;

int main()
{
  ofstream fout("c:\\bust\\polar\\mattias_xyz.txt");
  ifstream in_file("C:\\Bust\\Bust_samples\\Mattias_Files\\mattias_r_y.txt");
  if(!in_file.is_open())
  {
    cout<<"File not opened"<<"\n";
    return 1;
  }
  double angle = NAngle;
  int k = Profile::Points - 1;

  for(int i = 0; i < (FieldCount - 1); ++i)
  {
    float ar, ay;
    in_file >> ar >> ay; // Note: only whithespace _no_ end of line / Error checking needed

    const float ax = sin(angle * PI / 180) * ar;
    const float az = cos(angle * PI / 180) * ar;

    if(i == k)
    {
      angle += NAngle;
      k += Profile::Points;
    }
    fout << ax << " " << ay << " " << az << endl;
  }
  return 0;
}
Hi coder777

Looks good I will try your coding, I did think afterwards that you could put
the two calculations into the same loop.

I will let you know how it works.

Peter
Individual Bust
Hi coder777

I have compiled a scan with your coding and got strange results
the oriantion of the model is backwards to when I use my coding ?
and the rotation in my viewer is backwards to ?

so which is right ? :-) or (-:

Peter
Indiviudal Bust
Does that mean the values are stored in a reverse order?
It's supposed to produce exact the same like your program. Are the values different?

Do you mind to post excerpts of both outputs?
Hi coder777

I will check the values but think they are the same but different in + or -

I don't mind posting any of my work it's just how to do so, the example
is some 1MB in size for both files

the values are in reverse as in mine start in the Z - and end in the Z +
yours start in the Z + and end in the Z -

I have thought about this and I think my presentation and orientation
is correct.

If you look down from above the head ( in the plan view ) start the scan at the
back of the head and work round in a clockwise direction, this being the back of
the head is North the nose is South left ear is East and right ear is West.

Let me know how to get the files out to you.

Peter
Individual Bust
Well, I'd love to go thru megabytes of data as anyone does. But on the other hand I stared a while at the code and found the problem (I think). It's the offset of the angle of Z:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <fstream>
#include <iomanip>
#include <math.h>
using namespace std;


const double PI = 3.14159265;
namespace Profile
{
  const int Count = 256;
  const int Points = 124;
}
const int FieldCount = Profile::Count * Profile::Points;
namespace Angle // Note
{
  const double Step = 1.40625;
  namespace Start
  {
    const double X = 0.0;
    const double Z = Step * FieldCount;
  }
}

int main()
{
  ofstream fout("c:\\bust\\polar\\mattias_xyz.txt");
  ifstream in_file("C:\\Bust\\Bust_samples\\Mattias_Files\\mattias_r_y.txt");
  if(!in_file.is_open())
  {
    cout<<"File not opened"<<"\n";
    return 1;
  }
  double angle = Angle::Step;
  int k = Profile::Points - 1;

  for(int i = 0; i < (FieldCount - 1); ++i)
  {
    float ar, ay;
    in_file >> ar >> ay; // Note: only whithespace _no_ end of line / Error checking needed

    const float ax = sin((Angle::Start::X + angle) * PI / 180) * ar;  // Note
    const float az = cos((Angle::Start::Z + angle) * PI / 180) * ar;  // Note

    if(i == k)
    {
      angle += Angle::Step;
      k += Profile::Points;
    }
    fout << ax << " " << ay << " " << az << endl;
  }
  return 0;
}
Hope that's right.
Last edited on
Hi coder777

I did check the differences and yours tripped up after the first profile ( count or step )
but only slight differences, the scan I used was of 127 profiles ( counts steps ) and
250 points 1/2 a head.

I think a true test would be to generate ax & az from ar and then reveres the calculation
to produce a new ar1 then compare ar with ar1 ?.

I will have a go with your new code and try and reduce my coding down a bit.

You are welcome to any of my scan data they do zip down.

Peter
Individual Bust
Hi coder777

I tried your new coding and still the model was backwwards, the values were
closer to mine though.

Peter
Individual Bust
Polar to Cartesian conversion where known values are raidus from datum 0.00 together with height values
these values are described as ar and ay with the conversion being ax & az

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <fstream>
#include <iomanip>
#include <math.h>
using namespace std;
const double PI = 3.14159265;    //declare PI
float array[31750][2];                  //declare array size and number of columns
float ar[31750];                          //declare array ar
float ay[31750];                         //etc
float ax[31750];                         //etc
float az[31750];                         //etc
int i;                                        //declare i & j for run through
int j;

int main()
{
    ofstream fout("c:\bust\bust_samples\sam_xyz_3.txt"); //output file value space value space valuen
    ifstream in_file("C:\bust\bust_samples\sam_ry.txt");    //input file value space valuen
    if(!in_file.is_open()) {
        cout<<"File not opened"<<"n";
        return 1;
    }
    for(i=0;i<31749;i++) {
       for(j=0;j<2;j++) {
        in_file >> array[i][j];                          //read in to array
       }
    }
    for(i=0; i<31749; i++){
        for(j=0; j<1; j++){
            ar[i] = array[i][j];                         //split array for ar
        }
    }
    for(i=0; i<31749; i++){
       for(j=1; j<2; j++){
            ay[i] = array[i][j];                        //split array for ay
        }
    }
double angle = 1.40625;                        // set degree of rotation of profiles
int k = 250;                                          // set number of points per profile
    for(i=0; i<31749; i++){
        ax[i] = sin(angle* PI / 180)* ar[i];    // use ar to calculate ax for each point
        az[i] = cos(angle* PI / 180)* ar[i];   // use ar to calculate az for each point
    if(i==k){                                           // when number of points are reached goto next profile
        angle = angle + 1.40625;              // add the increment of rotation
            k = k + 250;                            // add the increment of points per profile
        }
    }
    for(i=0; i<31748; i++){
    fout<<ax[i]<<" "<<ay[i]<<" "<<az[i]<<"n"; // file out as (x) value space (y) value space (z) valuen
    }
    return 0;
}


Thank you for your company.

Peter
Individual Bust
Last edited on
Hm, it looks like that there's more elaboration needed for that. It's just that I don't have your background to veryfy those numbers (viewer) and sorry, but not much time on my side either.


I did check the differences and yours tripped up after the first profile ( count or step )
but only slight differences, the scan I used was of 127 profiles ( counts steps ) and
250 points 1/2 a head.
then did you change the constants?
1
2
3
4
5
namespace Profile
{
  const int Count = 256;
  const int Points = 124;
}


Btw. Is this a university project?
Hi coder777

Yes to change the constants.

No to a university project.

It is my work in reproducing the human head in 3D from laser line triangulation
scanning to computer control machining. For members of the general public
world wide.

Peter
Individual Bust
closed account (z05DSL3A)
peter hurley,

FYI: The code tags are: [code] and [/code]
Thanks Grey

I am new to this technology :-)))

Peter
Individual Bust
closed account (z05DSL3A)
The more I read this thread the more confused I get.

The title, Polar to Cartesian, suggests 2d data although your are talking about 3d data.

The polar coordinate system is a 2d system with coordinates (r,T). where r is a radial coordinate and T is an azimuthal (angular) coordinate. On a Cartesian frame, 0° is associated with the +x-axis and travels counterclockwise a T increases.

The 3d version of Polar coordinates, spherical coordinates, is defined by (r,T,P) where r is a radial coordinate, T is an azimuthal (angular) coordinate, and P is a zenith (angular) coordinate.

Another 3d system would be the cylindrical coordinate system, that is an extrusion of polar coordinates, (r,T,z). the polar coordinates would be in the xy-plane and the third coordinate is simply the z-value.

I can't, from the code, get it in my head what coordinate system you are reading in/inferring.

Edit:
T = theta, P = Phi (site no longer displaying greek letters :0(


Last edited on
Pages: 12