forked from s-nakaoka/openhrp-plugin
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ShapeSetInfo_impl.cpp
345 lines (282 loc) · 10.2 KB
/
ShapeSetInfo_impl.cpp
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*!
@file
@author Shizuko Hattori
*/
#include "ShapeSetInfo_impl.h"
#include "ModelLoaderUtil.h"
using namespace std;
using namespace cnoid;
using namespace cnoid::openHRPModelloader;
using namespace OpenHRP;
namespace {
typedef map< int, ShapePrimitiveType>PrimitiveTypeMap;
PrimitiveTypeMap primitiveTypeMap;
}
ShapeSetInfo_impl::ShapeSetInfo_impl(PortableServer::POA_ptr poa) :
poa(PortableServer::POA::_duplicate(poa))
{
if(primitiveTypeMap.empty()){
primitiveTypeMap[SgMesh::MESH] = SP_MESH;
primitiveTypeMap[SgMesh::BOX] = SP_BOX;
primitiveTypeMap[SgMesh::SPHERE] = SP_SPHERE;
primitiveTypeMap[SgMesh::CYLINDER] = SP_CYLINDER;
primitiveTypeMap[SgMesh::CONE] = SP_CONE;
primitiveTypeMap[SgMesh::CAPSULE] = SP_MESH;
// = SP_PLANE;
}
}
ShapeSetInfo_impl::~ShapeSetInfo_impl()
{
}
PortableServer::POA_ptr ShapeSetInfo_impl::_default_POA()
{
return PortableServer::POA::_duplicate(poa);
}
ShapeInfoSequence* ShapeSetInfo_impl::shapes()
{
return new ShapeInfoSequence(shapes_);
}
AppearanceInfoSequence* ShapeSetInfo_impl::appearances()
{
return new AppearanceInfoSequence(appearances_);
}
MaterialInfoSequence* ShapeSetInfo_impl::materials()
{
return new MaterialInfoSequence(materials_);
}
TextureInfoSequence* ShapeSetInfo_impl::textures()
{
return new TextureInfoSequence(textures_);
}
void ShapeSetInfo_impl::setShapeIndices(SgNode* node, TransformedShapeIndexSequence& shapeIndices)
{
MeshExtractor* extractor = new MeshExtractor;
extractor->extract(node, [&](){ createTransformedShapeIndex( extractor, shapeIndices ); });
}
void ShapeSetInfo_impl::createTransformedShapeIndex(MeshExtractor* extractor, TransformedShapeIndexSequence& shapeIndices)
{
SgShape* shape = extractor->currentShape();
const Affine3& T = extractor->currentTransform();
int shapeInfoIndex;
SgShapeToShapeInfoIndexMap::iterator p = shapeInfoIndexMap.find(shape);
if(p != shapeInfoIndexMap.end()){
shapeInfoIndex = p->second;
}else{
shapeInfoIndex = shapes_.length();
shapes_.length(shapeInfoIndex + 1);
ShapeInfo& shapeInfo = shapes_[shapeInfoIndex];
shapeInfo.url = "";
setMesh(shape->mesh(), shapeInfo);
setPrimitive(shape->mesh(), shapeInfo);
shapeInfo.appearanceIndex = setAppearance(shape);
shapeInfoIndexMap[shape] = shapeInfoIndex;
}
int n = shapeIndices.length();
shapeIndices.length(n + 1);
TransformedShapeIndex& transformedShapeIndex = shapeIndices[n];
transformedShapeIndex.shapeIndex = shapeInfoIndex;
transformedShapeIndex.inlinedShapeTransformMatrixIndex = -1;
setTransformMatrix(T, transformedShapeIndex.transformMatrix);
}
void ShapeSetInfo_impl::setMesh(SgMesh* mesh, ShapeInfo& shapeInfo)
{
SgVertexArray& vertices = *mesh->vertices();
const int numVertices = vertices.size();
shapeInfo.vertices.length(numVertices * 3);
for(size_t pos=0, i=0; i < numVertices; i++){
const Vector3f& v = vertices[i];
setVector3(v, &shapeInfo.vertices[pos]);
pos +=3;
}
SgIndexArray& indices = mesh->triangleVertices();
setSequence( indices, shapeInfo.triangles );
}
void ShapeSetInfo_impl::setPrimitive(SgMesh* mesh, ShapeInfo& shapeInfo)
{
shapeInfo.primitiveType = primitiveTypeMap[mesh->primitiveType()];
FloatSequence& param = shapeInfo.primitiveParameters;
switch(shapeInfo.primitiveType){
case SP_BOX : {
const Vector3& s = mesh->primitive<SgMesh::Box>().size;
setSequence( s, param );
break; }
case SP_SPHERE : {
SgMesh::Sphere sphere = mesh->primitive<SgMesh::Sphere>();
param.length( CORBA::ULong(1) );
param[0] = sphere.radius;
break; }
case SP_CYLINDER : {
SgMesh::Cylinder cylinder = mesh->primitive<SgMesh::Cylinder>();
param.length( CORBA::ULong(5) );
param[0] = cylinder.radius;
param[1] = cylinder.height;
param[2] = cylinder.top ? 1 : 0;
param[3] = cylinder.bottom ? 1 : 0;
param[4] = cylinder.side ? 1 : 0;
break; }
case SP_CONE : {
SgMesh::Cone cone = mesh->primitive<SgMesh::Cone>();
param.length( CORBA::ULong(4) );
param[0] = cone.radius;
param[1] = cone.height;
param[2] = cone.bottom ? 1 : 0;
param[3] = cone.side ? 1 : 0;
break; }
default :
break;
}
}
int ShapeSetInfo_impl::setAppearance(SgShape* shape)
{
int appearanceIndex = appearances_.length();
appearances_.length(appearanceIndex + 1);
AppearanceInfo& appInfo = appearances_[appearanceIndex];
// Choreonoid Shape node has no setting corresponding to solid and creaseAngle fields
appInfo.solid = true;
appInfo.creaseAngle = 0;
SgMesh* mesh = shape->mesh();
if(mesh->hasNormals()){
setNormals(mesh, appInfo);
}
if(mesh->hasColors()){
setColors(mesh, appInfo);
}
SgMaterial* material = shape->material();
if(material){
appInfo.materialIndex = setMaterial(material);
}else{
appInfo.materialIndex = -1;
}
SgTexture* texture = shape->texture();
if(texture){
appInfo.textureIndex = setTexture(texture);
setTexCoords(mesh, texture->textureTransform(), appInfo);
}else{
appInfo.textureIndex = -1;
}
return appearanceIndex;
}
void ShapeSetInfo_impl::setColors(SgMesh* mesh, AppearanceInfo& appInfo)
{
SgColorArray& colors = *mesh->colors();
int numColors = colors.size();
appInfo.colors.length(numColors * 3);
for(int pos=0, i=0; i<numColors; i++){
setVector3(colors[i], &appInfo.colors[pos]);
pos +=3;
}
const SgIndexArray& colorIndices = mesh->colorIndices();
if(colorIndices.empty()){
if( numColors==mesh->numTriangles() ){
appInfo.colorPerVertex = false;
}else{
appInfo.colorPerVertex = true;
}
}else{
if( colorIndices.size() == mesh->triangleVertices().size() ){
appInfo.colorPerVertex = true;
}else{
appInfo.colorPerVertex = false;
}
setSequence( colorIndices, appInfo.colorIndices );
}
}
void ShapeSetInfo_impl::setNormals(SgMesh* mesh, AppearanceInfo& appInfo)
{
SgNormalArray& normals = *mesh->normals();
int numNormals = normals.size();
appInfo.normals.length(numNormals * 3);
for(int pos=0, i=0; i<numNormals; i++){
setVector3(normals[i], &appInfo.normals[pos]);
pos +=3;
}
const SgIndexArray& normalIndices = mesh->normalIndices();
if(normalIndices.empty()){
if( numNormals==mesh->numTriangles() ){
appInfo.normalPerVertex = false;
}else{
appInfo.normalPerVertex = true;
}
}else{
if( normalIndices.size() == mesh->triangleVertices().size() ){
appInfo.normalPerVertex = true;
}else{
appInfo.normalPerVertex = false;
}
setSequence( normalIndices, appInfo.normalIndices );
}
}
int ShapeSetInfo_impl::setMaterial(SgMaterial* material)
{
SgMaterialToMaterialInfoIndexMap::iterator it = materialInfoIndexMap.find(material);
if(it!=materialInfoIndexMap.end()){
return it->second;
}
int materialInfoIndex = materials_.length();
materials_.length(materialInfoIndex + 1 );
MaterialInfo& materialInfo = materials_[materialInfoIndex];
materialInfoIndexMap[material] = materialInfoIndex;
materialInfo.ambientIntensity = material->ambientIntensity();
materialInfo.shininess = material->shininess();
materialInfo.transparency = material->transparency();
setVector3( material->diffuseColor(), materialInfo.diffuseColor);
setVector3( material->emissiveColor(), materialInfo.emissiveColor);
setVector3( material->specularColor(), materialInfo.specularColor);
return materialInfoIndex;
}
int ShapeSetInfo_impl::setTexture(SgTexture* texture)
{
SgTextureToTextureInfoIndexMap::iterator it = textureInfoIndexMap.find(texture);
if(it!=textureInfoIndexMap.end()){
return it->second;
}
int textureInfoIndex = textures_.length();
textures_.length(textureInfoIndex + 1 );
TextureInfo& textureInfo = textures_[textureInfoIndex];
textureInfoIndexMap[texture] = textureInfoIndex;
SgImage* image = texture->image();
unsigned char* pixels = image->pixels();
unsigned long pixelsLength = image->width() * image->height() * image->numComponents();
textureInfo.image.length( pixelsLength );
for(unsigned long i = 0 ; i < pixelsLength ; i++ ){
textureInfo.image[i] = pixels[i];
}
textureInfo.numComponents = image->numComponents();
textureInfo.width = image->width();
textureInfo.height = image->height();
textureInfo.repeatS = texture->repeatS();
textureInfo.repeatT = texture->repeatT();
textureInfo.url = "";
return textureInfoIndex;
}
void ShapeSetInfo_impl::setTexCoords(SgMesh* mesh, SgTextureTransform* textureTransform, AppearanceInfo& appInfo)
{
Eigen::Affine2d m;
if(textureTransform){
Eigen::Translation<double, 2> translation =
Eigen::Translation<double, 2>(textureTransform->translation());
Eigen::Translation<double, 2> center =
Eigen::Translation<double, 2>(textureTransform->center());
Eigen::Translation<double, 2> centerInv =
Eigen::Translation<double, 2>(-textureTransform->center());
Eigen::DiagonalMatrix<double, 2> scale = Eigen::Scaling(textureTransform->scale());
Eigen::Rotation2Dd rotation(textureTransform->rotation());
m = centerInv * scale * rotation * center * translation;
}else{
m = Eigen::Affine2d::Identity();
}
for(int i=0,k=0; i<3; i++){
for(int j=0; j<3; j++){
appInfo.textransformMatrix[k++] = m(i,j);
}
}
SgTexCoordArray& texCoords = *mesh->texCoords();
int numCoords = texCoords.size();
appInfo.textureCoordinate.length(numCoords * 2);
for(int i=0, pos=0; i < numCoords; i++ ){
Vector2 texCoord = m * texCoords[i].cast<double>();
appInfo.textureCoordinate[pos++] = texCoord(0);
appInfo.textureCoordinate[pos++] = texCoord(1);
}
setSequence( mesh->texCoordIndices(), appInfo.textureCoordIndices );
}