Skip to content

CSG Module 2.0 #206

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

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/modules.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"test": "Run unit tests",
"test:watch": "Watch files for changes and rerun tests related to changed files",
"dev": "Build bundles and tabs only, then serve. Skips linting / type checking, does not build jsons and HTML docs. For rapid testing during development",
"watch": "Watch files for changes and rebuild on those changes"
"watch": "Watch files for changes and rebuild on those changes",
"postinstall": "Install all patches to node_modules packages"
},
"type": "module",
"scripts": {
Expand All @@ -37,7 +38,8 @@
"test:scripts": "jest --config=scripts/src/jest.config.js",
"test:watch": "jest --watch",
"dev": "yarn scripts build modules && yarn serve",
"watch": "yarn scripts watch"
"watch": "yarn scripts watch",
"postinstall": "patch-package"
},
"devDependencies": {
"@types/dom-mediacapture-record": "^1.0.11",
Expand All @@ -57,7 +59,7 @@
"console-table-printer": "^2.11.1",
"copyfiles": "^2.4.1",
"cross-env": "^7.0.3",
"esbuild": "^0.17.8",
"esbuild": "^0.17.19",
"eslint": "^8.21.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-airbnb-typescript": "^17.0.0",
Expand All @@ -84,17 +86,21 @@
"@blueprintjs/popover2": "^1.4.3",
"@jscad/modeling": "^2.9.5",
"@jscad/regl-renderer": "^2.6.1",
"@jscad/stl-serializer": "^2.1.13",
"ace-builds": "^1.4.14",
"classnames": "^2.3.1",
"dayjs": "^1.10.4",
"gl-matrix": "^3.3.0",
"js-slang": "^0.5.38",
"lodash": "^4.17.21",
"patch-package": "^6.5.1",
"phaser": "^3.54.0",
"postinstall-postinstall": "^2.1.0",
"react": "^17.0.2",
"react-ace": "^10.1.0",
"react-dom": "^17.0.2",
"regl": "^2.1.0",
"save-file": "^2.3.1",
"tslib": "^2.3.1"
},
"jest": {
Expand All @@ -103,4 +109,4 @@
"scripts/src/jest.config.js"
]
}
}
}
243 changes: 243 additions & 0 deletions patches/@jscad+modeling+2.9.6.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
diff --git a/node_modules/@jscad/modeling/src/colors/hexToRgb.d.ts b/node_modules/@jscad/modeling/src/colors/hexToRgb.d.ts
index 7ae0817..aa09123 100644
--- a/node_modules/@jscad/modeling/src/colors/hexToRgb.d.ts
+++ b/node_modules/@jscad/modeling/src/colors/hexToRgb.d.ts
@@ -1,4 +1,4 @@
-import { RGB, RGBA } from './types'
+import { RGB, RGBA } from '@jscad/modeling/src/colors/types'

export default hexToRgb

diff --git a/node_modules/@jscad/modeling/src/geometries/geom3/toPolygons.d.ts b/node_modules/@jscad/modeling/src/geometries/geom3/toPolygons.d.ts
index 85720f4..9ef2a9d 100644
--- a/node_modules/@jscad/modeling/src/geometries/geom3/toPolygons.d.ts
+++ b/node_modules/@jscad/modeling/src/geometries/geom3/toPolygons.d.ts
@@ -3,4 +3,4 @@ import Poly3 from '../poly3/type'

export default toPolygons

-declare function toPolygons(geometry: Geom3): Array<Poly3>
+declare function toPolygons(geometry: Geom3, colorizePolygons: boolean): Array<Poly3>
diff --git a/node_modules/@jscad/modeling/src/geometries/geom3/toPolygons.js b/node_modules/@jscad/modeling/src/geometries/geom3/toPolygons.js
index 25f9f50..300aaa4 100644
--- a/node_modules/@jscad/modeling/src/geometries/geom3/toPolygons.js
+++ b/node_modules/@jscad/modeling/src/geometries/geom3/toPolygons.js
@@ -1,5 +1,20 @@
+const poly3 = require("../poly3")
const applyTransforms = require('./applyTransforms')

+// Colorize poly3
+const colorPoly3 = (color, object) => {
+ // const newpoly = poly3.clone(object)
+ // newpoly.color = color
+ // return newpoly
+ // console.log(object.color)
+ // console.log(color)
+ if(!object.color) {
+ object.color = color
+ }
+ return object
+}
+
+
/**
* Produces an array of polygons from the given geometry, after applying transforms.
* The returned array should not be modified as the polygons are shared with the geometry.
@@ -10,6 +25,12 @@ const applyTransforms = require('./applyTransforms')
* @example
* let sharedpolygons = toPolygons(geometry)
*/
-const toPolygons = (geometry) => applyTransforms(geometry).polygons
+const toPolygons = (geometry, colorizePolygons = false) => {
+ const newPolygons = applyTransforms(geometry).polygons
+ return colorizePolygons && geometry.color
+ ? newPolygons.map(polygon => colorPoly3(geometry.color, polygon))
+ : newPolygons
+}
+

module.exports = toPolygons
diff --git a/node_modules/@jscad/modeling/src/geometries/poly3/fromPointsAndPlane.d.ts b/node_modules/@jscad/modeling/src/geometries/poly3/fromPointsAndPlane.d.ts
index 6a06d4f..1c2d315 100644
--- a/node_modules/@jscad/modeling/src/geometries/poly3/fromPointsAndPlane.d.ts
+++ b/node_modules/@jscad/modeling/src/geometries/poly3/fromPointsAndPlane.d.ts
@@ -1,7 +1,8 @@
import Poly3 from './type'
import Vec3 from '../../maths/vec3/type'
import Plane from '../../maths/plane/type'
+import { RGB, RGBA } from './types'

export default fromPointsAndPlane

-declare function fromPointsAndPlane(vertices: Array<Vec3>, plane: Plane): Poly3
+declare function fromPointsAndPlane(vertices: Array<Vec3>, plane: Plane, color?: RGB|RGBA ): Poly3
diff --git a/node_modules/@jscad/modeling/src/geometries/poly3/fromPointsAndPlane.js b/node_modules/@jscad/modeling/src/geometries/poly3/fromPointsAndPlane.js
index ecef495..aefb41f 100644
--- a/node_modules/@jscad/modeling/src/geometries/poly3/fromPointsAndPlane.js
+++ b/node_modules/@jscad/modeling/src/geometries/poly3/fromPointsAndPlane.js
@@ -8,10 +8,17 @@ const create = require('./create')
* @returns {poly3} a new polygon
* @alias module:modeling/geometries/poly3.fromPointsAndPlane
*/
-const fromPointsAndPlane = (vertices, plane) => {
+const fromPointsAndPlane = (vertices, plane, color = undefined) => {
const poly = create(vertices)
poly.plane = plane // retain the plane for later use
- return poly
+
+ /* If the polygon has been colored
+ (Most likely due to a previous Boolean operation,
+ since polygons are not colored when the shape is first
+ created), then we should preserve the color of
+ the polygon).
+ */
+ return color ? {...poly, color} : poly
}

module.exports = fromPointsAndPlane
diff --git a/node_modules/@jscad/modeling/src/geometries/poly3/invert.js b/node_modules/@jscad/modeling/src/geometries/poly3/invert.js
index b37c672..4010edb 100644
--- a/node_modules/@jscad/modeling/src/geometries/poly3/invert.js
+++ b/node_modules/@jscad/modeling/src/geometries/poly3/invert.js
@@ -15,6 +15,9 @@ const invert = (polygon) => {
// Flip existing plane to save recompute
inverted.plane = plane.flip(plane.create(), polygon.plane)
}
+ if(polygon.color) {
+ inverted.color = polygon.color
+ }
return inverted
}

diff --git a/node_modules/@jscad/modeling/src/geometries/poly3/transform.js b/node_modules/@jscad/modeling/src/geometries/poly3/transform.js
index ad2d533..27f6232 100644
--- a/node_modules/@jscad/modeling/src/geometries/poly3/transform.js
+++ b/node_modules/@jscad/modeling/src/geometries/poly3/transform.js
@@ -16,7 +16,15 @@ const transform = (matrix, polygon) => {
// reverse the order to preserve the orientation
vertices.reverse()
}
- return create(vertices)
+ /* If the polygon has been colored
+ (Most likely due to a previous Boolean operation,
+ since polygons are not colored when the shape is first
+ created), then we should preserve the color of
+ the polygon).
+ */
+ const transformedVertices = create(vertices)
+ return polygon.color ? {color: polygon.color, ...transformedVertices}
+ : transformedVertices
}

module.exports = transform
diff --git a/node_modules/@jscad/modeling/src/operations/booleans/intersectGeom3.js b/node_modules/@jscad/modeling/src/operations/booleans/intersectGeom3.js
index a9370c9..14b674b 100644
--- a/node_modules/@jscad/modeling/src/operations/booleans/intersectGeom3.js
+++ b/node_modules/@jscad/modeling/src/operations/booleans/intersectGeom3.js
@@ -18,7 +18,6 @@ const intersect = (...geometries) => {
newgeometry = intersectSub(newgeometry, geometry)
})

- newgeometry = retessellate(newgeometry)
return newgeometry
}

diff --git a/node_modules/@jscad/modeling/src/operations/booleans/intersectGeom3Sub.js b/node_modules/@jscad/modeling/src/operations/booleans/intersectGeom3Sub.js
index b43dd86..6ac481d 100644
--- a/node_modules/@jscad/modeling/src/operations/booleans/intersectGeom3Sub.js
+++ b/node_modules/@jscad/modeling/src/operations/booleans/intersectGeom3Sub.js
@@ -15,9 +15,8 @@ const intersectGeom3Sub = (geometry1, geometry2) => {
return geom3.create() // empty geometry
}

- const a = new Tree(geom3.toPolygons(geometry1))
- const b = new Tree(geom3.toPolygons(geometry2))
-
+ const a = new Tree(geom3.toPolygons(geometry1, colorizePolygons = true))
+ const b = new Tree(geom3.toPolygons(geometry2, colorizePolygons = true))
a.invert()
b.clipTo(a)
b.invert()
diff --git a/node_modules/@jscad/modeling/src/operations/booleans/subtractGeom3.js b/node_modules/@jscad/modeling/src/operations/booleans/subtractGeom3.js
index df86fec..dba2983 100644
--- a/node_modules/@jscad/modeling/src/operations/booleans/subtractGeom3.js
+++ b/node_modules/@jscad/modeling/src/operations/booleans/subtractGeom3.js
@@ -18,7 +18,6 @@ const subtract = (...geometries) => {
newgeometry = subtractSub(newgeometry, geometry)
})

- newgeometry = retessellate(newgeometry)
return newgeometry
}

diff --git a/node_modules/@jscad/modeling/src/operations/booleans/subtractGeom3Sub.js b/node_modules/@jscad/modeling/src/operations/booleans/subtractGeom3Sub.js
index 62eb4cd..815ab38 100644
--- a/node_modules/@jscad/modeling/src/operations/booleans/subtractGeom3Sub.js
+++ b/node_modules/@jscad/modeling/src/operations/booleans/subtractGeom3Sub.js
@@ -15,8 +15,8 @@ const subtractGeom3Sub = (geometry1, geometry2) => {
return geom3.clone(geometry1)
}

- const a = new Tree(geom3.toPolygons(geometry1))
- const b = new Tree(geom3.toPolygons(geometry2))
+ const a = new Tree(geom3.toPolygons(geometry1, colorizePolygons = true))
+ const b = new Tree(geom3.toPolygons(geometry2, colorizePolygons = true))

a.invert()
a.clipTo(b)
diff --git a/node_modules/@jscad/modeling/src/operations/booleans/trees/splitPolygonByPlane.js b/node_modules/@jscad/modeling/src/operations/booleans/trees/splitPolygonByPlane.js
index 75e3ab8..b2f5e90 100644
--- a/node_modules/@jscad/modeling/src/operations/booleans/trees/splitPolygonByPlane.js
+++ b/node_modules/@jscad/modeling/src/operations/booleans/trees/splitPolygonByPlane.js
@@ -108,10 +108,10 @@ const splitPolygonByPlane = (splane, polygon) => {
}
}
if (frontvertices.length >= 3) {
- result.front = poly3.fromPointsAndPlane(frontvertices, pplane)
+ result.front = poly3.fromPointsAndPlane(frontvertices, pplane, polygon.color)
}
if (backvertices.length >= 3) {
- result.back = poly3.fromPointsAndPlane(backvertices, pplane)
+ result.back = poly3.fromPointsAndPlane(backvertices, pplane, polygon.color)
}
}
}
diff --git a/node_modules/@jscad/modeling/src/operations/booleans/unionGeom3.js b/node_modules/@jscad/modeling/src/operations/booleans/unionGeom3.js
index cf9c591..af1842c 100644
--- a/node_modules/@jscad/modeling/src/operations/booleans/unionGeom3.js
+++ b/node_modules/@jscad/modeling/src/operations/booleans/unionGeom3.js
@@ -18,7 +18,19 @@ const union = (...geometries) => {
geometries.push(unionSub(geometries[i - 1], geometries[i]))
}
let newgeometry = geometries[i - 1]
+
+ /*
+ To preserve the colors of the Geom3 object after boolean operations,
+ this line of code
+
newgeometry = retessellate(newgeometry)
+
+ was removed. The same was done to intersect and subtract operations.
+ This is due to the fact that retessellating combines coplanar polygons,
+ including those with different colors, and replaces them with
+ a new plane with the default color.
+ */
+
return newgeometry
}

diff --git a/node_modules/@jscad/modeling/src/operations/booleans/unionGeom3Sub.js b/node_modules/@jscad/modeling/src/operations/booleans/unionGeom3Sub.js
index a4026e3..2751e56 100644
--- a/node_modules/@jscad/modeling/src/operations/booleans/unionGeom3Sub.js
+++ b/node_modules/@jscad/modeling/src/operations/booleans/unionGeom3Sub.js
@@ -14,8 +14,8 @@ const unionSub = (geometry1, geometry2) => {
return unionForNonIntersecting(geometry1, geometry2)
}

- const a = new Tree(geom3.toPolygons(geometry1))
- const b = new Tree(geom3.toPolygons(geometry2))
+ const a = new Tree(geom3.toPolygons(geometry1, colorizePolygons = true))
+ const b = new Tree(geom3.toPolygons(geometry2, colorizePolygons = true))

a.clipTo(b, false)
// b.clipTo(a, true); // ERROR: doesn't work
Loading