kopia lustrzana https://gitlab.com/Zwarf/picplanner
Updated plot library: Circles can be plotted, 3D rotations of coordinate system is possible in realtime, Code clean up
rodzic
3a6ca01e18
commit
99c6fe8759
152
src/plot.c
152
src/plot.c
|
@ -1,35 +1,81 @@
|
|||
#include <plot.h>
|
||||
|
||||
/*TODO
|
||||
* Plot Circle
|
||||
*/
|
||||
float coordinate_system_settings[5];
|
||||
float teta;
|
||||
|
||||
double plot_calc_coordinate_x (double a1, double a2, float alpha, float beta) {
|
||||
/* coordinate transformation to transform 3D coordinates into 2D coordinates */
|
||||
double x = (sin(beta)*a1-sin(alpha)*a2)/(cos(alpha)*sin(beta)-sin(alpha)*cos(beta));
|
||||
void plot_set_basic_coordinate_system_settings (void){
|
||||
coordinate_system_settings[0] = -G_PI*3/5;
|
||||
coordinate_system_settings[1] = -G_PI*2/5;
|
||||
coordinate_system_settings[2] = 1;
|
||||
coordinate_system_settings[3] = 1;
|
||||
coordinate_system_settings[4] = 1.5;
|
||||
}
|
||||
|
||||
void plot_adjust_teta (GtkRange *range, GtkScrollType scroll, double value, gpointer widget){
|
||||
teta = -value;
|
||||
gtk_widget_queue_draw (widget);
|
||||
}
|
||||
|
||||
void plot_adjust_coordinate_system_settings (double s0, double s1, double s2, double s3, double s4){
|
||||
coordinate_system_settings[0] = s0;
|
||||
coordinate_system_settings[1] = s1;
|
||||
coordinate_system_settings[2] = s2;
|
||||
coordinate_system_settings[3] = s3;
|
||||
coordinate_system_settings[4] = s4;
|
||||
}
|
||||
|
||||
double plot_calc_coordinate_x (double a1, double a2) {
|
||||
float alpha, beta;
|
||||
double x,a1_new, a2_new;
|
||||
|
||||
if (!coordinate_system_settings[0]){
|
||||
plot_set_basic_coordinate_system_settings();
|
||||
}
|
||||
|
||||
alpha = *coordinate_system_settings+G_PI;
|
||||
beta = *(coordinate_system_settings+1);
|
||||
a1_new = cos(teta)*a1 - sin(teta)*a2;
|
||||
a2_new = sin(teta)*a1 + cos(teta)*a2;
|
||||
a1_new *= coordinate_system_settings[2];
|
||||
a2_new *= coordinate_system_settings[3];
|
||||
x = (sin(alpha)*a1_new-sin(beta)*a2_new)/(cos(beta)*sin(alpha)-sin(beta)*cos(alpha));
|
||||
return x;
|
||||
}
|
||||
double plot_calc_coordinate_y (double a1, double a2, double a3, float alpha, float beta) {
|
||||
/* coordinate transformation to transform 3D coordinates into 2D coordinates */
|
||||
double y = (-cos(beta)*a1+cos(alpha)*a2)/(cos(alpha)*sin(beta)-sin(alpha)*cos(beta)) + a3;
|
||||
|
||||
double plot_calc_coordinate_y (double a1, double a2, double a3) {
|
||||
float alpha, beta;
|
||||
double y, a1_new, a2_new;
|
||||
|
||||
if (!coordinate_system_settings[0]){
|
||||
plot_set_basic_coordinate_system_settings();
|
||||
}
|
||||
|
||||
alpha = *coordinate_system_settings+G_PI;
|
||||
beta = *(coordinate_system_settings+1);
|
||||
a1_new = cos(teta)*a1 - sin(teta)*a2;
|
||||
a2_new = sin(teta)*a1 + cos(teta)*a2;
|
||||
a1_new *= coordinate_system_settings[2];
|
||||
a2_new *= coordinate_system_settings[3];
|
||||
a3 *= coordinate_system_settings[4];
|
||||
y = (-cos(alpha)*a1_new+cos(beta)*a2_new)/(cos(beta)*sin(alpha)-sin(beta)*cos(alpha)) + a3;
|
||||
return y;
|
||||
}
|
||||
void plot_line_to (cairo_t *cr, double a1, double a2, double a3, float alpha, float beta){
|
||||
/* plot a line with automatic transformation from 3D coordinates into 2D */
|
||||
cairo_line_to (cr, plot_calc_coordinate_x (a1, a2, alpha, beta), plot_calc_coordinate_y (a1, a2, a3, alpha, beta));
|
||||
|
||||
void plot_line_to (cairo_t *cr, double a1, double a2, double a3){
|
||||
cairo_line_to (cr, plot_calc_coordinate_x (a1, a2), plot_calc_coordinate_y (a1, a2, a3));
|
||||
}
|
||||
void plot_move_to (cairo_t *cr, double a1, double a2, double a3, float alpha, float beta) {
|
||||
/* move cairo starting point with automatic transformation from 3D coordinates into 2D */
|
||||
cairo_move_to (cr, plot_calc_coordinate_x (a1, a2, alpha, beta), plot_calc_coordinate_y (a1, a2, a3, alpha, beta));
|
||||
|
||||
void plot_move_to (cairo_t *cr, double a1, double a2, double a3) {
|
||||
cairo_move_to (cr, plot_calc_coordinate_x (a1, a2), plot_calc_coordinate_y (a1, a2, a3));
|
||||
}
|
||||
void plot_vector_to (cairo_t *cr, double a1, double a2, double a3, float alpha, float beta) {
|
||||
/* plot a vector with automatic transformation from 3D coordinates into 2D */
|
||||
|
||||
void plot_vector_to (cairo_t *cr, double a1, double a2, double a3) {
|
||||
double current_x = 0;
|
||||
double current_y = 0;
|
||||
double *x_1 = ¤t_x;
|
||||
double *y_1 = ¤t_y;
|
||||
double x_2 = plot_calc_coordinate_x (a1, a2, alpha, beta);
|
||||
double y_2 = plot_calc_coordinate_y (a1, a2, a3, alpha, beta);
|
||||
double x_2 = plot_calc_coordinate_x (a1, a2);
|
||||
double y_2 = plot_calc_coordinate_y (a1, a2, a3);
|
||||
|
||||
cairo_get_current_point (cr, x_1, y_1);
|
||||
double direct_x = x_2-*x_1;
|
||||
|
@ -37,11 +83,77 @@ void plot_vector_to (cairo_t *cr, double a1, double a2, double a3, float alpha,
|
|||
double norm = pow(pow(direct_x,2) + pow(direct_y,2), 0.5);
|
||||
direct_x /= norm;
|
||||
direct_y /= norm;
|
||||
g_print("direct_x: %f, direct_y: %f\n", direct_x, direct_y);
|
||||
/*g_print("direct_x: %f, direct_y: %f\n", direct_x, direct_y);*/
|
||||
|
||||
float gamma = 0.5326;
|
||||
cairo_line_to (cr, x_2, y_2);
|
||||
cairo_line_to (cr, x_2+(-direct_x*cos(gamma)+direct_y*sin(gamma))*0.2, y_2+(-direct_y*cos(gamma)-direct_x*sin(gamma))*0.2);
|
||||
cairo_move_to (cr, x_2, y_2);
|
||||
cairo_line_to (cr, x_2+(-direct_x*cos(gamma)-direct_y*sin(gamma))*0.2, y_2+(-direct_y*cos(gamma)+direct_x*sin(gamma))*0.2);
|
||||
|
||||
}
|
||||
|
||||
double plot_rotate_coordinate (double x, double y, double z, int coordinate_index, float rot, int rot_index){
|
||||
/* coordniate_index is used to specify if the coordinate is x, y or z component
|
||||
* rot_index is used to specify if the rotation is around the x, y or z achsis */
|
||||
if (rot_index == 0){
|
||||
if (coordinate_index == 0) { return x; }
|
||||
else if (coordinate_index == 1){ return cos(rot)*y-sin(rot)*z; }
|
||||
else{ return sin(rot)*y+cos(rot)*z; }
|
||||
}
|
||||
else if (rot_index == 1){
|
||||
if (coordinate_index == 0){ return cos(rot)*x+sin(rot)*z; }
|
||||
else if (coordinate_index == 1){ return y; }
|
||||
else { return -sin(rot)*x+cos(rot)*z; }
|
||||
}
|
||||
else {
|
||||
if (coordinate_index == 0){ return cos(rot)*x-sin(rot)*y; }
|
||||
else if (coordinate_index == 1){ return sin(rot)*x+cos(rot)*y; }
|
||||
else { return z; }
|
||||
}
|
||||
}
|
||||
|
||||
void plot_circle (cairo_t *cr, double radius, float rot1, float rot2, double render){
|
||||
double x = radius;
|
||||
double y = 0;
|
||||
double z = 0;
|
||||
double x_plot, y_plot, z_plot;
|
||||
|
||||
x_plot = plot_rotate_coordinate (x,y,z,0,rot1,0);
|
||||
y_plot = plot_rotate_coordinate (x,y,z,1,rot1,0);
|
||||
z_plot = plot_rotate_coordinate (x,y,z,2,rot1,0);
|
||||
x = x_plot; y = y_plot; z = z_plot;
|
||||
x_plot = plot_rotate_coordinate (x,y,z,0,rot2,1);
|
||||
y_plot = plot_rotate_coordinate (x,y,z,1,rot2,1);
|
||||
z_plot = plot_rotate_coordinate (x,y,z,2,rot2,1);
|
||||
|
||||
plot_move_to (cr, x_plot, y_plot, z_plot);
|
||||
for (double ang = 2*G_PI/render; ang<=2*G_PI; ang += 2*G_PI/render){
|
||||
x = radius * cos(ang);
|
||||
y = radius * sin(ang);
|
||||
z = 0;
|
||||
|
||||
x_plot = plot_rotate_coordinate (x,y,z,0,rot1,0);
|
||||
y_plot = plot_rotate_coordinate (x,y,z,1,rot1,0);
|
||||
z_plot = plot_rotate_coordinate (x,y,z,2,rot1,0);
|
||||
x = x_plot; y = y_plot; z = z_plot;
|
||||
x_plot = plot_rotate_coordinate (x,y,z,0,rot2,1);
|
||||
y_plot = plot_rotate_coordinate (x,y,z,1,rot2,1);
|
||||
z_plot = plot_rotate_coordinate (x,y,z,2,rot2,1);
|
||||
|
||||
plot_line_to (cr, x_plot, y_plot, z_plot);
|
||||
}
|
||||
x = radius;
|
||||
y = 0;
|
||||
z = 0;
|
||||
|
||||
x_plot = plot_rotate_coordinate (x,y,z,0,rot1,0);
|
||||
y_plot = plot_rotate_coordinate (x,y,z,1,rot1,0);
|
||||
z_plot = plot_rotate_coordinate (x,y,z,2,rot1,0);
|
||||
x = x_plot; y = y_plot; z = z_plot;
|
||||
x_plot = plot_rotate_coordinate (x,y,z,0,rot2,1);
|
||||
y_plot = plot_rotate_coordinate (x,y,z,1,rot2,1);
|
||||
z_plot = plot_rotate_coordinate (x,y,z,2,rot2,1);
|
||||
|
||||
plot_line_to (cr, x_plot, y_plot, z_plot);
|
||||
}
|
||||
|
|
15
src/plot.h
15
src/plot.h
|
@ -2,12 +2,17 @@
|
|||
#include <math.h>
|
||||
#include <cairo.h>
|
||||
|
||||
double plot_calc_coordinate_x (double a1, double a2, float alpha, float beta);
|
||||
double plot_calc_coordinate_x (double a1, double a2);
|
||||
|
||||
double plot_calc_coordinate_y (double a1, double a2, double a3, float alpha, float beta);
|
||||
double plot_calc_coordinate_y (double a1, double a2, double a3);
|
||||
|
||||
void plot_line_to (cairo_t *cr, double a1, double a2, double a3, float alpha, float beta);
|
||||
void plot_line_to (cairo_t *cr, double a1, double a2, double a3);
|
||||
|
||||
void plot_move_to (cairo_t *cr, double a1, double a2, double a3, float alpha, float beta);
|
||||
void plot_move_to (cairo_t *cr, double a1, double a2, double a3);
|
||||
|
||||
void plot_vector_to (cairo_t *cr, double a1, double a2, double a3);
|
||||
|
||||
double plot_rotate_coordinate (double x, double y, double z, int coordinate_index, float rot, int rot_index);
|
||||
|
||||
void plot_circle (cairo_t *cr, double radius, float rot1, float rot2, double render);
|
||||
|
||||
void plot_vector_to (cairo_t *cr, double a1, double a2, double a3, float alpha, float beta);
|
||||
|
|
Ładowanie…
Reference in New Issue