File size: 7,187 Bytes
41791c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using PhysX5ForUnity;
using System;
using System.Collections.Generic;
using UnityEngine;

[DisallowMultipleComponent]
[AddComponentMenu("Learning/Suction Actor with Reward")]
public class SuctionActorWithReward : MonoBehaviour
{
    #region Properties

    public LinkedList<int> ActiveFluidIndices
    {
        get { return m_activeFluidIndices; }
        set { m_activeFluidIndices = value; }
    }

    public float Radius
    {
        get { return m_radius; }
        set { m_radius = value; }
    }

    public float ConeAngle
    {
        get { return m_coneAngle; }
        set { m_coneAngle = value; }
    }

    public float RemovalRadius
    {
        get { return m_removalRadius; }
        set { m_removalRadius = value; }
    }

    public float ForceScale
    {
        get { return m_forceScale; }
        set { m_forceScale = value; }
    }

    public bool InEpisode
    {
        get { return m_inEpisode; }
        set { m_inEpisode = value; }
    }

    #region Public

    public void InitializeActiveIndices(List<int> indices)
    {
        m_activeFluidIndices = new LinkedList<int>(indices);
    }
    #endregion

    #endregion

    #region Messages

    private void Start()
    {
        localCoordinatePosition = transform.position;
    }

    private void FixedUpdate()
    {
        UpdateParticles();

    }

    #endregion

    #region Private

    void UpdateParticles()
    {
        if (m_inEpisode)
        {
            Vector3 forceOrigin = transform.position;
            Vector3 forceDirecion = -transform.up; // the - Y axis of the gameObject
            //int[] activeIndices = m_particleDataContainer.ActiveIndices;
            //int activeCount = m_particleDataContainer.ActiveCount;
            int removalIndexCount = 0;
            Vector4 removalTeleportPosition = new Vector4(5.0f + localCoordinatePosition.x, -5.0f + localCoordinatePosition.y, -5.0f + localCoordinatePosition.z, 0.0f);
            float dT = Time.fixedDeltaTime;

            float currentHorizontalDistanceToFluid = float.MaxValue;


            for (LinkedListNode<int> it = m_activeFluidIndices.First; it != null;)
            {
                LinkedListNode<int> next = it.Next;
            // }
            // for (int i = 0; i < m_pxParticleActor.ParticleData.NumParticles; ++i)
            // {
                //int idx = activeIndices[i];
                int idx = it.Value;
                Vector4 position = m_pxParticleActor.ParticleData.PositionInvMass[idx];
                Vector3 p = position;
                Vector3 delta = p - forceOrigin;

                // in case particle drops below the map
                if (position.y < -10.0f)
                {
                    m_pxParticleActor.ParticleData.SetParticle(idx, removalTeleportPosition, true);
                    m_activeFluidIndices.Remove(it); // as a side effect it.Next == null
                    it = next;
                    continue;
                }

                float horizontalDistance = Mathf.Sqrt(HorizontalDistanceSquared(p, transform.position));
                if (horizontalDistance < currentHorizontalDistanceToFluid) currentHorizontalDistanceToFluid = horizontalDistance;

                float t = Vector3.Dot(delta, forceDirecion);
                float distanceToOrigin = delta.magnitude;
                if (distanceToOrigin > m_radius)
                {
                    // Early stopping
                    it = next;
                    continue;
                }

                Vector3 c = Vector3.Cross(delta, forceDirecion);
                float norm_c = Mathf.Sqrt(Mathf.Pow(c.x, 2) + Mathf.Pow(c.y, 2) + Mathf.Pow(c.z, 2));
                float a = Mathf.Atan2(norm_c, t) * 180 / 3.1415f;

                if (distanceToOrigin < m_removalRadius) // using distanceToOrigin is less realistic
                {
                    ++removalIndexCount;
                    m_pxParticleActor.ParticleData.SetParticle(idx, removalTeleportPosition, true);
                    m_activeFluidIndices.Remove(it);
                }


                if (a < m_coneAngle)
                {
                    Vector3 addedVelocity = -m_forceScale * dT * position.w * (delta.normalized / (distanceToOrigin + 0.0001f) - delta.normalized / (m_radius + 0.0001f));
                    Vector3 velocity = m_pxParticleActor.ParticleData.Velocity[idx];
                    m_pxParticleActor.ParticleData.SetVelocity(idx, velocity + addedVelocity, true);
                }
                it = next;
            }
            m_agent.AddSuctionReward(removalIndexCount);
            m_pxParticleActor.ParticleData.SyncParticlesSet(false);

            // Some checks to prevent negative reward when finishing suctioning an area
            // I don't know why sometimes large values are preset. Simply check the magnitude.
            if (!m_isSuctionLastStep && removalIndexCount == 0 && Mathf.Abs(currentHorizontalDistanceToFluid) < 1e6 && Mathf.Abs(m_lastHorizontalDistanceToFluid) < 1e6)
            {
                m_agent.AddSuctionApproachingReward(currentHorizontalDistanceToFluid, m_lastHorizontalDistanceToFluid);
            }
            m_lastHorizontalDistanceToFluid = currentHorizontalDistanceToFluid;
            m_isSuctionLastStep = removalIndexCount > 0;
            if (m_activeFluidIndices.Count <= 3)
            {
                m_agent.AddCompletionReward();
                m_agent.EndEpisode();
            }
        }
        else
        {
            // if not in episode, always update the last distance
            m_lastHorizontalDistanceToFluid = GetToBloodHorizontalDistance();
        }
    }

    private static float HorizontalDistanceSquared(Vector3 a, Vector3 b)
    {
        Vector3 diff = a - b;
        return diff.x * diff.x + diff.z * diff.z;
    }

    private static float HorizontalDistanceSquared(Vector4 a, Vector3 b)
    {
        float diffX = a.x - b.x;
        float diffZ = a.z - b.z;
        return diffX * diffX + diffZ * diffZ;
    }

    private float GetToBloodHorizontalDistance()
    {
        float dist = float.MaxValue;
        Vector3 liquidSourcePosition = transform.position;
        for (LinkedListNode<int> it = m_activeFluidIndices.First; it != null;)
        {
            LinkedListNode<int> next = it.Next;
            int idx = it.Value;
            Vector4 p = m_pxParticleActor.ParticleData.PositionInvMass[idx];
            float d = Mathf.Sqrt(HorizontalDistanceSquared(p, liquidSourcePosition));
            if (d < dist) dist = d;
            it = next;
        }
        return dist;
    }

    [SerializeField]
    PhysxParticleActor m_pxParticleActor;
    [SerializeField]
    float m_radius;
    [SerializeField]
    float m_removalRadius = 0.5f;
    [SerializeField]
    float m_coneAngle = 30.0f;
    [SerializeField]
    float m_forceScale = 1.0f;
    [SerializeField]
    SuctionAgent m_agent;

    [NonSerialized]
    bool m_inEpisode = true;
    [NonSerialized]
    Vector3 localCoordinatePosition;

    private LinkedList<int> m_activeFluidIndices;
    private float m_lastHorizontalDistanceToFluid;
    private bool m_isSuctionLastStep = false;
    #endregion
}