File: types.h

package info (click to toggle)
step 4%3A25.08.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,628 kB
  • sloc: cpp: 16,808; xml: 762; python: 380; javascript: 93; sh: 39; makefile: 3
file content (159 lines) | stat: -rw-r--r-- 4,299 bytes parent folder | download | duplicates (2)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
    SPDX-FileCopyrightText: 2007 Vladimir Kuznetsov <ks.vladimir@gmail.com>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

/** \file types.h
 *  \brief Type to and from string conversion helpers
 */

#ifndef STEPCORE_TYPES_H
#define STEPCORE_TYPES_H

#include "object.h"
#include "vector.h"
#define EIGEN_USE_NEW_STDVECTOR
#include <Eigen/StdVector>
#include <Eigen/Sparse>

#include <QByteArray>

namespace StepCore {

typedef Eigen::SparseMatrix<double> SparseColMatrix;
typedef Eigen::SparseMatrix<double,Eigen::RowMajor> SparseRowMatrix;
// a sparse matrix with efficient write facilities
typedef Eigen::SparseMatrix<double,Eigen::RowMajor> DynSparseRowMatrix;
typedef Eigen::Map<Eigen::VectorXd> MappedVector;

///////////////// Color
struct Color
{
    Color() {}
    Color(unsigned int v): value(v) {}
    operator unsigned int() const { return value; }
    unsigned int value;
};

template<> inline QString typeToString(const Color& v)
{
    return QStringLiteral("#%1").arg(v, 8, 16, QLatin1Char('0'));
}

template<> inline Color stringToType(const QString& s, bool *ok)
{
    if(ok) *ok = false;
    QString s1 = s.trimmed();
    if(!s1.startsWith('#')) return Color(0);
    s1 = s1.mid(1);
    return Color(s1.toUInt(ok, 16));
}

///////////////// QByteArray
template<> inline QString typeToString(const QByteArray& v)
{
    return QString::fromLatin1(v.toBase64());
}

template<> inline QByteArray stringToType(const QString& s, bool *ok)
{
    if(ok) *ok = true;
    return QByteArray::fromBase64(s.toLatin1());
}

///////////////// Vector2d
template<> inline QString typeToString(const Vector2d& v)
{
    return QStringLiteral("(%1,%2)").arg(v[0]).arg(v[1]);
}

template<> inline Vector2d stringToType(const QString& s, bool *ok)
{
    // XXX: Write something better
    if(ok) *ok = false;
    QString s1 = s.trimmed();
    if(!s1.startsWith('(') || !s1.endsWith(')')) return Vector2d();
    s1 = s1.mid(1, s1.size()-2);    
    bool ok1, ok2;
    StepCore::Vector2d v(s1.section(',',0,0).toDouble(&ok1), s1.section(',',1,1).toDouble(&ok2));
    if(!ok1 || !ok2) return v;
    if(ok) *ok = true;
    return v;
}

///////////////// Vector2i
template<> inline QString typeToString(const Vector2i& v)
{
    return QStringLiteral("(%1,%2)").arg(v[0]).arg(v[1]);
}

template<> inline Vector2i stringToType(const QString& s, bool *ok)
{
    // XXX: Write something better
    if(ok) *ok = false;
    QString s1 = s.trimmed();
    if(!s1.startsWith('(') || !s1.endsWith(')')) return Vector2i();
    s1 = s1.mid(1, s1.size()-2);    
    bool ok1, ok2;
    StepCore::Vector2i v(s1.section(',',0,0).toInt(&ok1), s1.section(',',1,1).toInt(&ok2));
    if(!ok1 || !ok2) return v;
    if(ok) *ok = true;
    return v;
}

///////////////// Vector2dList
typedef std::vector<Vector2d, Eigen::aligned_allocator<Vector2d> > Vector2dList;

template<> inline QString typeToString(const Vector2dList& v)
{
    QString ret;
    for(Vector2dList::const_iterator it = v.begin(); it != v.end(); ++it) {
        if(!ret.isEmpty()) ret += QLatin1String(",");
        ret += QStringLiteral("(%1,%2)").arg((*it)[0]).arg((*it)[1]);
    }
    return ret;
}

template<> inline Vector2dList stringToType(const QString& s, bool *ok)
{
    // XXX: Write something better
    Vector2dList ret;
    if(ok) *ok = false;
    QString s1 = s.trimmed();
    if (s1.isEmpty()) {
        if(ok) *ok = true;
        return ret;
    }
    //if(!s1.startsWith('(') || !s1.endsWith(')')) return ret;
    //s1 = s1.mid(1, s1.size()-2).trimmed();
    while(s1[0] == '(' && s1.contains(')')) {
        bool ok; double d1, d2;
        s1 = s1.mid(1);
        d1 = s1.section(',',0,0).toDouble(&ok);
        if(!ok) return Vector2dList();
        s1 = s1.section(',',1);
        d2 = s1.section(')',0,0).toDouble(&ok);
        if(!ok) return Vector2dList();
        s1 = s1.section(')',1).trimmed();
        ret.push_back(Vector2d(d1, d2));
        if(s1.isEmpty()) break;
        if(s1[0] != ',') return Vector2dList();
        s1 = s1.mid(1).trimmed();
    }
    if(!s1.isEmpty()) return Vector2dList();
    if(ok) *ok = true;
    return ret;
}

} // namespace StepCore

#ifdef STEPCORE_WITH_QT
Q_DECLARE_METATYPE(StepCore::Vector2dList)
Q_DECLARE_METATYPE(StepCore::Object*)
Q_DECLARE_METATYPE(StepCore::Color)
#endif


#endif