OpenJSCAD.org icon indicating copy to clipboard operation
OpenJSCAD.org copied to clipboard

Missing breakouts in my model due to eliminated allocations of measureBoundingBox

Open jue89 opened this issue 2 years ago • 10 comments

Expected Behavior

This model creates two STL files for a 3D-printable enclosure:

https://github.com/jue89/homie/tree/main/devices/relay/housing

With the old version of @jscad/modeling everything looks fine:

Screenshot 2021-12-17 at 17 15 45

Actual Behavior

The subtract() on one screw hole doesn't seem to be applied. I've used git bisect to identify the commit breaking my model: 8abe361fc0249ec10e438382353b316aec4644a4

Screenshot 2021-12-17 at 17 14 23

Steps to Reproduce the Problem

  1. Clone the repo stated above, checkout 959d6b6e6c94df76b4af7d1000838112e0785d16 and cd into the dir devices/relay/housing
  2. npm install && node mains-relay.js and see everything working ... (package-lock.json picks the old version)
  3. cd node_modules/quickbox && npm install && cd ../.. && node mains-relay.js and observe the missing screw hole.

Specifications

jue89 avatar Dec 17 '21 16:12 jue89

Those are the lines creating the breakouts: https://github.com/jue89/node-quickbox/blob/d84e8b3d89ee814a8a9eeb87933413dcf657a587/lib/box.js#L151-L154

jue89 avatar Dec 17 '21 16:12 jue89

UG… I’ll look at the recent changes but those should not have changed any behavior. I suspect that the caching logic has issues.

z3dev avatar Dec 18 '21 12:12 z3dev

@jue89 can you give an example of box constructor that produces model in screenshots ? I need a sample code to test

edit: nvm, found it

hrgdavor avatar Dec 20 '21 12:12 hrgdavor

@z3dev

UG… I’ll look at the recent changes but those should not have changed any behavior. I suspect that the caching logic has issues.

The issue is that applyTransforms is not respecting immutability. I think it was the next step that we decided to do separately and forgot to do. I will make i PR with fix applyTransforms and it should also fix this issue.

ofc. it then causes issue with caching. the inline change of polygons of geometry has no performance benefit, so it is definitely better to make applyTransforms respect immutability

hrgdavor avatar Dec 20 '21 13:12 hrgdavor

Not sure if it related to this issue I discovered on the weekend...

const {
	color, connectors, geometries, maths, primitives, text, utils,
	booleans, expansions, extrusions, hulls, measurements, transforms
} = jscad

console.log(jscad);
const { cuboid, roundedCuboid, cylinder, sphere } = primitives;
const { translate, translateZ, translateX, rotate, scale, align } = transforms;
const { measureBoundingBox, measureAggregateBoundingBox } = measurements;
const { vectorText } = text;
const { hull, hullChain } = hulls;
const { union } = booleans;


function getParameterDefinitions () {
  return [
		{ name: 'label', initial: 'A', type: 'text', caption: 'text to render', size: 20 },
		{ name: 'textmargin', initial: 5, type: 'number', caption: 'text margin', size: 20 },
		{ name: 'textScale', initial: 1, type: 'number', caption: 'textScale', size: 20 },
		{ name: 'textRadius', initial: 5, type: 'number', caption: 'text radius', size: 20 }
  ];
}

var letterOffset = 0;
var p = {
	baseHeight: 13.58,
	channelHeight: 17.3, // 17.6
	baseThickness: 3,
	channelThickness: 1.65, // 1.85,
	textScale: 0.4,
	textMargin: 2,
};

function main (param) {
	p.textScale *= param.textScale;
	let textObj = buildRoundText(param.label, param.textRadius);
	return union(
		textObj,
		base(param, textObj)
	)
}

function base(param, textObj) {
	let boundsText = measureBoundingBox(textObj);
	console.log('Bounds of Text', boundsText);
	let boundsUnion = measureBoundingBox(union(textObj, cuboid()));
	console.log('Bounds of union(text, cube())', boundsUnion);
	const bounds = boundsUnion; //boundsText fails, but x bounds shouls be identical

	let textWidth = bounds[1][0] - bounds[0][0] + 2 * param.textmargin;
	let base = union(
		roundedCuboid({center: [0,0,p.baseThickness/2], size:[textWidth,p.baseHeight,p.baseThickness]}),
		roundedCuboid({center: [0,0,p.channelThickness/2], size:[textWidth,p.channelHeight,p.channelThickness]})
	);
  base = align({relativeTo:[0,0,0], modes:['center', 'center', 'min']}, base);
	return base;
}


const buildRoundText = (message, radius) => {
  if (message === undefined || message.length === 0) return []

  const lineRadius = radius / 2
  const lineCorner = sphere({ radius: lineRadius, center: [0, 0, lineRadius], segments: 16 })

  const lineSegmentPointArrays = vectorText({ x: 0, y: 0, input: message }) // line segments for each character
  const lineSegments = []
  lineSegmentPointArrays.forEach((segmentPoints) => { // process the line segment
    const corners = segmentPoints.map((point) => translate(point, lineCorner))
    lineSegments.push(hullChain(corners))
  })
  let message3D = union(lineSegments);
  message3D = scale([p.textScale,p.textScale,p.textScale], message3D);
  message3D = align({relativeTo:[0,0,p.baseThickness], modes:['center', 'center', 'center']}, message3D);
  return message3D;
}


module.exports = { main, getParameterDefinitions }

The text object is returning incorrect bounds. Unioning it with a 1x1x1 cube gives the correct results (line 49)

SimonClark avatar Dec 20 '21 13:12 SimonClark

@SimonClark thnx. I will check it too.

hrgdavor avatar Dec 20 '21 14:12 hrgdavor

@jue89 the latest release has rolled back the changes to measureBoundingBox(). this is just a quick fix to insure that designs are not impacted. but please try out your designs once again.

z3dev avatar Dec 26 '21 06:12 z3dev

@SimonClark @jue89 there is a PR #970

I have tested with your examples and it seems to be working good

hrgdavor avatar Dec 26 '21 14:12 hrgdavor

Yes, I can confirm that all my designs are looking good with @jscad/[email protected].

Thank you for taking care of my problem! Shall we close this issue or shall we keep it open for the solution you are aiming for?

jue89 avatar Dec 28 '21 18:12 jue89

Hi. Yes I would prefer it stay open until my PR is in a published version so we can confirm the fix.

hrgdavor avatar Dec 28 '21 18:12 hrgdavor