1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
|
#include <CGAL/Three/Viewer_interface.h>
#include <CGAL/Three/Triangle_container.h>
#include <CGAL/Three/Edge_container.h>
#include <CGAL/Three/Point_container.h>
#include <CGAL/Three/Three.h>
#include <CGAL/Buffer_for_vao.h>
#include "Color_map.h"
#include "Scene_lcc_item.h"
//todo : create a struct for facets containing useful infos for drawing and their volume, and fill it during computeElements().
using namespace CGAL::Three;
typedef Triangle_container Tri;
typedef Edge_container Ec;
typedef Point_container Pc;
typedef Viewer_interface Vi;
typedef Scene_lcc_item::LCC::Dart_const_descriptor Dart_const_descriptor;
typedef Scene_lcc_item::LCC::Dart_descriptor Dart_descriptor;
typedef Scene_lcc_item::LCC::Point Point;
struct Facet{
Facet():normal(Scene_lcc_item::LCC::Vector(0,0,0)){}
Dart_const_descriptor f_handle;
std::vector<Point> points;
Scene_lcc_item::LCC::Vector normal;
std::size_t volume_id;
std::size_t size() { return points.size(); }
};
struct lcc_priv{
Scene_lcc_item::LCC lcc;
std::vector<float> faces;
std::vector<float> lines;
std::vector<float> vertices;
std::vector<float> colors;
std::vector<Facet> facets;
std::size_t nb_volumes;
bool is_mono_color;
std::size_t nb_lines, nb_vertices, nb_faces;
lcc_priv(const Scene_lcc_item::LCC& lcc)
:lcc(lcc), is_mono_color(true){}
bool compute_face(Dart_const_descriptor dh, Facet& f)
{
f.f_handle = dh;
const CGAL::qglviewer::Vec offset = static_cast<CGAL::Three::Viewer_interface*>(CGAL::QGLViewer::QGLViewerPool().first())->offset();
// We fill only closed faces.
Dart_const_descriptor cur=dh;
Dart_const_descriptor min=dh;
do
{
if (!lcc.is_next_exist(cur)) return false; // open face=>not filled
if (cur<min) min=cur;
cur=lcc.next(cur);
}
while(cur!=dh);
cur=dh;
do
{
f.points.push_back(lcc.point(cur));
cur=lcc.next(cur);
}
while(cur!=dh);
for (std::size_t i = 0; i < f.size() ; ++ i){
const Point& pa = f.points[i];
const Point& pb = f.points[(i+1)%f.size()];
double x = f.normal.x() + (pa.y()-pb.y())*(pa.z()+pb.z());
double y = f.normal.y() + (pa.z()-pb.z())*(pa.x()+pb.x());
double z = f.normal.z() + (pa.x()-pb.x())*(pa.y()+pb.y());
f.normal = Scene_lcc_item::LCC::Vector(x,y,z);
}
if (f.size()==3)
{
for(auto pt = f.points.begin();
pt != f.points.end();
++pt)
{
faces.push_back(pt->x() + offset.x);
faces.push_back(pt->y() + offset.y);
faces.push_back(pt->z() + offset.z);
}
}
else if (CGAL::Buffer_for_vao::is_facet_convex(f.points,f.normal))
{
if (f.size()==4)
{
Point p = f.points[0];
faces.push_back(p.x() + offset.x);
faces.push_back(p.y() + offset.y);
faces.push_back(p.z() + offset.z);
p = f.points[1];
faces.push_back(p.x() + offset.x);
faces.push_back(p.y() + offset.y);
faces.push_back(p.z() + offset.z);
p = f.points[2];
faces.push_back(p.x() + offset.x);
faces.push_back(p.y() + offset.y);
faces.push_back(p.z() + offset.z);
p = f.points[0];
faces.push_back(p.x() + offset.x);
faces.push_back(p.y() + offset.y);
faces.push_back(p.z() + offset.z);
p = f.points[2];
faces.push_back(p.x() + offset.x);
faces.push_back(p.y() + offset.y);
faces.push_back(p.z() + offset.z);
p = f.points[3];
faces.push_back(p.x() + offset.x);
faces.push_back(p.y() + offset.y);
faces.push_back(p.z() + offset.z);
}
else
{
for(std::size_t i=1; i<f.size()-1; ++i)
{
Point& p0 = f.points[0];
Point& p1 = f.points[i];
Point& p2 = f.points[i+1];
// (1) add points
faces.push_back(p0.x() + offset.x);
faces.push_back(p0.y() + offset.y);
faces.push_back(p0.z() + offset.z);
faces.push_back(p1.x() + offset.x);
faces.push_back(p1.y() + offset.y);
faces.push_back(p1.z() + offset.z);
faces.push_back(p2.x() + offset.x);
faces.push_back(p2.y() + offset.y);
faces.push_back(p2.z() + offset.z);
}
} // Convex face with > 4 vertices
}
else
{
struct Vertex_info
{
Scene_lcc_item::LCC::Vector v;
std::size_t index;
};
struct Face_info
{
bool exist_edge[3];
bool is_external;
bool is_process;
};
typedef CGAL::Projection_traits_3<CGAL::Exact_predicates_inexact_constructions_kernel> P_traits;
typedef CGAL::Triangulation_vertex_base_with_info_2<Vertex_info, P_traits> Vb;
typedef CGAL::Triangulation_face_base_with_info_2<Face_info, P_traits> Fb1;
typedef CGAL::Constrained_triangulation_face_base_2<P_traits, Fb1> Fb;
typedef CGAL::Triangulation_data_structure_2<Vb,Fb> TDS;
typedef CGAL::Exact_predicates_tag Itag;
typedef CGAL::Constrained_Delaunay_triangulation_2<P_traits, TDS, Itag> CDT;
P_traits cdt_traits(f.normal);
CDT cdt(cdt_traits);
// (1) We insert all the edges as constraint in the CDT.
typename CDT::Vertex_handle previous=nullptr, first=nullptr;
for (unsigned int i=0; i<f.size(); ++i)
{
typename CDT::Vertex_handle vh = cdt.insert(f.points[i]);
if(first==nullptr)
{ first=vh; }
vh->info().v=f.normal;
if(previous!=nullptr && previous!=vh)
{ cdt.insert_constraint(previous, vh); }
previous=vh;
}
if (previous!=nullptr && previous!=first)
{ cdt.insert_constraint(previous, first); }
// (2) We mark all external triangles
// (2.1) We initialize is_external and is_process values
for(typename CDT::All_faces_iterator fit = cdt.all_faces_begin(),
fitend = cdt.all_faces_end(); fit!=fitend; ++fit)
{
fit->info().is_external = true;
fit->info().is_process = false;
}
// (2.2) We check if the facet is external or internal
std::queue<typename CDT::Face_handle> face_queue;
typename CDT::Face_handle face_internal = nullptr;
if (cdt.infinite_vertex()->face()!=nullptr)
{ face_queue.push(cdt.infinite_vertex()->face()); }
while(!face_queue.empty())
{
typename CDT::Face_handle fh = face_queue.front();
face_queue.pop();
if(!fh->info().is_process)
{
fh->info().is_process = true;
for(int i=0; i<3; ++i)
{
if(!cdt.is_constrained(std::make_pair(fh, i)))
{
if (fh->neighbor(i)!=nullptr)
{ face_queue.push(fh->neighbor(i)); }
}
else if (face_internal==nullptr)
{
face_internal = fh->neighbor(i);
}
}
}
}
if ( face_internal!=nullptr )
{ face_queue.push(face_internal); }
while(!face_queue.empty())
{
typename CDT::Face_handle fh = face_queue.front();
face_queue.pop();
if(!fh->info().is_process)
{
fh->info().is_process = true;
fh->info().is_external = false;
for(unsigned int i=0; i<3; ++i)
{
if(!cdt.is_constrained(std::make_pair(fh, i)))
{
if (fh->neighbor(i)!=nullptr)
{ face_queue.push(fh->neighbor(i)); }
}
}
}
}
// (3) Now we iterates on the internal faces to add the vertices
// and the normals to the appropriate vectors
for(typename CDT::Finite_faces_iterator ffit=cdt.finite_faces_begin(),
ffitend = cdt.finite_faces_end(); ffit!=ffitend; ++ffit)
{
if(!ffit->info().is_external)
{
for(unsigned int i=0; i<3; ++i)
{
Point p = ffit->vertex(i)->point();
faces.push_back(p.x() + offset.x);
faces.push_back(p.y() + offset.y);
faces.push_back(p.z() + offset.z);
}
}
}
}
return true;
}
};
Scene_lcc_item::Scene_lcc_item(const LCC& lcc)
:d(new lcc_priv(lcc))
{
d->nb_faces = 0;
d->nb_lines = 0;
d->nb_vertices = 0;
d->nb_volumes = 0;
setTriangleContainer(0,
new Tri(Three::mainViewer()->isOpenGL_4_3() ? Vi::PROGRAM_FLAT
: Vi::PROGRAM_OLD_FLAT, false));
setEdgeContainer(0,
new Ec(Three::mainViewer()->isOpenGL_4_3() ? Vi::PROGRAM_SOLID_WIREFRAME
: Vi::PROGRAM_NO_SELECTION
, false));
setPointContainer(0,
new Pc(Vi::PROGRAM_NO_SELECTION, false));
}
Scene_lcc_item::~Scene_lcc_item()
{
delete d;
}
Scene_lcc_item* Scene_lcc_item::clone() const
{
Scene_lcc_item* item = new Scene_lcc_item(d->lcc);
return item;
}
bool Scene_lcc_item::supportsRenderingMode(RenderingMode m) const
{
return m==FlatPlusEdges;
}
QString Scene_lcc_item::toolTip() const
{
return QString();
}
void Scene_lcc_item::compute_bbox() const
{
Scene_item::Bbox bb;
for (LCC::Dart_range::const_iterator it=d->lcc.darts().begin(),
itend=d->lcc.darts().end(); it!=itend; ++it )
{
bb+=d->lcc.point(it).bbox();
}
this->setBbox(bb);
}
void Scene_lcc_item::draw(CGAL::Three::Viewer_interface* viewer) const
{
if(!isInit(viewer))
initGL(viewer);
if ( getBuffersFilled() &&
! getBuffersInit(viewer))
{
initializeBuffers(viewer);
setBuffersInit(viewer, true);
}
if(!getBuffersFilled())
{
computeElements();
initializeBuffers(viewer);
}
if(d->is_mono_color)
getTriangleContainer(0)->setColor(this->color());
getTriangleContainer(0)->draw(viewer, d->is_mono_color);
}
void Scene_lcc_item::drawEdges(CGAL::Three::Viewer_interface* viewer) const
{
if(!isInit(viewer))
initGL(viewer);
if ( getBuffersFilled() &&
! getBuffersInit(viewer))
{
initializeBuffers(viewer);
setBuffersInit(viewer, true);
}
if(!getBuffersFilled())
{
computeElements();
initializeBuffers(viewer);
}
GLfloat offset_factor;
GLfloat offset_units;
viewer->glGetFloatv(GL_POLYGON_OFFSET_FACTOR, &offset_factor);
viewer->glGetFloatv(GL_POLYGON_OFFSET_UNITS, &offset_units);
viewer->glEnable(GL_POLYGON_OFFSET_LINE);
viewer->glPolygonOffset(0.3f, 0.3f);
if(viewer->isOpenGL_4_3())
{
QVector2D vp(viewer->width(), viewer->height());
getEdgeContainer(0)->setViewport(vp);
getEdgeContainer(0)->setWidth(2);
}
getEdgeContainer(0)->setColor(QColor(Qt::black));
getEdgeContainer(0)->draw(viewer, true);
drawPoints(viewer);
viewer->glDisable(GL_POLYGON_OFFSET_LINE);
viewer->glPolygonOffset(offset_factor, offset_units);
}
void Scene_lcc_item::drawPoints(CGAL::Three::Viewer_interface* viewer) const
{
if(!visible())
return;
if(!isInit(viewer))
initGL(viewer);
if ( getBuffersFilled() &&
! getBuffersInit(viewer))
{
initializeBuffers(viewer);
setBuffersInit(viewer, true);
}
GLfloat point_size;
viewer->glGetFloatv(GL_POINT_SIZE, &point_size);
viewer->setGlPointSize(GLfloat(5));
if(!getBuffersFilled())
{
computeElements();
initializeBuffers(viewer);
}
getPointContainer(0)->setColor(QColor(Qt::black));
getPointContainer(0)->draw(viewer, true);
viewer->setGlPointSize(point_size);
}
void Scene_lcc_item::computeElements() const
{
CGAL::Three::Three::CursorScopeGuard guard{QCursor(Qt::WaitCursor)};
d->facets.clear();
const CGAL::qglviewer::Vec offset = CGAL::Three::Three::mainViewer()->offset();
typename LCC::size_type markvolumes = d->lcc.get_new_mark();
typename LCC::size_type markfaces = d->lcc.get_new_mark();
typename LCC::size_type markedges = d->lcc.get_new_mark();
typename LCC::size_type markvertices = d->lcc.get_new_mark();
std::size_t volume_id = 0;
for (typename LCC::Dart_range::const_iterator it=d->lcc.darts().begin(),
itend=d->lcc.darts().end(); it!=itend; ++it )
{
if (!d->lcc.is_marked(it, markvolumes))
{
for (typename LCC::template Dart_of_cell_basic_range<3>::
const_iterator itv=d->lcc.template darts_of_cell_basic<3>(it, markvolumes).begin(),
itvend=d->lcc.template darts_of_cell_basic<3>(it, markvolumes).end();
itv!=itvend; ++itv)
{
d->lcc.mark(itv, markvolumes); // To be sure that all darts of the basic iterator will be marked
if (!d->lcc.is_marked(itv, markfaces))
{
Facet f;
if(d->compute_face(itv, f))
d->facets.push_back(f);
d->facets.back().volume_id = volume_id;
for (typename LCC::template Dart_of_cell_basic_range<2>::
const_iterator itf=d->lcc.template darts_of_cell_basic<2>(itv, markfaces).begin(),
itfend=d->lcc.template darts_of_cell_basic<2>(itv, markfaces).end();
itf!=itfend; ++itf)
{
d->lcc.mark(itf, markfaces); // To be sure that all darts of the basic iterator will be marked
if ( !d->lcc.is_marked(itf, markedges))
{
Point p1 = d->lcc.point(itf);
LCC::Dart_const_descriptor d2 = d->lcc.other_extremity(itf);
Point p2 = d->lcc.point(d2);
d->lines.push_back(p1.x() + offset.x);
d->lines.push_back(p1.y() + offset.y);
d->lines.push_back(p1.z() + offset.z);
d->lines.push_back(p2.x() + offset.x);
d->lines.push_back(p2.y() + offset.y);
d->lines.push_back(p2.z() + offset.z);
for (typename LCC::template Dart_of_cell_basic_range<1>::
const_iterator ite=d->lcc.template darts_of_cell_basic<1>(itf, markedges).begin(),
iteend=d->lcc.template darts_of_cell_basic<1>(itf, markedges).end();
ite!=iteend; ++ite)
{
d->lcc.mark(ite, markedges); // To be sure that all darts of the basic iterator will be marked
if ( !d->lcc.is_marked(ite, markvertices))
{
Point p1 = d->lcc.point(ite);
d->vertices.push_back(p1.x() + offset.x);
d->vertices.push_back(p1.y() + offset.y);
d->vertices.push_back(p1.z() + offset.z);
CGAL::mark_cell<LCC, 0>(d->lcc, ite, markvertices);
}
}
}
}
}
}
++volume_id;
}
}
d->nb_volumes = volume_id;
for (typename LCC::Dart_range::const_iterator it=d->lcc.darts().begin(),
itend=d->lcc.darts().end(); it!=itend; ++it )
{
d->lcc.unmark(it, markvertices);
d->lcc.unmark(it, markedges);
d->lcc.unmark(it, markfaces);
d->lcc.unmark(it, markvolumes);
}
d->lcc.free_mark(markvolumes);
d->lcc.free_mark(markfaces);
d->lcc.free_mark(markedges);
d->lcc.free_mark(markvertices);
getTriangleContainer(0)->allocate(
Tri::Flat_vertices, d->faces.data(),
static_cast<int>(d->faces.size()*sizeof(float)));
if(!d->is_mono_color)
{
getTriangleContainer(0)->allocate(Tri::FColors, d->colors.data(),
static_cast<int>(d->colors.size()*sizeof(float)));
}
else
getTriangleContainer(0)->allocate(Tri::FColors, nullptr, 0);
getEdgeContainer(0)->allocate(
Ec::Vertices, d->lines.data(),
static_cast<int>(d->lines.size()*sizeof(float)));
getPointContainer(0)->allocate(
Pc::Vertices, d->vertices.data(),
static_cast<int>(d->vertices.size()*sizeof(float)));
setBuffersFilled(true);
d->nb_faces = d->faces.size();
d->nb_lines = d->lines.size();
d->nb_vertices= d->vertices.size();
}
void Scene_lcc_item::initializeBuffers(CGAL::Three::Viewer_interface *viewer) const
{
getTriangleContainer(0)->initializeBuffers(viewer);
getTriangleContainer(0)->setFlatDataSize(d->nb_faces);
getEdgeContainer(0)->initializeBuffers(viewer);
getEdgeContainer(0)->setFlatDataSize(d->nb_lines);
getPointContainer(0)->initializeBuffers(viewer);
getPointContainer(0)->setFlatDataSize(d->nb_vertices);
d->faces.clear();
d->faces.shrink_to_fit();
d->lines.clear();
d->lines.shrink_to_fit();
d->vertices.clear();
d->vertices.shrink_to_fit();
}
void Scene_lcc_item::invalidateOpenGLBuffers()
{
setBuffersFilled(false);
getTriangleContainer(0)->reset_vbos(ALL);
getEdgeContainer(0)->reset_vbos(ALL);
getPointContainer(0)->reset_vbos(ALL);
compute_bbox();
}
bool Scene_lcc_item::isEmpty() const
{
return false;
}
void Scene_lcc_item::randomFaceColors()
{
d->is_mono_color = false;
d->colors.resize(d->nb_faces);
for(std::size_t i=0; i< d->colors.size()-3; i+=3)
{
QColor col = generate_random_color();
d->colors[i] = col.redF();
d->colors[i+1] = col.greenF();
d->colors[i+2] = col.blueF();
}
invalidateOpenGLBuffers();
redraw();
}
void Scene_lcc_item::randomVolumeColors()
{
d->is_mono_color = false;
d->colors.resize(d->nb_faces);
std::vector<QColor> colors(d->nb_volumes);
for(std::size_t i = 0; i<d->nb_volumes; ++i)
{
colors[i] = generate_random_color();
}
std::size_t color_id = 0;
for(auto f : d->facets)//filled in the same order as GL faces
{
QColor col = colors[f.volume_id];
//3 points per face.
for(std::size_t j = 0; j < 3; ++j)
{
d->colors[color_id+j*3] = col.redF();
d->colors[color_id+j*3+1] = col.greenF();
d->colors[color_id+j*3+2] = col.blueF();
}
color_id += 9;
}
invalidateOpenGLBuffers();
redraw();
}
void Scene_lcc_item::resetColors()
{
d->is_mono_color = true;
invalidateOpenGLBuffers();
redraw();
}
QMenu* Scene_lcc_item::contextMenu()
{
const char* prop_name = "Menu modified by Scene_lcc_item.";
QMenu* menu = Scene_item::contextMenu();
// Use dynamic properties:
// https://doc.qt.io/qt-5/qobject.html#property
bool menuChanged = menu->property(prop_name).toBool();
if(!menuChanged) {
menu->addSeparator();
QAction* action = menu->addAction(tr("Set Random Colors for Faces."));
action->setObjectName("actionRandomFaceColors");
connect(action, &QAction::triggered,
this, &Scene_lcc_item::randomFaceColors);
action = menu->addAction(tr("Set Random Colors for Volumes."));
action->setObjectName("actionRandomVolumeColors");
connect(action, &QAction::triggered,
this, &Scene_lcc_item::randomVolumeColors);
menu->setProperty(prop_name, true);
}
QAction* action = menu->findChild<QAction*>("actionResetColors");
if(!action)
{
action = menu->addAction(tr("Reset Colors."));
action->setObjectName("actionResetColors");
connect(action, &QAction::triggered,
this, &Scene_lcc_item::resetColors);
}
action->setVisible(!d->is_mono_color);
return menu;
}
|