Using NPack for Graphics Processing

One of the primary scenarios supported by NPack is to abstract graphics transformations so that they can be represented in any graphics rendering system without having to have a reference to the specific graphics library. Almost all graphics libraries have a concept of an affine transform (see: Linear algebra for computer graphics), and NPack constructs map simply and nicely onto the classes used by graphics packages to represent affine transforms.

GDI

using System;
using System.Drawing;
using GdiMatrix = System.Drawing.Drawing2D.Matrix;

using AffineMatrixF = NPack.AffineMatrix<NPack.SingleComponent>;
using IVectorF = NPack.Interfaces.IVector<NPack.SingleComponent>;
using VectorF = NPack.Vector<NPack.SingleComponent>;
...
// GdiMatrix is an alias for System.Drawing.Drawing2D.Matrix, which is a GDI affine transform
GdiMatrix m1 = new GdiMatrix();

// We want to use an AffineMatrix with a format of MatrixFormat.RowMajor, since
// GDI uses row-major format for matrixes.
AffineMatrixF a1 = new AffineMatrixF(MatrixFormat.RowMajor, 3);

m1.Rotate(45);
m1.Translate(10, 103);
m1.Scale(-1, 13);
m1.RotateAt(30, new PointF(33, 33));

a1.RotateAlong(null, Math.PI / 4);
a1.Translate(new VectorF(10, 103));
a1.Scale(new VectorF(-1, 13));
a1.RotateAt(new VectorF(33, 33), null, Math.PI / 6);

PointF[] gdiPoints = new PointF[] { new PointF(1, 0), new PointF(0, 1) };
m1.TransformPoints(gdiPoints);

VectorF[] npackPoints = new VectorF[] { new VectorF(1, 0, 1), new VectorF(0, 1, 1) };
a1.TransformVectors(npackPoints);

DirectX

using Microsoft.DirectX;
using AffineMatrixF = NPack.AffineMatrix<NPack.SingleComponent>;
using VectorF = NPack.Vector<NPack.SingleComponent>;
using IVectorF = NPack.Interfaces.IVector<NPack.SingleComponent>;
...

Matrix r = Matrix.RotationX((float) Math.PI/4);
Matrix t = Matrix.Translation(new Vector3(1, 2, -3));
Matrix s = Matrix.Scaling(new Vector3(-1, 3, 5));
Matrix m1 = r*t*s;

// We want to use an AffineMatrix with a format of MatrixFormat.RowMajor, since
// DirectX uses row-major format for matrixes.
AffineMatrixF a1 = new AffineMatrixF(MatrixFormat.RowMajor, 4);
a1.RotateAlong(new VectorF(1, 0, 0), Math.PI / 4);
a1.Translate(new VectorF(1, 2, -3));
a1.Scale(new VectorF(-1, 3, 5));

Vector4[] dxPoints = new Vector4[]
{
    new Vector4(1, 0, 0, 1), 
    new Vector4(0, 1, 0, 1), 
    new Vector4(0, 0, 1, 1)
};

dxPoints = Vector4.Transform(dxPoints, m1);

VectorF[] npackPoints = new VectorF[]
{
    new VectorF(1, 0, 0, 1), 
    new VectorF(0, 1, 0, 1), 
    new VectorF(0, 0, 1, 1),
};

a1.TransformVectors(npackPoints);

OpenGL

WPF

using System.Windows.Media.Media3D;
using AffineMatrixD = NPack.AffineMatrix<NPack.DoubleComponent>;
using VectorD = NPack.Vector<NPack.DoubleComponent>;
using IVectorD = NPack.Interfaces.IVector<NPack.DoubleComponent>;
...

Matrix3D m1 = new Matrix3D();
m1.Rotate(new Quaternion(new Vector3D(1, 0, 0), 135));
m1.Translate(new Vector3D(1, 2, -3));
m1.Scale(new Vector3D(-1, 3, 5));

// We want to use an AffineMatrix with a format of MatrixFormat.RowMajor, since
// WPFuses row-major format for matrixes. WPF also uses doubles instead
// of floats, so we set the generic parameter appropriately.
AffineMatrixD a1 = new AffineMatrixD(MatrixFormat.RowMajor, 4);
a1.RotateAlong(new VectorD(1, 0, 0), Math.PI - Math.PI / 4);
a1.Translate(new VectorD(1, 2, -3));
a1.Scale(new VectorD(-1, 3, 5));

Point4D[] points = new Point4D[]
{
    new Point4D(1, 0, 0, 1), 
    new Point4D(0, 1, 0, 1), 
    new Point4D(0, 0, 1, 1)
};

m1.Transform(points);

VectorD[] npackPoints = new VectorD[]
{
    new VectorD(1, 0, 0, 1), 
    new VectorD(0, 1, 0, 1), 
    new VectorD(0, 0, 1, 1),
};

a1.TransformVectors(npackPoints);

Flash

SVG

Last edited Jul 19, 2007 at 12:40 AM by codekaizen, version 2

Comments

No comments yet.