-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.cpp
More file actions
189 lines (144 loc) · 5.27 KB
/
backend.cpp
File metadata and controls
189 lines (144 loc) · 5.27 KB
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
// ptio_backend.cpp
#include "PCCPointSet.h"
#include "ply.h"
#include "backend.h"
#include <Python.h>
using namespace pcc;
using namespace pcc::ply;
// C++实现
pcc_io* pcc_io_create() {
pcc_io* obj = new pcc_io;
obj->cloud = new PCCPointSet3;
return obj;
}
void pcc_io_destroy(pcc_io* obj) {
if (obj) {
delete static_cast<PCCPointSet3*>(obj->cloud);
delete obj;
}
}
bool pcc_io_read(pcc_io* obj, const char* filename, double positionScale) {
if (!obj) return false;
PCCPointSet3* cloud = static_cast<PCCPointSet3*>(obj->cloud);
PropertyNameMap attributeNames;
attributeNames.position = {"x", "y", "z"};
return ply::read(filename, attributeNames, positionScale, *cloud);
}
bool pcc_io_write(pcc_io* obj, const char* filename, double positionScale, bool asAscii) {
if (!obj) return false;
PCCPointSet3* cloud = static_cast<PCCPointSet3*>(obj->cloud);
PropertyNameMap attributeNames;
attributeNames.position = {"x", "y", "z"};
return ply::write(*cloud, attributeNames, positionScale, {0,0,0}, filename, asAscii);
}
size_t pcc_io_get_point_count(const pcc_io* obj) {
if (!obj) return 0;
return static_cast<const PCCPointSet3*>(obj->cloud)->getPointCount();
}
bool pcc_io_has_colors(const pcc_io* obj) {
if (!obj) return false;
return static_cast<const PCCPointSet3*>(obj->cloud)->hasColors();
}
bool pcc_io_has_reflectance(const pcc_io* obj) {
if (!obj) return false;
return static_cast<const PCCPointSet3*>(obj->cloud)->hasReflectances();
}
bool pcc_io_get_points(const pcc_io* obj, double** points, size_t* pointCount) {
if (!obj || !points || !pointCount) return false;
const PCCPointSet3* cloud = static_cast<const PCCPointSet3*>(obj->cloud);
*pointCount = cloud->getPointCount();
if (*pointCount == 0) return true;
size_t dataSize = (*pointCount) * 3;
*points = new double[dataSize];
for (size_t i = 0; i < *pointCount; ++i) {
const auto& point = (*cloud)[i];
(*points)[i * 3] = point[0];
(*points)[i * 3 + 1] = point[1];
(*points)[i * 3 + 2] = point[2];
}
return true;
}
bool pcc_io_get_colors(const pcc_io* obj, uint8_t** colors, size_t* pointCount) {
if (!obj || !colors || !pointCount) return false;
const PCCPointSet3* cloud = static_cast<const PCCPointSet3*>(obj->cloud);
*pointCount = cloud->getPointCount();
if (*pointCount == 0 || !cloud->hasColors()) return true;
size_t dataSize = (*pointCount) * 3;
*colors = new uint8_t[dataSize];
for (size_t i = 0; i < *pointCount; ++i) {
const Vec3<attr_t>& c = cloud->getColor(i); // GBR
(*colors)[i * 3] = c[2]; // R
(*colors)[i * 3 + 1] = c[0]; // G
(*colors)[i * 3 + 2] = c[1]; // B
}
return true;
}
bool pcc_io_get_reflectance(const pcc_io* obj, uint16_t** reflectance, size_t* pointCount) {
if (!obj || !reflectance || !pointCount) return false;
const PCCPointSet3* cloud = static_cast<const PCCPointSet3*>(obj->cloud);
*pointCount = cloud->getPointCount();
if (*pointCount == 0 || !cloud->hasReflectances()) return true;
*reflectance = new uint16_t[*pointCount];
for (size_t i = 0; i < *pointCount; ++i) {
(*reflectance)[i] = cloud->getReflectance(i);
}
return true;
}
bool pcc_io_set_points(pcc_io* obj, const double* points, size_t pointCount) {
if (!obj || !points || pointCount == 0) return false;
PCCPointSet3* cloud = static_cast<PCCPointSet3*>(obj->cloud);
cloud->resize(pointCount);
for (size_t i = 0; i < pointCount; ++i) {
(*cloud)[i] = Vec3<double>(points[i * 3], points[i * 3 + 1], points[i * 3 + 2]);
}
return true;
}
bool pcc_io_set_colors(pcc_io* obj, const uint8_t* colors, size_t pointCount) {
if (!obj || !colors || pointCount == 0) return false;
PCCPointSet3* cloud = static_cast<PCCPointSet3*>(obj->cloud);
cloud->addColors();
for (size_t i = 0; i < pointCount; ++i) {
cloud->getColor(i) = Vec3<attr_t>(colors[i * 3 + 1], colors[i * 3 + 2], colors[i * 3]); // GBR
}
return true;
}
bool pcc_io_set_reflectance(pcc_io* obj, const uint16_t* reflectance, size_t pointCount) {
if (!obj || !reflectance || pointCount == 0) return false;
PCCPointSet3* cloud = static_cast<PCCPointSet3*>(obj->cloud);
cloud->addReflectances();
for (size_t i = 0; i < pointCount; ++i) {
cloud->getReflectance(i) = reflectance[i];
}
return true;
}
void pcc_io_clear(pcc_io* obj) {
if (!obj) return;
static_cast<PCCPointSet3*>(obj->cloud)->clear();
}
void pcc_io_remove_colors(pcc_io* obj) {
if (!obj) return;
static_cast<PCCPointSet3*>(obj->cloud)->removeColors();
}
void pcc_io_remove_reflectance(pcc_io* obj) {
if (!obj) return;
static_cast<PCCPointSet3*>(obj->cloud)->removeReflectances();
}
void pcc_io_free_double_array(double* arr) {
delete[] arr;
}
void pcc_io_free_uint8_array(uint8_t* arr) {
delete[] arr;
}
void pcc_io_free_uint16_array(uint16_t* arr) {
delete[] arr;
}
static PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"ptio_backend",
NULL,
-1,
NULL, NULL, NULL, NULL, NULL
};
PyMODINIT_FUNC PyInit_ptio_backend(void) {
return PyModule_Create(&moduledef);
}