1
- //-----------------------------------------------------------------------------
2
- // Some sample code for slvs.dll. We draw some geometric entities, provide
3
- // initial guesses for their positions, and then constrain them. The solver
4
- // calculates their new positions, in order to satisfy the constraints.
5
- //
6
- // Copyright 2008-2013 Jonathan Westhues.
7
- //-----------------------------------------------------------------------------
8
- #include <windows.h>
1
+ /*-----------------------------------------------------------------------------
2
+ * Some sample code for slvs.dll. We draw some geometric entities, provide
3
+ * initial guesses for their positions, and then constrain them. The solver
4
+ * calculates their new positions, in order to satisfy the constraints.
5
+ *
6
+ * Copyright 2008-2013 Jonathan Westhues.
7
+ *---------------------------------------------------------------------------*/
8
+ #ifdef HAVE_CONFIG_H
9
+ # include <config.h>
10
+ #endif
11
+ #ifdef WIN32
12
+ # include <windows.h>
13
+ #endif
9
14
#include <stdio.h>
15
+ #include <stdlib.h>
16
+ #include <string.h>
17
+ #ifdef HAVE_STDINT_H
18
+ # include <stdint.h>
19
+ #endif
10
20
11
21
#include "slvs.h"
12
22
13
- Slvs_System sys ;
23
+ static Slvs_System sys ;
14
24
15
- void * CheckMalloc (size_t n )
25
+ static void * CheckMalloc (size_t n )
16
26
{
17
27
void * r = malloc (n );
18
28
if (!r ) {
@@ -22,44 +32,44 @@ void *CheckMalloc(size_t n)
22
32
return r ;
23
33
}
24
34
25
- // -----------------------------------------------------------------------------
26
- // An example of a constraint in 3d. We create a single group, with some
27
- // entities and constraints.
28
- // -----------------------------------------------------------------------------
29
- void Example3d (void )
35
+ /* -----------------------------------------------------------------------------
36
+ * An example of a constraint in 3d. We create a single group, with some
37
+ * entities and constraints.
38
+ * ---------------------------------------------------------------------------*/
39
+ static void Example3d (void )
30
40
{
31
- // This will contain a single group, which will arbitrarily number 1.
32
- int g = 1 ;
41
+ /* This will contain a single group, which will arbitrarily number 1. */
42
+ Slvs_hGroup g = 1 ;
33
43
34
- // A point, initially at (x y z) = (10 10 10)
44
+ /* A point, initially at (x y z) = (10 10 10) */
35
45
sys .param [sys .params ++ ] = Slvs_MakeParam (1 , g , 10.0 );
36
46
sys .param [sys .params ++ ] = Slvs_MakeParam (2 , g , 10.0 );
37
47
sys .param [sys .params ++ ] = Slvs_MakeParam (3 , g , 10.0 );
38
48
sys .entity [sys .entities ++ ] = Slvs_MakePoint3d (101 , g , 1 , 2 , 3 );
39
- // and a second point at (20 20 20)
49
+ /* and a second point at (20 20 20) */
40
50
sys .param [sys .params ++ ] = Slvs_MakeParam (4 , g , 20.0 );
41
51
sys .param [sys .params ++ ] = Slvs_MakeParam (5 , g , 20.0 );
42
52
sys .param [sys .params ++ ] = Slvs_MakeParam (6 , g , 20.0 );
43
53
sys .entity [sys .entities ++ ] = Slvs_MakePoint3d (102 , g , 4 , 5 , 6 );
44
- // and a line segment connecting them.
54
+ /* and a line segment connecting them. */
45
55
sys .entity [sys .entities ++ ] = Slvs_MakeLineSegment (200 , g ,
46
56
SLVS_FREE_IN_3D , 101 , 102 );
47
57
48
- // The distance between the points should be 30.0 units.
58
+ /* The distance between the points should be 30.0 units. */
49
59
sys .constraint [sys .constraints ++ ] = Slvs_MakeConstraint (
50
60
1 , g ,
51
61
SLVS_C_PT_PT_DISTANCE ,
52
62
SLVS_FREE_IN_3D ,
53
63
30.0 ,
54
64
101 , 102 , 0 , 0 );
55
65
56
- // Let's tell the solver to keep the second point as close to constant
57
- // as possible, instead moving the first point.
66
+ /* Let's tell the solver to keep the second point as close to constant
67
+ * as possible, instead moving the first point. */
58
68
sys .dragged [0 ] = 4 ;
59
69
sys .dragged [1 ] = 5 ;
60
70
sys .dragged [2 ] = 6 ;
61
71
62
- // Now that we have written our system, we solve.
72
+ /* Now that we have written our system, we solve. */
63
73
Slvs_Solve (& sys , g );
64
74
65
75
if (sys .result == SLVS_RESULT_OKAY ) {
@@ -73,25 +83,25 @@ void Example3d(void)
73
83
}
74
84
}
75
85
76
- // -----------------------------------------------------------------------------
77
- // An example of a constraint in 2d. In our first group, we create a workplane
78
- // along the reference frame's xy plane. In a second group, we create some
79
- // entities in that group and dimension them.
80
- // -----------------------------------------------------------------------------
81
- void Example2d (void )
86
+ /* -----------------------------------------------------------------------------
87
+ * An example of a constraint in 2d. In our first group, we create a workplane
88
+ * along the reference frame's xy plane. In a second group, we create some
89
+ * entities in that group and dimension them.
90
+ * ---------------------------------------------------------------------------*/
91
+ static void Example2d (void )
82
92
{
83
- int g ;
93
+ Slvs_hGroup g ;
84
94
double qw , qx , qy , qz ;
85
95
86
96
g = 1 ;
87
- // First, we create our workplane. Its origin corresponds to the origin
88
- // of our base frame (x y z) = (0 0 0)
97
+ /* First, we create our workplane. Its origin corresponds to the origin
98
+ * of our base frame (x y z) = (0 0 0) */
89
99
sys .param [sys .params ++ ] = Slvs_MakeParam (1 , g , 0.0 );
90
100
sys .param [sys .params ++ ] = Slvs_MakeParam (2 , g , 0.0 );
91
101
sys .param [sys .params ++ ] = Slvs_MakeParam (3 , g , 0.0 );
92
102
sys .entity [sys .entities ++ ] = Slvs_MakePoint3d (101 , g , 1 , 2 , 3 );
93
- // and it is parallel to the xy plane, so it has basis vectors (1 0 0)
94
- // and (0 1 0).
103
+ /* and it is parallel to the xy plane, so it has basis vectors (1 0 0)
104
+ * and (0 1 0). */
95
105
Slvs_MakeQuaternion (1 , 0 , 0 ,
96
106
0 , 1 , 0 , & qw , & qx , & qy , & qz );
97
107
sys .param [sys .params ++ ] = Slvs_MakeParam (4 , g , qw );
@@ -102,12 +112,12 @@ void Example2d(void)
102
112
103
113
sys .entity [sys .entities ++ ] = Slvs_MakeWorkplane (200 , g , 101 , 102 );
104
114
105
- // Now create a second group. We'll solve group 2, while leaving group 1
106
- // constant; so the workplane that we've created will be locked down,
107
- // and the solver can't move it.
115
+ /* Now create a second group. We'll solve group 2, while leaving group 1
116
+ * constant; so the workplane that we've created will be locked down,
117
+ * and the solver can't move it. */
108
118
g = 2 ;
109
- // These points are represented by their coordinates (u v) within the
110
- // workplane, so they need only two parameters each.
119
+ /* These points are represented by their coordinates (u v) within the
120
+ * workplane, so they need only two parameters each. */
111
121
sys .param [sys .params ++ ] = Slvs_MakeParam (11 , g , 10.0 );
112
122
sys .param [sys .params ++ ] = Slvs_MakeParam (12 , g , 20.0 );
113
123
sys .entity [sys .entities ++ ] = Slvs_MakePoint2d (301 , g , 200 , 11 , 12 );
@@ -116,11 +126,11 @@ void Example2d(void)
116
126
sys .param [sys .params ++ ] = Slvs_MakeParam (14 , g , 10.0 );
117
127
sys .entity [sys .entities ++ ] = Slvs_MakePoint2d (302 , g , 200 , 13 , 14 );
118
128
119
- // And we create a line segment with those endpoints.
129
+ /* And we create a line segment with those endpoints. */
120
130
sys .entity [sys .entities ++ ] = Slvs_MakeLineSegment (400 , g ,
121
131
200 , 301 , 302 );
122
132
123
- // Now three more points.
133
+ /* Now three more points. */
124
134
sys .param [sys .params ++ ] = Slvs_MakeParam (15 , g , 100.0 );
125
135
sys .param [sys .params ++ ] = Slvs_MakeParam (16 , g , 120.0 );
126
136
sys .entity [sys .entities ++ ] = Slvs_MakePoint2d (303 , g , 200 , 15 , 16 );
@@ -133,84 +143,85 @@ void Example2d(void)
133
143
sys .param [sys .params ++ ] = Slvs_MakeParam (20 , g , 115.0 );
134
144
sys .entity [sys .entities ++ ] = Slvs_MakePoint2d (305 , g , 200 , 19 , 20 );
135
145
136
- // And arc, centered at point 303, starting at point 304, ending at
137
- // point 305.
146
+ /* And arc, centered at point 303, starting at point 304, ending at
147
+ * point 305. */
138
148
sys .entity [sys .entities ++ ] = Slvs_MakeArcOfCircle (401 , g , 200 , 102 ,
139
149
303 , 304 , 305 );
140
150
141
- // Now one more point, and a distance
151
+ /* Now one more point, and a distance */
142
152
sys .param [sys .params ++ ] = Slvs_MakeParam (21 , g , 200.0 );
143
153
sys .param [sys .params ++ ] = Slvs_MakeParam (22 , g , 200.0 );
144
154
sys .entity [sys .entities ++ ] = Slvs_MakePoint2d (306 , g , 200 , 21 , 22 );
145
155
146
156
sys .param [sys .params ++ ] = Slvs_MakeParam (23 , g , 30.0 );
147
157
sys .entity [sys .entities ++ ] = Slvs_MakeDistance (307 , g , 200 , 23 );
148
158
149
- // And a complete circle, centered at point 306 with radius equal to
150
- // distance 307. The normal is 102, the same as our workplane.
159
+ /* And a complete circle, centered at point 306 with radius equal to
160
+ * distance 307. The normal is 102, the same as our workplane. */
151
161
sys .entity [sys .entities ++ ] = Slvs_MakeCircle (402 , g , 200 ,
152
162
306 , 102 , 307 );
153
163
154
164
155
- // The length of our line segment is 30.0 units.
165
+ /* The length of our line segment is 30.0 units. */
156
166
sys .constraint [sys .constraints ++ ] = Slvs_MakeConstraint (
157
167
1 , g ,
158
168
SLVS_C_PT_PT_DISTANCE ,
159
169
200 ,
160
170
30.0 ,
161
171
301 , 302 , 0 , 0 );
162
172
163
- // And the distance from our line segment to the origin is 10.0 units.
173
+ /* And the distance from our line segment to the origin is 10.0 units. */
164
174
sys .constraint [sys .constraints ++ ] = Slvs_MakeConstraint (
165
175
2 , g ,
166
176
SLVS_C_PT_LINE_DISTANCE ,
167
177
200 ,
168
178
10.0 ,
169
179
101 , 0 , 400 , 0 );
170
- // And the line segment is vertical.
180
+ /* And the line segment is vertical. */
171
181
sys .constraint [sys .constraints ++ ] = Slvs_MakeConstraint (
172
182
3 , g ,
173
183
SLVS_C_VERTICAL ,
174
184
200 ,
175
185
0.0 ,
176
186
0 , 0 , 400 , 0 );
177
- // And the distance from one endpoint to the origin is 15.0 units.
187
+ /* And the distance from one endpoint to the origin is 15.0 units. */
178
188
sys .constraint [sys .constraints ++ ] = Slvs_MakeConstraint (
179
189
4 , g ,
180
190
SLVS_C_PT_PT_DISTANCE ,
181
191
200 ,
182
192
15.0 ,
183
193
301 , 101 , 0 , 0 );
184
- /*
185
- // And same for the other endpoint; so if you add this constraint then
186
- // the sketch is overconstrained and will signal an error.
194
+ #if 0
195
+ /* And same for the other endpoint; so if you add this constraint then
196
+ * the sketch is overconstrained and will signal an error. */
187
197
sys .constraint [sys .constraints ++ ] = Slvs_MakeConstraint (
188
198
5 , g ,
189
199
SLVS_C_PT_PT_DISTANCE ,
190
200
200 ,
191
201
18.0 ,
192
- 302, 101, 0, 0); */
202
+ 302 , 101 , 0 , 0 );
203
+ #endif /* 0 */
193
204
194
- // The arc and the circle have equal radius.
205
+ /* The arc and the circle have equal radius. */
195
206
sys .constraint [sys .constraints ++ ] = Slvs_MakeConstraint (
196
207
6 , g ,
197
208
SLVS_C_EQUAL_RADIUS ,
198
209
200 ,
199
210
0.0 ,
200
211
0 , 0 , 401 , 402 );
201
- // The arc has radius 17.0 units.
212
+ /* The arc has radius 17.0 units. */
202
213
sys .constraint [sys .constraints ++ ] = Slvs_MakeConstraint (
203
214
7 , g ,
204
215
SLVS_C_DIAMETER ,
205
216
200 ,
206
217
17.0 * 2 ,
207
218
0 , 0 , 401 , 0 );
208
219
209
- // If the solver fails, then ask it to report which constraints caused
210
- // the problem.
220
+ /* If the solver fails, then ask it to report which constraints caused
221
+ * the problem. */
211
222
sys .calculateFaileds = 1 ;
212
223
213
- // And solve.
224
+ /* And solve. */
214
225
Slvs_Solve (& sys , g );
215
226
216
227
if (sys .result == SLVS_RESULT_OKAY ) {
@@ -253,7 +264,7 @@ int main(void)
253
264
sys .failed = CheckMalloc (50 * sizeof (sys .failed [0 ]));
254
265
sys .faileds = 50 ;
255
266
256
- // Example3d();
267
+ /* Example3d();*/
257
268
for (;;) {
258
269
Example2d ();
259
270
sys .params = sys .constraints = sys .entities = 0 ;
0 commit comments