From 9b69bb4c41231d8b8297de67a13a88c61c9bc92f Mon Sep 17 00:00:00 2001 From: Jiaming Guo Date: Fri, 14 Apr 2017 13:36:37 -0500 Subject: [PATCH] add diplay list to avoid waste --- flow.h | 199 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------------------------------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/flow.h b/flow.h index e0ce02d..6ba6abc 100644 --- a/flow.h +++ b/flow.h @@ -160,14 +160,15 @@ namespace stim { #endif template - class flow : public stim::gl_network{ + class flow : public stim::gl_network { private: - + unsigned num_edge; unsigned num_vertex; - - enum direction {UP, LEFT, DOWN, RIGHT}; + GLuint dlist; // display list for inlets/outlets connections + + enum direction { UP, LEFT, DOWN, RIGHT }; // calculate the cofactor of elemen[row][col] void get_minor(T** src, T** dest, int row, int col, int order) { @@ -232,7 +233,7 @@ namespace stim { using stim::network::get_r; using stim::network::get_average_r; using stim::network::get_l; - + T** C; // Conductance std::vector > Q; // volume flow rate std::vector QQ; // Q' vector @@ -292,11 +293,16 @@ namespace stim { output.clear(); inlet.clear(); outlet.clear(); + + if (glIsList(dlist)) { + glDeleteLists(dlist, 1); // delete display list for modify + glDeleteLists(dlist + 1, 1); + } } // copy radius from cylinder to flow void set_radius(unsigned i, T radius) { - + for (unsigned j = 0; j < num_edge; j++) { if (E[j].v[0] == i) E[j].cylinder::set_r(0, radius); @@ -307,7 +313,7 @@ namespace stim { // get the radii of vertex i T get_radius(unsigned i) { - + unsigned tmp_e; // edge index unsigned tmp_v; // vertex index in that edge for (unsigned j = 0; j < num_edge; j++) { @@ -326,7 +332,7 @@ namespace stim { // get the velocity of pendant vertex i T get_velocity(unsigned i) { - + unsigned tmp_e; // edge index for (unsigned j = 0; j < num_edge; j++) { if (E[j].v[0] == i) { @@ -400,7 +406,7 @@ namespace stim { // get the inverse of conductance matrix stim::matrix _C(num_vertex, num_vertex); inversion(C, num_vertex, _C.data()); - + // get the pressure in the network for (unsigned i = 0; i < num_vertex; i++) { for (unsigned j = 0; j < num_vertex; j++) { @@ -421,7 +427,7 @@ namespace stim { Q[i].first = start_vertex; Q[i].second = end_vertex; - + Q[i].third = ((float)stim::PI * std::pow(get_average_r(i), 4) * deltaP) / (8 * u * get_l(i)); v[i] = Q[i].third / ((float)stim::PI * std::pow(get_average_r(i), 2)); } @@ -429,7 +435,7 @@ namespace stim { // get the brewer color map based on velocity void get_color_map(T& max_v, T& min_v, std::vector& color, std::vector pendant_vertex) { - + unsigned num_edge = Q.size(); unsigned num_vertex = QQ.size(); @@ -441,7 +447,7 @@ namespace stim { max_v = *std::max_element(abs_V.begin(), abs_V.end()); min_v = *std::min_element(abs_V.begin(), abs_V.end()); - + // get the color map based on velocity range along the network color.clear(); if (pendant_vertex.size() == 2 && num_edge - num_vertex + 1 <= 0) // only one inlet and one outlet @@ -472,7 +478,7 @@ namespace stim { // @param: current direct length between two vertices // @param: desire length void find_hilbert_order(T l, T d, int &order) { - + bool flag = false; int o = 1; T tmp; // temp of length @@ -489,7 +495,7 @@ namespace stim { } void move(unsigned i, T *c, direction dir, T dl, int feeder, bool invert) { - + int cof = (invert) ? -1 : 1; switch (dir) { @@ -510,7 +516,7 @@ namespace stim { stim::vec3 tmp; for (unsigned i = 0; i < 3; i++) tmp[i] = c[i]; - + if (feeder == 1) // inlet main feeder inlet[i].V.push_back(tmp); else if (feeder == 0) // outlet main feeder @@ -518,8 +524,8 @@ namespace stim { } void hilbert_curve(unsigned i, T *c, int order, T dl, int feeder, bool invert, direction dir = DOWN) { - - if (order == 1) { + + if (order == 1) { switch (dir) { case UP: move(i, c, DOWN, dl, feeder, invert); @@ -542,7 +548,7 @@ namespace stim { move(i, c, RIGHT, dl, feeder, invert); break; } - + } else if (order > 1) { switch (dir) { @@ -717,7 +723,7 @@ namespace stim { // draw solid sphere at every vertex void glSolidSphere(T max_pressure, GLint subdivision) { - + // waste? for (unsigned i = 0; i < num_edge; i++) { for (unsigned j = 0; j < E[i].size(); j++) { @@ -771,7 +777,7 @@ namespace stim { r1 = get_r(i, j); r2 = get_r(i, j + 1); - + // calculate the envelope caps find_envelope(cp1, cp2, center1, center2, r1, r2, subdivision); @@ -789,7 +795,7 @@ namespace stim { // draw the flow direction as cone void glSolidCone(GLint subdivision) { - + stim::vec3 tmp_d; // direction stim::vec3 center; // cone hat center stim::vec3 head; // cone hat top @@ -825,7 +831,7 @@ namespace stim { // draw main feeder as solid cube void glSolidCuboid(T length = 210.0f, T height = 10.0f) { - + T width; stim::vec3 L = bb.A; // get the bottom left corner stim::vec3 U = bb.B; // get the top right corner @@ -886,84 +892,96 @@ namespace stim { // draw the bridge as lines void line_bridge() { - - for (unsigned i = 0; i < inlet.size(); i++) { - glBegin(GL_LINE_STRIP); - for (unsigned j = 0; j < inlet[i].V.size(); j++) - glVertex3f(inlet[i].V[j][0], inlet[i].V[j][1], inlet[i].V[j][2]); - glEnd(); - } - for (unsigned i = 0; i < outlet.size(); i++) { - glBegin(GL_LINE_STRIP); - for (unsigned j = 0; j < outlet[i].V.size(); j++) - glVertex3f(outlet[i].V[j][0], outlet[i].V[j][1], outlet[i].V[j][2]); - glEnd(); + + if (!glIsList(dlist)) { + dlist = glGenLists(1); + glNewList(dlist, GL_COMPILE); + for (unsigned i = 0; i < inlet.size(); i++) { + glBegin(GL_LINE_STRIP); + for (unsigned j = 0; j < inlet[i].V.size(); j++) + glVertex3f(inlet[i].V[j][0], inlet[i].V[j][1], inlet[i].V[j][2]); + glEnd(); + } + for (unsigned i = 0; i < outlet.size(); i++) { + glBegin(GL_LINE_STRIP); + for (unsigned j = 0; j < outlet[i].V.size(); j++) + glVertex3f(outlet[i].V[j][0], outlet[i].V[j][1], outlet[i].V[j][2]); + glEnd(); + } + glFlush(); + glEndList(); } - glFlush(); + glCallList(dlist); } // draw the bridge as tubes void tube_bridge(T subdivision, T radii = 5.0f) { - stim::vec3 dir; // direction vector - stim::circle unit_c; // unit circle for finding the rotation start direction - std::vector > cp1; - std::vector > cp2; - - for (unsigned i = 0; i < inlet.size(); i++) { - // render vertex as sphere - for (unsigned j = 1; j < inlet[i].V.size() - 1; j++) { - glPushMatrix(); - glTranslatef(inlet[i].V[j][0], inlet[i].V[j][1], inlet[i].V[j][2]); - glutSolidSphere(radii, subdivision, subdivision); - glPopMatrix(); - } - // render edge as cylinder - for (unsigned j = 0; j < inlet[i].V.size() - 1; j++) { - dir = inlet[i].V[j] - inlet[i].V[j + 1]; - dir = dir.norm(); - unit_c.rotate(dir); - stim::circle c1(inlet[i].V[j], inlet[i].r, dir, unit_c.U); - stim::circle c2(inlet[i].V[j + 1], inlet[i].r, dir, unit_c.U); - cp1 = c1.glpoints(subdivision); - cp2 = c2.glpoints(subdivision); - - glBegin(GL_QUAD_STRIP); - for (unsigned k = 0; k < cp1.size(); k++) { - glVertex3f(cp1[k][0], cp1[k][1], cp1[k][2]); - glVertex3f(cp2[k][0], cp2[k][1], cp2[k][2]); + if (!glIsList(dlist + 1)) { + glNewList(dlist + 1, GL_COMPILE); + + stim::vec3 dir; // direction vector + stim::circle unit_c; // unit circle for finding the rotation start direction + std::vector > cp1; + std::vector > cp2; + + for (unsigned i = 0; i < inlet.size(); i++) { + // render vertex as sphere + for (unsigned j = 1; j < inlet[i].V.size() - 1; j++) { + glPushMatrix(); + glTranslatef(inlet[i].V[j][0], inlet[i].V[j][1], inlet[i].V[j][2]); + glutSolidSphere(radii, subdivision, subdivision); + glPopMatrix(); + } + // render edge as cylinder + for (unsigned j = 0; j < inlet[i].V.size() - 1; j++) { + dir = inlet[i].V[j] - inlet[i].V[j + 1]; + dir = dir.norm(); + unit_c.rotate(dir); + stim::circle c1(inlet[i].V[j], inlet[i].r, dir, unit_c.U); + stim::circle c2(inlet[i].V[j + 1], inlet[i].r, dir, unit_c.U); + cp1 = c1.glpoints(subdivision); + cp2 = c2.glpoints(subdivision); + + glBegin(GL_QUAD_STRIP); + for (unsigned k = 0; k < cp1.size(); k++) { + glVertex3f(cp1[k][0], cp1[k][1], cp1[k][2]); + glVertex3f(cp2[k][0], cp2[k][1], cp2[k][2]); + } + glEnd(); } - glEnd(); } - } - for (unsigned i = 0; i < outlet.size(); i++) { - // render vertex as sphere - for (unsigned j = 1; j < outlet[i].V.size() - 1; j++) { - glPushMatrix(); - glTranslatef(outlet[i].V[j][0], outlet[i].V[j][1], outlet[i].V[j][2]); - glutSolidSphere(radii, subdivision, subdivision); - glPopMatrix(); - } - // render edge as cylinder - for (unsigned j = 0; j < outlet[i].V.size() - 1; j++) { - dir = outlet[i].V[j] - outlet[i].V[j + 1]; - dir = dir.norm(); - unit_c.rotate(dir); - stim::circle c1(outlet[i].V[j], outlet[i].r, dir, unit_c.U); - stim::circle c2(outlet[i].V[j + 1], outlet[i].r, dir, unit_c.U); - cp1 = c1.glpoints(subdivision); - cp2 = c2.glpoints(subdivision); - - glBegin(GL_QUAD_STRIP); - for (unsigned k = 0; k < cp1.size(); k++) { - glVertex3f(cp1[k][0], cp1[k][1], cp1[k][2]); - glVertex3f(cp2[k][0], cp2[k][1], cp2[k][2]); + for (unsigned i = 0; i < outlet.size(); i++) { + // render vertex as sphere + for (unsigned j = 1; j < outlet[i].V.size() - 1; j++) { + glPushMatrix(); + glTranslatef(outlet[i].V[j][0], outlet[i].V[j][1], outlet[i].V[j][2]); + glutSolidSphere(radii, subdivision, subdivision); + glPopMatrix(); + } + // render edge as cylinder + for (unsigned j = 0; j < outlet[i].V.size() - 1; j++) { + dir = outlet[i].V[j] - outlet[i].V[j + 1]; + dir = dir.norm(); + unit_c.rotate(dir); + stim::circle c1(outlet[i].V[j], outlet[i].r, dir, unit_c.U); + stim::circle c2(outlet[i].V[j + 1], outlet[i].r, dir, unit_c.U); + cp1 = c1.glpoints(subdivision); + cp2 = c2.glpoints(subdivision); + + glBegin(GL_QUAD_STRIP); + for (unsigned k = 0; k < cp1.size(); k++) { + glVertex3f(cp1[k][0], cp1[k][1], cp1[k][2]); + glVertex3f(cp2[k][0], cp2[k][1], cp2[k][2]); + } + glEnd(); } - glEnd(); } + glEndList(); } - } + glCallList(dlist + 1); + } // draw gradient color bounding box outside the object void bounding_box() { @@ -1172,7 +1190,10 @@ namespace stim { // automatically modify bridge to make it feasible void modify_synthetic_connection(T viscosity, T rou, T radii = 5.0f) { - + + glDeleteLists(dlist, 1); // delete display list for modify + glDeleteLists(dlist + 1, 1); + // because of radii change at the port vertex, there will be a pressure drop at that port // it follows the bernoulli equation // p1 + 1/2*rou*v1^2 + rou*g*h1 = p2 + 1/2*rou*v2^2 + rou*g*h2 -- libgit2 0.21.4