summaryrefslogtreecommitdiffstats
path: root/Project/PowerElement.cpp
blob: 4c4e0725ecf29b9c2055430faf74d5b25f17b91e (plain)
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
/*
 *  Copyright (C) 2017  Thales Lima Oliveira <thales@ufu.br>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "PowerElement.h"
#ifdef USING_WX_3_0_X
#include "DegreesAndRadians.h"
#endif

PowerElement::PowerElement() : Element()
{
    m_busColour.SetRGBA(0.0, 0.3, 1.0, 1.0);
    m_onlineElementColour.SetRGBA(0.2, 0.2, 0.2, 1.0);
    m_offlineElementColour.SetRGBA(0.5, 0.5, 0.5, 1.0);
    m_closedSwitchColour.SetRGBA(0.0, 0.4, 0.0, 1.0);
    m_openedSwitchColour.SetRGBA(1.0, 0.1, 0.1, 1.0);
    m_powerFlowArrowColour.SetRGBA(1.0, 0.51, 0.0, 1.0);
    m_dynamicEventColour.SetRGBA(1.0, 0.51, 0.0, 1.0);
}

PowerElement::~PowerElement() {}
void PowerElement::SetNominalVoltage(std::vector<double> nominalVoltage, std::vector<ElectricalUnit> nominalVoltageUnit)
{
}

wxPoint2DDouble PowerElement::GetSwitchPoint(Element* parent,
                                             wxPoint2DDouble parentPoint,
                                             wxPoint2DDouble secondPoint) const
{
    double swLineSize = 25.0;
    wxPoint2DDouble swPoint = wxPoint2DDouble(parentPoint.m_x, parentPoint.m_y - swLineSize);

    // Rotate the second point (to compare).
    double angle = parent->GetAngle();

    secondPoint =
        wxPoint2DDouble(std::cos(wxDegToRad(-angle)) * (secondPoint.m_x - parentPoint.m_x) -
                            std::sin(wxDegToRad(-angle)) * (secondPoint.m_y - parentPoint.m_y) + parentPoint.m_x,
                        std::sin(wxDegToRad(-angle)) * (secondPoint.m_x - parentPoint.m_x) +
                            std::cos(wxDegToRad(-angle)) * (secondPoint.m_y - parentPoint.m_y) + parentPoint.m_y);

    // Rotate
    if(secondPoint.m_y > parentPoint.m_y) angle -= 180.0;
    return wxPoint2DDouble(std::cos(wxDegToRad(angle)) * (swPoint.m_x - parentPoint.m_x) -
                               std::sin(wxDegToRad(angle)) * (swPoint.m_y - parentPoint.m_y) + parentPoint.m_x,
                           std::sin(wxDegToRad(angle)) * (swPoint.m_x - parentPoint.m_x) +
                               std::cos(wxDegToRad(angle)) * (swPoint.m_y - parentPoint.m_y) + parentPoint.m_y);
}

bool PowerElement::SwitchesContains(wxPoint2DDouble position) const
{
    for(int i = 0; i < (int)m_switchRect.size(); i++) {
        if(m_parentList[i]) {
            if(m_switchRect[i].Contains(position)) return true;
        }
    }
    return false;
}

void PowerElement::UpdateSwitches()
{
    // General method, to one switch only.
    wxPoint2DDouble swCenter = wxPoint2DDouble((m_pointList[0].m_x + m_pointList[1].m_x) / 2.0,
                                               (m_pointList[0].m_y + m_pointList[1].m_y) / 2.0);
    m_switchRect[0] = wxRect2DDouble(swCenter.m_x - m_switchSize / 2.0, swCenter.m_y - m_switchSize / 2.0, m_switchSize,
                                     m_switchSize);
}

void PowerElement::DrawSwitches() const
{
    int i = 0;
    for(auto it = m_parentList.begin(); it != m_parentList.end(); it++) {
        Element* parent = *it;
        if(parent) {
            if(m_online) {
                glColor4dv(m_closedSwitchColour.GetRGBA());
            } else {
                glColor4dv(m_openedSwitchColour.GetRGBA());
            }

            glPushMatrix();
            glTranslated(m_switchRect[i].GetPosition().m_x + m_switchSize / 2.0,
                         m_switchRect[i].GetPosition().m_y + m_switchSize / 2.0, 0.0);
            glRotated(parent->GetAngle(), 0.0, 0.0, 1.0);
            glTranslated(-m_switchRect[i].GetPosition().m_x - m_switchSize / 2.0,
                         -m_switchRect[i].GetPosition().m_y - m_switchSize / 2.0, 0.0);

            DrawRectangle(m_switchRect[i].GetPosition() + wxPoint2DDouble(m_switchSize / 2.0, m_switchSize / 2.0),
                          m_switchSize, m_switchSize);

            glPopMatrix();
        }
        i++;
    }
}

void PowerElement::CalculatePowerFlowPts(std::vector<wxPoint2DDouble> edges)
{
    double arrowRate = 100.0;  // One arrow to each "arrowRate" distance in pixels.

    if(edges.size() < 2) return;

    // Clear all power flow points
    for(int i = 0; i < (int)m_powerFlowArrow.size(); i++) m_powerFlowArrow[i].clear();
    m_powerFlowArrow.clear();

    for(int i = 1; i < (int)edges.size(); i++) {
        wxPoint2DDouble pt1 = edges[i - 1];
        wxPoint2DDouble pt2 = edges[i];

        double angle = std::atan2(pt2.m_y - pt1.m_y, pt2.m_x - pt1.m_x);

        wxPoint2DDouble rotPt2(
            std::cos(-angle) * (pt2.m_x - pt1.m_x) - std::sin(-angle) * (pt2.m_y - pt1.m_y) + pt1.m_x,
            std::sin(-angle) * (pt2.m_x - pt1.m_x) + std::cos(-angle) * (pt2.m_y - pt1.m_y) + pt1.m_y);

        int numArrows = std::abs(pt1.m_x - rotPt2.m_x) / arrowRate;
        if(numArrows == 0) numArrows = 1;

        for(int i = 0; i < numArrows; i++) {
            wxPoint2DDouble arrowCenter(pt1.m_x + ((rotPt2.m_x - pt1.m_x) / double(numArrows + 1)) * double(i + 1),
                                        pt1.m_y + ((rotPt2.m_y - pt1.m_y) / double(numArrows + 1)) * double(i + 1));

            std::vector<wxPoint2DDouble> triPts;
            triPts.push_back(arrowCenter + wxPoint2DDouble(5.0, 0.0));
            triPts.push_back(arrowCenter + wxPoint2DDouble(-5.0, 5.0));
            triPts.push_back(arrowCenter + wxPoint2DDouble(-5.0, -5.0));

            // Rotate back.
            for(int i = 0; i < 3; i++) {
                triPts[i] = wxPoint2DDouble(
                    std::cos(angle) * (triPts[i].m_x - pt1.m_x) - std::sin(angle) * (triPts[i].m_y - pt1.m_y) + pt1.m_x,
                    std::sin(angle) * (triPts[i].m_x - pt1.m_x) + std::cos(angle) * (triPts[i].m_y - pt1.m_y) +
                        pt1.m_y);
            }
            m_powerFlowArrow.push_back(triPts);
        }
    }
}

void PowerElement::DrawPowerFlowPts() const
{
    if(m_online) {
        glColor4dv(m_powerFlowArrowColour.GetRGBA());
        for(int i = 0; i < (int)m_powerFlowArrow.size(); i++) {
            DrawTriangle(m_powerFlowArrow[i]);
        }
    }
}

double PowerElement::GetValueFromUnit(double value, ElectricalUnit valueUnit)
{
    switch(valueUnit) {
        case UNIT_kV:
        case UNIT_kA:
        case UNIT_kW:
        case UNIT_kVA:
        case UNIT_kVAr: {
            return value * 1e3;
        } break;
        case UNIT_MW:
        case UNIT_MVA:
        case UNIT_MVAr: {
            return value * 1e6;
        }
        default:
            break;
    }
    return value;
}