public static double Angle(XwVector v1, XwVector v0) { double lengthProduct = v0.Length() * v1.Length(); double angle; if (lengthProduct == 0.0) { angle = 0.0; } else { double cos = XwVector.DotProduct(v1, v0) / lengthProduct; if (cos > 1.0) { cos = 1.0; } else if (cos < -1.0) { cos = -1.0; } angle = XwVector.AcosWithAtan2(cos); } return(angle); }
public static XwVector NormalizeAndMultiply(XwVector v0, double value) { double length = v0.Length(); if (length <= 0.0) { return(new XwVector(0.0, 0.0, 0.0)); } else { return(XwVector.Multiply(v0, value / length)); } }
static void XwParseOpt(OptFile opt, int meshIndex, Face face, out XwVector outNormal, out double outAngle, out double outAngleSum) { var verticesVertex = new List <XwVector>(face.VerticesCount); var verticesLength = new List <double>(face.VerticesCount); var verticesAngle = new List <double>(face.VerticesCount); for (int index = 0; index < face.VerticesCount; index++) { int index2 = XwOptGetVertexIndex(index + 1, face.VerticesCount); XwVector v1 = new XwVector(opt.Meshes[meshIndex].Vertices[face.VerticesIndex.AtIndex(index)]); XwVector v0 = new XwVector(opt.Meshes[meshIndex].Vertices[face.VerticesIndex.AtIndex(index2)]); XwVector v = XwVector.Substract(v1, v0); verticesVertex.Add(v); verticesLength.Add(v.Length()); } outAngleSum = 0.0; for (int index = 0; index < face.VerticesCount; index++) { int index2 = XwOptGetVertexIndex(index - 1, face.VerticesCount); double angle = XwVector.AngleRadianToDegree(XwVector.Angle(verticesVertex[index2], verticesVertex[index])); verticesAngle.Add(angle); outAngleSum += angle; } int maxLengthVertexIndex = 0; for (int index = 1; index < face.VerticesCount; index++) { if (verticesLength[maxLengthVertexIndex] < verticesLength[index]) { maxLengthVertexIndex = index; } } if (verticesLength[XwOptGetVertexIndex(maxLengthVertexIndex - 1, face.VerticesCount)] < verticesLength[XwOptGetVertexIndex(maxLengthVertexIndex + 1, face.VerticesCount)]) { maxLengthVertexIndex = XwOptGetVertexIndex(maxLengthVertexIndex + 1, face.VerticesCount); } XwVector crossV0 = XwVector.Multiply(verticesVertex[XwOptGetVertexIndex(maxLengthVertexIndex - 1, face.VerticesCount)], -1.0); XwVector crossV1 = verticesVertex[maxLengthVertexIndex]; XwVector normal = XwVector.NormalizeAndMultiply(XwVector.CrossProduct(crossV0, crossV1), 1.0); XwVector ebpC8 = new XwVector(0.0, 0.0, 0.0); XwVector ebpD4 = new XwVector(0.0, 0.0, 0.0); XwVector ebpE0 = new XwVector(0.0, 0.0, 0.0); XwVector ebpEC = new XwVector(0.0, 0.0, 0.0); if (face.VerticesCount == 4) { maxLengthVertexIndex = XwOptGetVertexIndex(maxLengthVertexIndex + 2, face.VerticesCount); int index2 = XwOptGetVertexIndex(maxLengthVertexIndex - 1, face.VerticesCount); ebpC8 = XwVector.Multiply(verticesVertex[index2], -1.0); ebpD4 = verticesVertex[maxLengthVertexIndex]; ebpE0 = XwVector.NormalizeAndMultiply(XwVector.CrossProduct(ebpC8, ebpD4), 1.0); ebpEC = XwVector.NormalizeAndMultiply(XwVector.Add(normal, ebpE0), 1.0); } outAngle = XwVector.AngleRadianToDegree(XwVector.Angle(normal, new XwVector(face.Normal))); outNormal = normal; }