Power System Platform  2026w10a-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)
 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 313 of file MathExpression.cpp.

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

◆ CalculateBlockSize()

void MathExpression::CalculateBlockSize ( double  numInNodes)
protected

Definition at line 341 of file MathExpression.cpp.

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

◆ 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(m_elementID);
266 *copy = *this;
267 copy->m_gcTextInputVector.clear();
268 for (auto it = m_gcTextInputVector.begin(), itEnd = m_gcTextInputVector.end(); it != itEnd; ++it) {
269 copy->m_gcTextInputVector.push_back((*it)->GetCopy());
270 }
271 return copy;
272}
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 382 of file MathExpression.cpp.

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

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

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

◆ RemoveInNode()

void MathExpression::RemoveInNode ( )

Definition at line 320 of file MathExpression.cpp.

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

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

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

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

◆ ShowForm()

bool MathExpression::ShowForm ( wxWindow *  parent,
Element element 
)
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 274 of file MathExpression.cpp.

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

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

360{
361 bool isTextureOK = true;
362 m_symbol.SetText(m_symbol.GetText());
363 //if (!m_symbol.IsTextureOK()) isTextureOK = false;
364 for (auto it = m_gcTextInputVector.begin(), itEnd = m_gcTextInputVector.end(); it != itEnd; ++it) {
365 (*it)->SetText((*it)->GetText());
366 //if (!(*it)->IsTextureOK()) isTextureOK = false;
367 }
368 return isTextureOK;
369}
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: