You are on page 1of 5

Extruder.

cpp

1 of 5

http://www.getcodesamples.com/src/149C1734/FAD1CAF7

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

////
////
////
////
////
////

THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF


ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
Copyright (c) Microsoft Corporation. All rights reserved

#include "pch.h"
#include "Extruder.h"
using namespace Microsoft::WRL;
static const float FlatteningTolerance = 0.1f;
void Extruder::ExtrudeGeometry(ID2D1Geometry* pGeometry, float height, std::vector<SimpleVertex>* vertices
{
//
// The basic idea here is to generate the side faces by walking the
// geometry and constructing quads, and use ID2D1Geometry::Tessellate
// to generate the front and back faces.
//
// Flatten our geometry first so we don't have to worry about stitching
// together seams of Beziers.
ComPtr<ID2D1Geometry> pFlattenedGeometry;
D2DFlatten(pGeometry, FlatteningTolerance, &pFlattenedGeometry);
// D2DOutline will remove any self-intersections. This is important to
// ensure that the tessellator doesn't introduce new vertices (which
// can cause T-junctions).
ComPtr<ID2D1Geometry> pOutlinedGeometry;
D2DOutline(pFlattenedGeometry.Get(), &pOutlinedGeometry);
ComPtr<ID2D1Geometry> pSnappedGeometry;
PointSnapper::SnapGeometry(pOutlinedGeometry.Get(), &pSnappedGeometry);
ComPtr<ExtrudingSink> helper;
ExtrudingSink::CreateExtrudingSink(height, vertices, &helper);
DX::ThrowIfFailed(
pSnappedGeometry->Tessellate(nullptr, helper.Get())
);
// Simplify is a convenient API for extracting the data out of a geometry.
DX::ThrowIfFailed(
pOutlinedGeometry->Simplify(
D2D1_GEOMETRY_SIMPLIFICATION_OPTION_LINES,
nullptr, // world transform
helper.Get()
)
);
// This Close() call is a little ambiguous, since it refers both to the
// ID2D1TessellationSink and to the ID2D1SimplifiedGeometrySink.
// Thankfully, it really doesn't matter with our ExtrudingSink.
DX::ThrowIfFailed(
helper->Close()
);
}
// Helper function for performing "flattening" -- transforming
// a geometry with Beziers into one containing only line segments.
void Extruder::D2DFlatten(ID2D1Geometry* pGeometry, float flatteningTolerance, ID2D1Geometry** ppGeometry)
{
ComPtr<ID2D1Factory> pFactory;
pGeometry->GetFactory(&pFactory);
ComPtr<ID2D1PathGeometry> pPathGeometry;
DX::ThrowIfFailed(
pFactory->CreatePathGeometry(&pPathGeometry)
);
ComPtr<ID2D1GeometrySink> pSink;
DX::ThrowIfFailed(
pPathGeometry->Open(&pSink)
);
DX::ThrowIfFailed(
pGeometry->Simplify(
D2D1_GEOMETRY_SIMPLIFICATION_OPTION_LINES,
nullptr, // world transform
flatteningTolerance,
pSink.Get()
)
);
DX::ThrowIfFailed(
pSink->Close()
);
*ppGeometry = pPathGeometry.Get();
(*ppGeometry)->AddRef();
}
// Helper function for performing "outlining" -- constructing an
// equivalent geometry with no self-intersections. Note: This
// uses the default flattening tolerance and hence should not be

7/21/2016 10:29 AM

Extruder.cpp

2 of 5

http://www.getcodesamples.com/src/149C1734/FAD1CAF7
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

}
// Helper function for performing "outlining" -- constructing an
// equivalent geometry with no self-intersections. Note: This
// uses the default flattening tolerance and hence should not be
// used with very small geometries.
void Extruder::D2DOutline(ID2D1Geometry* pGeometry, ID2D1Geometry** ppGeometry)
{
ComPtr<ID2D1Factory> pFactory;
pGeometry->GetFactory(&pFactory);
ComPtr<ID2D1PathGeometry> pPathGeometry;
DX::ThrowIfFailed(
pFactory->CreatePathGeometry(&pPathGeometry)
);
ComPtr<ID2D1GeometrySink> pSink;
DX::ThrowIfFailed(
pPathGeometry->Open(&pSink)
);
DX::ThrowIfFailed(
pGeometry->Outline(nullptr, pSink.Get())
);
DX::ThrowIfFailed(
pSink->Close()
);
*ppGeometry = pPathGeometry.Get();
(*ppGeometry)->AddRef();
}
// Internal sink used to implement Extruder.
// Note: This class makes certain assumptions about its usage
// (e.g. no Beziers), which is why it's a private class.
// Note 2: Both ID2D1SimplifiedGeometrySink and
// ID2D1TessellationSink define a Close() method, which is
// bending the rules a bit. This is another reason why we are
// significantly limiting its usage.
void Extruder::ExtrudingSink::CreateExtrudingSink(
float height,
_In_ std::vector<SimpleVertex>* pVertices,
_Outptr_ ExtrudingSink** ppExtrudingSink
)
{
*ppExtrudingSink = new ExtrudingSink(height, pVertices);
(*ppExtrudingSink)->AddRef();
}
Extruder::ExtrudingSink::ExtrudingSink(float height, _In_ std::vector<SimpleVertex>* pVertices) :
m_height(height),
m_vertices(pVertices),
m_ref(0)
{
}
void Extruder::ExtrudingSink::AddBeziers(const D2D1_BEZIER_SEGMENT* /*beziers*/, UINT /*beziersCount*/
{
//
// ExtrudingSink only handles line segments. Users should flatten
// their geometry prior to passing through an ExtrudingSink.
//
}
void Extruder::ExtrudingSink::AddLines(const D2D1_POINT_2F* points, UINT pointsCount)
{
for (UINT i = 0; i < pointsCount; ++i)
{
Vertex2D v;
v.pt = float2(points[i].x, points[i].y);
//
//
//
//
//
//
//
//
//
if

Take care to ignore degenerate segments, as we will be


unable to compute proper normals for them.
Note: This doesn't handle near-degenerate segments, which
should probably also be removed. The one complication here
is that the segments should be removed from both the outline
and the front/back tessellations.
((m_figureVertices.size() == 0) ||
(v.pt.x != m_figureVertices.back().pt.x) ||
(v.pt.y != m_figureVertices.back().pt.y)
)

{
m_figureVertices.push_back(v);
}
}
}
void Extruder::ExtrudingSink::BeginFigure(D2D1_POINT_2F startPoint, D2D1_FIGURE_BEGIN /*figureBegin*/
{
m_figureVertices.clear();
Vertex2D v = {
float2(startPoint.x, startPoint.y),

7/21/2016 10:29 AM

Extruder.cpp

3 of 5

http://www.getcodesamples.com/src/149C1734/FAD1CAF7
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

{
m_figureVertices.clear();
Vertex2D v = {
float2(startPoint.x, startPoint.y),
float2(0, 0), // dummy
float2(0, 0), // dummy
float2(0, 0) // dummy
};
m_figureVertices.push_back(v);
}
HRESULT Extruder::ExtrudingSink::Close()
{
return S_OK;
}
void Extruder::ExtrudingSink::EndFigure(D2D1_FIGURE_END /*figureEnd*/)
{
float2 front = m_figureVertices.front().pt;
float2 back = m_figureVertices.back().pt;
if (front.x == back.x && front.y == back.y)
{
m_figureVertices.pop_back();
}
// If we only have one vertex, then there is nothing to draw!
if (m_figureVertices.size() > 1)
{
//
// We construct the triangles corresponding to the sides of
// the extruded object in 3 steps:
//
//
// Step 1:
//
// Snap vertices and calculate normals.
//
//
// Note: it is important that we compute normals *before*
// snapping the vertices, otherwise, the normals will become
// discretized, which will manifest itself as faceting.
//
for (UINT i = 0; i < m_figureVertices.size(); ++i)
{
m_figureVertices[i].norm = GetNormal(i);
m_figureVertices[i].pt = PointSnapper::SnapPoint(m_figureVertices[i].pt);
}
//
// Step 2:
//
// Interpolate normals as appropriate.
//
for (UINT i = 0; i < m_figureVertices.size(); ++i)
{
UINT h = static_cast<UINT>((i+m_figureVertices.size()-1)%m_figureVertices.size());
float2 n1 =
float2 n2 =
//
//
//
//
//
if
{

m_figureVertices[h].norm;
m_figureVertices[i].norm;

Take a dot-product to determine if the angle between


the normals is small. If it is, then average them so we
get a smooth transition from one face to the next.
((n1.x * n2.x + n1.y * n2.y) > 0.5f)
float2 sum = m_figureVertices[i].norm + m_figureVertices[h].norm;

m_figureVertices[i].interpNorm1 = m_figureVertices[i].interpNorm2 = Normalize(sum);


}
else
{
m_figureVertices[i].interpNorm1 = m_figureVertices[h].norm;
m_figureVertices[i].interpNorm2 = m_figureVertices[i].norm;
}
}
//
// Step 3:
//
// Output the triangles.
//
// interpNorm1 == end normal of previous segment
// interpNorm2 == begin normal of next segment
for (UINT i = 0; i < m_figureVertices.size(); ++i)
{
UINT j = (i+1) % m_figureVertices.size();
float2 pt = m_figureVertices[i].pt;
float2 nextPt = m_figureVertices[j].pt;
float2 ptNorm3 = m_figureVertices[i].interpNorm2;
float2 nextPtNorm2 = m_figureVertices[j].interpNorm1;

7/21/2016 10:29 AM

Extruder.cpp

4 of 5

http://www.getcodesamples.com/src/149C1734/FAD1CAF7
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

float2 pt = m_figureVertices[i].pt;
float2 nextPt = m_figureVertices[j].pt;
float2 ptNorm3 = m_figureVertices[i].interpNorm2;
float2 nextPtNorm2 = m_figureVertices[j].interpNorm1;
//
// These 6 vertices define two adjacent triangles that
// together form a quad.
//
SimpleVertex newVertices[6] =
{
{float3(pt.x, pt.y, m_height/2), float3(ptNorm3.x, ptNorm3.y, 0.0f)},
{float3(pt.x, pt.y, -m_height/2), float3(ptNorm3.x, ptNorm3.y, 0.0f)},
{float3(nextPt.x, nextPt.y, -m_height/2), float3(nextPtNorm2.x, nextPtNorm2.y, 0.0f)},
{float3(nextPt.x, nextPt.y, -m_height/2), float3(nextPtNorm2.x, nextPtNorm2.y, 0.0f)},
{float3(nextPt.x, nextPt.y, m_height/2), float3(nextPtNorm2.x, nextPtNorm2.y, 0.0f)},
{float3(pt.x, pt.y, m_height/2), float3(ptNorm3.x, ptNorm3.y, 0.0f)},
};
for (UINT n = 0; n < ARRAYSIZE(newVertices); ++n)
{
m_vertices->push_back(newVertices[n]);
}
}
}
}
void Extruder::ExtrudingSink::SetFillMode(D2D1_FILL_MODE /*fillMode*/)
{
// Do nothing
}
void Extruder::ExtrudingSink::SetSegmentFlags(D2D1_PATH_SEGMENT /*vertexFlags*/)
{
// Do nothing
}
void Extruder::ExtrudingSink::AddTriangles(const D2D1_TRIANGLE* triangles, UINT trianglesCount)
{
//
// These triangles reprent the front and back faces of the extrusion.
//
for (UINT i = 0; i < trianglesCount; ++i)
{
D2D1_TRIANGLE tri = triangles[i];
D2D1_POINT_2F d1 = {tri.point2.x - tri.point1.x, tri.point2.y - tri.point1.y};
D2D1_POINT_2F d2 = {tri.point3.x - tri.point2.x, tri.point3.y - tri.point2.y};
tri.point1 = PointSnapper::SnapPoint(tri.point1);
tri.point2 = PointSnapper::SnapPoint(tri.point2);
tri.point3 = PointSnapper::SnapPoint(tri.point3);
//
// Currently, Tessellate does not guarantee the orientation
// of the triangles it produces, so we must check here.
//
float cross = d1.x * d2.y - d1.y*d2.x;
if (cross < 0)
{
D2D1_POINT_2F tmp = tri.point1;
tri.point1 = tri.point2;
tri.point2 = tmp;
}
SimpleVertex newVertices[] =
{
{float3(tri.point1.x, tri.point1.y, m_height/2), float3(0.0f, 0.0f, 1.0f)},
{float3(tri.point2.x, tri.point2.y, m_height/2), float3(0.0f, 0.0f, 1.0f)},
{float3(tri.point3.x, tri.point3.y, m_height/2), float3(0.0f, 0.0f, 1.0f)},
//
// Note: these points
// face should be the
//
{float3(tri.point2.x,
{float3(tri.point1.x,
{float3(tri.point3.x,

are listed in a different order since the orientation of the back


opposite of the front face.
tri.point2.y, -m_height/2), float3(0.0f, 0.0f, -1.0f)},
tri.point1.y, -m_height/2), float3(0.0f, 0.0f, -1.0f)},
tri.point3.y, -m_height/2), float3(0.0f, 0.0f, -1.0f)},

};
for (UINT i = 0; i < ARRAYSIZE(newVertices); ++i)
{
m_vertices->push_back(newVertices[i]);
}
}
}
float2 Extruder::ExtrudingSink::GetNormal(uint32 i)
{
uint32 j = (i+1) % m_figureVertices.size();
float2 pti = m_figureVertices[i].pt;
float2 ptj = m_figureVertices[j].pt;
float2 vecij = ptj - pti;

7/21/2016 10:29 AM

Extruder.cpp

5 of 5

http://www.getcodesamples.com/src/149C1734/FAD1CAF7
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

float2 pti = m_figureVertices[i].pt;


float2 ptj = m_figureVertices[j].pt;
float2 vecij = ptj - pti;
return Normalize(float2(vecij.y, vecij.x));
}
float2 Extruder::ExtrudingSink::Normalize(float2 pt)
{
return pt / sqrtf(pt.x*pt.x+pt.y*pt.y);
}
//
// IUnknown methods
//
// These use a basic, non-thread-safe implementation of the
// standard reference-counting logic.
//
HRESULT Extruder::ExtrudingSink::QueryInterface(REFIID riid, _Outptr_ void** object)
{
*object = nullptr;
return E_NOTIMPL;
}
ULONG Extruder::ExtrudingSink::AddRef()
{
m_ref++;
return m_ref;
}
ULONG Extruder::ExtrudingSink::Release()
{
m_ref--;
if (m_ref == 0)
{
delete this;
return 0;
}
return m_ref;
}

7/21/2016 10:29 AM

You might also like