/
surface.h
439 lines (368 loc) · 16.3 KB
/
surface.h
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//-----------------------------------------------------------------------------
// Functions relating to rational polynomial surfaces, which are trimmed by
// curves (either rational polynomial curves, or piecewise linear
// approximations to curves of intersection that can't be represented
// exactly in ratpoly form), and assembled into watertight shells.
//
// Copyright 2008-2013 Jonathan Westhues.
//-----------------------------------------------------------------------------
#ifndef SOLVESPACE_SURFACE_H
#define SOLVESPACE_SURFACE_H
class SBezierList;
class SSurface;
class SCurvePt;
// Utility data structure, a two-dimensional BSP to accelerate polygon
// operations.
class SBspUv {
public:
Point2d a, b;
SBspUv *pos;
SBspUv *neg;
SBspUv *more;
enum class Class : uint32_t {
INSIDE = 100,
OUTSIDE = 200,
EDGE_PARALLEL = 300,
EDGE_ANTIPARALLEL = 400,
EDGE_OTHER = 500
};
static SBspUv *Alloc();
static SBspUv *From(SEdgeList *el, SSurface *srf);
void ScalePoints(Point2d *pt, Point2d *a, Point2d *b, SSurface *srf) const;
double ScaledSignedDistanceToLine(Point2d pt, Point2d a, Point2d b,
SSurface *srf) const;
double ScaledDistanceToLine(Point2d pt, Point2d a, Point2d b, bool asSegment,
SSurface *srf) const;
void InsertEdge(Point2d a, Point2d b, SSurface *srf);
static SBspUv *InsertOrCreateEdge(SBspUv *where, Point2d ea, Point2d eb, SSurface *srf);
Class ClassifyPoint(Point2d p, Point2d eb, SSurface *srf) const;
Class ClassifyEdge(Point2d ea, Point2d eb, SSurface *srf) const;
double MinimumDistanceToEdge(Point2d p, SSurface *srf) const;
};
// Now the data structures to represent a shell of trimmed rational polynomial
// surfaces.
class SShell;
class hSSurface {
public:
uint32_t v;
};
template<>
struct IsHandleOracle<hSSurface> : std::true_type {};
class hSCurve {
public:
uint32_t v;
};
template<>
struct IsHandleOracle<hSCurve> : std::true_type {};
// Stuff for rational polynomial curves, of degree one to three. These are
// our inputs, and are also calculated for certain exact surface-surface
// intersections.
class SBezier {
public:
int tag;
int auxA, auxB;
int deg;
Vector ctrl[4];
double weight[4];
uint32_t entity;
Vector PointAt(double t) const;
Vector TangentAt(double t) const;
void ClosestPointTo(Vector p, double *t, bool mustConverge=true) const;
void SplitAt(double t, SBezier *bef, SBezier *aft) const;
bool PointOnThisAndCurve(const SBezier *sbb, Vector *p) const;
Vector Start() const;
Vector Finish() const;
bool Equals(SBezier *b) const;
void MakePwlInto(SEdgeList *sel, double chordTol=0, double max_dt=0.0) const;
void MakePwlInto(List<SCurvePt> *l, double chordTol=0, double max_dt=0.0) const;
void MakePwlInto(SContour *sc, double chordTol=0, double max_dt=0.0) const;
void MakePwlInto(List<Vector> *l, double chordTol=0, double max_dt=0.0) const;
void MakePwlWorker(List<Vector> *l, double ta, double tb, double chordTol, double max_dt) const;
void MakePwlInitialWorker(List<Vector> *l, double ta, double tb, double chordTol, double max_dt) const;
void MakeNonrationalCubicInto(SBezierList *bl, double tolerance, int depth = 0) const;
void AllIntersectionsWith(const SBezier *sbb, SPointList *spl) const;
void GetBoundingProjd(Vector u, Vector orig, double *umin, double *umax) const;
void Reverse();
bool IsInPlane(Vector n, double d) const;
bool IsCircle(Vector axis, Vector *center, double *r) const;
bool IsRational() const;
SBezier TransformedBy(Vector t, Quaternion q, double scale) const;
SBezier InPerspective(Vector u, Vector v, Vector n,
Vector origin, double cameraTan) const;
void ScaleSelfBy(double s);
static SBezier From(Vector p0, Vector p1, Vector p2, Vector p3);
static SBezier From(Vector p0, Vector p1, Vector p2);
static SBezier From(Vector p0, Vector p1);
static SBezier From(Vector4 p0, Vector4 p1, Vector4 p2, Vector4 p3);
static SBezier From(Vector4 p0, Vector4 p1, Vector4 p2);
static SBezier From(Vector4 p0, Vector4 p1);
};
class SBezierList {
public:
List<SBezier> l;
void Clear();
void ScaleSelfBy(double s);
void CullIdenticalBeziers(bool both=true);
void AllIntersectionsWith(SBezierList *sblb, SPointList *spl) const;
bool GetPlaneContainingBeziers(Vector *p, Vector *u, Vector *v,
Vector *notCoplanarAt) const;
};
class SBezierLoop {
public:
int tag;
List<SBezier> l;
inline void Clear() { l.Clear(); }
bool IsClosed() const;
void Reverse();
void MakePwlInto(SContour *sc, double chordTol=0) const;
void GetBoundingProjd(Vector u, Vector orig, double *umin, double *umax) const;
static SBezierLoop FromCurves(SBezierList *spcl,
bool *allClosed, SEdge *errorAt);
};
class SBezierLoopSet {
public:
List<SBezierLoop> l;
Vector normal;
Vector point;
double area;
static SBezierLoopSet From(SBezierList *spcl, SPolygon *poly,
double chordTol,
bool *allClosed, SEdge *errorAt,
SBezierLoopSet *openContours);
void GetBoundingProjd(Vector u, Vector orig, double *umin, double *umax) const;
double SignedArea();
void MakePwlInto(SPolygon *sp) const;
void Clear();
};
class SBezierLoopSetSet {
public:
List<SBezierLoopSet> l;
void FindOuterFacesFrom(SBezierList *sbl, SPolygon *spxyz, SSurface *srfuv,
double chordTol,
bool *allClosed, SEdge *notClosedAt,
bool *allCoplanar, Vector *notCoplanarAt,
SBezierLoopSet *openContours);
void AddOpenPath(SBezier *sb);
void Clear();
};
// Stuff for the surface trim curves: piecewise linear
class SCurvePt {
public:
int tag;
Vector p;
bool vertex;
};
class SCurve {
public:
hSCurve h;
// In a Boolean, C = A op B. The curves in A and B get copied into C, and
// therefore must get new hSCurves assigned. For the curves in A and B,
// we use newH to record their new handle in C.
hSCurve newH;
enum class Source : uint32_t {
A = 100,
B = 200,
INTERSECTION = 300
};
Source source;
bool isExact;
SBezier exact;
List<SCurvePt> pts;
hSSurface surfA;
hSSurface surfB;
static SCurve FromTransformationOf(SCurve *a, Vector t,
Quaternion q, double scale);
SCurve MakeCopySplitAgainst(SShell *agnstA, SShell *agnstB,
SSurface *srfA, SSurface *srfB) const;
void RemoveShortSegments(SSurface *srfA, SSurface *srfB);
SSurface *GetSurfaceA(SShell *a, SShell *b) const;
SSurface *GetSurfaceB(SShell *a, SShell *b) const;
void Clear();
void GetAxisAlignedBounding(Vector *ptMax, Vector *ptMin) const;
};
// A segment of a curve by which a surface is trimmed: indicates which curve,
// by its handle, and the starting and ending points of our segment of it.
// The vector out points out of the surface; it, the surface outer normal,
// and a tangent to the beginning of the curve are all orthogonal.
class STrimBy {
public:
hSCurve curve;
bool backwards;
// If a trim runs backwards, then start and finish still correspond to
// the actual start and finish, but they appear in reverse order in
// the referenced curve.
Vector start;
Vector finish;
static STrimBy EntireCurve(SShell *shell, hSCurve hsc, bool backwards);
};
// An intersection point between a line and a surface
class SInter {
public:
int tag;
Vector p;
SSurface *srf;
Point2d pinter;
Vector surfNormal; // of the intersecting surface, at pinter
bool onEdge; // pinter is on edge of trim poly
};
// A rational polynomial surface in Bezier form.
class SSurface {
public:
enum class CombineAs : uint32_t {
UNION = 10,
DIFFERENCE = 11,
INTERSECTION = 12
};
int tag;
hSSurface h;
// Same as newH for the curves; record what a surface gets renamed to
// when I copy things over.
hSSurface newH;
RgbaColor color;
uint32_t face;
int degm, degn;
Vector ctrl[4][4];
double weight[4][4];
List<STrimBy> trim;
// For testing whether a point (u, v) on the surface lies inside the trim
SBspUv *bsp;
SEdgeList edges;
// For caching our initial (u, v) when doing Newton iterations to project
// a point into our surface.
Point2d cached;
static SSurface FromExtrusionOf(SBezier *spc, Vector t0, Vector t1);
static SSurface FromRevolutionOf(SBezier *sb, Vector pt, Vector axis, double thetas,
double thetaf, double dists, double distf);
static SSurface FromPlane(Vector pt, Vector u, Vector v);
static SSurface FromTransformationOf(SSurface *a, Vector t, Quaternion q,
double scale,
bool includingTrims);
void ScaleSelfBy(double s);
void EdgeNormalsWithinSurface(Point2d auv, Point2d buv,
Vector *pt, Vector *enin, Vector *enout,
Vector *surfn,
uint32_t auxA,
SShell *shell, SShell *sha, SShell *shb);
void FindChainAvoiding(SEdgeList *src, SEdgeList *dest, SPointList *avoid);
SSurface MakeCopyTrimAgainst(SShell *parent, SShell *a, SShell *b,
SShell *into, SSurface::CombineAs type, int dbg_index);
void TrimFromEdgeList(SEdgeList *el, bool asUv);
void IntersectAgainst(SSurface *b, SShell *agnstA, SShell *agnstB,
SShell *into);
void AddExactIntersectionCurve(SBezier *sb, SSurface *srfB,
SShell *agnstA, SShell *agnstB, SShell *into);
typedef struct {
int tag;
Point2d p;
} Inter;
void WeightControlPoints();
void UnWeightControlPoints();
void CopyRowOrCol(bool row, int this_ij, SSurface *src, int src_ij);
void BlendRowOrCol(bool row, int this_ij, SSurface *a, int a_ij,
SSurface *b, int b_ij);
double DepartureFromCoplanar() const;
void SplitInHalf(bool byU, SSurface *sa, SSurface *sb);
void AllPointsIntersecting(Vector a, Vector b,
List<SInter> *l,
bool asSegment, bool trimmed, bool inclTangent);
void AllPointsIntersectingUntrimmed(Vector a, Vector b,
int *cnt, int *level,
List<Inter> *l, bool asSegment,
SSurface *sorig);
void ClosestPointTo(Vector p, Point2d *puv, bool mustConverge=true);
void ClosestPointTo(Vector p, double *u, double *v, bool mustConverge=true);
bool ClosestPointNewton(Vector p, double *u, double *v, bool mustConverge=true) const;
bool PointIntersectingLine(Vector p0, Vector p1, double *u, double *v) const;
Vector ClosestPointOnThisAndSurface(SSurface *srf2, Vector p);
void PointOnSurfaces(SSurface *s1, SSurface *s2, double *u, double *v);
void PointOnCurve(const SBezier *curve, double *up, double *vp);
Vector PointAt(double u, double v) const;
Vector PointAt(Point2d puv) const;
void TangentsAt(double u, double v, Vector *tu, Vector *tv, bool retry=true) const;
Vector NormalAt(Point2d puv) const;
Vector NormalAt(double u, double v) const;
bool LineEntirelyOutsideBbox(Vector a, Vector b, bool asSegment) const;
void GetAxisAlignedBounding(Vector *ptMax, Vector *ptMin) const;
bool CoincidentWithPlane(Vector n, double d) const;
bool CoincidentWith(SSurface *ss, bool sameNormal) const;
bool ContainsPlaneCurve(SCurve *sc) const;
bool IsExtrusion(SBezier *of, Vector *along) const;
bool IsCylinder(Vector *axis, Vector *center, double *r,
Vector *start, Vector *finish) const;
void TriangulateInto(SShell *shell, SMesh *sm);
// these are intended as bitmasks, even though there's just one now
enum class MakeAs : uint32_t {
UV = 0x01,
XYZ = 0x00
};
void MakeTrimEdgesInto(SEdgeList *sel, MakeAs flags, SCurve *sc, STrimBy *stb);
void MakeEdgesInto(SShell *shell, SEdgeList *sel, MakeAs flags,
SShell *useCurvesFrom=NULL);
Vector ExactSurfaceTangentAt(Vector p, SSurface *srfA, SSurface *srfB,
Vector dir);
void MakeSectionEdgesInto(SShell *shell, SEdgeList *sel, SBezierList *sbl);
void MakeClassifyingBsp(SShell *shell, SShell *useCurvesFrom);
double ChordToleranceForEdge(Vector a, Vector b) const;
void MakeTriangulationGridInto(List<double> *l, double vs, double vf,
bool swapped, int depth) const;
Vector PointAtMaybeSwapped(double u, double v, bool swapped) const;
Vector NormalAtMaybeSwapped(double u, double v, bool swapped) const;
void Reverse();
void Clear();
};
class SShell {
public:
IdList<SCurve,hSCurve> curve;
IdList<SSurface,hSSurface> surface;
bool booleanFailed;
void MakeFromExtrusionOf(SBezierLoopSet *sbls, Vector t0, Vector t1,
RgbaColor color);
bool CheckNormalAxisRelationship(SBezierLoopSet *sbls, Vector pt, Vector axis, double da, double dx);
void MakeFromRevolutionOf(SBezierLoopSet *sbls, Vector pt, Vector axis,
RgbaColor color, Group *group);
void MakeFromHelicalRevolutionOf(SBezierLoopSet *sbls, Vector pt, Vector axis, RgbaColor color,
Group *group, double angles, double anglef, double dists, double distf);
void MakeFirstOrderRevolvedSurfaces(Vector pt, Vector axis, int i0);
void MakeFromUnionOf(SShell *a, SShell *b);
void MakeFromDifferenceOf(SShell *a, SShell *b);
void MakeFromIntersectionOf(SShell *a, SShell *b);
void MakeFromBoolean(SShell *a, SShell *b, SSurface::CombineAs type);
void CopyCurvesSplitAgainst(bool opA, SShell *agnst, SShell *into);
void CopySurfacesTrimAgainst(SShell *sha, SShell *shb, SShell *into, SSurface::CombineAs type);
void MakeIntersectionCurvesAgainst(SShell *against, SShell *into);
void MakeClassifyingBsps(SShell *useCurvesFrom);
void AllPointsIntersecting(Vector a, Vector b, List<SInter> *il,
bool asSegment, bool trimmed, bool inclTangent);
void MakeCoincidentEdgesInto(SSurface *proto, bool sameNormal,
SEdgeList *el, SShell *useCurvesFrom);
void RewriteSurfaceHandlesForCurves(SShell *a, SShell *b);
void CleanupAfterBoolean();
// Definitions when classifying regions of a surface; it is either inside,
// outside, or coincident (with parallel or antiparallel normal) with a
// shell.
enum class Class : uint32_t {
INSIDE = 100,
OUTSIDE = 200,
COINC_SAME = 300,
COINC_OPP = 400
};
static const double DOTP_TOL;
Class ClassifyRegion(Vector edge_n, Vector inter_surf_n,
Vector edge_surf_n) const;
bool ClassifyEdge(Class *indir, Class *outdir,
Vector ea, Vector eb,
Vector p, Vector edge_n_in,
Vector edge_n_out, Vector surf_n);
void MakeFromCopyOf(SShell *a);
void MakeFromTransformationOf(SShell *a,
Vector trans, Quaternion q, double scale);
void MakeFromAssemblyOf(SShell *a, SShell *b);
void MergeCoincidentSurfaces();
void TriangulateInto(SMesh *sm);
void MakeEdgesInto(SEdgeList *sel);
void MakeSectionEdgesInto(Vector n, double d, SEdgeList *sel, SBezierList *sbl);
bool IsEmpty() const;
void RemapFaces(Group *g, int remap);
void Clear();
};
#endif