Skip to content

Commit 3146e4a

Browse files
committed
Merge pull request #2335 from paroj:ovisup
2 parents abfcde0 + 29d8e68 commit 3146e4a

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

modules/ovis/include/opencv2/ovis.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,17 @@ CV_EXPORTS_W void createPointCloudMesh(const String& name, InputArray vertices,
359359
*/
360360
CV_EXPORTS_W void createGridMesh(const String& name, const Size2f& size, const Size& segments = Size(1, 1));
361361

362+
/**
363+
* creates a triangle mesh from vertex-vertex or face-vertex representation
364+
*
365+
* creates a material with the same name
366+
* @param name name of the mesh
367+
* @param vertices float vector of positions
368+
* @param normals float vector of normals
369+
* @param indices int vector of indices
370+
*/
371+
CV_EXPORTS_W void createTriangleMesh(const String& name, InputArray vertices, InputArray normals = noArray(), InputArray indices = noArray());
372+
362373
/**
363374
* updates an existing texture
364375
*

modules/ovis/src/meshes.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,92 @@ void createPointCloudMesh(const String& name, InputArray vertices, InputArray co
102102
mesh->_setBounds(bounds);
103103
}
104104

105+
void createTriangleMesh(const String& name, InputArray vertices, InputArray normals, InputArray indices)
106+
{
107+
CV_CheckTypeEQ(vertices.type(), CV_32FC3, "vertices type must be Vec3f");
108+
CV_Assert(vertices.isContinuous());
109+
110+
if(!normals.empty())
111+
{
112+
CV_CheckTypeEQ(normals.type(), CV_32FC3, "normals type must be Vec3f");
113+
CV_Assert(normals.isContinuous());
114+
CV_Assert(normals.size() == vertices.size());
115+
}
116+
if(!indices.empty())
117+
{
118+
CV_CheckTypeEQ(indices.type(), CV_32S, "indices type must be int");
119+
CV_Assert(indices.isContinuous());
120+
}
121+
122+
// default material
123+
auto mat = MaterialManager::getSingleton().create(name, RESOURCEGROUP_NAME);
124+
125+
// mesh
126+
MeshPtr mesh = MeshManager::getSingleton().createManual(name, RESOURCEGROUP_NAME);
127+
SubMesh* sub = mesh->createSubMesh();
128+
sub->useSharedVertices = true;
129+
sub->operationType = RenderOperation::OT_TRIANGLE_LIST;
130+
sub->setMaterialName(name);
131+
132+
int n = vertices.rows();
133+
134+
mesh->sharedVertexData = new VertexData();
135+
mesh->sharedVertexData->vertexCount = n;
136+
VertexDeclaration* decl = mesh->sharedVertexData->vertexDeclaration;
137+
138+
// vertex data
139+
HardwareBufferManager& hbm = HardwareBufferManager::getSingleton();
140+
141+
Mat _vertices = vertices.getMat();
142+
143+
int source = 0;
144+
HardwareVertexBufferSharedPtr hwbuf;
145+
146+
decl->addElement(source, 0, VET_FLOAT3, VES_POSITION);
147+
hwbuf = hbm.createVertexBuffer(decl->getVertexSize(source), n, HardwareBuffer::HBU_STATIC_WRITE_ONLY);
148+
hwbuf->writeData(0, hwbuf->getSizeInBytes(), _vertices.ptr(), true);
149+
mesh->sharedVertexData->vertexBufferBinding->setBinding(source, hwbuf);
150+
151+
// normals
152+
if (!normals.empty())
153+
{
154+
source += 1;
155+
156+
Mat _normals = normals.getMat();
157+
decl->addElement(source, 0, VET_FLOAT3, VES_NORMAL);
158+
hwbuf =
159+
hbm.createVertexBuffer(decl->getVertexSize(source), n, HardwareBuffer::HBU_STATIC_WRITE_ONLY);
160+
hwbuf->writeData(0, hwbuf->getSizeInBytes(), _normals.ptr(), true);
161+
mesh->sharedVertexData->vertexBufferBinding->setBinding(source, hwbuf);
162+
}
163+
else
164+
{
165+
mat->setLightingEnabled(false);
166+
}
167+
168+
// indices
169+
if (!indices.empty())
170+
{
171+
Mat _indices = indices.getMat();
172+
173+
HardwareIndexBufferSharedPtr ibuf = HardwareBufferManager::getSingleton().createIndexBuffer(
174+
HardwareIndexBuffer::IT_32BIT, indices.total(), HardwareBuffer::HBU_STATIC_WRITE_ONLY);
175+
ibuf->writeData(0, ibuf->getSizeInBytes(), _indices.ptr(), true);
176+
177+
sub->indexData->indexBuffer = ibuf;
178+
sub->indexData->indexStart = 0;
179+
sub->indexData->indexCount = indices.total();
180+
}
181+
182+
AxisAlignedBox bounds(AxisAlignedBox::EXTENT_NULL);
183+
for (int i = 0; i < n; i++)
184+
{
185+
Vec3f v = _vertices.at<Vec3f>(i);
186+
bounds.merge(Vector3(v[0], v[1], v[2]));
187+
}
188+
mesh->_setBounds(bounds);
189+
}
190+
105191
void createGridMesh(const String& name, const Size2f& size, const Size& segments)
106192
{
107193
CV_Assert_N(_app, !segments.empty());

0 commit comments

Comments
 (0)