Skip to content

Commit

Permalink
Use double instead float when possible, ande code cleaning.
Browse files Browse the repository at this point in the history
change EXCHG macro to equivalent inline functions
(better code compatibility with some compilers)
  • Loading branch information
charras authored and charras committed Oct 30, 2008
1 parent abd75ea commit 78bbe94
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 108 deletions.
4 changes: 2 additions & 2 deletions 3d-viewer/3d_canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ void Pcb3D_GLCanvas::OnMouseEvent( wxMouseEvent& event )
/********************************************************/
{
wxSize size( GetClientSize() );
float spin_quat[4];
double spin_quat[4];


if( event.RightDown() )
Expand Down Expand Up @@ -298,7 +298,7 @@ void Pcb3D_GLCanvas::OnMouseEvent( wxMouseEvent& event )
{
/* middle button drag -> pan */
/* Current zoom and an additional factor are taken into account for the amount of panning. */
const float PAN_FACTOR = 8.0 * g_Parm_3D_Visu.m_Zoom;
const double PAN_FACTOR = 8.0 * g_Parm_3D_Visu.m_Zoom;
g_Draw3d_dx -= PAN_FACTOR * ( g_Parm_3D_Visu.m_Beginx - event.GetX() ) / size.x;
g_Draw3d_dy -= PAN_FACTOR * (event.GetY() - g_Parm_3D_Visu.m_Beginy) / size.y;
}
Expand Down
6 changes: 2 additions & 4 deletions 3d-viewer/3d_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
#include "pcbstruct.h"
#include "macros.h"

// #include "pcbnew.h"

#include "3d_viewer.h"
#include "trackball.h"

Expand Down Expand Up @@ -111,7 +109,7 @@ GLuint Pcb3D_GLCanvas::CreateDrawGL_List()
g_Parm_3D_Visu.m_Layers = pcb->m_BoardSettings->m_CopperLayerCount;
g_Parm_3D_Visu.m_BoardScale = 2.0 / MAX( g_Parm_3D_Visu.m_BoardSize.x,
g_Parm_3D_Visu.m_BoardSize.y );
float epoxy_width = 1.6; // epoxy width in mm
double epoxy_width = 1.6; // epoxy width in mm
g_Parm_3D_Visu.m_Epoxy_Width = epoxy_width / 2.54 * 1000
* g_Parm_3D_Visu.m_BoardScale;

Expand Down Expand Up @@ -591,7 +589,7 @@ void D_PAD::Draw3D( Pcb3D_GLCanvas* glcanvas )
int angle, delta_angle;
int coord[4][2];
double fcoord[8][2], f_hole_coord[8][2];
float scale;
double scale;
double zpos;
wxPoint shape_pos;
double x, y, r, w, hole, holeX, holeY;
Expand Down
14 changes: 7 additions & 7 deletions 3d-viewer/3d_viewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ class SEGVIA;
class Info_3D_Visu
{
public:
float m_Beginx, m_Beginy; /* position of mouse */
float m_Quat[4]; /* orientation of object */
float m_Rot[4]; /* man rotation of object */
float m_Zoom; /* field of view in degrees */
double m_Beginx, m_Beginy; /* position of mouse */
double m_Quat[4]; /* orientation of object */
double m_Rot[4]; /* man rotation of object */
double m_Zoom; /* field of view in degrees */
S3D_Color m_BgColor;
bool m_Draw3DAxis;
bool m_Draw3DModule;
Expand All @@ -65,11 +65,11 @@ class Info_3D_Visu
wxSize m_BoardSize;
int m_Layers;
EDA_BoardDesignSettings * m_BoardSettings; // Link to current board design settings
float m_Epoxy_Width; /* Epoxy tickness (normalized) */
double m_Epoxy_Width; /* Epoxy tickness (normalized) */

float m_BoardScale; /* Normalisation scale for coordinates:
double m_BoardScale; /* Normalisation scale for coordinates:
when scaled tey are between -1.0 and +1.0 */
float m_LayerZcoord[32];
double m_LayerZcoord[32];
public:
Info_3D_Visu();
~Info_3D_Visu();
Expand Down
76 changes: 38 additions & 38 deletions 3d-viewer/trackball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
* Gavin Bell
*/
#include <math.h>
#include "fctsys.h" // used only to define GLfloat
#include "3d_viewer.h" // used only to define GLfloat
#include "trackball.h"

/*
Expand All @@ -64,80 +66,80 @@
/*
* Local function prototypes (not defined in trackball.h)
*/
static float tb_project_to_sphere(float, float, float);
static void normalize_quat(float [4]);
static double tb_project_to_sphere(double, double, double);
static void normalize_quat(double [4]);

void
vzero(float *v)
vzero(double *v)
{
v[0] = 0.0;
v[1] = 0.0;
v[2] = 0.0;
}

void
vset(float *v, float x, float y, float z)
vset(double *v, double x, double y, double z)
{
v[0] = x;
v[1] = y;
v[2] = z;
}

void
vsub(const float *src1, const float *src2, float *dst)
vsub(const double *src1, const double *src2, double *dst)
{
dst[0] = src1[0] - src2[0];
dst[1] = src1[1] - src2[1];
dst[2] = src1[2] - src2[2];
}

void
vcopy(const float *v1, float *v2)
vcopy(const double *v1, double *v2)
{
register int i;
for (i = 0 ; i < 3 ; i++)
v2[i] = v1[i];
}

void
vcross(const float *v1, const float *v2, float *cross)
vcross(const double *v1, const double *v2, double *cross)
{
float temp[3];
double temp[3];

temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
vcopy(temp, cross);
}

float
vlength(const float *v)
double
vlength(const double *v)
{
return (float) sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
return (double) sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
}

void
vscale(float *v, float div)
vscale(double *v, double div)
{
v[0] *= div;
v[1] *= div;
v[2] *= div;
}

void
vnormal(float *v)
vnormal(double *v)
{
vscale(v, 1.0f/vlength(v));
}

float
vdot(const float *v1, const float *v2)
double
vdot(const double *v1, const double *v2)
{
return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
}

void
vadd(const float *src1, const float *src2, float *dst)
vadd(const double *src1, const double *src2, double *dst)
{
dst[0] = src1[0] + src2[0];
dst[1] = src1[1] + src2[1];
Expand All @@ -157,12 +159,12 @@ vadd(const float *src1, const float *src2, float *dst)
* (-1.0 ... 1.0)
*/
void
trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
trackball(double q[4], double p1x, double p1y, double p2x, double p2y)
{
float a[3]; /* Axis of rotation */
float phi; /* how much to rotate about axis */
float p1[3], p2[3], d[3];
float t;
double a[3]; /* Axis of rotation */
double phi; /* how much to rotate about axis */
double p1[3], p2[3], d[3];
double t;

if (p1x == p2x && p1y == p2y) {
/* Zero rotation */
Expand Down Expand Up @@ -194,7 +196,7 @@ trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
*/
if (t > 1.0) t = 1.0;
if (t < -1.0) t = -1.0;
phi = 2.0f * (float) asin(t);
phi = 2.0f * (double) asin(t);

axis_to_quat(a,phi,q);
}
Expand All @@ -203,26 +205,26 @@ trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
* Given an axis and angle, compute quaternion.
*/
void
axis_to_quat(float a[3], float phi, float q[4])
axis_to_quat(double a[3], double phi, double q[4])
{
vnormal(a);
vcopy(a, q);
vscale(q, (float) sin(phi/2.0));
q[3] = (float) cos(phi/2.0);
vscale(q, (double) sin(phi/2.0));
q[3] = (double) cos(phi/2.0);
}

/*
* Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
* if we are away from the center of the sphere.
*/
static float
tb_project_to_sphere(float r, float x, float y)
static double
tb_project_to_sphere(double r, double x, double y)
{
float d, t, z;
double d, t, z;

d = (float) sqrt(x*x + y*y);
d = (double) sqrt(x*x + y*y);
if (d < r * 0.70710678118654752440) { /* Inside sphere */
z = (float) sqrt(r*r - d*d);
z = (double) sqrt(r*r - d*d);
} else { /* On hyperbola */
t = r / 1.41421356237309504880f;
z = t*t / d;
Expand All @@ -244,11 +246,11 @@ tb_project_to_sphere(float r, float x, float y)
#define RENORMCOUNT 97

void
add_quats(float q1[4], float q2[4], float dest[4])
add_quats(double q1[4], double q2[4], double dest[4])
{
static int count=0;
float t1[4], t2[4], t3[4];
float tf[4];
double t1[4], t2[4], t3[4];
double tf[4];

vcopy(q1,t1);
vscale(t1,q2[3]);
Expand Down Expand Up @@ -284,11 +286,10 @@ add_quats(float q1[4], float q2[4], float dest[4])
* - Pletinckx, D., Quaternion calculus as a basic tool in computer
* graphics, The Visual Computer 5, 2-13, 1989.
*/
static void
normalize_quat(float q[4])
static void normalize_quat(double q[4])
{
int i;
float mag;
double mag;

mag = (q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
for (i = 0; i < 4; i++) q[i] /= mag;
Expand All @@ -298,8 +299,7 @@ normalize_quat(float q[4])
* Build a rotation matrix, given a quaternion rotation.
*
*/
void
build_rotmatrix(float m[4][4], float q[4])
void build_rotmatrix(GLfloat m[4][4], double q[4])
{
m[0][0] = 1.0f - 2.0f * (q[1] * q[1] + q[2] * q[2]);
m[0][1] = 2.0f * (q[0] * q[1] - q[2] * q[3]);
Expand Down
12 changes: 4 additions & 8 deletions 3d-viewer/trackball.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
* The resulting rotation is returned as a quaternion rotation in the
* first paramater.
*/
void
trackball(float q[4], float p1x, float p1y, float p2x, float p2y);
void trackball(double q[4], double p1x, double p1y, double p2x, double p2y);

/*
* Given two quaternions, add them together to get a third quaternion.
Expand All @@ -58,21 +57,18 @@ trackball(float q[4], float p1x, float p1y, float p2x, float p2y);
* rotation, the second and third the total rotation (which will be
* over-written with the resulting new total rotation).
*/
void
add_quats(float *q1, float *q2, float *dest);
void add_quats(double *q1, double *q2, double *dest);

/*
* A useful function, builds a rotation matrix in Matrix based on
* given quaternion.
*/
void
build_rotmatrix(float m[4][4], float q[4]);
void build_rotmatrix(GLfloat m[4][4], double q[4]);

/*
* This function computes a quaternion based on an axis (defined by
* the given vector) and an angle about which to rotate. The angle is
* expressed in radians. The result is put into the third argument.
*/
void
axis_to_quat(float a[3], float phi, float q[4]);
void axis_to_quat(double a[3], double phi, double q[4]);

9 changes: 9 additions & 0 deletions change_log.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ Started 2007-June-11
Please add newer entries at the top, list the date and your name with
email address.


2008-oct-30 UPDATE Jean-Pierre Charras <jean-pierre.charras@inpg.fr>
================================================================================
++All
Use double instead float when possible, ande code cleaning.
change EXCHG macro to equivalent inline functions
(better code compatibility with some compilers)


2008-Oct-19 UPDATE Dick Hollenbeck <dick@softplc.com>
================================================================================
+eeschema
Expand Down
Loading

0 comments on commit 78bbe94

Please sign in to comment.