-
Notifications
You must be signed in to change notification settings - Fork 1
/
myRealMesh.cc
298 lines (250 loc) · 11.4 KB
/
myRealMesh.cc
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "myRealMesh.h"
#include <iostream>
#include <iterator>
// -------------------- OpenMesh
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
//internal use snack_case || external use camelCase
typedef OpenMesh::TriMesh_ArrayKernelT<> TriMesh;
Napi::FunctionReference MyMesh::constructor;
Napi::Object MyMesh::Init(Napi::Env env, Napi::Object exports) {
//Napi::HandleScope scope(env); //not necessary
Napi::Function func =
DefineClass(env,
"MyMesh",
{InstanceMethod("addVertex", &MyMesh::AddVertex),
InstanceMethod("getNumFace", &MyMesh::GetFaceValue),
InstanceMethod("addFace", &MyMesh:: AddFace),
InstanceMethod("addFaceById", &MyMesh:: AddFaceById),
InstanceMethod("points", &MyMesh::GetPoints),
InstanceMethod("setPointById", &MyMesh::SetPointById),
InstanceMethod("setPoint", &MyMesh::SetPoint),
InstanceMethod("vv", &MyMesh::VVIter),
InstanceMethod("bFSNeighWeights", &MyMesh::BFSNeighWeights)
});
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
exports.Set("MyMesh", func);
return exports;
}
MyMesh::MyMesh(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<MyMesh>(info) {
// Napi::Env env = info.Env();
// Napi::HandleScope scope(env); //not necessary
TriMesh mesh;
this->myTriMesh_ = mesh;
std::vector<TriMesh::VertexHandle> storage_vhandles;
this->vHStorage_ = storage_vhandles;
// request vertex normals, so the mesh reader can use normal information
// if available
mesh.request_vertex_normals();
// assure we have vertex normals
if (!mesh.has_vertex_normals())
{
std::cerr << "ERROR: Standard vertex property 'Normals' not available!\n";
}
}
Napi::Value MyMesh::GetFaceValue(const Napi::CallbackInfo& info) {
TriMesh mesh = this->myTriMesh_;
// int num = mesh.n_faces().Int32Value(); error: member reference base type 'size_t' (aka 'unsigned long') is not a structure or union
size_t nFace = mesh.n_faces();
int num = static_cast<int>(nFace);
return Napi::Number::New(info.Env(), num);
}
Napi::Value MyMesh::AddVertex(const Napi::CallbackInfo& info) {
TriMesh mesh = this->myTriMesh_;
if (info.Length() != 3 || !info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber() ) {
Napi::TypeError::New(_env, "Napi AddVertex_Err: 3 numbers expected.").ThrowAsJavaScriptException();
return Napi::Number::New(info.Env(), -1);
} else {
double pointX = info[0].As<Napi::Number>().DoubleValue();
double pointY = info[1].As<Napi::Number>().DoubleValue();
double pointZ = info[2].As<Napi::Number>().DoubleValue();
TriMesh::VertexHandle new_vh = mesh.add_vertex(TriMesh::Point(pointX,pointY,pointZ));
this->vHStorage_.push_back(new_vh);
this->myTriMesh_ = mesh;
// if ((int)mesh.n_vertices()<=3){
// std::cout << "const ptrVHS : " << this->ptrVHS << " type: " << typeid(this->ptrVHS).name() << std::endl;
// std::cout << "CPP success: a new vertex added. ref: " << this->ptrVHS+(int)mesh.n_vertices()-1 << std::endl;
// }
return Napi::External<std::vector<TriMesh::VertexHandle>>::New(info.Env(), this->ptrVHS+(int)mesh.n_vertices()-1);
}
}
Napi::Value MyMesh::VVIter(const Napi::CallbackInfo& info) {
TriMesh mesh = this->myTriMesh_;
std::vector<OpenMesh::TriMesh_ArrayKernelT<>::VertexHandle> vhs = this->vHStorage_;
TriMesh::VertexVertexIter vv_it;
if (info.Length() != 1) {
Napi::TypeError::New(_env, "Napi VVIter_Err: 1 param expected.").ThrowAsJavaScriptException();
return Napi::Number::New(info.Env(), -1);
} else {
int num_neigh = 0 ;
auto vh = info[0].As<Napi::External<std::vector<TriMesh::VertexHandle>>>().Data();
int vh_idx = (vh)-(this->ptrVHS);
for (vv_it=mesh.vv_iter(vhs[vh_idx]); vv_it.is_valid(); ++vv_it){
// std::cout << "neighbor point idx : " << vv_it->idx() << std::endl;
// std::cout << "neighbor point data : " << mesh.point( *vv_it ) << std::endl;
num_neigh++;
}
unsigned int i = 0 ;
Napi::Array neighboursVHArray = Napi::Array::New(info.Env(),num_neigh);
for (vv_it=mesh.vv_iter(vhs[vh_idx]); vv_it.is_valid(); ++vv_it){
std::vector<TriMesh::VertexHandle> * temp_vh_ptr = this->ptrVHS + (int)vv_it->idx();
neighboursVHArray[i] = Napi::External<std::vector<TriMesh::VertexHandle>>::New(info.Env(), temp_vh_ptr);
i++;
}
return neighboursVHArray;
}
}
Napi::Value MyMesh::BFSNeighWeights(const Napi::CallbackInfo& info) {
TriMesh mesh = this->myTriMesh_;
std::vector<OpenMesh::TriMesh_ArrayKernelT<>::VertexHandle> vhs = this->vHStorage_;
TriMesh::VertexVertexIter vv_it;
if (info.Length() != 1) {
Napi::TypeError::New(_env, "Napi VVIter_Err: 1 param expected.").ThrowAsJavaScriptException();
return Napi::Number::New(info.Env(), -1);
} else {
// init Napi:Object-weights and temp_vhs
Napi::Object weights = Napi::Object::New(info.Env());
auto vh = info[0].As<Napi::External<std::vector<TriMesh::VertexHandle>>>().Data();
int vh_idx = (vh)-(this->ptrVHS);
weights.Set(vh_idx,(double)0);
std::vector<TriMesh::VertexHandle> new_added_points;
new_added_points.push_back(vhs[vh_idx]);
unsigned int i = 0;
while (new_added_points.size()>0){
std::vector<TriMesh::VertexHandle> temp_points;
temp_points.clear();
for (i=0;i<new_added_points.size();i++) {
int f = new_added_points[i].idx();
for (vv_it=mesh.vv_iter(new_added_points[i]); vv_it.is_valid(); ++vv_it){
uint32_t k = (int)vv_it->idx();
if (!weights.Has(k)){
double weightVal = weights.Get(f).As<Napi::Number>().DoubleValue() + 1.0;
weights.Set(k,weightVal);
temp_points.push_back(*vv_it);
}
}
}
new_added_points = temp_points;
}
for (i=0;i<mesh.n_vertices();i++){
if (!weights.Has(i)){
weights.Set(i,1000);
}
}
return weights;
}
}
void MyMesh::AddFace(const Napi::CallbackInfo& info) {
TriMesh mesh = this->myTriMesh_;
std::vector<OpenMesh::TriMesh_ArrayKernelT<>::VertexHandle> vhs = this->vHStorage_;
if (info.Length() != 3 ){
Napi::TypeError::New(_env, "Napi 3 VertexHandles expected.").ThrowAsJavaScriptException();
} else {
std::vector<TriMesh::VertexHandle> temp_face_vhandles;
temp_face_vhandles.clear();
auto vh1 = info[0].As<Napi::External<std::vector<TriMesh::VertexHandle>>>().Data();
auto vh2 = info[1].As<Napi::External<std::vector<TriMesh::VertexHandle>>>().Data();
auto vh3 = info[2].As<Napi::External<std::vector<TriMesh::VertexHandle>>>().Data();
int vh1_idx = (vh1)-(this->ptrVHS);
int vh2_idx = (vh2)-(this->ptrVHS);
int vh3_idx = (vh3)-(this->ptrVHS);
temp_face_vhandles.push_back(vhs[vh1_idx]);
temp_face_vhandles.push_back(vhs[vh2_idx]);
temp_face_vhandles.push_back(vhs[vh3_idx]);
mesh.add_face(temp_face_vhandles);
temp_face_vhandles.clear();
this->myTriMesh_ = mesh;
}
return;
}
void MyMesh::SetPoint(const Napi::CallbackInfo& info) {
if (info.Length() != 2 || !info[1].IsArray()) {
Napi::TypeError::New(_env, "Napi Err: 2 params expected: first as vertex_id second as new_point_array").ThrowAsJavaScriptException();
return ;// Napi::Number::New(info.Env(), -1);
}
else if (info[1].As<Napi::Array>().Length() != 3){
Napi::TypeError::New(_env, "Napi Err: second param must be an array with 3 numbers.").ThrowAsJavaScriptException();
return ;//Napi::Number::New(info.Env(), -1);
}
else {
TriMesh mesh = this->myTriMesh_;
std::vector<OpenMesh::TriMesh_ArrayKernelT<>::VertexHandle> vhs = this->vHStorage_;
auto vh = info[0].As<Napi::External<std::vector<TriMesh::VertexHandle>>>().Data();
int input_vidx = (vh)-(this->ptrVHS);
const Napi::Array newPointArray = info[1].As<Napi::Array>();
unsigned int i = 0 ;
double newPointX = newPointArray[i++].As<Napi::Number>().DoubleValue();
double newPointY = newPointArray[i++].As<Napi::Number>().DoubleValue();
double newPointZ = newPointArray[i++].As<Napi::Number>().DoubleValue();
mesh.set_point( vhs[input_vidx],
TriMesh::Point(newPointX,newPointY,newPointZ));
this->myTriMesh_ = mesh;
return ;
}
}
void MyMesh::AddFaceById(const Napi::CallbackInfo& info) {
TriMesh mesh = this->myTriMesh_;
std::vector<OpenMesh::TriMesh_ArrayKernelT<>::VertexHandle> vhs = this->vHStorage_;
if (info.Length() != 3 || !info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber() ) {
Napi::TypeError::New(_env, "Napi 3 numbers expected.").ThrowAsJavaScriptException();
} else {
std::vector<TriMesh::VertexHandle> temp_face_vhandles;
temp_face_vhandles.clear();
temp_face_vhandles.push_back(vhs[info[0].As<Napi::Number>().Int32Value()]);
temp_face_vhandles.push_back(vhs[info[1].As<Napi::Number>().Int32Value()]);
temp_face_vhandles.push_back(vhs[info[2].As<Napi::Number>().Int32Value()]);
mesh.add_face(temp_face_vhandles);
temp_face_vhandles.clear();
this->myTriMesh_ = mesh;
}
return;
}
void MyMesh::SetPointById(const Napi::CallbackInfo& info) {
if (info.Length() != 2 || !info[0].IsNumber() || !info[1].IsArray()) {
Napi::TypeError::New(_env, "Napi Err: 2 params expected: first as vertex_id second as new_point_array").ThrowAsJavaScriptException();
return ;//Napi::Number::New(info.Env(), -1);
} else {
if (info[1].As<Napi::Array>().Length() != 3){
Napi::TypeError::New(_env, "Napi Err: second param must be an array with 3 numbers.").ThrowAsJavaScriptException();
return ;//Napi::Number::New(info.Env(), -1);
}
TriMesh mesh = this->myTriMesh_;
std::vector<OpenMesh::TriMesh_ArrayKernelT<>::VertexHandle> vhs = this->vHStorage_;
int input_vidx = info[0].As<Napi::Number>().Int32Value();
const Napi::Array newPointArray = info[1].As<Napi::Array>();
unsigned int i = 0 ;
double newPointX = newPointArray[i++].As<Napi::Number>().DoubleValue();
double newPointY = newPointArray[i++].As<Napi::Number>().DoubleValue();
double newPointZ = newPointArray[i++].As<Napi::Number>().DoubleValue();
mesh.set_point( vhs[input_vidx],
TriMesh::Point(newPointX,newPointY,newPointZ));
this->myTriMesh_ = mesh;
return ;
}
}
Napi::Value MyMesh::GetPoints(const Napi::CallbackInfo& info) {
if (info.Length() != 0) {
Napi::TypeError::New(_env, "Napi Err: 0 params expected.").ThrowAsJavaScriptException();
return Napi::Number::New(info.Env(), -1);
} else {
TriMesh mesh = this->myTriMesh_;
std::vector<OpenMesh::TriMesh_ArrayKernelT<>::VertexHandle> vhs = this->vHStorage_;
Napi::Array pointsArray = Napi::Array::New(info.Env(),(int)mesh.n_vertices());
unsigned int iv = 0 ;//error: no viable overloaded operator[] for type 'Napi::Array'
for (TriMesh::VertexIter v_it = mesh.vertices_begin();
v_it != mesh.vertices_end();
++v_it){
OpenMesh::VectorT<float, 3> point = mesh.point(*v_it);
Napi::Array pointArray = Napi::Array::New(info.Env(),3);
unsigned int i = 0 ;
while (i<3) {
pointArray[i] = Napi::Number::New(info.Env(), point[i]);
i++;
}
pointsArray[iv++] = pointArray;
}
return pointsArray ;
}
}