36 #include <vtkSmartPointer.h> 37 #include <vtkPolyData.h> 38 #include <vtkCellArray.h> 39 #include <vtkPolyDataMapper.h> 40 #include <vtkPoints.h> 42 #include <vtkTriangle.h> 43 #include <vtkProperty.h> 44 #include <vtkImageData.h> 45 #include <vtkTexture.h> 46 #include <vtkFloatArray.h> 47 #include <vtkPointData.h> 48 #include <vtkCellData.h> 56 m_meshBuffer(meshBuffer)
77 vtkSmartPointer<vtkProperty>
p =
m_meshActor->GetProperty();
82 float inv_r = (float)1 - r;
83 float inv_g = (float)1 - g;
84 float inv_b = (float)1 - b;
85 p->SetColor(inv_r, inv_g, inv_b);
135 vtkSmartPointer<vtkPolyData>
mesh = vtkSmartPointer<vtkPolyData>::New();
138 size_t n_v, n_i, n_c;
140 n_v = meshbuffer->numVertices();
141 floatArr vertices = meshbuffer->getVertices();
142 n_i = meshbuffer->numFaces();
143 indexArray indices = meshbuffer->getFaceIndices();
147 vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
148 vtkSmartPointer<vtkCellArray> triangles = vtkSmartPointer<vtkCellArray>::New();
150 vtkSmartPointer<vtkUnsignedCharArray> scalars = vtkSmartPointer<vtkUnsignedCharArray>::New();
151 scalars->SetNumberOfComponents(3);
152 scalars->SetName(
"Colors");
154 for(
size_t i = 0; i < n_v; i++){
155 size_t index = 3 * i;
156 points->InsertNextPoint(
159 vertices[index + 2]);
163 size_t color_index = w_color * i;
164 unsigned char color[3];
165 color[0] = colors[color_index ];
166 color[1] = colors[color_index + 1];
167 color[2] = colors[color_index + 2];
168 #if VTK_MAJOR_VERSION < 7 169 scalars->InsertNextTupleValue(color);
171 scalars->InsertNextTypedTuple(color);
176 for(
size_t i = 0; i < n_i; i++)
178 size_t index = 3 * i;
179 vtkSmartPointer<vtkTriangle> t = vtkSmartPointer<vtkTriangle>::New();
180 t->GetPointIds()->SetId(0, indices[index]);
181 t->GetPointIds()->SetId(1, indices[index + 1]);
182 t->GetPointIds()->SetId(2, indices[index + 2]);
183 triangles->InsertNextCell(t);
187 mesh->SetPoints(points);
188 mesh->SetPolys(triangles);
192 mesh->GetPointData()->SetScalars(scalars);
195 vtkSmartPointer<vtkPolyDataMapper> mesh_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
197 mesh_mapper->SetInput(mesh);
199 mesh_mapper->SetInputData(mesh);
204 vtkSmartPointer<vtkPolyDataMapper> wireframe_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
206 wireframe_mapper->SetInput(mesh);
208 wireframe_mapper->SetInputData(mesh);
213 vtkSmartPointer<vtkProperty>
p = vtkSmartPointer<vtkProperty>::New();
215 p->SetRepresentationToWireframe();
224 vtkSmartPointer<vtkProperty>
p =
m_meshActor->GetProperty();
225 p->SetOpacity(opacityValue);
229 vtkSmartPointer<vtkProperty> prop =
getTexturedActors()->GetLastActor()->GetProperty();
230 prop->SetOpacity(opacityValue);
270 vtkSmartPointer<vtkProperty>
p =
m_meshActor->GetProperty();
271 p->SetShading(shader);
287 map<int, MaterialGroup*> texMatMap;
288 map<VecUChar, MaterialGroup*, Util::ColorVecCompare> colorMatMap;
291 vector<Material> &materials =
m_meshBuffer->getMaterials();
292 vector<Texture> &textures =
m_meshBuffer->getTextures();
296 size_t numMaterials = materials.size();
305 map<int, MaterialGroup*>::iterator texIt;
306 map<VecUChar, MaterialGroup*, Util::ColorVecCompare>::iterator colIt;
312 const Material &m = materials[faceMaterials[i]];
317 texIt = texMatMap.find(m.
m_texture->idx());
318 if(texIt == texMatMap.end())
324 textureMaterials.push_back(g);
335 if(colIt == colorMatMap.end())
341 colorMaterials.push_back(g);
345 colIt->second->faceBuffer.push_back(i);
355 vector<Vec >& vertices,
356 vector<Vec >& texCoords,
357 vector<int>& indices)
360 map<int, int> indexMap;
361 map<int, int>::iterator it;
374 for(
size_t i = 0; i < g->
faceBuffer.size(); i++)
382 it = indexMap.find(a);
383 if(it == indexMap.end())
385 indexMap[a] = globalIndex;
386 indices.push_back(globalIndex);
387 Vec v(vertexBuffer[3 * a], vertexBuffer[3 * a + 1], vertexBuffer[3 * a + 2]);
388 Vec t(textures[2 * a], textures[2 * a + 1], 0.0);
389 texCoords.push_back(t);
390 vertices.push_back(v);
395 indices.push_back(indexMap[a]);
399 it = indexMap.find(b);
400 if(it == indexMap.end())
402 indexMap[b] = globalIndex;
403 indices.push_back(globalIndex);
404 Vec v(vertexBuffer[3 * b], vertexBuffer[3 * b + 1], vertexBuffer[3 * b + 2]);
405 Vec t(textures[2 * b], textures[2 * b + 1], 0.0);
406 texCoords.push_back(t);
407 vertices.push_back(v);
412 indices.push_back(indexMap[b]);
416 it = indexMap.find(c);
417 if(it == indexMap.end())
419 indexMap[c] = globalIndex;
420 indices.push_back(globalIndex);
421 Vec v(vertexBuffer[3 * c], vertexBuffer[3 * c + 1], vertexBuffer[3 * c + 2]);
422 Vec t(textures[2 * c], textures[2 * c + 1], 0.0);
423 texCoords.push_back(t);
424 vertices.push_back(v);
429 indices.push_back(indexMap[c]);
436 vector<MaterialGroup*> groups,
437 vector<Vec >& vertices,
439 vector<int>& indices)
442 map<int, int> indexMap;
443 map<int, int>::iterator it;
450 for(
size_t a = 0; a < groups.size(); a++)
454 for(
size_t i = 0; i < g->
faceBuffer.size(); i++)
462 (
unsigned char) (255*g->
color[0]),
463 (
unsigned char) (255*g->
color[1]),
464 (
unsigned char) (255*g->
color[2])
468 it = indexMap.find(a);
469 if(it == indexMap.end())
471 indexMap[a] = globalIndex;
472 indices.push_back(globalIndex);
473 Vec v(vertexBuffer[3 * a], vertexBuffer[3 * a + 1], vertexBuffer[3 * a + 2]);
474 vertices.push_back(v);
479 indices.push_back(indexMap[a]);
483 it = indexMap.find(b);
484 if(it == indexMap.end())
486 indexMap[b] = globalIndex;
487 indices.push_back(globalIndex);
488 Vec v(vertexBuffer[3 * b], vertexBuffer[3 * b + 1], vertexBuffer[3 * b + 2]);
489 vertices.push_back(v);
494 indices.push_back(indexMap[b]); }
497 it = indexMap.find(c);
498 if(it == indexMap.end())
500 indexMap[c] = globalIndex;
501 indices.push_back(globalIndex);
502 Vec v(vertexBuffer[3 * c], vertexBuffer[3 * c + 1], vertexBuffer[3 * c + 2]);
503 vertices.push_back(v);
508 indices.push_back(indexMap[c]);
519 vtkSmartPointer<vtkPolyData>
mesh = vtkSmartPointer<vtkPolyData>::New();
520 vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
523 vector<Vec > vertices;
524 vector<Vec > texCoords;
528 vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
529 vtkSmartPointer<vtkCellArray> triangles = vtkSmartPointer<vtkCellArray>::New();
530 vtkSmartPointer<vtkFloatArray> tc = vtkSmartPointer<vtkFloatArray>::New();
531 tc->SetNumberOfComponents(3);
532 tc->SetName(
"TextureCoordinates");
535 for(
size_t i = 0; i < vertices.size(); i++){
536 points->InsertNextPoint(
540 tc->InsertNextTuple3(texCoords[i][1], texCoords[i][0], 0.0);
545 for(
size_t i = 0; i < indices.size() / 3; i++)
547 size_t index = 3 * i;
548 vtkSmartPointer<vtkTriangle> t = vtkSmartPointer<vtkTriangle>::New();
549 t->GetPointIds()->SetId(0, indices[index]);
550 t->GetPointIds()->SetId(1, indices[index + 1]);
551 t->GetPointIds()->SetId(2, indices[index + 2]);
552 triangles->InsertNextCell(t);
557 mesh->SetPoints(points);
558 mesh->SetPolys(triangles);
559 mesh->GetPointData()->SetTCoords(tc);
563 vtkSmartPointer<vtkPolyDataMapper> mesh_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
565 mesh_mapper->SetInput(mesh);
567 mesh_mapper->SetInputData(mesh);
569 actor = vtkSmartPointer<vtkActor>::New();
570 actor->SetMapper(mesh_mapper);
574 vtkSmartPointer<vtkProperty>
property = vtkSmartPointer<vtkProperty>::New();
576 actor->SetProperty(property);
583 vector<Texture> &textures =
m_meshBuffer->getTextures();
584 size_t n = textures.size();
585 Texture &tex = textures[index];
590 unsigned char* texData = tex.
m_data;
592 vtkSmartPointer<vtkImageData> data = vtkSmartPointer<vtkImageData>::New();
593 data->SetDimensions(h, w, 1);
595 data->SetNumberOfScalarComponents(3);
596 data->SetScalarTypeToUnsignedChar();
597 data->AllocateScalars();
599 data->AllocateScalars(VTK_UNSIGNED_CHAR, 3);
602 for(
int i = 0; i < h; i++)
604 for(
int j = 0; j < w; j++)
606 unsigned char* pixel =
static_cast<unsigned char*
>(data->GetScalarPointer(i,j,0));
607 size_t index = numChannels * numBytesPerChan * c;
608 pixel[0] = texData[index ];
609 pixel[1] = texData[index + 1 * numBytesPerChan];
610 pixel[2] = texData[index + 2 * numBytesPerChan];
616 vtkSmartPointer<vtkTexture> texture = vtkSmartPointer<vtkTexture>::New();
618 texture->SetInput(data);
620 texture->SetInputData(data);
631 vector<MaterialGroup*> textureGroups;
632 vector<MaterialGroup*> colorGroups;
638 for(
size_t i = 0; i < textureGroups.size(); i++)
648 for (
auto m : textureGroups)
652 for (
auto m : colorGroups)
664 vector<Vec> vertices;
668 vtkSmartPointer<vtkPolyData>
mesh = vtkSmartPointer<vtkPolyData>::New();
669 vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
673 vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
674 vtkSmartPointer<vtkCellArray> triangles = vtkSmartPointer<vtkCellArray>::New();
676 vtkSmartPointer<vtkUnsignedCharArray> scalars = vtkSmartPointer<vtkUnsignedCharArray>::New();
677 scalars->SetNumberOfComponents(3);
678 scalars->SetName(
"Colors");
681 for(
size_t i = 0; i < vertices.size(); i++){
682 points->InsertNextPoint(
689 for(
size_t i = 0; i < indices.size() / 3; i++)
691 unsigned char color[3];
692 color[0] = colors[i][0];
693 color[1] = colors[i][1];
694 color[2] = colors[i][2];
695 #if VTK_MAJOR_VERSION < 7 696 scalars->InsertNextTupleValue(color);
698 scalars->InsertNextTypedTuple(color);
701 size_t index = 3 * i;
702 vtkSmartPointer<vtkTriangle> t = vtkSmartPointer<vtkTriangle>::New();
703 t->GetPointIds()->SetId(0, indices[index]);
704 t->GetPointIds()->SetId(1, indices[index + 1]);
705 t->GetPointIds()->SetId(2, indices[index + 2]);
706 triangles->InsertNextCell(t);
712 mesh->SetPoints(points);
713 mesh->SetPolys(triangles);
714 mesh->GetCellData()->SetScalars(scalars);
718 vtkSmartPointer<vtkPolyDataMapper> mesh_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
720 mesh_mapper->SetInput(mesh);
722 mesh_mapper->SetInputData(mesh);
724 actor = vtkSmartPointer<vtkActor>::New();
725 actor->SetMapper(mesh_mapper);
vector< size_t > faceBuffer
unsigned char m_numBytesPerChan
The number of bytes per channel.
A material that stores information about color and texture of a cluster.
std::shared_ptr< MeshBuffer > MeshBufferPtr
boost::shared_array< unsigned int > uintArr
vtkSmartPointer< vtkActor > getMeshActor()
vtkSmartPointer< vtkActor > getColorMeshActor(vector< MaterialGroup *> groups)
void setShading(int shader)
size_t getNumTexturedFaces()
BaseVector< unsigned char > VecUChar
vtkSmartPointer< vtkActor > m_meshActor
vtkSmartPointer< vtkTexture > getTexture(int index)
void setOpacity(float opacityValue)
boost::shared_array< unsigned char > ucharArr
void remapTexturedIndices(MaterialGroup *g, vector< Vec > &vertices, vector< Vec > &texCoords, vector< int > &indices)
size_t m_numTexturedFaces
vtkSmartPointer< vtkActor > getTexturedActor(MaterialGroup *g)
boost::shared_array< unsigned int > indexArray
void computeMeshActor(MeshBufferPtr meshbuffer)
This class represents a texture.
boost::shared_array< float > floatArr
void remapIndices(vector< MaterialGroup *> g, vector< Vec > &vertices, vector< VecUChar > &colors, vector< int > &indices)
vtkSmartPointer< vtkActorCollection > getTexturedActors()
vtkSmartPointer< vtkActor > m_wireframeActor
vtkSmartPointer< vtkActorCollection > m_texturedActors
void computeMaterialGroups(vector< MaterialGroup *> &matGroups, vector< MaterialGroup *> &colorMatGroups)
vtkSmartPointer< vtkActor > getWireframeActor()
virtual ~LVRMeshBufferBridge()
unsigned char * m_data
The texture data.
MeshBufferPtr getMeshBuffer()
unsigned short int m_width
The dimensions of the texture.
boost::optional< Rgb8Color > m_color
Optional color.
unsigned char m_numChannels
The number of color channels.
LVRMeshBufferBridge(MeshBufferPtr meshbuffer)
void setVisibility(bool visible)
boost::optional< TextureHandle > m_texture
Optional texture handle.
void setBaseColor(float r, float g, float b)
unsigned short int m_height
size_t getNumColoredFaces()
MeshBufferPtr m_meshBuffer