thinkpad20

Chaikin's algorithm in Qt/OpenGL

Jun 26th, 2012
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "mypanelopengl.h"
  2. #include <cmath>
  3. #include <GL/glu.h>
  4. #include <iostream>
  5. #include <vector>
  6.  
  7. #define PI 3.14159265
  8.  
  9. using namespace std;
  10.  
  11. MyPanelOpenGL::MyPanelOpenGL(QWidget *parent) :
  12.     QGLWidget(parent)
  13. {
  14.     //set number of iterations
  15.     chaikin_iters = 0;
  16.  
  17.     //initializing the points
  18.     vector<double> A (2);
  19.     A[0] = 2;
  20.     A[1] = 2;
  21.     vector<double> B (2);
  22.     B[0] = 8;
  23.     B[1] = 10;
  24.     vector<double> C (2);
  25.     C[0] = 10;
  26.     C[1] = 7;
  27.     vector<double> D (2);
  28.     D[0] = 5;
  29.     D[1] = 6;
  30.     chaikin_points_initial.push_back(A);
  31.     chaikin_points_initial.push_back(B);
  32.     chaikin_points_initial.push_back(C);
  33.     chaikin_points_initial.push_back(D);
  34.  
  35.     chaikin_points_generated = generateChaikinPoints(chaikin_iters, chaikin_points_initial);
  36. }
  37.  
  38. void MyPanelOpenGL::initializeGL(){
  39.     glShadeModel(GL_SMOOTH);
  40.     glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  41.     glClearDepth(1.0f);
  42.     glEnable(GL_DEPTH_TEST);
  43.     glDepthFunc(GL_LEQUAL);
  44.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  45. }
  46. void MyPanelOpenGL::resizeGL(int width, int height){
  47.     double xMin = 0, xMax = 10, yMin = 0, yMax = 10;
  48.     glViewport(0, 0, (GLint)width, (GLint)height);
  49.  
  50.     glMatrixMode(GL_PROJECTION);
  51.     glLoadIdentity();
  52.     gluOrtho2D(-1,1,-1,1);
  53.     if (width > height){
  54.         height = height?height:1;
  55.         double newWidth = (xMax - xMin) * width / height;
  56.         double difWidth = newWidth - (xMax - xMin);
  57.         xMin = 0.0 + difWidth / 2.0;
  58.         xMax = 10 + difWidth / 2.0;
  59.     } else {
  60.         width = width?width:1;
  61.         double newHeight = (yMax - yMin) * width / height;
  62.         double difHeight = newHeight - (yMax - yMin);
  63.         yMin = 0.0 + difHeight / 2.0;
  64.         yMax = 10 + difHeight / 2.0;
  65.     }
  66.     gluOrtho2D(xMin, xMax, yMin, yMax);
  67.     glMatrixMode(GL_MODELVIEW);
  68.     glLoadIdentity();
  69. }
  70.  
  71. void MyPanelOpenGL::paintGL(){
  72.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  73.     glLoadIdentity();
  74.     //draw control points
  75.     glPointSize(3);
  76.     glColor3f(1,0,0);
  77.     glBegin(GL_POINTS);
  78.     for (int i=0; i < chaikin_points_initial.size(); i++)
  79.     {
  80.         double x = chaikin_points_initial[i][0];
  81.         double y = chaikin_points_initial[i][1];
  82.         glVertex2f((GLfloat)x, (GLfloat)y);
  83.     }
  84.     glEnd();
  85.  
  86.     glLineWidth(1);
  87.     glColor3f(0,0,0);
  88.     glBegin(GL_LINE_LOOP);
  89.     for (int i=0; i < chaikin_points_generated.size(); i++)
  90.     {
  91.         double x = chaikin_points_generated[i][0];
  92.         double y = chaikin_points_generated[i][1];
  93.         glVertex2f((GLfloat)x, (GLfloat)y);
  94.     }
  95.     glEnd();
  96. }
  97.  
  98. vector<vector<double> > MyPanelOpenGL::findChaikinPoints(vector<vector<double> > initial_points)
  99. {
  100.     vector<vector<double> > result_points; // we will add all the vectors we generate to this vector.
  101.     for (int i = 0; i < initial_points.size(); i++){// we iterate over all of the sequential pairs of points
  102.  
  103.         //initialize the pair of points
  104.         vector<double> start_p = initial_points[i];
  105.         vector<double> end_p = initial_points[((i + 1) % initial_points.size())];
  106.  
  107.         //find 1/4 point and 3/4 point of the segment made by these points
  108.         vector<double> one_quarter_mark (2);
  109.         vector<double> three_quarter_mark (2);
  110.         one_quarter_mark[0] = start_p[0] + (end_p[0] - start_p[0])/4;
  111.         one_quarter_mark[1] = start_p[1] + (end_p[1] - start_p[1])/4;
  112.         three_quarter_mark[0] = start_p[0] + 3*(end_p[0] - start_p[0])/4;
  113.         three_quarter_mark[1] = start_p[1] + 3*(end_p[1] - start_p[1])/4;
  114.  
  115.         //add these 1/4 and 3/4 points to our results vector
  116.         result_points.push_back(one_quarter_mark);
  117.         result_points.push_back(three_quarter_mark);
  118.     }
  119.     return result_points;
  120. }
  121.  
  122. vector<vector<double> > MyPanelOpenGL::generateChaikinPoints(int num_iters, vector<vector<double> > points)
  123. {
  124.     for (int i = 0; i<num_iters; i++) points = findChaikinPoints(points);
  125.     return points;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment