1
0
mirror of https://github.com/fadden/6502bench.git synced 2024-11-29 10:50:28 +00:00

Consolidate wireframe data validation

Some tests were duplicated between VisWireframe and the code that
consumed the data.  We now expose the Validate function as a public
interface, and invoke it from WireframeObject.  Failed validation
results in a null object being returned, which was previously allowed
but not actually checked for.
This commit is contained in:
Andy McFadden 2020-04-23 11:25:45 -07:00
parent 3820bfee8b
commit ea379fce18
5 changed files with 63 additions and 45 deletions

View File

@ -369,6 +369,8 @@ namespace PluginCommon {
IntPair[] GetEdgeFaces();
int[] GetExcludedVertices();
int[] GetExcludedEdges();
bool Validate(out string msg);
}
/// <summary>

View File

@ -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++) {

View File

@ -203,7 +203,11 @@ namespace SourceGen {
} else {
Debug.Assert(parms != null);
WireframeObject wireObj = WireframeObject.Create(visWire);
if (wireObj != null) {
CachedImage = GenerateWireframeImage(wireObj, THUMBNAIL_DIM, parms);
} else {
CachedImage = BROKEN_IMAGE;
}
}
Debug.Assert(CachedImage.IsFrozen);
}

View File

@ -105,8 +105,15 @@ namespace SourceGen {
/// Creates a new object from a wireframe visualization.
/// </summary>
/// <param name="visWire">Visualization object.</param>
/// <returns>New object.</returns>
/// <returns>New object, or null if visualization data fails validation.</returns>
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);

View File

@ -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 {
WireframeObject wireObj = WireframeObject.Create(visWire);
if (wireObj != null) {
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);
wireframePath.Data =
Visualization.GenerateWireframePath(wireObj, dim, parms);
BitmapDimensions = "n/a";
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;
}
}