27 std::vector<point> points;
28 std::vector<measurement> measurements;
29 std::vector<angle> angles;
33 FILE *file = fopen("spec.txt", "r");
35 fscanf(file, "%u", &num_points);
37 if (num_points < points.size()) {
38 points.erase(points.begin() + num_points, points.end());
39 } else if (num_points > points.size()) {
40 for (int i = points.size(); i < num_points; ++i) {
45 p.x = rand() / (RAND_MAX + 1.0);
46 p.y = rand() / (RAND_MAX + 1.0);
57 measurements.erase(measurements.begin(), measurements.end());
58 angles.erase(angles.begin(), angles.end());
62 if (fscanf(file, "%s", &buf) != 1)
67 if (fscanf(file, "%u %u %lf %lf", &l.p1, &l.p2, &l.d, &l.alpha) != 4)
69 measurements.push_back(l);
73 if (fscanf(file, "%u %u %u %lf %lf", &a.p1, &a.p2, &a.p3, &a.rad, &a.alpha) != 5)
75 a.rad *= M_PI / 180.0;
87 double k_angle = 0.02;
90 for (unsigned i = 0; i < points.size(); ++i) {
91 points[i].ax = points[i].ay = 0.0;
94 for (unsigned i = 0; i < measurements.size(); ++i) {
95 double dx = points[measurements[i].p2].x - points[measurements[i].p1].x;
96 double dy = points[measurements[i].p2].y - points[measurements[i].p1].y;
97 double d = sqrt(dx*dx + dy*dy);
98 measurements[i].stress = fabs(d - measurements[i].d);
100 points[measurements[i].p1].ax += (d - measurements[i].d) * dx/d * k;
101 points[measurements[i].p1].ay += (d - measurements[i].d) * dy/d * k;
102 points[measurements[i].p2].ax -= (d - measurements[i].d) * dx/d * k;
103 points[measurements[i].p2].ay -= (d - measurements[i].d) * dy/d * k;
106 for (unsigned i = 0; i < angles.size(); ++i) {
107 double cx = points[angles[i].p1].x - points[angles[i].p2].x;
108 double cy = points[angles[i].p1].y - points[angles[i].p2].y;
109 double c = sqrt(cx*cx + cy*cy);
111 double bx = points[angles[i].p3].x - points[angles[i].p2].x;
112 double by = points[angles[i].p3].y - points[angles[i].p2].y;
113 double b = sqrt(bx*bx + by*by);
115 double alpha = acos((bx*cx + by*cy) / (b*c));
116 angles[i].stress = fabs(alpha - angles[i].rad);
118 double tp3x = -by / b;
119 double tp3y = bx / b;
121 double tp1x = cy / c;
122 double tp1y = -cx / c;
124 points[angles[i].p1].ax -= (alpha - angles[i].rad) * tp1x * k_angle;
125 points[angles[i].p1].ay -= (alpha - angles[i].rad) * tp1y * k_angle;
126 points[angles[i].p3].ax -= (alpha - angles[i].rad) * tp3x * k_angle;
127 points[angles[i].p3].ay -= (alpha - angles[i].rad) * tp3y * k_angle;
131 if (points.size() >= 1) {
132 points[0].ax = points[0].ay = 0.0;
134 if (points.size() >= 2) {
138 // Verlet integration
139 for (unsigned i = 0; i < points.size(); ++i) {
140 double nx = (2.0 - f) * points[i].x - (1.0 - f) * points[i].px + points[i].ax * dt * dt;
141 double ny = (2.0 - f) * points[i].y - (1.0 - f) * points[i].py + points[i].ay * dt * dt;
142 points[i].px = points[i].x;
143 points[i].py = points[i].y;
148 if (points.size() >= 2) {
149 if (points[1].x < 0.0) {
150 points[1].x = -points[1].x;
151 points[1].px = -points[1].px;
152 points[1].ax = -points[1].ax;
157 void redraw(GLWindow &win)
159 glClear(GL_COLOR_BUFFER_BIT);
162 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
163 //glEnable(GL_LINE_SMOOTH);
166 glMatrixMode(GL_PROJECTION);
168 glOrtho(-1.0, 1.0, -1.0, 1.0, 0.0, 1.0);
170 glMatrixMode(GL_MODELVIEW);
172 glScalef(0.1, 0.1, 0.1);
175 glColor4f(1.0, 1.0, 1.0, 1.0);
178 for (int i = 0; i < points.size(); ++i) {
179 glVertex2f(points[i].x, points[i].y);
184 for (int i = 0; i < measurements.size(); ++i) {
185 double sfac = std::min(5.0 * (fabs(measurements[i].stress) / measurements[i].d), 1.0);
193 g = 1.0 - 2.0 * (sfac - 0.5);
197 glColor4f(r, g, b, measurements[i].alpha);
198 glVertex2f(points[measurements[i].p1].x, points[measurements[i].p1].y);
199 glVertex2f(points[measurements[i].p2].x, points[measurements[i].p2].y);
206 void randomize(double Z)
208 for (int i = 2; i < points.size(); ++i) {
209 points[i].x += Z * (rand() / (RAND_MAX + 1.0) - .5);
210 points[i].y += Z * (rand() / (RAND_MAX + 1.0) - .5);
216 double max_stress = 0.0;
219 for (int i = 0; i < measurements.size(); ++i) {
220 if (measurements[i].p1 <= 1 ||
221 measurements[i].p2 <= 1)
224 if (measurements[i].stress > max_stress) {
225 max_stress = measurements[i].stress;
226 p1 = measurements[i].p1;
227 p2 = measurements[i].p2;
231 std::swap(points[p1], points[p2]);
236 GLWindow glw("foo", 640, 480, 32, false, 16, -1);
239 srand((time_t)time(NULL));
241 ioctl(0, FIONBIO, &one);
246 while (read(0, &ch, 1) == 1) {