Commit 9b69bb4c41231d8b8297de67a13a88c61c9bc92f

Authored by Jiaming Guo
1 parent def55315

add diplay list to avoid waste

Showing 1 changed file with 110 additions and 89 deletions   Show diff stats
@@ -160,14 +160,15 @@ namespace stim { @@ -160,14 +160,15 @@ namespace stim {
160 #endif 160 #endif
161 161
162 template <typename T> 162 template <typename T>
163 - class flow : public stim::gl_network<T>{ 163 + class flow : public stim::gl_network<T> {
164 164
165 private: 165 private:
166 - 166 +
167 unsigned num_edge; 167 unsigned num_edge;
168 unsigned num_vertex; 168 unsigned num_vertex;
169 -  
170 - enum direction {UP, LEFT, DOWN, RIGHT}; 169 + GLuint dlist; // display list for inlets/outlets connections
  170 +
  171 + enum direction { UP, LEFT, DOWN, RIGHT };
171 172
172 // calculate the cofactor of elemen[row][col] 173 // calculate the cofactor of elemen[row][col]
173 void get_minor(T** src, T** dest, int row, int col, int order) { 174 void get_minor(T** src, T** dest, int row, int col, int order) {
@@ -232,7 +233,7 @@ namespace stim { @@ -232,7 +233,7 @@ namespace stim {
232 using stim::network<T>::get_r; 233 using stim::network<T>::get_r;
233 using stim::network<T>::get_average_r; 234 using stim::network<T>::get_average_r;
234 using stim::network<T>::get_l; 235 using stim::network<T>::get_l;
235 - 236 +
236 T** C; // Conductance 237 T** C; // Conductance
237 std::vector<typename stim::triple<unsigned, unsigned, float> > Q; // volume flow rate 238 std::vector<typename stim::triple<unsigned, unsigned, float> > Q; // volume flow rate
238 std::vector<T> QQ; // Q' vector 239 std::vector<T> QQ; // Q' vector
@@ -292,11 +293,16 @@ namespace stim { @@ -292,11 +293,16 @@ namespace stim {
292 output.clear(); 293 output.clear();
293 inlet.clear(); 294 inlet.clear();
294 outlet.clear(); 295 outlet.clear();
  296 +
  297 + if (glIsList(dlist)) {
  298 + glDeleteLists(dlist, 1); // delete display list for modify
  299 + glDeleteLists(dlist + 1, 1);
  300 + }
295 } 301 }
296 302
297 // copy radius from cylinder to flow 303 // copy radius from cylinder to flow
298 void set_radius(unsigned i, T radius) { 304 void set_radius(unsigned i, T radius) {
299 - 305 +
300 for (unsigned j = 0; j < num_edge; j++) { 306 for (unsigned j = 0; j < num_edge; j++) {
301 if (E[j].v[0] == i) 307 if (E[j].v[0] == i)
302 E[j].cylinder<T>::set_r(0, radius); 308 E[j].cylinder<T>::set_r(0, radius);
@@ -307,7 +313,7 @@ namespace stim { @@ -307,7 +313,7 @@ namespace stim {
307 313
308 // get the radii of vertex i 314 // get the radii of vertex i
309 T get_radius(unsigned i) { 315 T get_radius(unsigned i) {
310 - 316 +
311 unsigned tmp_e; // edge index 317 unsigned tmp_e; // edge index
312 unsigned tmp_v; // vertex index in that edge 318 unsigned tmp_v; // vertex index in that edge
313 for (unsigned j = 0; j < num_edge; j++) { 319 for (unsigned j = 0; j < num_edge; j++) {
@@ -326,7 +332,7 @@ namespace stim { @@ -326,7 +332,7 @@ namespace stim {
326 332
327 // get the velocity of pendant vertex i 333 // get the velocity of pendant vertex i
328 T get_velocity(unsigned i) { 334 T get_velocity(unsigned i) {
329 - 335 +
330 unsigned tmp_e; // edge index 336 unsigned tmp_e; // edge index
331 for (unsigned j = 0; j < num_edge; j++) { 337 for (unsigned j = 0; j < num_edge; j++) {
332 if (E[j].v[0] == i) { 338 if (E[j].v[0] == i) {
@@ -400,7 +406,7 @@ namespace stim { @@ -400,7 +406,7 @@ namespace stim {
400 // get the inverse of conductance matrix 406 // get the inverse of conductance matrix
401 stim::matrix<float> _C(num_vertex, num_vertex); 407 stim::matrix<float> _C(num_vertex, num_vertex);
402 inversion(C, num_vertex, _C.data()); 408 inversion(C, num_vertex, _C.data());
403 - 409 +
404 // get the pressure in the network 410 // get the pressure in the network
405 for (unsigned i = 0; i < num_vertex; i++) { 411 for (unsigned i = 0; i < num_vertex; i++) {
406 for (unsigned j = 0; j < num_vertex; j++) { 412 for (unsigned j = 0; j < num_vertex; j++) {
@@ -421,7 +427,7 @@ namespace stim { @@ -421,7 +427,7 @@ namespace stim {
421 427
422 Q[i].first = start_vertex; 428 Q[i].first = start_vertex;
423 Q[i].second = end_vertex; 429 Q[i].second = end_vertex;
424 - 430 +
425 Q[i].third = ((float)stim::PI * std::pow(get_average_r(i), 4) * deltaP) / (8 * u * get_l(i)); 431 Q[i].third = ((float)stim::PI * std::pow(get_average_r(i), 4) * deltaP) / (8 * u * get_l(i));
426 v[i] = Q[i].third / ((float)stim::PI * std::pow(get_average_r(i), 2)); 432 v[i] = Q[i].third / ((float)stim::PI * std::pow(get_average_r(i), 2));
427 } 433 }
@@ -429,7 +435,7 @@ namespace stim { @@ -429,7 +435,7 @@ namespace stim {
429 435
430 // get the brewer color map based on velocity 436 // get the brewer color map based on velocity
431 void get_color_map(T& max_v, T& min_v, std::vector<unsigned char>& color, std::vector<unsigned> pendant_vertex) { 437 void get_color_map(T& max_v, T& min_v, std::vector<unsigned char>& color, std::vector<unsigned> pendant_vertex) {
432 - 438 +
433 unsigned num_edge = Q.size(); 439 unsigned num_edge = Q.size();
434 unsigned num_vertex = QQ.size(); 440 unsigned num_vertex = QQ.size();
435 441
@@ -441,7 +447,7 @@ namespace stim { @@ -441,7 +447,7 @@ namespace stim {
441 447
442 max_v = *std::max_element(abs_V.begin(), abs_V.end()); 448 max_v = *std::max_element(abs_V.begin(), abs_V.end());
443 min_v = *std::min_element(abs_V.begin(), abs_V.end()); 449 min_v = *std::min_element(abs_V.begin(), abs_V.end());
444 - 450 +
445 // get the color map based on velocity range along the network 451 // get the color map based on velocity range along the network
446 color.clear(); 452 color.clear();
447 if (pendant_vertex.size() == 2 && num_edge - num_vertex + 1 <= 0) // only one inlet and one outlet 453 if (pendant_vertex.size() == 2 && num_edge - num_vertex + 1 <= 0) // only one inlet and one outlet
@@ -472,7 +478,7 @@ namespace stim { @@ -472,7 +478,7 @@ namespace stim {
472 // @param: current direct length between two vertices 478 // @param: current direct length between two vertices
473 // @param: desire length 479 // @param: desire length
474 void find_hilbert_order(T l, T d, int &order) { 480 void find_hilbert_order(T l, T d, int &order) {
475 - 481 +
476 bool flag = false; 482 bool flag = false;
477 int o = 1; 483 int o = 1;
478 T tmp; // temp of length 484 T tmp; // temp of length
@@ -489,7 +495,7 @@ namespace stim { @@ -489,7 +495,7 @@ namespace stim {
489 } 495 }
490 496
491 void move(unsigned i, T *c, direction dir, T dl, int feeder, bool invert) { 497 void move(unsigned i, T *c, direction dir, T dl, int feeder, bool invert) {
492 - 498 +
493 int cof = (invert) ? -1 : 1; 499 int cof = (invert) ? -1 : 1;
494 500
495 switch (dir) { 501 switch (dir) {
@@ -510,7 +516,7 @@ namespace stim { @@ -510,7 +516,7 @@ namespace stim {
510 stim::vec3<T> tmp; 516 stim::vec3<T> tmp;
511 for (unsigned i = 0; i < 3; i++) 517 for (unsigned i = 0; i < 3; i++)
512 tmp[i] = c[i]; 518 tmp[i] = c[i];
513 - 519 +
514 if (feeder == 1) // inlet main feeder 520 if (feeder == 1) // inlet main feeder
515 inlet[i].V.push_back(tmp); 521 inlet[i].V.push_back(tmp);
516 else if (feeder == 0) // outlet main feeder 522 else if (feeder == 0) // outlet main feeder
@@ -518,8 +524,8 @@ namespace stim { @@ -518,8 +524,8 @@ namespace stim {
518 } 524 }
519 525
520 void hilbert_curve(unsigned i, T *c, int order, T dl, int feeder, bool invert, direction dir = DOWN) { 526 void hilbert_curve(unsigned i, T *c, int order, T dl, int feeder, bool invert, direction dir = DOWN) {
521 -  
522 - if (order == 1) { 527 +
  528 + if (order == 1) {
523 switch (dir) { 529 switch (dir) {
524 case UP: 530 case UP:
525 move(i, c, DOWN, dl, feeder, invert); 531 move(i, c, DOWN, dl, feeder, invert);
@@ -542,7 +548,7 @@ namespace stim { @@ -542,7 +548,7 @@ namespace stim {
542 move(i, c, RIGHT, dl, feeder, invert); 548 move(i, c, RIGHT, dl, feeder, invert);
543 break; 549 break;
544 } 550 }
545 - 551 +
546 } 552 }
547 else if (order > 1) { 553 else if (order > 1) {
548 switch (dir) { 554 switch (dir) {
@@ -717,7 +723,7 @@ namespace stim { @@ -717,7 +723,7 @@ namespace stim {
717 723
718 // draw solid sphere at every vertex 724 // draw solid sphere at every vertex
719 void glSolidSphere(T max_pressure, GLint subdivision) { 725 void glSolidSphere(T max_pressure, GLint subdivision) {
720 - 726 +
721 // waste? 727 // waste?
722 for (unsigned i = 0; i < num_edge; i++) { 728 for (unsigned i = 0; i < num_edge; i++) {
723 for (unsigned j = 0; j < E[i].size(); j++) { 729 for (unsigned j = 0; j < E[i].size(); j++) {
@@ -771,7 +777,7 @@ namespace stim { @@ -771,7 +777,7 @@ namespace stim {
771 777
772 r1 = get_r(i, j); 778 r1 = get_r(i, j);
773 r2 = get_r(i, j + 1); 779 r2 = get_r(i, j + 1);
774 - 780 +
775 // calculate the envelope caps 781 // calculate the envelope caps
776 find_envelope(cp1, cp2, center1, center2, r1, r2, subdivision); 782 find_envelope(cp1, cp2, center1, center2, r1, r2, subdivision);
777 783
@@ -789,7 +795,7 @@ namespace stim { @@ -789,7 +795,7 @@ namespace stim {
789 795
790 // draw the flow direction as cone 796 // draw the flow direction as cone
791 void glSolidCone(GLint subdivision) { 797 void glSolidCone(GLint subdivision) {
792 - 798 +
793 stim::vec3<T> tmp_d; // direction 799 stim::vec3<T> tmp_d; // direction
794 stim::vec3<T> center; // cone hat center 800 stim::vec3<T> center; // cone hat center
795 stim::vec3<T> head; // cone hat top 801 stim::vec3<T> head; // cone hat top
@@ -825,7 +831,7 @@ namespace stim { @@ -825,7 +831,7 @@ namespace stim {
825 831
826 // draw main feeder as solid cube 832 // draw main feeder as solid cube
827 void glSolidCuboid(T length = 210.0f, T height = 10.0f) { 833 void glSolidCuboid(T length = 210.0f, T height = 10.0f) {
828 - 834 +
829 T width; 835 T width;
830 stim::vec3<T> L = bb.A; // get the bottom left corner 836 stim::vec3<T> L = bb.A; // get the bottom left corner
831 stim::vec3<T> U = bb.B; // get the top right corner 837 stim::vec3<T> U = bb.B; // get the top right corner
@@ -886,84 +892,96 @@ namespace stim { @@ -886,84 +892,96 @@ namespace stim {
886 892
887 // draw the bridge as lines 893 // draw the bridge as lines
888 void line_bridge() { 894 void line_bridge() {
889 -  
890 - for (unsigned i = 0; i < inlet.size(); i++) {  
891 - glBegin(GL_LINE_STRIP);  
892 - for (unsigned j = 0; j < inlet[i].V.size(); j++)  
893 - glVertex3f(inlet[i].V[j][0], inlet[i].V[j][1], inlet[i].V[j][2]);  
894 - glEnd();  
895 - }  
896 - for (unsigned i = 0; i < outlet.size(); i++) {  
897 - glBegin(GL_LINE_STRIP);  
898 - for (unsigned j = 0; j < outlet[i].V.size(); j++)  
899 - glVertex3f(outlet[i].V[j][0], outlet[i].V[j][1], outlet[i].V[j][2]);  
900 - glEnd(); 895 +
  896 + if (!glIsList(dlist)) {
  897 + dlist = glGenLists(1);
  898 + glNewList(dlist, GL_COMPILE);
  899 + for (unsigned i = 0; i < inlet.size(); i++) {
  900 + glBegin(GL_LINE_STRIP);
  901 + for (unsigned j = 0; j < inlet[i].V.size(); j++)
  902 + glVertex3f(inlet[i].V[j][0], inlet[i].V[j][1], inlet[i].V[j][2]);
  903 + glEnd();
  904 + }
  905 + for (unsigned i = 0; i < outlet.size(); i++) {
  906 + glBegin(GL_LINE_STRIP);
  907 + for (unsigned j = 0; j < outlet[i].V.size(); j++)
  908 + glVertex3f(outlet[i].V[j][0], outlet[i].V[j][1], outlet[i].V[j][2]);
  909 + glEnd();
  910 + }
  911 + glFlush();
  912 + glEndList();
901 } 913 }
902 - glFlush(); 914 + glCallList(dlist);
903 } 915 }
904 916
905 // draw the bridge as tubes 917 // draw the bridge as tubes
906 void tube_bridge(T subdivision, T radii = 5.0f) { 918 void tube_bridge(T subdivision, T radii = 5.0f) {
907 919
908 - stim::vec3<T> dir; // direction vector  
909 - stim::circle<T> unit_c; // unit circle for finding the rotation start direction  
910 - std::vector<typename stim::vec3<T> > cp1;  
911 - std::vector<typename stim::vec3<T> > cp2;  
912 -  
913 - for (unsigned i = 0; i < inlet.size(); i++) {  
914 - // render vertex as sphere  
915 - for (unsigned j = 1; j < inlet[i].V.size() - 1; j++) {  
916 - glPushMatrix();  
917 - glTranslatef(inlet[i].V[j][0], inlet[i].V[j][1], inlet[i].V[j][2]);  
918 - glutSolidSphere(radii, subdivision, subdivision);  
919 - glPopMatrix();  
920 - }  
921 - // render edge as cylinder  
922 - for (unsigned j = 0; j < inlet[i].V.size() - 1; j++) {  
923 - dir = inlet[i].V[j] - inlet[i].V[j + 1];  
924 - dir = dir.norm();  
925 - unit_c.rotate(dir);  
926 - stim::circle<T> c1(inlet[i].V[j], inlet[i].r, dir, unit_c.U);  
927 - stim::circle<T> c2(inlet[i].V[j + 1], inlet[i].r, dir, unit_c.U);  
928 - cp1 = c1.glpoints(subdivision);  
929 - cp2 = c2.glpoints(subdivision);  
930 -  
931 - glBegin(GL_QUAD_STRIP);  
932 - for (unsigned k = 0; k < cp1.size(); k++) {  
933 - glVertex3f(cp1[k][0], cp1[k][1], cp1[k][2]);  
934 - glVertex3f(cp2[k][0], cp2[k][1], cp2[k][2]); 920 + if (!glIsList(dlist + 1)) {
  921 + glNewList(dlist + 1, GL_COMPILE);
  922 +
  923 + stim::vec3<T> dir; // direction vector
  924 + stim::circle<T> unit_c; // unit circle for finding the rotation start direction
  925 + std::vector<typename stim::vec3<T> > cp1;
  926 + std::vector<typename stim::vec3<T> > cp2;
  927 +
  928 + for (unsigned i = 0; i < inlet.size(); i++) {
  929 + // render vertex as sphere
  930 + for (unsigned j = 1; j < inlet[i].V.size() - 1; j++) {
  931 + glPushMatrix();
  932 + glTranslatef(inlet[i].V[j][0], inlet[i].V[j][1], inlet[i].V[j][2]);
  933 + glutSolidSphere(radii, subdivision, subdivision);
  934 + glPopMatrix();
  935 + }
  936 + // render edge as cylinder
  937 + for (unsigned j = 0; j < inlet[i].V.size() - 1; j++) {
  938 + dir = inlet[i].V[j] - inlet[i].V[j + 1];
  939 + dir = dir.norm();
  940 + unit_c.rotate(dir);
  941 + stim::circle<T> c1(inlet[i].V[j], inlet[i].r, dir, unit_c.U);
  942 + stim::circle<T> c2(inlet[i].V[j + 1], inlet[i].r, dir, unit_c.U);
  943 + cp1 = c1.glpoints(subdivision);
  944 + cp2 = c2.glpoints(subdivision);
  945 +
  946 + glBegin(GL_QUAD_STRIP);
  947 + for (unsigned k = 0; k < cp1.size(); k++) {
  948 + glVertex3f(cp1[k][0], cp1[k][1], cp1[k][2]);
  949 + glVertex3f(cp2[k][0], cp2[k][1], cp2[k][2]);
  950 + }
  951 + glEnd();
935 } 952 }
936 - glEnd();  
937 } 953 }
938 - }  
939 954
940 - for (unsigned i = 0; i < outlet.size(); i++) {  
941 - // render vertex as sphere  
942 - for (unsigned j = 1; j < outlet[i].V.size() - 1; j++) {  
943 - glPushMatrix();  
944 - glTranslatef(outlet[i].V[j][0], outlet[i].V[j][1], outlet[i].V[j][2]);  
945 - glutSolidSphere(radii, subdivision, subdivision);  
946 - glPopMatrix();  
947 - }  
948 - // render edge as cylinder  
949 - for (unsigned j = 0; j < outlet[i].V.size() - 1; j++) {  
950 - dir = outlet[i].V[j] - outlet[i].V[j + 1];  
951 - dir = dir.norm();  
952 - unit_c.rotate(dir);  
953 - stim::circle<T> c1(outlet[i].V[j], outlet[i].r, dir, unit_c.U);  
954 - stim::circle<T> c2(outlet[i].V[j + 1], outlet[i].r, dir, unit_c.U);  
955 - cp1 = c1.glpoints(subdivision);  
956 - cp2 = c2.glpoints(subdivision);  
957 -  
958 - glBegin(GL_QUAD_STRIP);  
959 - for (unsigned k = 0; k < cp1.size(); k++) {  
960 - glVertex3f(cp1[k][0], cp1[k][1], cp1[k][2]);  
961 - glVertex3f(cp2[k][0], cp2[k][1], cp2[k][2]); 955 + for (unsigned i = 0; i < outlet.size(); i++) {
  956 + // render vertex as sphere
  957 + for (unsigned j = 1; j < outlet[i].V.size() - 1; j++) {
  958 + glPushMatrix();
  959 + glTranslatef(outlet[i].V[j][0], outlet[i].V[j][1], outlet[i].V[j][2]);
  960 + glutSolidSphere(radii, subdivision, subdivision);
  961 + glPopMatrix();
  962 + }
  963 + // render edge as cylinder
  964 + for (unsigned j = 0; j < outlet[i].V.size() - 1; j++) {
  965 + dir = outlet[i].V[j] - outlet[i].V[j + 1];
  966 + dir = dir.norm();
  967 + unit_c.rotate(dir);
  968 + stim::circle<T> c1(outlet[i].V[j], outlet[i].r, dir, unit_c.U);
  969 + stim::circle<T> c2(outlet[i].V[j + 1], outlet[i].r, dir, unit_c.U);
  970 + cp1 = c1.glpoints(subdivision);
  971 + cp2 = c2.glpoints(subdivision);
  972 +
  973 + glBegin(GL_QUAD_STRIP);
  974 + for (unsigned k = 0; k < cp1.size(); k++) {
  975 + glVertex3f(cp1[k][0], cp1[k][1], cp1[k][2]);
  976 + glVertex3f(cp2[k][0], cp2[k][1], cp2[k][2]);
  977 + }
  978 + glEnd();
962 } 979 }
963 - glEnd();  
964 } 980 }
  981 + glEndList();
965 } 982 }
966 - } 983 + glCallList(dlist + 1);
  984 + }
967 985
968 // draw gradient color bounding box outside the object 986 // draw gradient color bounding box outside the object
969 void bounding_box() { 987 void bounding_box() {
@@ -1172,7 +1190,10 @@ namespace stim { @@ -1172,7 +1190,10 @@ namespace stim {
1172 1190
1173 // automatically modify bridge to make it feasible 1191 // automatically modify bridge to make it feasible
1174 void modify_synthetic_connection(T viscosity, T rou, T radii = 5.0f) { 1192 void modify_synthetic_connection(T viscosity, T rou, T radii = 5.0f) {
1175 - 1193 +
  1194 + glDeleteLists(dlist, 1); // delete display list for modify
  1195 + glDeleteLists(dlist + 1, 1);
  1196 +
1176 // because of radii change at the port vertex, there will be a pressure drop at that port 1197 // because of radii change at the port vertex, there will be a pressure drop at that port
1177 // it follows the bernoulli equation 1198 // it follows the bernoulli equation
1178 // p1 + 1/2*rou*v1^2 + rou*g*h1 = p2 + 1/2*rou*v2^2 + rou*g*h2 1199 // p1 + 1/2*rou*v1^2 + rou*g*h1 = p2 + 1/2*rou*v2^2 + rou*g*h2