Condividi tramite


Come recuperare i dati geometry estendendo ID2D1SimplifiedGeometrySink

Mentre un oggetto ID2D1Geometry non è modificabile, esistono casi in cui è necessario modificare i dati geometry in un oggetto geometry geometry. Direct2D consente di eseguire questa operazione fornendo un'interfaccia estendibile denominata ID2D1SimplifiedGeometrySink. Per l'illustrazione del concetto, questo argomento descrive come estendere questa interfaccia per recuperare i dati geometry da un oggetto geometry del percorso.

Per estendere l'interfaccia ID2D1SimplifiedGeometrySink

  1. Implementare una classe che eredita da ID2D1SimplifiedGeometrySink.
  2. Creare un'istanza di tale classe e passarla a ID2D1Geometry::Semplifica.

Nell'esempio di codice seguente viene illustrato come implementare una classe denominata SpecializedSink che eredita dall'interfaccia ID2D1SimplifiedGeometrySink . Per la semplicità dell'illustrazione del concetto, il metodo AddLines esteso recupera i dati geometry e quindi lo visualizza nella finestra della console; è possibile personalizzare questo metodo per soddisfare le esigenze specifiche dei dati.

class SpecializedSink : public ID2D1SimplifiedGeometrySink
{
    public:
        SpecializedSink()
            : m_cRef(1)
        {
        }

        STDMETHOD_(ULONG, AddRef)(THIS)
        {
            return InterlockedIncrement(reinterpret_cast<LONG volatile *>(&m_cRef));
        }

        STDMETHOD_(ULONG, Release)(THIS)
        {
            ULONG cRef = static_cast<ULONG>(
            InterlockedDecrement(reinterpret_cast<LONG volatile *>(&m_cRef)));

            if(0 == cRef)
            {
                delete this;
            }

            return cRef;
        }

        STDMETHOD(QueryInterface)(THIS_ REFIID iid, void** ppvObject)
        {
            HRESULT hr = S_OK;

            if (__uuidof(IUnknown) == iid)
            {
                *ppvObject = static_cast<IUnknown*>(this);
                AddRef();
            }
            else if (__uuidof(ID2D1SimplifiedGeometrySink) == iid)
            {
                *ppvObject = static_cast<ID2D1SimplifiedGeometrySink*>(this);
                AddRef();
            }
            else
            {
                *ppvObject = NULL;
                hr = E_NOINTERFACE;
            }

            return hr;
        }

        STDMETHOD_(void, AddBeziers)(const D2D1_BEZIER_SEGMENT * /*beziers*/,
                                     UINT /*beziersCount*/)
        {
            // Customize this method to meet your specific data needs.
        }

        STDMETHOD_(void, AddLines)(const D2D1_POINT_2F *points, UINT pointsCount)
        {
            // Customize this method to meet your specific data needs.
            printf("\n\nRetrieving geometry data from a derived ID2D1SimplifiedGeometrySink object:\n");
            for (UINT i = 0; i < pointsCount; ++i)
            {
                printf("%.0f, %.0f\n", points[i].x, points[i].y);
            }
        }

        STDMETHOD_(void, BeginFigure)(D2D1_POINT_2F startPoint,
                                      D2D1_FIGURE_BEGIN figureBegin)
        {
        }

        STDMETHOD_(void, EndFigure)(D2D1_FIGURE_END figureEnd)
        {
        }

        STDMETHOD_(void, SetFillMode)(D2D1_FILL_MODE fillMode)
        {
        }

        STDMETHOD_(void, SetSegmentFlags)(D2D1_PATH_SEGMENT vertexFlags)
        {
        }

        STDMETHOD(Close)()
        {
            return S_OK;
        }

    private:
        UINT m_cRef;
};

L'esempio usa quindi un set di dati (182, 209), (211, 251), (251, 226), (392, 360) e (101, 360) per creare una geometria del percorso popolata (m_pGeometry) in cui è possibile recuperare i dati.

hr = m_pD2DFactory->CreatePathGeometry(&m_pGeometry);
if(SUCCEEDED(hr))
{
    ID2D1GeometrySink *pSink = NULL;

    hr = m_pGeometry->Open(&pSink);
    if (SUCCEEDED(hr))
    {
        pSink->SetFillMode(D2D1_FILL_MODE_WINDING);

        pSink->BeginFigure(
            D2D1::Point2F(101,360),
            D2D1_FIGURE_BEGIN_FILLED
            );
        D2D1_POINT_2F points[5] = {
           D2D1::Point2F(182,209),
           D2D1::Point2F(211,251),
           D2D1::Point2F(251,226),
           D2D1::Point2F(392,360),
           D2D1::Point2F(101,360),
           };

        printf("Adding the following geometry data to an ID2D1GeometrySink object:\n");
        printf("182, 209\n");
        printf("211, 251\n");
        printf("251, 226\n");
        printf("392, 360\n");
        printf("101, 360\n");

        pSink->AddLines(points, ARRAYSIZE(points));
        pSink->EndFigure(D2D1_FIGURE_END_CLOSED);
        hr = pSink->Close();
        pSink->Release();

Infine, l'esempio crea un oggetto SpecializedSink e quindi chiama il metodo ID2D1Geometry:: Simplify, passando l'oggetto SpecializedSink e il parametro D2D1_GEOMETRY_SIMPLIFICATION_OPTION_LINES , che causa l'appiattimento di qualsiasi curva in segmenti di riga.

        SpecializedSink *pSpecializedSink = NULL;

        if (SUCCEEDED(hr))
        {
            pSpecializedSink = new SpecializedSink();
            if (!pSpecializedSink)
            {
                hr = E_OUTOFMEMORY;
            }
        }

        if (SUCCEEDED(hr))
        {
            hr = m_pGeometry->Simplify(
                    D2D1_GEOMETRY_SIMPLIFICATION_OPTION_LINES, // This causes any curves to be flattened into line segments.
                    NULL, // world transform
                    pSpecializedSink
                    );


            if (SUCCEEDED(hr))
            {
                hr = pSpecializedSink->Close();
            }

            pSpecializedSink->Release();
        }

Il programma crea output, come illustrato nella schermata seguente.

schermata di una finestra della console con output sull'aggiunta e il recupero dei dati geometry

Riferimento a Direct2D