File: Utils.tpp

package info (click to toggle)
octave-level-set 0.3.1~git.2019.04.13-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 884 kB
  • sloc: cpp: 1,840; makefile: 34; sh: 20
file content (83 lines) | stat: -rw-r--r-- 2,602 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
    GNU Octave level-set package.
    Copyright (C) 2014  Daniel Kraft <d@domob.eu>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/* Template implementations of Utils.hpp.  */

/**
 * Check and possibly return dimensions of an array.  If d1 and d2 are
 * both given as positive numbers, check that the array has the given size.
 * If one of them is negative (-1), return the dimension along this
 * dimension and check only the other one.  Throw if mismatch.
 * @param var The array to check.
 * @param d1 Row dimension.
 * @param d2 Column dimension.
 * @return Possibly the -1 dimension size.
 */
template<typename Arr>
  unsigned
  getDimensionReal (const Arr& var, const std::string& name,
                    int d1, int d2)
{
  const dim_vector size = var.dims ();
  if (size.length () > 2)
    throw std::runtime_error ("Expected at most two dimensions for "
                              + name + ".");
  assert (size.length () == 2);

  const int expected[2] = {d1, d2};
  unsigned res = 0;
  bool foundRes = false;
  for (unsigned i = 0; i < 2; ++i)
    if (expected[i] < 0)
      {
        if (foundRes)
          throw std::runtime_error ("Two dimensions unknown for " + name + ".");

        foundRes = true;
        res = size(i);
      }
    else if (expected[i] != size(i))
      {
        std::ostringstream msg;
        msg << "Dimension mismatch for " << name
            << " in dimension " << (i + 1) << ": ";
        msg << "expected " << expected[i] << ", got " << size(i);
        throw std::runtime_error (msg.str ());
      }

  return res;
}

/**
 * Convert a generic container to an Octave ColumnVector.
 * @param cont Generic container (e. g., std::vector<double>).
 * @return ColumnVector with the container's content.
 */
template<typename C>
  ColumnVector
  convertToColumnVector (const C& cont)
{
  ColumnVector res(cont.size ());

  unsigned ind = 0;
  for (const auto& el : cont)
    res(ind++) = el;
  assert (ind == cont.size ());
  
  return res;
}