📄 cgaltest.cpp
字号:
// CGALtest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <vector>
#include <algorithm>
#include <CGAL/Cartesian.h>
#include <CGAL/enum.h>
#include <CGAL/linear_least_squares_fitting_3.h>
#include <CGAL/centroid.h>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/Subdivision_method_3.h>
#include <CGAL/Polyhedron_incremental_builder_3.h>
#include <CGAL/io/print_wavefront.h>
using namespace std;
typedef double FT;
typedef CGAL::Cartesian<FT> K;
typedef K::Point_3 Point_3;
typedef K::Point_2 Point_2;
typedef K::Triangle_3 Triangle_3;
typedef K::Triangle_2 Triangle_2;
typedef K::Segment_2 Segment_2;
typedef K::Segment_3 Segment_3;
typedef K::Vector_2 Vector_2;
typedef K::Vector_3 Vector_3;
typedef K::Plane_3 Plane_3;
typedef K::Line_2 Line_2;
typedef K::Line_3 Line_3;
typedef K::Ray_3 Ray_3;
typedef K::Direction_3 Direction_3;
typedef K::Sphere_3 Sphere_3;
struct Facet_normal {
template <class Facet>
void operator()( Facet& f) {
typename Facet::Halfedge_handle h = f.halfedge();
typename Facet::Normal_3 normal = CGAL::cross_product(
h->next()->vertex()->point() - h->vertex()->point(),
h->next()->next()->vertex()->point() - h->next()->vertex()->point());
f.normal() = normal / std::sqrt( normal * normal);
}
};
struct Vertex_normal {
template <class Vertex>
void operator()( Vertex& v) {
typename Vertex::Normal_3 normal = CGAL::NULL_VECTOR;
typedef typename Vertex::Halfedge_around_vertex_const_circulator Circ;
Circ c = v.vertex_begin();
Circ d = c;
CGAL_For_all( c, d) {
if ( ! c->is_border())
normal = normal + c->facet()->normal();
}
v.normal() = normal / std::sqrt( normal * normal);
}
};
// A redefined items class for the Polyhedron_3 with a refined vertex
// class that contains a member for the normal vector and a refined
// facet with a normal vector instead of the plane equation (this is
// an alternative solution instead of using Polyhedron_traits_with_normals_3).
template <class Refs, class T, class P, class Norm>
class My_vertex : public CGAL::HalfedgeDS_vertex_base<Refs, T, P> {
Norm norm;
public:
My_vertex() {} // repeat mandatory constructors
My_vertex( const P& pt) : CGAL::HalfedgeDS_vertex_base<Refs, T, P>(pt) {}
typedef Norm Normal_3;
Normal_3& normal() { return norm; }
const Normal_3& normal() const { return norm; }
};
template <class Refs, class T, class Norm>
class My_facet : public CGAL::HalfedgeDS_face_base<Refs, T> {
Norm norm;
public:
// no constructors to repeat, since only default constructor mandatory
typedef Norm Normal_3;
Normal_3& normal() { return norm; }
const Normal_3& normal() const { return norm; }
};
struct My_items : public CGAL::Polyhedron_items_3 {
template <class Refs, class Traits>
struct Vertex_wrapper {
typedef typename Traits::Point_3 Point;
typedef typename Traits::Vector_3 Normal;
typedef My_vertex<Refs, CGAL::Tag_true, Point, Normal> Vertex;
};
template <class Refs, class Traits>
struct Face_wrapper {
typedef typename Traits::Vector_3 Normal;
typedef My_facet<Refs, CGAL::Tag_true, Normal> Face;
};
};
// Tie all types together and a small main function using it.
typedef CGAL::Polyhedron_3<K, My_items> Polyhedron;
typedef Polyhedron::Vertex_iterator Vertex_iterator;
typedef Polyhedron::Facet_iterator Facet_iterator;
typedef Polyhedron::Facet Facet;
typedef Facet::Halfedge_around_facet_circulator Halfedge_around_facet_circulator;
typedef Polyhedron::Halfedge_handle Halfedge_handle;
typedef Polyhedron::Vertex_handle Vertex_handle;
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
reverse(vec.begin( ),vec.end( ));
Point_3 point0(0.0,0.0,0.0);
Point_3 point1(1.0,0.0,0.0);
Vector_3 vector0=point1-point0;
double len0=vector0.squared_length();
Point_3 point2(2.0,0.0,0.0);
Vector_3 vector1=point2-point0;
double len1=vector1.squared_length();
vector1=vector1/sqrt(len1);
len1=vector1.squared_length();
Point_3 point3(3.0,0.0,0.0);
//Point_3 point0(-1.0,1.0,0.0);
//Point_3 point1(1.0,1.0,0.0);
//Point_3 EndPoint0=point0+5*(point0-point1);
//Point_3 EndPoint1=point1+5*(point1-point0);
//Point_3
//Point_2 point0(-1.0,0.0);
//Point_2 point1(1.0,0.0);
//Vector_2 vector0=point0-point1;
//Vector_2 vector1=point1-point0;
//Point_2 EndPoint0=point0+5*vector0;
//Point_2 EndPoint1=point1+5*vector1;
//Vector_2 vectorL=EndPoint1-EndPoint0;
//Vector_2 vectorR=EndPoint0-EndPoint1;
//Point_2 Bpoint0=EndPoint0+vectorL.perpendicular(CGAL::COUNTERCLOCKWISE)/2;
//Point_2 Bpoint1=EndPoint0+vectorL.perpendicular(CGAL::CLOCKWISE)/2;
//Point_2 Bpoint2=EndPoint1+vectorR.perpendicular(CGAL::COUNTERCLOCKWISE)/2;
//Point_2 Bpoint3=EndPoint1+vectorR.perpendicular(CGAL::CLOCKWISE)/2;
//Point_3 p( 1, 0, 0);
//Point_3 q( 0, 1, 0);
//Point_3 r( 0, 0, 1);
//Point_3 s( 0, 0, 0);
Point_3 p( 1, 0, 0);
Point_3 q( 0, 1, 0);
Point_3 r( -1, 0, 0);
Point_3 s( 0, 0, 1);
Polyhedron P;
P.make_tetrahedron( p, q, r, s);
//std::for_each( P.facets_begin(), P.facets_end(), Facet_normal());
//std::for_each( P.vertices_begin(), P.vertices_end(), Vertex_normal());
//CGAL::set_pretty_mode( std::cout);
//for ( Vertex_iterator i = P.vertices_begin(); i != P.vertices_end(); ++i)
//{
// // std::cout << i->normal() << std::endl;
// std::cout << i->point().x()<<" "<<i->point().y()<<" "<<i->point().z() << std::endl;
// std::cout << i->normal().x()<<" " <<i->normal().y()<<" "<<i->normal().z() << std::endl;
//}
//for ( Facet_iterator i = P.facets_begin(); i != P.facets_end(); ++i)
//{
// std::cout << i->halfedge()->vertex()->point().x()<<" "
// <<i->halfedge()->vertex()->point().y()<<" "
// <<i->halfedge()->vertex()->point().z()<< std::endl;
// std::cout << i->halfedge()->next()->vertex()->point().x()<<" "
// <<i->halfedge()->next()->vertex()->point().y()<<" "
// <<i->halfedge()->next()->vertex()->point().z()<< std::endl;
// std::cout << i->halfedge()->next()->next()->vertex()->point().x()<<" "
// <<i->halfedge()->next()->next()->vertex()->point().y()<<" "
// <<i->halfedge()->next()->next()->vertex()->point().z()<< std::endl;
// std::cout<<std::endl;
//}
Vertex_handle test;
Facet_iterator i = P.facets_begin();
Halfedge_handle h=i->halfedge();
Halfedge_handle g=P.create_center_vertex(h);
g->vertex()->point()=Point_3(1.0,1.0,1.0);
test=g->vertex();
g->vertex()->point()=Point_3(1.1,1.1,1.1);
bool a=(test==g->vertex());
i++;
Halfedge_handle h1=i->halfedge();
Halfedge_handle g1=P.create_center_vertex(h1);
g1->vertex()->point()=Point_3(1.2,1.2,1.2);
a=(test==g->vertex());
std::cout << test->point().x()<<" "
<<test->point().y()<<" "
<<test->point().z()<< std::endl;
//attention:g->vertex() is just a copy of h->vertex()
//new position must be specified for g->vertex() in order to
//build a valid polyhedron
//for ( Facet_iterator i = P.facets_begin(); i != P.facets_end(); ++i)
//{
// std::cout << i->halfedge()->vertex()->point().x()<<" "
// <<i->halfedge()->vertex()->point().y()<<" "
// <<i->halfedge()->vertex()->point().z()<< std::endl;
// std::cout << i->halfedge()->next()->vertex()->point().x()<<" "
// <<i->halfedge()->next()->vertex()->point().y()<<" "
// <<i->halfedge()->next()->vertex()->point().z()<< std::endl;
// std::cout << i->halfedge()->next()->next()->vertex()->point().x()<<" "
// <<i->halfedge()->next()->next()->vertex()->point().y()<<" "
// <<i->halfedge()->next()->next()->vertex()->point().z()<< std::endl;
// std::cout<<std::endl;
//}
g->vertex()->point()=Point_3(1.0,1.0,1.0);
for ( Facet_iterator i = P.facets_begin(); i != P.facets_end(); ++i)
{
std::cout << i->halfedge()->vertex()->point().x()<<" "
<<i->halfedge()->vertex()->point().y()<<" "
<<i->halfedge()->vertex()->point().z()<< std::endl;
std::cout << i->halfedge()->next()->vertex()->point().x()<<" "
<<i->halfedge()->next()->vertex()->point().y()<<" "
<<i->halfedge()->next()->vertex()->point().z()<< std::endl;
std::cout << i->halfedge()->next()->next()->vertex()->point().x()<<" "
<<i->halfedge()->next()->next()->vertex()->point().y()<<" "
<<i->halfedge()->next()->next()->vertex()->point().z()<< std::endl;
std::cout<<std::endl;
}
std::for_each( P.facets_begin(), P.facets_end(), Facet_normal());
std::for_each( P.vertices_begin(), P.vertices_end(), Vertex_normal());
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -