예제 #1
0
        public void UpdateOutput()
        {
            var groupOfObjects = GetOrCreateGroupOfObjects();

            if (groupOfObjects != null)
            {
                var wasEnabled = groupOfObjects.Enabled;
                if (wasEnabled)
                {
                    groupOfObjects.Enabled = false;
                }

                Component_GroupOfObjects.Object[] objects;

                var surface = Surface.Value;
                if (surface != null)
                {
                    var element = groupOfObjects.GetComponent <Component_GroupOfObjectsElement_Surface>();
                    if (element == null)
                    {
                        element      = groupOfObjects.CreateComponent <Component_GroupOfObjectsElement_Surface>();
                        element.Name = "Surface";
                        //!!!!?
                        element.AutoAlign = false;
                        element.Surface   = ReferenceUtility.MakeThisReference(element, this, "Surface");
                    }

                    objects = UpdateOutputDefault(surface, groupOfObjects);
                }
                else
                {
                    objects = new Component_GroupOfObjects.Object[0];
                }

                OnUpdateOutput(ref objects);
                UpdateOutputEvent?.Invoke(this, ref objects);

                groupOfObjects.ObjectsSet(objects);

                updatedForPointPositions = GetPointPositions();

                if (wasEnabled)
                {
                    groupOfObjects.Enabled = true;
                }
            }
        }
예제 #2
0
        Component_GroupOfObjects.Object[] UpdateOutputDefault(Component_Surface surface, Component_GroupOfObjects groupOfObjects)
        {
            var pointPositions  = GetPointPositions();
            var pointPositions2 = pointPositions.Select(a => a.ToVector2()).ToArray();
            var pointBounds     = GetPointPositionsBounds();

            var lines = new List <Line2>();

            for (int n = 0; n < pointPositions2.Length; n++)
            {
                lines.Add(new Line2(pointPositions2[n], pointPositions2[(n + 1) % pointPositions2.Length]));
            }

            var random = new Random(RandomSeed.Value);

            double GetFadeFactor(Vector2 point)
            {
                var minLength = double.MaxValue;

                foreach (var line in lines)
                {
                    var d = Math.Min((line.Start - point).Length(), (line.End - point).Length());

                    var projected = MathAlgorithms.ProjectPointToLine(line.Start, line.End, point);

                    Rectangle b = new Rectangle(line.Start);
                    b.Add(line.End);
                    if (b.Contains(projected))
                    {
                        var d2 = (projected - point).Length();
                        if (d2 < d)
                        {
                            d = d2;
                        }
                    }

                    if (d < minLength)
                    {
                        minLength = d;
                    }
                }

                if (minLength >= FadeLength)
                {
                    return(1);
                }
                return(minLength / FadeLength);
            }

            if (pointPositions.Length >= 3)
            {
                //calculate object count
                int count;
                {
                    double polygonArea;
                    {
                        var points = new List <Vector2>(pointPositions2);
                        points.Add(points[0]);

                        polygonArea = Math.Abs(points.Take(points.Count - 1)
                                               .Select((p, i) => (points[i + 1].X - p.X) * (points[i + 1].Y + p.Y))
                                               .Sum() / 2);
                    }

                    //!!!!среднее от всех групп
                    double minDistanceBetweenObjects;
                    {
                        var groups = surface.GetComponents <Component_SurfaceGroupOfElements>();
                        if (groups.Length != 0)
                        {
                            minDistanceBetweenObjects = 0;
                            foreach (var group in groups)
                            {
                                minDistanceBetweenObjects += group.MinDistanceBetweenObjects;
                            }
                            minDistanceBetweenObjects /= groups.Length;
                        }
                        else
                        {
                            minDistanceBetweenObjects = 1;
                        }
                    }

                    double radius     = minDistanceBetweenObjects / 2;
                    double objectArea = Math.PI * radius * radius;
                    if (objectArea < 0.1)
                    {
                        objectArea = 0.1;
                    }

                    double maxCount = polygonArea / objectArea;

                    count = (int)(Strength * (double)maxCount);
                    count = Math.Max(count, 0);
                }

                var data = new List <Component_GroupOfObjects.Object>(count);

                var scene = groupOfObjects.FindParent <Component_Scene>();
                if (scene != null)
                {
                    var destinationCachedBaseObjects = groupOfObjects.GetBaseObjects();

                    var pointContainerFindFreePlace = new PointContainer3D(pointBounds, 100);

                    for (int n = 0; n < count; n++)
                    {
                        surface.GetRandomVariation(new Component_Surface.GetRandomVariationOptions(), random, out var groupIndex, out var elementIndex, out var positionZ, out var rotation, out var scale);
                        var surfaceGroup = surface.GetGroup(groupIndex);

                        Vector3?position = null;

                        int counter = 0;
                        while (counter < 20)
                        {
                            Vector2 position2 = Vector2.Zero;
                            position2.X = MathEx.Lerp(pointBounds.Minimum.X, pointBounds.Maximum.X, random.NextFloat());
                            position2.Y = MathEx.Lerp(pointBounds.Minimum.Y, pointBounds.Maximum.Y, random.NextFloat());

                            if (MathAlgorithms.IsPointInPolygon(pointPositions2, position2))
                            {
                                double factor = 1;
                                if (FadeLength > 0)
                                {
                                    factor = GetFadeFactor(position2);
                                }

                                if (factor >= 1 || random.NextDouble() <= factor)
                                {
                                    var result = Component_Scene_Utility.CalculateObjectPositionZ(scene, groupOfObjects, pointBounds.GetCenter().Z, position2, destinationCachedBaseObjects);
                                    if (result.found || destinationCachedBaseObjects.Count == 0)
                                    {
                                        var p = new Vector3(position2, destinationCachedBaseObjects.Count != 0 ? result.positionZ : pointBounds.GetCenter().Z);

                                        //check by MinDistanceBetweenObjects
                                        if (surfaceGroup == null || !pointContainerFindFreePlace.Contains(new Sphere(p, surfaceGroup.MinDistanceBetweenObjects)))
                                        {
                                            //found place to create
                                            position = p;
                                            break;
                                        }
                                    }
                                }
                            }

                            counter++;
                        }

                        if (position != null)
                        {
                            var obj = new Component_GroupOfObjects.Object();
                            obj.Element          = 0;                   // (ushort)element.Index.Value;
                            obj.VariationGroup   = groupIndex;
                            obj.VariationElement = elementIndex;
                            obj.Flags            = Component_GroupOfObjects.Object.FlagsEnum.Enabled | Component_GroupOfObjects.Object.FlagsEnum.Visible;
                            obj.Position         = position.Value + new Vector3(0, 0, positionZ);
                            obj.Rotation         = rotation;
                            obj.Scale            = scale;
                            obj.Color            = ColorValue.One;
                            data.Add(obj);

                            //add to point container
                            pointContainerFindFreePlace.Add(ref obj.Position);
                        }
                    }
                }

                return(data.ToArray());
            }
            else
            {
                return(new Component_GroupOfObjects.Object[0]);
            }
        }