Mejora con la implementacion de la funcion que calcula el porcentaje de superfecie compartida por una botella y un transporte

This commit is contained in:
Miguel 2024-05-15 09:59:48 +02:00
parent 494f960628
commit 794a5898c5
7 changed files with 400 additions and 7 deletions

View File

@ -29,6 +29,9 @@ namespace CtrEditor.ObjetosSim
return new ucBoton();
if (tipoObjeto == typeof(osTanque))
return new ucTanque();
if (tipoObjeto == typeof(osSensTemperatura))
return new ucSensTemperatura();
// Puedes añadir más condiciones para otros tipos
@ -53,6 +56,8 @@ namespace CtrEditor.ObjetosSim
return new osBoton();
if (tipoObjeto == typeof(osTanque))
return new osTanque();
if (tipoObjeto == typeof(osSensTemperatura))
return new osSensTemperatura();
// Puedes añadir más condiciones para otros tipos

View File

@ -0,0 +1,33 @@
<UserControl x:Class="CtrEditor.ObjetosSim.ucSensTemperatura"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:CtrEditor.ObjetosSim"
mc:Ignorable="d"
xmlns:convert="clr-namespace:CtrEditor.Convertidores">
<UserControl.Resources>
<convert:MeterToPixelConverter x:Key="MeterToPixelConverter"/>
</UserControl.Resources>
<Grid>
<Border x:Name="BackgroundRectangle"
BorderBrush="Black"
BorderThickness="2"
Height="{Binding Alto, Converter={StaticResource MeterToPixelConverter}, ConverterParameter=1.5}"
VerticalAlignment="Top"
Background="Gray"
HorizontalAlignment="Center"
Width="{Binding Ancho, Converter={StaticResource MeterToPixelConverter}}">
<Grid>
<Slider
HorizontalAlignment="Center"
VerticalAlignment="Center"
Height="{Binding Alto, Converter={StaticResource MeterToPixelConverter}}"
Orientation="Vertical"
Value="{Binding Value}"
Maximum="100"/>
</Grid>
</Border>
</Grid>
</UserControl>

View File

@ -0,0 +1,222 @@
using CtrEditor.Convertidores;
using CtrEditor.Siemens;
using Siemens.Simatic.Simulation.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace CtrEditor.ObjetosSim
{
/// <summary>
/// Interaction logic for ucSensTemperatura.xaml
/// </summary>
public class osSensTemperatura : osBase
{
// Otros datos y métodos relevantes para la simulación
private string _nombre = "Temperatura";
private float _ancho;
private float _alto;
private float _left;
private float _top;
private float _angulo;
private string _tag;
private float _value;
private float _max_OUT_Scaled;
private float _min_OUT_Scaled;
public string Tag
{
get => _tag;
set
{
if (_tag != value)
{
_tag = value;
OnPropertyChanged(nameof(Tag));
}
}
}
public float Min_OUT_Scaled
{
get => _min_OUT_Scaled;
set
{
_min_OUT_Scaled = value;
OnPropertyChanged(nameof(Min_OUT_Scaled));
}
}
public float Max_OUT_Scaled
{
get => _max_OUT_Scaled;
set
{
_max_OUT_Scaled = value;
OnPropertyChanged(nameof(Max_OUT_Scaled));
}
}
public float Value
{
get => _value;
set
{
_value = value;
OnPropertyChanged(nameof(Value));
}
}
public override float Left
{
get => _left;
set
{
_left = value;
CanvasSetLeftinMeter(value);
OnPropertyChanged(nameof(Left));
}
}
public override float Top
{
get => _top;
set
{
_top = value;
CanvasSetTopinMeter(value);
OnPropertyChanged(nameof(Top));
}
}
public float Ancho
{
get => _ancho;
set
{
_ancho = value;
OnPropertyChanged(nameof(Ancho));
}
}
public float Alto
{
get => _alto;
set
{
_alto = value;
OnPropertyChanged(nameof(Alto));
}
}
public float Angulo
{
get => _angulo;
set
{
_angulo = value;
OnPropertyChanged(nameof(Angulo));
}
}
public override string Nombre
{
get => _nombre;
set
{
if (_nombre != value)
{
_nombre = value;
OnPropertyChanged(nameof(Nombre));
}
}
}
public osSensTemperatura()
{
Ancho = 0.4f;
Alto = 1f;
Max_OUT_Scaled = 27648;
Min_OUT_Scaled = 0;
}
public override void UpdateGeometryStart()
{
// Se llama antes de la simulacion
}
public override void UpdateGeometryStep()
{
}
public override void UpdatePLC(PLCModel plc, int elapsedMilliseconds)
{
if (Tag.Length > 0)
{
SDataValue s = new SDataValue();
s.UInt16 = (ushort)((Value / 100.0 * Max_OUT_Scaled) + Min_OUT_Scaled);
plc.EscribirTag(Tag, s);
}
}
public override void UpdateControl()
{
}
public override void ucLoaded()
{
// El UserControl ya se ha cargado y podemos obtener las coordenadas para
// crear el objeto de simulacion
}
}
public partial class ucSensTemperatura : UserControl, IDataContainer
{
public osBase? Datos { get; set; }
public ucSensTemperatura()
{
InitializeComponent();
this.Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
Datos?.ucLoaded();
}
public void Resize(float width, float height)
{
if (Datos is osSensTemperatura datos)
{
datos.Ancho = PixelToMeter.Instance.calc.PixelsToMeters(width);
datos.Alto = PixelToMeter.Instance.calc.PixelsToMeters(width);
}
}
public void Move(float LeftPixels, float TopPixels)
{
if (Datos != null)
{
Datos.Left = PixelToMeter.Instance.calc.PixelsToMeters(LeftPixels);
Datos.Top = PixelToMeter.Instance.calc.PixelsToMeters(TopPixels);
}
}
public void Rotate(float Angle)
{
if (Datos != null)
if (Datos is osSensTemperatura datos)
datos.Angulo = Angle;
}
public void Highlight(bool State) { }
public int ZIndex()
{
return 10;
}
}
}

View File

@ -18,7 +18,15 @@
Source="/tank.png"
Width="{Binding Alto, Converter={StaticResource MeterToPixelConverter}}"
Height="{Binding Ancho, Converter={StaticResource MeterToPixelConverter}}"
Stretch="Uniform"/>
Stretch="Uniform">
</Image>
<Slider
HorizontalAlignment="Left"
VerticalAlignment="Center"
Height="{Binding Alto, Converter={StaticResource MeterToPixelConverter}}"
Orientation="Vertical"
Value="{Binding Level}"
Maximum="100"/>
<Rectangle x:Name="WaterLevel"
Fill="Blue"
@ -34,6 +42,7 @@
</MultiBinding>
</Rectangle.Height>
</Rectangle>
<Label Content="{Binding Level}" HorizontalAlignment="Center" Margin="10,10,0,0" VerticalAlignment="Top"/>
</Grid>
</UserControl>

View File

@ -1,5 +1,6 @@
using CtrEditor.Convertidores;
using CtrEditor.Siemens;
using Newtonsoft.Json.Linq;
using OpenCvSharp.Flann;
using Siemens.Simatic.Simulation.Runtime;
using System;
@ -34,7 +35,8 @@ namespace CtrEditor.ObjetosSim
private float _angulo;
private float _level;
private string _tag;
private float _max_OUT_Scaled;
private float _min_OUT_Scaled;
public string Tag
{
@ -48,6 +50,27 @@ namespace CtrEditor.ObjetosSim
}
}
}
public float Min_OUT_Scaled
{
get => _min_OUT_Scaled;
set
{
_min_OUT_Scaled = value;
OnPropertyChanged(nameof(Min_OUT_Scaled));
}
}
public float Max_OUT_Scaled
{
get => _max_OUT_Scaled;
set
{
_max_OUT_Scaled = value;
OnPropertyChanged(nameof(Max_OUT_Scaled));
}
}
public float Level
{
get => _level;
@ -125,6 +148,8 @@ namespace CtrEditor.ObjetosSim
{
Ancho = 0.30f;
Alto = 0.30f;
Max_OUT_Scaled = 27648;
Min_OUT_Scaled = 0;
}
public override void UpdateGeometryStart()
@ -140,7 +165,7 @@ namespace CtrEditor.ObjetosSim
if (Tag.Length > 0)
{
SDataValue s = new SDataValue();
s.UInt16 = (ushort)(Level / 100.0 * 26600);
s.UInt16 = (ushort)((Level / 100.0 * Max_OUT_Scaled) + Min_OUT_Scaled);
plc.EscribirTag(Tag, s);
}
}

View File

@ -12,6 +12,7 @@ using CtrEditor.Convertidores;
using FarseerPhysics.Common;
using System.Windows;
using System.Diagnostics;
using System.Windows.Documents;
namespace CtrEditor.Simulacion
{
@ -182,7 +183,32 @@ namespace CtrEditor.Simulacion
if (fixtureB.Body.UserData is simRectangle)
{
simRectangle conveyor = fixtureB.Body.UserData as simRectangle;
ApplyConveyorEffect(conveyor, fixtureA);
CircleShape circleShape = fixtureA.Shape as CircleShape;
PolygonShape polygonShape = fixtureB.Shape as PolygonShape;
// Obtener centro y radio del círculo
Vector2 centroCirculo = fixtureA.Body.Position;
float radio = circleShape.Radius;
// Obtener los vértices del polígono (rectángulo)
Vector2[] vertices = new Vector2[polygonShape.Vertices.Count];
float cos = (float)Math.Cos(fixtureB.Body.Rotation);
float sin = (float)Math.Sin(fixtureB.Body.Rotation);
for (int i = 0; i < polygonShape.Vertices.Count; i++)
{
Vector2 vertex = polygonShape.Vertices[i];
float rotatedX = vertex.X * cos - vertex.Y * sin + fixtureB.Body.Position.X;
float rotatedY = vertex.X * sin + vertex.Y * cos + fixtureB.Body.Position.Y;
vertices[i] = new Vector2(rotatedX, rotatedY);
}
// Calcular el porcentaje de la superficie compartida
float porcentajeCompartido = InterseccionCirculoRectangulo.CalcularSuperficieCompartida(vertices, centroCirculo, radio);
// Aplicar el efecto del transportador usando el porcentaje calculado
ApplyConveyorEffect(conveyor, fixtureA, porcentajeCompartido);
return true; // No aplicar respuestas físicas
}
return true; // No aplicar respuestas físicas
@ -193,11 +219,11 @@ namespace CtrEditor.Simulacion
// Aquí puedes restablecer cualquier estado si es necesario al separarse de un simRectangle
}
private void ApplyConveyorEffect(simRectangle conveyor, Fixture circleFixture)
private void ApplyConveyorEffect(simRectangle conveyor, Fixture circleFixture, float porcentajeCompartido)
{
float speedMetersPerSecond = conveyor.Speed / 60.0f;
Vector2 desiredVelocity = new Vector2((float)Math.Cos(conveyor.Body.Rotation), (float)Math.Sin(conveyor.Body.Rotation)) * speedMetersPerSecond;
circleFixture.Body.LinearVelocity = desiredVelocity;
circleFixture.Body.LinearVelocity += desiredVelocity * porcentajeCompartido;
}
}

View File

@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FarseerPhysics.Collision;
using Microsoft.VisualBasic.Devices;
using Microsoft.Xna.Framework;
namespace CtrEditor.Simulacion
{
internal class InterseccionCirculoRectangulo
{
// Definición de la función CalcularSuperficieCompartida
public static float CalcularSuperficieCompartida(Vector2[] vertices, Vector2 center, float r)
{
float totalCircleArea = (float)Math.PI * r * r;
// Distancia a líneas ajustado
float[] distances = new float[4];
for (int i = 0; i < 4; i++)
{
distances[i] = DistanceFromLine(center, vertices[i], vertices[(i + 1) % 4]);
}
float minDistance = float.MaxValue;
foreach (var dist in distances)
{
if (Math.Abs(dist) < Math.Abs(minDistance))
minDistance = dist;
}
float d = Math.Abs(minDistance);
float sharedArea = 0;
if (Array.TrueForAll(distances, dist => Math.Abs(dist) > r))
{
sharedArea = totalCircleArea;
}
else if (d < r)
{
float cosTheta = Math.Min(1, d / r);
float sinTheta = (float)Math.Sqrt(Math.Max(0, r * r - d * d));
if (minDistance < 0) // El centro está dentro del rectángulo
{
float areaOutside = r * r * (float)Math.Acos(cosTheta) - d * sinTheta;
sharedArea = totalCircleArea - areaOutside;
}
else // El centro está fuera del rectángulo
{
sharedArea = r * r * (float)Math.Acos(cosTheta) - d * sinTheta;
}
}
else
{
sharedArea = 0;
}
return sharedArea / totalCircleArea;
}
public static float DistanceFromLine(Vector2 point, Vector2 start, Vector2 end)
{
float A = end.Y - start.Y;
float B = start.X - end.X;
float C = end.X * start.Y - start.X * end.Y;
float distance = (A * point.X + B * point.Y + C) / (float)Math.Sqrt(A * A + B * B);
return distance;
}
}
}