Power System Platform  2026w11a-beta
Loading...
Searching...
No Matches
MathExpression Class Reference

A generic math expression block that can perform math and conditional operations with the inputs. More...

#include <MathExpression.h>

Inheritance diagram for MathExpression:
Collaboration diagram for MathExpression:

Public Member Functions

 MathExpression (int id)
 
virtual void DrawDC (wxPoint2DDouble translation, double scale, wxGraphicsContext *gc) const
 Draw the element using GDI+.
 
virtual bool Contains (wxPoint2DDouble position) const
 Checks if the element contains a position.
 
virtual bool Intersects (wxRect2DDouble rect) const
 Check if the element's rect intersects other rect.
 
virtual bool ShowForm (wxWindow *parent, Element *element, wxWindow *workspace=nullptr)
 Show element data form.
 
virtual void Rotate (bool clockwise=true)
 Rotate the element.
 
virtual bool Solve (double *input, double timeStep)
 
virtual bool Initialize ()
 
virtual wxString GetMathExpression ()
 
virtual void SetMathExpression (wxString mathExpression)
 
virtual std::vector< wxString > GetVariables ()
 
virtual void SetVariables (std::vector< wxString > variablesVector)
 
virtual MathExprParser GetParser ()
 
virtual void UpdatePoints ()
 
void AddInNode ()
 
void RemoveInNode ()
 
virtual bool UpdateText ()
 Update the OpenGL text in the element (if present).
 
virtual rapidxml::xml_node * SaveElement (rapidxml::xml_document<> &doc, rapidxml::xml_node<> *elementListNode)
 
virtual bool OpenElement (rapidxml::xml_node<> *elementNode)
 
virtual ElementGetCopy ()
 Get a the element copy.
 
- Public Member Functions inherited from ControlElement
 ControlElement (int id)
 
virtual void StartMove (wxPoint2DDouble position)
 Update the element attributes related to the movement.
 
virtual void Move (wxPoint2DDouble position)
 Move the element other position.
 
void SetNodeList (std::vector< Node * > nodeList)
 
std::vector< Node * > GetNodeList () const
 
virtual void DrawDCNodes (wxGraphicsContext *gc) const
 
virtual void ReplaceNode (Node *oldNode, Node *newNode)
 
virtual bool IsSolved () const
 
virtual void SetSolved (bool solved=true)
 
virtual double GetOutput () const
 
virtual void SetOutput (double output)
 
- Public Member Functions inherited from Element
 Element ()
 Constructor.
 
virtual ~Element ()
 Destructor.
 
void SetDragging (bool dragging=true)
 Set if the element are being dragged.
 
void SetHeight (double height)
 Set element height.
 
void SetPosition (const wxPoint2DDouble position)
 Set the element position and update the rectangle.
 
void SetSelected (bool selected=true)
 Set element selection.
 
void SetWidth (double width)
 Set element width.
 
void SetAngle (double angle)
 Set element angle.
 
void ShowPickbox (bool showPickbox=true)
 Set if the pickbox is shown.
 
void SetBorderSize (double borderSize)
 Set the size of the border (shown in selected elements).
 
bool SetOnline (bool online=true)
 Set if the element is online or offline.
 
virtual void SetPointList (std::vector< wxPoint2DDouble > pointList)
 Set the list of points that connect the element to the bus.
 
ElementType GetElementType ()
 
wxRect2DDouble GetRect () const
 Get the element rectangle.
 
wxPoint2DDouble GetPosition () const
 Get the element position.
 
bool IsDragging () const
 Checks if the element is being dragged.
 
double GetHeight () const
 Get the element height.
 
bool IsSelected () const
 Checks if the element is selected.
 
double GetWidth () const
 Get the element width.
 
double GetAngle () const
 Get the element angle.
 
double GetRotationAngle () const
 Get the angle of rotation.
 
bool IsPickboxShown () const
 Checks if the pickbox is shown.
 
bool IsOnline () const
 Checks if the element is online or offline.
 
virtual std::vector< wxPoint2DDouble > GetPointList () const
 Get the list of points that connect the element to bus.
 
virtual bool AddParent (Element *parent, wxPoint2DDouble position)
 Add a parent to the element. This method must be used on power elements that connect to a bus, so the parent must be a bus. The element basic points are calculated in this method, so apply this when the element is being inserted.
 
virtual void AddParent (Element *parent)
 Add a parent to the element.
 
virtual void SetID (int id)
 Set the element ID.
 
virtual int GetID () const
 Get the element ID.
 
virtual void AddChild (Element *child)
 Add a child to the child list.
 
virtual void RemoveChild (Element *child)
 Remove a child from the list.
 
virtual void ReplaceChild (Element *oldChild, Element *newChild)
 Replace a child from the list.
 
virtual wxString GetTipText () const
 Get the tip text.
 
virtual void DrawDC (wxPoint2DDouble translation, double scale, wxDC &dc) const
 Draw the element using wxDC.
 
virtual bool GetContextMenu (wxMenu &menu)
 Get the element contex menu.
 
virtual void AddPoint (wxPoint2DDouble point)
 Add point to the list of points that connect the element to the bus.
 
virtual void MoveNode (Element *parent, wxPoint2DDouble position)
 Move a node. StartMove(wxPoint2DDouble position) before start moving.
 
virtual bool NodeContains (wxPoint2DDouble position)
 Check if a node contains a point. If contains, set the attributes related to node movement.
 
virtual void UpdateNodes ()
 Update the nodes according to the parents. If a parent is removed, use this method.
 
void SetParent (Element *parent, int num)
 
void SetChild (Element *child, int num)
 
virtual bool SetNodeParent (Element *parent)
 Set a perent to the node. If all conditions are met, a new parent are added to the element and the points related to the nodes will be calculated.
 
virtual void RemoveParent (Element *parent)
 Remove a parent.
 
virtual void ReplaceParent (Element *oldParent, Element *newParent)
 Replace a parent.
 
virtual void RotateNode (Element *parent, bool clockwise=true)
 Rotate a node.
 
virtual bool PickboxContains (wxPoint2DDouble position)
 Check if a pickbox contains a point. If contains the attributes related to pickbox movement will be calculated.
 
virtual void MovePickbox (wxPoint2DDouble position)
 Move the pickbox.
 
virtual wxCursor GetBestPickboxCursor () const
 Get the best cursor to shown to the user when the mouse is above a pickbox.
 
virtual void ResetPickboxes ()
 Remove the pickboxes.
 
virtual void ResetNodes ()
 Remove the active nodes.
 
virtual wxPoint2DDouble WorldToScreen (wxPoint2DDouble translation, double scale, double offsetX=0.0, double offsetY=0.0) const
 Convert the element position to screen position.
 
virtual wxPoint2DDouble WorldToScreen (wxPoint2DDouble position, wxPoint2DDouble translation, double scale, double offsetX=0.0, double offsetY=0.0) const
 Convert a generic position to screen position.
 
virtual bool RotatedRectanglesIntersects (wxRect2DDouble rect1, wxRect2DDouble rect2, double angle1, double angle2) const
 Check if two roteted rectangles intersect.
 
virtual void DrawDCRectangle (wxPoint2DDouble position, double width, double height, double angle, wxDC &dc) const
 Draw a circle.
 
virtual void DrawDCRoundedRectRotated (wxDC &dc, const wxPoint2DDouble &center, double width, double height, double radius, double angleDeg, int arcSegments=8) const
 
virtual void DrawDCCircle (wxPoint2DDouble position, double radius, int numSegments, wxGraphicsContext *gc) const
 Draw a circle using device context.
 
virtual void DrawDCCircle (wxPoint2DDouble position, double radius, wxDC &dc) const
 
virtual void DrawDCArc (wxPoint2DDouble position, double radius, double initAngle, double finalAngle, int numSegments, wxGraphicsContext *gc) const
 
virtual void DrawDCArc (wxPoint2DDouble position, double radius, double initAngle, double finalAngle, wxDC &dc) const
 
virtual void DrawDCTriangle (std::vector< wxPoint2DDouble > points, wxGraphicsContext *gc) const
 Draw rectangle.
 
virtual void DrawDCTriangle (std::vector< wxPoint > points, wxDC &dc) const
 
virtual void DrawDCPickbox (wxPoint2DDouble position, wxGraphicsContext *gc) const
 Draw a point.
 
virtual wxPoint2DDouble RotateAtPosition (wxPoint2DDouble pointToRotate, double angle, bool degrees=true) const
 Rotate a point as element position being the origin.
 
virtual wxPoint2DDouble RotateLocal (wxPoint2DDouble local, double angleDeg) const
 
virtual wxPoint RotateAround (const wxPoint2DDouble &p, const wxPoint2DDouble &center, double angleDeg) const
 
virtual std::vector< Element * > GetParentList () const
 Get the parent list.
 
virtual std::vector< Element * > GetChildList () const
 Get the Child list.
 
virtual void CalculateBoundaries (wxPoint2DDouble &leftUp, wxPoint2DDouble &rightBottom) const
 Calculate the element boundaries.
 
virtual void GeneralMenuItens (wxMenu &menu)
 Insert general itens to context menu.
 
virtual double PointToLineDistance (wxPoint2DDouble point, int *segmentNumber=nullptr) const
 Calculate the distance between a line (formed by point list) and a point.
 
bool IsInserted () const
 Check if the element is properly inserted in the workspace.
 
void SetInserted (bool inserted=true)
 Set if the element is properly inserted in the workspace.
 
void SaveCADProperties (rapidxml::xml_document<> &doc, rapidxml::xml_node<> *elementNode)
 
bool OpenCADProperties (rapidxml::xml_node<> *elementNode)
 

Protected Member Functions

void CalculateBlockSize (double numInNodes)
 
- Protected Member Functions inherited from ControlElement
void SaveControlNodes (rapidxml::xml_document<> &doc, rapidxml::xml_node<> *elementNode)
 
bool OpenControlNodes (rapidxml::xml_node<> *elementNode)
 

Protected Attributes

MathExprParser m_fparser
 
wxString m_mathExpression = "sqrt(x^2 + y^2)"
 
wxString m_variables = "time,step,x,y"
 
std::vector< wxString > m_variablesVector
 
double * m_inputValues = nullptr
 
std::vector< GCText * > m_gcTextInputVector
 
GCText m_symbol
 
wxSize m_symbolSize
 
int m_maxSringSize = 0
 
double m_minimumSize = 36.0
 
- Protected Attributes inherited from ControlElement
std::vector< Node * > m_nodeList
 
bool m_solved = false
 
double m_output = 0.0
 
- Protected Attributes inherited from Element
ElementType m_elementType = ElementType::TYPE_NONE
 
int m_elementID = 0
 
std::vector< Element * > m_parentList
 
std::vector< Element * > m_childList
 
wxRect2DDouble m_rect
 
wxPoint2DDouble m_position
 
double m_width = 0.0
 
double m_height = 0.0
 
double m_angle = 0.0
 
double m_borderSize = 2.0
 
double m_rotationAngle = 45.0
 
double m_switchSize = 10.0
 
std::vector< wxRect2DDouble > m_switchRect
 
bool m_selected = false
 
bool m_dragging = false
 
bool m_showPickbox = false
 
bool m_inserted = false
 
int m_activePickboxID = ID_PB_NONE
 
int m_activeNodeID = 0
 
std::vector< wxPoint2DDouble > m_pointList
 
std::vector< wxPoint2DDouble > m_movePts
 
wxPoint2DDouble m_moveStartPt
 
wxPoint2DDouble m_movePos
 
bool m_online = true
 
wxColour m_selectionColour
 

Additional Inherited Members

- Static Public Member Functions inherited from ControlElement
static ControlElementGetControlElementFromID (std::vector< std::shared_ptr< ControlElement > > elementList, int id)
 
- Static Public Member Functions inherited from Element
static bool DoubleFromString (wxWindow *parent, wxString strValue, double &value, wxString errorMsg)
 Get a double value from a string. Show a error message if the conversion fail.
 
static bool IntFromString (wxWindow *parent, wxString strValue, int &value, wxString errorMsg)
 Convert a string to int. Show a error message if the conversion fail.
 
static wxString StringFromDouble (double value, int minDecimal=1, int maxDecimals=13)
 Convert a double value to string.
 

Detailed Description

A generic math expression block that can perform math and conditional operations with the inputs.

Author
Thales Lima Oliveira thale.nosp@m.s@uf.nosp@m.u.br
Date
19/12/2017

Definition at line 37 of file MathExpression.h.

Constructor & Destructor Documentation

◆ MathExpression()

MathExpression::MathExpression ( int  id)

Definition at line 24 of file MathExpression.cpp.

24 : ControlElement(id)
25{
26 m_variablesVector.push_back("x");
27 m_variablesVector.push_back("y");
28
29 for (unsigned int i = 0; i < m_variablesVector.size(); ++i) {
30 m_gcTextInputVector.push_back(new GCText(m_variablesVector[i]));
31 }
32
33 // Symbol
34 //m_symbol.SetFontSize(12);
35 //m_symbol.SetFontWeight(wxFONTWEIGHT_BOLD);
36 //m_symbol.SetFontStyle(wxFONTSTYLE_ITALIC);
37 //m_symbol.SetFontFamily(wxFONTFAMILY_ROMAN);
38 m_symbol.SetFont(wxFont(12, wxFONTFAMILY_ROMAN, wxFONTSTYLE_ITALIC, wxFONTWEIGHT_BOLD));
39 m_symbol.SetText("f(x)");
40 m_symbolSize = wxSize(m_symbol.GetWidth(), m_symbol.GetHeight());
41
42 CalculateBlockSize(static_cast<double>(m_variablesVector.size()));
43
44 for (unsigned int i = 0; i < m_variablesVector.size(); ++i) {
45 wxPoint2DDouble nodePosition(0, 0);
46 if (m_variablesVector.size() == 1) {
47 nodePosition = m_position + wxPoint2DDouble(-m_width / 2, 0);
48 }
49 else {
50 nodePosition = m_position + wxPoint2DDouble(-m_width / 2, 9 + 18 * i - m_height / 2);
51 }
52 Node* nodeIn = new Node(nodePosition, Node::NodeType::NODE_IN, m_borderSize);
53 nodeIn->StartMove(m_position);
54 m_nodeList.push_back(nodeIn);
55 }
56 Node* nodeOut = new Node(m_position + wxPoint2DDouble(m_width / 2, 0), Node::NodeType::NODE_OUT, m_borderSize);
57 nodeOut->SetAngle(180.0);
58 nodeOut->StartMove(m_position);
59 m_nodeList.push_back(nodeOut);
60
61 UpdatePoints();
62}
Class to draw text on Graphics Context using wxWidgets.
Definition GCText.h:32
virtual void SetText(wxString text)
Set correctly a new text string.
Definition GCText.cpp:68
Node of a control element. This class manages the user interaction with the connection and control el...

◆ ~MathExpression()

MathExpression::~MathExpression ( )

Definition at line 64 of file MathExpression.cpp.

65{
66 for (auto it = m_gcTextInputVector.begin(), itEnd = m_gcTextInputVector.end(); it != itEnd; ++it) { delete* it; }
67 m_gcTextInputVector.clear();
68 for (auto& node : m_nodeList) if (node) delete node;
69 m_nodeList.clear();
70}

Member Function Documentation

◆ AddInNode()

void MathExpression::AddInNode ( )

Definition at line 312 of file MathExpression.cpp.

313{
314 Node* newNode = new Node(wxPoint2DDouble(0, 0), Node::NodeType::NODE_IN, m_borderSize);
315 newNode->SetAngle(m_angle);
316 m_nodeList.insert(m_nodeList.end() - 1, newNode);
317}

◆ CalculateBlockSize()

void MathExpression::CalculateBlockSize ( double  numInNodes)
protected

Definition at line 340 of file MathExpression.cpp.

341{
342 m_maxSringSize = 0;
343 for (auto it = m_gcTextInputVector.begin(), itEnd = m_gcTextInputVector.end(); it != itEnd; ++it) {
344 if (m_maxSringSize < (*it)->GetWidth()) m_maxSringSize = (*it)->GetWidth();
345 }
346 if (m_angle == 0.0 || m_angle == 180.0) {
347 m_height = 18.0 * numInNodes;
348 if (m_height < m_minimumSize) m_height = m_minimumSize; // minimum height
349 m_width = m_maxSringSize + m_symbolSize.GetWidth() + 18;
350 }
351 else {
352 m_width = 18.0 * numInNodes;
353 if (m_width < m_minimumSize) m_width = m_minimumSize; // minimum width
354 m_height = m_maxSringSize + m_symbolSize.GetHeight() + 18;
355 }
356}

◆ Contains()

virtual bool MathExpression::Contains ( wxPoint2DDouble  position) const
inlinevirtual

Checks if the element contains a position.

Parameters
positionPosition to be checked.

Implements Element.

Definition at line 45 of file MathExpression.h.

45{ return m_rect.Contains(position); }

◆ DrawDC()

void MathExpression::DrawDC ( wxPoint2DDouble  translation,
double  scale,
wxGraphicsContext *  gc 
) const
virtual

Draw the element using GDI+.

Parameters
translationTranslation of the system.
scaleScale of the system.
gcGraphics context

Reimplemented from Element.

Definition at line 133 of file MathExpression.cpp.

134{
135 double pi = 3.1415926535897932;
136 if (m_selected) {
137 gc->SetPen(*wxTRANSPARENT_PEN);
138 gc->SetBrush(wxBrush(m_selectionColour));
139 double borderSize = (m_borderSize * 2.0 + 1.0) / scale;
140 gc->DrawRectangle(m_position.m_x - m_width / 2 - borderSize / 2, m_position.m_y - m_height / 2 - borderSize / 2, m_width + borderSize, m_height + borderSize);
141 }
142 gc->SetPen(wxPen(wxColour(0, 0, 0, 255), 1));
143 gc->SetBrush(wxBrush(wxColour(255, 255, 255, 255)));
144 gc->DrawRectangle(m_position.m_x - m_width / 2, m_position.m_y - m_height / 2, m_width, m_height);
145
146 // Plot input variables and symbol.
147 if (m_angle == 0.0) {
148 double w = static_cast<double>(m_symbolSize.GetWidth());
149 double h = static_cast<double>(m_symbolSize.GetHeight());
150 m_symbol.Draw(m_nodeList[m_nodeList.size() - 1]->GetPosition() - wxPoint2DDouble(w + 6.0, h / 2.0), gc, 0, wxColour(0, 77, 255, 255));
151 for (unsigned int i = 0; i < m_gcTextInputVector.size(); ++i) {
152 w = static_cast<double>(m_gcTextInputVector[i]->GetWidth());
153 h = static_cast<double>(m_gcTextInputVector[i]->GetHeight());
154 m_gcTextInputVector[i]->Draw(m_nodeList[i]->GetPosition() + wxPoint2DDouble(6.0, -h / 2.0), gc);
155 }
156 }
157 else if (m_angle == 90.0) {
158 double w = static_cast<double>(m_symbolSize.GetWidth());
159 double h = static_cast<double>(m_symbolSize.GetHeight());
160 m_symbol.Draw(m_nodeList[m_nodeList.size() - 1]->GetPosition() - wxPoint2DDouble(w / 2, h + 6.0), gc, 0, wxColour(0, 77, 255, 255));
161 for (unsigned int i = 0; i < m_gcTextInputVector.size(); ++i) {
162 w = static_cast<double>(m_gcTextInputVector[i]->GetWidth());
163 h = static_cast<double>(m_gcTextInputVector[i]->GetHeight());
164 m_gcTextInputVector[i]->Draw(m_nodeList[i]->GetPosition() + wxPoint2DDouble(-h / 2, w + 6.0), gc, pi / 2.0);
165 }
166 }
167 else if (m_angle == 180.0) {
168 double w = static_cast<double>(m_symbolSize.GetWidth());
169 double h = static_cast<double>(m_symbolSize.GetHeight());
170 m_symbol.Draw(m_nodeList[m_nodeList.size() - 1]->GetPosition() + wxPoint2DDouble(6.0, -h / 2.0), gc, 0, wxColour(0, 77, 255, 255));
171 for (unsigned int i = 0; i < m_gcTextInputVector.size(); ++i) {
172 w = static_cast<double>(m_gcTextInputVector[i]->GetWidth());
173 h = static_cast<double>(m_gcTextInputVector[i]->GetHeight());
174 m_gcTextInputVector[i]->Draw(m_nodeList[i]->GetPosition() - wxPoint2DDouble(w + 6.0, h / 2.0), gc);
175 }
176 }
177 else if (m_angle == 270.0) {
178 double w = static_cast<double>(m_symbolSize.GetWidth());
179 double h = static_cast<double>(m_symbolSize.GetHeight());
180 m_symbol.Draw(m_nodeList[m_nodeList.size() - 1]->GetPosition() + wxPoint2DDouble(-w / 2, 6.0), gc, 0, wxColour(0, 77, 255, 255));
181 for (unsigned int i = 0; i < m_gcTextInputVector.size(); ++i) {
182 w = static_cast<double>(m_gcTextInputVector[i]->GetWidth());
183 h = static_cast<double>(m_gcTextInputVector[i]->GetHeight());
184 m_gcTextInputVector[i]->Draw(m_nodeList[i]->GetPosition() - wxPoint2DDouble(-h / 2, w + 6.0), gc, -pi / 2.0);
185 }
186 }
187
188 gc->SetPen(*wxTRANSPARENT_PEN);
189 gc->SetBrush(*wxBLACK_BRUSH);
190 DrawDCNodes(gc);
191}
wxPoint2DDouble GetPosition() const
Get the element position.
Definition Element.h:186
virtual void Draw(wxPoint2DDouble position, wxGraphicsContext *gc, double angle=0.0, wxColour colour= *wxBLACK) const
Draw the text in wxGraphicsContext.
Definition GCText.cpp:35
Here is the call graph for this function:

◆ GetCopy()

Element * MathExpression::GetCopy ( )
virtual

Get a the element copy.

Returns
Copy of the element.

Reimplemented from Element.

Definition at line 263 of file MathExpression.cpp.

264{
265 MathExpression* copy = new MathExpression(*this);
266 copy->m_gcTextInputVector.clear();
267 for (auto it = m_gcTextInputVector.begin(), itEnd = m_gcTextInputVector.end(); it != itEnd; ++it) {
268 copy->m_gcTextInputVector.push_back((*it)->GetCopy());
269 }
270 return copy;
271}
A generic math expression block that can perform math and conditional operations with the inputs.

◆ GetMathExpression()

virtual wxString MathExpression::GetMathExpression ( )
inlinevirtual

Definition at line 52 of file MathExpression.h.

52{ return m_mathExpression; }

◆ GetParser()

virtual MathExprParser MathExpression::GetParser ( )
inlinevirtual

Definition at line 56 of file MathExpression.h.

56{ return m_fparser; }

◆ GetVariables()

virtual std::vector< wxString > MathExpression::GetVariables ( )
inlinevirtual

Definition at line 54 of file MathExpression.h.

54{ return m_variablesVector; }

◆ Initialize()

bool MathExpression::Initialize ( )
virtual

Reimplemented from ControlElement.

Definition at line 381 of file MathExpression.cpp.

382{
383 m_variables = "time,step,switch,";
384 for (auto it = m_variablesVector.begin(), itEnd = m_variablesVector.end(); it != itEnd; ++it)
385 m_variables += *(it)+",";
386 m_variables.RemoveLast();
387
388 // Set locale to ENGLISH_US to avoid parser error in numbers (eg. comma).
389 int currentLang = wxLocale::GetSystemLanguage();
390 wxLocale newLocale(wxLANGUAGE_ENGLISH_US);
391 int parserRes = m_fparser.Parse(static_cast<std::string>(m_mathExpression), static_cast<std::string>(m_variables));
392 if (parserRes != -1) return false; // Parse error.
393 wxLocale oldLocale(currentLang); // Return to current language.
394
395 if (m_inputValues) delete m_inputValues;
396 m_inputValues = new double[m_variablesVector.size() + 3]; // Custom variables + time + step + switch
397
398 // Optimize only once to gain performance.
399 m_fparser.Optimize();
400
401 m_solved = false;
402 m_output = 0.0;
403 return true;
404}

◆ Intersects()

virtual bool MathExpression::Intersects ( wxRect2DDouble  rect) const
inlinevirtual

Check if the element's rect intersects other rect.

Parameters
rectRect to check intersection.

Implements Element.

Definition at line 46 of file MathExpression.h.

46{ return m_rect.Intersects(rect); }

◆ OpenElement()

bool MathExpression::OpenElement ( rapidxml::xml_node<> *  elementNode)
virtual

Reimplemented from Element.

Definition at line 426 of file MathExpression.cpp.

427{
428 if (!OpenCADProperties(elementNode)) return false;
429 if (!OpenControlNodes(elementNode)) return false;
430
431 // Element properties
432 std::vector<wxString> variables;
433 auto variablesNode = elementNode->first_node("VariableList");
434 auto variable = variablesNode->first_node("Variable");
435 while (variable) {
436 variables.push_back(variable->value());
437 variable = variable->next_sibling("Variable");
438 }
439 SetVariables(variables);
440
441 auto mathExprValueNode = elementNode->first_node("MathExprValue");
442 m_mathExpression = mathExprValueNode->value();
443
444 // Init opened properties
445 StartMove(m_position);
446 UpdatePoints();
447
448 return true;
449}
virtual void StartMove(wxPoint2DDouble position)
Update the element attributes related to the movement.

◆ RemoveInNode()

void MathExpression::RemoveInNode ( )

Definition at line 319 of file MathExpression.cpp.

320{
321 Node* nodeToRemove = *(m_nodeList.end() - 2);
322 bool foundChild = false;
323 for (auto it = m_childList.begin(), itEnd = m_childList.end(); it != itEnd; ++it) {
324 ControlElement* child = static_cast<ControlElement*>(*it);
325 auto childNodeList = child->GetNodeList();
326 for (auto itN = childNodeList.begin(), itEndN = childNodeList.end(); itN != itEndN; ++itN) {
327 Node* node = *itN;
328 if (node == nodeToRemove) {
329 child->RemoveParent(this);
330 RemoveChild(child);
331 foundChild = true;
332 break;
333 }
334 }
335 if (foundChild) break;
336 }
337 m_nodeList.erase(m_nodeList.end() - 2);
338}
virtual void RemoveChild(Element *child)
Remove a child from the list.
Definition Element.cpp:495
virtual void RemoveParent(Element *parent)
Remove a parent.
Definition Element.h:371

◆ Rotate()

void MathExpression::Rotate ( bool  clockwise = true)
virtual

Rotate the element.

Parameters
clockwiseTrue to rotate clockwise, false to rotate counter-clockwise.

Reimplemented from Element.

Definition at line 203 of file MathExpression.cpp.

204{
205 if (clockwise)
206 m_angle += 90.0;
207 else
208 m_angle -= 90.0;
209 if (m_angle >= 360.0)
210 m_angle = 0.0;
211 else if (m_angle < 0)
212 m_angle = 270.0;
213
214 UpdatePoints();
215
216 for (auto it = m_nodeList.begin(), itEnd = m_nodeList.end(); it != itEnd; ++it) {
217 Node* node = *it;
218 node->Rotate(clockwise);
219 }
220}

◆ SaveElement()

rapidxml::xml_node * MathExpression::SaveElement ( rapidxml::xml_document<> &  doc,
rapidxml::xml_node<> *  elementListNode 
)
virtual

Reimplemented from Element.

Definition at line 406 of file MathExpression.cpp.

407{
408 auto elementNode = XMLParser::AppendNode(doc, elementListNode, "MathExpr");
409 XMLParser::SetNodeAttribute(doc, elementNode, "ID", m_elementID);
410
411 SaveCADProperties(doc, elementNode);
412 SaveControlNodes(doc, elementNode);
413
414 // Element properties
415 auto variablesNode = XMLParser::AppendNode(doc, elementNode, "VariableList");
416 for (unsigned int i = 0; i < m_variablesVector.size(); ++i) {
417 auto variable = XMLParser::AppendNode(doc, variablesNode, "Variable");
418 XMLParser::SetNodeValue(doc, variable, m_variablesVector[i]);
419 }
420 auto mathExprValue = XMLParser::AppendNode(doc, elementNode, "MathExprValue");
421 XMLParser::SetNodeValue(doc, mathExprValue, m_mathExpression);
422
423 return elementNode;
424}

◆ SetMathExpression()

virtual void MathExpression::SetMathExpression ( wxString  mathExpression)
inlinevirtual

Definition at line 53 of file MathExpression.h.

53{ m_mathExpression = mathExpression; }

◆ SetVariables()

void MathExpression::SetVariables ( std::vector< wxString >  variablesVector)
virtual

Definition at line 370 of file MathExpression.cpp.

371{
372 m_variablesVector = variablesVector;
373 // Clear old glTextVector
374 for (auto it = m_gcTextInputVector.begin(), itEnd = m_gcTextInputVector.end(); it != itEnd; ++it) { delete* it; }
375 m_gcTextInputVector.clear();
376
377 for (auto it = m_variablesVector.begin(), itEnd = m_variablesVector.end(); it != itEnd; ++it)
378 m_gcTextInputVector.push_back(new GCText(*(it)));
379}

◆ ShowForm()

bool MathExpression::ShowForm ( wxWindow *  parent,
Element element,
wxWindow *  workspace = nullptr 
)
virtual

Show element data form.

Parameters
parentForm parent
elementElement that will be edited.
Returns
True if the form is shown, false otherwise.

Reimplemented from Element.

Definition at line 193 of file MathExpression.cpp.

194{
195 MathExpressionForm mathExprForm(parent, this);
196 mathExprForm.CenterOnParent();
197 if (mathExprForm.ShowModal() == wxID_OK) {
198 return true;
199 }
200 return false;
201}

◆ Solve()

bool MathExpression::Solve ( double *  input,
double  timeStep 
)
virtual

Reimplemented from ControlElement.

Definition at line 222 of file MathExpression.cpp.

223{
224 if (!input) {
225 m_output = 0.0;
226 return true;
227 }
228 // Get the input vector from connection lines (can't use default (one) input argument)
229 m_inputValues[0] = input[1]; // Current time
230 m_inputValues[1] = timeStep;
231 m_inputValues[2] = input[2]; // Switch status
232 int i = 3;
233 for (auto itN = m_nodeList.begin(), itNEnd = m_nodeList.end(); itN != itNEnd; ++itN) {
234 Node* node = *itN;
235 if (node->GetNodeType() != Node::NodeType::NODE_OUT) {
236 if (!node->IsConnected()) {
237 m_inputValues[i] = 0.0; // Node not connected means zero value as input.
238 }
239 else {
240 for (auto itC = m_childList.begin(), itCEnd = m_childList.end(); itC != itCEnd; ++itC) {
241 ConnectionLine* cLine = static_cast<ConnectionLine*>(*itC);
242 auto nodeList = cLine->GetNodeList();
243 for (auto itCN = nodeList.begin(), itCNEnd = nodeList.end(); itCN != itCNEnd; ++itCN) {
244 Node* childNode = *itCN;
245 if (childNode == node) {
246 m_inputValues[i] = cLine->GetValue();
247 break;
248 }
249 }
250 }
251 }
252 ++i;
253 }
254 }
255
256 // Solve the math expression using fparser
257 double result = m_fparser.Eval(m_inputValues);
258 if (m_fparser.EvalError() != 0) return false;
259 m_output = result;
260 return true;
261}
Connection between two control elements or other connection line and an element.

◆ UpdatePoints()

void MathExpression::UpdatePoints ( )
virtual

Definition at line 273 of file MathExpression.cpp.

274{
275 CalculateBlockSize(static_cast<double>(m_nodeList.size()) - 1.0);
276
277 if (m_nodeList.size() == 2) // Only one input (and the output).
278 {
279 if (m_angle == 0.0)
280 m_nodeList[0]->SetPosition(m_position + wxPoint2DDouble(-m_width / 2, 0));
281 else if (m_angle == 90.0)
282 m_nodeList[0]->SetPosition(m_position + wxPoint2DDouble(0, -m_height / 2));
283 else if (m_angle == 180.0)
284 m_nodeList[0]->SetPosition(m_position + wxPoint2DDouble(m_width / 2, 0));
285 else if (m_angle == 270.0)
286 m_nodeList[0]->SetPosition(m_position + wxPoint2DDouble(0, m_height / 2));
287 }
288 else {
289 for (unsigned int i = 0; i < m_nodeList.size() - 1; ++i) {
290 if (m_angle == 0.0)
291 m_nodeList[i]->SetPosition(m_position + wxPoint2DDouble(-m_width / 2, 9 + 18 * i - m_height / 2));
292 else if (m_angle == 90.0)
293 m_nodeList[i]->SetPosition(m_position + wxPoint2DDouble(m_width / 2 - 9 - 18 * i, -m_height / 2));
294 else if (m_angle == 180.0)
295 m_nodeList[i]->SetPosition(m_position + wxPoint2DDouble(m_width / 2, m_height / 2 - 9 - 18 * i));
296 else if (m_angle == 270.0)
297 m_nodeList[i]->SetPosition(m_position + wxPoint2DDouble(9 + 18 * i - m_width / 2, m_height / 2));
298 }
299 }
300 if (m_angle == 0.0)
301 m_nodeList[m_nodeList.size() - 1]->SetPosition(m_position + wxPoint2DDouble(m_width / 2, 0));
302 else if (m_angle == 90.0)
303 m_nodeList[m_nodeList.size() - 1]->SetPosition(m_position + wxPoint2DDouble(0, m_height / 2));
304 else if (m_angle == 180.0)
305 m_nodeList[m_nodeList.size() - 1]->SetPosition(m_position + wxPoint2DDouble(-m_width / 2, 0));
306 else if (m_angle == 270.0)
307 m_nodeList[m_nodeList.size() - 1]->SetPosition(m_position + wxPoint2DDouble(0, -m_height / 2));
308
309 SetPosition(m_position); // Update rect.
310}
void SetPosition(const wxPoint2DDouble position)
Set the element position and update the rectangle.
Definition Element.cpp:28

◆ UpdateText()

bool MathExpression::UpdateText ( )
virtual

Update the OpenGL text in the element (if present).

Returns
true if OpenGLText is ok, false otherwise.

Reimplemented from ControlElement.

Definition at line 358 of file MathExpression.cpp.

359{
360 bool isTextureOK = true;
361 m_symbol.SetText(m_symbol.GetText());
362 //if (!m_symbol.IsTextureOK()) isTextureOK = false;
363 for (auto it = m_gcTextInputVector.begin(), itEnd = m_gcTextInputVector.end(); it != itEnd; ++it) {
364 (*it)->SetText((*it)->GetText());
365 //if (!(*it)->IsTextureOK()) isTextureOK = false;
366 }
367 return isTextureOK;
368}
Here is the call graph for this function:

Member Data Documentation

◆ m_fparser

MathExprParser MathExpression::m_fparser
protected

Definition at line 71 of file MathExpression.h.

◆ m_gcTextInputVector

std::vector<GCText*> MathExpression::m_gcTextInputVector
protected

Definition at line 76 of file MathExpression.h.

◆ m_inputValues

double* MathExpression::m_inputValues = nullptr
protected

Definition at line 75 of file MathExpression.h.

◆ m_mathExpression

wxString MathExpression::m_mathExpression = "sqrt(x^2 + y^2)"
protected

Definition at line 72 of file MathExpression.h.

◆ m_maxSringSize

int MathExpression::m_maxSringSize = 0
protected

Definition at line 79 of file MathExpression.h.

◆ m_minimumSize

double MathExpression::m_minimumSize = 36.0
protected

Definition at line 80 of file MathExpression.h.

◆ m_symbol

GCText MathExpression::m_symbol
protected

Definition at line 77 of file MathExpression.h.

◆ m_symbolSize

wxSize MathExpression::m_symbolSize
protected

Definition at line 78 of file MathExpression.h.

◆ m_variables

wxString MathExpression::m_variables = "time,step,x,y"
protected

Definition at line 73 of file MathExpression.h.

◆ m_variablesVector

std::vector<wxString> MathExpression::m_variablesVector
protected

Definition at line 74 of file MathExpression.h.


The documentation for this class was generated from the following files: