-
Notifications
You must be signed in to change notification settings - Fork 3
/
index_v1_33.js
219 lines (197 loc) · 12.7 KB
/
index_v1_33.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
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
const jscad = require('@jscad/modeling')
const { curves, maths, extrusions, primitives, transforms, booleans,
colors, geometries, measurements, utils } = jscad
const { bezier } = curves
const { slice, extrudeLinear } = extrusions
const { cuboid, polygon, polyhedron } = primitives
const { intersect, subtract,union } = booleans
const { center, scale, translateX, translateY, translateZ, translate
,rotateX, rotateY, rotateZ } = transforms
const { colorize } = colors
const { geom3, poly3 } = geometries
const { vec3 } = maths
const { measureBoundingBox, measureArea } = measurements
const { degToRad } = utils
//const vv = require('./v1_33.obj')
function main () {
const sc = 1.5
const vd = volume()
const vv = polyhedron({points:vd.vertices, faces:vd.faces})
const vol = center({axes:[true,true,true]}, rotateX(degToRad(90),
scale([sc,sc,sc], vv)))
let r = [], rH = [], rV = []
let bV = measureBoundingBox(vol)
// 1°) Traverses en X (H)
const ep = 12
const lH = [ -0.4, -0.15, 0.15, 0.4 ]
fH = bV[1][0] - bV[0][0]
mH = (bV[1][0] + bV[0][0]) / 2
var trH = cuboid( {size: [
ep,
1+bV[1][1]- bV[0][1],
1+bV[1][2] - bV[0][2]]} )
for (let i=0; i< lH.length; i++){
var t = intersect(vol, translateX(fH * lH[i], trH))
if (t.polygons.length > 0)
rH.push(t)
}
// 2°) Traverses en Y (V)
const lV = [-0.4, -0.15, 0.15, 0.4 ]
fV = bV[1][1] - bV[0][1]
mV = (bV[1][1] + bV[0][1]) / 2
var trV = cuboid( {size: [
1+ bV[1][0] - bV[0][0],
ep,
1+ bV[1][2] - bV[0][2] ]} )
for (let i =0; i< lV.length; i++){
var t = intersect(vol, translateY(fV * lV[i], trV))
if (t.polygons.length > 0)
rV.push(t)
}
// 3°) Entrecroisement
var ur = union(intersect(union(rH), union(rV)));
var tmp = scission3d(ur)
var eS = [], eH = [], eV = [];
for(let i=0; i< tmp.length; i++){
let p = tmp[i];
let b = measureBoundingBox(p),
d = vec3.subtract(b[1], b[0]);
let c1 = translate([b[0][0], b[0][1] + ep/2, b[0][2]],
cuboid({size: [d[0], d[1]*2, d[2]]}));
let c2 = translate([b[0][0] + ep/2, b[0][1], b[1][2]],
cuboid({size: [d[0]*2, d[1], d[2]]}));
eH.push(intersect(tmp[i], c1));
eV.push(intersect(tmp[i], c2));
}
rH = rH.map(x=> subtract(x, eV));
rV = rV.map(x=> subtract(x, eH));
// 3d
//r.push(colorize([0,1,0], translateX(-70, vol)));
//r.push(colorize([1,0,0], translateX(32-70, rH)));
//r.push(colorize([0,0,1], translateX(-32-70, rV)));
// 2d
var dk = Math.max(fV, fH) +1
for(let ih = 0; ih < rH.length; ih++){
let b = measureBoundingBox(rH[ih]);
//r.push(translateX(dk *ih, union(vol2surf(rH[ih], 'x', b[0][0]))))
r.push(translateX(dk *ih, vol2surf(rH[ih], 'x', b[0][0])))
}
for(let iv = 0; iv < rV.length; iv++){
let b = measureBoundingBox(rV[iv]);
//r.push(translate([dk *iv, -dk], union(vol2surf(rV[iv], 'y', b[0][1]))))
r.push(translate([dk *iv, -dk], vol2surf(rV[iv], 'y', b[0][1])))
}
return r;
}
function rndColors(){return [Math.random(), Math.random(), Math.random()];}
function sortNb (E){ // returns E numerically sorted and deduplicated
return E.sort(function(a, b) {return a-b}).filter(
function(item, pos, ary) {return !pos || item != ary[pos - 1]});
}
function scission3d (geom){
var i, Pl, j, i1, j1, ok, ti, tj, z,
zz = [], P, RScission, til, tjl, tii1, zzl, zzdl;
// construit table de correspondance entre Polygones (P)
// build polygons lookup table
//P = geom.toPolygons();
P = geom.polygons;
RScission = [];
Pl = P.length;
for (i = 0; i < Pl; i++){
ti = P[i].vertices;
z = [];
for (j = 0; j < Pl; j++){
tj = P[j].vertices;
ok = false;
for (i1 = 0; i1 < ti.length; i1++){
tii1 = ti[i1];
for(j1 = 0; j1 < tj.length; j1++)
if (!ok)ok = vec3.distance(tii1, tj[j1]) < 0.01;
}
if (ok)z.push(parseInt(j));
}
z = sortNb(z);
zz.push({e:0, d:z});
}
// regroupe les correspondances des polygones se touchant
// boucle ne s'arrêtant que quand deux passages retournent le même nb de polygones
// merge lookup data from linked polygons as long as possible
ok = false;
nElOk = 0;
do {
lnElOk = nElOk;
nElOk = 0;
for (i = 0; i < zz.length; i++){
if (zz[i].e >= 0) {
nElOk++;
for (j = 0; j < zz[i].d.length; j++){
a = zz[i].d[j];
if (zz[a].e >= 0)
if (i != a) {
zz[i].d = sortNb(zz[i].d.concat(zz[a].d));
zz[a].e = -1;
}
}
}
}
ok = lnElOk == nElOk;
}while (!ok);
// construit le tableau des CSG à retourner
// build array of CSG to return
for (i = 0, zzl = zz.length; i < zzl; i++) {
if (zz[i].e >= 0) {
z = [];
for (j = 0, zzdl = zz[i].d.length; j < zzdl; j++){
z.push(P[zz[i].d[j]]);
}
if(z.length > 0) {
RScission.push(geom3.create(z));
}
}
}
return RScission;
}
function vol2surf(vol, axe, orig = 0){ // axe = 'x' | 'y' | 'z'
// retourne la surface formee par le volume avec l'axe z (à 0)
let S = [];
let X, Y, Z;
for(let n = 0; n < vol.polygons.length; n++){
let pts = [];
let P = vol.polygons[n];
let ok = true;
switch(axe){
case 'x':
X = 1; Y = 2; Z = 0;
break;
case 'y':
X = 0; Y = 2; Z = 1;
break;
case 'z':
X = 0; Y = 1; Z = 2;
break;
}
for(let i=0; (i < P.vertices.length) && ok; i++){
let pt = P.vertices[i];
if(Math.abs(pt[Z] - orig)< 0.05){
pts.push([pt[X], pt[Y]]);
} else {
ok = false;
}
}
if (ok){
if(axe == 'x'){
S.push(polygon({points:pts.reverse()}));
} else {
S.push(polygon({points:pts}));
}
}
}
return S;
}
volume = function () {
const faces =[[0,22,6],[0,29,5],[0,38,4],[1,16,17],[1,17,7],[1,25,9],[1,40,8],[2,11,7],[2,34,3],[3,23,4],[3,34,23],[4,11,3],[4,22,0],[4,23,21],[5,39,0],[5,43,39],[5,108,110],[5,110,43],[6,20,42],[6,29,0],[6,56,28],[7,11,41],[7,12,2],[7,17,12],[7,41,1],[8,27,1],[8,44,10],[9,16,1],[9,47,16],[10,27,8],[10,46,107],[10,84,109],[10,101,66],[11,2,3],[11,38,37],[12,17,18],[12,21,13],[13,2,12],[13,14,32],[13,21,23],[13,32,2],[14,23,24],[14,48,36],[14,51,49],[15,14,20],[15,51,14],[16,20,19],[16,47,15],[17,16,22],[18,22,21],[19,6,22],[19,22,16],[20,6,19],[20,16,15],[20,56,42],[20,57,52],[21,12,18],[22,4,21],[22,18,17],[23,14,13],[24,20,14],[24,23,35],[24,57,20],[25,1,26],[25,47,9],[25,53,50],[25,54,53],[26,1,27],[26,27,29],[27,108,30],[28,25,29],[28,54,25],[29,6,28],[29,25,26],[30,5,29],[30,29,27],[31,2,32],[31,34,2],[32,34,31],[32,35,33],[33,23,34],[33,34,32],[33,35,23],[35,32,36],[35,48,55],[35,57,24],[36,32,14],[36,48,35],[37,40,41],[37,41,11],[38,0,39],[38,11,4],[38,39,37],[39,44,40],[40,1,41],[40,37,39],[40,44,8],[42,56,6],[43,46,44],[43,110,45],[44,39,43],[45,46,43],[45,110,106],[46,10,44],[46,106,105],[47,51,15],[48,92,55],[49,48,14],[49,71,48],[50,47,25],[50,51,47],[50,104,51],[51,71,49],[52,56,20],[52,77,56],[53,82,50],[54,82,53],[55,57,35],[55,92,57],[56,54,28],[56,85,54],[57,77,52],[58,79,62],[58,86,64],[58,96,63],[59,73,67],[59,84,66],[59,98,65],[60,69,65],[60,89,70],[60,91,88],[61,60,68],[61,68,62],[61,91,60],[62,68,95],[62,80,61],[62,95,58],[63,86,58],[64,56,99],[64,79,58],[65,68,60],[65,74,59],[66,84,10],[66,97,59],[66,101,97],[67,82,59],[67,104,82],[68,98,94],[69,60,70],[69,74,65],[70,71,80],[70,78,69],[71,51,72],[71,77,81],[71,89,93],[72,51,104],[72,73,77],[72,104,73],[73,79,76],[73,104,67],[74,73,59],[74,75,79],[75,69,78],[75,74,69],[76,64,77],[76,77,73],[77,57,81],[77,71,72],[78,62,79],[78,79,75],[78,80,62],[79,64,76],[79,73,74],[80,78,70],[80,91,61],[80,92,90],[81,57,92],[81,80,71],[82,54,85],[82,104,50],[83,59,82],[83,82,86],[84,59,83],[84,86,87],[85,56,64],[85,64,86],[86,63,87],[86,82,85],[86,84,83],[87,63,108],[87,108,84],[88,91,89],[89,60,88],[89,71,70],[89,91,90],[90,92,89],[91,80,90],[92,48,93],[92,80,81],[93,48,71],[93,89,92],[94,95,68],[94,96,95],[96,58,95],[96,94,97],[96,100,63],[97,101,96],[98,59,97],[98,68,65],[98,97,94],[99,56,77],[99,77,64],[100,96,101],[100,103,102],[100,110,63],[101,10,103],[101,103,100],[102,103,106],[102,110,100],[105,103,107],[105,107,46],[106,46,45],[106,103,105],[107,103,10],[108,5,30],[108,63,110],[109,27,10],[109,84,108],[109,108,27],[110,102,106]]
const vertices = [[-67.95149712,40.56678972,-101.79727131],[67.29345373,41.07803323,-102.09304463],[41.56672464,-94.80517714,-164.54562879],[-42.37998194,-94.81269571,-164.66661257],[-59.54777133,-21.17190289,-144.71336051],[-37.07839153,97.94955369,-89.61502752],[-96.6428791,24.323798,-55.48761293],[58.93052561,-21.69147017,-144.36176491],[35.29697024,97.11453697,-88.98020601],[96.43351171,24.32375603,-55.60360404],[62.24033572,105.55072094,-0.35358165],[-0.26551369,-32.98586038,-164.63434429],[63.77348058,-31.28125473,-133.51020171],[47.44216521,-94.80517714,-150.28563497],[109.40021083,-94.80517714,-13.32361615],[109.40021083,-23.12452053,-13.00950743],[93.86334253,6.65953531,-59.60919728],[69.85095387,19.9926451,-97.3537831],[63.83710384,-31.49311224,-133.36032487],[-97.41490551,6.65947453,-59.7771237],[-115.43805778,-23.12452053,-13.00950743],[-67.81025642,-31.48458064,-133.51087024],[-74.64582283,19.97773114,-97.22010745],[-53.54085539,-95.00967458,-150.01448243],[-115.43805778,-95.01719315,-13.32454522],[88.2079075,43.26563968,-49.57621798],[62.99147408,43.26563968,-85.52948265],[46.66955462,81.20856425,-78.92894799],[-83.85447101,43.26563968,-49.43570364],[-68.29845034,43.26563968,-85.52948265],[-51.00679422,80.90290066,-78.79857756],[41.5873402,-112.8714948,-164.66494624],[47.46278077,-112.8714948,-150.40495242],[-53.52023982,-113.07599224,-150.13379987],[-42.35936637,-112.87901336,-164.78593002],[-116.36446458,-113.08380219,-13.2603587],[109.40021083,-112.87178619,-13.25942963],[0.014258925,-1.81152543,-157.72286253],[-29.6268699,4.09545331,-147.76237064],[-18.39218,63.6561816,-120.21320414],[17.79550089,63.23867324,-119.89579339],[29.61227858,3.83566967,-147.58657284],[-110.5159291,0.680799,-18.67473508],[-18.67195261,112.7246528,-76.31584058],[17.51572828,112.30714444,-75.99842983],[-10.56873315,127.4997519,-31.6424749],[10.15284722,127.4997519,-31.66420331],[104.95113373,-2.27458637,-14.10212313],[109.40021083,-112.87178619,-0.33855334],[109.40021083,-94.80517714,-0.33855334],[104.9392053,6.73728661,-0.33855334],[109.40021083,-32.00756546,-0.33855334],[-115.43805778,-38.94116019,-0.33855334],[88.2118865,43.26563968,-0.33855334],[-83.85447101,43.26563968,-0.33855334],[-116.36446458,-112.87396937,-0.33855334],[-110.49688032,6.74868431,-0.33855334],[-115.43805778,-94.80631802,-0.33855334],[-67.95149712,40.56678972,101.12016464],[67.29345373,41.07803323,101.41593796],[41.56672464,-94.80517714,163.86852212],[-42.37998194,-94.81269571,163.9895059],[-59.54777133,-21.17190289,144.03625383],[-37.07839153,97.94955369,88.93792085],[-96.6428791,24.323798,54.81050626],[58.93052561,-21.69147017,143.68465824],[35.29697024,97.11453697,88.30309934],[96.43351171,24.32375603,54.92649737],[-0.26551369,-32.98586038,163.95723761],[63.77348058,-31.28125473,132.83309504],[47.44216521,-94.80517714,149.6085283],[109.40021083,-94.80517714,12.64650948],[109.40021083,-23.12452053,12.33240076],[93.86334253,6.65953531,58.93209061],[69.85095387,19.9926451,96.67667643],[63.83710384,-31.49311224,132.6832182],[-97.41490551,6.65947453,59.10001703],[-115.43805778,-23.12452053,12.33240076],[-67.81025642,-31.48458064,132.83376357],[-74.64582283,19.97773114,96.54300078],[-53.54085539,-95.00967458,149.33737576],[-115.43805778,-95.01719315,12.64743855],[88.2079075,43.26563968,48.89911131],[62.99147408,43.26563968,84.85237597],[46.66955462,81.20856425,78.25184132],[-83.85447101,43.26563968,48.75859697],[-68.29845034,43.26563968,84.85237597],[-51.00679422,80.90290066,78.12147089],[41.5873402,-112.8714948,163.98783957],[47.46278077,-112.8714948,149.72784575],[-53.52023982,-113.07599224,149.4566932],[-42.35936637,-112.87901336,164.10882335],[-116.36446458,-113.08380219,12.58325203],[109.40021083,-112.87178619,12.58232296],[0.014258925,-1.81152543,157.04575586],[-29.6268699,4.09545331,147.08526396],[-18.39218,63.6561816,119.53609747],[17.79550089,63.23867324,119.21868672],[29.61227858,3.83566967,146.90946617],[-110.5159291,0.680799,17.99762841],[-18.67195261,112.7246528,75.63873391],[17.51572828,112.30714444,75.32132316],[-10.56873315,127.4997519,30.96536823],[10.15284722,127.4997519,30.98709664],[104.95113373,-2.27458637,13.42501646],[20.56230242,127.4997519,-0.33855334],[-20.84888029,127.4997519,-0.33855334],[33.42147572,121.75559123,-0.33855334],[-68.90528727,88.55821358,-0.33855334],[69.92380991,88.24145121,-0.33855334],[-62.06516681,105.24059646,-0.33855334]]
const groups = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
return {faces:faces, vertices:vertices, groups:groups}
}
module.exports = { main }