Example #1
0
GeometryTransitPtr CSGGeometry::toOsgGeometry(CGAL::Polyhedron *p) {
	GeoPnt3fPropertyRecPtr positions = GeoPnt3fProperty::create();
	GeoVec3fPropertyRecPtr normals = GeoVec3fProperty::create();
	GeoUInt32PropertyRecPtr indices = GeoUInt32Property::create();

	/*
	 * Iterate over all faces, add their vertices to 'positions' && write indices at
	 * the same time. Results in no shared vertices && therefore no normal interpolation between
	 * faces, but makes cubes look good. Well, well...
	 */

	Matrix localToWorld = getWorldMatrix();
	OSG::Vec3f translation;
	OSG::Quaternion rotation;
	OSG::Vec3f scaleFactor;
	OSG::Quaternion scaleOrientation;
	localToWorld.getTransform(translation, rotation, scaleFactor, scaleOrientation);
	Matrix worldToLocal;
	worldToLocal.invertFrom(localToWorld);

	// Convert indices && positions
	int curIndex = 0;
	for (CGAL::Polyhedron::Facet_const_iterator it = p->facets_begin(); it != p->facets_end(); it++) {
		CGAL::Polyhedron::Halfedge_around_facet_const_circulator circ = it->facet_begin();
		do {
			CGAL::Point cgalPos = circ->vertex()->point();
			// We need to transform each point from global coordinates into our local coordinate system
			// (CGAL uses global, OpenSG has geometry in node-local coords)
			OSG::Vec3f vecPos = OSG::Vec3f(CGAL::to_double(cgalPos.x()),
											  CGAL::to_double(cgalPos.y()),
											  CGAL::to_double(cgalPos.z()));
			OSG::Vec3f localVec = worldToLocal * (vecPos - translation);
			OSG::Pnt3f osgPos(localVec.x(), localVec.y(), localVec.z());

			positions->addValue(osgPos);
			normals->addValue(Vec3f(0,1,0));
			indices->addValue(curIndex);
			curIndex++;
		} while (++circ != it->facet_begin());
	}

	GeoUInt8PropertyRecPtr types = GeoUInt8Property::create();
	types->addValue(GL_TRIANGLES);
	GeoUInt32PropertyRecPtr lengths = GeoUInt32Property::create();
	lengths->addValue(indices->size());

	GeometryRecPtr mesh = Geometry::create();
	mesh->setPositions(positions);
	mesh->setNormals(normals);
	mesh->setIndices(indices);
	mesh->setTypes(types);
	mesh->setLengths(lengths);
	mesh->setMaterial(VRMaterial::getDefault()->getMaterial());
    createSharedIndex(mesh);
	calcVertexNormals(mesh, 0.523598775598 /*30 deg in rad*/);

	return GeometryTransitPtr(mesh);
}
Example #2
0
bool DelaunayTriangulator::getAll(int i_index, std::vector<int>& neighborhood, std::vector<stk::Vector2d>& polygon)
{
	CGAL::Traits::Segment_2 s;

	CGAL::DelaunayTri* tri = (CGAL::DelaunayTri*) m_tri;
	std::vector<CGAL::VertexHandle>* vertices = (std::vector<CGAL::VertexHandle>*) m_vertices;
	
	if(tri->is_edge(vertices->at(i_index), tri->infinite_vertex())) return false;
	
	//List all incident vertices in delaunay triangulation
	{
		CGAL::VertexCirculator ec, start;
		ec = start = tri->incident_vertices(vertices->at(i_index));
		do
		{
			neighborhood.push_back(ec->info());
		}
		while ( ++ec != start );
	}
	
	//List all incident faces in delaunay triangulation
	{
		CGAL::FaceCirculator fc, start;
		fc = start = tri->incident_faces(vertices->at(i_index));
		if(fc != 0)
		{
			do
			{
				if(!tri->is_infinite(fc))
				{
					//The dual is a point in the voronoi polygon
					CGAL::Point currVert = tri->dual(fc);
					polygon.push_back(Vector2d(currVert.x(), currVert.y()));
				}
			}
			while (++fc != start);
		}
	}

	return true;
}