diff --git a/PluginCommon/Interfaces.cs b/PluginCommon/Interfaces.cs index 612dcc7..4014c83 100644 --- a/PluginCommon/Interfaces.cs +++ b/PluginCommon/Interfaces.cs @@ -369,6 +369,8 @@ namespace PluginCommon { IntPair[] GetEdgeFaces(); int[] GetExcludedVertices(); int[] GetExcludedEdges(); + + bool Validate(out string msg); } /// diff --git a/PluginCommon/VisWireframe.cs b/PluginCommon/VisWireframe.cs index 814d465..6b2d259 100644 --- a/PluginCommon/VisWireframe.cs +++ b/PluginCommon/VisWireframe.cs @@ -190,6 +190,10 @@ namespace PluginCommon { msg = "no vertices defined"; return false; } + if (mVerticesX.Count != mVerticesY.Count || mVerticesX.Count != mVerticesZ.Count) { + msg = "inconsistent vertex arrays"; + return false; + } // check points foreach (int vi in mPoints) { @@ -234,6 +238,10 @@ namespace PluginCommon { return false; } } + if (mNormalsX.Count != mNormalsY.Count || mNormalsX.Count != mNormalsZ.Count) { + msg = "inconsistent normal arrays"; + return false; + } // check excluded vertices for (int i = 0; i < mExcludedVertices.Count; i++) { diff --git a/SourceGen/Visualization.cs b/SourceGen/Visualization.cs index 1624005..137e40c 100644 --- a/SourceGen/Visualization.cs +++ b/SourceGen/Visualization.cs @@ -203,7 +203,11 @@ namespace SourceGen { } else { Debug.Assert(parms != null); WireframeObject wireObj = WireframeObject.Create(visWire); - CachedImage = GenerateWireframeImage(wireObj, THUMBNAIL_DIM, parms); + if (wireObj != null) { + CachedImage = GenerateWireframeImage(wireObj, THUMBNAIL_DIM, parms); + } else { + CachedImage = BROKEN_IMAGE; + } } Debug.Assert(CachedImage.IsFrozen); } diff --git a/SourceGen/WireframeObject.cs b/SourceGen/WireframeObject.cs index 453fb07..a30ccaf 100644 --- a/SourceGen/WireframeObject.cs +++ b/SourceGen/WireframeObject.cs @@ -105,8 +105,15 @@ namespace SourceGen { /// Creates a new object from a wireframe visualization. /// /// Visualization object. - /// New object. + /// New object, or null if visualization data fails validation. public static WireframeObject Create(IVisualizationWireframe visWire) { + if (!visWire.Validate(out string msg)) { + // Should not be here -- visualizer should have checked validation and + // reported an error. + Debug.WriteLine("Wireframe validation failed: " + msg); + return null; + } + WireframeObject wireObj = new WireframeObject(); wireObj.mIs2d = visWire.Is2d; @@ -125,11 +132,6 @@ namespace SourceGen { float[] normalsY = visWire.GetNormalsY(); float[] normalsZ = visWire.GetNormalsZ(); - if (normalsX.Length != normalsY.Length || normalsX.Length != normalsZ.Length) { - Debug.Assert(false); - return null; - } - for (int i = 0; i < normalsX.Length; i++) { wireObj.mFaces.Add(new Face(normalsX[i], normalsY[i], normalsZ[i])); } @@ -139,14 +141,6 @@ namespace SourceGen { float[] verticesY = visWire.GetVerticesY(); float[] verticesZ = visWire.GetVerticesZ(); int[] excludedVertices = visWire.GetExcludedVertices(); - if (verticesX.Length == 0) { - Debug.Assert(false); - return null; - } - if (verticesX.Length != verticesY.Length || verticesX.Length != verticesZ.Length) { - Debug.Assert(false); - return null; - } // Compute min/max for X/Y for 2d re-centering. The trick is that we only want // to use vertices that are visible. If the shape starts with a huge move off to @@ -173,11 +167,11 @@ namespace SourceGen { int v0index = edges[i].Val0; int v1index = edges[i].Val1; - if (v0index < 0 || v0index >= wireObj.mVertices.Count || - v1index < 0 || v1index >= wireObj.mVertices.Count) { - Debug.Assert(false); - return null; - } + //if (v0index < 0 || v0index >= wireObj.mVertices.Count || + // v1index < 0 || v1index >= wireObj.mVertices.Count) { + // Debug.Assert(false); + // return null; + //} Vertex vert0 = wireObj.mVertices[v0index]; Vertex vert1 = wireObj.mVertices[v1index]; @@ -192,11 +186,11 @@ namespace SourceGen { int vindex = vfaces[i].Val0; int findex = vfaces[i].Val1; - if (vindex < 0 || vindex >= wireObj.mVertices.Count || - findex < 0 || findex >= wireObj.mFaces.Count) { - Debug.Assert(false); - return null; - } + //if (vindex < 0 || vindex >= wireObj.mVertices.Count || + // findex < 0 || findex >= wireObj.mFaces.Count) { + // Debug.Assert(false); + // return null; + //} Face face = wireObj.mFaces[findex]; wireObj.mVertices[vindex].Faces.Add(face); @@ -210,11 +204,11 @@ namespace SourceGen { int eindex = efaces[i].Val0; int findex = efaces[i].Val1; - if (eindex < 0 || eindex >= wireObj.mEdges.Count || - findex < 0 || findex >= wireObj.mFaces.Count) { - Debug.Assert(false); - return null; - } + //if (eindex < 0 || eindex >= wireObj.mEdges.Count || + // findex < 0 || findex >= wireObj.mFaces.Count) { + // Debug.Assert(false); + // return null; + //} Face face = wireObj.mFaces[findex]; wireObj.mEdges[eindex].Faces.Add(face); diff --git a/SourceGen/WpfGui/EditVisualization.xaml.cs b/SourceGen/WpfGui/EditVisualization.xaml.cs index 758f920..374e1c8 100644 --- a/SourceGen/WpfGui/EditVisualization.xaml.cs +++ b/SourceGen/WpfGui/EditVisualization.xaml.cs @@ -591,15 +591,9 @@ namespace SourceGen.WpfGui { LastPluginMessage = ex.Message; } } + bool failed = false; if (vis2d == null && visWire == null) { - previewImage.Source = sBadParamsImage; - if (!string.IsNullOrEmpty(LastPluginMessage)) { - // Report the last message we got as an error. - PluginErrMessage = LastPluginMessage; - } else { - PluginErrMessage = (string)FindResource("str_VisGenFailed"); - } - IsValid = false; + failed = true; } else if (vis2d != null) { previewGrid.Background = null; previewImage.Source = Visualization.ConvertToBitmapSource(vis2d); @@ -609,15 +603,31 @@ namespace SourceGen.WpfGui { mThumbnail = (BitmapSource)previewImage.Source; } else { - previewGrid.Background = Brushes.Black; - previewImage.Source = Visualization.BLANK_IMAGE; - double dim = Math.Floor( - Math.Min(previewGrid.ActualWidth, previewGrid.ActualHeight)); WireframeObject wireObj = WireframeObject.Create(visWire); - wireframePath.Data = Visualization.GenerateWireframePath(wireObj, dim, parms); - BitmapDimensions = "n/a"; + if (wireObj != null) { + previewGrid.Background = Brushes.Black; + previewImage.Source = Visualization.BLANK_IMAGE; + double dim = Math.Floor( + Math.Min(previewGrid.ActualWidth, previewGrid.ActualHeight)); + wireframePath.Data = + Visualization.GenerateWireframePath(wireObj, dim, parms); + BitmapDimensions = "n/a"; - mWireObj = wireObj; + mWireObj = wireObj; + } else { + failed = true; + } + } + if (failed) { + previewImage.Source = sBadParamsImage; + if (!string.IsNullOrEmpty(LastPluginMessage)) { + // Report the last message we got as an error. + PluginErrMessage = LastPluginMessage; + } else { + // Generic failure message. + PluginErrMessage = (string)FindResource("str_VisGenFailed"); + } + IsValid = false; } }