UNPKG

7.42 kBJavaScriptView Raw
1/*
2## License
3
4Copyright (c) 2014 bebbi (elghatta@gmail.com)
5Copyright (c) 2013 Eduard Bespalov (edwbes@gmail.com)
6Copyright (c) 2012 Joost Nieuwenhuijse (joost@newhouse.nl)
7Copyright (c) 2011 Evan Wallace (http://evanw.github.com/csg.js/)
8Copyright (c) 2012 Alexandre Girard (https://github.com/alx)
9
10All code released under MIT license
11
12## Overview
13
14For an overview of the CSG process see the original csg.js code:
15http://evanw.github.com/csg.js/
16
17CSG operations through BSP trees suffer from one problem: heavy fragmentation
18of polygons. If two CSG solids of n polygons are unified, the resulting solid may have
19in the order of n*n polygons, because each polygon is split by the planes of all other
20polygons. After a few operations the number of polygons explodes.
21
22This version of CSG.js solves the problem in 3 ways:
23
241. Every polygon split is recorded in a tree (CSG.PolygonTreeNode). This is a separate
25tree, not to be confused with the CSG tree. If a polygon is split into two parts but in
26the end both fragments have not been discarded by the CSG operation, we can retrieve
27the original unsplit polygon from the tree, instead of the two fragments.
28
29This does not completely solve the issue though: if a polygon is split multiple times
30the number of fragments depends on the order of subsequent splits, and we might still
31end up with unncessary splits:
32Suppose a polygon is first split into A and B, and then into A1, B1, A2, B2. Suppose B2 is
33discarded. We will end up with 2 polygons: A and B1. Depending on the actual split boundaries
34we could still have joined A and B1 into one polygon. Therefore a second approach is used as well:
35
362. After CSG operations all coplanar polygon fragments are joined by a retesselating
37operation. See CSG.reTesselated(). Retesselation is done through a
38linear sweep over the polygon surface. The sweep line passes over the y coordinates
39of all vertices in the polygon. Polygons are split at each sweep line, and the fragments
40are joined horizontally and vertically into larger polygons (making sure that we
41will end up with convex polygons).
42This still doesn't solve the problem completely: due to floating point imprecisions
43we may end up with small gaps between polygons, and polygons may not be exactly coplanar
44anymore, and as a result the retesselation algorithm may fail to join those polygons.
45Therefore:
46
473. A canonicalization algorithm is implemented: it looks for vertices that have
48approximately the same coordinates (with a certain tolerance, say 1e-5) and replaces
49them with the same vertex. If polygons share a vertex they will actually point to the
50same CSG.Vertex instance. The same is done for polygon planes. See CSG.canonicalized().
51
52Performance improvements to the original CSG.js:
53
54Replaced the flip() and invert() methods by flipped() and inverted() which don't
55modify the source object. This allows to get rid of all clone() calls, so that
56multiple polygons can refer to the same CSG.Plane instance etc.
57
58The original union() used an extra invert(), clipTo(), invert() sequence just to remove the
59coplanar front faces from b; this is now combined in a single b.clipTo(a, true) call.
60
61Detection whether a polygon is in front or in back of a plane: for each polygon
62we are caching the coordinates of the bounding sphere. If the bounding sphere is
63in front or in back of the plane we don't have to check the individual vertices
64anymore.
65
66Other additions to the original CSG.js:
67
68CSG.Vector class has been renamed into CSG.Vector3D
69
70Classes for 3D lines, 2D vectors, 2D lines, and methods to find the intersection of
71a line and a plane etc.
72
73Transformations: CSG.transform(), CSG.translate(), CSG.rotate(), CSG.scale()
74
75Expanding or contracting a solid: CSG.expand() and CSG.contract(). Creates nice
76smooth corners.
77
78The vertex normal has been removed since it complicates retesselation. It's not needed
79for solid CAD anyway.
80
81*/
82
83const {addTransformationMethodsToPrototype, addCenteringToPrototype} = require('./src/mutators')
84let CSG = require('./src/CSG')
85let CAG = require('./src/CAG')
86
87// FIXME: how many are actual usefull to be exposed as API ?? looks like a code smell
88const { _CSGDEBUG,
89 defaultResolution2D,
90 defaultResolution3D,
91 EPS,
92 angleEPS,
93 areaEPS,
94 all,
95 top,
96 bottom,
97 left,
98 right,
99 front,
100 back,
101 staticTag,
102 getTag} = require('./src/constants')
103
104CSG._CSGDEBUG = _CSGDEBUG
105CSG.defaultResolution2D = defaultResolution2D
106CSG.defaultResolution3D = defaultResolution3D
107CSG.EPS = EPS
108CSG.angleEPS = angleEPS
109CSG.areaEPS = areaEPS
110CSG.all = all
111CSG.top = top
112CSG.bottom = bottom
113CSG.left = left
114CSG.right = right
115CSG.front = front
116CSG.back = back
117CSG.staticTag = staticTag
118CSG.getTag = getTag
119
120// eek ! all this is kept for backwards compatibility...for now
121CSG.Vector2D = require('./src/math/Vector2')
122CSG.Vector3D = require('./src/math/Vector3')
123CSG.Vertex = require('./src/math/Vertex3')
124CAG.Vertex = require('./src/math/Vertex2')
125CSG.Plane = require('./src/math/Plane')
126CSG.Polygon = require('./src/math/Polygon3')
127CSG.Polygon2D = require('./src/math/Polygon2')
128CSG.Line2D = require('./src/math/Line2')
129CSG.Line3D = require('./src/math/Line3')
130CSG.Path2D = require('./src/math/Path2')
131CSG.OrthoNormalBasis = require('./src/math/OrthoNormalBasis')
132CSG.Matrix4x4 = require('./src/math/Matrix4')
133
134CAG.Side = require('./src/math/Side')
135
136CSG.Connector = require('./src/connectors').Connector
137CSG.ConnectorList = require('./src/connectors').ConnectorList
138CSG.Properties = require('./src/Properties')
139
140const {circle, ellipse, rectangle, roundedRectangle} = require('./src/primitives2d')
141const {sphere, cube, roundedCube, cylinder, roundedCylinder, cylinderElliptic, polyhedron} = require('./src/primitives3d')
142
143CSG.sphere = sphere
144CSG.cube = cube
145CSG.roundedCube = roundedCube
146CSG.cylinder = cylinder
147CSG.roundedCylinder = roundedCylinder
148CSG.cylinderElliptic = cylinderElliptic
149CSG.polyhedron = polyhedron
150
151CAG.circle = circle
152CAG.ellipse = ellipse
153CAG.rectangle = rectangle
154CAG.roundedRectangle = roundedRectangle
155
156//
157const {fromCompactBinary, fromObject, fromSlices} = require('./src/CSGFactories')
158CSG.fromCompactBinary = fromCompactBinary
159CSG.fromObject = fromObject
160CSG.fromSlices = fromSlices
161
162CSG.toPointCloud = require('./src/debugHelpers').toPointCloud
163
164const CAGMakers = require('./src/CAGFactories')
165CAG.fromObject = CAGMakers.fromObject
166CAG.fromPointsNoCheck = CAGMakers.fromPointsNoCheck
167CAG.fromPath2 = CAGMakers.fromPath2
168
169// ////////////////////////////////////
170addTransformationMethodsToPrototype(CSG.prototype)
171addTransformationMethodsToPrototype(CSG.Vector2D.prototype)
172addTransformationMethodsToPrototype(CSG.Vector3D.prototype)
173addTransformationMethodsToPrototype(CSG.Vertex.prototype)
174addTransformationMethodsToPrototype(CSG.Plane.prototype)
175addTransformationMethodsToPrototype(CSG.Polygon.prototype)
176addTransformationMethodsToPrototype(CSG.Line2D.prototype)
177addTransformationMethodsToPrototype(CSG.Line3D.prototype)
178addTransformationMethodsToPrototype(CSG.Path2D.prototype)
179addTransformationMethodsToPrototype(CSG.OrthoNormalBasis.prototype)
180addTransformationMethodsToPrototype(CSG.Connector.prototype)
181
182addTransformationMethodsToPrototype(CAG.prototype)
183addTransformationMethodsToPrototype(CAG.Side.prototype)
184addTransformationMethodsToPrototype(CAG.Vertex.prototype)
185
186addCenteringToPrototype(CSG.prototype, ['x', 'y', 'z'])
187addCenteringToPrototype(CAG.prototype, ['x', 'y'])
188
189module.exports = {CSG, CAG}