FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
solvespace/src/render/render2d.cpp at master · ppd/solvespace · GitHub
ppd
solvespace
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
solvespace
/
src
/
render
/
render2d.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
413 lines (348 loc) · 13.2 KB
Breadcrumbs
solvespace
/
src
/
render
/
render2d.cpp
Copy path
File metadata and controls
413 lines (348 loc) · 13.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
-----------------------------------------------------------------------------
//
Rendering projections to 2d surfaces: z-sorting, occlusion testing, etc.
//
//
Copyright 2016 whitequark
//
-----------------------------------------------------------------------------
#
include
"
solvespace.h
"
namespace
SolveSpace
{
//
FIXME: The export coordinate system has a different handedness than display
//
coordinate system; lighting and occlusion calculations are right-handed.
static
Vector
ProjectPoint3RH
(
const
Camera &camera, Vector p) {
p = p.
Plus
(camera.
offset
);
Vector r;
r.
x
= p.
Dot
(camera.
projRight
);
r.
y
= p.
Dot
(camera.
projUp
);
r.
z
= p.
Dot
(camera.
projRight
.
Cross
(camera.
projUp
));
double
w =
1
+ r.
z
*camera.
tangent
*camera.
scale
;
return
r.
ScaledBy
(camera.
scale
/w);
}
//
-----------------------------------------------------------------------------
//
Accumulation of geometry
//
-----------------------------------------------------------------------------
void
SurfaceRenderer::DrawLine
(
const
Vector &a,
const
Vector &b, hStroke hcs) {
edges[hcs].
AddEdge
(
ProjectPoint3RH
(camera, a),
ProjectPoint3RH
(camera, b));
}
void
SurfaceRenderer::DrawEdges
(
const
SEdgeList &el, hStroke hcs) {
for
(
const
SEdge &e : el.
l
) {
edges[hcs].
AddEdge
(
ProjectPoint3RH
(camera, e.
a
),
ProjectPoint3RH
(camera, e.
b
));
}
}
bool
SurfaceRenderer::DrawBeziers
(
const
SBezierList &bl, hStroke hcs) {
if
(!
CanOutputCurves
())
return
false
;
for
(
const
SBezier &b : bl.
l
) {
SBezier pb = camera.
ProjectBezier
(b);
beziers[hcs].
l
.
Add
(&pb);
}
return
true
;
}
void
SurfaceRenderer::DrawOutlines
(
const
SOutlineList &ol, hStroke hcs, DrawOutlinesAs drawAs) {
Vector projDir = camera.
projRight
.
Cross
(camera.
projUp
);
for
(
const
SOutline &o : ol.
l
) {
if
(drawAs == DrawOutlinesAs::
EMPHASIZED_AND_CONTOUR
&&
!(o.
IsVisible
(projDir) || o.
tag
!=
0
))
continue
;
if
(drawAs == DrawOutlinesAs::
EMPHASIZED_WITHOUT_CONTOUR
&&
!(!o.
IsVisible
(projDir) && o.
tag
!=
0
))
continue
;
if
(drawAs == DrawOutlinesAs::
CONTOUR_ONLY
&&
!(o.
IsVisible
(projDir)))
continue
;
edges[hcs].
AddEdge
(
ProjectPoint3RH
(camera, o.
a
),
ProjectPoint3RH
(camera, o.
b
));
}
}
void
SurfaceRenderer::DrawVectorText
(
const
std::string &text,
double
height,
const
Vector &o,
const
Vector &u,
const
Vector &v,
hStroke hcs) {
auto
traceEdge = [&](Vector a, Vector b) {
edges[hcs].
AddEdge
(
ProjectPoint3RH
(camera, a),
ProjectPoint3RH
(camera, b));
};
VectorFont::Builtin
()->
Trace
(height, o, u, v, text, traceEdge, camera);
}
void
SurfaceRenderer::DrawQuad
(
const
Vector &a,
const
Vector &b,
const
Vector &c,
const
Vector &d,
hFill hcf) {
Fill *fill = fills.
FindById
(hcf);
ssassert
(fill->
layer
== Layer::
NORMAL
||
fill->
layer
== Layer::
DEPTH_ONLY
||
fill->
layer
== Layer::
FRONT
||
fill->
layer
== Layer::
BACK
,
"
Unexpected mesh layer
"
);
Vector zOffset = {};
if
(fill->
layer
== Layer::
BACK
) {
zOffset.
z
-=
1e6
;
}
else
if
(fill->
layer
== Layer::
FRONT
) {
zOffset.
z
+=
1e6
;
}
zOffset.
z
+= camera.
scale
* fill->
zIndex
;
STriMeta meta = {};
if
(fill->
layer
!= Layer::
DEPTH_ONLY
) {
meta.
color
= fill->
color
;
}
Vector ta =
ProjectPoint3RH
(camera, a).
Plus
(zOffset),
tb =
ProjectPoint3RH
(camera, b).
Plus
(zOffset),
tc =
ProjectPoint3RH
(camera, c).
Plus
(zOffset),
td =
ProjectPoint3RH
(camera, d).
Plus
(zOffset);
mesh.
AddTriangle
(meta, tc, tb, ta);
mesh.
AddTriangle
(meta, ta, td, tc);
}
void
SurfaceRenderer::DrawPoint
(
const
Vector &o, Canvas::hStroke hcs) {
Stroke *stroke = strokes.
FindById
(hcs);
Fill fill = {};
fill.
layer
= stroke->
layer
;
fill.
zIndex
= stroke->
zIndex
;
fill.
color
= stroke->
color
;
hFill hcf =
GetFill
(fill);
Vector u = camera.
projRight
.
ScaledBy
(stroke->
width
/
2.0
/camera.
scale
),
v = camera.
projUp
.
ScaledBy
(stroke->
width
/
2.0
/camera.
scale
);
DrawQuad
(o.
Minus
(u).
Minus
(v), o.
Minus
(u).
Plus
(v),
o.
Plus
(u).
Plus
(v), o.
Plus
(u).
Minus
(v), hcf);
}
void
SurfaceRenderer::DrawPolygon
(
const
SPolygon &p, hFill hcf) {
SMesh m = {};
p.
TriangulateInto
(&m);
DrawMesh
(m, hcf, {});
m.
Clear
();
}
void
SurfaceRenderer::DrawMesh
(
const
SMesh &m,
hFill hcfFront, hFill hcfBack) {
Fill *fill = fills.
FindById
(hcfFront);
ssassert
(fill->
layer
== Layer::
NORMAL
||
fill->
layer
== Layer::
DEPTH_ONLY
,
"
Unexpected mesh layer
"
);
Vector l0 = (lighting.
lightDirection
[
0
]).
WithMagnitude
(
1
),
l1 = (lighting.
lightDirection
[
1
]).
WithMagnitude
(
1
);
for
(STriangle tr : m.
l
) {
tr.
a
=
ProjectPoint3RH
(camera, tr.
a
);
tr.
b
=
ProjectPoint3RH
(camera, tr.
b
);
tr.
c
=
ProjectPoint3RH
(camera, tr.
c
);
if
(
CanOutputTriangles
() && fill->
layer
== Layer::
NORMAL
) {
if
(fill->
color
.
IsEmpty
()) {
//
Compute lighting, since we're going to draw the shaded triangles.
Vector n = tr.
Normal
().
WithMagnitude
(
1
);
double
intensity = lighting.
ambientIntensity
+
max
(
0.0
, (lighting.
lightIntensity
[
0
])*(n.
Dot
(l0))) +
max
(
0.0
, (lighting.
lightIntensity
[
1
])*(n.
Dot
(l1)));
double
r =
min
(
1.0
, tr.
meta
.
color
.
redF
() * intensity),
g =
min
(
1.0
, tr.
meta
.
color
.
greenF
() * intensity),
b =
min
(
1.0
, tr.
meta
.
color
.
blueF
() * intensity);
tr.
meta
.
color
=
RGBf
(r, g, b);
}
else
{
//
We're going to draw this triangle, but it's not shaded.
tr.
meta
.
color
= fill->
color
;
}
}
else
{
//
This triangle is just for occlusion testing.
tr.
meta
.
color
= {};
}
mesh.
AddTriangle
(&tr);
}
}
void
SurfaceRenderer::DrawFaces
(
const
SMesh &m,
const
std::vector<
uint32_t
> &faces, hFill hcf) {
Fill *fill = fills.
FindById
(hcf);
ssassert
(fill->
layer
== Layer::
NORMAL
||
fill->
layer
== Layer::
DEPTH_ONLY
,
"
Unexpected mesh layer
"
);
Vector zOffset = {};
zOffset.
z
+= camera.
scale
* fill->
zIndex
;
size_t
facesSize = faces.
size
();
for
(STriangle tr : m.
l
) {
uint32_t
face = tr.
meta
.
face
;
for
(
size_t
j =
0
; j < facesSize; j++) {
if
(faces[j] != face)
continue
;
if
(!fill->
color
.
IsEmpty
()) {
tr.
meta
.
color
= fill->
color
;
}
mesh.
AddTriangle
(tr.
meta
,
ProjectPoint3RH
(camera, tr.
a
).
Plus
(zOffset),
ProjectPoint3RH
(camera, tr.
b
).
Plus
(zOffset),
ProjectPoint3RH
(camera, tr.
c
).
Plus
(zOffset));
break
;
}
}
}
void
SurfaceRenderer::DrawPixmap
(std::shared_ptr<
const
Pixmap> pm,
const
Vector &o,
const
Vector &u,
const
Vector &v,
const
Point2d &ta,
const
Point2d &tb, hFill hcf) {
dbp
(
"
Not implemented
"
);
}
void
SurfaceRenderer::InvalidatePixmap
(std::shared_ptr<
const
Pixmap> pm) {
dbp
(
"
Not implemented
"
);
}
//
-----------------------------------------------------------------------------
//
Processing of geometry
//
-----------------------------------------------------------------------------
void
SurfaceRenderer::CalculateBBox
() {
bbox.
minp
=
Vector::From
(
VERY_POSITIVE
,
VERY_POSITIVE
,
VERY_POSITIVE
);
bbox.
maxp
=
Vector::From
(
VERY_NEGATIVE
,
VERY_NEGATIVE
,
VERY_NEGATIVE
);
for
(
auto
&it : edges) {
SEdgeList &el = it.
second
;
for
(SEdge &e : el.
l
) {
bbox.
Include
(e.
a
);
bbox.
Include
(e.
b
);
}
}
for
(
auto
&it : beziers) {
SBezierList &bl = it.
second
;
for
(SBezier &b : bl.
l
) {
for
(
int
i =
0
; i <= b.
deg
; i++) {
bbox.
Include
(b.
ctrl
[i]);
}
}
}
for
(STriangle &tr : mesh.
l
) {
for
(
int
i =
0
; i <
3
; i++) {
bbox.
Include
(tr.
vertices
[i]);
}
}
}
void
SurfaceRenderer::ConvertBeziersToEdges
() {
for
(
auto
&it : beziers) {
hStroke hcs = it.
first
;
SBezierList &bl = it.
second
;
SEdgeList &el = edges[hcs];
for
(
const
SBezier &b : bl.
l
) {
if
(b.
deg
==
1
) {
el.
AddEdge
(b.
ctrl
[
0
], b.
ctrl
[
1
]);
}
else
{
List<Vector> lv = {};
b.
MakePwlInto
(&lv, chordTolerance);
for
(
int
i =
1
; i < lv.
n
; i++) {
el.
AddEdge
(lv[i-
1
], lv[i]);
}
lv.
Clear
();
}
}
bl.
l
.
Clear
();
}
beziers.
clear
();
}
void
SurfaceRenderer::CullOccludedStrokes
() {
//
Perform occlusion testing, if necessary.
if
(mesh.
l
.
IsEmpty
())
return
;
//
We can't perform hidden line removal on exact curves.
ConvertBeziersToEdges
();
//
Remove hidden lines (on NORMAL layers), or remove visible lines (on OCCLUDED layers).
SKdNode *root =
SKdNode::From
(&mesh);
root->
ClearTags
();
int
cnt =
1234
;
for
(
auto
&eit : edges) {
hStroke hcs = eit.
first
;
SEdgeList &el = eit.
second
;
Stroke *stroke = strokes.
FindById
(hcs);
if
(stroke->
layer
!= Layer::
NORMAL
&&
stroke->
layer
!= Layer::
OCCLUDED
)
continue
;
SEdgeList nel = {};
for
(
const
SEdge &e : el.
l
) {
SEdgeList oel = {};
oel.
AddEdge
(e.
a
, e.
b
);
root->
OcclusionTestLine
(e, &oel, cnt);
if
(stroke->
layer
== Layer::
OCCLUDED
) {
for
(SEdge &oe : oel.
l
) {
oe.
tag
= !oe.
tag
;
}
}
oel.
l
.
RemoveTagged
();
oel.
MergeCollinearSegments
(e.
a
, e.
b
);
for
(
const
SEdge &oe : oel.
l
) {
nel.
AddEdge
(oe.
a
, oe.
b
);
}
oel.
Clear
();
cnt++;
}
el.
l
.
Clear
();
el.
l
= nel.
l
;
}
}
void
SurfaceRenderer::OutputInPaintOrder
() {
//
Sort our strokes in paint order.
std::vector<std::pair<Layer,
int
>> paintOrder;
paintOrder.
emplace_back
(Layer::
NORMAL
,
0
);
//
mesh
for
(
const
Stroke &cs : strokes) {
paintOrder.
emplace_back
(cs.
layer
, cs.
zIndex
);
}
const
Layer stackup[] = {
Layer::
BACK
, Layer::
NORMAL
, Layer::
DEPTH_ONLY
, Layer::
OCCLUDED
, Layer::
FRONT
};
std::sort
(paintOrder.
begin
(), paintOrder.
end
(),
[&](std::pair<Layer,
int
> a, std::pair<Layer,
int
> b) {
Layer aLayer = a.
first
,
bLayer = b.
first
;
int
aZIndex = a.
second
,
bZIndex = b.
second
;
size_t
aLayerIndex =
std::find
(
std::begin
(stackup),
std::end
(stackup), aLayer) -
std::begin
(stackup);
size_t
bLayerIndex =
std::find
(
std::begin
(stackup),
std::end
(stackup), bLayer) -
std::begin
(stackup);
if
(aLayerIndex == bLayerIndex) {
return
aZIndex < bZIndex;
}
else
{
return
aLayerIndex < bLayerIndex;
}
});
auto
last =
std::unique
(paintOrder.
begin
(), paintOrder.
end
());
paintOrder.
erase
(last, paintOrder.
end
());
//
Output geometry in paint order.
OutputStart
();
for
(
auto
&it : paintOrder) {
Layer layer = it.
first
;
int
zIndex = it.
second
;
if
(layer == Layer::
NORMAL
&& zIndex ==
0
) {
SMesh mp = {};
SBsp3 *bsp =
SBsp3::FromMesh
(&mesh);
if
(bsp) bsp->
GenerateInPaintOrder
(&mp);
for
(
const
STriangle &tr : mp.
l
) {
//
Cull back-facing and invisible triangles.
if
(tr.
Normal
().
z
<
0
)
continue
;
if
(tr.
meta
.
color
.
IsEmpty
())
continue
;
OutputTriangle
(tr);
}
mp.
Clear
();
}
for
(
auto
eit : edges) {
hStroke hcs = eit.
first
;
const
SEdgeList &el = eit.
second
;
Stroke *stroke = strokes.
FindById
(hcs);
if
(stroke->
layer
!= layer || stroke->
zIndex
!= zIndex)
continue
;
for
(
const
SEdge &e : el.
l
) {
OutputBezier
(
SBezier::From
(e.
a
, e.
b
), hcs);
}
}
for
(
auto
&bit : beziers) {
hStroke hcs = bit.
first
;
const
SBezierList &bl = bit.
second
;
Stroke *stroke = strokes.
FindById
(hcs);
if
(stroke->
layer
!= layer || stroke->
zIndex
!= zIndex)
continue
;
for
(
const
SBezier &b : bl.
l
) {
OutputBezier
(b, hcs);
}
}
}
OutputEnd
();
}
void
SurfaceRenderer::Clear
() {
Canvas::Clear
();
for
(
auto
&eit : edges) {
SEdgeList &el = eit.
second
;
el.
l
.
Clear
();
}
edges.
clear
();
for
(
auto
&bit : beziers) {
SBezierList &bl = bit.
second
;
bl.
l
.
Clear
();
}
beziers.
clear
();
mesh.
Clear
();
}
void
SurfaceRenderer::OutputBezierAsNonrationalCubic
(
const
SBezier &b, hStroke hcs) {
//
Arbitrary choice of tolerance; make it a little finer than pwl tolerance since
//
it should be easier to achieve that with the smooth curves.
SBezierList bl;
b.
MakeNonrationalCubicInto
(&bl, chordTolerance /
2
);
for
(
const
SBezier &cb : bl.
l
) {
OutputBezier
(cb, hcs);
}
bl.
Clear
();
}
}
Back
|
FazBrowse Home
|
New Git URL