Salome HOME
scotch6.0.4 needs pthread... Quick and dirty solution to be improved
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingMemArray.cxx
index 91bef50389f19b4805a8f2d441ed511c2dd2aa22..4d4fed46d4d31e4f7bfb79a9b02beaff89b075f1 100644 (file)
 #include "InterpKernelAutoPtr.hxx"
 #include "InterpKernelExprParser.hxx"
 
+#include "InterpKernelAutoPtr.hxx"
+#include "InterpKernelGeo2DEdgeArcCircle.hxx"
+#include "InterpKernelAutoPtr.hxx"
+#include "InterpKernelGeo2DNode.hxx"
+#include "InterpKernelGeo2DEdgeLin.hxx"
+
 #include <set>
 #include <cmath>
 #include <limits>
@@ -631,7 +637,7 @@ void DataArray::CheckValueInRange(int ref, int value, const std::string& msg)
 
 /*!
  * This method checks that [\b start, \b end) is compliant with ref length \b value.
- * typicaly start in [0,\b value) and end in [0,\b value). If value==start and start==end, it is supported.
+ * typically start in [0,\b value) and end in [0,\b value). If value==start and start==end, it is supported.
  */
 void DataArray::CheckValueInRangeEx(int value, int start, int end, const std::string& msg)
 {
@@ -665,9 +671,9 @@ void DataArray::CheckClosingParInRange(int ref, int value, const std::string& ms
  *
  * The input \a sliceId should be an id in [0, \a nbOfSlices) that specifies the slice of work.
  *
- * \param [in] start - the start of the input slice of the whole work to perform splitted into slices.
- * \param [in] stop - the stop of the input slice of the whole work to perform splitted into slices.
- * \param [in] step - the step (that can be <0) of the input slice of the whole work to perform splitted into slices.
+ * \param [in] start - the start of the input slice of the whole work to perform split into slices.
+ * \param [in] stop - the stop of the input slice of the whole work to perform split into slices.
+ * \param [in] step - the step (that can be <0) of the input slice of the whole work to perform split into slices.
  * \param [in] sliceId - the slice id considered
  * \param [in] nbOfSlices - the number of slices (typically the number of cores on which the work is expected to be sliced)
  * \param [out] startSlice - the start of the slice considered
@@ -1517,7 +1523,7 @@ void DataArrayDouble::recenterForMaxPrecision(double eps)
 
 /*!
  * Returns the maximal value and all its locations within \a this one-dimensional array.
- *  \param [out] tupleIds - a new instance of DataArrayInt containg indices of
+ *  \param [out] tupleIds - a new instance of DataArrayInt containing indices of
  *               tuples holding the maximal value. The caller is to delete it using
  *               decrRef() as it is no more needed.
  *  \return double - the maximal value among all values of \a this array.
@@ -1535,7 +1541,7 @@ double DataArrayDouble::getMaxValue2(DataArrayInt*& tupleIds) const
 
 /*!
  * Returns the minimal value and all its locations within \a this one-dimensional array.
- *  \param [out] tupleIds - a new instance of DataArrayInt containg indices of
+ *  \param [out] tupleIds - a new instance of DataArrayInt containing indices of
  *               tuples holding the minimal value. The caller is to delete it using
  *               decrRef() as it is no more needed.
  *  \return double - the minimal value among all values of \a this array.
@@ -1611,7 +1617,7 @@ double DataArrayDouble::norm2() const
 
 /*!
  * Returns the maximum norm of the vector defined by \a this array.
- * This method works even if the number of components is diferent from one.
+ * This method works even if the number of components is different from one.
  * If the number of elements in \a this is 0, -1. is returned.
  *  \return double - the value of the maximum norm, i.e.
  *          the maximal absolute value among values of \a this array (whatever its number of components).
@@ -1634,7 +1640,7 @@ double DataArrayDouble::normMax() const
 
 /*!
  * Returns the minimum norm (absolute value) of the vector defined by \a this array.
- * This method works even if the number of components is diferent from one.
+ * This method works even if the number of components is different from one.
  * If the number of elements in \a this is 0, std::numeric_limits<double>::max() is returned.
  *  \return double - the value of the minimum norm, i.e.
  *          the minimal absolute value among values of \a this array (whatever its number of components).
@@ -2489,6 +2495,38 @@ DataArrayDouble *DataArrayDouble::buildEuclidianDistanceDenseMatrixWith(const Da
   return ret.retn();
 }
 
+/*!
+ * This method expects that \a this stores 3 tuples containing 2 components each.
+ * Each of this tuples represent a point into 2D space.
+ * This method tries to find an arc of circle starting from first point (tuple) to 2nd and middle point (tuple) along 3nd and last point (tuple).
+ * If such arc of circle exists, the corresponding center, radius of circle is returned. And additionnaly the length of arc expressed with an \a ang output variable in ]0,2*pi[.
+ *  
+ *  \throw If \a this is not allocated.
+ *  \throw If \a this has not 3 tuples of 2 components
+ *  \throw If tuples/points in \a this are aligned
+ */
+void DataArrayDouble::asArcOfCircle(double center[2], double& radius, double& ang) const
+{
+  checkAllocated();
+  INTERP_KERNEL::QuadraticPlanarArcDetectionPrecision arcPrec(1e-14);
+  if(getNumberOfTuples()!=3 && getNumberOfComponents()!=2)
+    throw INTERP_KERNEL::Exception("DataArrayDouble::asArcCircle : this method expects");
+  const double *pt(begin());
+  MCAuto<INTERP_KERNEL::Node> n0(new INTERP_KERNEL::Node(pt[0],pt[1])),n1(new INTERP_KERNEL::Node(pt[2],pt[3])),n2(new INTERP_KERNEL::Node(pt[4],pt[5]));
+  {
+    INTERP_KERNEL::AutoCppPtr<INTERP_KERNEL::EdgeLin> e1(new INTERP_KERNEL::EdgeLin(n0,n2)),e2(new INTERP_KERNEL::EdgeLin(n2,n1));
+    INTERP_KERNEL::SegSegIntersector inters(*e1,*e2);
+    bool colinearity(inters.areColinears());
+    if(colinearity)
+      throw INTERP_KERNEL::Exception("DataArrayDouble::asArcOfCircle : 3 points in this have been detected as colinear !");
+  }
+  INTERP_KERNEL::AutoCppPtr<INTERP_KERNEL::EdgeArcCircle> ret(new INTERP_KERNEL::EdgeArcCircle(n0,n2,n1));
+  const double *c(ret->getCenter());
+  center[0]=c[0]; center[1]=c[1];
+  radius=ret->getRadius();
+  ang=ret->getAngle();
+}
+
 /*!
  * Sorts value within every tuple of \a this array.
  *  \param [in] asc - if \a true, the values are sorted in ascending order, else,
@@ -3005,7 +3043,7 @@ DataArrayDoubleIterator *DataArrayDouble::iterator()
 }
 
 /*!
- * Returns a new DataArrayInt contating indices of tuples of \a this one-dimensional
+ * Returns a new DataArrayInt containing indices of tuples of \a this one-dimensional
  * array whose values are within a given range. Textual data is not copied.
  *  \param [in] vmin - a lowest acceptable value (included).
  *  \param [in] vmax - a greatest acceptable value (included).
@@ -3036,7 +3074,7 @@ DataArrayInt *DataArrayDouble::findIdsInRange(double vmin, double vmax) const
 }
 
 /*!
- * Returns a new DataArrayInt contating indices of tuples of \a this one-dimensional
+ * Returns a new DataArrayInt containing indices of tuples of \a this one-dimensional
  * array whose values are not within a given range. Textual data is not copied.
  *  \param [in] vmin - a lowest not acceptable value (excluded).
  *  \param [in] vmax - a greatest not acceptable value (excluded).
@@ -3955,7 +3993,7 @@ bool DataArrayInt::isRange(int& strt, int& sttoopp, int& stteepp) const
  *  \throw If any value of \a this can't be used as a valid index for 
  *         [\a indArrBg, \a indArrEnd).
  *
- *  \sa changeValue
+ *  \sa changeValue, findIdForEach
  */
 void DataArrayInt::transformWithIndArr(const int *indArrBg, const int *indArrEnd)
 {
@@ -3970,7 +4008,7 @@ void DataArrayInt::transformWithIndArr(const int *indArrBg, const int *indArrEnd
       else
         {
           std::ostringstream oss; oss << "DataArrayInt::transformWithIndArr : error on tuple #" << i << " of this value is " << *pt << ", should be in [0," << nbElemsIn << ") !";
-          throw INTERP_KERNEL::Exception(oss.str().c_str());
+          throw INTERP_KERNEL::Exception(oss.str());
         }
     }
   this->declareAsNew();
@@ -3981,7 +4019,7 @@ void DataArrayInt::transformWithIndArr(const MapKeyVal<int>& m)
   this->checkAllocated();
   if(this->getNumberOfComponents()!=1)
     throw INTERP_KERNEL::Exception("Call transformWithIndArr method on DataArrayInt with only one component, you can call 'rearrange' method before !");
-  const std::map<int,int> dat(m.data());
+  const std::map<int,int>& dat(m.data());
   int nbOfTuples(getNumberOfTuples()),*pt(getPointer());
   for(int i=0;i<nbOfTuples;i++,pt++)
     {
@@ -3991,7 +4029,7 @@ void DataArrayInt::transformWithIndArr(const MapKeyVal<int>& m)
       else
         {
           std::ostringstream oss; oss << "DataArrayInt::transformWithIndArr : error on tuple #" << i << " of this value is " << *pt << " not in map !";
-          throw INTERP_KERNEL::Exception(oss.str().c_str());
+          throw INTERP_KERNEL::Exception(oss.str());
         }
     }
   this->declareAsNew();
@@ -4169,14 +4207,16 @@ DataArrayInt *DataArrayInt::invertArrayN2O2O2N(int oldNbOfElem) const
  *  \sa invertArrayN2O2O2N
  *  \endif
  */
-MCAuto< MapKeyVal<int> > DataArrayInt::invertArrayN2O2O2NOptimized() const
+MCAuto< MapKeyVal<int> > DataArrayInt32::invertArrayN2O2O2NOptimized() const
 {
   checkAllocated();
+  if(getNumberOfComponents()!=1)
+    throw INTERP_KERNEL::Exception("DataArrayInt32::invertArrayN2O2O2NOptimized : single component expected !");
   MCAuto< MapKeyVal<int> > ret(MapKeyVal<int>::New());
   std::map<int,int>& m(ret->data());
   const int *new2Old(begin());
-  int nbOfNewElems(this->getNumberOfTuples());
-  for(int i=0;i<nbOfNewElems;i++)
+  std::size_t nbOfNewElems(this->getNumberOfTuples());
+  for(std::size_t i=0;i<nbOfNewElems;i++)
     {
       int v(new2Old[i]);
       m[v]=i;
@@ -4212,17 +4252,25 @@ DataArrayInt *DataArrayInt::checkAndPreparePermutation() const
 }
 
 /*!
- * This method tries to find the permutation to apply to the first input \a ids1 to obtain the same array (without considering strings informations) the second
+ * This method tries to find the permutation to apply to the first input \a ids1 to obtain the same array (without considering strings information) the second
  * input array \a ids2.
  * \a ids1 and \a ids2 are expected to be both a list of ids (both with number of components equal to one) not sorted and with values that can be negative.
  * This method will throw an exception is no such permutation array can be obtained. It is typically the case if there is some ids in \a ids1 not in \a ids2 or
  * inversely.
- * In case of success (no throw) : \c ids1->renumber(ret)->isEqual(ids2) where \a ret is the return of this method.
+ * In case of success both assertion will be true (no throw) :
+ * \c ids1->renumber(ret)->isEqual(ids2) where \a ret is the return of this method.
+ * \c ret->transformWithIndArr(ids2)->isEqual(ids1)
+ *
+ * \b Example:
+ * - \a ids1 : [3,1,103,4,6,10,-7,205]
+ * - \a ids2 : [-7,1,205,10,6,3,103,4]
+ * - \a return is : [5,1,6,7,4,3,0,2] because ids2[5]==ids1[0], ids2[1]==ids1[1], ids2[6]==ids1[2]...
  *
  * \return DataArrayInt * - a new instance of DataArrayInt. The caller is to delete this
  *          array using decrRef() as it is no more needed.
  * \throw If either ids1 or ids2 is null not allocated or not with one components.
  * 
+ * \sa DataArrayInt32::findIdForEach
  */
 DataArrayInt *DataArrayInt::FindPermutationFromFirstToSecond(const DataArrayInt *ids1, const DataArrayInt *ids2)
 {
@@ -4654,6 +4702,47 @@ DataArrayInt *DataArrayInt::findIdsEqualTuple(const int *tupleBg, const int *tup
   return ret.retn();
 }
 
+/*!
+ * This method finds for each element \a ELT in [valsBg,valsEnd) elements in \a this equal to it. Associated to ELT
+ * this method will return the tuple id of last element found. If there is no element in \a this equal to ELT
+ * an exception will be thrown.
+ *
+ * In case of success this[ret]==vals. Samely ret->transformWithIndArr(this->begin(),this->end())==vals.
+ * Where \a vals is the [valsBg,valsEnd) array and \a ret the array returned by this method.
+ * This method can be seen as an extension of FindPermutationFromFirstToSecond.
+ * <br>
+ * \b Example: <br>
+ * - \a this: [17,27,2,10,-4,3,12,27,16]
+ * - \a val : [3,16,-4,27,17]
+ * - result: [5,8,4,7,0]
+ *
+ * \return - An array of size std::distance(valsBg,valsEnd)
+ *
+ * \sa DataArrayInt32::FindPermutationFromFirstToSecond
+ */
+MCAuto<DataArrayInt32> DataArrayInt32::findIdForEach(const int *valsBg, const int *valsEnd) const
+{
+  MCAuto<DataArrayInt32> ret(DataArrayInt32::New());
+  std::size_t nbOfTuplesOut(std::distance(valsBg,valsEnd));
+  ret->alloc(nbOfTuplesOut,1);
+  MCAuto< MapKeyVal<int> > zeMap(invertArrayN2O2O2NOptimized());
+  const std::map<int,int>& dat(zeMap->data());
+  int *ptToFeed(ret->getPointer());
+  for(const int *pt=valsBg;pt!=valsEnd;pt++)
+    {
+      std::map<int,int>::const_iterator it(dat.find(*pt));
+      if(it!=dat.end())
+        *ptToFeed++=(*it).second;
+      else
+        {
+          std::ostringstream oss; oss << "DataArrayInt32::findIdForEach : error for element at place " << std::distance(valsBg,pt);
+          oss << " of input array value is " << *pt << " which is not in this !";
+          throw INTERP_KERNEL::Exception(oss.str());
+        }
+    }
+  return ret;
+}
+
 /*!
  * Assigns \a newValue to all elements holding \a oldValue within \a this
  * one-dimensional array.
@@ -5567,7 +5656,7 @@ DataArrayInt *DataArrayInt::BuildListOfSwitchedOff(const std::vector<bool>& v)
 }
 
 /*!
- * This method allows to put a vector of vector of integer into a more compact data stucture (skyline). 
+ * This method allows to put a vector of vector of integer into a more compact data structure (skyline). 
  * This method is not available into python because no available optimized data structure available to map std::vector< std::vector<int> >.
  *
  * \param [in] v the input data structure to be translate into skyline format.
@@ -5791,7 +5880,7 @@ DataArrayInt *DataArrayInt::buildUniqueNotSorted() const
  * "MEDCouplingUMesh::buildDescendingConnectivity" and
  * \ref MEDCoupling::MEDCouplingUMesh::getNodalConnectivityIndex
  * "MEDCouplingUMesh::getNodalConnectivityIndex" etc.
- * This method preforms the reverse operation of DataArrayInt::computeOffsetsFull.
+ * This method performs the reverse operation of DataArrayInt::computeOffsetsFull.
  *  \return DataArrayInt * - a new instance of DataArrayInt, whose number of tuples
  *          equals to \a this->getNumberOfComponents() - 1, and number of components is 1.
  *          The caller is to delete this array using decrRef() as it is no more needed. 
@@ -5867,7 +5956,7 @@ void DataArrayInt::computeOffsets()
  * components remains the same and number of tuples is inceamented by one.<br>
  * This method is useful for allToAllV in MPI with contiguous policy. This method
  * differs from computeOffsets() in that the number of tuples is changed by this one.
- * This method preforms the reverse operation of DataArrayInt::deltaShiftIndex.
+ * This method performs the reverse operation of DataArrayInt::deltaShiftIndex.
  *  \throw If \a this is not allocated.
  *  \throw If \a this->getNumberOfComponents() != 1.
  *