public static Color Color(this Gradient gradient, double val, double from, double to) { if (gradient == null) { BH.Engine.Reflection.Compute.RecordError("Cannot query the colour of a null gradient."); return(System.Drawing.Color.Transparent); } return(gradient.Color((val - from) / (to - from))); }
public static Gradient CenterGradientAsymmetric(this Gradient gradient, double lowerBound, double upperBound) { if (gradient?.Markers == null || gradient.Markers.Count < 2) { BH.Engine.Reflection.Compute.RecordError("Cannot edit gradient because gradient is null or invalid."); return(null); } Gradient result = gradient.ShallowClone(); // Add a marker to avoid issues when deleting and transforming if (!result.Markers.ContainsKey((decimal)0.5)) { result.Markers.Add((decimal)0.5, result.Color(0.5)); } if (upperBound <= 0) { // Scale marker positions to span 0 to 2 and delete those above 1 result = Graphics.Create.Gradient(result.Markers.Where(x => x.Key <= (decimal)0.5).Select(x => x.Value), result.Markers.Keys.Select(x => x * 2).Where(x => x <= 1)); } else if (lowerBound >= 0) { // Scale marker positions to span -1 to 1 and delete those below 0 result = Graphics.Create.Gradient(result.Markers.Where(x => x.Key >= (decimal)0.5).Select(x => x.Value), result.Markers.Keys.Select(x => (x - 1) * 2 + 1).Where(x => x >= 0)); } else { // found = the relative position of zero on the gradient decimal found = (decimal)(-lowerBound / (upperBound - lowerBound)); // scale marker positions below 'found' from: 0 to 0.5 => 0 to found // scale marker positions above 'found' from: 0.5 to 1 => found to 1 result = Graphics.Create.Gradient(result.Markers.Values, result.Markers.Keys.Select(x => x > found ? (x - 1) * 2 * (1 - found) + 1 : x * 2 * found)); } return(result); }
public static Color Color(this Gradient gradient, double val, double from, double to) { return(gradient.Color((val - from) / (to - from))); }
private static RenderMesh DisplayMeshResults <TNode, TFace, TMeshElementResult>(this IMesh <TNode, TFace> mesh, IMeshResult <TMeshElementResult> meshResult, Type identifier, string meshResultDisplay, Gradient gradient, double from, double to) where TNode : INode where TFace : IFace where TMeshElementResult : IMeshElementResult { if (mesh?.Nodes == null || mesh?.Faces == null || mesh.Nodes.Count < 1 || mesh.Faces.Count < 1) { Engine.Reflection.Compute.RecordError("A mesh is null or invalid. Cannot display results for this mesh."); return(null); } if (meshResult?.Results == null || meshResult.Results.Count < 1) { Engine.Reflection.Compute.RecordError("A result is null or invalid. Cannot display results for this mesh."); return(null); } // Order the MeshNodeResults by the IMesh Nodes List <List <TMeshElementResult> > tempMappedElementResults = mesh.Nodes.MapResults(meshResult.Results, "NodeId", identifier); // Get the relevant values into a list List <Vertex> verts = new List <Vertex>(); List <Face> faces; object smoothing = Reflection.Query.PropertyValue(meshResult, "Smoothing"); MeshResultSmoothingType smoothingType = MeshResultSmoothingType.None; if (smoothing is MeshResultSmoothingType) { smoothingType = (MeshResultSmoothingType)smoothing; } switch (smoothingType) { case MeshResultSmoothingType.None: // pair nodeValue as list<Dictionary<FaceId,nodeValue>> // all nodes are expected to have FaceIds List <Dictionary <IComparable, double> > nodeValuePairs = tempMappedElementResults.Select(x => x.ToDictionary(y => y.MeshFaceId, y => /*propFunction(y)*/ y.ResultToValue(meshResultDisplay))).ToList(); // put the Faces in a Dictionary<FaceId,Face> Dictionary <object, Face> faceDictionaryResult = mesh.Faces.ToDictionary(x => x.FindFragment <IAdapterId>(identifier).Id, x => x.Geometry()); Dictionary <object, Face> faceDictionaryRefrence = mesh.Faces.ToDictionary(x => x.FindFragment <IAdapterId>(identifier).Id, x => x.Geometry()); // Add all verticies to a list with their colour and update the Faces for (int k = 0; k < mesh.Nodes.Count; k++) { foreach (KeyValuePair <IComparable, double> FaceRelatedValue in nodeValuePairs[k]) { verts.Add(new Vertex() { Point = mesh.Nodes[k].Position, Color = gradient.Color(FaceRelatedValue.Value, from, to) }); // Face management, faceResult points to verts and faceReference points to mesh.Nodes Face faceResult = faceDictionaryResult[FaceRelatedValue.Key]; Face faceReference = faceDictionaryRefrence[FaceRelatedValue.Key]; Face newFace = new Face() { A = faceReference.A == k ? verts.Count - 1 : faceResult.A, B = faceReference.B == k ? verts.Count - 1 : faceResult.B, C = faceReference.C == k ? verts.Count - 1 : faceResult.C, D = faceReference.IsQuad() ? faceReference.D == k ? verts.Count - 1 : faceResult.D : -1 }; faceDictionaryResult[FaceRelatedValue.Key] = newFace; } } faces = faceDictionaryResult.Select(x => x.Value).ToList(); break; case MeshResultSmoothingType.ByPanel: case MeshResultSmoothingType.Global: List <double> nodeValues = tempMappedElementResults.Select(x => x[0].ResultToValue(meshResultDisplay)).ToList(); // Add all verticies to a list with their colour for (int k = 0; k < mesh.Nodes.Count; k++) { verts.Add(new Vertex() { Point = mesh.Nodes[k].Position, Color = gradient.Color(nodeValues[k], from, to) }); } faces = mesh.Faces.Geometry().ToList(); break; case MeshResultSmoothingType.ByFiniteElementCentres: case MeshResultSmoothingType.BySelection: default: Engine.Reflection.Compute.RecordError("Unsupported SmoothingType: " + Reflection.Query.PropertyValue(meshResult, "Smoothing").ToString() + " detected, meshResult for ObjectId: " + meshResult.ObjectId.ToString() + " and ResultCase: " + meshResult.ResultCase.ToString() + "will be returned empty."); return(new RenderMesh()); } return(new RenderMesh() { Vertices = verts, Faces = faces }); }