// Use this for initialization void Start() { MyRenderEngine render = new MyDirectX(); MyIShape shape = new MySphere(); shape.SetRenderEngine(render); shape.Draw(); MyRenderEngine renderOpenGl = new MyOpenGL(); shape.SetRenderEngine(renderOpenGl); shape.Draw(); }
public static MySphere CreateMySphereFromFace(Surface faceSurface) { var mySphereParameters = faceSurface.SphereParams; double[] mySphereCenter = { (double)mySphereParameters.GetValue(0), (double)mySphereParameters.GetValue(1), (double)mySphereParameters.GetValue(2) }; double mySphereRay = (double)mySphereParameters.GetValue(3); var newMySphere = new MySphere(mySphereCenter, mySphereRay); return(newMySphere); }
//Restituisce le equazioni cartesiane della circonferenza passante per V1, V2, V3 public static MyCircumForPath CircumPassingThrough(MyVertex V1, MyVertex V2, MyVertex V3, ref StringBuilder fileOutput, ModelDoc2 SwModel, SldWorks swApplication) { //The circumference does not exist if the 3 points lie on the same line or if 2 of them are coincident. if (V1.Lieonline(LinePassingThrough(V2, V3)) || V1.Equals(V2) || V1.Equals(V3) || V2.Equals(V3)) { MyCircumForPath OutputCircum = new MyCircumForPath(); return(OutputCircum); } else { MyPlane CircumPlane = PlanePassingThrough(V1, V2, V3); //There is a unique sphere passing through 4 points: we must arbitrarily choose a fourth point not lying on the V1-V2-V3 plane //So we choose the point resulting from adding the normal direction to a point lying on the V1-V2-V3 plane, for example V1 MyVertex V4 = new MyVertex(V1.x + CircumPlane.a * 10, V1.y + CircumPlane.b * 10, V1.z + CircumPlane.c * 10); //Vertex V4 = new Vertex(0, 0, 0); //if (SwModel != null) //{ // SwModel = swApplication.ActiveDoc; // SwModel.ClearSelection2(true); // SwModel.Insert3DSketch(); // SwModel.CreatePoint2(V1.x, V1.y, V1.z); // SwModel.InsertSketch(); // SwModel.CreatePoint2(V2.x, V2.y, V2.z); // SwModel.InsertSketch(); // SwModel.CreatePoint2(V3.x, V3.y, V3.z); // SwModel.InsertSketch(); // SwModel.CreatePoint2(V4.x, V4.y, V4.z); // SwModel.InsertSketch(); //} MySphere CircumSphere = SpherePassingThrough(V1, V2, V3, V4, ref fileOutput, SwModel); if (CircumSphere.centerSphere == null) { } else if (CircumSphere.centerSphere != null) { MyCircumForPath OutputCircum = new MyCircumForPath(CircumPlane, CircumSphere); return(OutputCircum); } } MyCircumForPath OutputCircumNull = new MyCircumForPath(); return(OutputCircumNull); }
//Restituisce l'equazione cartesiana della sfera passante per V1, V2, V3, V4 //[DAI TEST: i 4 vertici stanno sulla sfera con precisione dell'ordine in media di 10^(-6) se // le coordinate dei vertici sono numeri con al più 3 cifre dopo la virgola e 3 cifre prima della virgola. // Inoltre ho osservato che più aumenta la differenza tra l'ordine di grandezza tra tutte le coordinate dei vari // vertici e più diminuisce la precisione...o no???] public static MySphere SpherePassingThrough(MyVertex V1, MyVertex V2, MyVertex V3, MyVertex V4, ref StringBuilder fileOutput, ModelDoc2 SwModel) { //MySphere OutputMySphere = new MySphere(); //First, the sphere does not exist if 3 of the 4 points lie on a line. Four possible line: V1-V2-V3, V1-V3-V4, V1-V2-V4, V2-V3-V4 //Second, the sphere does not exist if the 4 points lie on the same plane. //(MANCA IL CONTROLLO CHE NON SIANO PUNTI COINCIDENTI...) if (V1.Lieonline(LinePassingThrough(V2, V3)) || V1.Lieonline(LinePassingThrough(V3, V4)) || V1.Lieonline(LinePassingThrough(V2, V4)) || V2.Lieonline(LinePassingThrough(V3, V4))) { fileOutput.AppendLine("Sistema non risolvibile"); MySphere OutputMySphere = new MySphere(); return(OutputMySphere); // MySphere does not exist } else { double[,] CoefficientsMatrix = { { V1.x, V1.y, V1.z, 1 }, { V2.x, V2.y, V2.z, 1 }, { V3.x, V3.y, V3.z, 1 }, { V4.x, V4.y, V4.z, 1 }, }; double alfa = Math.Pow(V1.x, 2) + Math.Pow(V1.y, 2) + Math.Pow(V1.z, 2); double beta = Math.Pow(V2.x, 2) + Math.Pow(V2.y, 2) + Math.Pow(V2.z, 2); double gamma = Math.Pow(V3.x, 2) + Math.Pow(V3.y, 2) + Math.Pow(V3.z, 2); double delta = Math.Pow(V4.x, 2) + Math.Pow(V4.y, 2) + Math.Pow(V4.z, 2); double[,] KnownTerms = { { -alfa }, { -beta }, { -gamma }, { -delta } }; double[,] SphereCoef = Matrix.Solve(CoefficientsMatrix, KnownTerms, leastSquares: true); //OutputMySphere.a = SphereCoef[0, 0]; OutputMySphere.b = SphereCoef[1, 0]; OutputMySphere.c = SphereCoef[2, 0]; OutputMySphere.d = SphereCoef[3, 0]; MySphere OutputMySphere = new MySphere(SphereCoef[0, 0], SphereCoef[1, 0], SphereCoef[2, 0], SphereCoef[3, 0]); return(OutputMySphere); } }
public static bool MyEqualsSurface(Surface firstSurf, Surface secondSurf, SldWorks swApplWorks) { var answer = false; //var FirstSurface = (Surface)FirstFace.GetSurface(); var typeOfFirstSurf = firstSurf.Identity(); //var SecondSurface = (Surface)SecondFace.GetSurface(); var typeOfSecondSurf = secondSurf.Identity(); if (typeOfFirstSurf == typeOfSecondSurf) { if (firstSurf.IsPlane()) { var firstPlaneParameters = firstSurf.PlaneParams; double[] firstPlaneNormal = { (double)firstPlaneParameters.GetValue(0), (double)firstPlaneParameters.GetValue(1), (double)firstPlaneParameters.GetValue(2) }; double[] firstPlanePoint = { (double)firstPlaneParameters.GetValue(3), (double)firstPlaneParameters.GetValue(4), (double)firstPlaneParameters.GetValue(5) }; MyPlane firstPlane = new MyPlane(firstPlaneNormal, firstPlanePoint); var secondPlaneParameters = secondSurf.PlaneParams; double[] secondPlaneNormal = { (double)secondPlaneParameters.GetValue(0), (double)secondPlaneParameters.GetValue(1), (double)secondPlaneParameters.GetValue(2) }; double[] secondPlanePoint = { (double)secondPlaneParameters.GetValue(3), (double)secondPlaneParameters.GetValue(4), (double)secondPlaneParameters.GetValue(5) }; MyPlane secondPlane = new MyPlane(secondPlaneNormal, secondPlanePoint); //string fileNameBuildRepeatedEntity = "buildRepeatedEntity.txt"; //string whatToWrite = string.Format("Primo piano: a {0}, b {1}, c {2}, d {3}", firstPlane.a, firstPlane.b, firstPlane.c, firstPlane.d); //KLdebug.Print(whatToWrite, fileNameBuildRepeatedEntity); //whatToWrite = string.Format("Secondo piano: a {0}, b {1}, c {2}, d {3}", secondPlane.a, secondPlane.b, secondPlane.c, secondPlane.d); //KLdebug.Print(whatToWrite, fileNameBuildRepeatedEntity); if (firstPlane.Equals(secondPlane)) { answer = true; } } if (firstSurf.IsSphere()) { var firstSphereParameters = firstSurf.SphereParams; double[] firstSphereCenter = { (double)firstSphereParameters.GetValue(0), (double)firstSphereParameters.GetValue(1), (double)firstSphereParameters.GetValue(2) }; double firstSphereRay = (double)firstSphereParameters.GetValue(3); var firstSphere = new MySphere(firstSphereCenter, firstSphereRay); var secondSphereParameters = secondSurf.SphereParams; double[] secondSphereCenter = { (double)secondSphereParameters.GetValue(0), (double)secondSphereParameters.GetValue(1), (double)secondSphereParameters.GetValue(2) }; double secondSphereRay = (double)secondSphereParameters.GetValue(3); var secondSphere = new MySphere(secondSphereCenter, secondSphereRay); if (firstSphere.Equals(secondSphere)) { answer = true; } } if (firstSurf.IsCone()) { var firstConeParameters = firstSurf.ConeParams; swApplWorks.SendMsgToUser("lunghezza vettore paramentri: " + firstConeParameters.GetValue(0)); swApplWorks.SendMsgToUser("lunghezza vettore paramentri: " + firstConeParameters.GetValue(1)); swApplWorks.SendMsgToUser("lunghezza vettore paramentri: " + firstConeParameters.GetValue(2)); swApplWorks.SendMsgToUser("lunghezza vettore paramentri: " + firstConeParameters.GetValue(3)); swApplWorks.SendMsgToUser("lunghezza vettore paramentri: " + firstConeParameters.GetValue(4)); swApplWorks.SendMsgToUser("lunghezza vettore paramentri: " + firstConeParameters.GetValue(5)); swApplWorks.SendMsgToUser("lunghezza vettore paramentri: " + firstConeParameters.GetValue(6)); swApplWorks.SendMsgToUser("lunghezza vettore paramentri: " + firstConeParameters.GetValue(7)); swApplWorks.SendMsgToUser("lunghezza vettore paramentri: " + firstConeParameters.GetValue(8)); double[] firstConeOrigin = { (double)firstConeParameters.GetValue(0), (double)firstConeParameters.GetValue(1), (double)firstConeParameters.GetValue(2) }; double[] firstConeAxis = { (double)firstConeParameters.GetValue(3), (double)firstConeParameters.GetValue(4), (double)firstConeParameters.GetValue(5) }; double firstConeHalfAngle = (double)firstConeParameters.GetValue(8); var firstCone = new MyCone(firstConeOrigin, firstConeAxis, firstConeHalfAngle); var secondConeParameters = secondSurf.ConeParams; double[] secondConeOrigin = { (double)secondConeParameters.GetValue(0), (double)secondConeParameters.GetValue(1), (double)secondConeParameters.GetValue(2) }; double[] secondConeAxis = { (double)secondConeParameters.GetValue(3), (double)secondConeParameters.GetValue(4), (double)secondConeParameters.GetValue(5) }; double secondConeHalfAngle = (double)secondConeParameters.GetValue(8); var secondCone = new MyCone(secondConeOrigin, secondConeAxis, secondConeHalfAngle); if (firstCone.Equals(secondCone)) { // L'EQUALS DEL CONO NON E' PROPRIO GIUSTO. answer = true; } } if (firstSurf.IsCylinder()) { double[] firstCylinderParameters = firstSurf.CylinderParams; double[] firstCylinderOrigin = { (double)firstCylinderParameters.GetValue(0), (double)firstCylinderParameters.GetValue(1), (double)firstCylinderParameters.GetValue(2) }; double[] firstCylinderAxis = { (double)firstCylinderParameters.GetValue(3), (double)firstCylinderParameters.GetValue(4), (double)firstCylinderParameters.GetValue(5) }; double firstCylinderRadius = (double)firstCylinderParameters.GetValue(6); var firstCylinder = new MyCylinder(firstCylinderOrigin, firstCylinderAxis, firstCylinderRadius); double[] secondCylinderParameters = secondSurf.CylinderParams; double[] secondCylinderOrigin = { (double)secondCylinderParameters.GetValue(0), (double)secondCylinderParameters.GetValue(1), (double)secondCylinderParameters.GetValue(2) }; double[] secondCylinderAxis = { (double)secondCylinderParameters.GetValue(3), (double)secondCylinderParameters.GetValue(4), (double)secondCylinderParameters.GetValue(5) }; double secondCylinderRadius = (double)secondCylinderParameters.GetValue(6); var secondCylinder = new MyCylinder(secondCylinderOrigin, secondCylinderAxis, secondCylinderRadius); if (firstCylinder.Equals(secondCylinder)) { // CONTROLLARE L'EQUALS DEL CILINDRO answer = true; } } if (firstSurf.IsTorus()) { var firstTorusParameters = firstSurf.TorusParams; double[] firstTorusCenter = { (double)firstTorusParameters.GetValue(0), (double)firstTorusParameters.GetValue(1), (double)firstTorusParameters.GetValue(2) }; double[] firstTorusAxis = { (double)firstTorusParameters.GetValue(3), (double)firstTorusParameters.GetValue(4), (double)firstTorusParameters.GetValue(5) }; double firstTorusMajorRadius = (double)firstTorusParameters.GetValue(6); double firstTorusMinorRadius = (double)firstTorusParameters.GetValue(7); var firstTorus = new MyTorus(firstTorusCenter, firstTorusAxis, firstTorusMajorRadius, firstTorusMinorRadius); var secondTorusParameters = secondSurf.TorusParams; double[] secondTorusCenter = { (double)secondTorusParameters.GetValue(0), (double)secondTorusParameters.GetValue(1), (double)secondTorusParameters.GetValue(2) }; double[] secondTorusAxis = { (double)secondTorusParameters.GetValue(3), (double)secondTorusParameters.GetValue(4), (double)secondTorusParameters.GetValue(5) }; double secondTorusMajorRadius = (double)secondTorusParameters.GetValue(6); double secondTorusMinorRadius = (double)secondTorusParameters.GetValue(7); var secondTorus = new MyTorus(secondTorusCenter, secondTorusAxis, secondTorusMajorRadius, secondTorusMinorRadius); if (firstTorus.Equals(secondTorus)) { // CONTROLLARE L'EQUALS DEL TORO answer = true; } } } else { answer = false; } return(answer); }