added colors to the triangles

This commit is contained in:
Baruch Zukerman 2006-10-03 12:35:17 +00:00
parent 5fe648d3d7
commit 2ce18f9194
2 changed files with 83 additions and 6 deletions

View File

@ -32,9 +32,8 @@ int main(int, char*){
#include <fstream>
#include <string>
#include "typedefs.h"
#include <CGAL/IO/Qt_widget.h>
#include <CGAL/Random.h>
@ -63,6 +62,7 @@ int main(int, char*){
#include <qpixmap.h>
#include <qpainter.h>
#include <qpushbutton.h>
#include "typedefs.h"
//The envelope diagram
@ -82,7 +82,19 @@ public:
// this method overrides the virtual method 'draw()' of Qt_widget_layer
void draw()
{
widget->lock(); // widget have to be locked before drawing
widget->lock(); // widget have to be locked before drawing
for(Face_const_iterator fit = diag.faces_begin();
fit != diag.faces_end();
++fit)
{
if(! fit->number_of_surfaces())
continue;
draw_face(widget, fit);
}
*widget << CGAL::BLUE;
for(Edge_const_iterator eit = diag.edges_begin();
eit != diag.edges_end();
@ -197,7 +209,7 @@ public slots:
if(s==QString::null)
return;
curr_dir = s;
std::ifstream in_file(s);
if(!in_file.is_open())
{
@ -214,11 +226,16 @@ public slots:
std::list<Surface_3> triangles;
int num_of_triangles;
in_file >> num_of_triangles;
CGAL::Random rand;
for(int i=0 ; i<num_of_triangles; i++)
{
int r = rand.get_int(0, 256);
int g = rand.get_int(0, 256);
int b = rand.get_int(0, 256);
Triangle_3 tri;
in_file >> tri;
triangles.push_back(tri);
triangles.push_back(Surface_3(tri, CGAL::Color(r, g, b)));
box = box + bbox_2d(tri);
}
diag.clear();

View File

@ -23,7 +23,12 @@
#include <CGAL/Lazy_exact_nt.h>
#include <CGAL/envelope_3.h>
#include <CGAL/Env_triangle_traits_3.h>
#include <CGAL/Env_surface_data_traits_3.h>
#include <CGAL/Bbox_2.h>
#include <CGAL/IO/Color.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/IO/Qt_widget_Polygon_2.h>
#include <qpainter.h>
#ifdef CGAL_USE_GMP
@ -47,14 +52,21 @@ typedef Kernel::Point_2 Point_2;
typedef Kernel::Point_3 Point_3;
typedef Kernel::Triangle_2 Triangle_2;
typedef Kernel::Triangle_3 Triangle_3;
typedef CGAL::Polygon_2<Kernel> Polygon_2;
typedef CGAL::Env_triangle_traits_3<Kernel> Traits;
typedef CGAL::Env_triangle_traits_3<Kernel> Base_traits;
typedef CGAL::Env_surface_data_traits_3<Base_traits,
CGAL::Color> Traits;
typedef Traits::Surface_3 Surface_3;
typedef Traits::Xy_monotone_surface_3 Xy_monotone_surface_3;
typedef CGAL::Envelope_diagram_2<Traits> Envelope_diagram_2;
typedef Envelope_diagram_2::Edge_const_iterator Edge_const_iterator;
typedef Envelope_diagram_2::Vertex_const_iterator Vertex_const_iterator;
typedef Envelope_diagram_2::Face_const_iterator Face_const_iterator;
typedef Envelope_diagram_2::Hole_const_iterator Hole_const_iterator;
typedef Envelope_diagram_2::Ccb_halfedge_const_circulator
Ccb_halfedge_const_circulator;
CGAL::Bbox_2 bbox_2d(const Triangle_3& tri)
{
@ -66,4 +78,52 @@ CGAL::Bbox_2 bbox_2d(const Triangle_3& tri)
Point_2(p2.x(), p2.y()));
return proj_tri.bbox();
}
void construct_polygon(Ccb_halfedge_const_circulator ccb, Polygon_2& pgn)
{
/* running with around the outer of the face and generate from it
* polygon
*/
Ccb_halfedge_const_circulator cc=ccb;
do
{
pgn.push_back(cc->source()->point());
//created from the outer boundary of the face
}
while (++cc !=ccb);
}
void draw_face(CGAL::Qt_widget* w, Face_const_iterator f)
{
if(f->is_unbounded())
return;
Qt::RasterOp old_rasterop = w->rasterOp();
w->get_painter().setRasterOp(Qt::XorROP);
// make polygon from the outer ccb of the face 'f'
Polygon_2 pgn;
construct_polygon(f->outer_ccb(), pgn);
w->setFilled(true);
CGAL::Color c = f->surface().data();
w->setFillColor(c);
(*w) << pgn ; // draw the polyong
for(Hole_const_iterator hit = f->holes_begin();
hit != f->holes_end();
++hit)
{
Polygon_2 hole;
construct_polygon(*hit, hole);
(*w) << hole ; // draw the polyong
}
w->get_painter().setRasterOp(old_rasterop);
w->setFilled(false);
}
#endif