-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
171 lines (151 loc) · 4.88 KB
/
common.js
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
/**
* Copyright 2021, 2022 Emanuele Paolini (emanuele.paolini@unipi.it)
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
**/
const VERSION = "1.1.0"
function $elem(s) {return $(`<${s}></${s}>`);}
function str(x) {
return JSON.stringify(x)
}
function round(x) { return Math.round(x*1E3)/1E3 }
function assert(condition, message=null) {
if (!condition) {
throw "assertion failed" + (message?": "+ message:"");
}
}
function assert_close(x1, x2, error=1E-10, message=null) {
assert(Math.abs(x1-x2) < error, message);
}
function dump(x) {
console.log(str(x))
}
function last(array) {
return array[array.length - 1]
}
function array_remove(array, item) {
for (let i=array.length; i>0;) {
i--
if (array[i] === item) {
array.splice(i, 1)
}
}
}
function some(array, f) {
for (let i=0; i<array.length; ++i) {
const r = f(array[i], i, array)
if (r) return r
}
return null
}
function signed_elements_add(signed_elements, sign, element) {
for(let i=0; i<signed_elements.length; ++i) {
if (signed_elements[i][1] === element) {
signed_elements[i][0] += sign
if (signed_elements[i][0] === 0) {
signed_elements.splice(i,1)
return 0
}
return signed_elements[i][0]
}
}
signed_elements.push([sign, element])
return sign
}
function signed_elements_remove(array, sign, element) {
if (!element) {
throw new Error("element required")
}
for (let i=array.length; i>0;) {
i--
if ((array[i][0] === sign || sign ===null) && array[i][1] === element) {
array.splice(i, 1)
}
}
}
function find_closest_vertex(chains, p) {
let dist = Infinity
let chain = null
let idx = null
chains.forEach(chain_ => {
for(let i=1; i<chain_.vertices.length-1; ++i) {
const v = chain_.vertices[i]
const d = vec_distance(v, p);
if (d < dist) {
dist = d
chain = chain_
idx = i
}
}
})
return {dist, chain, idx}
}
function find_closest_chain(chains, p) {
let dist = Infinity
let chain = null
chains.forEach(chain_ => {
const v = chain_.vertices[Math.floor(chain_.vertices.length/2)]
const d = vec_distance(v, p);
if (d < dist) {
dist = d
chain = chain_
}
})
return chain
}
function find_closest_node(vertices, p) {
return vertices.reduce(([best_d, best_v], v) => {
const d = vec_distance(v, p)
if (d<best_d) return [d, v]
else return [best_d, best_v]
}, [Infinity, null])[1]
}
function locate_path(signed_chains, start, end, sign, remove) {
// return a signed_chains following the oriented chains
// from start to end
let path = []
// dump({signed_chains, start, end, sign})
remove = remove || false
if (!remove) signed_chains = signed_chains.map(x => x) // clone
for(let v=start; v!== end;) {
let next = null
let i
for (i=0;i<signed_chains.length;++i) {
let [s, chain] = signed_chains[i]
if (chain.node(-sign*s) === v) {
next = chain.node(sign*s)
// dump({ next, s, sign, chain })
path.push([s*sign, chain])
break
}
}
if (i===signed_chains.length) return null
signed_chains.splice(i, 1)
if (next === start) return null
v = next
}
return path
}
function path_area(signed_chains) {
let area = 0.0
signed_chains.forEach( ([sign, chain]) => {
area += sign * chain.area()
})
return area
}
function compare_json(one, two) {
if (Array.isArray(one)) {
if (!Array.isArray(two)) throw new Error()
if (one.length !== two.length) throw new Error()
for (let i=0; i< one.length; ++i) compare_json(one[i], two[i])
} else if (typeof(one) === 'object') {
if (!typeof(two) === 'object') throw new Error()
const keys = Object.keys(one).sort()
compare_json(keys, Object.keys(two).sort())
keys.forEach(key => compare_json(one[key], two[key]))
} else {
if (one !== two) throw new Error(`${one}!=${two}`)
}
return true
}