Debian Codegraph @

texmf-dist/tex/generic/pgfplots/lua/prepcontour.lua

raw
1 #!/usr/bin/luatex
2
3 --[[
4
5 prepcontour [Lua variant] - prepare contour lines (for pgfplots)
6
7 Version: 1.4 (2021-02-22)
8
9 Copyright (C) 2020-2021 Francesco Poli <[email protected]>
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License along
22 with this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
25 --]]
26
27 -- class creation function
28 function FpolyCreateClass()
29 local newclass = {} -- this table is the created class (which will act
30 -- as metatable for the instantiated objects)
31
32 newclass.__index = newclass -- set the __index metamethod (so that
33 -- members missing from objects are searched
34 -- for in the class)
35
36 newclass.new = function (...) -- member function that will be used to
37 -- instantiate a new object of this class
38 local object = setmetatable({}, newclass) -- a table with this class
39 -- set as metatable
40
41 if object.init then
42 object:init(...) -- run the init method, if present
43 end
44
45 return object
46 end
47
48 return newclass
49 end
50
51 -- class for the coordinates (and meta value) of a point in 3D space
52 Coord = FpolyCreateClass()
53
54 function Coord:init()
55 self.x = { nil, nil, nil } -- point x,y,z coordinates
56 self.meta = nil -- point meta value
57 end
58
59 -- class for a 2D mesh of points in 3D space
60 PrepcMesh = FpolyCreateClass()
61
62 function PrepcMesh:init(yvaries, nblocks, nlines, copylines,
63 input, output)
64 if yvaries then
65 self.ni = nblocks - 1 -- number of cells in the i direction
66 self.nj = nlines - 1 -- number of cells in the j direction
67 else
68 self.ni = nlines - 1 -- number of cells in the i direction
69 self.nj = nblocks - 1 -- number of cells in the j direction
70 end
71 self.st = self.nj + 1 -- stride (number of nodes in the j direction)
72 self.si = self.nj + 1 -- stride for done_i
73 self.sj = self.nj -- stride for done_j
74 local cl = tonumber(copylines) or 0
75 self.is = input or io.stdin
76 self.os = output or io.stdout
77
78 self.debug = false
79
80 local st = self.st -- shorter name
81
82 self.coords = {} -- mesh of nodes
83 -- use single-index table for efficiency
84 -- (i,j) will be mapped to [i*st+j]
85 --[[
86 The mesh of nodes represented by this table is:
87
88 nj o o o o o o o o o o o o
89
90 ^ o o o o o o o o o o o o
91 |
92 2| o o o o o o o o o o o o
93 |
94 1| o o o o o o o o o o o o
95 |
96 j=0+ o o o o o o o o o o o o
97
98 +----------------------------->
99 i=0 1 2 ni
100 --]]
101
102 -- copy the first "cl" text lines from self.is to self.os
103 for l = 1, cl do
104 self.os:write(self.is:read('L'))
105 end
106
107 -- read all the nodes
108 for bl = 0, nblocks - 1 do
109 if bl > 0 then
110 self.is:read('L') -- this text line should be empty
111 end
112 for ln = 0, nlines - 1 do
113 if yvaries then
114 i, j = bl, ln
115 else
116 i, j = ln, bl
117 end
118 n = Coord.new()
119 n.x[1], n.x[2], n.x[3], n.meta = self.is:read('n', 'n', 'n', 'n')
120 self.coords[i*st+j] = n
121 end
122 end
123 end
124
125
126 --[[ begin core of the program logic ]]--
127
128 -- build N contour lines between meta_min and meta_max
129 function PrepcMesh:autocontour(N, meta_min, meta_max, corners, tolerance)
130 -- subdivide the meta_min÷meta_max interval into N equal sub-intervals
131 -- and pick the midpoints of those sub-intervals
132 meta_min = tonumber(meta_min)
133 meta_max = tonumber(meta_max)
134 local step = (meta_max - meta_min)/N
135 local meta_mid = (meta_max + meta_min)/2
136 local n_mid = (N + 1)/2
137 for n = 1, N do
138 local isoval = meta_mid + (n - n_mid)*step
139 self:contour(isoval, corners, tolerance)
140 end
141 end
142
143 -- build contour lines for meta==isoval
144 function PrepcMesh:contour(isoval, corners, tolerance)
145 -- set relative tolerance
146 local tol = tolerance or 1e-3
147
148 -- short names
149 local st = self.st
150 local si = self.si
151 local sj = self.sj
152
153 -- local variables
154 local i, j
155
156 if self.done_i == nil then
157 self.done_i = {} -- table of markers for i-sides
158 -- again single-index table
159 -- (i,j) will be mapped to [i*si+j]
160 --[[
161 The corresponding i-sides are:
162
163 nj o--o--o--o--o--o--o--o--o--o--o--
164
165 ^ o--o--o--o--o--o--o--o--o--o--o--
166 |
167 2| o--o--o--o--o--o--o--o--o--o--o--
168 |
169 1| o--o--o--o--o--o--o--o--o--o--o--
170 |
171 j=0+ o--o--o--o--o--o--o--o--o--o--o--
172
173 +-------------------------->
174 i=0 1 2 ni-1
175 --]]
176 end
177
178 if self.done_j == nil then
179 self.done_j = {} -- table of markers for j-sides
180 -- again single-index table
181 -- (i,j) will be mapped to [i*sj+j]
182 --[[
183 The corresponding j-sides are:
184
185 | | | | | | | | | | | |
186 nj-1 o o o o o o o o o o o o
187 | | | | | | | | | | | |
188 ^ o o o o o o o o o o o o
189 | | | | | | | | | | | | |
190 1| o o o o o o o o o o o o
191 | | | | | | | | | | | | |
192 j=0+ o o o o o o o o o o o o
193
194 +----------------------------->
195 i=0 1 2 ni
196 --]]
197 end
198
199 if self.newcoords == nil then
200 self.newcoords = {} -- nodes for contour lines will be placed here
201 end
202
203 -- scan all the i-sides, searching for
204 -- intersections with the level surface
205 for i = 0, self.ni - 1 do
206 for j = 0, self.nj do
207 self.done_i[i*si+j] = ((self.coords[ i*st+j].meta > isoval) ==
208 (self.coords[(i+1)*st+j].meta > isoval))
209 --[[
210 This is equivalent to testing the two nodes of the i-side:
211
212 j o-----o
213 i i+1
214
215 If meta > isoval in both nodes, or in none of them, then
216 set done_i to true (never look again at this i-side).
217 Otherwise, set done_i to false (we have not finished with
218 this i-side, since it contains one point of a contour line).
219 --]]
220 end
221 end
222
223 -- scan all the j-sides, searching for
224 -- intersections with the level surface
225 for i = 0, self.ni do
226 for j = 0, self.nj - 1 do
227 self.done_j[i*sj+j] = ((self.coords[i*st+j ].meta > isoval) ==
228 (self.coords[i*st+j+1].meta > isoval))
229 --[[
230 This is equivalent to testing the two nodes of the j-side:
231
232 j+1 o
233 |
234 |
235 j o
236 i
237
238 Similarly, set done_j accordingly...
239 --]]
240 end
241 end
242
243 if self.debug then
244 self:show_sides(isoval)
245 end
246
247 --[[
248 We now have all the sides containing points of the meta==isoval
249 contour lines. We need to connect the points into distinct lines.
250
251 First of all, scan all the boundary sides, searching for sides
252 with done==false. For each boundary side with done==false, set
253 its done to true and build one contour line, starting from
254 the side itself and the real cell adjacent to it.
255
256 How can we specify a cell and one of its sides?
257 We can specify cell (i,j) and a side (South, West, North, East)
258
259 N
260 j o-------o
261 | |
262 W | (i,j) | E
263 | |
264 j-1 o-------o
265 i-1 S i
266 --]]
267
268 j = 0 -- i-sides with j=0
269 for i = 0, self.ni - 1 do
270 if not self.done_i[i*si+j] then
271 self.done_i[i*si+j] = true
272 self:build_line(isoval, tol, i+1, j+1, 'S', corners)
273 if self.debug then
274 self:show_sides(isoval)
275 end
276 end
277 end
278
279 i = 0 -- j-sides with i=0
280 for j = 0, self.nj - 1 do
281 if not self.done_j[i*sj+j] then
282 self.done_j[i*sj+j] = true
283 self:build_line(isoval, tol, i+1, j+1, 'W', corners)
284 if self.debug then
285 self:show_sides(isoval)
286 end
287 end
288 end
289
290 j = self.nj -- i-sides with j=nj
291 for i = 0, self.ni - 1 do
292 if not self.done_i[i*si+j] then
293 self.done_i[i*si+j] = true
294 self:build_line(isoval, tol, i+1, j , 'N', corners)
295 if self.debug then
296 self:show_sides(isoval)
297 end
298 end
299 end
300
301 i = self.ni -- j-sides with i=ni
302 for j = 0, self.nj - 1 do
303 if not self.done_j[i*sj+j] then
304 self.done_j[i*sj+j] = true
305 self:build_line(isoval, tol, i , j+1, 'E', corners)
306 if self.debug then
307 self:show_sides(isoval)
308 end
309 end
310 end
311
312 --[[
313 Finally, scan all the internal sides, again searching for sides
314 with done==false. For each internal side with done==false, do _not_
315 alter its done value and build one contour line, starting from
316 the side itself and one of the two real cells adjacent to it.
317 --]]
318
319 -- i-sides with j=1,...,nj-1 (i-sides with j=0 are already done)
320 -- j-sides with i=1,...,ni-1 (j-sides with i=0 are already done)
321 for j = 0, self.nj - 1 do
322 for i = 0, self.ni - 1 do
323 if not self.done_i[i*si+j] then
324 self:build_line(isoval, tol, i+1, j+1, 'S', corners)
325 if self.debug then
326 self:show_sides(isoval)
327 end
328 end
329 if not self.done_j[i*sj+j] then
330 self:build_line(isoval, tol, i , j+1, 'E', corners)
331 if self.debug then
332 self:show_sides(isoval)
333 end
334 end
335 end
336 end
337 end
338
339 -- build a single contour line
340 function PrepcMesh:build_line(isoval, tol, ic, jc, side, corners)
341 -- short names
342 local st = self.st
343 local si = self.si
344 local sj = self.sj
345
346 -- local variables
347 local ia, ja, na, wa
348 local ib, jb, nb, wb
349 local ie, je, ne
350 local id, jd, nd
351 local pt, k, count, next_side
352 local cc, xi_k, eta_k, ck, above, sign_ck
353 local xi_v, eta_v, xi_0, xi_1, eta_0, eta_1
354 local next_done, next_done_idx
355
356 while true do
357 --[[
358 Start from cell (ic,jc) and its South, West, North, or East side
359
360 e d b d a N b d b
361 o-------o o-------o o-*-----o o-------o
362 | | | | | | | *
363 | c | W | c | | c | | c | E
364 | | * | | | | |
365 o----*--o o-------o o-------o o-------o
366 a S b a e e d e a
367 --]]
368
369 -- find nodes a and b
370 if side == 'E' then ia = ic else ia = ic - 1 end
371 if side == 'N' then ja = jc else ja = jc - 1 end
372 if side == 'W' then ib = ic - 1 else ib = ic end
373 if side == 'S' then jb = jc - 1 else jb = jc end
374 if ia < 0 or ib > self.ni or ja < 0 or jb > self.nj then
375 self.newcoords[#self.newcoords + 1] = Coord.new()
376 break -- out of range: abort
377 end
378 na = self.coords[ia*st+ja] -- node a
379 nb = self.coords[ib*st+jb] -- node b
380
381 -- compute the intersection point between the side
382 -- and the meta==isoval level surface
383 wa = (isoval - nb.meta)/(na.meta - nb.meta) -- weights
384 wb = 1 - wa
385
386 pt = Coord.new()
387 for k = 1, 3 do
388 pt.x[k] = wa*na.x[k] + wb*nb.x[k] -- intersection point
389 end
390 pt.meta = isoval
391 -- add the intersection point to the contour line
392 self.newcoords[#self.newcoords + 1] = pt
393
394 -- check the cell: if it is a phantom cell, then stop
395 if ic < 1 or ic > self.ni or jc < 1 or jc > self.nj then
396 self.newcoords[#self.newcoords + 1] = Coord.new()
397 break -- the contour line is finished
398 end
399
400 -- find nodes e and d
401 if side == 'W' then ie = ic else ie = ic - 1 end
402 if side == 'S' then je = jc else je = jc - 1 end
403 if side == 'E' then id = ic - 1 else id = ic end
404 if side == 'N' then jd = jc - 1 else jd = jc end
405 ne = self.coords[ie*st+je] -- node e
406 nd = self.coords[id*st+jd] -- node d
407
408 -- look at the other three sides of the cell:
409 -- how many of them have done==false ?
410 count = 0
411 if side ~= 'S' and not self.done_i[(ic-1)*si+(jc-1)] then
412 count = count + 1
413 next_side = 'N' -- next side could be the North side of the
414 -- adjacent south cell
415 end
416 if side ~= 'W' and not self.done_j[(ic-1)*sj+(jc-1)] then
417 count = count + 1
418 next_side = 'E' -- next side could be the East side of the
419 -- adjacent west cell
420 end
421 if side ~= 'N' and not self.done_i[(ic-1)*si+ jc ] then
422 count = count + 1
423 next_side = 'S' -- next side could be the South side of the
424 -- adjacent north cell
425 end
426 if side ~= 'E' and not self.done_j[ ic *sj+(jc-1)] then
427 count = count + 1
428 next_side = 'W' -- next side could be the West side of the
429 -- adjacent east cell
430 end
431
432 --[[
433 if zero other sides have done==false, then stop
434
435 o o
436
437
438 o---*-o
439 --]]
440 if count == 0 then
441 self.newcoords[#self.newcoords + 1] = Coord.new()
442 break -- the contour line is finished
443 end
444
445 --[[
446 if exactly one other side has done==false, then
447 next_side already specifies the next side to start from
448
449 o-*---o
450 |
451 \
452 o---*-o
453
454 nothing to be done for the time being...
455 --]]
456
457 --[[
458 the surface inside the cell is thought as the bilinear
459 interpolation of the four nodes and hence mapped onto
460 a unit square in the xi,eta plane:
461
462 eta ^
463 | pt.x[k] = (1-xi)*(1-eta)*na.x[k] +
464 1+------+ xi *(1-eta)*nb.x[k] +
465 |e d| (1-xi)* eta *ne.x[k] +
466 | | xi * eta *nd.x[k]
467 |a b|
468 0+------+------>
469 0 1 xi
470
471 the equation of the contour line is therefore:
472 cc*xi*eta - eta_k*xi - xi_k*eta == ck
473 --]]
474 cc = na.meta - nb.meta + nd.meta - ne.meta
475 eta_k = na.meta - nb.meta
476 xi_k = na.meta - ne.meta
477 ck = isoval - na.meta
478
479 if math.abs(cc) <=
480 math.max(math.abs(tol*eta_k), math.abs(tol*xi_k)) then
481 --[[
482 cc is negligible and the contour line is
483 basically a straight line:
484 eta_k*xi + xi_k*eta + ck == 0
485 --]]
486 cc = 0
487 else
488 --[[
489 cc is non-zero (and non-negligible) and we can divide
490 both sides of the equation by cc:
491 xi*eta - (eta_k/cc)*xi - (xi_k/cc)*eta == ck/cc
492 --]]
493 xi_k = xi_k /cc -- let's rename the coefficients
494 eta_k = eta_k/cc
495 ck = xi_k*eta_k + ck/cc
496 --[[
497 the contour line is an equilateral hyperbola (in the
498 xi,eta plane) with asymptotes xi == xi_k and eta == eta_k
499 and constant product ck:
500 (xi - xi_k)*(eta - eta_k) == ck
501 --]]
502 if math.abs(ck) <= math.abs(tol/2) then
503 --[[
504 ck is negligible and the hyperbola basically
505 degenerates into its asymptotes
506 --]]
507 ck = 0
508 end
509 end
510
511 --[[
512 if two or three other sides have done==false, then
513 choose the next side
514
515 ?
516 o-*---o
517 | * ?
518 ? * |
519 o---*-o
520
521 --]]
522 if count >= 2 then
523 -- how can we choose the next side?
524 --[[
525 if there's more than one side to choose from, cc is
526 necessarily non-zero (a straight line could not cross
527 more than two sides of the unit square) and the
528 center k of the hyperbola is necessarily inside the
529 cell (otherwise one hyperbola branch would be completely
530 outside the cell and the other branch could not cross
531 more than two sides)
532
533 hence, choose the next side by checking the quadrant
534 where the hyperbola branch crossing side a-b lies
535
536 eta ^
537 | :
538 1+--:---+
539 |e : d|
540 ······+k······
541 |a : b|
542 0+--:---+------>
543 0 : 1 xi
544 --]]
545 if ck == 0 then -- math.abs(ck) <= math.abs(tol/2)
546 -- choose side e-d
547 next_side = side
548 elseif ck < 0 then -- ck < -tol/2
549 -- choose side b-d
550 if side == 'N' or side == 'S' then
551 next_side = 'W'
552 else
553 next_side = 'S'
554 end
555 else -- ck > +tol/2
556 -- choose side a-e
557 if side == 'N' or side == 'S' then
558 next_side = 'E'
559 else
560 next_side = 'N'
561 end
562 end
563 end
564
565 if corners then -- enhanced corner algorithm
566
567 --[[
568 if, within the cell, the contour line is not straight
569 we can improve its representation by computing an
570 additional point: the vertex v of the hyperbola branch,
571 as long as it lies inside the cell
572 --]]
573 if cc ~= 0 then
574 -- depending on where the center k is, consider the
575 -- vertex above or below k
576 if eta_k < 0 then above = 1 else above = -1 end
577 -- also check the sign of ck
578 if ck < 0 then sign_ck = -1 else sign_ck = 1 end
579 -- compute the vertex v
580 xi_v = xi_k + sign_ck*above*math.sqrt(math.abs(ck))
581 eta_v = eta_k + above*math.sqrt(math.abs(ck))
582
583 --[[
584 the vertex will be considered inside the cell, as long as
585 xi_0 <= xi_v <= xi_1 and eta_0 <= eta_v <= eta_1
586 --]]
587 xi_0 = - math.abs(tol/10)
588 xi_1 = 1 + math.abs(tol/10)
589 eta_0 = math.abs(tol)
590 eta_1 = 1 + math.abs(tol/10)
591 -- eta_0 is meant to reject a vertex too close to side a-b
592 -- (which already has a contour line point!)
593 -- we should also reject a vertex too close to the next side
594 if next_side == side then
595 -- next side is e-d
596 eta_1 = 1 - math.abs(tol)
597 elseif next_side == 'W' or next_side == 'S' then
598 -- next side is b-d
599 xi_1 = 1 - math.abs(tol)
600 else -- next_side == 'E' or next_side == 'N'
601 -- next side is a-e
602 xi_0 = math.abs(tol)
603 end
604
605 if xi_0 <= xi_v and xi_v <= xi_1 and
606 eta_0 <= eta_v and eta_v <= eta_1 then
607 -- v is inside the cell: compute its actual coordinates
608 pt = Coord.new()
609 for k = 1, 3 do
610 pt.x[k] = (1-xi_v)*(1-eta_v)*na.x[k] +
611 xi_v *(1-eta_v)*nb.x[k] +
612 (1-xi_v)* eta_v *ne.x[k] +
613 xi_v * eta_v *nd.x[k]
614 end
615 pt.meta = isoval
616 -- add the vertex to the contour line
617 self.newcoords[#self.newcoords + 1] = pt
618 end
619 end
620
621 end
622
623 -- point to done value for the next side and
624 -- determine indexes for the next cell
625 -- (the other cell adjacent to the next side)
626 if next_side == 'W' then ic = ic + 1 end
627 if next_side == 'S' then jc = jc + 1 end
628 if next_side == 'N' or next_side == 'S' then
629 next_done = self.done_i
630 next_done_idx = (ic-1)*si+(jc-1)
631 else
632 next_done = self.done_j
633 next_done_idx = (ic-1)*sj+(jc-1)
634 end
635 if next_side == 'E' then ic = ic - 1 end
636 if next_side == 'N' then jc = jc - 1 end
637
638 -- if next side is already done, then stop
639 if next_done[next_done_idx] then
640 self.newcoords[#self.newcoords + 1] = Coord.new()
641 break -- the contour line is finished
642 end
643
644 -- set next side done to true and
645 -- iterate starting from this new side and the next cell
646 next_done[next_done_idx] = true
647 side = next_side
648 end
649 end
650
651 --[[ end core of the program logic ]]--
652
653
654 -- print out all the contour lines
655 function PrepcMesh:printcontours()
656 for n = 1, #self.newcoords do
657 if self.newcoords[n].x[1] then
658 self.os:write(string.format("%14.6g%14.6g%14.6g%14.6g\n",
659 self.newcoords[n].x[1],
660 self.newcoords[n].x[2],
661 self.newcoords[n].x[3],
662 self.newcoords[n].meta))
663 else
664 self.os:write('\n')
665 end
666 end
667 end
668
669 -- print a debug representation of mesh and not-done sides
670 function PrepcMesh:show_sides(isoval)
671 -- short names
672 local st = self.st
673 local si = self.si
674 local sj = self.sj
675
676 self.os:write(string.format("# contour level = %.6g \n#\n", isoval))
677 for j = self.nj, 0, -1 do
678 if j <= self.nj - 1 then
679 self.os:write("# ")
680 for i = 0, self.ni do
681 if self.done_j[i*sj+j] then
682 self.os:write(" ")
683 else
684 self.os:write(" *")
685 end
686 end
687 self.os:write("\n")
688 end
689 self.os:write("# ")
690 for i = 0, self.ni do
691 if self.coords[i*st+j].meta > isoval then
692 self.os:write("•")
693 else
694 self.os:write("o")
695 end
696 if i <= self.ni - 1 then
697 if self.done_i[i*si+j] then
698 self.os:write(" ")
699 else
700 self.os:write("*")
701 end
702 end
703 end
704 self.os:write("\n")
705 end
706 self.os:write("#\n")
707 end
708