Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In memory images for textures. #851

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions Elements/src/Analysis/AnalysisImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;

using System.IO;

namespace Elements.Analysis
{
/// <summary>
Expand Down Expand Up @@ -63,13 +61,14 @@ public AnalysisImage(Polygon perimeter,
double vLength,
ColorScale colorScale,
Func<Vector3, double> analyze,
Guid id = default(Guid),
Guid id = default,
string name = null) : base(perimeter, uLength, vLength, colorScale, analyze, id, name) { }

/// <summary>
/// Compute a value for each grid cell, and create the required material.
/// </summary>
public override void Analyze() {
public override void Analyze()
{
base.Analyze();

_perimBounds = new BBox3(new[] { this.Perimeter });
Expand All @@ -83,14 +82,14 @@ public override void Analyze() {

var image = new Image<Rgba32>(_imgPixels, _imgPixels);

foreach (var result in this._results)
foreach (var (cell, value) in this._results)
{
var center = result.cell.Center();
var center = cell.Center();
if (this.Perimeter.Contains(center))
{
var vertexColor = this.ColorScale.GetColor(result.value);
var u = (result.cell.Min.X - _perimBounds.Min.X) / _perimW * (_numPixelsX / _imgPixels);
var v = (result.cell.Min.Y - _perimBounds.Min.Y) / _perimH * (_numPixelsY / _imgPixels);
var vertexColor = this.ColorScale.GetColor(value);
var u = (cell.Min.X - _perimBounds.Min.X) / _perimW * (_numPixelsX / _imgPixels);
var v = (cell.Min.Y - _perimBounds.Min.Y) / _perimH * (_numPixelsY / _imgPixels);

var pX = (int)Math.Round(u * _imgPixels); // pixels in world coordinates
var pY = (int)Math.Round(v * _imgPixels); // pixels in world coordinates
Expand All @@ -108,7 +107,7 @@ public override void Analyze() {
image[x, y] = rgbaColor;

// Extend this color to the right
if (Math.Abs(result.cell.Max.X - _perimBounds.Max.X) < Vector3.EPSILON)
if (Math.Abs(cell.Max.X - _perimBounds.Max.X) < Vector3.EPSILON)
{
while (x < _imgPixels - 1)
{
Expand All @@ -118,7 +117,7 @@ public override void Analyze() {
}

// Extend this color to the top
if (Math.Abs(result.cell.Max.Y - _perimBounds.Max.Y) < Vector3.EPSILON)
if (Math.Abs(cell.Max.Y - _perimBounds.Max.Y) < Vector3.EPSILON)
{
while (y >= 0)
{
Expand All @@ -129,16 +128,13 @@ public override void Analyze() {
}
}

var imagePath = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.png");
image.Save(imagePath);

this.Material = new Material($"Analysis_{Guid.NewGuid().ToString()}", Colors.White, 0, 0, imagePath, true, true, interpolateTexture:false, id: Guid.NewGuid());
this.Material = new Material($"Analysis_{Guid.NewGuid()}", Colors.White, image, 0, 0, true, true, interpolateTexture: false, id: Guid.NewGuid());
}

/// <summary>
/// Gives an element with a mapped texture.
/// </summary>
public override void Tessellate(ref Mesh mesh, Transform transform = null, Elements.Geometry.Color color = default(Elements.Geometry.Color))
public override void Tessellate(ref Mesh mesh, Transform transform = null, Elements.Geometry.Color color = default)
{
var meshVertices = new List<Vertex>();

Expand Down
80 changes: 80 additions & 0 deletions Elements/src/Image.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.IO;
using System.Net;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;

namespace Elements
{
/// <summary>
/// An image.
/// </summary>
internal class Image
{
/// <summary>
/// Create an image.
/// </summary>
/// <param name="uri">The URI of the image.</param>
/// <returns>An image or null if an image cannot be created from the provided URI.</returns>
internal static Image<Rgba32> CreateFromUri(Uri uri)
{
if (!RemoteFileExists(uri))
{
return null;
}

var webClient = new WebClient();
byte[] imageBytes = webClient.DownloadData(uri);

// We don't wrap this in a using statement because
// we hold onto the image in other places.
var texImage = SixLabors.ImageSharp.Image.Load(imageBytes);

// Flip the texture image vertically
// to align with OpenGL convention.
// 0,1 1,1
// 0,0 1,0
texImage.Mutate(x => x.Flip(FlipMode.Vertical));

return texImage;
}

/// <summary>
/// Create an image.
/// </summary>
/// <param name="data">A byte array containing the image data.</param>
/// <returns>An image.</returns>
internal static Image<Rgba32> CreateFromBytes(byte[] data)
{
var texImage = SixLabors.ImageSharp.Image.Load(data);
return texImage;
}

internal static bool RemoteFileExists(Uri uri)
{
if (uri.IsFile)
{
var localPath = uri.LocalPath;
return File.Exists(localPath);
}

// https://stackoverflow.com/questions/924679/c-sharp-how-can-i-check-if-a-url-exists-is-valid
try
{
HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
request.Method = "HEAD";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
var ok = response.StatusCode == HttpStatusCode.OK;
response.Close();
return ok;
}
catch (Exception ex)
{
//Any exception will returns false.
Console.WriteLine(ex.Message);
return false;
}
}
}
}
Loading