/
Curve.cpp
199 lines (182 loc) · 4.38 KB
/
Curve.cpp
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Curve.cpp
*
* Created on: Nov 17, 2011
* Author: pjm
*/
#include "Curve.h"
#include <stddef.h>
#include <math.h>
#include <iostream>
Curve::Curve(int uDegree, int vDegree, float * transform, float ** points, int precision)
{
this->uDegree = uDegree;
this->vDegree = vDegree;
this->transform = transform;
this->points = points;
this->precision = precision;
}
void Curve::draw()
{
if(0 == vDegree) {
drawCurve();
} else {
drawSurface();
}
}
void Curve::drawCurve()
{
glPushMatrix();
glMultMatrixf(transform);
int numPoints = pow(3,precision-1);
glBegin(GL_LINES);
float * lastV = NULL;
float * v = NULL;
for(int sample = 0; sample <= numPoints; sample++) {
float pointsCopy[uDegree + 1][3];
float t = (float) sample / (float) numPoints;
if((NULL == lastV) && (NULL == v)) {
v = new float[3];
} else {
if(NULL != lastV) {
delete[] lastV;
}
lastV = v;
v = new float[3];
}
for(int j = 0; j < uDegree + 1; j++) {
pointsCopy[j][0] = points[j][0];
pointsCopy[j][1] = points[j][1];
pointsCopy[j][2] = points[j][2];
}
for(int i = uDegree - 1; i >= 0; i--) { //we refer to every i from i-1, so one less than limit
for(int j = 0; j <= i; j++) {
pointsCopy[j][0] = (1-t) * pointsCopy[j][0] + t * pointsCopy[j+1][0];
pointsCopy[j][1] = (1-t) * pointsCopy[j][1] + t * pointsCopy[j+1][1];
pointsCopy[j][2] = (1-t) * pointsCopy[j][2] + t * pointsCopy[j+1][2];
}
}
v[0] = pointsCopy[0][0];
v[1] = pointsCopy[0][1];
v[2] = pointsCopy[0][2];
if(NULL != lastV) {
glVertex3fv(lastV);
glVertex3fv(v);
}
}
if(NULL != v) {
delete[] v;
}
if(NULL != lastV) {
delete[] lastV;
}
glEnd();
glPopMatrix();
}
void Curve::drawSurface()
{
/*
* for(int row = 0; row <= vDegree; row++) {
std::vector<std::string> strs;
boost::split(strs, line, boost::is_space());
for(int i = 0; i < 3*(uDegree+1); i++) {
string d = strs.at(i);
sscanf(d.c_str(),"%f",&(coordinates[i][index]));
}
}
*
*/
/* First we get the triangle vertices, then we draw the mesh */
float *** realPoints = new float**[vDegree+1];
for(int v = vDegree; v >= 0; v--) {
realPoints[v] = new float*[uDegree+1];
for(int u = 0; u <= uDegree; u++) {
float * coord = new float[3];
coord[0] = points[v][3*u];
coord[1] = points[v][3*u+1];
coord[2] = points[v][3*u+2];
realPoints[v][u] = coord;
}
}
int numSamples = pow(3,precision-1) * vDegree;
float *** matrix = new float**[numSamples + 1];
for(int i = 0; i <= numSamples; i++) {
matrix[i] = new float*[numSamples + 1];
}
for(int vIndex = 0; vIndex <= numSamples; vIndex++) {
for(int uIndex = 0; uIndex <= numSamples; uIndex++) {
float * coordinates = new float[3];
coordinates[0] = 0;
coordinates[1] = 0;
coordinates[2] = 0;
for(int i = 0; i < vDegree+1; i++) {
for(int j = 0; j < uDegree+1; j++) {
float v = (float) vIndex / (float) numSamples;
float u = (float) uIndex / (float) numSamples;
float * b_ij = realPoints[i][j];
float B_i = bernstein(i, vDegree, v);
float B_j = bernstein(j, uDegree, u);
float c = B_i + B_j;
coordinates[0] += c * b_ij[0];
coordinates[1] += c * b_ij[1];
coordinates[2] += c * b_ij[2];
}
}
matrix[vIndex][uIndex] = coordinates;
}
}
glPushMatrix();
glMultMatrixf(transform);
/* Now we draw the mesh from the sampled points */
bool br = false;
float black[] = {0.0, 0.0, 0.0};
float red[] = {1.0, 0.0, 0.0};
for(int v = 0; v < numSamples; v++) {
glBegin(GL_TRIANGLE_STRIP);
for(int u = 0; u <= numSamples; u++) {
float * p1 = matrix[v][u];
float * p2 = matrix[v+1][u];
if(br) {
glColor3fv(black);
br = false;
} else {
glColor3fv(red);
br = true;
}
glVertex3fv(p1);
glVertex3fv(p2);
}
glEnd();
}
glPopMatrix();
for(int i = 0; i <= numSamples; i++) {
for(int j = 0; j <= numSamples; j++) {
delete[] matrix[i][j];
}
delete[] matrix[i];
}
delete[] matrix;
}
void Curve::setPrecision(int precision)
{
this->precision = precision;
}
int Curve::factorial(int n)
{
int r = 1;
for(int i = n; i > 0; i--) {
r *= i;
}
return r;
}
/* n > i */
float Curve::bernstein(int i, int n, float t)
{
float base = (float) factorial(n) / (float) (factorial(i) * factorial(n - i));
base *= powf(t,i);
base *= powf(1-t,n-i);
return base;
}
Curve::~Curve() {
delete[] points;
}