File: IJsonConvertible.cpp

package info (click to toggle)
open3d 0.19.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,496 kB
  • sloc: cpp: 206,543; python: 27,254; ansic: 8,356; javascript: 1,883; sh: 1,527; makefile: 259; xml: 69
file content (195 lines) | stat: -rw-r--r-- 6,441 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// ----------------------------------------------------------------------------
// -                        Open3D: www.open3d.org                            -
// ----------------------------------------------------------------------------
// Copyright (c) 2018-2024 www.open3d.org
// SPDX-License-Identifier: MIT
// ----------------------------------------------------------------------------

#include "open3d/utility/IJsonConvertible.h"

#include <json/json.h>

#include "tests/Tests.h"

namespace open3d {
namespace tests {

// ----------------------------------------------------------------------------
// Test the conversion of Eigen::Vector3d to and from JsonArray.
// ----------------------------------------------------------------------------
TEST(IJsonConvertible, EigenVector3dToFromJsonArray) {
    int loops = 10000;
    srand((unsigned int)time(0));
    for (int i = 0; i < loops; i++) {
        Eigen::Vector3d v3d = Eigen::Vector3d::Random();

        bool status = false;
        Json::Value json_value;
        Eigen::Vector3d ref;

        status = utility::IJsonConvertible::EigenVector3dToJsonArray(
                v3d, json_value);
        EXPECT_TRUE(status);

        status = utility::IJsonConvertible::EigenVector3dFromJsonArray(
                ref, json_value);
        EXPECT_TRUE(status);

        ExpectEQ(ref, v3d);
    }
}

// ----------------------------------------------------------------------------
// Test the conversion of Eigen::Vector4d to and from JsonArray.
// ----------------------------------------------------------------------------
TEST(IJsonConvertible, EigenVector4dToFromJsonArray) {
    int loops = 10000;
    srand((unsigned int)time(0));
    for (int i = 0; i < loops; i++) {
        Eigen::Vector4d v4d = Eigen::Vector4d::Random();

        bool status = false;
        Json::Value json_value;
        Eigen::Vector4d ref;

        status = utility::IJsonConvertible::EigenVector4dToJsonArray(
                v4d, json_value);
        EXPECT_TRUE(status);

        status = utility::IJsonConvertible::EigenVector4dFromJsonArray(
                ref, json_value);
        EXPECT_TRUE(status);

        ExpectEQ(ref, v4d);
    }
}

// ----------------------------------------------------------------------------
// Test the conversion of Eigen::Matrix3d to and from JsonArray.
// ----------------------------------------------------------------------------
TEST(IJsonConvertible, EigenMatrix3dToFromJsonArray) {
    int loops = 10000;
    srand((unsigned int)time(0));
    for (int i = 0; i < loops; i++) {
        Eigen::Matrix3d m3d = Eigen::Matrix3d::Random();

        bool status = false;
        Json::Value json_value;
        Eigen::Matrix3d ref;

        status = utility::IJsonConvertible::EigenMatrix3dToJsonArray(
                m3d, json_value);
        EXPECT_TRUE(status);

        status = utility::IJsonConvertible::EigenMatrix3dFromJsonArray(
                ref, json_value);
        EXPECT_TRUE(status);

        ExpectEQ(ref, m3d);
    }
}

// ----------------------------------------------------------------------------
// Test the conversion of Eigen::Matrix4d to and from JsonArray.
// ----------------------------------------------------------------------------
TEST(IJsonConvertible, EigenMatrix4dToFromJsonArray) {
    int loops = 10000;
    srand((unsigned int)time(0));
    for (int i = 0; i < loops; i++) {
        Eigen::Matrix4d m4d = Eigen::Matrix4d::Random();

        bool status = false;
        Json::Value json_value;
        Eigen::Matrix4d ref;

        status = utility::IJsonConvertible::EigenMatrix4dToJsonArray(
                m4d, json_value);
        EXPECT_TRUE(status);

        status = utility::IJsonConvertible::EigenMatrix4dFromJsonArray(
                ref, json_value);
        EXPECT_TRUE(status);

        ExpectEQ(ref, m4d);
    }
}

// ----------------------------------------------------------------------------
// Test the conversion of unaligned Eigen::Matrix4d to and from
// JsonArray.
// ----------------------------------------------------------------------------
TEST(IJsonConvertible, EigenMatrix4d_uToFromJsonArray) {
    int loops = 10000;
    srand((unsigned int)time(0));
    for (int i = 0; i < loops; i++) {
        Eigen::Matrix4d_u m4d_u = Eigen::Matrix4d::Random();

        bool status = false;
        Json::Value json_value;
        Eigen::Matrix4d_u ref;

        status = utility::IJsonConvertible::EigenMatrix4dToJsonArray(
                m4d_u, json_value);
        EXPECT_TRUE(status);

        status = utility::IJsonConvertible::EigenMatrix4dFromJsonArray(
                ref, json_value);
        EXPECT_TRUE(status);

        ExpectEQ(ref, m4d_u);
    }
}

// ----------------------------------------------------------------------------
// Test the conversion of Eigen::Matrix6d to and from JsonArray.
// ----------------------------------------------------------------------------
TEST(IJsonConvertible, EigenMatrix6dToFromJsonArray) {
    int loops = 10000;
    srand((unsigned int)time(0));
    for (int i = 0; i < loops; i++) {
        Eigen::Matrix6d m6d = Eigen::Matrix6d::Random();

        bool status = false;
        Json::Value json_value;
        Eigen::Matrix6d ref;

        status = utility::IJsonConvertible::EigenMatrix6dToJsonArray(
                m6d, json_value);
        EXPECT_TRUE(status);

        status = utility::IJsonConvertible::EigenMatrix6dFromJsonArray(
                ref, json_value);
        EXPECT_TRUE(status);

        ExpectEQ(ref, m6d);
    }
}

// ----------------------------------------------------------------------------
// Test the conversion of unaligned Eigen::Matrix6d to and from
// JsonArray.
// ----------------------------------------------------------------------------
TEST(IJsonConvertible, EigenMatrix6d_uToFromJsonArray) {
    int loops = 10000;
    srand((unsigned int)time(0));
    for (int i = 0; i < loops; i++) {
        Eigen::Matrix6d_u m6d_u = Eigen::Matrix6d::Random();

        bool status = false;
        Json::Value json_value;
        Eigen::Matrix6d_u ref;

        status = utility::IJsonConvertible::EigenMatrix6dToJsonArray(
                m6d_u, json_value);
        EXPECT_TRUE(status);

        status = utility::IJsonConvertible::EigenMatrix6dFromJsonArray(
                ref, json_value);
        EXPECT_TRUE(status);

        ExpectEQ(ref, m6d_u);
    }
}

}  // namespace tests
}  // namespace open3d