Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2 : odd result of measureArea() #570

Closed
minatani opened this issue Apr 26, 2020 · 9 comments
Closed

V2 : odd result of measureArea() #570

minatani opened this issue Apr 26, 2020 · 9 comments
Labels

Comments

@minatani
Copy link

Expected Behavior

An object with area, bounds and volume is located at the position pointed by translate().

Actual Behavior

In some case, an object's area is disappeared by translate() with certain values.

Steps to Reproduce the Problem

The problem can be reproduced by this script.

// V2

const { cuboid, cylinder } = require('@jscad/modeling').primitives
const { translateX, translateY } = require('@jscad/modeling').transforms
const { subtract } = require('@jscad/modeling').booleans
const { measureArea, measureBounds, measureVolume } = require('@jscad/modeling').measurements

const main = () => {
const plateZ=1;
const len=100;
const holeRad=3;
const holeDis=9.5;
let ret;
let binder=translateX([len/2],
cuboid({size: [len, holeDis, plateZ]}));
for (i=holeDis/2; i <=len-holeDis/2; i+=holeDis ) { // punching loose-leaf notebook's holes
binder=subtract(binder, translateX([i],
cylinder({ startRadius: holeRad, endRadius: holeRad, height: plateZ })));
}
for (y=0; y<=3; y+=0.1) {
ret=translateY([y], binder);
console.log(y pos: ${y}, Area: ${measureArea(ret)}, Bounds: ${measureBounds(ret)}, Volume: ${measureVolume(ret)}\n)
}
return [ ret ]
}

module.exports = { main }

Specifications

  • Version: V2 1b159c0
  • Platform: Debian Stretch, nodejs v9.15.2
  • Environment: cli
@z3dev
Copy link
Member

z3dev commented Apr 26, 2020

Please change translateX([len/2], ...) to translateX(len/2, ...)

These functions are...

translateX( NUMBER, ...objects )
translateY( NUMBER, ...objects )

Hope that helps.

@minatani
Copy link
Author

Thank you for your kind reply.
But, the result is same even if parameters of translateX()/translateY() are corrected.

JSCAD: generating output

from: binder-test.js
to: ../stl/binder-test.stl �[33m(STereoLithography, Binary)

y pos: 0, Area: 1767.8944694294753, Bounds: 0,-4.75,-0.5,100,4.75,0.5, Volume: 681.3996182477186

y pos: 0.1, Area: 1773.7473318485027, Bounds: 0,-4.650000095367432,-0.5,100,4.849999904632568,0.5, Volume: 681.3996225578863

y pos: 0.2, Area: 1773.7473317320587, Bounds: 0,-4.550000190734863,-0.5,100,4.949999809265137,0.5, Volume: 681.3996186298648

y pos: 0.30000000000000004, Area: NaN, Bounds: 0,-4.449999809265137,-0.5,100,5.050000190734863,0.5, Volume: 681.3996235487045

y pos: 0.4, Area: 1767.8944691418465, Bounds: 0,-4.349999904632568,-0.5,100,5.150000095367432,0.5, Volume: 681.3996183980468

y pos: 0.5, Area: 1767.8944678037044, Bounds: 0,-4.25,-0.5,100,5.25,0.5, Volume: 681.3996190990447

y pos: 0.6, Area: NaN, Bounds: 0,-4.150000095367432,-0.5,100,5.349999904632568,0.5, Volume: 681.399616254163

y pos: 0.7, Area: NaN, Bounds: 0,-4.050000190734863,-0.5,100,5.449999809265137,0.5, Volume: 681.3996229324766

y pos: 0.7999999999999999, Area: NaN, Bounds: 0,-3.950000047683716,-0.5,100,5.550000190734863,0.5, Volume: 681.3996392061447

y pos: 0.8999999999999999, Area: NaN, Bounds: 0,-3.8499999046325684,-0.5,100,5.650000095367432,0.5, Volume: 681.3996208583865

y pos: 0.9999999999999999, Area: 1773.7472879814206, Bounds: 0,-3.75,-0.5,100,5.75,0.5, Volume: 681.3996177179633

y pos: 1.0999999999999999, Area: NaN, Bounds: 0,-3.6500000953674316,-0.5,100,5.849999904632568,0.5, Volume: 681.3996214888167

y pos: 1.2, Area: NaN, Bounds: 0,-3.549999952316284,-0.5,100,5.949999809265137,0.5, Volume: 681.3996094907109

y pos: 1.3, Area: NaN, Bounds: 0,-3.450000047683716,-0.5,100,6.050000190734863,0.5, Volume: 681.3996318118301

y pos: 1.4000000000000001, Area: NaN, Bounds: 0,-3.3499999046325684,-0.5,100,6.150000095367432,0.5, Volume: 681.3996180532441

y pos: 1.5000000000000002, Area: 1767.8944321269767, Bounds: 0,-3.25,-0.5,100,6.25,0.5, Volume: 681.3996236057928

y pos: 1.6000000000000003, Area: NaN, Bounds: 0,-3.1500000953674316,-0.5,100,6.349999904632568,0.5, Volume: 681.3996211386387

y pos: 1.7000000000000004, Area: NaN, Bounds: 0,-3.049999952316284,-0.5,100,6.449999809265137,0.5, Volume: 681.3996082361081

y pos: 1.8000000000000005, Area: NaN, Bounds: 0,-2.950000047683716,-0.5,100,6.550000190734863,0.5, Volume: 681.3996309595591

y pos: 1.9000000000000006, Area: NaN, Bounds: 0,-2.8499999046325684,-0.5,100,6.650000095367432,0.5, Volume: 681.399621775875

y pos: 2.0000000000000004, Area: 1767.8944321988454, Bounds: 0,-2.75,-0.5,100,6.75,0.5, Volume: 681.3996209918657

y pos: 2.1000000000000005, Area: NaN, Bounds: 0,-2.6500000953674316,-0.5,100,6.849999904632568,0.5, Volume: 681.3996138859183

y pos: 2.2000000000000006, Area: NaN, Bounds: 0,-2.549999952316284,-0.5,100,6.949999809265137,0.5, Volume: 681.3996091818095

y pos: 2.3000000000000007, Area: NaN, Bounds: 0,-2.450000047683716,-0.5,100,7.050000190734863,0.5, Volume: 681.3996348221616

y pos: 2.400000000000001, Area: NaN, Bounds: 0,-2.3499999046325684,-0.5,100,7.150000095367432,0.5, Volume: 681.3996062258217

y pos: 2.500000000000001, Area: NaN, Bounds: 0,-2.25,-0.5,100,7.25,0.5, Volume: 681.3996218566804

y pos: 2.600000000000001, Area: NaN, Bounds: 0,-2.1500000953674316,-0.5,100,7.349999904632568,0.5, Volume: 681.3996136115277

y pos: 2.700000000000001, Area: 1761.962705186312, Bounds: 0,-2.049999952316284,-0.5,100,7.449999809265137,0.5, Volume: 681.3996092097775

y pos: 2.800000000000001, Area: NaN, Bounds: 0,-1.9500000476837158,-0.5,100,7.550000190734863,0.5, Volume: 681.3996317419461

y pos: 2.9000000000000012, Area: NaN, Bounds: 0,-1.8499999046325684,-0.5,100,7.650000095367432,0.5, Volume: 681.3996105219315

@z3dev
Copy link
Member

z3dev commented Apr 27, 2020

@minatani thanks again. there's an issue with the calculation of area, i.e. measureArea()

It seems to be related to the Float32Array values, as the results are inconsistent, even after a small fix.

y: 0, a: 1767.8944694294753, v: 681.3996182477186
y: 0.1, a: 1773.7473318485027, v: 681.3996225578863
y: 0.2, a: 1773.7473317320587, v: 681.3996186298648
y: 0.30000000000000004, a: 1764.928558634499, v: 681.3996235487045
y: 0.4, a: 1767.8944691418465, v: 681.3996183980468
y: 0.5, a: 1767.8944678037044, v: 681.3996190990447
y: 0.6, a: 1773.7473170853725, v: 681.399616254163
y: 0.7, a: 1773.7473157766622, v: 681.3996229324766
y: 0.7999999999999999, a: 1770.7815040039052, v: 681.3996392061447
y: 0.8999999999999999, a: 1767.8944090107973, v: 681.3996208583865
y: 0.9999999999999999, a: 1773.7472879814206, v: 681.3996177179633
y: 1.0999999999999999, a: 1767.8944582307183, v: 681.3996214888167
y: 1.2, a: 1764.928554816633, v: 681.3996094907109
y: 1.3, a: 1764.928639300917, v: 681.3996318118301
y: 1.4000000000000001, a: 1762.041544237735, v: 681.3996180532441
y: 1.5000000000000002, a: 1767.8944321269767, v: 681.3996236057928
y: 1.6000000000000003, a: 1767.8944582307183, v: 681.3996211386387
y: 1.7000000000000004, a: 1764.9285547687196, v: 681.3996082361081
y: 1.8000000000000005, a: 1764.9286392530034, v: 681.3996309595591
y: 1.9000000000000006, a: 1762.0415441179528, v: 681.399621775875
y: 2.0000000000000004, a: 1767.8944321988454, v: 681.3996209918657
y: 2.1000000000000005, a: 1773.7472686816834, v: 681.3996138859183
y: 2.2000000000000006, a: 1770.7814051998012, v: 681.3996091818095
y: 2.3000000000000007, a: 1770.7814896840846, v: 681.3996348221616
y: 2.400000000000001, a: 1773.7472686816834, v: 681.3996062258217
y: 2.500000000000001, a: 1773.747252243277, v: 681.3996218566804
y: 2.600000000000001, a: 1767.8944141317932, v: 681.3996136115277
y: 2.700000000000001, a: 1761.962705186312, v: 681.3996092097775
y: 2.800000000000001, a: 1767.8155274878256, v: 681.3996317419461
y: 2.9000000000000012, a: 1767.8944141317932, v: 681.3996105219315

If I switch to Number values then the results are consistent.

y: 0, a: 1765.3497124738026, v: 680.0000000000005
y: 0.1, a: 1765.3497124738026, v: 680.0000000000002
y: 0.2, a: 1765.3497124738026, v: 680.0000000000005
y: 0.30000000000000004, a: 1765.3497124738028, v: 680.0000000000001
y: 0.4, a: 1765.3497124738028, v: 680.0000000000003
y: 0.5, a: 1765.3497124738028, v: 680.0000000000002
y: 0.6, a: 1765.3497124738026, v: 680.0000000000003
y: 0.7, a: 1765.3497124738026, v: 680.0000000000007
y: 0.7999999999999999, a: 1765.3497124738028, v: 680.0000000000002
y: 0.8999999999999999, a: 1765.3497124738026, v: 680.0000000000005
y: 0.9999999999999999, a: 1765.3497124738026, v: 680.0000000000007
y: 1.0999999999999999, a: 1765.3497124738028, v: 680.0000000000005
y: 1.2, a: 1765.3497124738026, v: 680.0000000000003
y: 1.3, a: 1765.3497124738028, v: 680.0000000000007
y: 1.4000000000000001, a: 1765.3497124738026, v: 680.0000000000003
y: 1.5000000000000002, a: 1765.3497124738028, v: 680.0000000000002
y: 1.6000000000000003, a: 1765.3497124738026, v: 680.0000000000005
y: 1.7000000000000004, a: 1765.3497124738028, v: 680.0000000000005
y: 1.8000000000000005, a: 1765.3497124738026, v: 680.0000000000007
y: 1.9000000000000006, a: 1765.3497124738028, v: 680.0000000000006
y: 2.0000000000000004, a: 1765.3497124738026, v: 680.0000000000006
y: 2.1000000000000005, a: 1765.3497124738028, v: 680.0000000000005
y: 2.2000000000000006, a: 1765.3497124738026, v: 680.0000000000006
y: 2.3000000000000007, a: 1765.3497124738028, v: 680.0000000000006
y: 2.400000000000001, a: 1765.3497124738026, v: 680.0000000000005
y: 2.500000000000001, a: 1765.3497124738028, v: 680.0000000000005
y: 2.600000000000001, a: 1765.3497124738026, v: 680.0000000000006
y: 2.700000000000001, a: 1765.3497124738028, v: 680.0000000000007
y: 2.800000000000001, a: 1765.3497124738028, v: 680.0000000000003
y: 2.9000000000000012, a: 1765.3497124738028, v: 680.0000000000002

@z3dev
Copy link
Member

z3dev commented Apr 27, 2020

UG. The number of polygons is totally different as well.

Float32Array : 362 polygons
Number: 260 polygons

This is going to take some time... UG

@z3dev
Copy link
Member

z3dev commented Apr 27, 2020

I looked into this further.

This is an issue with the reTesselateCoplanarPolygons() function. This function is merging/adding polygons to the geometry, and obviously has MAJOR issues if using Float32Array.

@z3dev
Copy link
Member

z3dev commented Apr 27, 2020

Good news. I wrote a new function called mergeCoplanarPolygons() to replace reTesselateCoplanarPolygons(). After making the switch...

y: 0, a: 1758.2897793046259, v: 680.0000281924738
y: 0.1, a: 1758.2897778645586, v: 680.0000279028617
y: 0.2, a: 1758.2897698433073, v: 680.0000179016224
y: 0.30000000000000004, a: 1758.2897917867758, v: 680.0000325041563
y: 0.4, a: 1758.289780108269, v: 680.0000341569478
y: 0.5, a: 1758.2897794829437, v: 680.0000282458033
y: 0.6, a: 1758.289779676747, v: 680.0000167293766
y: 0.7, a: 1758.2897631585029, v: 680.0000241641173
y: 0.7999999999999999, a: 1758.289826408774, v: 680.0000509627844
y: 0.8999999999999999, a: 1758.289775983483, v: 680.0000322173146
y: 0.9999999999999999, a: 1758.2897782801392, v: 680.0000302566373
y: 1.0999999999999999, a: 1758.2897813435984, v: 680.0000276050015
y: 1.2, a: 1758.2897375326584, v: 680.0000088949258
y: 1.3, a: 1758.2898231921508, v: 680.0000484535009
y: 1.4000000000000001, a: 1758.289777590321, v: 680.0000323807484
y: 1.5000000000000002, a: 1758.2897762585026, v: 680.0000306774015
y: 1.6000000000000003, a: 1758.28977883004, v: 680.0000286949361
y: 1.7000000000000004, a: 1758.289732551647, v: 680.0000181414407
y: 1.8000000000000005, a: 1758.289827584198, v: 680.0000482774943
y: 1.9000000000000006, a: 1758.2897801689155, v: 680.0000270477339
y: 2.0000000000000004, a: 1758.2897759259397, v: 680.0000272686548
y: 2.1000000000000005, a: 1758.2897798055894, v: 680.0000218140627
y: 2.2000000000000006, a: 1758.2897329040527, v: 680.0000075045967
y: 2.3000000000000007, a: 1758.2898279366038, v: 680.0000502504489
y: 2.400000000000001, a: 1758.2897797964886, v: 680.0000241298765
y: 2.500000000000001, a: 1758.289777767429, v: 680.0000256866264
y: 2.600000000000001, a: 1758.2897783225858, v: 680.0000277191657
y: 2.700000000000001, a: 1758.2897321279813, v: 680.0000109852845
y: 2.800000000000001, a: 1758.2898284630703, v: 680.0000473587893
y: 2.9000000000000012, a: 1758.289779056813, v: 680.0000202794564

And there are now 470 polygons.

FYI, the subtract will produce 498 polygons without some kind of merge/retesselation.

@z3dev z3dev changed the title V2 : odd result of translate() V2 : odd result of measureArea() Apr 27, 2020
@minatani
Copy link
Author

Thank you for your immediate investigation and fix.
I'm looking forward to commit this improvement.

@z3dev
Copy link
Member

z3dev commented Jul 20, 2020

@minatani The calculation of volumes should be consistent now. Please try the latest version of V2 branch.

If resolved then close this issue.

@minatani
Copy link
Author

Confirmed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants