summaryrefslogtreecommitdiff
path: root/ipl/gprocs/tieutils.icn
blob: 042e1025bc0a0021d53d05687ec3fede80e2c759 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
############################################################################
#
#	File:     tieutils.icn
#
#	Subject:  Procedures related to weaving tie-ups
#
#	Author:   Ralph E. Griswold
#
#	Date:     September 15, 2001
#
############################################################################
#
#  This file is in the public domain.
#
############################################################################
#
#	imr2tie(imr)	converts g2 image record to tie-ip
#
#	pat2tie(pat)	converts bi-level pattern to tie-up string
#
#	pat2tier(pat)	converts bi-level pattern to tie-up record
#
#	showpat(pat, size, fg, bg)
#			produces a hidden window for the pattern as a matrix
#			with the specified foreground and background colors
#
#	str2matrix(shafts, treadles, s)
#			produce matrix from binary string
#
#	testtie(s)	succeeds if s is a valid tie-up but fails otherwise
#
#	tie2imr(s)	converts tie-up to g2 image record
#
#	tie2pat(i, j, tie)
#			converts tie-up to bi-level pattern
#
#	tie2coltier(s)	creates a black/white color tieup-record for
#			  tie-up s
#
#	tie2tier(s)	creates a 0/1 tie-up record for tie-up s
#
#	tier2rstring(r)	creates a tie-up string from a tie-up record
#
#	twill(pattern, shift, shafts)
#			twill tie-up
#
#	overunder(pattern, treadles)
#			over/under tie-up structure
#
#	direct(shafts, treadles)
#			direct tie-up
#
#	satin(counter, shafts, treadles)
#			satin tie-up
#
#	tabby(shafts, treadles)
#			tabby tie-up
#
#	general(pattern, shift, rep, shafts)
#			general tie-up
#
#	exptie(expression, shafts, treadles)
#			expression tie-up
#
############################################################################
#
#  Requires:  Version 9 graphics
#
############################################################################
#
#  Links:  cells, numbers,  wopen, patutils, imrutils, patxform
#
############################################################################

link cells
link numbers
link wopen
link patutils
link patxform
link imrutils

record tie(shafts, treadles, matrix)

procedure imr2tie(imr)			#: convert image record to tie-up

   return imr.width || ";" || *imr.pixels / imr.width || ";" || imr.pixels

end

procedure pat2tie(pat)			#: convert pattern to tie-up string
   local matrix, tieup, shafts, treadles

   pat ? {		# OLD-STYLE BIT STRING TIE-UP
      if shafts := tab(upto(',')) &
      move(1) &
      treadles := tab(upto(',')) &
      move(1) then {
         matrix := list(shafts)
         while put(matrix, move(treadles))
         }
      else matrix := pat2rows(pat)
      }

   tieup := tie(*matrix[1], *matrix, matrix)

   return tier2string(tieup)

end

procedure pat2tier(pat)			#: convert pattern to tie-up record
   local matrix

   matrix := pat2rows(pat)

   return tie(*matrix[1], *matrix, matrix)

end

#  Set up empty palette grid

procedure showpat(pat, cellsize, fg, bg)	#: image of bi-level pattern
   local x, y, panel, row, rows, color, tieup

   /cellsize := 10

   rows := pat2rows(pat)

   panel := makepanel(*rows[1], *rows, cellsize, fg, bg)

   y := 1

   every row := !rows do {
      every x := 1 to *row do {
         color := if row[x] == "1" then "black" else "white"
         colorcell(panel, x, y, color)
         }
      y +:= 1
      }

   return panel
 
end

procedure str2matrix(shafts, treadles, tieup)
   local matrix

   matrix := []

   tieup ? {
      every 1 to treadles do 
         put(matrix, move(shafts))
      }

   return matrix

end

procedure testtie(s)			#: test validity of tie-up s
   local n, m, bits

   s ? {
      n := (0 < integer(tab(upto(';')))) &
      move(1) &
      m := (0 < integer(tab(upto(';')))) &
      move(1) &
      bits := tab(0)
      } | fail					# bad header

   if *(cset(bits) -- '01') > 0 then fail	# illegal characters

   if *bits ~= (n * m) then fail		# wrong length

   return s

end

procedure tie2imr(tie)			#: convert tie-up to image record
   local width

   tie ? {
      width := tab(upto(';'))
      move(1)
      tab(upto(';') + 1)
      return imstoimr(width || ",g2," || tab(0))
      }

end

procedure tie2pat(shafts, treadles, tie)	#: convert tie-up record to ims
   local tieup, matrix

   tieup := tie2tier(shafts, treadles, tie)
   matrix := tieup.matrix
   return rows2pat(matrix)

end 

procedure tie2tier(shafts, treadles, tieup)	#: create 0/1 tie-up record
   local matrix

   matrix := []

   tieup ? {
      every 1 to treadles do 
         put(matrix, move(shafts))
      }

   return tie(shafts, treadles, matrix)

end

procedure tie2coltier(tieup)		#: create color tie-up record
   local result, shafts, treadles, rec

   result := []

   if not upto(';', tieup) then		# old-style tie-up
      tieup := "8;8;" || tieup

   tieup ? {
      (
         shafts := tab(upto(';')) &
         move(1) &
         treadles := tab(upto(';')) &
         move(1)
         ) | stop("*** invalid tieup")
      every 1 to shafts do 
         put(result, tcolors(move(treadles)))
      }

   return tie(shafts, treadles, result)

end

procedure tcolors(s)
   local i, result

   result := []

   every i := 1 to *s do
      put(result, if s[i] == "0" then "black" else "white")

   return result

end

procedure tier2string(rec)	#: convert tie-up record to string
   local result

   result := ""

   every result ||:= !rec.matrix

   return result

end

procedure twill(pattern, shift, shafts, treadles)	#: twill tie-up
   local row, rows

   /treadles := shafts

   row := overunder(pattern, treadles) | fail

   rows := []

   put(rows, row)

   every 1 to shafts - 1 do
      put(rows, row := rotate(row, shift))

   return rows

end

procedure overunder(pattern, treadles)
   local row, count, i

   row := ""

   count := 1			# odd/even over/under toggle

   pattern ? {
      while ="/" do {		#  INITIAL / NEEDS TO BE REMOVED
         i := tab(many(&digits)) | fail
         row ||:= repl(count, i)
         count +:= 1
         count %:= 2
         }
      if not pos(0) then fail
      }
      
   return extend(row, treadles)

end

#  direct() supports a "generalized" tie-up when the number of shafts
#  is not the same as the number of treadles.

procedure direct(shafts, treadles)	#: direct tie-up
   local row, i, rows, swap

   /treadles := shafts			# normal direct tie-up

   if shafts ~= treadles then {
      shafts :=: treadles
      swap := 1
      }

   rows := []

   row := "1" || repl("0", treadles - 1)

   put(rows, row)

   every i := 1 to shafts - 1 do
      put(rows, row := rotate(row, -1))

   if /swap then return rows
   else return pflip(protate(rows, -90), "v")

end

procedure satin(counter, shafts, treadles)	#: satin tie-up
   local row, rows, m, k

   rows := list(shafts, repl("0", treadles))

   m := 1
   rows[1, 1]  := "1"

   every k := 2 to shafts do
      rows[k, residue(m +:= counter, shafts, 1)] := "1"

   return rows

end

procedure tabby(shafts, treadles)	#: tabby tie-up
   local rows, row, i

   rows := []

   row := repl("01", (treadles + 1) / 2)

   push(rows, row)

   every i := 1 to shafts - 1 do
      push(rows, row := rotate(row, 1))

   return rows

   return

end

procedure general(pattern, shift, rep, shafts)	#: general tie-up
   local row, rows, i

   row := overunder(pattern, shafts) | fail

   rows := []

   every 1 to rep do
      put(rows, row)

   every i := (1 to shafts - 1) \ (shafts / rep) do {
      row := rotate(row, shift)
      every 1 to rep do
         put(rows, row)
      }

   rows := rows[1+:shafts]		# trim

   return rows

end

procedure exptie(expression, shafts, treadles)	#: expression tie-up
   local output, size, row, rows, values, input

   size := shafts * treadles

   output := open("/tmp/expr.icn", "w") | {
      stop("*** cannot open file for tie-up expression")
      fail
      }

   write(output, "$include \"/tmp/include.wvp\"")
   write(output, "link seqfncs")
   write(output, "procedure main()")
   write(output, "   every write(", expression, " %  2) \\ ", size)
   write(output, "end")

   close(output)

#  remove("/tmp/seqdraft.err")

   if system("icont -s /tmp/expr >/dev/null 2>/tmp/seqdraft.err") ~= 0 then
      fail

   input := open("expr", "p")

   values := ""
   every values ||:= !input

   close(input)

   remove("expr")

   rows := []

   if *values < (shafts * treadles) then {
      stop("*** short tie-up sequence")
      fail
      }

   values ? {
      while put(rows, move(shafts))
      }

   return rows

end