Selected id displayed one by one

- Segfault when removing the item from the scene
This commit is contained in:
Maxime Gimeno 2015-12-04 16:05:25 +01:00
parent cbdbc725aa
commit d20a35d282
4 changed files with 87 additions and 22 deletions

View File

@ -401,12 +401,19 @@ Scene_polyhedron_item::initialize_buffers(CGAL::Three::Viewer_interface* viewer)
CGAL::set_halfedgeds_items_id(*poly);
TextRenderer *renderer = viewer->textRenderer;
Q_FOREACH(double id, text_ids)
renderer->removeText(id);
text_ids.clear();
QFont font;
font.setBold(true);
Q_FOREACH(Polyhedron::Vertex_const_handle vh, vertices(*poly))
{
const Point& p = vh->point();
viewer->textRenderer->addText((float)p.x(), (float)p.y(), (float)p.z(), QString("%1").arg(vh->id()), font, Qt::red);
renderer->addText((float)p.x(), (float)p.y(), (float)p.z(), QString("%1").arg(vh->id()), renderer->lastId(), font, Qt::red);
text_ids.append(renderer->lastId());
}
@ -414,7 +421,8 @@ Scene_polyhedron_item::initialize_buffers(CGAL::Three::Viewer_interface* viewer)
{
const Point& p1 = source(e, *poly)->point();
const Point& p2 = target(e, *poly)->point();
viewer->textRenderer->addText((float)(p1.x()+p2.x())/2, (float)(p1.y()+p2.y())/2, (float)(p1.z()+p2.z())/2, QString("%1").arg(e.halfedge()->id()/2), font, Qt::green);
renderer->addText((float)(p1.x()+p2.x())/2, (float)(p1.y()+p2.y())/2, (float)(p1.z()+p2.z())/2, QString("%1").arg(e.halfedge()->id()/2), renderer->lastId(), font, Qt::green);
text_ids.append(renderer->lastId());
}
Q_FOREACH(Polyhedron::Facet_handle fh, faces(*poly))
@ -429,11 +437,13 @@ Scene_polyhedron_item::initialize_buffers(CGAL::Three::Viewer_interface* viewer)
++total;
}
viewer->textRenderer->addText((float)x/total, (float)y/total, (float)z/total, QString("%1").arg(fh->id()), font, Qt::blue);
renderer->addText((float)x/total, (float)y/total, (float)z/total, QString("%1").arg(fh->id()), renderer->lastId(), font, Qt::blue);
text_ids.append(renderer->lastId());
}
}
void
Scene_polyhedron_item::compute_normals_and_vertices(const bool colors_only) const
{
@ -667,7 +677,6 @@ Scene_polyhedron_item::Scene_polyhedron_item(const Polyhedron& p)
erase_next_picked_facet_m(false),
plugin_has_set_color_vector_m(false)
{
//setItemIsMulticolor(true);
cur_shading=FlatPlusEdges;
is_selected=true;
init();
@ -681,6 +690,13 @@ Scene_polyhedron_item::~Scene_polyhedron_item()
{
delete_aabb_tree(this);
delete poly;
QGLViewer* viewer = *QGLViewer::QGLViewerPool().begin();
CGAL::Three::Viewer_interface* v = qobject_cast<CGAL::Three::Viewer_interface*>(viewer);
if(v)
{
Q_FOREACH(double id, text_ids)
v->textRenderer->removeText(id);
}
}
#include "Color_map.h"
@ -1065,6 +1081,7 @@ Scene_polyhedron_item::select(double orig_x,
double dir_y,
double dir_z)
{
if(facet_picking_m) {
typedef Input_facets_AABB_tree Tree;
typedef Tree::Object_and_primitive_id Object_and_primitive_id;
@ -1125,7 +1142,6 @@ Scene_polyhedron_item::select(double orig_x,
nearest_v = v;
}
}
//bottleneck
Q_EMIT selected_vertex((void*)(&*nearest_v));
}

View File

@ -174,12 +174,15 @@ private:
using CGAL::Three::Scene_item::initialize_buffers;
void initialize_buffers(CGAL::Three::Viewer_interface *viewer = 0) const;
void compute_normals_and_vertices(const bool colors_only = false) const;
template<typename FaceNormalPmap, typename VertexNormalPmap>
void triangulate_facet(Facet_iterator,
const FaceNormalPmap&, const VertexNormalPmap&,
const bool colors_only) const;
double volume, area;
mutable QList<double> text_ids;
int m_min_patch_id; // the min value of the patch ids initialized in init()
}; // end class Scene_polyhedron_item

View File

@ -562,6 +562,7 @@ void Viewer::attrib_buffers(int program_name) const {
void Viewer::beginSelection(const QPoint &point)
{
textRenderer->printFacetId(point, this);
makeCurrent();
glEnable(GL_SCISSOR_TEST);
glScissor(point.x(), camera()->screenHeight()-1-point.y(), 1, 1);
@ -817,7 +818,7 @@ void Viewer::drawVisualHints()
rendering_program.release();
vao[0].release();
}
bool has_text = true;
bool has_text = true ;
if(has_text)
{
//So that the text is drawn in front of everything
@ -1154,6 +1155,36 @@ void Viewer::wheelEvent(QWheelEvent* e)
QGLViewer::wheelEvent(e);
}
void TextRenderer::printFacetId(QPoint pt, CGAL::Three::Viewer_interface *viewer)
{
qDebug()<<"print";
displayList.clear();
QMap<float,double> distances;
bool found;
float min_dist = 0;
qglviewer::Vec pup = viewer->camera()->pointUnderPixel(pt, found);
if(found)
{
TextItem *it = textItems.values().first();
float dist =
(it->position()->x() - pup.x)*(it->position()->x() - pup.x)
+(it->position()->y() - pup.y)*(it->position()->y() - pup.y)
+(it->position()->z() - pup.z)*(it->position()->z() - pup.z);
min_dist = dist;
Q_FOREACH(TextItem* item, textItems.values())
{
float dist =
(item->position()->x() - pup.x)*(item->position()->x() - pup.x)
+(item->position()->y() - pup.y)*(item->position()->y() - pup.y)
+(item->position()->z() - pup.z)*(item->position()->z() - pup.z);
distances[dist] = item->id();
if(dist < min_dist)
min_dist = dist;
}
displayList.append(distances[min_dist]);
}
}
void TextRenderer::draw(CGAL::Three::Viewer_interface *viewer)
{
QPainter *painter = viewer->painter;
@ -1166,8 +1197,9 @@ void TextRenderer::draw(CGAL::Three::Viewer_interface *viewer)
QRect rect;
qglviewer::Camera* camera = viewer->camera();
//painter->setBackgroundMode(Qt::TransparentMode);
Q_FOREACH(TextItem* item, textItems)
Q_FOREACH(double i, displayList)
{
TextItem* item = textItems[i];
qglviewer::Vec src(item->position()->x(), item->position()->y(),item->position()->z());
rect = QRect(camera->projectedCoordinatesOf(src).x-item->width()/2,
camera->projectedCoordinatesOf(src).y-item->height()/2,
@ -1175,27 +1207,33 @@ void TextRenderer::draw(CGAL::Three::Viewer_interface *viewer)
item->height());
painter->setFont(item->font());
painter->setPen(QPen(item->color()));
QPoint pouaing(camera->projectedCoordinatesOf(src).x,
camera->projectedCoordinatesOf(src).y);
qglviewer::Vec v = camera->projectedCoordinatesOf(src);
QPoint pouaing((int)(v.x),
(int)(v.y));
bool found;
qglviewer::Vec pUp =viewer->camera()->pointUnderPixel(pouaing, found);
qglviewer::Vec pos_point = qglviewer::Vec(item->position()->x(), item->position()->y(), item->position()->z());
float dist =(pUp.x-pos_point.x) * (pUp.x-pos_point.x) + (pUp.y-pos_point.y) * (pUp.y-pos_point.y) + (pUp.z-pos_point.z) * (pUp.z-pos_point.z);
// qDebug()<<item->text()<<": dist = "<<dist<<", rad2 = "<<viewer->sceneRadius() * viewer->sceneRadius()/10.0;
if( dist < viewer->sceneRadius() * viewer->sceneRadius()/10000.0)
if( dist < viewer->sceneRadius()*viewer->sceneRadius()/1000.0)
painter->drawText(rect, item->text());
}
}
void TextRenderer::addText(TextItem *ti)
{
textItems.push_back(ti);
m_lastId+=0.1;
textItems[ti->id()]=ti;
}
void TextRenderer::addText(float p_x, float p_y, float p_z, QString p_text, QFont font , QColor p_color )
void TextRenderer::addText(float p_x, float p_y, float p_z, QString p_text, double p_id, QFont p_font , QColor p_color )
{
textItems.push_back(new TextItem(p_x, p_y, p_z, p_text, font, p_color));
m_lastId+=0.1;
textItems[p_id]=new TextItem(p_x, p_y, p_z, p_text, p_id, p_font, p_color);
}
void TextRenderer::removeText(double id)
{
textItems.remove(id);
}

View File

@ -139,8 +139,8 @@ protected:
//!This class holds the properties of each line of text to be rendered.
class TextItem{
public :
TextItem(float p_x, float p_y, float p_z, QString p_text, QFont font = QFont(), QColor p_color = Qt::black)
:x(p_x), y(p_y), z(p_z), m_text(p_text), m_font(font), m_color(p_color)
TextItem(float p_x, float p_y, float p_z, QString p_text, double id, QFont font = QFont(), QColor p_color = Qt::black)
:x(p_x), y(p_y), z(p_z), m_text(p_text), m_id(id), m_font(font), m_color(p_color)
{
QFontMetrics fm(m_font);
_width = fm.width(m_text);
@ -153,6 +153,7 @@ public :
float height(){return _height;}
QFont font(){return m_font;}
QColor color() {return m_color;}
double id(){return m_id;}
private:
float x;
float y;
@ -160,6 +161,7 @@ private:
float _width;
float _height;
QString m_text;
double m_id;
QFont m_font;
QColor m_color;
};//end class TextItem
@ -169,19 +171,25 @@ class TextRenderer{
public:
TextRenderer()
{
textItems.resize(0);
m_lastId = 0.0;
}
/*!
* Projects each textItem from the world coordinates to the Screen coordinates
* and draws it.
*/
void draw(CGAL::Three::Viewer_interface* viewer);
void draw(CGAL::Three::Viewer_interface* viewer, TextItem* item);
void addText(TextItem* ti);
void addText(float p_x, float p_y, float p_z, QString p_text, QFont font = QFont(), QColor p_color = Qt::black);
void addText(float p_x, float p_y, float p_z, QString p_text, double p_id, QFont font = QFont(), QColor p_color = Qt::black);
void removeText(double id);
double lastId()const{return m_lastId;}
QMap<double,TextItem*> items() const{return textItems;}
void printFacetId(QPoint pt, CGAL::Three::Viewer_interface*);
private:
std::vector<TextItem*> textItems;
QList<double> displayList;
QMap<double,TextItem*> textItems;
QOpenGLFramebufferObject *fbo;
double m_lastId;
};//end class TextRenderer
#endif // VIEWER_H