WIP c3t3 not picking

This commit is contained in:
Maxime Gimeno 2019-12-20 15:40:36 +01:00
parent cf816d2b98
commit cbf8e33009
8 changed files with 212 additions and 47 deletions

View File

@ -347,6 +347,7 @@ void Scene_edit_polyhedron_item_priv::compute_normals_and_vertices(Mesh* mesh)
typedef typename boost::property_map<Mesh, boost::vertex_point_t>::type VertexPointMap;
VertexPointMap pmap = get(boost::vertex_point, *mesh);
int s_index = 0;
for(mesh_vd vd : fs.get_deform_mesh(mesh)->roi_vertices())
{
if(!fs.get_deform_mesh(mesh)->is_control_vertex(vd))
@ -360,7 +361,7 @@ void Scene_edit_polyhedron_item_priv::compute_normals_and_vertices(Mesh* mesh)
{
CGAL::Color c(0,255,0);
EPICK::Point_3 point(p.x()+offset.x, p.y()+offset.y, p.z()+offset.z);
spheres->add_sphere(EPICK::Sphere_3(point, length_of_axis/15.0*length_of_axis/15.0), c);
spheres->add_sphere(EPICK::Sphere_3(point, length_of_axis/15.0*length_of_axis/15.0), s_index++, c);
}
}
@ -400,7 +401,7 @@ void Scene_edit_polyhedron_item_priv::compute_normals_and_vertices(Mesh* mesh)
EPICK::Point_3 center(p.x()+offset.x,
p.y()+offset.y,
p.z()+offset.z);
spheres_ctrl->add_sphere(EPICK::Sphere_3(center, length_of_axis/15.0*length_of_axis/15.0), c);
spheres_ctrl->add_sphere(EPICK::Sphere_3(center, length_of_axis/15.0*length_of_axis/15.0), s_index++, c);
}
}
}
@ -1271,7 +1272,7 @@ void Scene_edit_polyhedron_item::ShowAsSphere(bool b)
{
if(!d->spheres)
{
d->spheres = new Scene_spheres_item(this, false);
d->spheres = new Scene_spheres_item(this, 0, false);
d->spheres->setName("ROI spheres");
d->spheres->setRenderingMode(Gouraud);
connect(d->spheres, SIGNAL(destroyed()), this, SLOT(reset_spheres()));

View File

@ -1390,7 +1390,7 @@ void Scene_c3t3_item_priv::computeSpheres()
if(!spheres)
return;
int s_id = 0;
for(Tr::Finite_vertices_iterator
vit = c3t3.triangulation().finite_vertices_begin(),
end = c3t3.triangulation().finite_vertices_end();
@ -1436,7 +1436,7 @@ void Scene_c3t3_item_priv::computeSpheres()
wp2p(vit->point()).z() + offset.z);
float radius = vit->point().weight() ;
typedef unsigned char UC;
spheres->add_sphere(Geom_traits::Sphere_3(center, radius),
spheres->add_sphere(Geom_traits::Sphere_3(center, radius),s_id++,
CGAL::Color(UC(c.red()), UC(c.green()), UC(c.blue())));
}
spheres->invalidateOpenGLBuffers();
@ -1602,7 +1602,7 @@ void Scene_c3t3_item::show_spheres(bool b)
contextMenu()->findChild<QAction*>("actionShowSpheres")->setChecked(b);
if(b && !d->spheres)
{
d->spheres = new Scene_spheres_item(this, true);
d->spheres = new Scene_spheres_item(this, d->c3t3.number_of_vertices_in_complex(), true);
d->spheres->setName("Protecting spheres");
d->spheres->setRenderingMode(Gouraud);
connect(d->spheres, SIGNAL(destroyed()), this, SLOT(reset_spheres()));

View File

@ -218,6 +218,7 @@ Scene_polylines_item_private::computeSpheres()
// At this point, 'corner_polyline_nb' gives the multiplicity of all
// corners.
//Finds the centers of the spheres and their color
int s_id = 0;
for(iterator
p_it = corner_polyline_nb.begin(),
end = corner_polyline_nb.end();
@ -253,7 +254,7 @@ Scene_polylines_item_private::computeSpheres()
}
CGAL::Color c(colors[0], colors[1], colors[2]);
spheres->add_sphere(K::Sphere_3(center+offset, spheres_drawn_square_radius), c);
spheres->add_sphere(K::Sphere_3(center+offset, spheres_drawn_square_radius),s_id++, c);
}
spheres->setToolTip(
QString("<p>Legend of endpoints colors: <ul>"
@ -560,7 +561,7 @@ void Scene_polylines_item::change_corner_radii(double r) {
d->draw_extremities = (r > 0);
if(r>0 && !d->spheres)
{
d->spheres = new Scene_spheres_item(this, false);
d->spheres = new Scene_spheres_item(this, 0, false);
d->spheres->setName("Corner spheres");
d->spheres->setRenderingMode(Gouraud);
connect(d->spheres, SIGNAL(destroyed()), this, SLOT(reset_spheres()));

View File

@ -1,4 +1,5 @@
#include "Scene_spheres_item.h"
#include<fstream>
#include <CGAL/Three/Triangle_container.h>
#include <CGAL/Three/Edge_container.h>
#include <QApplication>
@ -12,11 +13,13 @@ typedef Edge_container Ec;
struct Scene_spheres_item_priv
{
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef CGAL::Sphere_3<Kernel> Sphere;
typedef std::pair<Sphere, CGAL::Color> Sphere_pair;
typedef std::vector<std::vector<Sphere_pair> > Spheres_container;
Scene_spheres_item_priv(bool planed, Scene_spheres_item* parent)
Scene_spheres_item_priv(bool planed, std::size_t max_index, Scene_spheres_item* parent)
:precision(36)
,has_plane(planed)
{
item = parent;
create_flat_and_wire_sphere(1.0f,vertices,normals, edges);
@ -24,6 +27,7 @@ struct Scene_spheres_item_priv
edges_colors.clear();
centers.clear();
radius.clear();
spheres.resize(max_index + 1);
nb_centers = 0;
nb_edges = 0;
nb_vertices = 0;
@ -33,6 +37,9 @@ struct Scene_spheres_item_priv
~Scene_spheres_item_priv()
{
}
void pick(int id)const;
void initializeBuffers(CGAL::Three::Viewer_interface *viewer)const;
int precision;
@ -44,6 +51,7 @@ struct Scene_spheres_item_priv
mutable std::vector<float> edges;
mutable std::vector<float> colors;
mutable std::vector<float> edges_colors;
mutable std::vector<float> picking_colors;
mutable std::vector<float> centers;
mutable std::vector<float> radius;
mutable QOpenGLShaderProgram *program;
@ -53,12 +61,49 @@ struct Scene_spheres_item_priv
mutable bool model_sphere_is_up;
Scene_spheres_item* item;
QString tooltip;
mutable Spheres_container spheres;
};
Scene_spheres_item::Scene_spheres_item(Scene_group_item* parent, bool planed)
void Scene_spheres_item_priv::pick(int id) const
{
int offset = 0;
float color[4];
for(std::size_t i=0; i<spheres.size(); ++i)
{
for( std::size_t j = 0; j< spheres[i].size(); ++j)
{
if(id == -1 || i != static_cast<std::size_t>(id))
{
color[0]=spheres[i][j].second.red()/255.0;
color[1]=spheres[i][j].second.green()/255.0;
color[2]=spheres[i][j].second.blue()/255.0;
}
else
{
color[0]=1.0f;
color[1]=1.0f;
color[2]=0.0f;
}
Vbo* color_vbo = item->getTriangleContainer(0)->getVbo(Tc::FColors);
color_vbo->bind();
color_vbo->vbo.write(offset*3*sizeof(float), color, 3*sizeof(float));
color_vbo->release();
++offset;
}
}
}
Scene_spheres_item::Scene_spheres_item(Scene_group_item* parent, std::size_t max_index, bool planed)
{
setParent(parent);
d = new Scene_spheres_item_priv(planed, this);
d = new Scene_spheres_item_priv(planed, max_index, this);
//for picking
setTriangleContainer(1,
new Tc(planed ? Vi::PROGRAM_CUTPLANE_SPHERES
: Vi::PROGRAM_SPHERES
,false));
//for drawing
setTriangleContainer(0,
new Tc(planed ? Vi::PROGRAM_CUTPLANE_SPHERES
: Vi::PROGRAM_SPHERES
@ -79,6 +124,10 @@ void Scene_spheres_item_priv::initializeBuffers(CGAL::Three::Viewer_interface *v
item->getTriangleContainer(0)->initializeBuffers(viewer);
item->getTriangleContainer(0)->setFlatDataSize(nb_vertices);
item->getTriangleContainer(0)->setCenterSize(nb_centers);
item->getTriangleContainer(1)->initializeBuffers(viewer);
item->getTriangleContainer(1)->setFlatDataSize(nb_vertices);
item->getTriangleContainer(1)->setCenterSize(nb_centers);
vertices.clear();
vertices.shrink_to_fit();
item->getEdgeContainer(0)->initializeBuffers(viewer);
@ -91,6 +140,8 @@ void Scene_spheres_item_priv::initializeBuffers(CGAL::Three::Viewer_interface *v
centers.swap(centers);
colors.clear();
colors.swap(colors);
picking_colors.clear();
picking_colors.shrink_to_fit();
radius.clear();
radius.swap(radius);
edges_colors.clear();
@ -113,13 +164,41 @@ void Scene_spheres_item::draw(Viewer_interface *viewer) const
setBuffersFilled(true);
setBuffersInit(viewer, true);
}
int deviceWidth = viewer->camera()->screenWidth();
int deviceHeight = viewer->camera()->screenHeight();
if(d->has_plane)
{
QVector4D cp(d->plane.a(),d->plane.b(),d->plane.c(),d->plane.d());
getTriangleContainer(0)->setPlane(cp);
}
if(d->spheres.size() > 1 && viewer->inDrawWithNames())
{
getTriangleContainer(1)->allocate(Tc::Flat_normals, 0, 0);
getTriangleContainer(1)->getVao(viewer)->program->setAttributeValue("normals", QVector3D(0,0,0));
getTriangleContainer(1)->draw(viewer, false);
}
else
{
getTriangleContainer(0)->draw(viewer, false);
}
if(d->spheres.size() > 1 && viewer->inDrawWithNames())
{
int rowLength = deviceWidth * 4; // data asked in RGBA,so 4 bytes.
const static int dataLength = rowLength * deviceHeight;
GLubyte* buffer = new GLubyte[dataLength];
// Qt uses upper corner for its origin while GL uses the lower corner.
QPoint picking_target = viewer->mapFromGlobal(QCursor::pos());
viewer->glReadPixels(picking_target.x(), deviceHeight-1-picking_target.y(), 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
int ID = (buffer[0] + buffer[1] * 256 +buffer[2] * 256*256) ;
if(buffer[0]*buffer[1]*buffer[2] < 255*255*255)
{
d->pick(ID);
viewer->displayMessage(QString("Picked spheres index : %1 .").arg(ID), 2000);
}
else
d->pick(-1);
}
}
void Scene_spheres_item::drawEdges(Viewer_interface *viewer) const
{
@ -146,11 +225,25 @@ void Scene_spheres_item::drawEdges(Viewer_interface *viewer) const
getEdgeContainer(0)->draw(viewer, false);
}
void Scene_spheres_item::add_sphere(const CGAL::Sphere_3<Kernel>& sphere, CGAL::Color color)
void Scene_spheres_item::add_sphere(const Sphere &sphere, std::size_t index, CGAL::Color color)
{
if(index > d->spheres.size()-1)
d->spheres.resize(index+1);
d->spheres[index].push_back(std::make_pair(sphere, color));
d->colors.push_back((float)color.red()/255);
d->colors.push_back((float)color.green()/255);
d->colors.push_back((float)color.blue()/255);
int R = (index & 0x000000FF) >> 0;
int G = (index & 0x0000FF00) >> 8;
int B = (index & 0x00FF0000) >> 16;
float r = R/255.0;
float g = G/255.0;
float b = B/255.0;
d->picking_colors.push_back(r);
d->picking_colors.push_back(g);
d->picking_colors.push_back(b);
d->edges_colors.push_back((float)color.red()/255);
d->edges_colors.push_back((float)color.green()/255);
@ -176,6 +269,7 @@ void Scene_spheres_item::invalidateOpenGLBuffers()
{
setBuffersFilled(false);
getTriangleContainer(0)->reset_vbos(NOT_INSTANCED);
getTriangleContainer(1)->reset_vbos(NOT_INSTANCED);
getEdgeContainer(0)->reset_vbos(NOT_INSTANCED);
}
@ -209,6 +303,11 @@ void Scene_spheres_item::computeElements() const
getTriangleContainer(0)->allocate(Tc::Flat_normals, d->normals.data(),
static_cast<int>(d->normals.size()*sizeof(float)));
getTriangleContainer(1)->allocate(Tc::Flat_vertices, d->vertices.data(),
static_cast<int>(d->vertices.size()*sizeof(float)));
d->nb_vertices = d->vertices.size();
}
@ -216,9 +315,16 @@ void Scene_spheres_item::computeElements() const
static_cast<int>(d->colors.size()*sizeof(float)));
getTriangleContainer(0)->allocate(Tc::Radius, d->radius.data(),
static_cast<int>(d->radius.size()*sizeof(float)));
getTriangleContainer(0)->allocate(Tc::Facet_centers, d->centers.data(),
static_cast<int>(d->centers.size()*sizeof(float)));
getTriangleContainer(1)->allocate(Tc::FColors, d->picking_colors.data(),
static_cast<int>(d->picking_colors.size()*sizeof(float)));
getTriangleContainer(1)->allocate(Tc::Radius, d->radius.data(),
static_cast<int>(d->radius.size()*sizeof(float)));
getTriangleContainer(1)->allocate(Tc::Facet_centers, d->centers.data(),
static_cast<int>(d->centers.size()*sizeof(float)));
if(!d->model_sphere_is_up)
{
getEdgeContainer(0)->allocate(Ec::Vertices, d->edges.data(),
@ -237,7 +343,6 @@ void Scene_spheres_item::computeElements() const
getEdgeContainer(0)->allocate(Ec::Centers, d->centers.data(),
static_cast<int>(d->centers.size()*sizeof(float)));
d->nb_centers = d->centers.size();
}
@ -254,3 +359,39 @@ void Scene_spheres_item::gl_initialization(Vi* viewer)
setBuffersFilled(true);
}
}
void Scene_spheres_item::compute_bbox() const
{
Bbox box = Bbox();
for(std::size_t id=0; id<d->spheres.size(); ++id)
{
for(Sphere_pair pair : d->spheres[id])
{
box += pair.first.bbox();
}
}
_bbox = box;
}
bool Scene_spheres_item::save(const std::string& file_name)const
{
std::ofstream out(file_name.c_str());
if(!out) { return false; }
std::size_t nb_spheres=0;
for(std::size_t i = 0; i<d->spheres.size(); ++i)
nb_spheres += d->spheres[i].size();
out<<nb_spheres << " " << d->spheres.size() -1<<"\n";
for(std::size_t i = 0; i<d->spheres.size(); ++i)
{
for(const Sphere_pair& pair : d->spheres[i])
{
out << i << " " << pair.first.center() << " " << CGAL::sqrt(pair.first.squared_radius())<<"\n";
}
}
out << "\n";
return true;
}

View File

@ -15,27 +15,37 @@
#include <QList>
#include <vector>
struct Scene_spheres_item_priv;
/* This item contains spheres and associated colors. They are kept in a Spheres_container,
* sorted by the value of their "index". This item also has an internal picking mechanism that
* colorizes all the spheres that has the same index as the one that has been picked.
* The picking is only usable if several indices exist.
* If all the spheres have the index 0, they can have independant colors (generally used by the items that
* have a Scene_spheres_item child).
*/
class SCENE_BASIC_OBJECTS_EXPORT Scene_spheres_item
: public CGAL::Three::Scene_item_rendering_helper
{
Q_OBJECT
public:
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef std::pair<CGAL::Sphere_3<Kernel>*, CGAL::Color> Sphere_pair ;
typedef CGAL::Sphere_3<Kernel> Sphere;
typedef std::pair<Sphere, CGAL::Color> Sphere_pair;
typedef std::vector<std::vector<Sphere_pair> > Spheres_container;
Scene_spheres_item(Scene_group_item* parent, bool planed = false);
Scene_spheres_item(Scene_group_item* parent, std::size_t max_index = 0, bool planed = false);
~Scene_spheres_item();
bool isFinite() const Q_DECL_OVERRIDE{ return false; }
bool isFinite() const Q_DECL_OVERRIDE{ return true; }
bool isEmpty() const Q_DECL_OVERRIDE{ return false; }
Scene_item* clone() const Q_DECL_OVERRIDE{return 0;}
QString toolTip() const Q_DECL_OVERRIDE;
bool supportsRenderingMode(RenderingMode m) const Q_DECL_OVERRIDE{
return (m == Gouraud || m == Wireframe);
}
void compute_bbox() const Q_DECL_OVERRIDE{ _bbox = Bbox(); }
void add_sphere(const CGAL::Sphere_3<Kernel> &sphere, CGAL::Color = CGAL::Color(120,120,120));
void compute_bbox() const Q_DECL_OVERRIDE;
void add_sphere(const Sphere &sphere, std::size_t index = 0, CGAL::Color = CGAL::Color(120,120,120));
void clear_spheres();
void setPrecision(int prec);
void gl_initialization(CGAL::Three::Viewer_interface* viewer);
@ -45,6 +55,9 @@ public:
void setPlane(Kernel::Plane_3 p_plane);
void setToolTip(QString s);
void setColor(QColor c) Q_DECL_OVERRIDE;
bool save(const std::string &file_name) const;
void initializeBuffers(Viewer_interface *) const Q_DECL_OVERRIDE;
void computeElements() const Q_DECL_OVERRIDE;
Q_SIGNALS:

View File

@ -50,16 +50,19 @@ void main(void) {
highp vec3 L = light_pos.xyz - fP.xyz;
highp vec3 V = -fP.xyz;
highp vec3 N;
highp vec4 my_color = highp vec4(color.xyz, 1.0);
if(fN == vec3(0.0,0.0,0.0))
N = vec3(0.0,0.0,0.0);
else
{
out_color = my_color;
return;
}
N = normalize(fN);
L = normalize(L);
V = normalize(V);
highp vec3 R = reflect(-L, N);
highp vec4 diffuse;
float dot_prod = dot(N,L);
highp vec4 my_color;
if(back_front_shading)
{
if (dot_prod > 0)
@ -67,10 +70,7 @@ void main(void) {
else
my_color = back_color;
}
else
{
my_color = highp vec4(color.xyz, 1.0);
}
if(is_two_side == 1)
diffuse = abs(dot(N,L)) * light_diff * color;
else

View File

@ -42,7 +42,10 @@ void main(void) {
vec3 V = -fP.xyz;
vec3 N;
if(fN == vec3(0.0,0.0,0.0))
N = vec3(0.0,0.0,0.0);
{
out_color = my_color;
return;
}
else
N = normalize(fN);
L = normalize(L);

View File

@ -51,6 +51,12 @@ void main(void) {
vec3 L = light_pos.xyz - fP.xyz;
vec3 V = -fP.xyz;
vec3 N;
vec4 my_color = vec4(color.xyz, 1.0);
if(fN == vec3(0.0,0.0,0.0))
{
out_color = my_color;
return;
}
if(fN == vec3(0.0,0.0,0.0))
N = vec3(0.0,0.0,0.0);
else
@ -60,7 +66,7 @@ void main(void) {
vec3 R = reflect(-L, N);
vec4 diffuse;
float dot_prod = dot(N,L);
vec4 my_color;
if(back_front_shading)
{
if (dot_prod > 0)