From: fps Date: Tue, 27 Aug 2013 15:56:27 +0000 (+0000) Subject: add steps X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=d6721a3981dcc1982df640bcb4aa12bfc739d0e5;p=modules%2Fgeom.git add steps add unit tests --- diff --git a/src/XAO/BooleanField.cxx b/src/XAO/BooleanField.cxx index 5142fac32..5bfdd9cc6 100644 --- a/src/XAO/BooleanField.cxx +++ b/src/XAO/BooleanField.cxx @@ -25,36 +25,33 @@ using namespace XAO; -BooleanField::BooleanField(const XAO::Dimension dimension, const int nbComponents) +BooleanField::BooleanField(const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents) + : Field("", dimension, nbElements, nbComponents) { - m_dimension = dimension; - m_nbComponents = nbComponents; } -BooleanField::BooleanField(const std::string name, const XAO::Dimension dimension, const int nbComponents) +BooleanField::BooleanField(const std::string& name, const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents) + : Field(name, dimension, nbElements, nbComponents) { - m_dimension = dimension; - m_nbComponents = nbComponents; - m_name = name; } -BooleanStep* BooleanField::addStep(const int step) +BooleanStep* BooleanField::addStep(const int& step) { BooleanStep* bstep = new BooleanStep(step, m_nbElements, m_nbComponents); m_steps.push_back(bstep); return bstep; } -BooleanStep* BooleanField::addStep(const int step, const int stamp) +BooleanStep* BooleanField::addStep(const int& step, const int& stamp) { BooleanStep* bstep = new BooleanStep(step, stamp, m_nbElements, m_nbComponents); m_steps.push_back(bstep); return bstep; } -BooleanStep* BooleanField::getStep(const int index) +BooleanStep* BooleanField::getStep(const int& index) { if (index < m_steps.size()) - return m_steps[index]; + return (BooleanStep*)m_steps[index]; throw SALOME_Exception("IndexOutOfRange"); } diff --git a/src/XAO/BooleanField.hxx b/src/XAO/BooleanField.hxx index 45723be9d..358a6803b 100644 --- a/src/XAO/BooleanField.hxx +++ b/src/XAO/BooleanField.hxx @@ -34,17 +34,17 @@ namespace XAO class BooleanField : public Field { public: - BooleanField(const XAO::Dimension dimension, const int nbComponents); - BooleanField(const std::string name, const XAO::Dimension dimension, const int nbComponents); + BooleanField(const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents); + BooleanField(const std::string& name, const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents); virtual const XAO::Type getType() { return XAO::BOOLEAN; } - BooleanStep* addStep(const int step); - BooleanStep* addStep(const int step, const int stamp); - BooleanStep* getStep(const int index); + BooleanStep* addStep(const int& step); + BooleanStep* addStep(const int& step, const int& stamp); + BooleanStep* getStep(const int& index); - private: - std::vector m_steps; +// private: +// std::vector m_steps; }; } diff --git a/src/XAO/BooleanStep.cxx b/src/XAO/BooleanStep.cxx index dc1a626c9..3a351495e 100644 --- a/src/XAO/BooleanStep.cxx +++ b/src/XAO/BooleanStep.cxx @@ -48,8 +48,11 @@ void BooleanStep::Init(const int& step, const int& stamp, const int& nbElements, m_values.reserve(m_nbElements); for (int i = 0; i < m_nbElements; ++i) { + std::vector row; + row.reserve(m_nbComponents); for (int j = 0; j < m_nbComponents; ++j) - m_values[i][j] = false; + row.push_back(false); + m_values.push_back(row); } } @@ -78,7 +81,7 @@ std::vector BooleanStep::getElement(const int& element) std::vector BooleanStep::getComponent(const int& component) { - checkElement(component); + checkComponent(component); std::vector result; result.reserve(m_nbElements); diff --git a/src/XAO/DoubleField.cxx b/src/XAO/DoubleField.cxx new file mode 100644 index 000000000..6295a0ede --- /dev/null +++ b/src/XAO/DoubleField.cxx @@ -0,0 +1,57 @@ +// Copyright (C) 2013 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// Author : Frederic Pons (OpenCascade) + +#include "DoubleField.hxx" +#include "DoubleStep.hxx" + +#include + +using namespace XAO; + +DoubleField::DoubleField(const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents) + : Field("", dimension, nbElements, nbComponents) +{ +} + +DoubleField::DoubleField(const std::string& name, const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents) + : Field(name, dimension, nbElements, nbComponents) +{ +} + +DoubleStep* DoubleField::addStep(const int& step) +{ + DoubleStep* bstep = new DoubleStep(step, m_nbElements, m_nbComponents); + m_steps.push_back(bstep); + return bstep; +} + +DoubleStep* DoubleField::addStep(const int& step, const int& stamp) +{ + DoubleStep* bstep = new DoubleStep(step, stamp, m_nbElements, m_nbComponents); + m_steps.push_back(bstep); + return bstep; +} + +DoubleStep* DoubleField::getStep(const int& index) +{ + if (index < m_steps.size()) + return (DoubleStep*)m_steps[index]; + throw SALOME_Exception("IndexOutOfRange"); +} diff --git a/src/XAO/DoubleField.hxx b/src/XAO/DoubleField.hxx index 8c0657efa..332dc99fb 100644 --- a/src/XAO/DoubleField.hxx +++ b/src/XAO/DoubleField.hxx @@ -35,14 +35,17 @@ namespace XAO class DoubleField : public Field { public: - DoubleField(const XAO::Dimension dimension, const int nbComponents); - DoubleField(const std::string name, const XAO::Dimension dimension, const int nbComponents); + DoubleField(const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents); + DoubleField(const std::string& name, const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents); virtual const XAO::Type getType() { return XAO::DOUBLE; } - DoubleStep* addStep(const int step); - DoubleStep* addStep(const int step, const int stamp); - DoubleStep* getStep(const int index); + DoubleStep* addStep(const int& step); + DoubleStep* addStep(const int& step, const int& stamp); + DoubleStep* getStep(const int& index); + +// private: +// std::vector m_steps; }; } diff --git a/src/XAO/DoubleStep.cxx b/src/XAO/DoubleStep.cxx new file mode 100644 index 000000000..f58735c60 --- /dev/null +++ b/src/XAO/DoubleStep.cxx @@ -0,0 +1,147 @@ +// Copyright (C) 2013 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// Author : Frederic Pons (OpenCascade) + +#include "DoubleStep.hxx" +#include + +using namespace XAO; + +DoubleStep::DoubleStep(const int& nbElements, const int& nbComponents) +{ + Init(0, 0, nbElements, nbComponents); +} + +DoubleStep::DoubleStep(const int& step, const int& nbElements, const int& nbComponents) +{ + Init(step, 0, nbElements, nbComponents); +} + +DoubleStep::DoubleStep(const int& step, const int& stamp, const int& nbElements, const int& nbComponents) +{ + Init(step, stamp, nbElements, nbComponents); +} + +void DoubleStep::Init(const int& step, const int& stamp, const int& nbElements, const int& nbComponents) +{ + m_nbElements = nbElements; + m_nbComponents = nbComponents; + m_step = step; + m_stamp = stamp; + + m_values.reserve(m_nbElements); + for (int i = 0; i < m_nbElements; ++i) + { + std::vector row; + row.reserve(m_nbComponents); + for (int j = 0; j < m_nbComponents; ++j) + row.push_back(0); + m_values.push_back(row); + } +} + +std::vector DoubleStep::getValues() +{ + std::vector result; + result.reserve(m_nbElements * m_nbComponents); + + std::vector< std::vector >::iterator it; + for (it = m_values.begin(); it != m_values.end(); ++it) + { + std::vector eltValues = *it; + result.insert(result.end(), eltValues.begin(), eltValues.end()); + } + + return result; +} + +std::vector DoubleStep::getElement(const int& element) +{ + checkElement(element); + + std::vector result(m_values[element]); + return result; +} + +std::vector DoubleStep::getComponent(const int& component) +{ + checkComponent(component); + + std::vector result; + result.reserve(m_nbElements); + + std::vector< std::vector >::iterator it; + for (it = m_values.begin(); it != m_values.end(); ++it) + { + std::vector eltValues = *it; + result.push_back(eltValues[component]); + } + + return result; +} + +const double DoubleStep::getValue(const int& element, const int& component) +{ + checkElement(element); + checkComponent(component); + + return m_values[element][component]; +} + +void DoubleStep::setValues(const std::vector& values) +{ + if (values.size() != m_nbComponents * m_nbElements) + throw SALOME_Exception("bad size"); // TODO + + for (int i = 0; i < m_nbElements; ++i) + { + for (int j = 0; j < m_nbComponents; ++j) + { + m_values[i][j] = values[i * m_nbComponents + j]; + } + } +} + +void DoubleStep::setElements(const int& element, const std::vector& elements) +{ + checkElement(element); + if (elements.size() != m_nbComponents) + throw SALOME_Exception("bad size"); // TODO + + for (int i = 0; i < m_nbComponents; ++i) + m_values[element][i] = elements[i]; +} + +void DoubleStep::setComponents(const int& component, const std::vector& components) +{ + checkElement(component); + if (components.size() != m_nbElements) + throw SALOME_Exception("bad size"); // TODO + + for (int i = 0; i < m_nbElements; ++i) + m_values[i][component] = components[i]; +} + +void DoubleStep::setValue(const int& element, const int& component, const double& value) +{ + checkElement(element); + checkComponent(component); + + m_values[element][component] = value; +} diff --git a/src/XAO/DoubleStep.hxx b/src/XAO/DoubleStep.hxx index b66afd9be..c4d1db43b 100644 --- a/src/XAO/DoubleStep.hxx +++ b/src/XAO/DoubleStep.hxx @@ -25,28 +25,35 @@ #include #include "Xao.hxx" +#include "Step.hxx" namespace XAO { class DoubleStep : public Step { public: - DoubleStep(const int nbElements, const int nbComponents); - DoubleStep(const int step, const int nbElements, const int nbComponents); - DoubleStep(const int step, const int stamp, const int nbElements, const int nbComponents); + DoubleStep(const int& nbElements, const int& nbComponents); + DoubleStep(const int& step, const int& nbElements, const int& nbComponents); + DoubleStep(const int& step, const int& stamp, const int& nbElements, const int& nbComponents); virtual const XAO::Type getType() { return XAO::DOUBLE; } std::vector getValues(); - std::vector getElement(const int i); - std::vector getComponent(const int j); + std::vector getElement(const int& element); + std::vector getComponent(const int& component); - const double getValue(const int i, const int j); + const double getValue(const int& element, const int& component); - void setValues(std::vector values); - void setElements(const int i, std::vector elements); - void setComponents(const int j, std::vector components); - void setValue(const int i, const int j, const double value); + void setValues(const std::vector& values); + void setElements(const int& element, const std::vector& elements); + void setComponents(const int& component, const std::vector& components); + void setValue(const int& element, const int& component, const double& value); + + private: + void Init(const int& step, const int& stamp, const int& nbElements, const int& nbComponents); + + private: + std::vector< std::vector > m_values; }; } diff --git a/src/XAO/Field.cxx b/src/XAO/Field.cxx index c8394fc38..0e19d9e26 100644 --- a/src/XAO/Field.cxx +++ b/src/XAO/Field.cxx @@ -33,39 +33,67 @@ using namespace XAO; // ------------------------------------------------------- -Field* Field::createField(const XAO::Type type, const XAO::Dimension dimension, const int nbComponents) +Field::Field(const std::string& name, const XAO::Dimension& dimension, + const int& nbElements, const int& nbComponents) + : m_name(name), m_dimension(dimension), m_nbElements(nbElements), m_nbComponents(nbComponents) { - return createField(type, "", dimension, nbComponents); + m_components.reserve(nbComponents); + for (int i = 0; i < nbComponents; ++i) + m_components.push_back(""); } -Field* Field::createField(const XAO::Type type, const std::string name, const XAO::Dimension dimension, const int nbComponents) +Field* Field::createField(const XAO::Type& type, const XAO::Dimension& dimension, + const int& nbElements, const int& nbComponents) +{ + return createField(type, "", dimension, nbElements, nbComponents); +} + +Field* Field::createField(const XAO::Type& type, const std::string& name, const XAO::Dimension& dimension, + const int& nbElements, const int& nbComponents) { if (type == XAO::BOOLEAN) - return new BooleanField(name, dimension, nbComponents); + return new BooleanField(name, dimension, nbElements, nbComponents); if (type == XAO::INTEGER) - return new IntegerField(name, dimension, nbComponents); + return new IntegerField(name, dimension, nbElements, nbComponents); if (type == XAO::DOUBLE) - return new DoubleField(name, dimension, nbComponents); + return new DoubleField(name, dimension, nbElements, nbComponents); if (type == XAO::STRING) - return new StringField(name, dimension, nbComponents); + return new StringField(name, dimension, nbElements, nbComponents); throw SALOME_Exception("Bad Type"); } -const std::string Field::getComponentName(const int index) +const std::string Field::getComponentName(const int& index) { if (index < m_components.size()) return m_components[index]; - // TODO: throw - return ""; + throw SALOME_Exception("IndexOutOfRange component"); } -void Field::setComponentName(const int index, const std::string name) +void Field::setComponentName(const int& index, const std::string& name) { if (index < m_components.size()) { m_components[index] = name; + return; + } + + throw SALOME_Exception("IndexOutOfRange component"); +} + +bool Field::removeStep(Step* step) +{ + std::vector::iterator it = m_steps.begin(); + for (; it != m_steps.end(); ++it) + { + Step* current = *it; + if (step == current) + { + m_steps.erase(it); + return true; + } } - // TODO: throw + + return false; } diff --git a/src/XAO/Field.hxx b/src/XAO/Field.hxx index d33b9c806..f449277e3 100644 --- a/src/XAO/Field.hxx +++ b/src/XAO/Field.hxx @@ -38,13 +38,16 @@ namespace XAO class Field { protected: - Field(); + Field(const std::string& name, const XAO::Dimension& dimension, + const int& nbElements, const int& nbComponents); public: - static Field* createField(const XAO::Type type, const XAO::Dimension dimension, const int nbComponents); - static Field* createField(const XAO::Type type, const std::string name, const XAO::Dimension dimension, const int nbComponents); + static Field* createField(const XAO::Type& type, const XAO::Dimension& dimension, + const int& nbElements, const int& nbComponents); + static Field* createField(const XAO::Type& type, const std::string& name, const XAO::Dimension& dimension, + const int& nbElements, const int& nbComponents); - virtual ~Field(); + virtual ~Field() {}; /** * Gets the Type of the field. @@ -65,7 +68,7 @@ namespace XAO * Sets the name of the Field. * @param name the name to set. */ - void setName(std::string name) + void setName(const std::string& name) { m_name = name; } @@ -79,7 +82,14 @@ namespace XAO return m_dimension; } - int countElements(); + /** + * Gets the number of elements of each step. + * @return the number of elements of each step. + */ + const int countElements() + { + return m_nbElements; + } /** * Gets the number of components. @@ -87,41 +97,48 @@ namespace XAO */ const int countComponents() { - return m_components.size(); + return m_nbComponents; } - const int countValues(); + /** + * Gets the number of values for each step. + * @return the number of values for each step. + */ + const int countValues() + { + return m_nbElements * m_nbComponents; + } /** * Gets the number of the steps. * @return the number of steps. */ - const int countSteps() - { - return m_steps.size(); - } + const int countSteps() { return m_steps.size(); } /** * Gets the name of a component. * @param index the index of the component to get. * @return the name of the component for the given index. */ - const std::string getComponentName(const int index); + const std::string getComponentName(const int& index); /** * Sets the name of a component. * @param componentIndex the index of the component to set. * @param name the name to set. */ - void setComponentName(const int componentIndex, const std::string name); + void setComponentName(const int& componentIndex, const std::string& name); /** * Remove a step. * @param step the step to remove. - * @return + * @return true if the step has been removed. */ bool removeStep(Step* step); + private: + void checkComponent(const int& component); + protected: /** The name of the Field. */ std::string m_name; @@ -132,10 +149,11 @@ namespace XAO int m_nbComponents; /** The components of the field. */ std::vector m_components; - /** The steps. */ - std::map m_steps; - + /** The number of elements. */ int m_nbElements; + + /** The list of steps. */ + std::vector m_steps; }; } diff --git a/src/XAO/IntegerField.hxx b/src/XAO/IntegerField.hxx index 5946ad56a..9f40b0839 100644 --- a/src/XAO/IntegerField.hxx +++ b/src/XAO/IntegerField.hxx @@ -35,14 +35,17 @@ namespace XAO class IntegerField : public Field { public: - IntegerField(const XAO::Dimension dimension, const int nbComponents); - IntegerField(const std::string name, const XAO::Dimension dimension, const int nbComponents); + IntegerField(const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents); + IntegerField(const std::string& name, const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents); virtual const XAO::Type getType() { return XAO::INTEGER; } - IntegerStep* addStep(const int step); - IntegerStep* addStep(const int step, const int stamp); - IntegerStep* getStep(const int index); + IntegerStep* addStep(const int& step); + IntegerStep* addStep(const int& step, const int& stamp); + IntegerStep* getStep(const int& index); + +// private: +// std::vector m_steps; }; } diff --git a/src/XAO/IntegerStep.cxx b/src/XAO/IntegerStep.cxx new file mode 100644 index 000000000..a129bf629 --- /dev/null +++ b/src/XAO/IntegerStep.cxx @@ -0,0 +1,147 @@ +// Copyright (C) 2013 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// Author : Frederic Pons (OpenCascade) + +#include "IntegerStep.hxx" +#include + +using namespace XAO; + +IntegerStep::IntegerStep(const int& nbElements, const int& nbComponents) +{ + Init(0, 0, nbElements, nbComponents); +} + +IntegerStep::IntegerStep(const int& step, const int& nbElements, const int& nbComponents) +{ + Init(step, 0, nbElements, nbComponents); +} + +IntegerStep::IntegerStep(const int& step, const int& stamp, const int& nbElements, const int& nbComponents) +{ + Init(step, stamp, nbElements, nbComponents); +} + +void IntegerStep::Init(const int& step, const int& stamp, const int& nbElements, const int& nbComponents) +{ + m_nbElements = nbElements; + m_nbComponents = nbComponents; + m_step = step; + m_stamp = stamp; + + m_values.reserve(m_nbElements); + for (int i = 0; i < m_nbElements; ++i) + { + std::vector row; + row.reserve(m_nbComponents); + for (int j = 0; j < m_nbComponents; ++j) + row.push_back(0); + m_values.push_back(row); + } +} + +std::vector IntegerStep::getValues() +{ + std::vector result; + result.reserve(m_nbElements * m_nbComponents); + + std::vector< std::vector >::iterator it; + for (it = m_values.begin(); it != m_values.end(); ++it) + { + std::vector eltValues = *it; + result.insert(result.end(), eltValues.begin(), eltValues.end()); + } + + return result; +} + +std::vector IntegerStep::getElement(const int& element) +{ + checkElement(element); + + std::vector result(m_values[element]); + return result; +} + +std::vector IntegerStep::getComponent(const int& component) +{ + checkComponent(component); + + std::vector result; + result.reserve(m_nbElements); + + std::vector< std::vector >::iterator it; + for (it = m_values.begin(); it != m_values.end(); ++it) + { + std::vector eltValues = *it; + result.push_back(eltValues[component]); + } + + return result; +} + +const int IntegerStep::getValue(const int& element, const int& component) +{ + checkElement(element); + checkComponent(component); + + return m_values[element][component]; +} + +void IntegerStep::setValues(const std::vector& values) +{ + if (values.size() != m_nbComponents * m_nbElements) + throw SALOME_Exception("bad size"); // TODO + + for (int i = 0; i < m_nbElements; ++i) + { + for (int j = 0; j < m_nbComponents; ++j) + { + m_values[i][j] = values[i * m_nbComponents + j]; + } + } +} + +void IntegerStep::setElements(const int& element, const std::vector& elements) +{ + checkElement(element); + if (elements.size() != m_nbComponents) + throw SALOME_Exception("bad size"); // TODO + + for (int i = 0; i < m_nbComponents; ++i) + m_values[element][i] = elements[i]; +} + +void IntegerStep::setComponents(const int& component, const std::vector& components) +{ + checkElement(component); + if (components.size() != m_nbElements) + throw SALOME_Exception("bad size"); // TODO + + for (int i = 0; i < m_nbElements; ++i) + m_values[i][component] = components[i]; +} + +void IntegerStep::setValue(const int& element, const int& component, const int& value) +{ + checkElement(element); + checkComponent(component); + + m_values[element][component] = value; +} diff --git a/src/XAO/IntegerStep.hxx b/src/XAO/IntegerStep.hxx index eb967f15c..2cdfc8abb 100644 --- a/src/XAO/IntegerStep.hxx +++ b/src/XAO/IntegerStep.hxx @@ -25,28 +25,35 @@ #include #include "Xao.hxx" +#include "Step.hxx" namespace XAO { class IntegerStep : public Step { public: - IntegerStep(const int nbElements, const int nbComponents); - IntegerStep(const int step, const int nbElements, const int nbComponents); - IntegerStep(const int step, const int stamp, const int nbElements, const int nbComponents); + IntegerStep(const int& nbElements, const int& nbComponents); + IntegerStep(const int& step, const int& nbElements, const int& nbComponents); + IntegerStep(const int& step, const int& stamp, const int& nbElements, const int& nbComponents); virtual const XAO::Type getType() { return XAO::INTEGER; } std::vector getValues(); - std::vector getElement(const int i); - std::vector getComponent(const int j); + std::vector getElement(const int& element); + std::vector getComponent(const int& component); - const int getValue(const int i, const int j); + const int getValue(const int& element, const int& component); - void setValues(std::vector values); - void setElements(const int i, std::vector elements); - void setComponents(const int j, std::vector components); - void setValue(const int i, const int j, const int value); + void setValues(const std::vector& values); + void setElements(const int& element, const std::vector& elements); + void setComponents(const int& element, const std::vector& components); + void setValue(const int& element, const int& component, const int& value); + + private: + void Init(const int& step, const int& stamp, const int& nbElements, const int& nbComponents); + + private: + std::vector< std::vector > m_values; }; } diff --git a/src/XAO/Makefile.am b/src/XAO/Makefile.am index 5b7e29f2c..c681dd255 100644 --- a/src/XAO/Makefile.am +++ b/src/XAO/Makefile.am @@ -53,8 +53,14 @@ dist_libXAO_la_SOURCES = \ Group.cxx \ Field.cxx \ BooleanField.cxx \ + IntegerField.cxx \ + DoubleField.cxx \ + StringField.cxx \ Step.cxx \ BooleanStep.cxx \ + IntegerStep.cxx \ + DoubleStep.cxx \ + StringStep.cxx \ XaoExporter.cxx diff --git a/src/XAO/Step.cxx b/src/XAO/Step.cxx index 74e72aa82..17ef7a318 100644 --- a/src/XAO/Step.cxx +++ b/src/XAO/Step.cxx @@ -29,7 +29,7 @@ using namespace XAO; -Step* Step::createStep(const XAO::Type& type, const int& nbElements, const int &nbComponents) +Step* Step::createStep(const XAO::Type& type, const int& nbElements, const int& nbComponents) { return createStep(type, 0, 0, nbElements, nbComponents); } diff --git a/src/XAO/Step.hxx b/src/XAO/Step.hxx index 4ff39dd37..e8dde48c0 100644 --- a/src/XAO/Step.hxx +++ b/src/XAO/Step.hxx @@ -29,7 +29,7 @@ namespace XAO class Step { protected: - Step(); + Step() {} public: static Step* createStep(const XAO::Type& type, const int& nbElements, const int& nbComponents); @@ -66,7 +66,7 @@ namespace XAO const int countElements() { return m_nbElements; } - virtual const int countValues(); + const int countValues() { return m_nbElements * m_nbComponents; } protected: void checkElement(const int& element); @@ -77,7 +77,6 @@ namespace XAO int m_stamp; int m_nbComponents; int m_nbElements; - }; } diff --git a/src/XAO/StringField.cxx b/src/XAO/StringField.cxx new file mode 100644 index 000000000..3f061c2f7 --- /dev/null +++ b/src/XAO/StringField.cxx @@ -0,0 +1,57 @@ +// Copyright (C) 2013 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// Author : Frederic Pons (OpenCascade) + +#include "StringField.hxx" +#include "StringStep.hxx" + +#include + +using namespace XAO; + +StringField::StringField(const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents) + : Field("", dimension, nbElements, nbComponents) +{ +} + +StringField::StringField(const std::string& name, const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents) + : Field(name, dimension, nbElements, nbComponents) +{ +} + +StringStep* StringField::addStep(const int& step) +{ + StringStep* bstep = new StringStep(step, m_nbElements, m_nbComponents); + m_steps.push_back(bstep); + return bstep; +} + +StringStep* StringField::addStep(const int& step, const int& stamp) +{ + StringStep* bstep = new StringStep(step, stamp, m_nbElements, m_nbComponents); + m_steps.push_back(bstep); + return bstep; +} + +StringStep* StringField::getStep(const int& index) +{ + if (index < m_steps.size()) + return (StringStep*)m_steps[index]; + throw SALOME_Exception("IndexOutOfRange"); +} diff --git a/src/XAO/StringField.hxx b/src/XAO/StringField.hxx index c1b09a403..db7de71ef 100644 --- a/src/XAO/StringField.hxx +++ b/src/XAO/StringField.hxx @@ -35,14 +35,17 @@ namespace XAO class StringField : public Field { public: - StringField(const XAO::Dimension dimension, const int nbComponents); - StringField(const std::string name, const XAO::Dimension dimension, const int nbComponents); + StringField(const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents); + StringField(const std::string& name, const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents); virtual const XAO::Type getType() { return XAO::STRING; } - StringStep* addStep(const int step); - StringStep* addStep(const int step, const int stamp); - StringStep* getStep(const int index); + StringStep* addStep(const int& step); + StringStep* addStep(const int& step, const int& stamp); + StringStep* getStep(const int& index); + +// private: +// std::vector m_steps; }; } diff --git a/src/XAO/StringStep.cxx b/src/XAO/StringStep.cxx new file mode 100644 index 000000000..c62427465 --- /dev/null +++ b/src/XAO/StringStep.cxx @@ -0,0 +1,147 @@ +// Copyright (C) 2013 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// Author : Frederic Pons (OpenCascade) + +#include "StringStep.hxx" +#include + +using namespace XAO; + +StringStep::StringStep(const int& nbElements, const int& nbComponents) +{ + Init(0, 0, nbElements, nbComponents); +} + +StringStep::StringStep(const int& step, const int& nbElements, const int& nbComponents) +{ + Init(step, 0, nbElements, nbComponents); +} + +StringStep::StringStep(const int& step, const int& stamp, const int& nbElements, const int& nbComponents) +{ + Init(step, stamp, nbElements, nbComponents); +} + +void StringStep::Init(const int& step, const int& stamp, const int& nbElements, const int& nbComponents) +{ + m_nbElements = nbElements; + m_nbComponents = nbComponents; + m_step = step; + m_stamp = stamp; + + m_values.reserve(m_nbElements); + for (int i = 0; i < m_nbElements; ++i) + { + std::vector row; + row.reserve(m_nbComponents); + for (int j = 0; j < m_nbComponents; ++j) + row.push_back(""); + m_values.push_back(row); + } +} + +std::vector StringStep::getValues() +{ + std::vector result; + result.reserve(m_nbElements * m_nbComponents); + + std::vector< std::vector >::iterator it; + for (it = m_values.begin(); it != m_values.end(); ++it) + { + std::vector eltValues = *it; + result.insert(result.end(), eltValues.begin(), eltValues.end()); + } + + return result; +} + +std::vector StringStep::getElement(const int& element) +{ + checkElement(element); + + std::vector result(m_values[element]); + return result; +} + +std::vector StringStep::getComponent(const int& component) +{ + checkComponent(component); + + std::vector result; + result.reserve(m_nbElements); + + std::vector< std::vector >::iterator it; + for (it = m_values.begin(); it != m_values.end(); ++it) + { + std::vector eltValues = *it; + result.push_back(eltValues[component]); + } + + return result; +} + +const std::string StringStep::getValue(const int& element, const int& component) +{ + checkElement(element); + checkComponent(component); + + return m_values[element][component]; +} + +void StringStep::setValues(const std::vector& values) +{ + if (values.size() != m_nbComponents * m_nbElements) + throw SALOME_Exception("bad size"); // TODO + + for (int i = 0; i < m_nbElements; ++i) + { + for (int j = 0; j < m_nbComponents; ++j) + { + m_values[i][j] = values[i * m_nbComponents + j]; + } + } +} + +void StringStep::setElements(const int& element, const std::vector& elements) +{ + checkElement(element); + if (elements.size() != m_nbComponents) + throw SALOME_Exception("bad size"); // TODO + + for (int i = 0; i < m_nbComponents; ++i) + m_values[element][i] = elements[i]; +} + +void StringStep::setComponents(const int& component, const std::vector& components) +{ + checkElement(component); + if (components.size() != m_nbElements) + throw SALOME_Exception("bad size"); // TODO + + for (int i = 0; i < m_nbElements; ++i) + m_values[i][component] = components[i]; +} + +void StringStep::setValue(const int& element, const int& component, const std::string& value) +{ + checkElement(element); + checkComponent(component); + + m_values[element][component] = value; +} diff --git a/src/XAO/StringStep.hxx b/src/XAO/StringStep.hxx index cd0b68cd7..9b8a9250e 100644 --- a/src/XAO/StringStep.hxx +++ b/src/XAO/StringStep.hxx @@ -26,28 +26,35 @@ #include #include "Xao.hxx" +#include "Step.hxx" namespace XAO { class StringStep : public Step { public: - StringStep(const int nbElements, const int nbComponents); - StringStep(const int step, const int nbElements, const int nbComponents); - StringStep(const int step, const int stamp, const int nbElements, const int nbComponents); + StringStep(const int& nbElements, const int& nbComponents); + StringStep(const int& step, const int& nbElements, const int& nbComponents); + StringStep(const int& step, const int& stamp, const int& nbElements, const int& nbComponents); virtual const XAO::Type getType() { return XAO::STRING; } std::vector getValues(); - std::vector getElement(const int i); - std::vector getComponent(const int j); + std::vector getElement(const int& element); + std::vector getComponent(const int& component); - const std::string getValue(const int i, const int j); + const std::string getValue(const int& element, const int& component); - void setValues(std::vector values); - void setElements(const int i, std::vector elements); - void setComponents(const int j, std::vector components); - void setValue(const int i, const int j, const std::string value); + void setValues(const std::vector& values); + void setElements(const int& element, const std::vector& elements); + void setComponents(const int& component, const std::vector& components); + void setValue(const int& element, const int& component, const std::string& value); + + private: + void Init(const int& step, const int& stamp, const int& nbElements, const int& nbComponents); + + private: + std::vector< std::vector > m_values; }; } diff --git a/src/XAO/tests/FieldTest.cxx b/src/XAO/tests/FieldTest.cxx new file mode 100644 index 000000000..96880a797 --- /dev/null +++ b/src/XAO/tests/FieldTest.cxx @@ -0,0 +1,160 @@ +#include +#include + +#include "FieldTest.hxx" +#include "../Xao.hxx" +#include "../Field.hxx" +#include "../Step.hxx" +#include "../IntegerStep.hxx" + +using namespace XAO; + + +void FieldTest::setUp() +{ +} + +void FieldTest::tearDown() +{ +} + +void FieldTest::cleanUp() +{ +} + +void FieldTest::testField(XAO::Type type) +{ + Field* f = Field::createField(type, XAO::FACE, 10, 3); + + CPPUNIT_ASSERT(f->getName().size() == 0); + CPPUNIT_ASSERT(f->getType() == type); + CPPUNIT_ASSERT(f->getDimension() == XAO::FACE); + CPPUNIT_ASSERT(f->countComponents() == 3); + CPPUNIT_ASSERT(f->countElements() == 10); + CPPUNIT_ASSERT(f->countValues() == 30); + + f->setName("field1"); + CPPUNIT_ASSERT(f->getName() == "field1"); + + CPPUNIT_ASSERT(f->getComponentName(0).size() == 0); + f->setComponentName(0, "x"); + f->setComponentName(1, "y"); + f->setComponentName(2, "z"); + CPPUNIT_ASSERT(f->countComponents() == 3); + CPPUNIT_ASSERT(f->getComponentName(0) == "x"); + CPPUNIT_ASSERT(f->getComponentName(1) == "y"); + CPPUNIT_ASSERT(f->getComponentName(2) == "z"); + CPPUNIT_ASSERT_THROW(f->setComponentName(3, "a"), SALOME_Exception); + CPPUNIT_ASSERT_THROW(f->getComponentName(3), SALOME_Exception); +} + +void FieldTest::testBooleanField() +{ + testField(XAO::BOOLEAN); +} +void FieldTest::testIntegerField() +{ + testField(XAO::INTEGER); +} +void FieldTest::testDoubleField() +{ + testField(XAO::DOUBLE); +} +void FieldTest::testStringField() +{ + testField(XAO::STRING); +} + +void FieldTest::testStep(XAO::Type type) +{ + Step* step = Step::createStep(type, 5, 3); + CPPUNIT_ASSERT(step->getType() == type); + + CPPUNIT_ASSERT(step->getStep() == 0); + step->setStep(10); + CPPUNIT_ASSERT(step->getStep() == 10); + + CPPUNIT_ASSERT(step->getStamp() == 0); + step->setStamp(100); + CPPUNIT_ASSERT(step->getStamp() == 100); + + CPPUNIT_ASSERT(step->countElements() == 5); + CPPUNIT_ASSERT(step->countComponents() == 3); + CPPUNIT_ASSERT(step->countValues() == 15); +} + +void FieldTest::testBooleanStep() +{ + testStep(XAO::BOOLEAN); +} +void FieldTest::testIntegerStep() +{ + testStep(XAO::INTEGER); +} +void FieldTest::testDoubleStep() +{ + testStep(XAO::DOUBLE); +} +void FieldTest::testStringStep() +{ + testStep(XAO::STRING); +} + +void FieldTest::testIntegerStepValues() +{ + int nbComponents = 3; + int nbElements = 5; + + IntegerStep* istep = (IntegerStep*)Step::createStep(XAO::INTEGER, nbElements, nbComponents); + for (int i = 0; i < istep->countElements(); ++i) + { + for (int j = 0; j < istep->countComponents(); ++j) + istep->setValue(i, j, i*10 + j); + } + + CPPUNIT_ASSERT(istep->getValue(1, 2) == 12); + CPPUNIT_ASSERT_THROW(istep->getValue(nbElements, 2), SALOME_Exception); + CPPUNIT_ASSERT_THROW(istep->getValue(1, nbComponents), SALOME_Exception); + + // get all values + std::vector values; + values = istep->getValues(); + CPPUNIT_ASSERT(values.size() == nbElements * nbComponents); + for (int i = 0; i < nbElements; ++i) + { + for (int j = 0; j < nbComponents; ++j) + CPPUNIT_ASSERT(values[i*nbComponents+j] == 10*i+j); + } + + // get one element + values = istep->getElement(2); + CPPUNIT_ASSERT_THROW(istep->getElement(nbElements), SALOME_Exception); + CPPUNIT_ASSERT(values.size() == nbComponents); + for (int i = 0; i < nbComponents; ++i) + CPPUNIT_ASSERT(values[i] == 20+i); + + // get one component + values = istep->getComponent(1); + CPPUNIT_ASSERT_THROW(istep->getComponent(nbComponents), SALOME_Exception); + CPPUNIT_ASSERT(values.size() == nbElements); + for (int i = 0; i < nbElements; ++i) + CPPUNIT_ASSERT(values[i] == 10*i+1); + + // set one element + std::vector newEltValues; + newEltValues.push_back(1); + CPPUNIT_ASSERT_THROW(istep->setElements(2, newEltValues), SALOME_Exception); + for (int i = 1; i < nbComponents; ++i) + newEltValues.push_back(1); + istep->setElements(2, newEltValues); + + // set one component + std::vector newCompValues; + newCompValues.push_back(100); + CPPUNIT_ASSERT_THROW(istep->setComponents(1, newCompValues), SALOME_Exception); + for (int i = 1; i < nbElements; ++i) + newCompValues.push_back(100); + istep->setComponents(1, newCompValues); +} + + diff --git a/src/XAO/tests/FieldTest.hxx b/src/XAO/tests/FieldTest.hxx new file mode 100644 index 000000000..5a5d13bc5 --- /dev/null +++ b/src/XAO/tests/FieldTest.hxx @@ -0,0 +1,45 @@ +#ifndef __XAO_FIELD_TEST_HXX__ +#define __XAO_FIELD_TEST_HXX__ + +#include + +#include "../Xao.hxx" + +namespace XAO +{ + class FieldTest: public CppUnit::TestFixture + { + CPPUNIT_TEST_SUITE(FieldTest); + CPPUNIT_TEST(testBooleanField); + CPPUNIT_TEST(testDoubleField); + CPPUNIT_TEST(testIntegerField); + CPPUNIT_TEST(testStringField); + CPPUNIT_TEST(testBooleanStep); + CPPUNIT_TEST(testIntegerStep); + CPPUNIT_TEST(testDoubleStep); + CPPUNIT_TEST(testStringStep); + CPPUNIT_TEST(testIntegerStepValues); + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp(); + void tearDown(); + void cleanUp(); + + void testField(XAO::Type type); + void testBooleanField(); + void testIntegerField(); + void testDoubleField(); + void testStringField(); + + void testStep(XAO::Type type); + void testBooleanStep(); + void testIntegerStep(); + void testDoubleStep(); + void testStringStep(); + + void testIntegerStepValues(); + }; +} + +#endif // __XAO_FIELD_TEST_HXX__ diff --git a/src/XAO/tests/ImportExportTest.hxx b/src/XAO/tests/ImportExportTest.hxx index 6f7926808..317760cab 100644 --- a/src/XAO/tests/ImportExportTest.hxx +++ b/src/XAO/tests/ImportExportTest.hxx @@ -1,5 +1,5 @@ -#ifndef __BUILDING_IMPORT_TEST_HXX__ -#define __BUILDING_IMPORT_TEST_HXX__ +#ifndef __XAO_IMPORT_TEST_HXX__ +#define __XAO_IMPORT_TEST_HXX__ #include @@ -32,4 +32,4 @@ namespace XAO }; } -#endif +#endif // __XAO_IMPORT_TEST_HXX__ diff --git a/src/XAO/tests/Makefile.am b/src/XAO/tests/Makefile.am index 4533db12a..19d677123 100755 --- a/src/XAO/tests/Makefile.am +++ b/src/XAO/tests/Makefile.am @@ -39,6 +39,7 @@ TestXAO_LDFLAGS = \ ../libXAO.la dist_TestXAO_SOURCES = \ + FieldTest.cxx \ ImportExportTest.cxx \ XAOTests.cxx diff --git a/src/XAO/tests/XAOTests.cxx b/src/XAO/tests/XAOTests.cxx index daa59ca2d..a95cf5d0b 100644 --- a/src/XAO/tests/XAOTests.cxx +++ b/src/XAO/tests/XAOTests.cxx @@ -1,5 +1,7 @@ +#include "FieldTest.hxx" #include "ImportExportTest.hxx" +CPPUNIT_TEST_SUITE_REGISTRATION(XAO::FieldTest); CPPUNIT_TEST_SUITE_REGISTRATION(XAO::ImportExportTest); #include "MainTest.hxx"