refactored Mesh, added tests, added failing "twice spin()" test
This commit is contained in:
parent
70a81db567
commit
baee8c3aa5
@ -65,14 +65,14 @@ namespace cv
|
|||||||
SHADING
|
SHADING
|
||||||
};
|
};
|
||||||
|
|
||||||
enum RenderingRepresentationProperties
|
enum RepresentationValues
|
||||||
{
|
{
|
||||||
REPRESENTATION_POINTS,
|
REPRESENTATION_POINTS,
|
||||||
REPRESENTATION_WIREFRAME,
|
REPRESENTATION_WIREFRAME,
|
||||||
REPRESENTATION_SURFACE
|
REPRESENTATION_SURFACE
|
||||||
};
|
};
|
||||||
|
|
||||||
enum ShadingRepresentationProperties
|
enum ShadingValues
|
||||||
{
|
{
|
||||||
SHADING_FLAT,
|
SHADING_FLAT,
|
||||||
SHADING_GOURAUD,
|
SHADING_GOURAUD,
|
||||||
|
@ -126,49 +126,47 @@ void cv::viz::WCloudCollection::addCloud(InputArray cloud, InputArray colors, co
|
|||||||
transform_filter->SetTransform(transform);
|
transform_filter->SetTransform(transform);
|
||||||
transform_filter->Update();
|
transform_filter->Update();
|
||||||
|
|
||||||
|
vtkSmartPointer<vtkPolyData> polydata = transform_filter->GetOutput();
|
||||||
vtkSmartPointer<vtkLODActor> actor = vtkLODActor::SafeDownCast(WidgetAccessor::getProp(*this));
|
vtkSmartPointer<vtkLODActor> actor = vtkLODActor::SafeDownCast(WidgetAccessor::getProp(*this));
|
||||||
CV_Assert("Incompatible widget type." && actor);
|
CV_Assert("Incompatible widget type." && actor);
|
||||||
|
|
||||||
vtkSmartPointer<vtkPolyData> poly_data = transform_filter->GetOutput();
|
|
||||||
|
|
||||||
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkPolyDataMapper::SafeDownCast(actor->GetMapper());
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkPolyDataMapper::SafeDownCast(actor->GetMapper());
|
||||||
|
|
||||||
if (!mapper)
|
if (!mapper)
|
||||||
{
|
{
|
||||||
// This is the first cloud
|
// This is the first cloud
|
||||||
mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
||||||
#if VTK_MAJOR_VERSION <= 5
|
#if VTK_MAJOR_VERSION <= 5
|
||||||
mapper->SetInput(poly_data);
|
mapper->SetInput(polydata);
|
||||||
#else
|
#else
|
||||||
mapper->SetInputData(poly_data);
|
mapper->SetInputData(polydata);
|
||||||
#endif
|
#endif
|
||||||
mapper->SetScalarRange(0, 255);
|
mapper->SetScalarRange(0, 255);
|
||||||
mapper->SetScalarModeToUsePointData();
|
mapper->SetScalarModeToUsePointData();
|
||||||
mapper->ScalarVisibilityOn();
|
mapper->ScalarVisibilityOn();
|
||||||
mapper->ImmediateModeRenderingOff();
|
mapper->ImmediateModeRenderingOff();
|
||||||
|
|
||||||
actor->SetNumberOfCloudPoints(std::max(1, poly_data->GetNumberOfPoints()/10));
|
actor->SetNumberOfCloudPoints(std::max(1, polydata->GetNumberOfPoints()/10));
|
||||||
actor->GetProperty()->SetInterpolationToFlat();
|
actor->GetProperty()->SetInterpolationToFlat();
|
||||||
actor->GetProperty()->BackfaceCullingOn();
|
actor->GetProperty()->BackfaceCullingOn();
|
||||||
actor->SetMapper(mapper);
|
actor->SetMapper(mapper);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
vtkPolyData *data = vtkPolyData::SafeDownCast(mapper->GetInput());
|
vtkPolyData *currdata = vtkPolyData::SafeDownCast(mapper->GetInput());
|
||||||
CV_Assert("Cloud Widget without data" && data);
|
CV_Assert("Cloud Widget without data" && currdata);
|
||||||
|
|
||||||
vtkSmartPointer<vtkAppendPolyData> appendFilter = vtkSmartPointer<vtkAppendPolyData>::New();
|
vtkSmartPointer<vtkAppendPolyData> appendFilter = vtkSmartPointer<vtkAppendPolyData>::New();
|
||||||
#if VTK_MAJOR_VERSION <= 5
|
#if VTK_MAJOR_VERSION <= 5
|
||||||
appendFilter->AddInput(data);
|
appendFilter->AddInput(currdata);
|
||||||
appendFilter->AddInput(poly_data);
|
appendFilter->AddInput(polydata);
|
||||||
mapper->SetInput(appendFilter->GetOutput());
|
mapper->SetInput(appendFilter->GetOutput());
|
||||||
#else
|
#else
|
||||||
appendFilter->AddInputData(data);
|
appendFilter->AddInputData(currdata);
|
||||||
appendFilter->AddInputData(poly_data);
|
appendFilter->AddInputData(polydata);
|
||||||
mapper->SetInputData(appendFilter->GetOutput());
|
mapper->SetInputData(appendFilter->GetOutput());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
actor->SetNumberOfCloudPoints(std::max(1, actor->GetNumberOfCloudPoints() + poly_data->GetNumberOfPoints()/10));
|
actor->SetNumberOfCloudPoints(std::max(1, actor->GetNumberOfCloudPoints() + polydata->GetNumberOfPoints()/10));
|
||||||
}
|
}
|
||||||
|
|
||||||
void cv::viz::WCloudCollection::addCloud(InputArray cloud, const Color &color, const Affine3d &pose)
|
void cv::viz::WCloudCollection::addCloud(InputArray cloud, const Color &color, const Affine3d &pose)
|
||||||
@ -328,150 +326,78 @@ template<> cv::viz::WCloudNormals cv::viz::Widget::cast<cv::viz::WCloudNormals>(
|
|||||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
/// Mesh Widget implementation
|
/// Mesh Widget implementation
|
||||||
|
|
||||||
namespace cv { namespace viz { namespace
|
|
||||||
{
|
|
||||||
struct MeshUtils
|
|
||||||
{
|
|
||||||
template<typename _Tp>
|
|
||||||
static Vec<_Tp, 3> * copy(const Mat &source, Vec<_Tp, 3> *output, int *look_up, const Mat &nan_mask)
|
|
||||||
{
|
|
||||||
CV_Assert(DataDepth<_Tp>::value == source.depth() && source.size() == nan_mask.size());
|
|
||||||
CV_Assert(nan_mask.channels() == 3 || nan_mask.channels() == 4);
|
|
||||||
CV_DbgAssert(DataDepth<_Tp>::value == nan_mask.depth());
|
|
||||||
|
|
||||||
int s_chs = source.channels();
|
|
||||||
int m_chs = nan_mask.channels();
|
|
||||||
|
|
||||||
int index = 0;
|
|
||||||
const _Tp* srow = source.ptr<_Tp>(0);
|
|
||||||
const _Tp* mrow = nan_mask.ptr<_Tp>(0);
|
|
||||||
|
|
||||||
for (int x = 0; x < source.cols; ++x, srow += s_chs, mrow += m_chs)
|
|
||||||
{
|
|
||||||
if (!isNan(mrow[0]) && !isNan(mrow[1]) && !isNan(mrow[2]))
|
|
||||||
{
|
|
||||||
look_up[x] = index;
|
|
||||||
*output++ = Vec<_Tp, 3>(srow);
|
|
||||||
++index;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}}}
|
|
||||||
|
|
||||||
cv::viz::WMesh::WMesh(const Mesh3d &mesh)
|
cv::viz::WMesh::WMesh(const Mesh3d &mesh)
|
||||||
{
|
{
|
||||||
CV_Assert(mesh.cloud.rows == 1 && (mesh.cloud.type() == CV_32FC3 || mesh.cloud.type() == CV_64FC3 || mesh.cloud.type() == CV_32FC4 || mesh.cloud.type() == CV_64FC4));
|
CV_Assert(mesh.cloud.rows == 1 && mesh.polygons.type() == CV_32SC1);
|
||||||
CV_Assert(mesh.colors.empty() || (mesh.colors.depth() == CV_8U && mesh.cloud.size() == mesh.colors.size()));
|
|
||||||
CV_Assert(!mesh.polygons.empty() && mesh.polygons.type() == CV_32SC1);
|
|
||||||
|
|
||||||
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
|
vtkSmartPointer<vtkCloudMatSource> source = vtkSmartPointer<vtkCloudMatSource>::New();
|
||||||
vtkIdType nr_points = mesh.cloud.total();
|
source->SetColorCloud(mesh.cloud, mesh.colors);
|
||||||
Mat look_up_mat(1, nr_points, CV_32SC1);
|
source->Update();
|
||||||
int * look_up = look_up_mat.ptr<int>();
|
|
||||||
points->SetNumberOfPoints(nr_points);
|
|
||||||
|
|
||||||
// Copy data from cloud to vtkPoints
|
Mat lookup_buffer(1, mesh.cloud.total(), CV_32SC1);
|
||||||
if (mesh.cloud.depth() == CV_32F)
|
int *lookup = lookup_buffer.ptr<int>();
|
||||||
|
for(int y = 0, index = 0; y < mesh.cloud.rows; ++y)
|
||||||
{
|
{
|
||||||
points->SetDataTypeToFloat();
|
int s_chs = mesh.cloud.channels();
|
||||||
Vec3f *data_beg = vtkpoints_data<float>(points);
|
|
||||||
Vec3f *data_end = MeshUtils::copy(mesh.cloud, data_beg, look_up, mesh.cloud);
|
|
||||||
nr_points = data_end - data_beg;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
points->SetDataTypeToDouble();
|
|
||||||
Vec3d *data_beg = vtkpoints_data<double>(points);
|
|
||||||
Vec3d *data_end = MeshUtils::copy(mesh.cloud, data_beg, look_up, mesh.cloud);
|
|
||||||
nr_points = data_end - data_beg;
|
|
||||||
}
|
|
||||||
|
|
||||||
vtkSmartPointer<vtkUnsignedCharArray> scalars;
|
if (mesh.cloud.depth() == CV_32F)
|
||||||
|
|
||||||
if (!mesh.colors.empty())
|
|
||||||
{
|
|
||||||
Vec3b *colors_data = new Vec3b[nr_points];
|
|
||||||
NanFilter::copyColor(mesh.colors, colors_data, mesh.cloud);
|
|
||||||
|
|
||||||
scalars = vtkSmartPointer<vtkUnsignedCharArray>::New();
|
|
||||||
scalars->SetNumberOfComponents(3);
|
|
||||||
scalars->SetNumberOfTuples(nr_points);
|
|
||||||
scalars->SetArray(colors_data->val, 3 * nr_points, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
points->SetNumberOfPoints(nr_points);
|
|
||||||
|
|
||||||
vtkSmartPointer<vtkPointSet> data;
|
|
||||||
|
|
||||||
if (mesh.polygons.size().area() > 1)
|
|
||||||
{
|
|
||||||
vtkSmartPointer<vtkCellArray> cell_array = vtkSmartPointer<vtkCellArray>::New();
|
|
||||||
const int * polygons = mesh.polygons.ptr<int>();
|
|
||||||
|
|
||||||
int idx = 0;
|
|
||||||
int poly_size = mesh.polygons.total();
|
|
||||||
for (int i = 0; i < poly_size; ++idx)
|
|
||||||
{
|
{
|
||||||
int n_points = polygons[i++];
|
const float* srow = mesh.cloud.ptr<float>(y);
|
||||||
|
const float* send = srow + mesh.cloud.cols * s_chs;
|
||||||
|
|
||||||
cell_array->InsertNextCell(n_points);
|
for (; srow != send; srow += s_chs, ++lookup)
|
||||||
for (int j = 0; j < n_points; ++j, ++idx)
|
if (!isNan(srow[0]) && !isNan(srow[1]) && !isNan(srow[2]))
|
||||||
cell_array->InsertCellPoint(look_up[polygons[i++]]);
|
*lookup = index++;
|
||||||
}
|
}
|
||||||
vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
|
|
||||||
cell_array->GetData()->SetNumberOfValues(idx);
|
|
||||||
cell_array->Squeeze();
|
|
||||||
polydata->SetStrips(cell_array);
|
|
||||||
polydata->SetPoints(points);
|
|
||||||
|
|
||||||
if (scalars)
|
if (mesh.cloud.depth() == CV_64F)
|
||||||
polydata->GetPointData()->SetScalars(scalars);
|
{
|
||||||
|
const double* srow = mesh.cloud.ptr<double>(y);
|
||||||
|
const double* send = srow + mesh.cloud.cols * s_chs;
|
||||||
|
|
||||||
data = polydata;
|
for (; srow != send; srow += s_chs, ++lookup)
|
||||||
|
if (!isNan(srow[0]) && !isNan(srow[1]) && !isNan(srow[2]))
|
||||||
|
*lookup = index++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
lookup = lookup_buffer.ptr<int>();
|
||||||
|
|
||||||
|
vtkSmartPointer<vtkPolyData> polydata = source->GetOutput();
|
||||||
|
polydata->SetVerts(0);
|
||||||
|
|
||||||
|
const int * polygons = mesh.polygons.ptr<int>();
|
||||||
|
vtkSmartPointer<vtkCellArray> cell_array = vtkSmartPointer<vtkCellArray>::New();
|
||||||
|
|
||||||
|
int idx = 0;
|
||||||
|
int poly_size = mesh.polygons.total();
|
||||||
|
for (int i = 0; i < poly_size; ++idx)
|
||||||
{
|
{
|
||||||
// Only one polygon
|
int n_points = polygons[i++];
|
||||||
vtkSmartPointer<vtkPolygon> polygon = vtkSmartPointer<vtkPolygon>::New();
|
|
||||||
const int * polygons = mesh.polygons.ptr<int>();
|
|
||||||
int n_points = polygons[0];
|
|
||||||
|
|
||||||
polygon->GetPointIds()->SetNumberOfIds(n_points);
|
cell_array->InsertNextCell(n_points);
|
||||||
|
for (int j = 0; j < n_points; ++j, ++idx)
|
||||||
for (int j = 1; j < n_points+1; ++j)
|
cell_array->InsertCellPoint(lookup[polygons[i++]]);
|
||||||
polygon->GetPointIds()->SetId(j, look_up[polygons[j]]);
|
|
||||||
|
|
||||||
vtkSmartPointer<vtkUnstructuredGrid> poly_grid = vtkSmartPointer<vtkUnstructuredGrid>::New();
|
|
||||||
poly_grid->Allocate(1, 1);
|
|
||||||
poly_grid->InsertNextCell(polygon->GetCellType(), polygon->GetPointIds());
|
|
||||||
poly_grid->SetPoints(points);
|
|
||||||
|
|
||||||
if (scalars)
|
|
||||||
poly_grid->GetPointData()->SetScalars(scalars);
|
|
||||||
|
|
||||||
data = poly_grid;
|
|
||||||
}
|
}
|
||||||
|
cell_array->GetData()->SetNumberOfValues(idx);
|
||||||
|
cell_array->Squeeze();
|
||||||
|
polydata->SetStrips(cell_array);
|
||||||
|
|
||||||
vtkSmartPointer<vtkLODActor> actor = vtkSmartPointer<vtkLODActor>::New();
|
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New();
|
||||||
|
mapper->SetScalarModeToUsePointData();
|
||||||
|
mapper->ImmediateModeRenderingOff();
|
||||||
|
#if VTK_MAJOR_VERSION <= 5
|
||||||
|
mapper->SetInput(polydata);
|
||||||
|
#else
|
||||||
|
mapper->SetInputData(polydata);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
||||||
|
//actor->SetNumberOfCloudPoints(std::max(1, polydata->GetNumberOfPoints() / 10));
|
||||||
actor->GetProperty()->SetRepresentationToSurface();
|
actor->GetProperty()->SetRepresentationToSurface();
|
||||||
actor->GetProperty()->BackfaceCullingOff(); // Backface culling is off for higher efficiency
|
actor->GetProperty()->BackfaceCullingOff(); // Backface culling is off for higher efficiency
|
||||||
actor->GetProperty()->SetInterpolationToFlat();
|
actor->GetProperty()->SetInterpolationToFlat();
|
||||||
actor->GetProperty()->EdgeVisibilityOff();
|
actor->GetProperty()->EdgeVisibilityOff();
|
||||||
actor->GetProperty()->ShadingOff();
|
actor->GetProperty()->ShadingOff();
|
||||||
|
|
||||||
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New();
|
|
||||||
#if VTK_MAJOR_VERSION <= 5
|
|
||||||
mapper->SetInput(data);
|
|
||||||
#else
|
|
||||||
mapper->SetInputData(data);
|
|
||||||
#endif
|
|
||||||
mapper->ImmediateModeRenderingOff();
|
|
||||||
|
|
||||||
vtkIdType numberOfCloudPoints = nr_points * 0.1;
|
|
||||||
actor->SetNumberOfCloudPoints(int(numberOfCloudPoints > 1 ? numberOfCloudPoints : 1));
|
|
||||||
actor->SetMapper(mapper);
|
actor->SetMapper(mapper);
|
||||||
|
|
||||||
WidgetAccessor::setProp(*this, actor);
|
WidgetAccessor::setProp(*this, actor);
|
||||||
|
@ -163,14 +163,14 @@ int cv::viz::vtkCloudMatSource::filterNanCopy(const Mat& cloud)
|
|||||||
points->Allocate(cloud.total());
|
points->Allocate(cloud.total());
|
||||||
points->SetNumberOfPoints(cloud.total());
|
points->SetNumberOfPoints(cloud.total());
|
||||||
|
|
||||||
int cn = cloud.channels();
|
int s_chs = cloud.channels();
|
||||||
int total = 0;
|
int total = 0;
|
||||||
for (int y = 0; y < cloud.rows; ++y)
|
for (int y = 0; y < cloud.rows; ++y)
|
||||||
{
|
{
|
||||||
const _Tp* srow = cloud.ptr<_Tp>(y);
|
const _Tp* srow = cloud.ptr<_Tp>(y);
|
||||||
const _Tp* send = srow + cloud.cols * cn;
|
const _Tp* send = srow + cloud.cols * s_chs;
|
||||||
|
|
||||||
for (; srow != send; srow += cn)
|
for (; srow != send; srow += s_chs)
|
||||||
if (!isNan(srow))
|
if (!isNan(srow))
|
||||||
points->SetPoint(total++, srow);
|
points->SetPoint(total++, srow);
|
||||||
}
|
}
|
||||||
|
@ -100,3 +100,37 @@ TEST(Viz, DISABLED_show_cloud_collection)
|
|||||||
viz.showWidget("ccol", ccol);
|
viz.showWidget("ccol", ccol);
|
||||||
viz.spin();
|
viz.spin();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(Viz, DISABLED_show_mesh)
|
||||||
|
{
|
||||||
|
Mesh3d mesh = Mesh3d::load(get_dragon_ply_file_path());
|
||||||
|
|
||||||
|
Viz3d viz("show_mesh");
|
||||||
|
viz.showWidget("coosys", WCoordinateSystem());
|
||||||
|
viz.showWidget("mesh", WMesh(mesh));
|
||||||
|
viz.spin();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Viz, DISABLED_show_mesh_random_colors)
|
||||||
|
{
|
||||||
|
Mesh3d mesh = Mesh3d::load(get_dragon_ply_file_path());
|
||||||
|
theRNG().fill(mesh.colors, RNG::UNIFORM, 0, 255);
|
||||||
|
|
||||||
|
Viz3d viz("show_mesh_random_color");
|
||||||
|
viz.showWidget("coosys", WCoordinateSystem());
|
||||||
|
viz.showWidget("mesh", WMesh(mesh));
|
||||||
|
viz.setRenderingProperty("mesh", SHADING, SHADING_PHONG);
|
||||||
|
viz.spin();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Viz, DISABLED_spin_twice_____________________________TODO_UI_BUG)
|
||||||
|
{
|
||||||
|
Mesh3d mesh = Mesh3d::load(get_dragon_ply_file_path());
|
||||||
|
|
||||||
|
Viz3d viz("spin_twice");
|
||||||
|
viz.showWidget("coosys", WCoordinateSystem());
|
||||||
|
viz.showWidget("mesh", WMesh(mesh));
|
||||||
|
viz.spin();
|
||||||
|
viz.spin();
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user