Commit 6ea69c93b241f27c03b91f54792cf670b3d722bb

Authored by Jiaming Guo
1 parent 8deac51f

change bus size

Showing 2 changed files with 107 additions and 69 deletions   Show diff stats
... ... @@ -463,7 +463,7 @@ namespace stim {
463 463 // get the color map based on velocity range along the network
464 464 color.clear();
465 465 if (pendant_vertex.size() == 2 && num_edge - num_vertex + 1 <= 0) // only one inlet and one outlet
466   - color.resize(num_edge * 3, (unsigned char)255);
  466 + color.resize(num_edge * 3, (unsigned char)128);
467 467 else {
468 468 color.resize(num_edge * 3);
469 469 stim::cpu2cpu<float>(&abs_V[0], &color[0], num_edge, min_v, max_v, stim::cmBrewer);
... ... @@ -736,7 +736,7 @@ namespace stim {
736 736 }
737 737  
738 738 // draw solid sphere at every vertex
739   - void glSolidSphere(T max_pressure, GLint subdivision) {
  739 + void glSolidSphere(T max_pressure, T scale, GLint subdivision) {
740 740  
741 741 // waste?
742 742 for (unsigned i = 0; i < num_edge; i++) {
... ... @@ -750,9 +750,21 @@ namespace stim {
750 750  
751 751 glPushMatrix();
752 752 glTranslatef(E[i][0][0], E[i][0][1], E[i][0][2]);
753   - glutSolidSphere(get_r(i, 0), subdivision, subdivision);
  753 + glutSolidSphere(get_r(i, 0) * scale, subdivision, subdivision);
754 754 glPopMatrix();
755 755 }
  756 + else {
  757 + glEnable(GL_BLEND); // enable color blend
  758 + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // set blend function
  759 + glDisable(GL_DEPTH_TEST);
  760 + glColor4f(0.7f, 0.7f, 0.7f, 0.7f); // gray color
  761 + glPushMatrix();
  762 + glTranslatef(E[i][0][0], E[i][0][1], E[i][0][2]);
  763 + glutSolidSphere(get_r(i, 0) * scale, subdivision, subdivision);
  764 + glPopMatrix();
  765 + glDisable(GL_BLEND);
  766 + glEnable(GL_DEPTH_TEST);
  767 + }
756 768  
757 769 // draw the ending vertex
758 770 if (P[E[i].v[1]] != 0) {
... ... @@ -764,41 +776,26 @@ namespace stim {
764 776  
765 777 glPushMatrix();
766 778 glTranslatef(E[i][E[i].size() - 1][0], E[i][E[i].size() - 1][1], E[i][E[i].size() - 1][2]);
767   - glutSolidSphere(get_r(i, E[i].size() - 1), subdivision, subdivision);
  779 + glutSolidSphere(get_r(i, E[i].size() - 1) * scale, subdivision, subdivision);
768 780 glPopMatrix();
769 781 }
770   - //for (unsigned j = 0; j < E[i].size(); j++) {
771   - // if (j == 0) { // for start vertex
772   - // if (P[E[i].v[0]] != 0) {
773   - // stim::vec3<float> new_color;
774   - // new_color[0] = (P[E[i].v[0]] / max_pressure) > 0.5f ? 1.0f : 2.0f * P[E[i].v[0]] / max_pressure; // red
775   - // new_color[1] = 0.0f; // green
776   - // new_color[2] = (P[E[i].v[0]] / max_pressure) > 0.5f ? 1.0f - 2.0f * (P[E[i].v[0]] / max_pressure - 0.5f) : 1.0f; // blue
777   - // glColor3f(new_color[0], new_color[1], new_color[2]);
778   - // }
779   - // }
780   - // else if (j == E[i].size() - 1) { // for end vertex
781   - // if (P[E[i].v[1]] != 0) {
782   - // stim::vec3<float> new_color;
783   - // new_color[0] = (P[E[i].v[1]] / max_pressure) > 0.5f ? 1.0f : 2.0f * P[E[i].v[1]] / max_pressure; // red
784   - // new_color[1] = 0.0f; // green
785   - // new_color[2] = (P[E[i].v[1]] / max_pressure) > 0.5f ? 1.0f - 2.0f * (P[E[i].v[1]] / max_pressure - 0.5f) : 1.0f; // blue
786   - // glColor3f(new_color[0], new_color[1], new_color[2]);
787   - // }
788   - // }
789   - // else
790   - // glColor3f(0.5f, 0.5f, 0.5f); // gray point
791   -
792   - // glPushMatrix();
793   - // glTranslatef(E[i][j][0], E[i][j][1], E[i][j][2]);
794   - // glutSolidSphere(get_r(i, j), subdivision, subdivision);
795   - // glPopMatrix();
796   - //}
  782 + else {
  783 + glEnable(GL_BLEND); // enable color blend
  784 + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // set blend function
  785 + glDisable(GL_DEPTH_TEST);
  786 + glColor4f(0.7f, 0.7f, 0.7f, 0.7f); // gray color
  787 + glPushMatrix();
  788 + glTranslatef(E[i][E[i].size() - 1][0], E[i][E[i].size() - 1][1], E[i][E[i].size() - 1][2]);
  789 + glutSolidSphere(get_r(i, E[i].size() - 1) * scale, subdivision, subdivision);
  790 + glPopMatrix();
  791 + glDisable(GL_BLEND);
  792 + glEnable(GL_DEPTH_TEST);
  793 + }
797 794 }
798 795 }
799 796  
800 797 // draw edges as series of cylinders
801   - void glSolidCylinder(unsigned index, std::vector<unsigned char> color, GLint subdivision) {
  798 + void glSolidCylinder(unsigned index, std::vector<unsigned char> color, T scale, GLint subdivision) {
802 799  
803 800 stim::vec3<float> tmp_d;
804 801 stim::vec3<float> center1;
... ... @@ -813,7 +810,7 @@ namespace stim {
813 810 glEnable(GL_BLEND); // enable color blend
814 811 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // set blend function
815 812 glDisable(GL_DEPTH_TEST);
816   - glColor4f((float)color[i * 3 + 0] / 255, (float)color[i * 3 + 1] / 255, (float)color[i * 3 + 2] / 255, 0.5f);
  813 + glColor4f((float)color[i * 3 + 0] / 255, (float)color[i * 3 + 1] / 255, (float)color[i * 3 + 2] / 255, 0.3f);
817 814 }
818 815 else
819 816 glColor3f((float)color[i * 3 + 0] / 255, (float)color[i * 3 + 1] / 255, (float)color[i * 3 + 2] / 255);
... ... @@ -822,8 +819,8 @@ namespace stim {
822 819 center1 = E[i][j];
823 820 center2 = E[i][j + 1];
824 821  
825   - r1 = get_r(i, j);
826   - r2 = get_r(i, j + 1);
  822 + r1 = get_r(i, j) * scale;
  823 + r2 = get_r(i, j + 1) * scale;
827 824  
828 825 //// calculate the envelope caps
829 826 //find_envelope(cp1, cp2, center1, center2, r1, r2, subdivision);
... ... @@ -884,28 +881,31 @@ namespace stim {
884 881 glFlush();
885 882 }
886 883  
887   - // draw the flow direction as cone
888   - void glSolidCone(unsigned i, GLint subdivision) {
  884 + // draw the flow direction as cone, the size of the cone depends on the length of that edge
  885 + void glSolidCone(unsigned i, T scale, GLint subdivision) {
889 886  
890 887 stim::vec3<T> tmp_d; // direction
891 888 stim::vec3<T> center; // cone hat center
892 889 stim::vec3<T> head; // cone hat top
893 890 stim::circle<T> tmp_c;
  891 + T h; // height base of the cone
894 892 std::vector<typename stim::vec3<T> > cp;
895 893 T radius;
896 894  
897   - glColor3f(0.0f, 0.0f, 0.0f);
  895 + glColor3f(0.0f, 0.0f, 0.0f); // lime color
898 896  
899 897 unsigned index = E[i].size() / 2 - 1;
900 898 tmp_d = E[i][index + 1] - E[i][index];
  899 + h = tmp_d.len() / 3.0f; // get the height base by factor 3
901 900 tmp_d = tmp_d.norm();
902 901 center = (E[i][index + 1] + E[i][index]) / 2;
903 902 tmp_c.rotate(tmp_d);
904   - radius = (E[i].r(index + 1) + E[i].r(index)) / 2;
  903 + radius = (E[i].r(index + 1) + E[i].r(index)) / 2 * scale;
  904 + radius = (h / sqrt(3) < radius) ? h / sqrt(3) : radius; // update radius
905 905 if (v[i] > 0)
906   - head = center + tmp_d * 2 * sqrt(3) * radius;
  906 + head = center + tmp_d * h;
907 907 else
908   - head = center - tmp_d * 2 * sqrt(3) * radius;
  908 + head = center - tmp_d * h;
909 909  
910 910 stim::circle<float> c(center, radius, tmp_d, tmp_c.U);
911 911 cp = c.glpoints(subdivision);
... ... @@ -947,22 +947,25 @@ namespace stim {
947 947 stim::vec3<T> head; // cone hat top
948 948 stim::circle<T> tmp_c;
949 949 std::vector<typename stim::vec3<T> > cp;
  950 + T h;
950 951 T radius;
951 952  
952   - glColor3f(0.0f, 0.0f, 0.0f);
  953 + glColor3f(0.600f, 0.847f, 0.788f);
953 954 // draw a cone for every edge to indicate
954 955 for (unsigned i = 0; i < num_edge; i++) { // for every edge
955 956 unsigned k1 = E[i].size() / 2 - 1; // start and end index
956 957 unsigned k2 = E[i].size() / 2;
957 958 tmp_d = E[i][k2] - E[i][k1];
  959 + h = tmp_d.len() / 3.0f; // get the height base by factor 3
958 960 tmp_d = tmp_d.norm();
959 961 center = (E[i][k2] + E[i][k1]) / 2;
960 962 tmp_c.rotate(tmp_d);
961 963 radius = (E[i].r(k2) + E[i].r(k1)) / 2;
  964 + radius = (h / sqrt(3) < radius) ? h / sqrt(3) : radius; // update radius by height base if necessary
962 965 if (v[i] > 0) // if flow flows from k1 to k2
963   - head = center + tmp_d * 2 * sqrt(3) * radius;
  966 + head = center + tmp_d * h;
964 967 else
965   - head = center - tmp_d * 2 * sqrt(3) * radius;
  968 + head = center - tmp_d * h;
966 969 stim::circle<float> c(center, radius, tmp_d, tmp_c.U);
967 970 cp = c.glpoints(subdivision);
968 971  
... ... @@ -997,7 +1000,7 @@ namespace stim {
997 1000 }
998 1001  
999 1002 // draw main feeder as solid cube
1000   - void glSolidCuboid(T length = 210.0f, T height = 10.0f) {
  1003 + void glSolidCuboid(T length = 40.0f, T height = 10.0f) {
1001 1004  
1002 1005 T width;
1003 1006 stim::vec3<T> L = bb.A; // get the bottom left corner
... ... @@ -1219,11 +1222,11 @@ namespace stim {
1219 1222 }
1220 1223  
1221 1224 // mark the vertex
1222   - void mark_vertex() {
  1225 + void mark_vertex(T scale) {
1223 1226  
1224 1227 glColor3f(0.0f, 0.0f, 0.0f);
1225 1228 for (unsigned i = 0; i < num_vertex; i++) {
1226   - glRasterPos3f(V[i][0], V[i][1] + get_radius(i), V[i][2]);
  1229 + glRasterPos3f(V[i][0], V[i][1] + get_radius(i) * scale, V[i][2]);
1227 1230 std::stringstream ss;
1228 1231 ss << i;
1229 1232 glutBitmapString(GLUT_BITMAP_HELVETICA_18, (const unsigned char*)(ss.str().c_str()));
... ... @@ -1232,7 +1235,7 @@ namespace stim {
1232 1235  
1233 1236 // find the nearest vertex of current click position
1234 1237 // return true and a value if found
1235   - inline bool epsilon_vertex(T x, T y, T z, T eps, unsigned& v) {
  1238 + inline bool epsilon_vertex(T x, T y, T z, T eps, T scale, unsigned& v) {
1236 1239  
1237 1240 T d = FLT_MAX; // minimum distance between 2 vertices
1238 1241 T tmp_d = 0.0f; // temporary stores distance for loop
... ... @@ -1250,7 +1253,7 @@ namespace stim {
1250 1253 tmp_i = i; // get the index of that vertex
1251 1254 }
1252 1255 }
1253   - eps += get_radius(tmp_i); // increase epsilon accordingly
  1256 + eps += get_radius(tmp_i) * scale; // increase epsilon accordingly
1254 1257 if (d < eps) { // if current click is close to any vertex
1255 1258 v = tmp_i; // copy the extant vertex's index to v
1256 1259 return true;
... ... @@ -1292,7 +1295,7 @@ namespace stim {
1292 1295 else
1293 1296 online = false;
1294 1297  
1295   - if (tmp_d <= 0.0 || tmp_d > std::pow(v1.len(), 2) && !online) // projection lies outside the line-segment
  1298 + if (tmp_d <= 0.0 || tmp_d >= std::pow(v1.len(), 2) && !online) // projection lies outside the line-segment
1296 1299 continue;
1297 1300 else {
1298 1301 tmp_d = v1.cross(v2).len() / v1.len(); // perpendicular distance of point to segment: |v1 x v2| / |v1|
... ... @@ -1397,7 +1400,7 @@ namespace stim {
1397 1400  
1398 1401 /// build main feeder connection
1399 1402 // set up main feeder and main port of both input and output
1400   - void set_main_feeder(T border = 400.0f) {
  1403 + void set_main_feeder(T border = 100.0f) {
1401 1404  
1402 1405 // 0 means outgoing while 1 means incoming
1403 1406 stim::vec3<T> inlet_main_feeder;
... ... @@ -1466,11 +1469,11 @@ namespace stim {
1466 1469 stim::vec3<T> tmp_v; // start vertex
1467 1470 stim::vec3<T> mid_v; // middle point of the bridge
1468 1471 stim::vec3<T> bus_v; // point on the bus
1469   - x0 = main_feeder[0][0] + 100.0f; // assume bus length is 210.0f
  1472 + x0 = main_feeder[0][0] + 15.0f; // assume bus length is 40.0f
1470 1473 for (unsigned i = 0; i < input.size(); i++) {
1471 1474  
1472 1475 tmp_v = V[input[i].first];
1473   - dx = 200.0f * ((tmp_v[0] - L[0]) / box_length); // the socket position depends on proximity
  1476 + dx = 30.0f * ((tmp_v[0] - L[0]) / box_length); // the socket position depends on proximity
1474 1477 bus_v = stim::vec3<T>(x0 - dx, main_feeder[0][1], tmp_v[2]);
1475 1478 mid_v = stim::vec3<T>(x0 - dx, tmp_v[1], tmp_v[2]);
1476 1479  
... ... @@ -1486,11 +1489,11 @@ namespace stim {
1486 1489 inlet.push_back(tmp_b);
1487 1490 }
1488 1491  
1489   - x0 = main_feeder[1][0] - 100.0f;
  1492 + x0 = main_feeder[1][0] - 15.0f;
1490 1493 for (unsigned i = 0; i < output.size(); i++) {
1491 1494  
1492 1495 tmp_v = V[output[i].first];
1493   - dx = 200.0f * ((U[0] - tmp_v[0]) / box_length); // the socket position depends on proximity
  1496 + dx = 30.0f * ((U[0] - tmp_v[0]) / box_length); // the socket position depends on proximity
1494 1497 bus_v = stim::vec3<T>(x0 + dx, main_feeder[1][1], tmp_v[2]);
1495 1498 mid_v = stim::vec3<T>(x0 + dx, tmp_v[1], tmp_v[2]);
1496 1499  
... ... @@ -2196,7 +2199,7 @@ namespace stim {
2196 2199  
2197 2200 /// make binary image stack
2198 2201 // prepare for image stack
2199   - void preparation(T &Xl, T &Xr, T &Yt, T &Yb, T &Z, T length = 210.0f, T height = 10.0f) {
  2202 + void preparation(T &Xl, T &Xr, T &Yt, T &Yb, T &Z, T length = 40.0f, T height = 10.0f) {
2200 2203  
2201 2204 T max_radius = 0.0f;
2202 2205 T top = FLT_MIN;
... ...
... ... @@ -46,7 +46,8 @@ float min_v;
46 46 int mods; // special keyboard input
47 47 std::vector<unsigned char> color; // velocity color map
48 48 std::vector<int> velocity_bar; // velocity bar
49   -float length = 210.0f; // cuboid length
  49 +float length = 40.0f; // cuboid length
  50 +float scale = 1.0f; // render scale factor
50 51  
51 52 // hard-coded parameters
52 53 float camera_factor = 1.2f; // start point of the camera as a function of X and Y size
... ... @@ -74,8 +75,10 @@ bool render_direction = false; // flag indicates rendering flow direction for o
74 75 bool simulation = false; // flag indicates simulation mode
75 76 bool color_bound = false; // flag indicates velocity color map bound
76 77 bool to_select_pressure = false; // flag indicates having selected a vertex to modify pressure
  78 +bool mark_index = true; // flag indicates marking the index near the vertex
77 79 unsigned pressure_index; // the index of vertex that is clicked
78 80 unsigned direction_index = -1; // the index of edge that is pointed at
  81 +unsigned index_index = -1; // the index of the vertex
79 82  
80 83 // build inlet/outlet parameters
81 84 bool build_inlet_outlet = false; // flag indicates building inlets and outlets
... ... @@ -218,13 +221,14 @@ void glut_render() {
218 221 }
219 222 else {
220 223 flow.bounding_box();
221   - flow.glSolidSphere(max_pressure, subdivision);
222   - flow.mark_vertex();
  224 + flow.glSolidSphere(max_pressure, scale, subdivision);
  225 + if (mark_index)
  226 + flow.mark_vertex(scale);
223 227 //flow.glSolidCone(subdivision);
224   - flow.glSolidCylinder(direction_index, color, subdivision);
  228 + flow.glSolidCylinder(direction_index, color, scale, subdivision);
225 229 flow.glSolidCuboid(length);
226 230 if (render_direction)
227   - flow.glSolidCone(direction_index, subdivision);
  231 + flow.glSolidCone(direction_index, scale, subdivision);
228 232 }
229 233  
230 234 if (build_inlet_outlet) {
... ... @@ -411,8 +415,13 @@ void glut_menu(int value) {
411 415 manufacture = false;
412 416 modified_bridge = false;
413 417 connection_done = false;
414   - if (!flow.set)
  418 + // first time
  419 + if (!flow.set) {
415 420 flow_initialize();
  421 + menu_option[0] = "resimulation";
  422 + }
  423 +
  424 + // simulation / resimulation
416 425 flow_stable_state(); // main function of solving the linear system
417 426 flow.print_flow();
418 427  
... ... @@ -563,7 +572,7 @@ void glut_mouse(int button, int state, int x, int y) {
563 572 GLdouble posX, posY, posZ;
564 573 window_to_world(posX, posY, posZ); // get the world coordinates
565 574  
566   - bool flag = flow.epsilon_vertex((float)posX, (float)posY, (float)posZ, eps, pressure_index);
  575 + bool flag = flow.epsilon_vertex((float)posX, (float)posY, (float)posZ, eps, scale, pressure_index);
567 576 if (flag) {
568 577 std::vector<unsigned>::iterator it = std::find(pendant_vertex.begin(), pendant_vertex.end(), pressure_index);
569 578 if (it != pendant_vertex.end()) // if it is dangle vertex
... ... @@ -576,8 +585,8 @@ void glut_mouse(int button, int state, int x, int y) {
576 585 float tmp_pressure = (float)(vY - y - border_factor) / ((float)vY - border_factor) * max_pressure;
577 586 flow.set_pressure(pressure_index, tmp_pressure);
578 587  
579   - flow_stable_state(); // main function of solving the linear system
580   - flow.print_flow();
  588 + //flow_stable_state(); // main function of solving the linear system
  589 + //flow.print_flow();
581 590 }
582 591 }
583 592 else if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && !mods && modified_bridge && change_fragment) {
... ... @@ -720,6 +729,8 @@ void glut_mouse(int button, int state, int x, int y) {
720 729 // define camera move based on mouse wheel move
721 730 void glut_wheel(int wheel, int direction, int x, int y) {
722 731  
  732 + mods = glutGetModifiers();
  733 +
723 734 mouse_x = x;
724 735 mouse_y = y;
725 736  
... ... @@ -727,7 +738,7 @@ void glut_wheel(int wheel, int direction, int x, int y) {
727 738 window_to_world(posX, posY, posZ); // get the world coordinates
728 739  
729 740 if (!to_select_pressure) {
730   - bool flag = flow.epsilon_vertex((float)posX, (float)posY, (float)posZ, eps, pressure_index);
  741 + bool flag = flow.epsilon_vertex((float)posX, (float)posY, (float)posZ, eps, scale, pressure_index);
731 742 if (flag && simulation) {
732 743 float tmp_r;
733 744 if (direction > 0) { // increase radii
... ... @@ -741,10 +752,8 @@ void glut_wheel(int wheel, int direction, int x, int y) {
741 752 tmp_r = default_radius;
742 753 }
743 754 flow.set_radius(pressure_index, tmp_r);
744   - flow_stable_state();
745   - flow.print_flow();
746 755 }
747   - else {
  756 + else if (!mods) {
748 757 if (direction > 0) // if it is button 3(up), move closer
749 758 move_pace = zoom_factor;
750 759 else // if it is button 4(down), leave farther
... ... @@ -753,6 +762,24 @@ void glut_wheel(int wheel, int direction, int x, int y) {
753 762 cam.Push(move_pace);
754 763 }
755 764 }
  765 +
  766 + // rescale
  767 + if (mods == GLUT_ACTIVE_CTRL) {
  768 + if (direction > 0) {
  769 + if (scale >= 1)
  770 + scale += 1.0f;
  771 + else
  772 + scale += 0.1f;
  773 + }
  774 + else {
  775 + if (scale > 1)
  776 + scale -= 1.0f;
  777 + else if (scale <= 1 && scale > 0.1f)
  778 + scale -= 0.1f;
  779 + else
  780 + scale = 1.0f;
  781 + }
  782 + }
756 783  
757 784 glutPostRedisplay();
758 785 }
... ... @@ -773,6 +800,14 @@ void glut_keyboard(unsigned char key, int x, int y) {
773 800 cam.Push(move_pace);
774 801 break;
775 802  
  803 + // open/close index marks
  804 + case 'e':
  805 + if (mark_index)
  806 + mark_index = false;
  807 + else
  808 + mark_index = true;
  809 + break;
  810 +
776 811 // output image stack
777 812 case 'm':
778 813 if (manufacture) {
... ...