public void AddModelByDepthMap(Bitmap image, Point3D origin, Vector3D xVec, Vector3D yVec, Vector3D zVec, double xSize, double ySize, double zSize,
bool whiteIsBackground = true)
{
xVec.Normalize();
yVec.Normalize();
zVec.Normalize();
Func<int, int, Point3D> getPoint = ((x, y) =>
{
Color color = image.GetPixel(x, y);
int depth = (whiteIsBackground ? color.R + color.G + color.B : 765 - (color.R + color.G + color.B));
if (depth != 765)
return origin + xVec * xSize * x / (image.Width - 1) + yVec * ySize * y / (image.Height - 1) + zVec * zSize * depth / 765;
else
return null;
});
for (int x = 0; x < image.Width - 1; x++)
for (int y = 0; y < image.Height - 1; y++)
{
Point3D p1 = getPoint(x, y), p2 = getPoint(x + 1, y), p3 = getPoint(x, y + 1), p4 = getPoint(x + 1, y + 1);
if (p1 != null && p2 != null && p3 != null)
AddPolygon(new Point3D[] { p1, p2, p3 });
if (p2 != null && p3 != null && p4 != null)
AddPolygon(new Point3D[] { p2, p3, p4 });
if (p2 == null && p1 != null && p3 != null && p4 != null)
AddPolygon(new Point3D[] { p1, p3, p4 });
if (p3 == null && p1 != null && p2 != null && p4 != null)
AddPolygon(new Point3D[] { p1, p2, p4 });
}
}