Esempio n. 1
0
        public Vector3Class AttachToModel(STLModel3D stlModel, Vector3Class centerPoint, float sphereRadius, float topHeight)
        {
            var totalModelIntersectionPointCorrectionVector = new Vector3Class();
            var trianglesWithinRange = stlModel.Triangles.GetTrianglesWithinSphereBoundry(centerPoint, sphereRadius * 1.5f);

            var bottomPoints         = this.TopPoints;
            var attachedBottomPoints = new Dictionary <Vector3Class, Vector3Class>();

            for (var bottomPointIndex = 0; bottomPointIndex < bottomPoints.Count; bottomPointIndex++)
            {
                var intersectionPoints = IntersectionProvider.IntersectTriangles(bottomPoints[bottomPointIndex] + new Vector3Class(0, 0, 0.1f), this.Normal, trianglesWithinRange, IntersectionProvider.typeDirection.OneWay);
                //if (intersectionPoints.Count > 0)
                //{
                var nearestPoint    = new Vector3Class();
                var nearestDistance = float.MaxValue;
                foreach (var intersectionPoint in intersectionPoints)
                {
                    var currentDistance = (intersectionPoint - bottomPoints[bottomPointIndex]).Length;
                    if (currentDistance < nearestDistance && currentDistance < topHeight / 2f)
                    {
                        nearestDistance = currentDistance;
                        nearestPoint    = intersectionPoint;
                    }
                }

                if (nearestPoint != new Vector3Class())
                {
                    attachedBottomPoints.Add(bottomPoints[bottomPointIndex], nearestPoint);
                }
                else
                {
                    //no intersection point. Try to find point using x amount of points on line between bottompoint and model intersection point
                    var intersectionPointToBottomPointVector = -(bottomPoints[bottomPointIndex] - centerPoint);
                    var intersectionPointFound = false;
                    for (var vectorIndex = 0.9f; vectorIndex >= 0f; vectorIndex -= 0.1f)
                    {
                        var alternativePoint = centerPoint - (intersectionPointToBottomPointVector * vectorIndex);
                        intersectionPoints = IntersectionProvider.IntersectTriangles(alternativePoint, this.Normal, trianglesWithinRange, IntersectionProvider.typeDirection.OneWay);

                        if (intersectionPoints.Count > 0)
                        {
                            foreach (var intersectionPoint in intersectionPoints.Where(s => s != new Vector3Class()))
                            {
                                if ((alternativePoint - intersectionPoint).Length < topHeight / 2f)
                                {
                                    attachedBottomPoints.Add(bottomPoints[bottomPointIndex], intersectionPoint);
                                    intersectionPointFound = true;
                                    break;
                                }
                            }
                        }

                        if (intersectionPointFound)
                        {
                            break;
                        }
                    }

                    //if (!intersectionPointFound)
                    //{
                    //var alternativePoint = centerPoint;// - (intersectionPointToBottomPointVector * 0.0001f);
                    //    intersectionPoints = IntersectionProvider.IntersectTriangles(alternativePoint, this.Normal, trianglesWithinRange, IntersectionProvider.typeDirection.OneWay);

                    //    if (intersectionPoints.Count > 0)
                    //    {
                    //        foreach (var intersectionPoint in intersectionPoints.Where(s => s != new Vector3Class()))
                    //        {
                    //            attachedBottomPoints.Add(bottomPoints[bottomPointIndex], intersectionPoint);
                    //            intersectionPointFound = true;
                    //            break;
                    //        }
                    //    }
                    //}

                    if (!intersectionPointFound)
                    {
                        attachedBottomPoints.Add(bottomPoints[bottomPointIndex], centerPoint);
                    }
                }
                //}
                //else
                //{
                //    //no intersectionpoint found
                //}
            }

            foreach (var triangle in this[0])
            {
                for (var vectorIndex = 0; vectorIndex < 3; vectorIndex++)
                {
                    if (attachedBottomPoints.ContainsKey(triangle.Vectors[vectorIndex].Position))
                    {
                        totalModelIntersectionPointCorrectionVector += (triangle.Vectors[vectorIndex].Position - attachedBottomPoints[triangle.Vectors[vectorIndex].Position]) / 16f;
                        triangle.Vectors[vectorIndex].Position       = attachedBottomPoints[triangle.Vectors[vectorIndex].Position];
                    }
                }
            }

            return(totalModelIntersectionPointCorrectionVector);
        }