public List <Line> SubdivideThreeLines(SpringMesh sMesh, double factor)
        {
            List <Line> allLines = new List <Line>();

            foreach (Edge edge in sMesh.Edges)
            {
                if (edge.SecondTriangleIndex >= 0)
                {
                    int triangle1 = edge.FirstTriangleIndex;
                    int triangle2 = edge.SecondTriangleIndex;

                    //Point3d centreTri1 = sMesh.ComputeTriangleCenter(triangle1);
                    //Point3d centreTri2 = sMesh.ComputeTriangleCenter(triangle2);
                    Point3d centreTri1 = sMesh.ComputeCircumscribedCircleCenter(triangle1);
                    Point3d centreTri2 = sMesh.ComputeCircumscribedCircleCenter(triangle2);

                    List <Point3d> pointOnTri1 = sMesh.ComputeTriangleThreePts(triangle1, factor);
                    List <Point3d> pointOnTri2 = sMesh.ComputeTriangleThreePts(triangle2, factor);

                    foreach (Point3d IntPtr in pointOnTri1)
                    {
                        allLines.Add(new Line(centreTri1, IntPtr));
                    }
                    foreach (Point3d IntPtr in pointOnTri2)
                    {
                        allLines.Add(new Line(centreTri2, IntPtr));
                    }
                }
            }

            return(allLines);
        }
        public List <double> checkCoplanarity(SpringMesh sMesh, double thickness, List <Line> linesUp, List <Line> linesDown)
        {
            List <double> coplanarity = new List <double>();

            foreach (Edge edge in sMesh.Edges)
            {
                if (edge.SecondTriangleIndex >= 0)
                {
                    int triLeft  = edge.FirstTriangleIndex;
                    int triRight = edge.SecondTriangleIndex;

                    //Point3d centreLeft = sMesh.ComputeTriangleCenter(triLeft);
                    //Point3d centreRight = sMesh.ComputeTriangleCenter(triRight);

                    Point3d centreLeft  = sMesh.ComputeCircumscribedCircleCenter(triLeft);
                    Point3d centreRight = sMesh.ComputeCircumscribedCircleCenter(triRight);

                    Vector3d normalLeft  = sMesh.ComputeTriangleNormal(triLeft);
                    Vector3d normalRight = sMesh.ComputeTriangleNormal(triRight);

                    Point3d centreLeftUp  = centreLeft + normalLeft * thickness;
                    Point3d centreRightUp = centreRight + normalRight * thickness;

                    Point3d centreLeftDown  = centreLeft - normalLeft * thickness;
                    Point3d centreRightDown = centreRight - normalRight * thickness;

                    linesUp.Add(new Line(centreLeftUp, centreRightUp));
                    linesDown.Add(new Line(centreLeftDown, centreRightDown));

                    Line planarCheckLine01 = new Line(centreLeftUp, centreRightDown);
                    Line planarCheckLine02 = new Line(centreRightUp, centreLeftDown);

                    double dist = planarCheckLine01.MinimumDistanceTo(planarCheckLine02);
                    coplanarity.Add(dist);
                }
            }
            return(coplanarity);
        }
Exemple #3
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // =============================================================================================================
            // Reset
            // =============================================================================================================

            DA.GetData <bool>("Reset", ref iReset);

            if (iReset)
            {
                DA.GetData <SpringMesh>("Spring Mesh", ref iSpringMesh);
                DA.GetData <int>("Population Size", ref iPopulationSize);

                triangleCentres = new List <Point3d>();
                trianglePlanes  = new List <Plane>();
                triangleNormals = new List <Vector3d>();
                for (int i = 0; i < iSpringMesh.Triangles.Count; i++)
                {
                    triangleCentres.Add(iSpringMesh.ComputeCircumscribedCircleCenter(i));
                    trianglePlanes.Add(new Plane(
                                           iSpringMesh.Vertices[iSpringMesh.Triangles[i].FirstVertexIndex].Position,
                                           iSpringMesh.Vertices[iSpringMesh.Triangles[i].SecondVertexIndex].Position,
                                           iSpringMesh.Vertices[iSpringMesh.Triangles[i].ThirdVertexIndex].Position
                                           ));
                    triangleNormals.Add(iSpringMesh.ComputeTriangleNormal(i));
                }

                edgeXAxes   = new List <Vector3d>();
                edgeYAxes   = new List <Vector3d>();
                edgeZAxes   = new List <Vector3d>();
                edgePlanes  = new List <Plane>();
                edgeOrigins = new List <Point3d>();

                List <double> geneMinValues = new List <double>();
                List <double> geneMaxValues = new List <double>();

                foreach (Edge edge in iSpringMesh.Edges)
                {
                    if (edge.SecondTriangleIndex >= 0)
                    {
                        Vector3d edgeXAxis = iSpringMesh.Vertices[edge.SecondVertexIndex].Position - iSpringMesh.Vertices[edge.FirstVertexIndex].Position;
                        edgeXAxes.Add(edgeXAxis);

                        Vector3d edgeZAxis = triangleNormals[edge.FirstTriangleIndex] + triangleNormals[edge.SecondTriangleIndex];
                        edgeZAxis.Unitize();
                        edgeZAxes.Add(edgeZAxis);

                        Vector3d edgeYAxis = Vector3d.CrossProduct(edgeZAxis, edgeXAxis);
                        edgeYAxes.Add(edgeYAxis);

                        Point3d edgePlaneOrigin = 0.5 * (iSpringMesh.Vertices[edge.FirstVertexIndex].Position + iSpringMesh.Vertices[edge.SecondVertexIndex].Position);

                        edgeOrigins.Add(edgePlaneOrigin);


                        edgePlanes.Add(new Plane(edgePlaneOrigin, edgeXAxis, edgeYAxis));

                        double angle = Utils.AngleBetweenTwoUnitVectors(edgeZAxis, triangleNormals[edge.FirstTriangleIndex]);

                        geneMinValues.Add(-angle);
                        geneMaxValues.Add(angle);
                    }
                    else
                    {
                        Vector3d edgeXAxis = iSpringMesh.Vertices[edge.SecondVertexIndex].Position - iSpringMesh.Vertices[edge.FirstVertexIndex].Position;
                        edgeXAxes.Add(edgeXAxis);

                        Vector3d edgeZAxis = triangleNormals[edge.FirstTriangleIndex];
                        if (edgeZAxis.Z < 0.0)
                        {
                            edgeZAxis += -Vector3d.ZAxis;
                        }
                        else
                        {
                            Vector3d temp = new Vector3d(-edgeZAxis.X, -edgeZAxis.Y, 0.0);
                            temp.Unitize();
                            edgeZAxis += temp;
                        }

                        edgeZAxis.Unitize();
                        edgeZAxes.Add(edgeZAxis);

                        Vector3d edgeYAxis = Vector3d.CrossProduct(triangleNormals[edge.FirstTriangleIndex], edgeXAxis);
                        edgeYAxes.Add(edgeYAxis);

                        Point3d edgePlaneOrigin = 0.5 * (iSpringMesh.Vertices[edge.FirstVertexIndex].Position + iSpringMesh.Vertices[edge.SecondVertexIndex].Position);

                        edgeOrigins.Add(edgePlaneOrigin);

                        edgePlanes.Add(new Plane(edgePlaneOrigin, edgeXAxis, edgeYAxis));

                        double angle = Utils.AngleBetweenTwoUnitVectors(edgeZAxis, triangleNormals[edge.FirstTriangleIndex]);

                        geneMinValues.Add(-angle);
                        geneMaxValues.Add(angle);
                    }
                }

                beagle = new Beagle(iPopulationSize, geneMinValues, geneMaxValues);

                foreach (Genome genome in beagle.Genomes)
                {
                    computeFitness(genome);
                }

                goto Conclusion;
            }


            DA.GetData <bool>("Play", ref iPlay);

            if (iPlay)
            {
                ExpireSolution(true);
            }
            else
            {
                List <double> values = new List <double>();
                foreach (Gene gene in beagle.Genomes[0].Genes)
                {
                    values.Add(gene.Value);
                }
                DA.SetDataList(4, values);
                goto Conclusion;
            }
            //if (!iPlay) goto Conclusion;


            // ====================================================================================================
            // Evolution
            // ====================================================================================================

            DA.GetData <int>("Subiteration Count", ref iSubiterationCount);
            DA.GetData <double>("Max. Coupling Distance", ref iMaxCouplingDistance);
            DA.GetData <double>("Mutation Rate", ref iMutationRate);
            DA.GetData <double>("Mutation Amount", ref iMutationAmount);

            beagle.MaxCouplingDistance = iMaxCouplingDistance;
            beagle.MutationRate        = iMutationRate;
            beagle.MutationAmount      = iMutationAmount;

            for (int i = 0; i < iSubiterationCount; i++)
            {
                beagle.SelectParents();
                beagle.Reproduce();

                for (int j = beagle.PopulationSize; j < beagle.Genomes.Count; j++)
                {
                    computeFitness(beagle.Genomes[j]);
                }

                beagle.SelectFittests();
            }

            DA.SetDataList(3, new List <double> {
                beagle.Genomes[0].Fitness
            });

            // ====================================================================================================
            // Conclusion
            // ====================================================================================================

Conclusion:

            Genome bestGenome = beagle.Genomes[0];

            for (int i = 0; i < bestGenome.Genes.Count; i++)
            {
                double angle = bestGenome.Genes[i].Value;

                Vector3d planeNormal = Math.Sin(angle) * edgeYAxes[i] + Math.Cos(angle) * edgeZAxes[i];

                edgePlanes[i] = new Plane(
                    edgePlanes[i].Origin,
                    edgePlanes[i].XAxis,
                    Vector3d.CrossProduct(planeNormal, edgePlanes[i].XAxis)
                    );
            }

            //List<Plane> nastyPlanes = new List<Plane>();
            List <Point3d> planeTripletIntersections = new List <Point3d>();

            foreach (Triangle triangle in iSpringMesh.Triangles)
            {
                Point3d intersectionPoint;
                if (Intersection.PlanePlanePlane(edgePlanes[triangle.FirstEdgeIndex], edgePlanes[triangle.SecondEdgeIndex], edgePlanes[triangle.ThirdEdgeIndex], out intersectionPoint))
                {
                    planeTripletIntersections.Add(intersectionPoint);
                }
                //else
                //{
                //    nastyPlanes.Add(edgePlanes[triangle.FirstEdgeIndex]);
                //    nastyPlanes.Add(edgePlanes[triangle.FirstEdgeIndex]);
                //    nastyPlanes.Add(edgePlanes[triangle.FirstEdgeIndex]);
                //}
            }

            DA.SetDataList(1, edgePlanes);
            DA.SetDataList(2, planeTripletIntersections);



            //DA.SetDataList(1, edgePlanes);
            //DA.SetDataList(2, edgeOrigins);
            //DA.SetDataList(3, edgeXAxes);
            //DA.SetDataList(4, edgeYAxes);
            //DA.SetDataList(5, edgeZAxes);
        }
        public bool CalculateSurface(SpringMesh sMesh, double thickness, List <Curve> curvesRight, List <Curve> curvesLeft)
        {
            foreach (Edge edge in sMesh.Edges)
            {
                if (edge.SecondTriangleIndex >= 0)
                {
                    // first vertex
                    // first triangle
                    #region curve01

                    int firstPtId            = edge.FirstVertexIndex;
                    int firstTriangleIdRight = edge.FirstTriangleIndex;

                    Vertex firstPtVertex = sMesh.Vertices[firstPtId];
                    //Point3d firstTriCentreRight = sMesh.ComputeTriangleCenter(firstTriangleIdRight);
                    Point3d firstTriCentreRight = sMesh.ComputeCircumscribedCircleCenter(firstTriangleIdRight);

                    Vector3d firstNormalRight = thickness * sMesh.ComputeTriangleNormal(firstTriangleIdRight);

                    Point3d firstTriCentreUpRight   = firstTriCentreRight + firstNormalRight;    // the first control point
                    Point3d firstTriCentreDownRight = firstTriCentreRight - firstNormalRight;    // the seventh control point

                    Point3d firstCPt02Right = (firstPtVertex.Factor - 0.2) * firstPtVertex.Position + (1 - firstPtVertex.Factor + 0.2) * firstTriCentreRight;

                    Point3d firstCPt02UpRight   = firstCPt02Right + firstNormalRight;              // the second control point
                    Point3d firstCPt02DownRight = firstCPt02Right - firstNormalRight;              // the sixth control point

                    Point3d firstCPt03Right = firstPtVertex.Factor * firstPtVertex.Position + (1 - firstPtVertex.Factor) * firstTriCentreRight;

                    Point3d firstCPt03UpRight   = firstCPt03Right + firstNormalRight;      // the third control point
                    Point3d firstCPt03DownRight = firstCPt03Right - firstNormalRight;      // the fifth control point

                    Point3d firstUpPtRight = firstPtVertex.Position;                       // the fourth control point

                    List <Point3d> firstControlPointRight = new List <Point3d>()
                    {
                        firstTriCentreUpRight, firstCPt02UpRight, firstCPt03UpRight, firstUpPtRight,
                        firstCPt03DownRight, firstCPt02DownRight, firstTriCentreDownRight
                    };
                    #endregion
                    NurbsCurve firstNurbCurveRight = NurbsCurve.Create(false, 5, firstControlPointRight);

                    // second triangle
                    #region curve02

                    int firstTriangleIdLeft = edge.SecondTriangleIndex;

                    //Point3d firstTriCentreLeft = sMesh.ComputeTriangleCenter(firstTriangleIdLeft);
                    Point3d firstTriCentreLeft = sMesh.ComputeCircumscribedCircleCenter(firstTriangleIdLeft);

                    Vector3d firstNormalLeft = thickness * sMesh.ComputeTriangleNormal(firstTriangleIdLeft);

                    Point3d firstTriCentreUpLeft   = firstTriCentreLeft + firstNormalLeft;    // the first control point
                    Point3d firstTriCentreDownLeft = firstTriCentreLeft - firstNormalLeft;    // the seventh control point

                    Point3d firstCPt02Left = (firstPtVertex.Factor - 0.2) * firstPtVertex.Position + (1 - firstPtVertex.Factor + 0.2) * firstTriCentreLeft;

                    Point3d firstCPt02UpLeft   = firstCPt02Left + firstNormalLeft;              // the second control point
                    Point3d firstCPt02DownLeft = firstCPt02Left - firstNormalLeft;              // the sixth control point

                    Point3d firstCPt03Left = firstPtVertex.Factor * firstPtVertex.Position + (1 - firstPtVertex.Factor) * firstTriCentreLeft;

                    Point3d firstCPt03UpLeft   = firstCPt03Left + firstNormalLeft;        // the third control point
                    Point3d firstCPt03DownLeft = firstCPt03Left - firstNormalLeft;        // the fifth control point

                    Point3d firstUpPtLeft = firstPtVertex.Position;                       // the fourth control point

                    List <Point3d> firstControlPointLeft = new List <Point3d>()
                    {
                        firstTriCentreUpLeft, firstCPt02UpLeft, firstCPt03UpLeft, firstUpPtLeft,
                        firstCPt03DownLeft, firstCPt02DownLeft, firstTriCentreDownLeft
                    };
                    #endregion
                    NurbsCurve firstNurbCurveLeft = NurbsCurve.Create(false, 5, firstControlPointLeft);

                    curvesRight.Add(firstNurbCurveRight);
                    curvesLeft.Add(firstNurbCurveLeft);

                    /*
                     * Brep[] brep1 = Brep.CreateFromLoft(
                     *  new List<Curve>() { firstNurbCurveRight, firstNurbCurveLeft },
                     *  Point3d.Unset, Point3d.Unset, LoftType.Developable, false);
                     *
                     * if (brep1.Length > 0) breps.Add(brep1[0]); */

                    // second vertex
                    // first triangle
                    #region curve03

                    int secondPtId            = edge.SecondVertexIndex;
                    int secondTriangleIdRight = edge.FirstTriangleIndex;

                    Vertex secondPtVertex = sMesh.Vertices[secondPtId];
                    //Point3d secondTriCentreRight = sMesh.ComputeTriangleCenter(secondTriangleIdRight);
                    Point3d secondTriCentreRight = sMesh.ComputeCircumscribedCircleCenter(secondTriangleIdRight);

                    Vector3d secondNormalRight = thickness * sMesh.ComputeTriangleNormal(secondTriangleIdRight);

                    Point3d secondTriCentreUpRight   = secondTriCentreRight + secondNormalRight;    // the second control point
                    Point3d secondTriCentreDownRight = secondTriCentreRight - secondNormalRight;    // the seventh control point

                    Point3d secondCPt02Right = (secondPtVertex.Factor - 0.2) * secondPtVertex.Position + (1 - secondPtVertex.Factor + 0.2) * secondTriCentreRight;

                    Point3d secondCPt02UpRight   = secondCPt02Right + secondNormalRight;              // the second control point
                    Point3d secondCPt02DownRight = secondCPt02Right - secondNormalRight;              // the sixth control point

                    Point3d secondCPt03Right = secondPtVertex.Factor * secondPtVertex.Position + (1 - secondPtVertex.Factor) * secondTriCentreRight;

                    Point3d secondCPt03UpRight   = secondCPt03Right + secondNormalRight;     // the third control point
                    Point3d secondCPt03DownRight = secondCPt03Right - secondNormalRight;     // the fifth control point

                    Point3d secondUpPtRight = secondPtVertex.Position;                       // the fourth control point

                    List <Point3d> secondControlPointRight = new List <Point3d>()
                    {
                        secondTriCentreUpRight, secondCPt02UpRight, secondCPt03UpRight, secondUpPtRight,
                        secondCPt03DownRight, secondCPt02DownRight, secondTriCentreDownRight
                    };

                    #endregion
                    NurbsCurve secondNurbCurveRight = NurbsCurve.Create(false, 5, secondControlPointRight);

                    // second triangle
                    #region curve04

                    int secondTriangleIdLeft = edge.SecondTriangleIndex;

                    //Point3d secondTriCentreLeft = sMesh.ComputeTriangleCenter(secondTriangleIdLeft);
                    Point3d secondTriCentreLeft = sMesh.ComputeCircumscribedCircleCenter(secondTriangleIdLeft);

                    Vector3d secondNormalLeft = thickness * sMesh.ComputeTriangleNormal(secondTriangleIdLeft);

                    Point3d secondTriCentreUpLeft   = secondTriCentreLeft + secondNormalLeft;    // the second control point
                    Point3d secondTriCentreDownLeft = secondTriCentreLeft - secondNormalLeft;    // the seventh control point

                    Point3d secondCPt02Left = (secondPtVertex.Factor - 0.2) * secondPtVertex.Position + (1 - secondPtVertex.Factor + 0.2) * secondTriCentreLeft;

                    Point3d secondCPt02UpLeft   = secondCPt02Left + secondNormalLeft;              // the second control point
                    Point3d secondCPt02DownLeft = secondCPt02Left - secondNormalLeft;              // the sixth control point

                    Point3d secondCPt03Left = secondPtVertex.Factor * secondPtVertex.Position + (1 - secondPtVertex.Factor) * secondTriCentreLeft;

                    Point3d secondCPt03UpLeft   = secondCPt03Left + secondNormalLeft;       // the third control point
                    Point3d secondCPt03DownLeft = secondCPt03Left - secondNormalLeft;       // the fifth control point

                    Point3d secondUpPtLeft = secondPtVertex.Position;                       // the fourth control point

                    List <Point3d> secondControlPointLeft = new List <Point3d>()
                    {
                        secondTriCentreUpLeft, secondCPt02UpLeft, secondCPt03UpLeft, secondUpPtLeft,
                        secondCPt03DownLeft, secondCPt02DownLeft, secondTriCentreDownLeft
                    };
                    #endregion
                    NurbsCurve secondNurbCurveLeft = NurbsCurve.Create(false, 5, secondControlPointLeft);

                    curvesRight.Add(secondNurbCurveRight);
                    curvesLeft.Add(secondNurbCurveLeft);

                    /*
                     * Brep[] brep2 = Brep.CreateFromLoft(
                     *  new List<Curve>() { firstNurbCurveRight, firstNurbCurveLeft },
                     *  Point3d.Unset, Point3d.Unset, LoftType.Developable, false);
                     *
                     * if (brep2.Length > 0) breps.Add(brep2[0]);  */
                }
            }
            return(true);
        }