Power System Platform  2026w23a-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 (GUIColour *guiColour, 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 void SetFont (wxFont &font)
 
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 (GUIColour *guiColour, 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
 
wxFont m_font
 
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
 

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.SetFont(wxFont(12, wxFONTFAMILY_ROMAN, wxFONTSTYLE_ITALIC, wxFONTWEIGHT_BOLD));
35 m_symbol.SetText("f(x)");
36 m_symbolSize = wxSize(m_symbol.GetWidth(), m_symbol.GetHeight());
37
38 CalculateBlockSize(static_cast<double>(m_variablesVector.size()));
39
40 for (unsigned int i = 0; i < m_variablesVector.size(); ++i) {
41 wxPoint2DDouble nodePosition(0, 0);
42 if (m_variablesVector.size() == 1) {
43 nodePosition = m_position + wxPoint2DDouble(-m_width / 2, 0);
44 }
45 else {
46 nodePosition = m_position + wxPoint2DDouble(-m_width / 2, 9 + 18 * i - m_height / 2);
47 }
48 Node* nodeIn = new Node(nodePosition, Node::NodeType::NODE_IN, m_borderSize);
49 nodeIn->StartMove(m_position);
50 m_nodeList.push_back(nodeIn);
51 }
52 Node* nodeOut = new Node(m_position + wxPoint2DDouble(m_width / 2, 0), Node::NodeType::NODE_OUT, m_borderSize);
53 nodeOut->SetAngle(180.0);
54 nodeOut->StartMove(m_position);
55 m_nodeList.push_back(nodeOut);
56
57 UpdatePoints();
58}
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 60 of file MathExpression.cpp.

61{
62 for (auto it = m_gcTextInputVector.begin(), itEnd = m_gcTextInputVector.end(); it != itEnd; ++it) { delete* it; }
63 m_gcTextInputVector.clear();
64 for (auto& node : m_nodeList) if (node) delete node;
65 m_nodeList.clear();
66}

Member Function Documentation

◆ AddInNode()

void MathExpression::AddInNode ( )

Definition at line 257 of file MathExpression.cpp.

258{
259 Node* newNode = new Node(wxPoint2DDouble(0, 0), Node::NodeType::NODE_IN, m_borderSize);
260 newNode->SetAngle(m_angle);
261 m_nodeList.insert(m_nodeList.end() - 1, newNode);
262}

◆ CalculateBlockSize()

void MathExpression::CalculateBlockSize ( double  numInNodes)
protected

Definition at line 285 of file MathExpression.cpp.

286{
287 m_maxSringSize = 0;
288 for (auto it = m_gcTextInputVector.begin(), itEnd = m_gcTextInputVector.end(); it != itEnd; ++it) {
289 if (m_maxSringSize < (*it)->GetWidth()) m_maxSringSize = (*it)->GetWidth();
290 }
291 if (m_angle == 0.0 || m_angle == 180.0) {
292 m_height = 18.0 * numInNodes;
293 if (m_height < m_minimumSize) m_height = m_minimumSize; // minimum height
294 m_width = m_maxSringSize + m_symbolSize.GetWidth() + 18;
295 }
296 else {
297 m_width = 18.0 * numInNodes;
298 if (m_width < m_minimumSize) m_width = m_minimumSize; // minimum width
299 m_height = m_maxSringSize + m_symbolSize.GetHeight() + 18;
300 }
301}

◆ 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 ( GUIColour guiColour,
wxPoint2DDouble  translation,
double  scale,
wxGraphicsContext *  gc 
) const
virtual

Draw the element using GDI+.

Parameters
guiColourColours of the system defined in properties.
translationTranslation of the system.
scaleScale of the system.
gcGraphics context

Reimplemented from Element.

Definition at line 69 of file MathExpression.cpp.

70{
71 double pi = 3.1415926535897932;
72 if (m_selected) {
73 gc->SetPen(*wxTRANSPARENT_PEN);
74 gc->SetBrush(guiColour->selection);
75 double borderSize = (m_borderSize * 2.0 + 1.0) / scale;
76 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);
77 }
78 gc->SetPen(wxPen(guiColour->enabled, 1));
79 gc->SetBrush(guiColour->background);
80 gc->DrawRectangle(m_position.m_x - m_width / 2, m_position.m_y - m_height / 2, m_width, m_height);
81
82 // Plot input variables and symbol.
83 if (m_angle == 0.0) {
84 double w = static_cast<double>(m_symbolSize.GetWidth());
85 double h = static_cast<double>(m_symbolSize.GetHeight());
86 m_symbol.Draw(m_nodeList[m_nodeList.size() - 1]->GetPosition() - wxPoint2DDouble(w + 6.0, h / 2.0), gc, 0, guiColour->bus);
87 for (unsigned int i = 0; i < m_gcTextInputVector.size(); ++i) {
88 w = static_cast<double>(m_gcTextInputVector[i]->GetWidth());
89 h = static_cast<double>(m_gcTextInputVector[i]->GetHeight());
90 m_gcTextInputVector[i]->Draw(m_nodeList[i]->GetPosition() + wxPoint2DDouble(6.0, -h / 2.0), gc, 0, guiColour->enabled);
91 }
92 }
93 else if (m_angle == 90.0) {
94 double w = static_cast<double>(m_symbolSize.GetWidth());
95 double h = static_cast<double>(m_symbolSize.GetHeight());
96 m_symbol.Draw(m_nodeList[m_nodeList.size() - 1]->GetPosition() - wxPoint2DDouble(w / 2, h + 6.0), gc, 0, guiColour->bus);
97 for (unsigned int i = 0; i < m_gcTextInputVector.size(); ++i) {
98 w = static_cast<double>(m_gcTextInputVector[i]->GetWidth());
99 h = static_cast<double>(m_gcTextInputVector[i]->GetHeight());
100 m_gcTextInputVector[i]->Draw(m_nodeList[i]->GetPosition() + wxPoint2DDouble(-h / 2, w + 6.0), gc, pi / 2.0, guiColour->enabled);
101 }
102 }
103 else if (m_angle == 180.0) {
104 double w = static_cast<double>(m_symbolSize.GetWidth());
105 double h = static_cast<double>(m_symbolSize.GetHeight());
106 m_symbol.Draw(m_nodeList[m_nodeList.size() - 1]->GetPosition() + wxPoint2DDouble(6.0, -h / 2.0), gc, 0, guiColour->bus);
107 for (unsigned int i = 0; i < m_gcTextInputVector.size(); ++i) {
108 w = static_cast<double>(m_gcTextInputVector[i]->GetWidth());
109 h = static_cast<double>(m_gcTextInputVector[i]->GetHeight());
110 m_gcTextInputVector[i]->Draw(m_nodeList[i]->GetPosition() - wxPoint2DDouble(w + 6.0, h / 2.0), gc, 0, guiColour->enabled);
111 }
112 }
113 else if (m_angle == 270.0) {
114 double w = static_cast<double>(m_symbolSize.GetWidth());
115 double h = static_cast<double>(m_symbolSize.GetHeight());
116 m_symbol.Draw(m_nodeList[m_nodeList.size() - 1]->GetPosition() + wxPoint2DDouble(-w / 2, 6.0), gc, 0, guiColour->bus);
117 for (unsigned int i = 0; i < m_gcTextInputVector.size(); ++i) {
118 w = static_cast<double>(m_gcTextInputVector[i]->GetWidth());
119 h = static_cast<double>(m_gcTextInputVector[i]->GetHeight());
120 m_gcTextInputVector[i]->Draw(m_nodeList[i]->GetPosition() - wxPoint2DDouble(-h / 2, w + 6.0), gc, -pi / 2.0, guiColour->enabled);
121 }
122 }
123
124 gc->SetPen(*wxTRANSPARENT_PEN);
125 gc->SetBrush(guiColour->enabled);
126 DrawDCNodes(gc);
127}
wxPoint2DDouble GetPosition() const
Get the element position.
Definition Element.h:188
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 208 of file MathExpression.cpp.

209{
210 MathExpression* copy = new MathExpression(*this);
211 copy->m_gcTextInputVector.clear();
212 for (auto it = m_gcTextInputVector.begin(), itEnd = m_gcTextInputVector.end(); it != itEnd; ++it) {
213 copy->m_gcTextInputVector.push_back((*it)->GetCopy());
214 }
215 return copy;
216}
A generic math expression block that can perform math and conditional operations with the inputs.

◆ GetMathExpression()

virtual wxString MathExpression::GetMathExpression ( )
inlinevirtual

Definition at line 53 of file MathExpression.h.

53{ return m_mathExpression; }

◆ GetParser()

virtual MathExprParser MathExpression::GetParser ( )
inlinevirtual

Definition at line 57 of file MathExpression.h.

57{ return m_fparser; }

◆ GetVariables()

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

Definition at line 55 of file MathExpression.h.

55{ return m_variablesVector; }

◆ Initialize()

bool MathExpression::Initialize ( )
virtual

Reimplemented from ControlElement.

Definition at line 329 of file MathExpression.cpp.

330{
331 m_variables = "time,step,switch,";
332 for (auto it = m_variablesVector.begin(), itEnd = m_variablesVector.end(); it != itEnd; ++it)
333 m_variables += *(it)+",";
334 m_variables.RemoveLast();
335
336 // Set locale to ENGLISH_US to avoid parser error in numbers (eg. comma).
337 int currentLang = wxLocale::GetSystemLanguage();
338 wxLocale newLocale(wxLANGUAGE_ENGLISH_US);
339 int parserRes = m_fparser.Parse(static_cast<std::string>(m_mathExpression), static_cast<std::string>(m_variables));
340 if (parserRes != -1) return false; // Parse error.
341 wxLocale oldLocale(currentLang); // Return to current language.
342
343 if (m_inputValues) delete m_inputValues;
344 m_inputValues = new double[m_variablesVector.size() + 3]; // Custom variables + time + step + switch
345
346 // Optimize only once to gain performance.
347 m_fparser.Optimize();
348
349 m_solved = false;
350 m_output = 0.0;
351 return true;
352}

◆ 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 374 of file MathExpression.cpp.

375{
376 if (!OpenCADProperties(elementNode)) return false;
377 if (!OpenControlNodes(elementNode)) return false;
378
379 // Element properties
380 std::vector<wxString> variables;
381 auto variablesNode = elementNode->first_node("VariableList");
382 auto variable = variablesNode->first_node("Variable");
383 while (variable) {
384 variables.push_back(variable->value());
385 variable = variable->next_sibling("Variable");
386 }
387 SetVariables(variables);
388
389 auto mathExprValueNode = elementNode->first_node("MathExprValue");
390 m_mathExpression = mathExprValueNode->value();
391
392 // Init opened properties
393 StartMove(m_position);
394 UpdatePoints();
395
396 return true;
397}
virtual void StartMove(wxPoint2DDouble position)
Update the element attributes related to the movement.

◆ RemoveInNode()

void MathExpression::RemoveInNode ( )

Definition at line 264 of file MathExpression.cpp.

265{
266 Node* nodeToRemove = *(m_nodeList.end() - 2);
267 bool foundChild = false;
268 for (auto it = m_childList.begin(), itEnd = m_childList.end(); it != itEnd; ++it) {
269 ControlElement* child = static_cast<ControlElement*>(*it);
270 auto childNodeList = child->GetNodeList();
271 for (auto itN = childNodeList.begin(), itEndN = childNodeList.end(); itN != itEndN; ++itN) {
272 Node* node = *itN;
273 if (node == nodeToRemove) {
274 child->RemoveParent(this);
275 RemoveChild(child);
276 foundChild = true;
277 break;
278 }
279 }
280 if (foundChild) break;
281 }
282 m_nodeList.erase(m_nodeList.end() - 2);
283}
virtual void RemoveChild(Element *child)
Remove a child from the list.
Definition Element.cpp:500
virtual void RemoveParent(Element *parent)
Remove a parent.
Definition Element.h:374

◆ 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 139 of file MathExpression.cpp.

140{
141 if (clockwise)
142 m_angle += 90.0;
143 else
144 m_angle -= 90.0;
145 if (m_angle >= 360.0)
146 m_angle = 0.0;
147 else if (m_angle < 0)
148 m_angle = 270.0;
149
150 UpdatePoints();
151
152 for (auto it = m_nodeList.begin(), itEnd = m_nodeList.end(); it != itEnd; ++it) {
153 Node* node = *it;
154 node->Rotate(clockwise);
155 }
156}

◆ SaveElement()

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

Reimplemented from Element.

Definition at line 354 of file MathExpression.cpp.

355{
356 auto elementNode = XMLParser::AppendNode(doc, elementListNode, "MathExpr");
357 XMLParser::SetNodeAttribute(doc, elementNode, "ID", m_elementID);
358
359 SaveCADProperties(doc, elementNode);
360 SaveControlNodes(doc, elementNode);
361
362 // Element properties
363 auto variablesNode = XMLParser::AppendNode(doc, elementNode, "VariableList");
364 for (unsigned int i = 0; i < m_variablesVector.size(); ++i) {
365 auto variable = XMLParser::AppendNode(doc, variablesNode, "Variable");
366 XMLParser::SetNodeValue(doc, variable, m_variablesVector[i]);
367 }
368 auto mathExprValue = XMLParser::AppendNode(doc, elementNode, "MathExprValue");
369 XMLParser::SetNodeValue(doc, mathExprValue, m_mathExpression);
370
371 return elementNode;
372}

◆ SetFont()

void MathExpression::SetFont ( wxFont &  font)
virtual

Reimplemented from ControlElement.

Definition at line 158 of file MathExpression.cpp.

159{
160 m_font = font;
161 for (auto& txtInput : m_gcTextInputVector) {
162 if (txtInput) txtInput->SetFont(font);
163 }
164 UpdateText();
165}
virtual bool UpdateText()
Update the OpenGL text in the element (if present).

◆ SetMathExpression()

virtual void MathExpression::SetMathExpression ( wxString  mathExpression)
inlinevirtual

Definition at line 54 of file MathExpression.h.

54{ m_mathExpression = mathExpression; }

◆ SetVariables()

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

Definition at line 314 of file MathExpression.cpp.

315{
316 m_variablesVector = variablesVector;
317 for (auto it = m_gcTextInputVector.begin(), itEnd = m_gcTextInputVector.end(); it != itEnd; ++it) { delete* it; }
318 m_gcTextInputVector.clear();
319
320 for (auto& it : m_variablesVector) {
321 GCText* newInputText = new GCText(it);
322 newInputText->SetFont(m_font);
323 m_gcTextInputVector.push_back(newInputText);
324 }
325 UpdateText();
326 UpdatePoints();
327}

◆ 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 129 of file MathExpression.cpp.

130{
131 MathExpressionForm mathExprForm(parent, this);
132 mathExprForm.CenterOnParent();
133 if (mathExprForm.ShowModal() == wxID_OK) {
134 return true;
135 }
136 return false;
137}

◆ Solve()

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

Reimplemented from ControlElement.

Definition at line 167 of file MathExpression.cpp.

168{
169 if (!input) {
170 m_output = 0.0;
171 return true;
172 }
173 // Get the input vector from connection lines (can't use default (one) input argument)
174 m_inputValues[0] = input[1]; // Current time
175 m_inputValues[1] = timeStep;
176 m_inputValues[2] = input[2]; // Switch status
177 int i = 3;
178 for (auto itN = m_nodeList.begin(), itNEnd = m_nodeList.end(); itN != itNEnd; ++itN) {
179 Node* node = *itN;
180 if (node->GetNodeType() != Node::NodeType::NODE_OUT) {
181 if (!node->IsConnected()) {
182 m_inputValues[i] = 0.0; // Node not connected means zero value as input.
183 }
184 else {
185 for (auto itC = m_childList.begin(), itCEnd = m_childList.end(); itC != itCEnd; ++itC) {
186 ConnectionLine* cLine = static_cast<ConnectionLine*>(*itC);
187 auto nodeList = cLine->GetNodeList();
188 for (auto itCN = nodeList.begin(), itCNEnd = nodeList.end(); itCN != itCNEnd; ++itCN) {
189 Node* childNode = *itCN;
190 if (childNode == node) {
191 m_inputValues[i] = cLine->GetValue();
192 break;
193 }
194 }
195 }
196 }
197 ++i;
198 }
199 }
200
201 // Solve the math expression using fparser
202 double result = m_fparser.Eval(m_inputValues);
203 if (m_fparser.EvalError() != 0) return false;
204 m_output = result;
205 return true;
206}
Connection between two control elements or other connection line and an element.

◆ UpdatePoints()

void MathExpression::UpdatePoints ( )
virtual

Definition at line 218 of file MathExpression.cpp.

219{
220 CalculateBlockSize(static_cast<double>(m_nodeList.size()) - 1.0);
221
222 if (m_nodeList.size() == 2) // Only one input (and the output).
223 {
224 if (m_angle == 0.0)
225 m_nodeList[0]->SetPosition(m_position + wxPoint2DDouble(-m_width / 2, 0));
226 else if (m_angle == 90.0)
227 m_nodeList[0]->SetPosition(m_position + wxPoint2DDouble(0, -m_height / 2));
228 else if (m_angle == 180.0)
229 m_nodeList[0]->SetPosition(m_position + wxPoint2DDouble(m_width / 2, 0));
230 else if (m_angle == 270.0)
231 m_nodeList[0]->SetPosition(m_position + wxPoint2DDouble(0, m_height / 2));
232 }
233 else {
234 for (unsigned int i = 0; i < m_nodeList.size() - 1; ++i) {
235 if (m_angle == 0.0)
236 m_nodeList[i]->SetPosition(m_position + wxPoint2DDouble(-m_width / 2, 9 + 18 * i - m_height / 2));
237 else if (m_angle == 90.0)
238 m_nodeList[i]->SetPosition(m_position + wxPoint2DDouble(m_width / 2 - 9 - 18 * i, -m_height / 2));
239 else if (m_angle == 180.0)
240 m_nodeList[i]->SetPosition(m_position + wxPoint2DDouble(m_width / 2, m_height / 2 - 9 - 18 * i));
241 else if (m_angle == 270.0)
242 m_nodeList[i]->SetPosition(m_position + wxPoint2DDouble(9 + 18 * i - m_width / 2, m_height / 2));
243 }
244 }
245 if (m_angle == 0.0)
246 m_nodeList[m_nodeList.size() - 1]->SetPosition(m_position + wxPoint2DDouble(m_width / 2, 0));
247 else if (m_angle == 90.0)
248 m_nodeList[m_nodeList.size() - 1]->SetPosition(m_position + wxPoint2DDouble(0, m_height / 2));
249 else if (m_angle == 180.0)
250 m_nodeList[m_nodeList.size() - 1]->SetPosition(m_position + wxPoint2DDouble(-m_width / 2, 0));
251 else if (m_angle == 270.0)
252 m_nodeList[m_nodeList.size() - 1]->SetPosition(m_position + wxPoint2DDouble(0, -m_height / 2));
253
254 SetPosition(m_position); // Update rect.
255}
void SetPosition(const wxPoint2DDouble position)
Set the element position and update the rectangle.
Definition Element.cpp:33

◆ 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 303 of file MathExpression.cpp.

304{
305 bool isTextureOK = true;
306 m_symbol.SetText(m_symbol.GetText());
307 for (auto& txtInput : m_gcTextInputVector)
308 {
309 txtInput->SetText(txtInput->GetText());
310 }
311 return isTextureOK;
312}
Here is the call graph for this function:

Member Data Documentation

◆ m_font

wxFont MathExpression::m_font
protected

Definition at line 77 of file MathExpression.h.

◆ m_fparser

MathExprParser MathExpression::m_fparser
protected

Definition at line 72 of file MathExpression.h.

◆ m_gcTextInputVector

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

Definition at line 78 of file MathExpression.h.

◆ m_inputValues

double* MathExpression::m_inputValues = nullptr
protected

Definition at line 76 of file MathExpression.h.

◆ m_mathExpression

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

Definition at line 73 of file MathExpression.h.

◆ m_maxSringSize

int MathExpression::m_maxSringSize = 0
protected

Definition at line 81 of file MathExpression.h.

◆ m_minimumSize

double MathExpression::m_minimumSize = 36.0
protected

Definition at line 82 of file MathExpression.h.

◆ m_symbol

GCText MathExpression::m_symbol
protected

Definition at line 79 of file MathExpression.h.

◆ m_symbolSize

wxSize MathExpression::m_symbolSize
protected

Definition at line 80 of file MathExpression.h.

◆ m_variables

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

Definition at line 74 of file MathExpression.h.

◆ m_variablesVector

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

Definition at line 75 of file MathExpression.h.


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