Clean file IO code in Stream_support (no real changes)

This commit is contained in:
Mael Rouxel-Labbé 2020-01-20 10:42:46 +01:00
parent e317c689aa
commit 490a726ec0
22 changed files with 1409 additions and 1226 deletions

View File

@ -11,30 +11,35 @@
#ifndef CGAL_IO_READ_3MF_H #ifndef CGAL_IO_READ_3MF_H
#define CGAL_IO_READ_3MF_H #define CGAL_IO_READ_3MF_H
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <CGAL/IO/Color.h> #include <CGAL/IO/Color.h>
#include <CGAL/Kernel_traits.h> #include <CGAL/Kernel_traits.h>
#include <Model/COM/NMR_DLLInterfaces.h>
namespace CGAL{
namespace transform_nmr_internal{ #include <Model/COM/NMR_DLLInterfaces.h>
#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
namespace CGAL {
namespace transform_nmr_internal {
NMR::MODELTRANSFORM initMatrix() NMR::MODELTRANSFORM initMatrix()
{ {
NMR::MODELTRANSFORM mMatrix; NMR::MODELTRANSFORM mMatrix;
int i, j; int i, j;
for (i = 0; i < 4; i++) { for(i = 0; i < 4; i++) {
for (j = 0; j < 3; j++) { for(j = 0; j < 3; j++) {
mMatrix.m_fFields[j][i] = (i == j) ? 1.0f : 0.0f; mMatrix.m_fFields[j][i] = (i == j) ? 1.0f : 0.0f;
} }
} }
return mMatrix; return mMatrix;
} }
}//end transform_nmr_internal
} // namespace transform_nmr_internal
template<typename PointRange, template<typename PointRange,
typename PolygonRange, typename PolygonRange,
@ -44,24 +49,27 @@ bool extract_soups (NMR::PLib3MFModelMeshObject *pMeshObject,
PointRange& points, PointRange& points,
PolygonRange& triangles, PolygonRange& triangles,
ColorRange& colors, ColorRange& colors,
std::string& name) { std::string& name)
{
typedef typename PointRange::value_type Point_3; typedef typename PointRange::value_type Point_3;
typedef typename PolygonRange::value_type Polygon; typedef typename PolygonRange::value_type Polygon;
typedef typename Kernel_traits<Point_3>::Kernel Kernel; typedef typename Kernel_traits<Point_3>::Kernel Kernel;
HRESULT hResult; HRESULT hResult;
DWORD nNeededChars; DWORD nNeededChars;
std::vector<char> pBuffer; std::vector<char> pBuffer;
// Retrieve Mesh Name Length // Retrieve Mesh Name Length
hResult = NMR::lib3mf_object_getnameutf8(pMeshObject, NULL, 0, &nNeededChars); hResult = NMR::lib3mf_object_getnameutf8(pMeshObject, NULL, 0, &nNeededChars);
if (hResult != LIB3MF_OK) if(hResult != LIB3MF_OK)
{ {
std::cerr<<"Error during name extraction."; std::cerr<<"Error during name extraction.";
return false; return false;
} }
// Retrieve Mesh Name // Retrieve Mesh Name
if (nNeededChars > 0) { if(nNeededChars > 0)
{
pBuffer.resize(nNeededChars + 1); pBuffer.resize(nNeededChars + 1);
hResult = NMR::lib3mf_object_getnameutf8(pMeshObject, &pBuffer[0], nNeededChars + 1, NULL); hResult = NMR::lib3mf_object_getnameutf8(pMeshObject, &pBuffer[0], nNeededChars + 1, NULL);
pBuffer[nNeededChars] = 0; pBuffer[nNeededChars] = 0;
@ -74,17 +82,20 @@ bool extract_soups (NMR::PLib3MFModelMeshObject *pMeshObject,
name = std::string(&pBuffer[0]); name = std::string(&pBuffer[0]);
} }
else else
{
name = std::string("Unknown Mesh"); name = std::string("Unknown Mesh");
}
typename Kernel::Aff_transformation_3 t( typename Kernel::Aff_transformation_3 t(
transform.m_fFields[0][0], transform.m_fFields[0][1], transform.m_fFields[0][2], transform.m_fFields[0][3], transform.m_fFields[0][0], transform.m_fFields[0][1], transform.m_fFields[0][2], transform.m_fFields[0][3],
transform.m_fFields[1][0], transform.m_fFields[1][1], transform.m_fFields[1][2], transform.m_fFields[1][3], transform.m_fFields[1][0], transform.m_fFields[1][1], transform.m_fFields[1][2], transform.m_fFields[1][3],
transform.m_fFields[2][0], transform.m_fFields[2][1], transform.m_fFields[2][2], transform.m_fFields[2][3] transform.m_fFields[2][0], transform.m_fFields[2][1], transform.m_fFields[2][2], transform.m_fFields[2][3]
); );
NMR::PLib3MFPropertyHandler * pPropertyHandler; NMR::PLib3MFPropertyHandler * pPropertyHandler;
hResult = NMR::lib3mf_meshobject_createpropertyhandler(pMeshObject, &pPropertyHandler); hResult = NMR::lib3mf_meshobject_createpropertyhandler(pMeshObject, &pPropertyHandler);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
DWORD nErrorMessage; DWORD nErrorMessage;
LPCSTR pszErrorMessage; LPCSTR pszErrorMessage;
std::cerr << "could not create property handler: " << std::hex << hResult << std::endl; std::cerr << "could not create property handler: " << std::hex << hResult << std::endl;
@ -103,6 +114,7 @@ bool extract_soups (NMR::PLib3MFModelMeshObject *pMeshObject,
pVertex.m_fPosition[2]); pVertex.m_fPosition[2]);
points[vid] = t.transform(p); points[vid] = t.transform(p);
} }
for(DWORD pid = 0; pid < triangles.size(); ++pid) for(DWORD pid = 0; pid < triangles.size(); ++pid)
{ {
NMR::MODELMESHTRIANGLE pTriangle; NMR::MODELMESHTRIANGLE pTriangle;
@ -117,6 +129,7 @@ bool extract_soups (NMR::PLib3MFModelMeshObject *pMeshObject,
colors[pid]=CGAL::Color(mColor.m_Red, mColor.m_Green, colors[pid]=CGAL::Color(mColor.m_Red, mColor.m_Green,
mColor.m_Blue, mColor.m_Alpha); mColor.m_Blue, mColor.m_Alpha);
} }
return true; return true;
} }
@ -128,7 +141,8 @@ bool extract_polylines (NMR::PLib3MFModelMeshObject *pMeshObject,
PointRange& points, PointRange& points,
PolygonRange&, PolygonRange&,
ColorRange& colors, ColorRange& colors,
std::string& name) { std::string& name)
{
typedef typename PointRange::value_type Point_3; typedef typename PointRange::value_type Point_3;
HRESULT hResult; HRESULT hResult;
@ -136,7 +150,7 @@ bool extract_polylines (NMR::PLib3MFModelMeshObject *pMeshObject,
std::vector<char> pBuffer; std::vector<char> pBuffer;
// Retrieve Mesh Name Length // Retrieve Mesh Name Length
hResult = NMR::lib3mf_object_getnameutf8(pMeshObject, NULL, 0, &nNeededChars); hResult = NMR::lib3mf_object_getnameutf8(pMeshObject, NULL, 0, &nNeededChars);
if (hResult != LIB3MF_OK) if(hResult != LIB3MF_OK)
{ {
points.resize(0); points.resize(0);
std::cerr<<"Error during name extraction."; std::cerr<<"Error during name extraction.";
@ -144,7 +158,8 @@ bool extract_polylines (NMR::PLib3MFModelMeshObject *pMeshObject,
} }
// Retrieve Mesh Name // Retrieve Mesh Name
if (nNeededChars > 0) { if(nNeededChars > 0)
{
pBuffer.resize(nNeededChars + 1); pBuffer.resize(nNeededChars + 1);
hResult = NMR::lib3mf_object_getnameutf8(pMeshObject, &pBuffer[0], nNeededChars + 1, NULL); hResult = NMR::lib3mf_object_getnameutf8(pMeshObject, &pBuffer[0], nNeededChars + 1, NULL);
pBuffer[nNeededChars] = 0; pBuffer[nNeededChars] = 0;
@ -164,7 +179,8 @@ bool extract_polylines (NMR::PLib3MFModelMeshObject *pMeshObject,
NMR::PLib3MFPropertyHandler * pPropertyHandler; NMR::PLib3MFPropertyHandler * pPropertyHandler;
hResult = NMR::lib3mf_meshobject_createpropertyhandler(pMeshObject, &pPropertyHandler); hResult = NMR::lib3mf_meshobject_createpropertyhandler(pMeshObject, &pPropertyHandler);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
DWORD nErrorMessage; DWORD nErrorMessage;
LPCSTR pszErrorMessage; LPCSTR pszErrorMessage;
std::cerr << "could not create property handler: " << std::hex << hResult << std::endl; std::cerr << "could not create property handler: " << std::hex << hResult << std::endl;
@ -188,8 +204,8 @@ bool extract_polylines (NMR::PLib3MFModelMeshObject *pMeshObject,
NMR::MODELMESH_TRIANGLECOLOR_SRGB pColor; NMR::MODELMESH_TRIANGLECOLOR_SRGB pColor;
NMR::lib3mf_propertyhandler_getcolor(pPropertyHandler, 0, &pColor);//get color of the dummy triangle NMR::lib3mf_propertyhandler_getcolor(pPropertyHandler, 0, &pColor);//get color of the dummy triangle
NMR::MODELMESHCOLOR_SRGB mColor = pColor.m_Colors[0]; NMR::MODELMESHCOLOR_SRGB mColor = pColor.m_Colors[0];
colors[0]=CGAL::Color(mColor.m_Red, mColor.m_Green, colors[0] = CGAL::Color(mColor.m_Red, mColor.m_Green,
mColor.m_Blue, mColor.m_Alpha); mColor.m_Blue, mColor.m_Alpha);
return true; return true;
} }
@ -201,15 +217,17 @@ bool extract_point_clouds (NMR::PLib3MFModelMeshObject *pMeshObject,
PointRange& points, PointRange& points,
PolygonRange&, PolygonRange&,
ColorRange& colors, ColorRange& colors,
std::string& name) { std::string& name)
{
typedef typename PointRange::value_type Point_3; typedef typename PointRange::value_type Point_3;
HRESULT hResult; HRESULT hResult;
DWORD nNeededChars; DWORD nNeededChars;
std::vector<char> pBuffer; std::vector<char> pBuffer;
// Retrieve Mesh Name Length // Retrieve Mesh Name Length
hResult = NMR::lib3mf_object_getnameutf8(pMeshObject, NULL, 0, &nNeededChars); hResult = NMR::lib3mf_object_getnameutf8(pMeshObject, NULL, 0, &nNeededChars);
if (hResult != LIB3MF_OK) if(hResult != LIB3MF_OK)
{ {
std::cerr<<"Error during name extraction."; std::cerr<<"Error during name extraction.";
points.resize(0); points.resize(0);
@ -217,7 +235,8 @@ bool extract_point_clouds (NMR::PLib3MFModelMeshObject *pMeshObject,
} }
// Retrieve Mesh Name // Retrieve Mesh Name
if (nNeededChars > 0) { if(nNeededChars > 0)
{
pBuffer.resize(nNeededChars + 1); pBuffer.resize(nNeededChars + 1);
hResult = NMR::lib3mf_object_getnameutf8(pMeshObject, &pBuffer[0], nNeededChars + 1, NULL); hResult = NMR::lib3mf_object_getnameutf8(pMeshObject, &pBuffer[0], nNeededChars + 1, NULL);
pBuffer[nNeededChars] = 0; pBuffer[nNeededChars] = 0;
@ -229,14 +248,15 @@ bool extract_point_clouds (NMR::PLib3MFModelMeshObject *pMeshObject,
} }
name = std::string(&pBuffer[0]); name = std::string(&pBuffer[0]);
} }
else{ else
{
points.resize(0); points.resize(0);
return false; return false;
} }
NMR::PLib3MFPropertyHandler * pPropertyHandler; NMR::PLib3MFPropertyHandler * pPropertyHandler;
hResult = NMR::lib3mf_meshobject_createpropertyhandler(pMeshObject, &pPropertyHandler); hResult = NMR::lib3mf_meshobject_createpropertyhandler(pMeshObject, &pPropertyHandler);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK) {
DWORD nErrorMessage; DWORD nErrorMessage;
LPCSTR pszErrorMessage; LPCSTR pszErrorMessage;
std::cerr << "could not create property handler: " << std::hex << hResult << std::endl; std::cerr << "could not create property handler: " << std::hex << hResult << std::endl;
@ -276,32 +296,34 @@ int read_from_3mf(const std::string& file_name, PointRanges& all_points,
PointRange&, PointRange&,
PolygonRange&, PolygonRange&,
ColorRange&, ColorRange&,
std::string&)> func std::string&)> func)
)
{ {
DWORD nInterfaceVersionMajor, nInterfaceVersionMinor, nInterfaceVersionMicro, nbVertices, nbPolygons; DWORD nInterfaceVersionMajor, nInterfaceVersionMinor, nInterfaceVersionMicro, nbVertices, nbPolygons;
HRESULT hResult; HRESULT hResult;
NMR::PLib3MFModel * pModel; NMR::PLib3MFModel * pModel;
NMR::PLib3MFModelReader * pReader; NMR::PLib3MFModelReader * pReader;
// Extract Extension of filename // Extract Extension of filename
std::string sReaderName("3mf"); std::string sReaderName("3mf");
hResult = NMR::lib3mf_getinterfaceversion(&nInterfaceVersionMajor, &nInterfaceVersionMinor, &nInterfaceVersionMicro); hResult = NMR::lib3mf_getinterfaceversion(&nInterfaceVersionMajor, &nInterfaceVersionMinor, &nInterfaceVersionMicro);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
std::cerr << "could not get 3MF Library version: " << std::hex << hResult << std::endl; std::cerr << "could not get 3MF Library version: " << std::hex << hResult << std::endl;
return -1; return -1;
} }
// Create Model Instance // Create Model Instance
hResult = NMR::lib3mf_createmodel(&pModel); hResult = NMR::lib3mf_createmodel(&pModel);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK) {
std::cerr << "could not create model: " << std::hex << hResult << std::endl; std::cerr << "could not create model: " << std::hex << hResult << std::endl;
return -1; return -1;
} }
// Create Model Reader // Create Model Reader
hResult = NMR::lib3mf_model_queryreader(pModel, sReaderName.c_str(), &pReader); hResult = NMR::lib3mf_model_queryreader(pModel, sReaderName.c_str(), &pReader);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
std::cerr << "could not create model reader: " << std::hex << hResult << std::endl; std::cerr << "could not create model reader: " << std::hex << hResult << std::endl;
NMR::lib3mf_release(pModel); NMR::lib3mf_release(pModel);
return -1; return -1;
@ -309,7 +331,8 @@ int read_from_3mf(const std::string& file_name, PointRanges& all_points,
// Import Model from File // Import Model from File
hResult = NMR::lib3mf_reader_readfromfileutf8(pReader, file_name.c_str()); hResult = NMR::lib3mf_reader_readfromfileutf8(pReader, file_name.c_str());
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
std::cerr << "could not parse file: " << std::hex << hResult << std::endl; std::cerr << "could not parse file: " << std::hex << hResult << std::endl;
NMR::lib3mf_release(pReader); NMR::lib3mf_release(pReader);
NMR::lib3mf_release(pModel); NMR::lib3mf_release(pModel);
@ -325,13 +348,16 @@ int read_from_3mf(const std::string& file_name, PointRanges& all_points,
NMR::PLib3MFModelResourceIterator * pResourceIterator; NMR::PLib3MFModelResourceIterator * pResourceIterator;
hResult = NMR::lib3mf_model_getobjects(pModel, &pResourceIterator); hResult = NMR::lib3mf_model_getobjects(pModel, &pResourceIterator);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
std::cerr << "could not get object: " << std::hex << hResult << std::endl; std::cerr << "could not get object: " << std::hex << hResult << std::endl;
NMR::lib3mf_release(pModel); NMR::lib3mf_release(pModel);
return -1; return -1;
} }
hResult = NMR::lib3mf_resourceiterator_movenext(pResourceIterator, &pbHasNext); hResult = NMR::lib3mf_resourceiterator_movenext(pResourceIterator, &pbHasNext);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
std::cerr << "could not get next object: " << std::hex << hResult << std::endl; std::cerr << "could not get next object: " << std::hex << hResult << std::endl;
NMR::lib3mf_release(pResourceIterator); NMR::lib3mf_release(pResourceIterator);
NMR::lib3mf_release(pModel); NMR::lib3mf_release(pModel);
@ -350,7 +376,8 @@ int read_from_3mf(const std::string& file_name, PointRanges& all_points,
// get current resource // get current resource
hResult = NMR::lib3mf_resourceiterator_getcurrent(pResourceIterator, &pResource); hResult = NMR::lib3mf_resourceiterator_getcurrent(pResourceIterator, &pResource);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
std::cerr << "could not get resource: " << std::hex << hResult << std::endl; std::cerr << "could not get resource: " << std::hex << hResult << std::endl;
NMR::lib3mf_release(pResourceIterator); NMR::lib3mf_release(pResourceIterator);
NMR::lib3mf_release(pModel); NMR::lib3mf_release(pModel);
@ -359,68 +386,79 @@ int read_from_3mf(const std::string& file_name, PointRanges& all_points,
// get resource ID // get resource ID
hResult = NMR::lib3mf_resource_getresourceid(pResource, &ResourceID); hResult = NMR::lib3mf_resource_getresourceid(pResource, &ResourceID);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
std::cerr << "could not get resource id: " << std::hex << hResult << std::endl; std::cerr << "could not get resource id: " << std::hex << hResult << std::endl;
NMR::lib3mf_release(pResource); NMR::lib3mf_release(pResource);
NMR::lib3mf_release(pResourceIterator); NMR::lib3mf_release(pResourceIterator);
NMR::lib3mf_release(pModel); NMR::lib3mf_release(pModel);
return -1; return -1;
} }
// Query mesh interface // Query mesh interface
BOOL bIsMeshObject; BOOL bIsMeshObject;
hResult = NMR::lib3mf_object_ismeshobject(pResource, &bIsMeshObject); hResult = NMR::lib3mf_object_ismeshobject(pResource, &bIsMeshObject);
if (hResult == LIB3MF_OK) if(hResult == LIB3MF_OK)
{ {
if(bIsMeshObject) { if(bIsMeshObject)
{
//skip it. Only get it through the components and buildItems. //skip it. Only get it through the components and buildItems.
} }
else { else
{
BOOL bIsComponentsObject; BOOL bIsComponentsObject;
hResult = NMR::lib3mf_object_iscomponentsobject(pResource, &bIsComponentsObject); hResult = NMR::lib3mf_object_iscomponentsobject(pResource, &bIsComponentsObject);
if ((hResult == LIB3MF_OK) && (bIsComponentsObject)) { if((hResult == LIB3MF_OK) && (bIsComponentsObject)) {
pComponentsObject = (NMR::PLib3MFModelComponentsObject*)pResource; pComponentsObject = (NMR::PLib3MFModelComponentsObject*)pResource;
DWORD nComponentCount; DWORD nComponentCount;
hResult = NMR::lib3mf_componentsobject_getcomponentcount(pComponentsObject, &nComponentCount); hResult = NMR::lib3mf_componentsobject_getcomponentcount(pComponentsObject, &nComponentCount);
if (hResult != LIB3MF_OK) if(hResult != LIB3MF_OK)
return -1; return -1;
//for each component //for each component
DWORD nIndex; DWORD nIndex;
for (nIndex = 0; nIndex < nComponentCount; ++nIndex) { for(nIndex = 0; nIndex < nComponentCount; ++nIndex) {
NMR::PLib3MFModelResource * compResource; NMR::PLib3MFModelResource * compResource;
NMR::PLib3MFModelComponent * pComponent; NMR::PLib3MFModelComponent * pComponent;
hResult = NMR::lib3mf_componentsobject_getcomponent(pComponentsObject, nIndex, &pComponent); hResult = NMR::lib3mf_componentsobject_getcomponent(pComponentsObject, nIndex, &pComponent);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
return -1; return -1;
}
hResult = NMR::lib3mf_component_getobjectresource(pComponent, &compResource); hResult = NMR::lib3mf_component_getobjectresource(pComponent, &compResource);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
NMR::lib3mf_release(pComponent); NMR::lib3mf_release(pComponent);
return -1; return -1;
} }
hResult = NMR::lib3mf_object_ismeshobject(compResource, &bIsMeshObject); hResult = NMR::lib3mf_object_ismeshobject(compResource, &bIsMeshObject);
if (hResult == LIB3MF_OK) if(hResult == LIB3MF_OK)
{ {
if(bIsMeshObject) { if(bIsMeshObject)
{
BOOL bHasTransform; BOOL bHasTransform;
NMR::MODELTRANSFORM Transform; NMR::MODELTRANSFORM Transform;
hResult = NMR::lib3mf_component_hastransform(pComponent, &bHasTransform); hResult = NMR::lib3mf_component_hastransform(pComponent, &bHasTransform);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
NMR::lib3mf_release(pComponent); NMR::lib3mf_release(pComponent);
return -1; return -1;
} }
if (bHasTransform) { if(bHasTransform)
{
// Retrieve Transform // Retrieve Transform
hResult = NMR::lib3mf_component_gettransform(pComponent, &Transform); hResult = NMR::lib3mf_component_gettransform(pComponent, &Transform);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
NMR::lib3mf_release(pComponent); NMR::lib3mf_release(pComponent);
return -1; return -1;
} }
} }
else { else
{
Transform = transform_nmr_internal::initMatrix(); Transform = transform_nmr_internal::initMatrix();
} }
pMeshObject = compResource; pMeshObject = compResource;
NMR::lib3mf_meshobject_getvertexcount(pMeshObject, &nbVertices); NMR::lib3mf_meshobject_getvertexcount(pMeshObject, &nbVertices);
NMR::lib3mf_meshobject_gettrianglecount(pMeshObject, &nbPolygons); NMR::lib3mf_meshobject_gettrianglecount(pMeshObject, &nbPolygons);
@ -429,7 +467,8 @@ int read_from_3mf(const std::string& file_name, PointRanges& all_points,
ColorRange colors(nbPolygons); ColorRange colors(nbPolygons);
std::string name; std::string name;
if(func(pMeshObject, Transform, points, triangles, colors, name)){ if(func(pMeshObject, Transform, points, triangles, colors, name))
{
all_points.push_back(points); all_points.push_back(points);
all_polygons.push_back(triangles); all_polygons.push_back(triangles);
all_colors.push_back(colors); all_colors.push_back(colors);
@ -442,16 +481,17 @@ int read_from_3mf(const std::string& file_name, PointRanges& all_points,
} }
} }
} }
// free instances // free instances
NMR::lib3mf_release(pResource); NMR::lib3mf_release(pResource);
hResult = NMR::lib3mf_resourceiterator_movenext(pResourceIterator, &pbHasNext); hResult = NMR::lib3mf_resourceiterator_movenext(pResourceIterator, &pbHasNext);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
std::cerr << "could not get next object: " << std::hex << hResult << std::endl; std::cerr << "could not get next object: " << std::hex << hResult << std::endl;
return -1; return -1;
} }
} }
/******************************************************** /********************************************************
**** Iterate Build items To Find Transformed Meshes **** **** Iterate Build items To Find Transformed Meshes ****
********************************************************/ ********************************************************/
@ -459,7 +499,8 @@ int read_from_3mf(const std::string& file_name, PointRanges& all_points,
// Iterate through all the Build items // Iterate through all the Build items
NMR::PLib3MFModelBuildItemIterator * pBuildItemIterator; NMR::PLib3MFModelBuildItemIterator * pBuildItemIterator;
hResult = NMR::lib3mf_model_getbuilditems(pModel, &pBuildItemIterator); hResult = NMR::lib3mf_model_getbuilditems(pModel, &pBuildItemIterator);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
std::cout << "could not get build items: " << std::hex << hResult << std::endl; std::cout << "could not get build items: " << std::hex << hResult << std::endl;
NMR::lib3mf_release(pBuildItemIterator); NMR::lib3mf_release(pBuildItemIterator);
NMR::lib3mf_release(pModel); NMR::lib3mf_release(pModel);
@ -467,20 +508,23 @@ int read_from_3mf(const std::string& file_name, PointRanges& all_points,
} }
hResult = NMR::lib3mf_builditemiterator_movenext(pBuildItemIterator, &pbHasNext); hResult = NMR::lib3mf_builditemiterator_movenext(pBuildItemIterator, &pbHasNext);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
std::cout << "could not get next build item: " << std::hex << hResult << std::endl; std::cout << "could not get next build item: " << std::hex << hResult << std::endl;
NMR::lib3mf_release(pBuildItemIterator); NMR::lib3mf_release(pBuildItemIterator);
NMR::lib3mf_release(pModel); NMR::lib3mf_release(pModel);
return -1; return -1;
} }
while (pbHasNext) { while (pbHasNext)
{
NMR::PLib3MFModelMeshObject * pMeshObject; NMR::PLib3MFModelMeshObject * pMeshObject;
NMR::MODELTRANSFORM Transform; NMR::MODELTRANSFORM Transform;
NMR::PLib3MFModelBuildItem * pBuildItem; NMR::PLib3MFModelBuildItem * pBuildItem;
// Retrieve Build Item // Retrieve Build Item
hResult = NMR::lib3mf_builditemiterator_getcurrent(pBuildItemIterator, &pBuildItem); hResult = NMR::lib3mf_builditemiterator_getcurrent(pBuildItemIterator, &pBuildItem);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
std::cout << "could not get build item: " << std::hex << hResult << std::endl; std::cout << "could not get build item: " << std::hex << hResult << std::endl;
NMR::lib3mf_release(pBuildItemIterator); NMR::lib3mf_release(pBuildItemIterator);
NMR::lib3mf_release(pModel); NMR::lib3mf_release(pModel);
@ -490,7 +534,8 @@ int read_from_3mf(const std::string& file_name, PointRanges& all_points,
// Retrieve Resource // Retrieve Resource
NMR::PLib3MFModelObjectResource * pObjectResource; NMR::PLib3MFModelObjectResource * pObjectResource;
hResult = NMR::lib3mf_builditem_getobjectresource(pBuildItem, &pObjectResource); hResult = NMR::lib3mf_builditem_getobjectresource(pBuildItem, &pObjectResource);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
std::cout << "could not get build item resource: " << std::hex << hResult << std::endl; std::cout << "could not get build item resource: " << std::hex << hResult << std::endl;
NMR::lib3mf_release(pBuildItem); NMR::lib3mf_release(pBuildItem);
NMR::lib3mf_release(pBuildItemIterator); NMR::lib3mf_release(pBuildItemIterator);
@ -500,9 +545,10 @@ int read_from_3mf(const std::string& file_name, PointRanges& all_points,
BOOL bIsMeshObject; BOOL bIsMeshObject;
hResult = NMR::lib3mf_object_ismeshobject(pObjectResource, &bIsMeshObject); hResult = NMR::lib3mf_object_ismeshobject(pObjectResource, &bIsMeshObject);
if (hResult == LIB3MF_OK) if(hResult == LIB3MF_OK)
{ {
if(bIsMeshObject) { if(bIsMeshObject)
{
pMeshObject = pObjectResource; pMeshObject = pObjectResource;
NMR::lib3mf_meshobject_getvertexcount(pMeshObject, &nbVertices); NMR::lib3mf_meshobject_getvertexcount(pMeshObject, &nbVertices);
NMR::lib3mf_meshobject_gettrianglecount(pMeshObject, &nbPolygons); NMR::lib3mf_meshobject_gettrianglecount(pMeshObject, &nbPolygons);
@ -511,11 +557,11 @@ int read_from_3mf(const std::string& file_name, PointRanges& all_points,
ColorRange colors(nbPolygons); ColorRange colors(nbPolygons);
std::string name; std::string name;
// Check Object Transform // Check Object Transform
BOOL bHasTransform; BOOL bHasTransform;
hResult = NMR::lib3mf_builditem_hasobjecttransform(pBuildItem, &bHasTransform); hResult = NMR::lib3mf_builditem_hasobjecttransform(pBuildItem, &bHasTransform);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
NMR::lib3mf_release(pBuildItem); NMR::lib3mf_release(pBuildItem);
NMR::lib3mf_release(pBuildItemIterator); NMR::lib3mf_release(pBuildItemIterator);
NMR::lib3mf_release(pModel); NMR::lib3mf_release(pModel);
@ -523,10 +569,11 @@ int read_from_3mf(const std::string& file_name, PointRanges& all_points,
return -1; return -1;
} }
if (bHasTransform) { if(bHasTransform)
{
// Retrieve Transform // Retrieve Transform
hResult = NMR::lib3mf_builditem_getobjecttransform(pBuildItem, &Transform); hResult = NMR::lib3mf_builditem_getobjecttransform(pBuildItem, &Transform);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK) {
NMR::lib3mf_release(pBuildItem); NMR::lib3mf_release(pBuildItem);
NMR::lib3mf_release(pBuildItemIterator); NMR::lib3mf_release(pBuildItemIterator);
NMR::lib3mf_release(pModel); NMR::lib3mf_release(pModel);
@ -534,7 +581,8 @@ int read_from_3mf(const std::string& file_name, PointRanges& all_points,
return -1; return -1;
} }
} }
else { else
{
Transform = transform_nmr_internal::initMatrix(); Transform = transform_nmr_internal::initMatrix();
} }
@ -546,6 +594,7 @@ int read_from_3mf(const std::string& file_name, PointRanges& all_points,
} }
} }
} }
// Release Object Resource ID // Release Object Resource ID
NMR::lib3mf_release(pObjectResource); NMR::lib3mf_release(pObjectResource);
// Release Build Item // Release Build Item
@ -553,11 +602,13 @@ int read_from_3mf(const std::string& file_name, PointRanges& all_points,
// Move to next Item // Move to next Item
hResult = NMR::lib3mf_builditemiterator_movenext(pBuildItemIterator, &pbHasNext); hResult = NMR::lib3mf_builditemiterator_movenext(pBuildItemIterator, &pbHasNext);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
std::cerr << "could not get next build item: " << std::hex << hResult << std::endl; std::cerr << "could not get next build item: " << std::hex << hResult << std::endl;
return -1; return -1;
} }
} }
// Release Build Item Iterator // Release Build Item Iterator
NMR::lib3mf_release(pBuildItemIterator); NMR::lib3mf_release(pBuildItemIterator);
return all_points.size(); return all_points.size();
@ -592,57 +643,54 @@ int read_from_3mf(const std::string& file_name, PointRanges& all_points,
*/ */
template<typename PointRanges, typename PolygonRanges, typename ColorRanges> template<typename PointRanges, typename PolygonRanges, typename ColorRanges>
int read_triangle_soups_from_3mf(const std::string& file_name, PointRanges& all_points, int read_triangle_soups_from_3mf(const std::string& file_name, PointRanges& all_points,
PolygonRanges& all_polygons, ColorRanges& all_colors, PolygonRanges& all_polygons, ColorRanges& all_colors,
std::vector<std::string>& names std::vector<std::string>& names)
)
{ {
typedef typename PointRanges::value_type PointRange; typedef typename PointRanges::value_type PointRange;
typedef typename PolygonRanges::value_type PolygonRange; typedef typename PolygonRanges::value_type PolygonRange;
typedef typename ColorRanges::value_type ColorRange; typedef typename ColorRanges::value_type ColorRange;
return read_from_3mf<PointRanges,PolygonRanges,ColorRanges, return read_from_3mf<PointRanges,PolygonRanges,ColorRanges,
PointRange, PolygonRange, ColorRange> PointRange, PolygonRange, ColorRange>
(file_name, all_points, all_polygons, (file_name, all_points, all_polygons, all_colors, names,
all_colors, names, extract_soups<PointRange, PolygonRange, ColorRange>); extract_soups<PointRange, PolygonRange, ColorRange>);
} }
template<typename PointRanges, typename ColorRanges> template<typename PointRanges, typename ColorRanges>
int read_polylines_from_3mf(const std::string& file_name, int read_polylines_from_3mf(const std::string& file_name,
PointRanges& all_points, PointRanges& all_points,
ColorRanges& all_colors, ColorRanges& all_colors,
std::vector<std::string>& names std::vector<std::string>& names)
)
{ {
typedef typename PointRanges::value_type PointRange; typedef typename PointRanges::value_type PointRange;
typedef std::vector<std::size_t> Polygon; typedef std::vector<std::size_t> Polygon;
typedef std::vector<Polygon> PolygonRange; typedef std::vector<Polygon> PolygonRange;
typedef std::vector<CGAL::Color> ColorRange; typedef std::vector<CGAL::Color> ColorRange;
std::vector<PolygonRange> all_polygons; std::vector<PolygonRange> all_polygons;
return read_from_3mf<PointRanges,std::vector<PolygonRange>, return read_from_3mf<PointRanges,std::vector<PolygonRange>,
std::vector<ColorRange>, PointRange, PolygonRange, ColorRange> std::vector<ColorRange>, PointRange, PolygonRange, ColorRange>
(file_name, all_points, all_polygons, all_colors, names, (file_name, all_points, all_polygons, all_colors, names,
extract_polylines<PointRange, PolygonRange, ColorRange>); extract_polylines<PointRange, PolygonRange, ColorRange>);
} }
template<typename PointRanges, typename ColorRanges> template<typename PointRanges, typename ColorRanges>
int read_point_clouds_from_3mf(const std::string& file_name, int read_point_clouds_from_3mf(const std::string& file_name,
PointRanges& all_points, PointRanges& all_points,
ColorRanges& all_colors, ColorRanges& all_colors,
std::vector<std::string>& names std::vector<std::string>& names)
)
{ {
typedef typename PointRanges::value_type PointRange; typedef typename PointRanges::value_type PointRange;
typedef std::vector<std::size_t> Polygon; typedef std::vector<std::size_t> Polygon;
typedef std::vector<Polygon> PolygonRange; typedef std::vector<Polygon> PolygonRange;
typedef std::vector<CGAL::Color> ColorRange; typedef std::vector<CGAL::Color> ColorRange;
std::vector<PolygonRange> all_polygons; std::vector<PolygonRange> all_polygons;
return read_from_3mf<PointRanges,std::vector<PolygonRange>, return read_from_3mf<PointRanges,std::vector<PolygonRange>,
std::vector<ColorRange>, PointRange, PolygonRange, ColorRange> std::vector<ColorRange>, PointRange, PolygonRange, ColorRange>
(file_name, all_points, all_polygons, all_colors, names, (file_name, all_points, all_polygons, all_colors, names,
extract_point_clouds<PointRange, PolygonRange, ColorRange>); extract_point_clouds<PointRange, PolygonRange, ColorRange>);
} }
}//end CGAL
} // namespace CGAL
#endif // CGAL_IO_READ_3MF_H #endif // CGAL_IO_READ_3MF_H

View File

@ -11,16 +11,19 @@
#ifndef CGAL_IO_WRITE_3MF_H #ifndef CGAL_IO_WRITE_3MF_H
#define CGAL_IO_WRITE_3MF_H #define CGAL_IO_WRITE_3MF_H
#include <iostream>
#include <vector>
#include <string>
#include <CGAL/boost/graph/iterator.h>
#include <CGAL/IO/Color.h> #include <CGAL/IO/Color.h>
#include <Model/COM/NMR_DLLInterfaces.h> #include <Model/COM/NMR_DLLInterfaces.h>
namespace CGAL{ #include <iostream>
namespace tmf_internal{ #include <vector>
#include <string>
namespace CGAL {
namespace tmf_internal {
// Utility functions to create vertices and triangles // Utility functions to create vertices and triangles
NMR::MODELMESHVERTEX fnCreateVertex(float x, float y, float z) NMR::MODELMESHVERTEX fnCreateVertex(float x, float y, float z)
{ {
@ -30,6 +33,7 @@ NMR::MODELMESHVERTEX fnCreateVertex(float x, float y, float z)
result.m_fPosition[2] = z; result.m_fPosition[2] = z;
return result; return result;
} }
NMR::MODELMESHTRIANGLE fnCreateTriangle(int v0, int v1, int v2) NMR::MODELMESHTRIANGLE fnCreateTriangle(int v0, int v1, int v2)
{ {
NMR::MODELMESHTRIANGLE result; NMR::MODELMESHTRIANGLE result;
@ -38,6 +42,7 @@ NMR::MODELMESHTRIANGLE fnCreateTriangle(int v0, int v1, int v2)
result.m_nIndices[2] = v2; result.m_nIndices[2] = v2;
return result; return result;
} }
NMR::MODELMESHCOLOR_SRGB fnCreateColor(unsigned char red, unsigned char green, NMR::MODELMESHCOLOR_SRGB fnCreateColor(unsigned char red, unsigned char green,
unsigned char blue, unsigned char alpha=255) unsigned char blue, unsigned char alpha=255)
{ {
@ -47,10 +52,9 @@ NMR::MODELMESHCOLOR_SRGB fnCreateColor(unsigned char red, unsigned char green,
result.m_Blue = blue; result.m_Blue = blue;
result.m_Alpha = alpha; result.m_Alpha = alpha;
return result; return result;
} }
} //end internal
} // namespace tmf_internal
bool add_build_item(NMR::PLib3MFModel * pModel, bool add_build_item(NMR::PLib3MFModel * pModel,
NMR::PLib3MFModelMeshObject* pMeshObject) NMR::PLib3MFModelMeshObject* pMeshObject)
@ -58,19 +62,21 @@ bool add_build_item(NMR::PLib3MFModel * pModel,
HRESULT hResult; HRESULT hResult;
DWORD nErrorMessage; DWORD nErrorMessage;
LPCSTR pszErrorMessage; LPCSTR pszErrorMessage;
// Add Build Item for Mesh // Add Build Item for Mesh
NMR::PLib3MFModelBuildItem * pBuildItem; NMR::PLib3MFModelBuildItem * pBuildItem;
hResult = NMR::lib3mf_model_addbuilditem(pModel, pMeshObject, NULL, &pBuildItem); hResult = NMR::lib3mf_model_addbuilditem(pModel, pMeshObject, NULL, &pBuildItem);
if (hResult != LIB3MF_OK) {
std::cerr << "could not create build item: " if(hResult != LIB3MF_OK)
<< std::hex << hResult << std::endl; {
std::cerr << "could not create build item: " << std::hex << hResult << std::endl;
NMR::lib3mf_getlasterror(pModel, &nErrorMessage, &pszErrorMessage); NMR::lib3mf_getlasterror(pModel, &nErrorMessage, &pszErrorMessage);
std::cerr << "error #" << std::hex << nErrorMessage std::cerr << "error #" << std::hex << nErrorMessage << ": " << pszErrorMessage << std::endl;
<< ": " << pszErrorMessage << std::endl;
NMR::lib3mf_release(pMeshObject); NMR::lib3mf_release(pMeshObject);
NMR::lib3mf_release(pModel); NMR::lib3mf_release(pModel);
return false; return false;
} }
// Release BuildItem and Mesh // Release BuildItem and Mesh
NMR::lib3mf_release(pMeshObject); NMR::lib3mf_release(pMeshObject);
NMR::lib3mf_release(pBuildItem); NMR::lib3mf_release(pBuildItem);
@ -83,78 +89,76 @@ bool export_model_to_file(const std::string& file_name,
HRESULT hResult; HRESULT hResult;
DWORD nErrorMessage; DWORD nErrorMessage;
LPCSTR pszErrorMessage; LPCSTR pszErrorMessage;
// Output mesh as 3MF // Output mesh as 3MF
// Create Model Writer for 3MF // Create Model Writer for 3MF
NMR::PLib3MFModelWriter * p3MFWriter; NMR::PLib3MFModelWriter * p3MFWriter;
hResult = NMR::lib3mf_model_querywriter(pModel, "3mf", &p3MFWriter); hResult = NMR::lib3mf_model_querywriter(pModel, "3mf", &p3MFWriter);
if (hResult != LIB3MF_OK) {
std::cerr << "could not create model reader: " if(hResult != LIB3MF_OK)
<< std::hex << hResult << std::endl; {
std::cerr << "could not create model reader: " << std::hex << hResult << std::endl;
NMR::lib3mf_getlasterror(pModel, &nErrorMessage, &pszErrorMessage); NMR::lib3mf_getlasterror(pModel, &nErrorMessage, &pszErrorMessage);
std::cerr << "error #" << std::hex << nErrorMessage std::cerr << "error #" << std::hex << nErrorMessage << ": " << pszErrorMessage << std::endl;
<< ": " << pszErrorMessage << std::endl;
NMR::lib3mf_release(pModel); NMR::lib3mf_release(pModel);
return false; return false;
} }
// Export Model into File // Export Model into File
hResult = NMR::lib3mf_writer_writetofileutf8(p3MFWriter, file_name.c_str()); hResult = NMR::lib3mf_writer_writetofileutf8(p3MFWriter, file_name.c_str());
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK) {
std::cerr << "could not write file: " << std::hex << hResult << std::endl; std::cerr << "could not write file: " << std::hex << hResult << std::endl;
NMR::lib3mf_getlasterror(p3MFWriter, &nErrorMessage, &pszErrorMessage); NMR::lib3mf_getlasterror(p3MFWriter, &nErrorMessage, &pszErrorMessage);
std::cerr << "error #" << std::hex << nErrorMessage << ": " std::cerr << "error #" << std::hex << nErrorMessage << ": " << pszErrorMessage << std::endl;
<< pszErrorMessage << std::endl;
NMR::lib3mf_release(pModel); NMR::lib3mf_release(pModel);
NMR::lib3mf_release(p3MFWriter); NMR::lib3mf_release(p3MFWriter);
return false; return false;
} }
// Release Model Writer // Release Model Writer
NMR::lib3mf_release(p3MFWriter); NMR::lib3mf_release(p3MFWriter);
return true; return true;
} }
template<typename PointRange, typename PolygonRange, typename ColorRange> template<typename PointRange, typename PolygonRange, typename ColorRange>
bool write_mesh_to_model( const PointRange& points, bool write_mesh_to_model(const PointRange& points,
const PolygonRange& polygons, const PolygonRange& polygons,
const ColorRange& colors, const ColorRange& colors,
const std::string& name, const std::string& name,
NMR::PLib3MFModelMeshObject** pMeshObject, NMR::PLib3MFModelMeshObject** pMeshObject,
NMR::PLib3MFModel * pModel NMR::PLib3MFModel * pModel)
)
{ {
DWORD nErrorMessage; DWORD nErrorMessage;
LPCSTR pszErrorMessage; LPCSTR pszErrorMessage;
HRESULT hResult; HRESULT hResult;
// Create mesh structure // Create mesh structure
std::vector<NMR::MODELMESHVERTEX> pVertices; std::vector<NMR::MODELMESHVERTEX> pVertices;
std::vector<NMR::MODELMESHTRIANGLE> pTriangles; std::vector<NMR::MODELMESHTRIANGLE> pTriangles;
// Create Mesh Object // Create Mesh Object
hResult = NMR::lib3mf_model_addmeshobject(pModel, pMeshObject); hResult = NMR::lib3mf_model_addmeshobject(pModel, pMeshObject);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
std::cerr << "could not add mesh object: " << std::hex {
<< hResult << std::endl; std::cerr << "could not add mesh object: " << std::hex << hResult << std::endl;
NMR::lib3mf_getlasterror(pModel, &nErrorMessage, &pszErrorMessage); NMR::lib3mf_getlasterror(pModel, &nErrorMessage, &pszErrorMessage);
std::cerr << "error #" << std::hex << nErrorMessage << ": " std::cerr << "error #" << std::hex << nErrorMessage << ": " << pszErrorMessage << std::endl;
<< pszErrorMessage << std::endl;
NMR::lib3mf_release(pModel); NMR::lib3mf_release(pModel);
return false; return false;
} }
for( auto point : points)
{ for(const auto& point : points)
pVertices.push_back(tmf_internal::fnCreateVertex(point.x(), point.y(), point.z())); pVertices.push_back(tmf_internal::fnCreateVertex(point.x(), point.y(), point.z()));
}
for(const auto& triangle : polygons)
for( auto triangle : polygons)
{
pTriangles.push_back(tmf_internal::fnCreateTriangle(triangle[0], triangle[1], triangle[2])); pTriangles.push_back(tmf_internal::fnCreateTriangle(triangle[0], triangle[1], triangle[2]));
}
hResult = NMR::lib3mf_meshobject_setgeometry(*pMeshObject, pVertices.data(), hResult = NMR::lib3mf_meshobject_setgeometry(*pMeshObject, pVertices.data(),
pVertices.size(), pTriangles.data(), pVertices.size(), pTriangles.data(),
pTriangles.size()); pTriangles.size());
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
std::cerr << "could not set mesh geometry: " {
<< std::hex << hResult << std::endl; std::cerr << "could not set mesh geometry: " << std::hex << hResult << std::endl;
NMR::lib3mf_getlasterror(*pMeshObject, &nErrorMessage, &pszErrorMessage); NMR::lib3mf_getlasterror(*pMeshObject, &nErrorMessage, &pszErrorMessage);
std::cerr << "error #" << std::hex << nErrorMessage std::cerr << "error #" << std::hex << nErrorMessage
<< ": " << pszErrorMessage << std::endl; << ": " << pszErrorMessage << std::endl;
@ -162,16 +166,18 @@ bool write_mesh_to_model( const PointRange& points,
NMR::lib3mf_release(pModel); NMR::lib3mf_release(pModel);
return false; return false;
} }
// Create color entries // Create color entries
NMR::PLib3MFPropertyHandler * pPropertyHandler; NMR::PLib3MFPropertyHandler * pPropertyHandler;
hResult = NMR::lib3mf_meshobject_createpropertyhandler(*pMeshObject, &pPropertyHandler); hResult = NMR::lib3mf_meshobject_createpropertyhandler(*pMeshObject, &pPropertyHandler);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
std::cerr << "could not create property handler: " << std::hex << hResult << std::endl; {
NMR::lib3mf_getlasterror(*pMeshObject, &nErrorMessage, &pszErrorMessage); std::cerr << "could not create property handler: " << std::hex << hResult << std::endl;
std::cerr << "error #" << std::hex << nErrorMessage << ": " << pszErrorMessage << std::endl; NMR::lib3mf_getlasterror(*pMeshObject, &nErrorMessage, &pszErrorMessage);
NMR::lib3mf_release(*pMeshObject); std::cerr << "error #" << std::hex << nErrorMessage << ": " << pszErrorMessage << std::endl;
NMR::lib3mf_release(pModel); NMR::lib3mf_release(*pMeshObject);
return false; NMR::lib3mf_release(pModel);
return false;
} }
// define colors // define colors
@ -188,7 +194,8 @@ bool write_mesh_to_model( const PointRange& points,
// make sure to define a default property // make sure to define a default property
NMR::PLib3MFDefaultPropertyHandler * pDefaultPropertyHandler; NMR::PLib3MFDefaultPropertyHandler * pDefaultPropertyHandler;
hResult = NMR::lib3mf_object_createdefaultpropertyhandler(*pMeshObject, &pDefaultPropertyHandler); hResult = NMR::lib3mf_object_createdefaultpropertyhandler(*pMeshObject, &pDefaultPropertyHandler);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
std::cerr<< "could not create default property handler: " << std::hex << hResult << std::endl; std::cerr<< "could not create default property handler: " << std::hex << hResult << std::endl;
NMR::lib3mf_getlasterror(*pMeshObject, &nErrorMessage, &pszErrorMessage); NMR::lib3mf_getlasterror(*pMeshObject, &nErrorMessage, &pszErrorMessage);
std::cerr<< "error #" << std::hex << nErrorMessage << ": " << pszErrorMessage << std::endl; std::cerr<< "error #" << std::hex << nErrorMessage << ": " << pszErrorMessage << std::endl;
@ -196,16 +203,17 @@ bool write_mesh_to_model( const PointRange& points,
NMR::lib3mf_release(pModel); NMR::lib3mf_release(pModel);
return false; return false;
} }
NMR::MODELMESHCOLOR_SRGB default_color = tmf_internal::fnCreateColor(0,0,0,0); NMR::MODELMESHCOLOR_SRGB default_color = tmf_internal::fnCreateColor(0,0,0,0);
NMR::lib3mf_defaultpropertyhandler_setcolor(pDefaultPropertyHandler, NMR::lib3mf_defaultpropertyhandler_setcolor(pDefaultPropertyHandler, &default_color);
&default_color);
// release default property handler // release default property handler
NMR::lib3mf_release(pDefaultPropertyHandler); NMR::lib3mf_release(pDefaultPropertyHandler);
// Set name // Set name
hResult = NMR::lib3mf_object_setnameutf8(*pMeshObject, name.c_str()); hResult = NMR::lib3mf_object_setnameutf8(*pMeshObject, name.c_str());
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
std::cerr << "could not set object name: " << std::hex << hResult << std::endl; std::cerr << "could not set object name: " << std::hex << hResult << std::endl;
NMR::lib3mf_getlasterror(*pMeshObject, &nErrorMessage, &pszErrorMessage); NMR::lib3mf_getlasterror(*pMeshObject, &nErrorMessage, &pszErrorMessage);
std::cerr << "error #" << std::hex << nErrorMessage << ": " << pszErrorMessage << std::endl; std::cerr << "error #" << std::hex << nErrorMessage << ": " << pszErrorMessage << std::endl;
@ -224,41 +232,41 @@ bool write_points(const PointRange& points,
const Color& color, const Color& color,
const std::string& name, const std::string& name,
NMR::PLib3MFModelMeshObject** pMeshObject, NMR::PLib3MFModelMeshObject** pMeshObject,
NMR::PLib3MFModel * pModel NMR::PLib3MFModel * pModel)
)
{ {
DWORD nErrorMessage; DWORD nErrorMessage;
LPCSTR pszErrorMessage; LPCSTR pszErrorMessage;
HRESULT hResult; HRESULT hResult;
// Create mesh structure // Create mesh structure
std::vector<NMR::MODELMESHVERTEX> pVertices; std::vector<NMR::MODELMESHVERTEX> pVertices;
// Create Mesh Object // Create Mesh Object
hResult = NMR::lib3mf_model_addmeshobject(pModel, pMeshObject); hResult = NMR::lib3mf_model_addmeshobject(pModel, pMeshObject);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
std::cerr << "could not add mesh object: " << std::hex {
<< hResult << std::endl; std::cerr << "could not add mesh object: " << std::hex << hResult << std::endl;
NMR::lib3mf_getlasterror(pModel, &nErrorMessage, &pszErrorMessage); NMR::lib3mf_getlasterror(pModel, &nErrorMessage, &pszErrorMessage);
std::cerr << "error #" << std::hex << nErrorMessage << ": " std::cerr << "error #" << std::hex << nErrorMessage << ": " << pszErrorMessage << std::endl;
<< pszErrorMessage << std::endl;
NMR::lib3mf_release(pModel); NMR::lib3mf_release(pModel);
return false; return false;
} }
//add 3 demmy vertices to be sure to have a valid triangle and accept point sets with less than 3 vertices. //add 3 demmy vertices to be sure to have a valid triangle and accept point sets with less than 3 vertices.
for(int i = 0; i< 3; ++i) for(int i = 0; i< 3; ++i)
pVertices.push_back(tmf_internal::fnCreateVertex(0,0,0)); pVertices.push_back(tmf_internal::fnCreateVertex(0,0,0));
for( auto point : points)
{ for(const auto& point : points)
pVertices.push_back(tmf_internal::fnCreateVertex(point.x(), point.y(), point.z())); pVertices.push_back(tmf_internal::fnCreateVertex(point.x(), point.y(), point.z()));
}
NMR::MODELMESHTRIANGLE dummy_triangle = tmf_internal::fnCreateTriangle(0,1,2); //add a triangle to avoid lib error. NMR::MODELMESHTRIANGLE dummy_triangle = tmf_internal::fnCreateTriangle(0,1,2); //add a triangle to avoid lib error.
hResult = NMR::lib3mf_meshobject_setgeometry(*pMeshObject, pVertices.data(), hResult = NMR::lib3mf_meshobject_setgeometry(*pMeshObject, pVertices.data(),
pVertices.size(), &dummy_triangle, 1); pVertices.size(), &dummy_triangle, 1);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
std::cerr << "could not set mesh geometry: " {
<< std::hex << hResult << std::endl; std::cerr << "could not set mesh geometry: " << std::hex << hResult << std::endl;
NMR::lib3mf_getlasterror(*pMeshObject, &nErrorMessage, &pszErrorMessage); NMR::lib3mf_getlasterror(*pMeshObject, &nErrorMessage, &pszErrorMessage);
std::cerr << "error #" << std::hex << nErrorMessage std::cerr << "error #" << std::hex << nErrorMessage << ": " << pszErrorMessage << std::endl;
<< ": " << pszErrorMessage << std::endl;
NMR::lib3mf_release(*pMeshObject); NMR::lib3mf_release(*pMeshObject);
NMR::lib3mf_release(pModel); NMR::lib3mf_release(pModel);
return false; return false;
@ -267,27 +275,29 @@ bool write_points(const PointRange& points,
// Create color entries // Create color entries
NMR::PLib3MFPropertyHandler * pPropertyHandler; NMR::PLib3MFPropertyHandler * pPropertyHandler;
hResult = NMR::lib3mf_meshobject_createpropertyhandler(*pMeshObject, &pPropertyHandler); hResult = NMR::lib3mf_meshobject_createpropertyhandler(*pMeshObject, &pPropertyHandler);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
std::cerr << "could not create property handler: " << std::hex << hResult << std::endl; {
NMR::lib3mf_getlasterror(*pMeshObject, &nErrorMessage, &pszErrorMessage); std::cerr << "could not create property handler: " << std::hex << hResult << std::endl;
std::cerr << "error #" << std::hex << nErrorMessage << ": " << pszErrorMessage << std::endl; NMR::lib3mf_getlasterror(*pMeshObject, &nErrorMessage, &pszErrorMessage);
NMR::lib3mf_release(*pMeshObject); std::cerr << "error #" << std::hex << nErrorMessage << ": " << pszErrorMessage << std::endl;
NMR::lib3mf_release(pModel); NMR::lib3mf_release(*pMeshObject);
return false; NMR::lib3mf_release(pModel);
return false;
} }
// define colors // define colors
NMR::MODELMESHCOLOR_SRGB sColor = tmf_internal::fnCreateColor (color.red(), NMR::MODELMESHCOLOR_SRGB sColor = tmf_internal::fnCreateColor (color.red(),
color.green(), color.green(),
color.blue(), color.blue(),
color.alpha()); color.alpha());
// One-colored Triangles // One-colored Triangles
NMR::lib3mf_propertyhandler_setsinglecolor(pPropertyHandler, 0, &sColor); NMR::lib3mf_propertyhandler_setsinglecolor(pPropertyHandler, 0, &sColor);
// make sure to define a default property // make sure to define a default property
NMR::PLib3MFDefaultPropertyHandler * pDefaultPropertyHandler; NMR::PLib3MFDefaultPropertyHandler * pDefaultPropertyHandler;
hResult = NMR::lib3mf_object_createdefaultpropertyhandler(*pMeshObject, &pDefaultPropertyHandler); hResult = NMR::lib3mf_object_createdefaultpropertyhandler(*pMeshObject, &pDefaultPropertyHandler);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
std::cerr<< "could not create default property handler: " << std::hex << hResult << std::endl; std::cerr<< "could not create default property handler: " << std::hex << hResult << std::endl;
NMR::lib3mf_getlasterror(*pMeshObject, &nErrorMessage, &pszErrorMessage); NMR::lib3mf_getlasterror(*pMeshObject, &nErrorMessage, &pszErrorMessage);
std::cerr<< "error #" << std::hex << nErrorMessage << ": " << pszErrorMessage << std::endl; std::cerr<< "error #" << std::hex << nErrorMessage << ": " << pszErrorMessage << std::endl;
@ -295,6 +305,7 @@ bool write_points(const PointRange& points,
NMR::lib3mf_release(pModel); NMR::lib3mf_release(pModel);
return false; return false;
} }
NMR::MODELMESHCOLOR_SRGB default_color = tmf_internal::fnCreateColor(0,0,0,0); NMR::MODELMESHCOLOR_SRGB default_color = tmf_internal::fnCreateColor(0,0,0,0);
NMR::lib3mf_defaultpropertyhandler_setcolor(pDefaultPropertyHandler, NMR::lib3mf_defaultpropertyhandler_setcolor(pDefaultPropertyHandler,
&default_color); &default_color);
@ -304,7 +315,8 @@ bool write_points(const PointRange& points,
// Set name // Set name
hResult = NMR::lib3mf_object_setnameutf8(*pMeshObject, name.c_str()); hResult = NMR::lib3mf_object_setnameutf8(*pMeshObject, name.c_str());
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
std::cerr << "could not set object name: " << std::hex << hResult << std::endl; std::cerr << "could not set object name: " << std::hex << hResult << std::endl;
NMR::lib3mf_getlasterror(*pMeshObject, &nErrorMessage, &pszErrorMessage); NMR::lib3mf_getlasterror(*pMeshObject, &nErrorMessage, &pszErrorMessage);
std::cerr << "error #" << std::hex << nErrorMessage << ": " << pszErrorMessage << std::endl; std::cerr << "error #" << std::hex << nErrorMessage << ": " << pszErrorMessage << std::endl;
@ -312,6 +324,7 @@ bool write_points(const PointRange& points,
NMR::lib3mf_release(pModel); NMR::lib3mf_release(pModel);
return false; return false;
} }
return add_build_item(pModel, *pMeshObject); return add_build_item(pModel, *pMeshObject);
} }
@ -320,8 +333,7 @@ bool write_point_cloud_to_model(const PointRange& points,
const Color& color, const Color& color,
const std::string& name, const std::string& name,
NMR::PLib3MFModelMeshObject** pMeshObject, NMR::PLib3MFModelMeshObject** pMeshObject,
NMR::PLib3MFModel * pModel NMR::PLib3MFModel * pModel)
)
{ {
std::string pc_name = name; std::string pc_name = name;
pc_name.append("_cgal_pc"); pc_name.append("_cgal_pc");
@ -333,8 +345,7 @@ bool write_polyline_to_model(const PointRange& points,
const Color& color, const Color& color,
const std::string& name, const std::string& name,
NMR::PLib3MFModelMeshObject** pMeshObject, NMR::PLib3MFModelMeshObject** pMeshObject,
NMR::PLib3MFModel * pModel NMR::PLib3MFModel * pModel)
)
{ {
std::string pc_name = name; std::string pc_name = name;
pc_name.append("_cgal_pl"); pc_name.append("_cgal_pl");
@ -362,33 +373,36 @@ bool write_polyline_to_model(const PointRange& points,
*/ */
template<typename PointRanges, typename PolygonRanges> template<typename PointRanges, typename PolygonRanges>
bool write_triangle_soups_to_3mf(const std::string& file_name, bool write_triangle_soups_to_3mf(const std::string& file_name,
const PointRanges& all_points, const PointRanges& all_points,
const PolygonRanges& all_polygons, const PolygonRanges& all_polygons,
const std::vector<std::string>& names) const std::vector<std::string>& names)
{ {
HRESULT hResult; // Create Model Instance
// Create Model Instance
NMR::PLib3MFModel * pModel; NMR::PLib3MFModel * pModel;
hResult = NMR::lib3mf_createmodel(&pModel); HRESULT hResult = NMR::lib3mf_createmodel(&pModel);
if (hResult != LIB3MF_OK) { if(hResult != LIB3MF_OK)
{
std::cerr << "could not create model: " << std::hex << hResult << std::endl; std::cerr << "could not create model: " << std::hex << hResult << std::endl;
return false; return false;
} }
for(std::size_t id = 0; id < all_points.size(); ++id)
for(std::size_t id = 0; id < all_points.size(); ++id)
{ {
NMR::PLib3MFModelMeshObject* pMeshObject; NMR::PLib3MFModelMeshObject* pMeshObject;
std::string name; std::string name;
if(names.size() > id if(names.size() > id && ! names[id].empty())
&& ! names[id].empty())
{ {
name=names[id]; name=names[id];
} }
else else
{
name = std::string(""); name = std::string("");
}
std::vector<CGAL::Color> colors(all_polygons[id].size()); std::vector<CGAL::Color> colors(all_polygons[id].size());
write_mesh_to_model(all_points[id], all_polygons[id], colors, name, &pMeshObject, pModel); write_mesh_to_model(all_points[id], all_polygons[id], colors, name, &pMeshObject, pModel);
} }
return export_model_to_file(file_name, pModel); return export_model_to_file(file_name, pModel);
} }
@ -412,46 +426,53 @@ bool write_triangle_meshes_to_3mf(const std::string& file_name,
const TriangleMeshRange& tms, const TriangleMeshRange& tms,
const std::vector<std::string>& names) const std::vector<std::string>& names)
{ {
typedef typename TriangleMeshRange::value_type Mesh; typedef typename TriangleMeshRange::value_type Mesh;
typedef typename boost::property_map<Mesh, boost::vertex_point_t>::type VPMap; typedef typename boost::property_map<Mesh, boost::vertex_point_t>::type VPMap;
typedef typename boost::property_traits<VPMap>::value_type Point_3; typedef typename boost::property_traits<VPMap>::value_type Point_3;
typedef std::vector<std::size_t> Polygon; typedef boost::graph_traits<Mesh>::vertex_descriptor vertex_descriptor;
typedef std::vector<Polygon> PolygonRange; typedef boost::graph_traits<Mesh>::face_descriptor face_descriptor;
typedef std::vector<Point_3> PointRange; typedef std::vector<std::size_t> Polygon;
typedef std::vector<Polygon> PolygonRange;
typedef std::vector<Point_3> PointRange;
std::vector<PointRange> all_points; std::vector<PointRange> all_points;
std::vector<PolygonRange> all_polygons; std::vector<PolygonRange> all_polygons;
for(const auto& tm : tms)
for(auto tm : tms)
{ {
PointRange points; PointRange points;
points.reserve(num_vertices(tm));
PolygonRange triangles; PolygonRange triangles;
triangles.reserve(num_faces(tm));
VPMap vpm = get(boost::vertex_point, tm); VPMap vpm = get(boost::vertex_point, tm);
std::unordered_map<typename boost::graph_traits<Mesh>::vertex_descriptor, std::unordered_map<typename boost::graph_traits<Mesh>::vertex_descriptor, std::size_t> vertex_id_map;
std::size_t> vertex_id_map;
std::size_t i = 0; std::size_t i = 0;
for(auto v : vertices(tm)) for(vertex_descriptor v : vertices(tm))
{ {
points.push_back(get(vpm, v)); points.push_back(get(vpm, v));
vertex_id_map[v] = i++; vertex_id_map[v] = i++;
} }
all_points.push_back(points); all_points.push_back(points);
for(auto f : faces(tm)) for(face_descriptor f : faces(tm))
{ {
Polygon triangle; Polygon triangle;
for(auto vert : CGAL::vertices_around_face(halfedge(f, tm), tm)) for(vertex_descriptor vert : CGAL::vertices_around_face(halfedge(f, tm), tm))
{
triangle.push_back(vertex_id_map[vert]); triangle.push_back(vertex_id_map[vert]);
}
triangles.push_back(triangle); triangles.push_back(triangle);
} }
all_polygons.push_back(triangles); all_polygons.push_back(triangles);
} }
return write_triangle_soups_to_3mf(file_name, all_points, all_polygons, names); return write_triangle_soups_to_3mf(file_name, all_points, all_polygons, names);
} }
}//end CGAL
} // namespace CGAL
#endif // CGAL_IO_WRITE_3MF_H #endif // CGAL_IO_WRITE_3MF_H

View File

@ -1,57 +1,70 @@
// Copyright (c) 1997 // Copyright (c) 1997
// Utrecht University (The Netherlands), // Utrecht University (The Netherlands),
// ETH Zurich (Switzerland), // ETH Zurich (Switzerland),
// INRIA Sophia-Antipolis (France), // INRIA Sophia-Antipolis (France),
// Max-Planck-Institute Saarbruecken (Germany), // Max-Planck-Institute Saarbruecken (Germany),
// and Tel-Aviv University (Israel). All rights reserved. // and Tel-Aviv University (Israel). All rights reserved.
// //
// This file is part of CGAL (www.cgal.org); // This file is part of CGAL (www.cgal.org);
// //
// $URL$ // $URL$
// $Id$ // $Id$
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial // SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
// //
// //
// Author(s) : Lutz Kettner <kettner@mpi-sb.mpg.de> // Author(s) : Lutz Kettner <kettner@mpi-sb.mpg.de>
#ifndef CGAL_IO_OBJ_FILE_WRITER_WAVEFRONT_H #ifndef CGAL_IO_OBJ_FILE_WRITER_WAVEFRONT_H
#define CGAL_IO_OBJ_FILE_WRITER_WAVEFRONT_H 1 #define CGAL_IO_OBJ_FILE_WRITER_WAVEFRONT_H
#include <CGAL/IO/binary_file_io.h> #include <CGAL/IO/binary_file_io.h>
#include <iostream> #include <iostream>
#include <cstddef> #include <cstddef>
namespace CGAL { namespace CGAL {
class CGAL_EXPORT File_writer_wavefront { class File_writer_wavefront
std::ostream* m_out; {
std::size_t m_facets; std::ostream* m_out;
std::size_t m_facets;
public: public:
std::ostream& out() const { return *m_out; } std::ostream& out() const { return *m_out; }
void write_header( std::ostream& out, void write_header(std::ostream& out,
std::size_t vertices, std::size_t vertices,
std::size_t halfedges, std::size_t halfedges,
std::size_t facets); std::size_t facets)
void write_footer() const { {
out() << "\n# End of Wavefront obj format #" << std::endl; m_out = &o;
} m_facets = facets;
void write_vertex( const double& x, const double& y, const double& z) {
out() << "v " << x << ' ' << y << ' ' << z << '\n'; // Print header.
} out() << "# file written from a CGAL tool in Wavefront obj format\n";
void write_facet_header() { out() << "# " << vertices << " vertices\n";
out() << "\n# " << m_facets << " facets\n"; out() << "# " << halfedges << " halfedges\n";
out() << "# ------------------------------------------\n\n"; out() << "# " << facets << " facets\n\n";
}
void write_facet_begin( std::size_t) { out() << "f "; } out() << "\n# " << vertices << " vertices\n";
void write_facet_vertex_index( std::size_t idx) { out() << ' ' << idx+1; } out() << "# ------------------------------------------\n\n";
void write_facet_end() { out() << '\n'; } }
void write_footer() const { out() << "\n# End of Wavefront obj format #" << std::endl; }
void write_vertex(const double& x, const double& y, const double& z) {
out() << "v " << x << ' ' << y << ' ' << z << '\n';
}
void write_facet_header()
{
out() << "\n# " << m_facets << " facets\n";
out() << "# ------------------------------------------\n\n";
}
void write_facet_begin(std::size_t) { out() << "f "; }
void write_facet_vertex_index(std::size_t idx) { out() << ' ' << idx+1; }
void write_facet_end() { out() << '\n'; }
}; };
} //namespace CGAL } // namespace CGAL
#ifdef CGAL_HEADER_ONLY #endif // CGAL_IO_OBJ_FILE_WRITER_WAVEFRONT_H
#include <CGAL/IO/OBJ/File_writer_wavefront_impl.h>
#endif // CGAL_HEADER_ONLY
#endif // CGAL_IO_OBJ_FILE_WRITER_WAVEFRONT_H //
// EOF //

View File

@ -16,34 +16,37 @@
#include <vector> #include <vector>
#include <limits> #include <limits>
namespace CGAL { namespace CGAL {
//Points_3 a RandomAccessContainer of Point_3, // Points_3 a RandomAccessContainer of Point_3,
//Faces a RandomAccessContainer of RandomAccessContainer of std::size_t // Faces a RandomAccessContainer of RandomAccessContainer of std::size_t
template <class Points_3, class Faces> template <class Points_3, class Faces>
bool bool read_OBJ(std::istream& input,
read_OBJ( std::istream& input, Points_3 &points,
Points_3 &points, Faces &faces)
Faces &faces)
{ {
typedef typename Points_3::value_type Point_3; typedef typename Points_3::value_type Point_3;
int mini(1), int mini(1),
maxi(-INT_MAX); maxi(-INT_MAX);
Point_3 p; Point_3 p;
std::string line; std::string line;
while(getline(input, line)) {
if(line[0] == 'v' && line[1] == ' ') { while(getline(input, line))
{
if(line[0] == 'v' && line[1] == ' ')
{
std::istringstream iss(line.substr(1)); std::istringstream iss(line.substr(1));
iss >> p; iss >> p;
if(!iss) if(!iss)
return false; return false;
points.push_back(p); points.push_back(p);
} }
else if(line[0] == 'f') { else if(line[0] == 'f')
{
std::istringstream iss(line.substr(1)); std::istringstream iss(line.substr(1));
int i; int i;
faces.push_back( std::vector<std::size_t>() ); faces.push_back(std::vector<std::size_t>());
while(iss >> i) while(iss >> i)
{ {
if(i < 1) if(i < 1)
@ -52,13 +55,15 @@ read_OBJ( std::istream& input,
if(i<mini) if(i<mini)
mini=i; mini=i;
} }
else { else
{
faces.back().push_back(i-1); faces.back().push_back(i-1);
if(i-1 > maxi) if(i-1 > maxi)
maxi = i-1; maxi = i-1;
} }
iss.ignore(256, ' '); iss.ignore(256, ' ');
} }
if(!iss.good() && !iss.eof()) if(!iss.good() && !iss.eof())
return false; return false;
} }
@ -68,10 +73,13 @@ read_OBJ( std::istream& input,
continue; continue;
} }
} }
if(maxi > points.size() || mini < -static_cast<int>(points.size())){
if(maxi > points.size() || mini < -static_cast<int>(points.size()))
{
std::cerr<<"a face index is invalid "<<std::endl; std::cerr<<"a face index is invalid "<<std::endl;
return false; return false;
} }
return true; return true;
} }

View File

@ -24,17 +24,17 @@
namespace CGAL { namespace CGAL {
template <class Point_3, class Polygon_3> template <class Point_3, class Polygon_3>
bool bool read_OFF(std::istream& in,
read_OFF( std::istream& in, std::vector< Point_3 >& points,
std::vector< Point_3 >& points, std::vector< Polygon_3 >& polygons,
std::vector< Polygon_3 >& polygons, bool /* verbose */ = false)
bool /* verbose */ = false)
{ {
CGAL::File_scanner_OFF scanner(in); CGAL::File_scanner_OFF scanner(in);
points.resize(scanner.size_of_vertices()); points.resize(scanner.size_of_vertices());
polygons.resize(scanner.size_of_facets()); polygons.resize(scanner.size_of_facets());
for (std::size_t i = 0; i < scanner.size_of_vertices(); ++i) { for(std::size_t i = 0; i < scanner.size_of_vertices(); ++i)
{
double x, y, z, w; double x, y, z, w;
scanner.scan_vertex( x, y, z, w); scanner.scan_vertex( x, y, z, w);
CGAL_assertion(w!=0); CGAL_assertion(w!=0);
@ -45,19 +45,20 @@ read_OFF( std::istream& in,
if(!in) if(!in)
return false; return false;
for (std::size_t i = 0; i < scanner.size_of_facets(); ++i) { for(std::size_t i = 0; i < scanner.size_of_facets(); ++i)
{
std::size_t no; std::size_t no;
scanner.scan_facet( no, i); scanner.scan_facet( no, i);
IO::internal::resize(polygons[i], no); IO::internal::resize(polygons[i], no);
for(std::size_t j = 0; j < no; ++j) { for(std::size_t j = 0; j < no; ++j)
{
std::size_t id; std::size_t id;
scanner.scan_facet_vertex_index(id, i); scanner.scan_facet_vertex_index(id, i);
if(id < scanner.size_of_vertices()) { if(id < scanner.size_of_vertices())
polygons[i][j] = id; polygons[i][j] = id;
} else { else
return false; return false;
}
} }
} }
@ -65,13 +66,12 @@ read_OFF( std::istream& in,
} }
template <class Point_3, class Polygon_3, class Color_rgb > template <class Point_3, class Polygon_3, class Color_rgb >
bool bool read_OFF(std::istream& in,
read_OFF( std::istream& in, std::vector< Point_3 >& points,
std::vector< Point_3 >& points, std::vector< Polygon_3 >& polygons,
std::vector< Polygon_3 >& polygons, std::vector<Color_rgb>& fcolors,
std::vector<Color_rgb>& fcolors, std::vector<Color_rgb>& vcolors,
std::vector<Color_rgb>& vcolors, bool /* verbose */ = false)
bool /* verbose */ = false)
{ {
CGAL::File_scanner_OFF scanner(in); CGAL::File_scanner_OFF scanner(in);
@ -81,16 +81,20 @@ read_OFF( std::istream& in,
if(scanner.has_colors()) if(scanner.has_colors())
vcolors.resize(scanner.size_of_vertices()); vcolors.resize(scanner.size_of_vertices());
for (std::size_t i = 0; i < scanner.size_of_vertices(); ++i) { for(std::size_t i = 0; i < scanner.size_of_vertices(); ++i)
{
double x, y, z, w; double x, y, z, w;
scanner.scan_vertex( x, y, z, w); scanner.scan_vertex( x, y, z, w);
CGAL_assertion(w!=0); CGAL_assertion(w!=0);
IO::internal::fill_point( x/w, y/w, z/w, points[i] ); IO::internal::fill_point( x/w, y/w, z/w, points[i] );
if(scanner.has_colors()) { if(scanner.has_colors())
{
unsigned char r=0, g=0, b=0; unsigned char r=0, g=0, b=0;
scanner.scan_color( r, g, b); scanner.scan_color( r, g, b);
vcolors[i] = Color_rgb(r,g,b); vcolors[i] = Color_rgb(r,g,b);
} else { }
else
{
scanner.skip_to_next_vertex(i); scanner.skip_to_next_vertex(i);
} }
@ -99,7 +103,7 @@ read_OFF( std::istream& in,
} }
bool has_fcolors = false; bool has_fcolors = false;
for (std::size_t i = 0; i < scanner.size_of_facets(); ++i) for(std::size_t i = 0; i < scanner.size_of_facets(); ++i)
{ {
std::size_t no; std::size_t no;
scanner.scan_facet( no, i); scanner.scan_facet( no, i);
@ -108,30 +112,33 @@ read_OFF( std::istream& in,
return false; return false;
IO::internal::resize(polygons[i], no); IO::internal::resize(polygons[i], no);
for(std::size_t j = 0; j < no; ++j) { for(std::size_t j = 0; j < no; ++j)
{
std::size_t id; std::size_t id;
scanner.scan_facet_vertex_index(id, i); scanner.scan_facet_vertex_index(id, i);
if(id < scanner.size_of_vertices()) { if(id < scanner.size_of_vertices())
polygons[i][j] = id; polygons[i][j] = id;
} else { else
return false; return false;
}
} }
if(i==0) if(i == 0)
{ {
std::string col; std::string col;
std::getline(in, col); std::getline(in, col);
std::istringstream iss(col); std::istringstream iss(col);
char ci =' '; char ci =' ';
if(iss >> ci) { if(iss >> ci)
{
has_fcolors = true; has_fcolors = true;
fcolors.resize(scanner.size_of_facets()); fcolors.resize(scanner.size_of_facets());
std::istringstream iss2(col); std::istringstream iss2(col);
fcolors[i] = scanner.get_color_from_line(iss2); fcolors[i] = scanner.get_color_from_line(iss2);
} }
} else if(has_fcolors) { }
else if(has_fcolors)
{
unsigned char r=0, g=0, b=0; unsigned char r=0, g=0, b=0;
scanner.scan_color(r,g,b); scanner.scan_color(r,g,b);
fcolors[i] = Color_rgb(r,g,b); fcolors[i] = Color_rgb(r,g,b);
@ -142,37 +149,33 @@ read_OFF( std::istream& in,
} }
template <class Point_3, class Polygon_3> template <class Point_3, class Polygon_3>
bool bool write_OFF(std::ostream& out,
write_OFF(std::ostream& out, std::vector< Point_3 >& points,
std::vector< Point_3 >& points, std::vector< Polygon_3 >& polygons)
std::vector< Polygon_3 >& polygons)
{ {
CGAL::File_writer_OFF writer; CGAL::File_writer_OFF writer;
writer.write_header(out, writer.write_header(out, points.size(), 0, polygons.size());
points.size(),
0, for(std::size_t i = 0, end = points.size(); i < end; ++i)
polygons.size());
for(std::size_t i = 0, end = points.size();
i < end; ++i)
{ {
const Point_3& p = points[i]; const Point_3& p = points[i];
writer.write_vertex( p.x(), p.y(), p.z() ); writer.write_vertex( p.x(), p.y(), p.z() );
} }
writer.write_facet_header(); writer.write_facet_header();
for(std::size_t i = 0, end = polygons.size(); for(std::size_t i=0, end=polygons.size(); i<end; ++i)
i < end; ++i)
{ {
Polygon_3& polygon = polygons[i]; Polygon_3& polygon = polygons[i];
const std::size_t size = polygon.size(); const std::size_t size = polygon.size();
writer.write_facet_begin(size); writer.write_facet_begin(size);
for(std::size_t j = 0; j < size; ++j) { for(std::size_t j=0; j<size; ++j)
writer.write_facet_vertex_index(polygon[j]); writer.write_facet_vertex_index(polygon[j]);
}
writer.write_facet_end(); writer.write_facet_end();
} }
writer.write_footer(); writer.write_footer();
return (bool) out; return out.good();
} }
} // namespace CGAL } // namespace CGAL

View File

@ -18,36 +18,62 @@
#define CGAL_IO_FILE_WRITER_INVENTOR_H 1 #define CGAL_IO_FILE_WRITER_INVENTOR_H 1
#include <CGAL/basic.h> #include <CGAL/basic.h>
#include <iostream> #include <iostream>
#include <cstddef> #include <cstddef>
namespace CGAL { namespace CGAL {
class CGAL_EXPORT File_writer_inventor { class CGAL_EXPORT File_writer_inventor
std::ostream* m_out; {
std::size_t m_facets; std::ostream* m_out;
std::size_t m_facets;
public: public:
File_writer_inventor() {} File_writer_inventor() {}
std::ostream& out() const { return *m_out; } std::ostream& out() const { return *m_out; }
void write_header( std::ostream& o,
std::size_t vertices, void write_header(std::ostream& o,
std::size_t halfedges, std::size_t vertices,
std::size_t facets); std::size_t halfedges,
void write_footer() const; std::size_t facets)
void write_vertex( const double& x, const double& y, const double& z) { {
out() << " " << x << ' ' << y << ' ' << z << ',' <<'\n'; m_out = &o;
} m_facets = facets;
void write_facet_header() const;
void write_facet_begin( std::size_t) { out() << " "; } out() << "# " << vertices << " vertices\n";
void write_facet_vertex_index( std::size_t idx) { out() << idx << ',';} out() << "# " << halfedges << " halfedges\n";
void write_facet_end() { out() << "-1,\n"; } out() << "# " << facets << " facets\n\n";
out() << "Separator {\n"
" Coordinate3 {\n"
" point [" << std::endl;
}
void write_footer() const
{
out() << " ] #coordIndex\n"
" } #IndexedFaceSet\n"
"} #Separator" << std::endl;
}
void write_vertex( const double& x, const double& y, const double& z) {
out() << " " << x << ' ' << y << ' ' << z << ',' <<'\n';
}
void write_facet_header() const
{
out() << " ] #point\n"
" } #Coordinate3\n"
" # " << m_facets << " facets\n"
" IndexedFaceSet {\n"
" coordIndex [\n";
}
void write_facet_begin( std::size_t) { out() << " "; }
void write_facet_vertex_index( std::size_t idx) { out() << idx << ','; }
void write_facet_end() { out() << "-1,\n"; }
}; };
} //namespace CGAL } //namespace CGAL
#ifdef CGAL_HEADER_ONLY #endif // CGAL_IO_FILE_WRITER_INVENTOR_H
#include <CGAL/IO/File_writer_inventor_impl.h>
#endif // CGAL_HEADER_ONLY
#endif // CGAL_IO_FILE_WRITER_INVENTOR_H //
// EOF //

View File

@ -13,327 +13,324 @@
#include <CGAL/IO/PLY.h> #include <CGAL/IO/PLY.h>
namespace CGAL{ namespace CGAL {
namespace internal {
namespace internal template <typename Integer, class Polygon_3, class Color_rgb>
{ bool read_PLY_faces(std::istream& in,
template <typename Integer, class Polygon_3, class Color_rgb>
bool
read_PLY_faces (std::istream& in,
internal::PLY::PLY_element& element, internal::PLY::PLY_element& element,
std::vector< Polygon_3 >& polygons, std::vector< Polygon_3 >& polygons,
std::vector< Color_rgb >& fcolors, std::vector< Color_rgb >& fcolors,
const char* vertex_indices_tag) const char* vertex_indices_tag)
{
bool has_colors = false;
std::string rtag = "r", gtag = "g", btag = "b";
if((element.has_property<boost::uint8_t>("red") || element.has_property<boost::uint8_t>("r")) &&
(element.has_property<boost::uint8_t>("green") || element.has_property<boost::uint8_t>("g")) &&
(element.has_property<boost::uint8_t>("blue") || element.has_property<boost::uint8_t>("b")))
{
has_colors = true;
if(element.has_property<boost::uint8_t>("red"))
rtag = "red"; gtag = "green"; btag = "blue";
}
for(std::size_t j = 0; j < element.number_of_items(); ++ j)
{
for(std::size_t k = 0; k < element.number_of_properties(); ++ k)
{
internal::PLY::PLY_read_number* property = element.property(k);
property->get(in);
if(in.fail())
return false;
}
std::tuple<std::vector<Integer>, boost::uint8_t, boost::uint8_t, boost::uint8_t> new_face;
if(has_colors)
{
PLY::process_properties(element, new_face,
std::make_pair(CGAL::make_nth_of_tuple_property_map<0>(new_face),
PLY_property<std::vector<Integer> >(vertex_indices_tag)),
std::make_pair(CGAL::make_nth_of_tuple_property_map<1>(new_face),
PLY_property<boost::uint8_t>(rtag.c_str())),
std::make_pair(CGAL::make_nth_of_tuple_property_map<2>(new_face),
PLY_property<boost::uint8_t>(gtag.c_str())),
std::make_pair(CGAL::make_nth_of_tuple_property_map<3>(new_face),
PLY_property<boost::uint8_t>(btag.c_str())));
fcolors.push_back(Color_rgb(get<1>(new_face), get<2>(new_face), get<3>(new_face)));
}
else
{
PLY::process_properties(element, new_face,
std::make_pair(CGAL::make_nth_of_tuple_property_map<0>(new_face),
PLY_property<std::vector<Integer> >(vertex_indices_tag)));
}
polygons.push_back(Polygon_3(get<0>(new_face).size()));
for(std::size_t i = 0; i < get<0>(new_face).size(); ++ i)
polygons.back()[i] = std::size_t(get<0>(new_face)[i]);
}
return true;
}
} // namespace internal
template <class Point_3, class Polygon_3>
bool
read_PLY(std::istream& in,
std::vector< Point_3 >& points,
std::vector< Polygon_3 >& polygons,
bool /* verbose */ = false)
{
if(!in)
{
std::cerr << "Error: cannot open file" << std::endl;
return false;
}
internal::PLY::PLY_reader reader;
if(!(reader.init(in)))
{
in.setstate(std::ios::failbit);
return false;
}
for(std::size_t i = 0; i < reader.number_of_elements(); ++ i)
{
internal::PLY::PLY_element& element = reader.element(i);
if(element.name() == "vertex" || element.name() == "vertices")
{
for(std::size_t j = 0; j < element.number_of_items(); ++ j)
{
for(std::size_t k = 0; k < element.number_of_properties(); ++ k)
{
internal::PLY::PLY_read_number* property = element.property(k);
property->get(in);
if(in.fail())
return false;
}
Point_3 new_vertex;
internal::PLY::process_properties(element, new_vertex,
make_ply_point_reader(CGAL::Identity_property_map<Point_3>()));
points.push_back(get<0>(new_vertex));
}
}
else if(element.name() == "face" || element.name() == "faces")
{
std::vector<CGAL::Color> dummy;
if(element.has_property<std::vector<boost::int32_t> >("vertex_indices"))
internal::read_PLY_faces<boost::int32_t>(in, element, polygons, dummy, "vertex_indices");
else if(element.has_property<std::vector<boost::uint32_t> >("vertex_indices"))
internal::read_PLY_faces<boost::uint32_t>(in, element, polygons, dummy, "vertex_indices");
else if(element.has_property<std::vector<boost::int32_t> >("vertex_index"))
internal::read_PLY_faces<boost::int32_t>(in, element, polygons, dummy, "vertex_index");
else if(element.has_property<std::vector<boost::uint32_t> >("vertex_index"))
internal::read_PLY_faces<boost::uint32_t>(in, element, polygons, dummy, "vertex_index");
else
{
std::cerr << "Error: can't find vertex indices in PLY input" << std::endl;
return false;
}
}
else // Read other elements and ignore
{
for(std::size_t j = 0; j < element.number_of_items(); ++ j)
{
for(std::size_t k = 0; k < element.number_of_properties(); ++ k)
{
internal::PLY::PLY_read_number* property = element.property(k);
property->get(in);
if(in.fail())
return false;
}
}
}
}
return !in.bad();
}
template <class Point_3, class Polygon_3, class Color_rgb>
bool read_PLY(std::istream& in,
std::vector< Point_3 >& points,
std::vector< Polygon_3 >& polygons,
std::vector<std::pair<unsigned int, unsigned int> >& hedges,
std::vector<Color_rgb>& fcolors,
std::vector<Color_rgb>& vcolors,
std::vector<std::pair<float, float> >& huvs,
bool /* verbose */ = false)
{
if(!in)
{
std::cerr << "Error: cannot open file" << std::endl;
return false;
}
internal::PLY::PLY_reader reader;
if(!(reader.init(in)))
{
in.setstate(std::ios::failbit);
return false;
}
for(std::size_t i = 0; i < reader.number_of_elements(); ++ i)
{
internal::PLY::PLY_element& element = reader.element(i);
if(element.name() == "vertex" || element.name() == "vertices")
{ {
bool has_colors = false; bool has_colors = false;
std::string rtag = "r", gtag = "g", btag = "b"; std::string rtag = "r", gtag = "g", btag = "b";
if ((element.has_property<boost::uint8_t>("red") || element.has_property<boost::uint8_t>("r")) && if((element.has_property<boost::uint8_t>("red") || element.has_property<boost::uint8_t>("r")) &&
(element.has_property<boost::uint8_t>("green") || element.has_property<boost::uint8_t>("g")) && (element.has_property<boost::uint8_t>("green") || element.has_property<boost::uint8_t>("g")) &&
(element.has_property<boost::uint8_t>("blue") || element.has_property<boost::uint8_t>("b"))) (element.has_property<boost::uint8_t>("blue") || element.has_property<boost::uint8_t>("b")))
{ {
has_colors = true; has_colors = true;
if (element.has_property<boost::uint8_t>("red")) if(element.has_property<boost::uint8_t>("red"))
{ {
rtag = "red"; gtag = "green"; btag = "blue"; rtag = "red"; gtag = "green"; btag = "blue";
} }
} }
for (std::size_t j = 0; j < element.number_of_items(); ++ j) for(std::size_t j = 0; j < element.number_of_items(); ++ j)
{ {
for (std::size_t k = 0; k < element.number_of_properties(); ++ k) for(std::size_t k = 0; k < element.number_of_properties(); ++ k)
{ {
internal::PLY::PLY_read_number* property = element.property(k); internal::PLY::PLY_read_number* property = element.property(k);
property->get (in); property->get(in);
if (in.fail()) if(in.fail())
return false; return false;
} }
std::tuple<std::vector<Integer>, boost::uint8_t, boost::uint8_t, boost::uint8_t> new_face; std::tuple<Point_3, boost::uint8_t, boost::uint8_t, boost::uint8_t> new_vertex;
if (has_colors) if(has_colors)
{ {
PLY::process_properties (element, new_face, internal::PLY::process_properties(element, new_vertex,
std::make_pair (CGAL::make_nth_of_tuple_property_map<0>(new_face), make_ply_point_reader(CGAL::make_nth_of_tuple_property_map<0>(new_vertex)),
PLY_property<std::vector<Integer> >(vertex_indices_tag)), std::make_pair(CGAL::make_nth_of_tuple_property_map<1>(new_vertex),
std::make_pair (CGAL::make_nth_of_tuple_property_map<1>(new_face), PLY_property<boost::uint8_t>(rtag.c_str())),
PLY_property<boost::uint8_t>(rtag.c_str())), std::make_pair(CGAL::make_nth_of_tuple_property_map<2>(new_vertex),
std::make_pair (CGAL::make_nth_of_tuple_property_map<2>(new_face), PLY_property<boost::uint8_t>(gtag.c_str())),
PLY_property<boost::uint8_t>(gtag.c_str())), std::make_pair(CGAL::make_nth_of_tuple_property_map<3>(new_vertex),
std::make_pair (CGAL::make_nth_of_tuple_property_map<3>(new_face), PLY_property<boost::uint8_t>(btag.c_str())));
PLY_property<boost::uint8_t>(btag.c_str())));
fcolors.push_back (Color_rgb (get<1>(new_face), get<2>(new_face), get<3>(new_face))); vcolors.push_back(Color_rgb(get<1>(new_vertex), get<2>(new_vertex), get<3>(new_vertex)));
} }
else else
PLY::process_properties (element, new_face, internal::PLY::process_properties(element, new_vertex,
std::make_pair (CGAL::make_nth_of_tuple_property_map<0>(new_face), make_ply_point_reader(CGAL::make_nth_of_tuple_property_map<0>(new_vertex)));
PLY_property<std::vector<Integer> >(vertex_indices_tag)));
polygons.push_back (Polygon_3(get<0>(new_face).size())); points.push_back(get<0>(new_vertex));
for (std::size_t i = 0; i < get<0>(new_face).size(); ++ i)
polygons.back()[i] = std::size_t(get<0>(new_face)[i]);
} }
return true;
} }
else if(element.name() == "face" || element.name() == "faces")
}
template <class Point_3, class Polygon_3>
bool
read_PLY( std::istream& in,
std::vector< Point_3 >& points,
std::vector< Polygon_3 >& polygons,
bool /* verbose */ = false)
{
if(!in)
{ {
std::cerr << "Error: cannot open file" << std::endl; if(element.has_property<std::vector<boost::int32_t> >("vertex_indices"))
return false; internal::read_PLY_faces<boost::int32_t>(in, element, polygons, fcolors, "vertex_indices");
} else if(element.has_property<std::vector<boost::uint32_t> >("vertex_indices"))
internal::read_PLY_faces<boost::uint32_t>(in, element, polygons, fcolors, "vertex_indices");
internal::PLY::PLY_reader reader; else if(element.has_property<std::vector<boost::int32_t> >("vertex_index"))
internal::read_PLY_faces<boost::int32_t>(in, element, polygons, fcolors, "vertex_index");
if (!(reader.init (in))) else if(element.has_property<std::vector<boost::uint32_t> >("vertex_index"))
{ internal::read_PLY_faces<boost::uint32_t>(in, element, polygons, fcolors, "vertex_index");
in.setstate(std::ios::failbit); else
return false;
}
for (std::size_t i = 0; i < reader.number_of_elements(); ++ i)
{
internal::PLY::PLY_element& element = reader.element(i);
if (element.name() == "vertex" || element.name() == "vertices")
{ {
for (std::size_t j = 0; j < element.number_of_items(); ++ j) std::cerr << "Error: can't find vertex indices in PLY input" << std::endl;
return false;
}
}
else if(element.name() == "halfedge" )
{
bool has_uv = false;
std::string stag = "source", ttag = "target", utag = "u", vtag = "v";
if( element.has_property<unsigned int>("source") &&
element.has_property<unsigned int>("target") &&
element.has_property<float>("u") &&
element.has_property<float>("v"))
{
has_uv = true;
}
cpp11::tuple<unsigned int, unsigned int, float, float, float> new_hedge;
for(std::size_t j = 0; j < element.number_of_items(); ++ j)
{
for(std::size_t k = 0; k < element.number_of_properties(); ++ k)
{ {
for (std::size_t k = 0; k < element.number_of_properties(); ++ k) internal::PLY::PLY_read_number* property = element.property(k);
{ property->get(in);
internal::PLY::PLY_read_number* property = element.property(k);
property->get (in);
if (in.fail()) if(in.eof())
return false; return false;
}
Point_3 new_vertex;
internal::PLY::process_properties (element, new_vertex,
make_ply_point_reader (CGAL::Identity_property_map<Point_3>()));
points.push_back (get<0>(new_vertex));
} }
}
else if (element.name() == "face" || element.name() == "faces")
{
std::vector<CGAL::Color> dummy;
if (element.has_property<std::vector<boost::int32_t> > ("vertex_indices")) if(has_uv)
internal::read_PLY_faces<boost::int32_t> (in, element, polygons, dummy, "vertex_indices"); {
else if (element.has_property<std::vector<boost::uint32_t> > ("vertex_indices")) internal::PLY::process_properties(element, new_hedge,
internal::read_PLY_faces<boost::uint32_t> (in, element, polygons, dummy, "vertex_indices"); std::make_pair(CGAL::make_nth_of_tuple_property_map<0>(new_hedge),
else if (element.has_property<std::vector<boost::int32_t> > ("vertex_index")) PLY_property<unsigned int>(stag.c_str())),
internal::read_PLY_faces<boost::int32_t> (in, element, polygons, dummy, "vertex_index"); std::make_pair(CGAL::make_nth_of_tuple_property_map<1>(new_hedge),
else if (element.has_property<std::vector<boost::uint32_t> > ("vertex_index")) PLY_property<unsigned int>(ttag.c_str())),
internal::read_PLY_faces<boost::uint32_t> (in, element, polygons, dummy, "vertex_index"); std::make_pair(CGAL::make_nth_of_tuple_property_map<2>(new_hedge),
PLY_property<float>(utag.c_str())),
std::make_pair(CGAL::make_nth_of_tuple_property_map<3>(new_hedge),
PLY_property<float>(vtag.c_str())));
hedges.push_back(std::make_pair(get<0>(new_hedge), get<1>(new_hedge)));
huvs.push_back(std::make_pair(get<2>(new_hedge), get<3>(new_hedge)));
}
else else
{ {
std::cerr << "Error: can't find vertex indices in PLY input" << std::endl; internal::PLY::process_properties(element, new_hedge,
return false; std::make_pair(CGAL::make_nth_of_tuple_property_map<0>(new_hedge),
} PLY_property<unsigned int>(stag.c_str())),
} std::make_pair(CGAL::make_nth_of_tuple_property_map<1>(new_hedge),
else // Read other elements and ignore PLY_property<unsigned int>(ttag.c_str()))
{ );
for (std::size_t j = 0; j < element.number_of_items(); ++ j)
{
for (std::size_t k = 0; k < element.number_of_properties(); ++ k)
{
internal::PLY::PLY_read_number* property = element.property(k);
property->get (in);
if (in.fail())
return false;
}
} }
} }
} }
else // Read other elements and ignore
return !in.bad();
}
template <class Point_3, class Polygon_3, class Color_rgb>
bool
read_PLY( std::istream& in,
std::vector< Point_3 >& points,
std::vector< Polygon_3 >& polygons,
std::vector<std::pair<unsigned int, unsigned int> >& hedges,
std::vector<Color_rgb>& fcolors,
std::vector<Color_rgb>& vcolors,
std::vector<std::pair<float, float> >& huvs,
bool /* verbose */ = false)
{
if(!in)
{ {
std::cerr << "Error: cannot open file" << std::endl; for(std::size_t j = 0; j < element.number_of_items(); ++ j)
return false;
}
internal::PLY::PLY_reader reader;
if (!(reader.init (in)))
{
in.setstate(std::ios::failbit);
return false;
}
for (std::size_t i = 0; i < reader.number_of_elements(); ++ i)
{
internal::PLY::PLY_element& element = reader.element(i);
if (element.name() == "vertex" || element.name() == "vertices")
{ {
bool has_colors = false; for(std::size_t k = 0; k < element.number_of_properties(); ++ k)
std::string rtag = "r", gtag = "g", btag = "b";
if ((element.has_property<boost::uint8_t>("red") || element.has_property<boost::uint8_t>("r")) &&
(element.has_property<boost::uint8_t>("green") || element.has_property<boost::uint8_t>("g")) &&
(element.has_property<boost::uint8_t>("blue") || element.has_property<boost::uint8_t>("b")))
{ {
has_colors = true; internal::PLY::PLY_read_number* property = element.property(k);
if (element.has_property<boost::uint8_t>("red")) property->get(in);
{ if(in.fail())
rtag = "red"; gtag = "green"; btag = "blue"; return false;
}
}
for (std::size_t j = 0; j < element.number_of_items(); ++ j)
{
for (std::size_t k = 0; k < element.number_of_properties(); ++ k)
{
internal::PLY::PLY_read_number* property = element.property(k);
property->get (in);
if (in.fail())
return false;
}
std::tuple<Point_3, boost::uint8_t, boost::uint8_t, boost::uint8_t> new_vertex;
if (has_colors)
{
internal::PLY::process_properties (element, new_vertex,
make_ply_point_reader (CGAL::make_nth_of_tuple_property_map<0>(new_vertex)),
std::make_pair (CGAL::make_nth_of_tuple_property_map<1>(new_vertex),
PLY_property<boost::uint8_t>(rtag.c_str())),
std::make_pair (CGAL::make_nth_of_tuple_property_map<2>(new_vertex),
PLY_property<boost::uint8_t>(gtag.c_str())),
std::make_pair (CGAL::make_nth_of_tuple_property_map<3>(new_vertex),
PLY_property<boost::uint8_t>(btag.c_str())));
vcolors.push_back (Color_rgb (get<1>(new_vertex), get<2>(new_vertex), get<3>(new_vertex)));
}
else
internal::PLY::process_properties (element, new_vertex,
make_ply_point_reader (CGAL::make_nth_of_tuple_property_map<0>(new_vertex)));
points.push_back (get<0>(new_vertex));
}
}
else if (element.name() == "face" || element.name() == "faces")
{
if (element.has_property<std::vector<boost::int32_t> > ("vertex_indices"))
internal::read_PLY_faces<boost::int32_t> (in, element, polygons, fcolors, "vertex_indices");
else if (element.has_property<std::vector<boost::uint32_t> > ("vertex_indices"))
internal::read_PLY_faces<boost::uint32_t> (in, element, polygons, fcolors, "vertex_indices");
else if (element.has_property<std::vector<boost::int32_t> > ("vertex_index"))
internal::read_PLY_faces<boost::int32_t> (in, element, polygons, fcolors, "vertex_index");
else if (element.has_property<std::vector<boost::uint32_t> > ("vertex_index"))
internal::read_PLY_faces<boost::uint32_t> (in, element, polygons, fcolors, "vertex_index");
else
{
std::cerr << "Error: can't find vertex indices in PLY input" << std::endl;
return false;
}
}
else if(element.name() == "halfedge" )
{
bool has_uv = false;
std::string stag = "source", ttag = "target", utag = "u", vtag = "v";
if ( element.has_property<unsigned int>("source") &&
element.has_property<unsigned int>("target") &&
element.has_property<float>("u") &&
element.has_property<float>("v"))
{
has_uv = true;
}
cpp11::tuple<unsigned int, unsigned int, float, float, float> new_hedge;
for (std::size_t j = 0; j < element.number_of_items(); ++ j)
{
for (std::size_t k = 0; k < element.number_of_properties(); ++ k)
{
internal::PLY::PLY_read_number* property = element.property(k);
property->get (in);
if (in.eof())
return false;
}
if (has_uv)
{
internal::PLY::process_properties (element, new_hedge,
std::make_pair (CGAL::make_nth_of_tuple_property_map<0>(new_hedge),
PLY_property<unsigned int>(stag.c_str())),
std::make_pair (CGAL::make_nth_of_tuple_property_map<1>(new_hedge),
PLY_property<unsigned int>(ttag.c_str())),
std::make_pair (CGAL::make_nth_of_tuple_property_map<2>(new_hedge),
PLY_property<float>(utag.c_str())),
std::make_pair (CGAL::make_nth_of_tuple_property_map<3>(new_hedge),
PLY_property<float>(vtag.c_str())));
hedges.push_back (std::make_pair(get<0>(new_hedge), get<1>(new_hedge)));
huvs.push_back (std::make_pair(get<2>(new_hedge), get<3>(new_hedge)));
}
else
internal::PLY::process_properties (element, new_hedge,
std::make_pair(CGAL::make_nth_of_tuple_property_map<0>(new_hedge),
PLY_property<unsigned int>(stag.c_str())),
std::make_pair(CGAL::make_nth_of_tuple_property_map<1>(new_hedge),
PLY_property<unsigned int>(ttag.c_str()))
);
}
}
else // Read other elements and ignore
{
for (std::size_t j = 0; j < element.number_of_items(); ++ j)
{
for (std::size_t k = 0; k < element.number_of_properties(); ++ k)
{
internal::PLY::PLY_read_number* property = element.property(k);
property->get (in);
if (in.fail())
return false;
}
} }
} }
} }
return !in.bad();
} }
return !in.bad();
}
template <class Point_3, class Polygon_3, class Color_rgb>
bool read_PLY(std::istream& in,
std::vector< Point_3 >& points,
std::vector< Polygon_3 >& polygons,
std::vector<Color_rgb>& fcolors,
std::vector<Color_rgb>& vcolors,
bool /* verbose */ = false)
{
std::vector<std::pair<unsigned int, unsigned int> > dummy_pui;
std::vector<std::pair<float, float> > dummy_pf;
return read_PLY<Point_3, Polygon_3, Color_rgb>(in, points, polygons, dummy_pui, fcolors, vcolors, dummy_pf);
}
template <class Point_3, class Polygon_3, class Color_rgb>
bool
read_PLY( std::istream& in,
std::vector< Point_3 >& points,
std::vector< Polygon_3 >& polygons,
std::vector<Color_rgb>& fcolors,
std::vector<Color_rgb>& vcolors,
bool /* verbose */ = false)
{
std::vector<std::pair<unsigned int, unsigned int> > dummy_pui;
std::vector<std::pair<float, float> > dummy_pf;
return read_PLY<Point_3, Polygon_3, Color_rgb>(in, points, polygons,
dummy_pui,
fcolors, vcolors,
dummy_pf);
}
} // namespace CGAL } // namespace CGAL
#endif // CGAL_IO_PLY_PLY_READER_H #endif // CGAL_IO_PLY_PLY_READER_H

View File

@ -13,148 +13,144 @@
#include <CGAL/IO/PLY.h> #include <CGAL/IO/PLY.h>
namespace CGAL{ namespace CGAL {
template <class Point_3, class Polygon_3> template <class Point_3, class Polygon_3>
bool bool write_PLY(std::ostream& out,
write_PLY(std::ostream& out, const std::vector< Point_3 >& points,
const std::vector< Point_3 >& points, const std::vector< Polygon_3 >& polygons,
const std::vector< Polygon_3 >& polygons, bool /* verbose */ = false)
bool /* verbose */ = false) {
if(!out)
{ {
std::cerr << "Error: cannot open file" << std::endl;
if(!out) return false;
{
std::cerr << "Error: cannot open file" << std::endl;
return false;
}
// Write header
out << "ply" << std::endl
<< ((get_mode(out) == IO::BINARY) ? "format binary_little_endian 1.0" : "format ascii 1.0") << std::endl
<< "comment Generated by the CGAL library" << std::endl
<< "element vertex " << points.size() << std::endl;
internal::PLY::output_property_header (out,
make_ply_point_writer (CGAL::Identity_property_map<Point_3>()));
out << "element face " << polygons.size() << std::endl;
internal::PLY::output_property_header (out,
std::make_pair (CGAL::Identity_property_map<Polygon_3>(),
PLY_property<std::vector<int> >("vertex_indices")));
out << "end_header" << std::endl;
for (std::size_t i = 0; i < points.size(); ++ i)
internal::PLY::output_properties (out, points.begin() + i,
make_ply_point_writer (CGAL::Identity_property_map<Point_3>()));
for (std::size_t i = 0; i < polygons.size(); ++ i)
internal::PLY::output_properties (out, polygons.begin() + i,
std::make_pair (CGAL::Identity_property_map<Polygon_3>(),
PLY_property<std::vector<int> >("vertex_indices")));
return out.good();
} }
template <class SurfaceMesh> // Write header
bool out << "ply" << std::endl
write_PLY(std::ostream& out, << ((get_mode(out) == IO::BINARY) ? "format binary_little_endian 1.0" : "format ascii 1.0") << std::endl
const SurfaceMesh& mesh, << "comment Generated by the CGAL library" << std::endl
bool /* verbose */ = false) << "element vertex " << points.size() << std::endl;
internal::PLY::output_property_header(out, make_ply_point_writer (CGAL::Identity_property_map<Point_3>()));
out << "element face " << polygons.size() << std::endl;
internal::PLY::output_property_header(out, std::make_pair(CGAL::Identity_property_map<Polygon_3>(),
PLY_property<std::vector<int> >("vertex_indices")));
out << "end_header" << std::endl;
for (std::size_t i = 0; i < points.size(); ++ i)
internal::PLY::output_properties(out, points.begin() + i,
make_ply_point_writer (CGAL::Identity_property_map<Point_3>()));
for (std::size_t i = 0; i < polygons.size(); ++ i)
internal::PLY::output_properties(out, polygons.begin() + i,
std::make_pair(CGAL::Identity_property_map<Polygon_3>(),
PLY_property<std::vector<int> >("vertex_indices")));
return out.good();
}
template <class SurfaceMesh>
bool write_PLY(std::ostream& out,
const SurfaceMesh& mesh,
bool /* verbose */ = false)
{
typedef typename boost::graph_traits<SurfaceMesh>::face_descriptor face_descriptor;
typedef typename boost::graph_traits<SurfaceMesh>::halfedge_descriptor halfedge_descriptor;
typedef typename boost::graph_traits<SurfaceMesh>::vertex_descriptor vertex_descriptor;
typedef typename boost::property_map<SurfaceMesh, boost::vertex_point_t>::type::value_type Point_3;
typedef typename SurfaceMesh::template Property_map<halfedge_descriptor,std::pair<float, float> > UV_map;
UV_map h_uv;
bool has_texture;
boost::tie(h_uv, has_texture) = mesh.template property_map<halfedge_descriptor,std::pair<float, float> >("h:uv");
if(!out)
{ {
typedef typename boost::graph_traits<SurfaceMesh>::face_descriptor face_descriptor; std::cerr << "Error: cannot open file" << std::endl;
typedef typename boost::graph_traits<SurfaceMesh>::halfedge_descriptor halfedge_descriptor; return false;
typedef typename boost::graph_traits<SurfaceMesh>::vertex_descriptor vertex_descriptor;
typedef typename boost::property_map<SurfaceMesh, boost::vertex_point_t>::type::value_type Point_3;
typedef typename SurfaceMesh::template Property_map<halfedge_descriptor,std::pair<float, float> > UV_map;
UV_map h_uv;
bool has_texture;
boost::tie(h_uv, has_texture) = mesh.template property_map<halfedge_descriptor,std::pair<float, float> >("h:uv");
if(!out)
{
std::cerr << "Error: cannot open file" << std::endl;
return false;
}
// Write header
out << "ply" << std::endl
<< ((get_mode(out) == IO::BINARY) ? "format binary_little_endian 1.0" : "format ascii 1.0") << std::endl
<< "comment Generated by the CGAL library" << std::endl
<< "element vertex " << num_vertices(mesh) << std::endl;
internal::PLY::output_property_header (out,
make_ply_point_writer (CGAL::Identity_property_map<Point_3>()));
out << "element face " << num_faces(mesh) << std::endl;
internal::PLY::output_property_header (out,
std::make_pair (CGAL::Identity_property_map<std::vector<std::size_t> >(),
PLY_property<std::vector<int> >("vertex_indices")));
if(has_texture)
{
out << "element halfedge " << num_halfedges(mesh) << std::endl;
internal::PLY::output_property_header (out,
std::make_pair (CGAL::Identity_property_map<std::size_t >(),
PLY_property<unsigned int >("source")));
internal::PLY::output_property_header (out,
std::make_pair (CGAL::Identity_property_map<std::size_t >(),
PLY_property<unsigned int >("target")));
internal::PLY::output_property_header (out,
std::make_tuple (h_uv,
PLY_property<float>("u"),
PLY_property<float>("v")));
}
out << "end_header" << std::endl;
for(vertex_descriptor vd : vertices(mesh))
{
Point_3 p = get(get(CGAL::vertex_point, mesh), vd);
internal::PLY::output_properties (out, &p,
make_ply_point_writer (CGAL::Identity_property_map<Point_3>()));
}
std::vector<std::size_t> polygon;
for(face_descriptor fd : faces(mesh))
{
polygon.clear();
for(halfedge_descriptor hd : halfedges_around_face(halfedge(fd, mesh), mesh))
polygon.push_back (get(get(boost::vertex_index, mesh), target(hd,mesh)));
internal::PLY::output_properties (out, &polygon,
std::make_pair (CGAL::Identity_property_map<std::vector<std::size_t> >(),
PLY_property<std::vector<int> >("vertex_indices")));
}
if(has_texture)
{
for(halfedge_descriptor hd : halfedges(mesh))
{
typedef std::tuple<unsigned int, unsigned int, float, float> Super_tuple;
Super_tuple t =
std::make_tuple(source(hd, mesh),target(hd, mesh),
h_uv[hd].first,
h_uv[hd].second);
internal::PLY::output_properties (out, &t,
std::make_pair (Nth_of_tuple_property_map<0,Super_tuple>(),
PLY_property<unsigned int >("source")),
std::make_pair (Nth_of_tuple_property_map<1,Super_tuple>(),
PLY_property<unsigned int >("target")),
std::make_pair (Nth_of_tuple_property_map<2,Super_tuple>(),
PLY_property<float>("u")),
std::make_pair (Nth_of_tuple_property_map<3,Super_tuple>(),
PLY_property<float>("v")));
}
}
return out.good();
} }
// Write header
out << "ply" << std::endl
<< ((get_mode(out) == IO::BINARY) ? "format binary_little_endian 1.0" : "format ascii 1.0") << std::endl
<< "comment Generated by the CGAL library" << std::endl
<< "element vertex " << num_vertices(mesh) << std::endl;
internal::PLY::output_property_header(out,
make_ply_point_writer (CGAL::Identity_property_map<Point_3>()));
out << "element face " << num_faces(mesh) << std::endl;
internal::PLY::output_property_header(out,
std::make_pair(CGAL::Identity_property_map<std::vector<std::size_t> >(),
PLY_property<std::vector<int> >("vertex_indices")));
if(has_texture)
{
out << "element halfedge " << num_halfedges(mesh) << std::endl;
internal::PLY::output_property_header(out,
std::make_pair(CGAL::Identity_property_map<std::size_t >(),
PLY_property<unsigned int >("source")));
internal::PLY::output_property_header(out,
std::make_pair(CGAL::Identity_property_map<std::size_t >(),
PLY_property<unsigned int >("target")));
internal::PLY::output_property_header(out,
std::make_tuple (h_uv,
PLY_property<float>("u"),
PLY_property<float>("v")));
}
out << "end_header" << std::endl;
for(vertex_descriptor vd : vertices(mesh))
{
Point_3 p = get(get(CGAL::vertex_point, mesh), vd);
internal::PLY::output_properties(out, &p,
make_ply_point_writer (CGAL::Identity_property_map<Point_3>()));
}
std::vector<std::size_t> polygon;
for(face_descriptor fd : faces(mesh))
{
polygon.clear();
for(halfedge_descriptor hd : halfedges_around_face(halfedge(fd, mesh), mesh))
polygon.push_back(get(get(boost::vertex_index, mesh), target(hd,mesh)));
internal::PLY::output_properties(out, &polygon,
std::make_pair(CGAL::Identity_property_map<std::vector<std::size_t> >(),
PLY_property<std::vector<int> >("vertex_indices")));
}
if(has_texture)
{
for(halfedge_descriptor hd : halfedges(mesh))
{
typedef std::tuple<unsigned int, unsigned int, float, float> Super_tuple;
Super_tuple t = std::make_tuple(source(hd, mesh),target(hd, mesh),
h_uv[hd].first,
h_uv[hd].second);
internal::PLY::output_properties(out, &t,
std::make_pair(Nth_of_tuple_property_map<0,Super_tuple>(),
PLY_property<unsigned int >("source")),
std::make_pair(Nth_of_tuple_property_map<1,Super_tuple>(),
PLY_property<unsigned int >("target")),
std::make_pair(Nth_of_tuple_property_map<2,Super_tuple>(),
PLY_property<float>("u")),
std::make_pair(Nth_of_tuple_property_map<3,Super_tuple>(),
PLY_property<float>("v")));
}
}
return out.good();
}
} // namespace CGAL } // namespace CGAL
#endif // CGAL_IO_PLY_PLY_WRITER_H #endif // CGAL_IO_PLY_PLY_WRITER_H

View File

@ -15,7 +15,7 @@
#include <CGAL/IO/io.h> #include <CGAL/IO/io.h>
#include <CGAL/IO/reader_helpers.h> #include <CGAL/IO/reader_helpers.h>
#include <boost/cstdint.hpp> #include <boost/cstdint.hpp>
#include <cctype> #include <cctype>
#include <iostream> #include <iostream>
@ -40,7 +40,7 @@ bool read_ASCII_facet(std::istream& input,
std::string s; std::string s;
std::string vertex("vertex"), std::string vertex("vertex"),
endfacet("endfacet"); endfacet("endfacet");
int count = 0; int count = 0;
double x,y,z; double x,y,z;
@ -133,9 +133,8 @@ bool parse_ASCII_STL(std::istream& input,
if(s == solid) if(s == solid)
{ {
if(in_solid) if(in_solid)
{
break; break;
}
in_solid = true; in_solid = true;
} }
if(s == facet) if(s == facet)
@ -149,12 +148,14 @@ bool parse_ASCII_STL(std::istream& input,
} }
} }
if(in_solid){ if(in_solid)
{
if(verbose) if(verbose)
std::cerr << "Error while parsing ASCII file" << std::endl; std::cerr << "Error while parsing ASCII file" << std::endl;
return false; return false;
} }
return true; return true;
} }
@ -331,9 +332,7 @@ bool read_STL(std::istream& input,
return true; // empty file return true; // empty file
// If the first word is not 'solid', the file must be binary // If the first word is not 'solid', the file must be binary
if(s != "solid" if(s != "solid" || (word[5] !='\n' && word[5] != ' '))
|| (word[5] !='\n'
&& word[5] != ' '))
{ {
if(parse_binary_STL(input, points, facets, verbose)) if(parse_binary_STL(input, points, facets, verbose))
{ {

View File

@ -16,21 +16,17 @@
#include <boost/cstdint.hpp> #include <boost/cstdint.hpp>
namespace CGAL {
namespace CGAL{
template <class PointRange, class TriangleRange> template <class PointRange, class TriangleRange>
std::ostream& std::ostream& write_STL(const PointRange& points,
write_STL(const PointRange& points, const TriangleRange& facets,
const TriangleRange& facets, std::ostream& out)
std::ostream& out)
{ {
typedef typename PointRange::value_type Point; typedef typename PointRange::value_type Point;
typedef typename CGAL::Kernel_traits<Point>::Kernel K; typedef typename CGAL::Kernel_traits<Point>::Kernel K;
typedef typename K::Vector_3 Vector_3; typedef typename K::Vector_3 Vector_3;
if (get_mode(out) == IO::BINARY) if (get_mode(out) == IO::BINARY)
{ {
out << "FileType: Binary "; out << "FileType: Binary ";
@ -43,14 +39,12 @@ write_STL(const PointRange& points,
const Point& q = points[face[1]]; const Point& q = points[face[1]];
const Point& r = points[face[2]]; const Point& r = points[face[2]];
Vector_3 n = collinear(p,q,r) ? Vector_3(1,0,0): Vector_3 n = collinear(p,q,r) ? Vector_3(1,0,0) : unit_normal(p,q,r);
unit_normal(p,q,r);
const float coords[12]={ const float coords[12] = { static_cast<float>(n.x()), static_cast<float>(n.y()), static_cast<float>(n.z()),
static_cast<float>(n.x()), static_cast<float>(n.y()), static_cast<float>(n.z()), static_cast<float>(p.x()), static_cast<float>(p.y()), static_cast<float>(p.z()),
static_cast<float>(p.x()), static_cast<float>(p.y()), static_cast<float>(p.z()), static_cast<float>(q.x()), static_cast<float>(q.y()), static_cast<float>(q.z()),
static_cast<float>(q.x()), static_cast<float>(q.y()), static_cast<float>(q.z()), static_cast<float>(r.x()), static_cast<float>(r.y()), static_cast<float>(r.z()) };
static_cast<float>(r.x()), static_cast<float>(r.y()), static_cast<float>(r.z()) };
for (int i=0; i<12; ++i) for (int i=0; i<12; ++i)
out.write(reinterpret_cast<const char *>(&coords[i]), sizeof(coords[i])); out.write(reinterpret_cast<const char *>(&coords[i]), sizeof(coords[i]));
@ -66,8 +60,7 @@ write_STL(const PointRange& points,
const Point& q = points[face[1]]; const Point& q = points[face[1]];
const Point& r = points[face[2]]; const Point& r = points[face[2]];
Vector_3 n = collinear(p,q,r) ? Vector_3(1,0,0): Vector_3 n = collinear(p,q,r) ? Vector_3(1,0,0) : unit_normal(p,q,r);
unit_normal(p,q,r);
out << "facet normal " << n << "\nouter loop\n"; out << "facet normal " << n << "\nouter loop\n";
out << "vertex " << p << "\n"; out << "vertex " << p << "\n";
out << "vertex " << q << "\n"; out << "vertex " << q << "\n";
@ -76,9 +69,10 @@ write_STL(const PointRange& points,
} }
out << "endsolid\n"; out << "endsolid\n";
} }
return out; return out;
} }
} // end of namespace CGAL } // namespace CGAL
#endif // CGAL_IO_STL_STL_WRITER_H #endif // CGAL_IO_STL_STL_WRITER_H

View File

@ -1,16 +1,16 @@
// Copyright (c) 1997 // Copyright (c) 1997
// Utrecht University (The Netherlands), // Utrecht University (The Netherlands),
// ETH Zurich (Switzerland), // ETH Zurich (Switzerland),
// INRIA Sophia-Antipolis (France), // INRIA Sophia-Antipolis (France),
// Max-Planck-Institute Saarbruecken (Germany), // Max-Planck-Institute Saarbruecken (Germany),
// and Tel-Aviv University (Israel). All rights reserved. // and Tel-Aviv University (Israel). All rights reserved.
// //
// This file is part of CGAL (www.cgal.org) // This file is part of CGAL (www.cgal.org)
// //
// $URL$ // $URL$
// $Id$ // $Id$
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial // SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
// //
// //
// Author(s) : Lutz Kettner <kettner@mpi-sb.mpg.de> // Author(s) : Lutz Kettner <kettner@mpi-sb.mpg.de>
@ -18,39 +18,70 @@
#define CGAL_IO_FILE_WRITER_VRML_2_H 1 #define CGAL_IO_FILE_WRITER_VRML_2_H 1
#include <CGAL/basic.h> #include <CGAL/basic.h>
#include <iostream> #include <iostream>
#include <cstddef> #include <cstddef>
namespace CGAL { namespace CGAL {
class CGAL_EXPORT File_writer_VRML_2 { class File_writer_VRML_2
std::ostream* m_out; {
std::size_t m_facets; std::ostream* m_out;
std::size_t m_facets;
public: public:
File_writer_VRML_2() {} File_writer_VRML_2() {}
std::ostream& out() const { return *m_out; } std::ostream& out() const { return *m_out; }
void write_header( std::ostream& o, void write_header( std::ostream& o,
std::size_t vertices, std::size_t vertices,
std::size_t halfedges, std::size_t halfedges,
std::size_t facets); std::size_t facets)
void write_footer() const; {
void write_vertex( const double& x, const double& y, const double& z) { m_out = &o;
out() << " " m_facets = facets;
<< x << ' ' << y << ' ' << z << ',' << '\n';
} out() << " #-- Begin of Polyhedron_3\n";
void write_facet_header() const; out() << " # " << vertices << " vertices\n";
void write_facet_begin( std::size_t) { out() << " # " << halfedges << " halfedges\n";
out() << " "; out() << " # " << facets << " facets\n";
} out() << " Group {\n"
void write_facet_vertex_index( std::size_t idx) { out() << idx << ',';} " children [\n"
void write_facet_end() { out() << "-1,\n"; } " Shape {\n"
" appearance Appearance { material "
"USE Material }\n"
" geometry IndexedFaceSet {\n"
" convex FALSE\n"
" solid FALSE\n"
" coord Coordinate {\n"
" point [" << std::endl;
}
void write_footer() const
{
out() << " ] #coordIndex\n"
" } #geometry\n"
" } #Shape\n"
" ] #children\n"
" } #Group" << std::endl;
}
void write_vertex( const double& x, const double& y, const double& z) {
out() << " "
<< x << ' ' << y << ' ' << z << ',' << '\n';
}
void write_facet_header() const
{
out() << " ] #point\n"
" } #coord Coordinate\n"
" coordIndex [" << std::endl;
}
void write_facet_begin( std::size_t) {
out() << " ";
}
void write_facet_vertex_index( std::size_t idx) { out() << idx << ',';}
void write_facet_end() { out() << "-1,\n"; }
}; };
} //namespace CGAL } // namespace CGAL
#ifdef CGAL_HEADER_ONLY
#include <CGAL/IO/File_writer_VRML_2_impl.h>
#endif // CGAL_HEADER_ONLY
#endif // CGAL_IO_FILE_WRITER_VRML_2_H // #endif // CGAL_IO_FILE_WRITER_VRML_2_H //
// EOF //

View File

@ -1,16 +1,16 @@
// Copyright (c) 1997 // Copyright (c) 1997
// Utrecht University (The Netherlands), // Utrecht University (The Netherlands),
// ETH Zurich (Switzerland), // ETH Zurich (Switzerland),
// INRIA Sophia-Antipolis (France), // INRIA Sophia-Antipolis (France),
// Max-Planck-Institute Saarbruecken (Germany), // Max-Planck-Institute Saarbruecken (Germany),
// and Tel-Aviv University (Israel). All rights reserved. // and Tel-Aviv University (Israel). All rights reserved.
// //
// This file is part of CGAL (www.cgal.org); // This file is part of CGAL (www.cgal.org);
// //
// $URL$ // $URL$
// $Id$ // $Id$
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial // SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
// //
// Author(s) : Andreas Fabri // Author(s) : Andreas Fabri
// Lutz Kettner <kettner@inf.ethz.ch> // Lutz Kettner <kettner@inf.ethz.ch>
// Herve Bronnimann <Herve.Bronnimann@sophia.inria.fr> // Herve Bronnimann <Herve.Bronnimann@sophia.inria.fr>
@ -20,6 +20,7 @@
#define CGAL_IO_VRML_VRML_1_OSTREAM_H #define CGAL_IO_VRML_VRML_1_OSTREAM_H
#include <CGAL/basic.h> #include <CGAL/basic.h>
#include <iostream> #include <iostream>
// Declare the common base class for OpenInventor and VRML 1.0 format. // Declare the common base class for OpenInventor and VRML 1.0 format.
@ -32,22 +33,23 @@
namespace CGAL { namespace CGAL {
class VRML_1_ostream : public Inventor_ostream_base { class VRML_1_ostream : public Inventor_ostream_base
{
public: public:
VRML_1_ostream() {} VRML_1_ostream() {}
VRML_1_ostream(std::ostream& o) : Inventor_ostream_base(o) { VRML_1_ostream(std::ostream& o) : Inventor_ostream_base(o) { header(); }
header(); void open(std::ostream& o) {
} Inventor_ostream_base::open(o);
void open(std::ostream& o) { header();
Inventor_ostream_base::open(o); }
header();
}
private: private:
void header() { void header()
os() << "#VRML V1.0 ascii" << std::endl; {
os() << "# File written with the help of the CGAL Library" os() << "#VRML V1.0 ascii" << std::endl;
<< std::endl; os() << "# File written with the help of the CGAL Library"
} << std::endl;
}
}; };
} //namespace CGAL } //namespace CGAL
@ -86,11 +88,13 @@ operator<<(VRML_1_ostream& os,
<< CGAL::to_double(t[3].y()) << " " << CGAL::to_double(t[3].y()) << " "
<< CGAL::to_double(t[3].z()) << " ]" << CGAL::to_double(t[3].z()) << " ]"
<< "\n } #Coordinate3" ; << "\n } #Coordinate3" ;
os.os() << "\n IndexedFaceSet {" os.os() << "\n IndexedFaceSet {"
<< Indent << "coordIndex [ 0,1,2,-1, 1,3,2,-1,\n" << Indent << "coordIndex [ 0,1,2,-1, 1,3,2,-1,\n"
<< Indent << " 0,2,3,-1, 0,3,1,-1 ]\n" << Indent << " 0,2,3,-1, 0,3,1,-1 ]\n"
<< "\n } #IndexedFaceSet" << "\n } #IndexedFaceSet"
<< "\n } #Separator\n"; << "\n } #Separator\n";
return os; return os;
} }

View File

@ -1,9 +1,9 @@
// Copyright (c) 1997 // Copyright (c) 1997
// Utrecht University (The Netherlands), // Utrecht University (The Netherlands),
// ETH Zurich (Switzerland), // ETH Zurich (Switzerland),
// INRIA Sophia-Antipolis (France), // INRIA Sophia-Antipolis (France),
// Max-Planck-Institute Saarbruecken (Germany), // Max-Planck-Institute Saarbruecken (Germany),
// and Tel-Aviv University (Israel). All rights reserved. // and Tel-Aviv University (Israel). All rights reserved.
// //
// This file is part of CGAL (www.cgal.org); // This file is part of CGAL (www.cgal.org);
// //
@ -24,76 +24,80 @@
namespace CGAL { namespace CGAL {
class VRML_2_ostream { class VRML_2_ostream
{
public: public:
VRML_2_ostream() : m_os(0) {} VRML_2_ostream() : m_os(0) {}
VRML_2_ostream(std::ostream& o) : m_os(&o) { header();} VRML_2_ostream(std::ostream& o) : m_os(&o) { header(); }
~VRML_2_ostream() { close(); } ~VRML_2_ostream() { close(); }
void open(std::ostream& o) { m_os = &o; header(); } void open(std::ostream& o) { m_os = &o; header(); }
void close() { void close()
if ( m_os) {
footer(); if ( m_os)
m_os = 0; footer();
} m_os = 0;
explicit operator bool () { }
return m_os && !m_os->fail();
} explicit operator bool () { return m_os && !m_os->fail(); }
std::ostream& os() {
// The behaviour if m_os == 0 could be changed to return std::ostream& os()
// cerr or a file handle to /dev/null. The latter one would {
// mimick the behaviour that one can still use a stream with // The behaviour if m_os == 0 could be changed to return
// an invalid stream, but without producing any output. // cerr or a file handle to /dev/null. The latter one would
CGAL_assertion( m_os != 0 ); // mimick the behaviour that one can still use a stream with
return *m_os; // an invalid stream, but without producing any output.
} CGAL_assertion( m_os != 0 );
return *m_os;
}
private: private:
void header() { void header()
os() << "#VRML V2.0 utf8\n" {
"# File written with the help of the CGAL Library\n" os() << "#VRML V2.0 utf8\n"
"#-- Begin of file header\n" "# File written with the help of the CGAL Library\n"
"Group {\n" "#-- Begin of file header\n"
" children [\n" "Group {\n"
" Shape {\n" " children [\n"
" appearance DEF A1 Appearance {\n" " Shape {\n"
" material Material {\n" " appearance DEF A1 Appearance {\n"
" diffuseColor .6 .5 .9\n" " material Material {\n"
" }\n }\n" " diffuseColor .6 .5 .9\n"
" appearance\n" " }\n }\n"
" Appearance {\n" " appearance\n"
" material DEF Material Material {}\n" " Appearance {\n"
" }\n" " material DEF Material Material {}\n"
" geometry nullptr\n" " }\n"
" }\n" " geometry nullptr\n"
" #-- End of file header" << std::endl; " }\n"
} " #-- End of file header" << std::endl;
void footer() { }
os() << " #-- Begin of file footer\n"
" ]\n" void footer()
"}\n" {
"#-- End of file footer" << std::endl; os() << " #-- Begin of file footer\n"
} " ]\n"
std::ostream* m_os; "}\n"
"#-- End of file footer" << std::endl;
}
std::ostream* m_os;
}; };
inline inline VRML_2_ostream& operator<<(VRML_2_ostream& os,
VRML_2_ostream& const char* s)
operator<<(VRML_2_ostream& os,
const char* s)
{ {
os.os() << s; os.os() << s;
return os; return os;
} }
inline inline VRML_2_ostream& operator<<(VRML_2_ostream& os,
VRML_2_ostream& const double& d)
operator<<(VRML_2_ostream& os,
const double& d)
{ {
os.os() << d; os.os() << d;
return os; return os;
} }
} //namespace CGAL } // namespace CGAL
#endif // CGAL_IO_VRML_2_OSTREAM_H #endif // CGAL_IO_VRML_2_OSTREAM_H
@ -109,42 +113,43 @@ operator<<(VRML_2_ostream& os,
const Tetrahedron_3<R > &t) const Tetrahedron_3<R > &t)
{ {
const char *Indent = " "; const char *Indent = " ";
os << " Group {\n" os << " Group {\n"
" children [\n" " children [\n"
" Shape {\n" " Shape {\n"
" appearance\n" " appearance\n"
" Appearance {\n" " Appearance {\n"
" material USE Material\n" " material USE Material\n"
" } #Appearance\n" " } #Appearance\n"
" geometry\n" " geometry\n"
" IndexedFaceSet {\n" " IndexedFaceSet {\n"
" coord Coordinate {\n" " coord Coordinate {\n"
" point [ \n" " point [ \n"
<< Indent << " " << Indent << " "
<< CGAL::to_double(t[0].x()) << " " << CGAL::to_double(t[0].x()) << " "
<< CGAL::to_double(t[0].y()) << " " << CGAL::to_double(t[0].y()) << " "
<< CGAL::to_double(t[0].z()) << " ,\n" << CGAL::to_double(t[0].z()) << " ,\n"
<< Indent << " " << Indent << " "
<< CGAL::to_double(t[1].x()) << " " << CGAL::to_double(t[1].x()) << " "
<< CGAL::to_double(t[1].y()) << " " << CGAL::to_double(t[1].y()) << " "
<< CGAL::to_double(t[1].z()) << " ,\n" << CGAL::to_double(t[1].z()) << " ,\n"
<< Indent << " " << Indent << " "
<< CGAL::to_double(t[2].x()) << " " << CGAL::to_double(t[2].x()) << " "
<< CGAL::to_double(t[2].y()) << " " << CGAL::to_double(t[2].y()) << " "
<< CGAL::to_double(t[2].z()) << " ,\n" << CGAL::to_double(t[2].z()) << " ,\n"
<< Indent << " " << Indent << " "
<< CGAL::to_double(t[3].x()) << " " << CGAL::to_double(t[3].x()) << " "
<< CGAL::to_double(t[3].y()) << " " << CGAL::to_double(t[3].y()) << " "
<< CGAL::to_double(t[3].z()) << << CGAL::to_double(t[3].z()) <<
"\n ]\n" "\n ]\n"
" }\n" " }\n"
" solid FALSE\n" " solid FALSE\n"
<< Indent << "coordIndex [ 0,1,2,-1, 1,3,2,-1,\n" << Indent << "coordIndex [ 0,1,2,-1, 1,3,2,-1,\n"
<< Indent << " 0,2,3,-1, 0,3,1,-1 ]\n" << Indent << " 0,2,3,-1, 0,3,1,-1 ]\n"
" } #IndexedFaceSet\n" " } #IndexedFaceSet\n"
" } #Shape\n" " } #Shape\n"
" ] #children\n" " ] #children\n"
" } #Group\n"; " } #Group\n";
return os; return os;
} }
@ -165,16 +170,15 @@ operator<<(VRML_2_ostream& os,
const Point_3<R > &p) const Point_3<R > &p)
{ {
const char *Indent = " "; const char *Indent = " ";
os << " Group {\n" os << " Group {\n"
" children [\n" " children [\n"
" Shape {\n" " Shape {\n"
" appearance USE A1\n" " appearance USE A1\n"
" geometry\n" " geometry\n"
" PointSet {\n" " PointSet {\n"
" coord Coordinate {\n" " coord Coordinate {\n"
" point [ "; " point [ ";
os << CGAL::to_double(p.x()) << " " << CGAL::to_double(p.y()) os << CGAL::to_double(p.x()) << " " << CGAL::to_double(p.y()) << " " << CGAL::to_double(p.z()) << " ]\n";
<< " " << CGAL::to_double(p.z()) << " ]\n";
os << Indent << "}\n"; os << Indent << "}\n";
os << Indent << "} # PointSet\n"; os << Indent << "} # PointSet\n";
os << " } #Shape\n" os << " } #Shape\n"
@ -188,8 +192,6 @@ operator<<(VRML_2_ostream& os,
#endif // CGAL_IO_VRML_2_POINT_3 #endif // CGAL_IO_VRML_2_POINT_3
#endif // CGAL_POINT_3_H #endif // CGAL_POINT_3_H
#ifdef CGAL_TRIANGLE_3_H #ifdef CGAL_TRIANGLE_3_H
#ifndef CGAL_IO_VRML_2_TRIANGLE_3 #ifndef CGAL_IO_VRML_2_TRIANGLE_3
#define CGAL_IO_VRML_2_TRIANGLE_3 #define CGAL_IO_VRML_2_TRIANGLE_3
@ -202,37 +204,34 @@ operator<<(VRML_2_ostream& os,
const Triangle_3<R > &t) const Triangle_3<R > &t)
{ {
const char *Indent = " "; const char *Indent = " ";
os << " Group {\n" os << " Group {\n"
" children [\n" " children [\n"
" Shape {\n" " Shape {\n"
" appearance USE A1\n" " appearance USE A1\n"
" geometry\n" " geometry\n"
" IndexedLineSet {\n" " IndexedLineSet {\n"
" coord Coordinate {\n" " coord Coordinate {\n"
" point [ \n"; " point [ \n";
os << Indent ; os << Indent ;
os << CGAL::to_double(t[0].x()) << " " << CGAL::to_double(t[0].y()) os << CGAL::to_double(t[0].x()) << " " << CGAL::to_double(t[0].y()) << " " << CGAL::to_double(t[0].z()) << ",\n";
<< " " << CGAL::to_double(t[0].z()) << ",\n";
os << Indent; os << Indent;
os << CGAL::to_double(t[1].x()) << " " << CGAL::to_double(t[1].y()) os << CGAL::to_double(t[1].x()) << " " << CGAL::to_double(t[1].y()) << " " << CGAL::to_double(t[1].z()) << ",\n";
<< " " << CGAL::to_double(t[1].z()) << ",\n";
os << Indent; os << Indent;
os << CGAL::to_double(t[2].x()) << " " << CGAL::to_double(t[2].y()) os << CGAL::to_double(t[2].x()) << " " << CGAL::to_double(t[2].y()) << " " << CGAL::to_double(t[2].z()) << " ]\n";
<< " " << CGAL::to_double(t[2].z()) << " ]\n";
os << Indent << "}\n" << Indent << "coordIndex [ 0 1, 1 2, 2 0 -1 ]\n"; os << Indent << "}\n" << Indent << "coordIndex [ 0 1, 1 2, 2 0 -1 ]\n";
os << Indent << "} # IndexedLineSet\n"; os << Indent << "} # IndexedLineSet\n";
os << " } #Shape\n" os << " } #Shape\n"
" ] #children\n" " ] #children\n"
" } #Group\n"; " } #Group\n";
return os; return os;
} }
} //namespace CGAL } // namespace CGAL
#endif // CGAL_IO_VRML_2_TRIANGLE_3 #endif // CGAL_IO_VRML_2_TRIANGLE_3
#endif // CGAL_TRIANGLE_3_H #endif // CGAL_TRIANGLE_3_H
#ifdef CGAL_SEGMENT_3_H #ifdef CGAL_SEGMENT_3_H
#ifndef CGAL_IO_VRML_2_SEGMENT_3 #ifndef CGAL_IO_VRML_2_SEGMENT_3
#define CGAL_IO_VRML_2_SEGMENT_3 #define CGAL_IO_VRML_2_SEGMENT_3
@ -245,20 +244,20 @@ operator<<(VRML_2_ostream& os,
const Segment_3<R > &s) const Segment_3<R > &s)
{ {
const char *Indent = " "; const char *Indent = " ";
os << " Group {\n" os << " Group {\n"
" children [\n" " children [\n"
" Shape {\n" " Shape {\n"
" appearance USE A1\n" " appearance USE A1\n"
" geometry\n" " geometry\n"
" IndexedLineSet {\n" " IndexedLineSet {\n"
" coord Coordinate {\n" " coord Coordinate {\n"
" point [ \n"; " point [ \n";
os << Indent << CGAL::to_double(s.source().x()); os << Indent << CGAL::to_double(s.source().x())
os << " " << CGAL::to_double(s.source().y()) << " " << CGAL::to_double(s.source().y())
<< " " << CGAL::to_double(s.source().z()) << ",\n"; << " " << CGAL::to_double(s.source().z()) << ",\n";
os << Indent; os << Indent;
os << CGAL::to_double(s.target().x()) os << CGAL::to_double(s.target().x())
<< " " << CGAL::to_double(s.target().y()) << " " << CGAL::to_double(s.target().y())
<< " " << CGAL::to_double(s.target().z()) << " ]\n"; << " " << CGAL::to_double(s.target().z()) << " ]\n";
os << Indent << "}\n" << Indent << "coordIndex [ 0 1 -1 ]\n"; os << Indent << "}\n" << Indent << "coordIndex [ 0 1 -1 ]\n";
os << Indent << "} # IndexedLineSet\n"; os << Indent << "} # IndexedLineSet\n";
@ -285,23 +284,23 @@ VRML_2_ostream&
operator<<(VRML_2_ostream& os, operator<<(VRML_2_ostream& os,
const Sphere_3<R > &s) const Sphere_3<R > &s)
{ {
os << " Group {\n" os << " Group {\n"
" children [\n" " children [\n"
" Transform {\n" " Transform {\n"
" translation "; " translation ";
os << CGAL::to_double(s.center().x()) << " " os << CGAL::to_double(s.center().x()) << " "
<< CGAL::to_double(s.center().y()) << " " << CGAL::to_double(s.center().y()) << " "
<< CGAL::to_double(s.center().z()) << "\n"; << CGAL::to_double(s.center().z()) << "\n";
os << " children Shape {\n" os << " children Shape {\n"
" appearance USE A1\n" " appearance USE A1\n"
" geometry\n" " geometry\n"
" Sphere { " " Sphere { "
"radius "; "radius ";
os << std::sqrt(CGAL::to_double(s.squared_radius())) <<" }\n"; os << std::sqrt(CGAL::to_double(s.squared_radius())) <<" }\n";
os << " } #children Shape\n" os << " } #children Shape\n"
" } # Transform\n" " } # Transform\n"
" ] #children\n" " ] #children\n"
" } #Group\n"; " } #Group\n";
return os; return os;
} }

View File

@ -1,12 +1,12 @@
// Copyright (c) 2018 // Copyright (c) 2018
// GeometryFactory( France) All rights reserved. // GeometryFactory (France) All rights reserved.
// //
// This file is part of CGAL (www.cgal.org) // This file is part of CGAL (www.cgal.org)
// //
// $URL$ // $URL$
// $Id$ // $Id$
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial // SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
// //
// //
// Author(s) : Stephane Tayeb // Author(s) : Stephane Tayeb
@ -17,15 +17,15 @@
#include <fstream> #include <fstream>
#include <vector> #include <vector>
template <class FT>
void template <class FT>
write_vector(std::ostream& os, void write_vector(std::ostream& os,
const std::vector<FT>& vect) const std::vector<FT>& vect)
{ {
const char* buffer = reinterpret_cast<const char*>(&(vect[0])); const char* buffer = reinterpret_cast<const char*>(&(vect[0]));
std::size_t size = vect.size()*sizeof(FT); std::size_t size = vect.size()*sizeof(FT);
os.write(reinterpret_cast<const char *>(&size), sizeof(std::size_t)); // number of bytes encoded os.write(reinterpret_cast<const char *>(&size), sizeof(std::size_t)); // number of bytes encoded
os.write(buffer, vect.size()*sizeof(FT)); // encoded data os.write(buffer, vect.size()*sizeof(FT)); // encoded data
} }
#endif #endif

View File

@ -14,19 +14,13 @@
#ifndef CGAL_IO_WKT_H #ifndef CGAL_IO_WKT_H
#define CGAL_IO_WKT_H #define CGAL_IO_WKT_H
#include <CGAL/Point_2.h> #include <CGAL/Point_2.h>
#include <CGAL/Point_3.h> #include <CGAL/Point_3.h>
#include <CGAL/Polygon_2.h> #include <CGAL/Polygon_2.h>
#include <CGAL/Polygon_with_holes_2.h> #include <CGAL/Polygon_with_holes_2.h>
#if BOOST_VERSION >= 105600 && (! defined(BOOST_GCC) || BOOST_GCC >= 40500) #if BOOST_VERSION >= 105600 && (! defined(BOOST_GCC) || BOOST_GCC >= 40500)
#include <iostream>
#include <sstream>
#include <string>
#include <boost/geometry/io/wkt/read.hpp>
#include <boost/geometry/io/wkt/write.hpp>
#include <CGAL/IO/WKT/traits_point.h> #include <CGAL/IO/WKT/traits_point.h>
#include <CGAL/IO/WKT/traits_point_3.h> #include <CGAL/IO/WKT/traits_point_3.h>
@ -36,68 +30,74 @@
#include <CGAL/IO/WKT/traits_multilinestring.h> #include <CGAL/IO/WKT/traits_multilinestring.h>
#include <CGAL/IO/WKT/traits_multipolygon.h> #include <CGAL/IO/WKT/traits_multipolygon.h>
#include <boost/geometry/io/wkt/read.hpp>
#include <boost/geometry/io/wkt/write.hpp>
namespace CGAL{ #include <iostream>
#include <sstream>
#include <string>
namespace CGAL {
namespace internal { namespace internal {
template <typename K> template <typename K>
void pop_back_if_equal_to_front(CGAL::Polygon_2<K>& poly) void pop_back_if_equal_to_front(CGAL::Polygon_2<K>& poly)
{ {
typename CGAL::Polygon_2<K>::iterator it = poly.end(); typename CGAL::Polygon_2<K>::iterator it = poly.end();
--it; --it;
if( (*poly.begin()) == *it){ if((*poly.begin()) == *it)
poly.erase(it); poly.erase(it);
}
}
template <typename K>
void pop_back_if_equal_to_front(CGAL::Polygon_with_holes_2<K>& pwh)
{
pop_back_if_equal_to_front(pwh.outer_boundary());
for(auto i = pwh.holes_begin(); i!= pwh.holes_end(); ++i){
pop_back_if_equal_to_front(*i);
}
}
} }
template <typename K>
void pop_back_if_equal_to_front(CGAL::Polygon_with_holes_2<K>& pwh)
{
pop_back_if_equal_to_front(pwh.outer_boundary());
for(auto i = pwh.holes_begin(); i!= pwh.holes_end(); ++i)
pop_back_if_equal_to_front(*i);
}
} // namespace internal
template<typename Point> template<typename Point>
bool bool read_point_WKT(std::istream& in,
read_point_WKT( std::istream& in, Point& point)
Point& point)
{ {
if(!in) if(!in)
{ {
std::cerr << "Error: cannot open file" << std::endl; std::cerr << "Error: cannot open file" << std::endl;
return false; return false;
} }
std::string line; std::string line;
while(std::getline(in, line)) while(std::getline(in, line))
{ {
std::istringstream iss(line); std::istringstream iss(line);
std::string type; std::string type;
iss >> type; iss >> type;
if(type.substr(0, 5).compare("POINT")==0) if(type.substr(0, 5).compare("POINT") == 0)
{ {
try{ try
boost::geometry::read_wkt(line, point);
} catch(...)
{ {
std::cerr<<"error."<<std::endl; boost::geometry::read_wkt(line, point);
}
catch(...)
{
std::cerr << "error." << std::endl;
return false; return false;
} }
break; break;
} }
} }
return in.good(); return in.good();
} }
template<typename MultiPoint> template<typename MultiPoint>
bool bool read_multi_point_WKT(std::istream& in,
read_multi_point_WKT( std::istream& in, MultiPoint& mp)
MultiPoint& mp)
{ {
if(!in) if(!in)
{ {
@ -111,13 +111,13 @@ read_multi_point_WKT( std::istream& in,
std::istringstream iss(line); std::istringstream iss(line);
std::string type; std::string type;
iss >> type; iss >> type;
if(type.substr(0, 10).compare("MULTIPOINT")==0) if(type.substr(0, 10).compare("MULTIPOINT") == 0)
{ {
try{ try{
boost::geometry::read_wkt(line, gc); boost::geometry::read_wkt(line, gc);
} catch(...){ } catch(...){
std::cerr<<"error."<<std::endl; std::cerr << "error." << std::endl;
return false; return false;
} }
break; break;
@ -126,11 +126,9 @@ read_multi_point_WKT( std::istream& in,
return in.good(); return in.good();
} }
template<typename LineString> template<typename LineString>
bool bool read_linestring_WKT(std::istream& in,
read_linestring_WKT( std::istream& in, LineString& polyline)
LineString& polyline)
{ {
if(!in) if(!in)
{ {
@ -144,13 +142,13 @@ read_linestring_WKT( std::istream& in,
std::istringstream iss(line); std::istringstream iss(line);
std::string type; std::string type;
iss >> type; iss >> type;
if(type.substr(0, 10).compare("LINESTRING")==0) if(type.substr(0, 10).compare("LINESTRING") == 0)
{ {
try{ try{
boost::geometry::read_wkt(line, gc); boost::geometry::read_wkt(line, gc);
} catch(...){ } catch(...){
std::cerr<<"error."<<std::endl; std::cerr << "error." << std::endl;
return false; return false;
} }
break; break;
@ -160,18 +158,18 @@ read_linestring_WKT( std::istream& in,
} }
template<typename MultiLineString> template<typename MultiLineString>
bool bool read_multi_linestring_WKT(std::istream& in,
read_multi_linestring_WKT( std::istream& in, MultiLineString& mls)
MultiLineString& mls)
{ {
if(!in) if(!in)
{ {
std::cerr << "Error: cannot open file" << std::endl; std::cerr << "Error: cannot open file" << std::endl;
return false; return false;
} }
typedef typename MultiLineString::value_type PointRange;
typedef typename MultiLineString::value_type PointRange;
typedef internal::Geometry_container<PointRange, boost::geometry::linestring_tag> LineString; typedef internal::Geometry_container<PointRange, boost::geometry::linestring_tag> LineString;
std::vector<LineString> pr_range; std::vector<LineString> pr_range;
internal::Geometry_container<std::vector<LineString>, boost::geometry::multi_linestring_tag> gc(pr_range); internal::Geometry_container<std::vector<LineString>, boost::geometry::multi_linestring_tag> gc(pr_range);
std::string line; std::string line;
@ -180,52 +178,58 @@ read_multi_linestring_WKT( std::istream& in,
std::istringstream iss(line); std::istringstream iss(line);
std::string type; std::string type;
iss >> type; iss >> type;
if(type.substr(0, 15).compare("MULTILINESTRING")==0) if(type.substr(0, 15).compare("MULTILINESTRING") == 0)
{ {
try{ try
{
boost::geometry::read_wkt(line, gc); boost::geometry::read_wkt(line, gc);
} catch(...){ }
std::cerr<<"error."<<std::endl; catch(...)
{
std::cerr << "error." << std::endl;
return false; return false;
} }
break; break;
} }
} }
for(LineString& ls : gc) for(LineString& ls : gc)
{
mls.push_back(*ls.range); mls.push_back(*ls.range);
}
return in.good(); return in.good();
} }
template<typename Polygon> template<typename Polygon>
bool bool read_polygon_WKT(std::istream& in,
read_polygon_WKT( std::istream& in, Polygon& polygon)
Polygon& polygon
)
{ {
if(!in) if(!in)
{ {
std::cerr << "Error: cannot open file" << std::endl; std::cerr << "Error: cannot open file" << std::endl;
return false; return false;
} }
std::string line; std::string line;
while(std::getline(in, line)) while(std::getline(in, line))
{ {
std::istringstream iss(line); std::istringstream iss(line);
std::string type; std::string type;
iss >> type; iss >> type;
if(type.substr(0, 7).compare("POLYGON")==0) if(type.substr(0, 7).compare("POLYGON") == 0)
{ {
try { try
{
boost::geometry::read_wkt(line, polygon); boost::geometry::read_wkt(line, polygon);
} catch( ...){ }
catch( ...)
{
in.setstate(std::ios::failbit); in.setstate(std::ios::failbit);
return false; return false;
}; };
internal::pop_back_if_equal_to_front(polygon); internal::pop_back_if_equal_to_front(polygon);
break; break;
} }
@ -234,16 +238,15 @@ read_polygon_WKT( std::istream& in,
} }
template<typename MultiPolygon> template<typename MultiPolygon>
bool bool read_multi_polygon_WKT(std::istream& in,
read_multi_polygon_WKT( std::istream& in, MultiPolygon& polygons)
MultiPolygon& polygons
)
{ {
if(!in) if(!in)
{ {
std::cerr << "Error: cannot open file" << std::endl; std::cerr << "Error: cannot open file" << std::endl;
return false; return false;
} }
internal::Geometry_container<MultiPolygon, boost::geometry::multi_polygon_tag> gc(polygons); internal::Geometry_container<MultiPolygon, boost::geometry::multi_polygon_tag> gc(polygons);
std::string line; std::string line;
while(std::getline(in, line)) while(std::getline(in, line))
@ -251,21 +254,22 @@ read_multi_polygon_WKT( std::istream& in,
std::istringstream iss(line); std::istringstream iss(line);
std::string type; std::string type;
iss >> type; iss >> type;
if(type.substr(0, 12).compare("MULTIPOLYGON")==0) if(type.substr(0, 12).compare("MULTIPOLYGON") == 0)
{ {
try { try
{
boost::geometry::read_wkt(line, gc); boost::geometry::read_wkt(line, gc);
} catch( ...){ }
catch( ...)
{
in.setstate(std::ios::failbit); in.setstate(std::ios::failbit);
return false; return false;
}; };
for( typename
internal::Geometry_container<MultiPolygon, boost::geometry::multi_polygon_tag>::iterator it for(typename internal::Geometry_container<MultiPolygon, boost::geometry::multi_polygon_tag>::iterator it = gc.begin(); it != gc.end(); ++it)
= gc.begin(); it != gc.end(); ++it)
{
internal::pop_back_if_equal_to_front(*it); internal::pop_back_if_equal_to_front(*it);
}
break; break;
} }
} }
@ -273,56 +277,51 @@ read_multi_polygon_WKT( std::istream& in,
} }
template<typename Point> template<typename Point>
std::ostream& std::ostream& write_point_WKT(std::ostream& out,
write_point_WKT( std::ostream& out, const Point& point)
const Point& point
)
{ {
if(!out) if(!out)
{ {
std::cerr << "Error: cannot open file" << std::endl; std::cerr << "Error: cannot open file" << std::endl;
return out; return out;
} }
out<<boost::geometry::wkt(point)<<std::endl;
out << boost::geometry::wkt(point) << std::endl;
return out; return out;
} }
template<typename Polygon> template<typename Polygon>
std::ostream& std::ostream& write_polygon_WKT(std::ostream& out,
write_polygon_WKT( std::ostream& out, const Polygon& poly)
const Polygon& poly
)
{ {
if(!out) if(!out)
{ {
std::cerr << "Error: cannot open file" << std::endl; std::cerr << "Error: cannot open file" << std::endl;
return out; return out;
} }
out<<boost::geometry::wkt(poly)<<std::endl;
out << boost::geometry::wkt(poly) << std::endl;
return out; return out;
} }
template<typename LineString> template<typename LineString>
std::ostream& std::ostream& write_linestring_WKT(std::ostream& out,
write_linestring_WKT( std::ostream& out, LineString ls)
LineString ls
)
{ {
if(!out) if(!out)
{ {
std::cerr << "Error: cannot open file" << std::endl; std::cerr << "Error: cannot open file" << std::endl;
return out; return out;
} }
internal::Geometry_container<LineString, boost::geometry::linestring_tag> gc(ls); internal::Geometry_container<LineString, boost::geometry::linestring_tag> gc(ls);
out<<boost::geometry::wkt(gc)<<std::endl; out << boost::geometry::wkt(gc) << std::endl;
return out; return out;
} }
template<typename MultiPoint> template<typename MultiPoint>
std::ostream& std::ostream& write_multi_point_WKT(std::ostream& out,
write_multi_point_WKT( std::ostream& out, MultiPoint& mp)
MultiPoint& mp
)
{ {
if(!out) if(!out)
{ {
@ -330,64 +329,65 @@ write_multi_point_WKT( std::ostream& out,
return out; return out;
} }
internal::Geometry_container<MultiPoint, boost::geometry::multi_point_tag> gc(mp); internal::Geometry_container<MultiPoint, boost::geometry::multi_point_tag> gc(mp);
out<<boost::geometry::wkt(gc)<<std::endl; out << boost::geometry::wkt(gc) << std::endl;
return out; return out;
} }
template<typename MultiPolygon> template<typename MultiPolygon>
std::ostream& std::ostream& write_multi_polygon_WKT(std::ostream& out,
write_multi_polygon_WKT( std::ostream& out, MultiPolygon& polygons)
MultiPolygon& polygons
)
{ {
if(!out) if(!out)
{ {
std::cerr << "Error: cannot open file" << std::endl; std::cerr << "Error: cannot open file" << std::endl;
return out; return out;
} }
internal::Geometry_container<MultiPolygon, boost::geometry::multi_polygon_tag> gc(polygons); internal::Geometry_container<MultiPolygon, boost::geometry::multi_polygon_tag> gc(polygons);
out<<boost::geometry::wkt(gc)<<std::endl; out << boost::geometry::wkt(gc) << std::endl;
return out; return out;
} }
template<typename MultiLineString> template<typename MultiLineString>
std::ostream& std::ostream& write_multi_linestring_WKT(std::ostream& out,
write_multi_linestring_WKT( std::ostream& out, MultiLineString& mls)
MultiLineString& mls
)
{ {
if(!out) if(!out)
{ {
std::cerr << "Error: cannot open file" << std::endl; std::cerr << "Error: cannot open file" << std::endl;
return out; return out;
} }
typedef typename MultiLineString::value_type PointRange;
typedef typename MultiLineString::value_type PointRange;
typedef internal::Geometry_container<PointRange, boost::geometry::linestring_tag> LineString; typedef internal::Geometry_container<PointRange, boost::geometry::linestring_tag> LineString;
std::vector<LineString> pr_range; std::vector<LineString> pr_range;
for(PointRange& pr : mls) for(PointRange& pr : mls)
{ {
LineString ls(pr); LineString ls(pr);
pr_range.push_back(ls); pr_range.push_back(ls);
} }
internal::Geometry_container<std::vector<LineString>, boost::geometry::multi_linestring_tag> gc(pr_range); internal::Geometry_container<std::vector<LineString>, boost::geometry::multi_linestring_tag> gc(pr_range);
out<<boost::geometry::wkt(gc)<<std::endl; out << boost::geometry::wkt(gc) << std::endl;
return out; return out;
} }
template<typename MultiPoint, template<typename MultiPoint,
typename MultiLineString, typename MultiLineString,
typename MultiPolygon> typename MultiPolygon>
std::istream& std::istream& read_WKT(std::istream& input,
read_WKT( std::istream& input, MultiPoint& points,
MultiPoint& points, MultiLineString& polylines,
MultiLineString& polylines, MultiPolygon& polygons)
MultiPolygon& polygons)
{ {
do do
{ {
typedef typename MultiPoint::value_type Point; typedef typename MultiPoint::value_type Point;
typedef typename MultiLineString::value_type LineString; typedef typename MultiLineString::value_type LineString;
typedef typename MultiPolygon::value_type Polygon; typedef typename MultiPolygon::value_type Polygon;
std::string line; std::string line;
std::streampos input_pos = input.tellg(); std::streampos input_pos = input.tellg();
std::getline(input, line); std::getline(input, line);
@ -395,13 +395,16 @@ read_WKT( std::istream& input,
std::string t; std::string t;
std::string type=""; std::string type="";
iss >> t; iss >> t;
for(std::size_t pos=0; pos < t.length(); ++pos) for(std::size_t pos=0; pos < t.length(); ++pos)
{ {
char c=t[pos]; char c = t[pos];
if(c=='(') if(c == '(')
break; break;
type.push_back(c); type.push_back(c);
} }
input.seekg(input_pos); input.seekg(input_pos);
if(type == "POINT") if(type == "POINT")
{ {
@ -427,24 +430,26 @@ read_WKT( std::istream& input,
MultiPoint mp; MultiPoint mp;
CGAL::read_multi_point_WKT(input, mp); CGAL::read_multi_point_WKT(input, mp);
for(const Point& point : mp) for(const Point& point : mp)
points.push_back(point); points.push_back(point);
} }
else if(type == "MULTILINESTRING") else if(type == "MULTILINESTRING")
{ {
MultiLineString mls; MultiLineString mls;
CGAL::read_multi_linestring_WKT(input, mls); CGAL::read_multi_linestring_WKT(input, mls);
for(const LineString& ls : mls) for(const LineString& ls : mls)
polylines.push_back(ls); polylines.push_back(ls);
} }
else if(type == "MULTIPOLYGON") else if(type == "MULTIPOLYGON")
{ {
MultiPolygon mp; MultiPolygon mp;
CGAL::read_multi_polygon_WKT(input, mp); CGAL::read_multi_polygon_WKT(input, mp);
for(const Polygon& poly : mp) for(const Polygon& poly : mp)
polygons.push_back(poly); polygons.push_back(poly);
} }
}while(input.good() && !input.eof()); }
return input; while(input.good() && !input.eof());
return input;
} }
}//end CGAL }//end CGAL
#endif #endif

View File

@ -14,20 +14,27 @@
#ifndef CGAL_IO_WKT_TRAITS_LINESTRING_H #ifndef CGAL_IO_WKT_TRAITS_LINESTRING_H
#define CGAL_IO_WKT_TRAITS_LINESTRING_H #define CGAL_IO_WKT_TRAITS_LINESTRING_H
#if BOOST_VERSION >= 105600 && (! defined(BOOST_GCC) || BOOST_GCC >= 40500) #if BOOST_VERSION >= 105600 && (! defined(BOOST_GCC) || BOOST_GCC >= 40500)
#include <CGAL/internal/Geometry_container.h> #include <CGAL/internal/Geometry_container.h>
#include <boost/geometry/io/wkt/write.hpp> #include <boost/geometry/io/wkt/write.hpp>
#include <boost/geometry/io/wkt/read.hpp> #include <boost/geometry/io/wkt/read.hpp>
namespace boost {
namespace geometry {
namespace traits {
template< typename R>
struct tag<CGAL::internal::Geometry_container<R, linestring_tag> >
{
typedef linestring_tag type;
};
namespace boost{ } // namespace traits
namespace geometry{ } // namespace geometry
namespace traits{ } // namespace boost
template< typename R> struct tag<CGAL::internal::Geometry_container<R, linestring_tag> >
{ typedef linestring_tag type; };
}}} //end namespaces #endif // BOOST VERSION CHECKS
#endif // CGAL_IO_WKT_TRAITS_LINESTRING_H
#endif
#endif // TRAITS_LINESTRING_H

View File

@ -14,21 +14,28 @@
#ifndef CGAL_IO_WKT_TRAITS_MULTILINESTRING_H #ifndef CGAL_IO_WKT_TRAITS_MULTILINESTRING_H
#define CGAL_IO_WKT_TRAITS_MULTILINESTRING_H #define CGAL_IO_WKT_TRAITS_MULTILINESTRING_H
#if BOOST_VERSION >= 105600 && (! defined(BOOST_GCC) || BOOST_GCC >= 40500) #if BOOST_VERSION >= 105600 && (! defined(BOOST_GCC) || BOOST_GCC >= 40500)
#include <CGAL/internal/Geometry_container.h> #include <CGAL/internal/Geometry_container.h>
#include <boost/geometry/io/wkt/write.hpp> #include <boost/geometry/io/wkt/write.hpp>
#include <boost/geometry/io/wkt/read.hpp> #include <boost/geometry/io/wkt/read.hpp>
namespace boost{ namespace boost {
namespace geometry{ namespace geometry {
namespace traits{ namespace traits {
// WKT traits for MultiLinestring // WKT traits for MultiLinestring
template< typename R > template< typename R >
struct tag<CGAL::internal::Geometry_container<R, multi_linestring_tag> > struct tag<CGAL::internal::Geometry_container<R, multi_linestring_tag> >
{ typedef multi_linestring_tag type; }; {
typedef multi_linestring_tag type;
};
}//end traits } // namespace traits
}//end geometry } // namespace geometry
}//end boost } // namespace boost
#endif // TRAITS_MULTILINESTRING_H
#endif #endif // BOOST VERSION CHECKS
#endif // CGAL_IO_WKT_TRAITS_MULTILINESTRING_H

View File

@ -14,21 +14,28 @@
#ifndef CGAL_IO_WKT_TRAITS_MULTIPOINT_H #ifndef CGAL_IO_WKT_TRAITS_MULTIPOINT_H
#define CGAL_IO_WKT_TRAITS_MULTIPOINT_H #define CGAL_IO_WKT_TRAITS_MULTIPOINT_H
#if BOOST_VERSION >= 105600 && (! defined(BOOST_GCC) || BOOST_GCC >= 40500) #if BOOST_VERSION >= 105600 && (! defined(BOOST_GCC) || BOOST_GCC >= 40500)
#include <CGAL/internal/Geometry_container.h> #include <CGAL/internal/Geometry_container.h>
#include <boost/geometry/io/wkt/write.hpp> #include <boost/geometry/io/wkt/write.hpp>
#include <boost/geometry/io/wkt/read.hpp> #include <boost/geometry/io/wkt/read.hpp>
namespace boost{ namespace boost {
namespace geometry{ namespace geometry {
namespace traits{ namespace traits {
// WKT traits for MultiPoint // WKT traits for MultiPoint
template< typename R > template< typename R >
struct tag<CGAL::internal::Geometry_container<R, multi_point_tag > > struct tag<CGAL::internal::Geometry_container<R, multi_point_tag > >
{ typedef multi_point_tag type; }; {
typedef multi_point_tag type;
};
}//end traits } // namespace traits
}//end geometry } // namespace geometry
}//end boost } // namespace boost
#endif // TRAITS_MULTIPOINT_H
#endif #endif // BOOST VERSION CHECKS
#endif // CGAL_IO_WKT_TRAITS_MULTIPOINT_H

View File

@ -14,24 +14,29 @@
#ifndef CGAL_IO_WKT_TRAITS_MULTIPOLYGON_H #ifndef CGAL_IO_WKT_TRAITS_MULTIPOLYGON_H
#define CGAL_IO_WKT_TRAITS_MULTIPOLYGON_H #define CGAL_IO_WKT_TRAITS_MULTIPOLYGON_H
#if BOOST_VERSION >= 105600 && (! defined(BOOST_GCC) || BOOST_GCC >= 40500) #if BOOST_VERSION >= 105600 && (! defined(BOOST_GCC) || BOOST_GCC >= 40500)
#include <CGAL/internal/Geometry_container.h> #include <CGAL/internal/Geometry_container.h>
#include <boost/geometry/io/wkt/write.hpp> #include <boost/geometry/io/wkt/write.hpp>
#include <boost/geometry/io/wkt/read.hpp> #include <boost/geometry/io/wkt/read.hpp>
namespace boost {
namespace geometry {
namespace traits {
namespace boost{
namespace geometry{
namespace traits{
// WKT traits for MultiPolygon // WKT traits for MultiPolygon
template< typename R > template< typename R >
struct tag<CGAL::internal::Geometry_container<R, multi_polygon_tag> > struct tag<CGAL::internal::Geometry_container<R, multi_polygon_tag> >
{ typedef multi_polygon_tag type; }; {
typedef multi_polygon_tag type;
};
}//end traits } // namespace traits
}//end geometry } // namespace geometry
}//end boost } // namespace boost
#endif #endif // BOOST VERSION CHECKS
#endif #endif // CGAL_IO_WKT_TRAITS_MULTIPOLYGON_H

View File

@ -14,58 +14,60 @@
#ifndef CGAL_IO_WKT_TRAITS_POINT_H #ifndef CGAL_IO_WKT_TRAITS_POINT_H
#define CGAL_IO_WKT_TRAITS_POINT_H #define CGAL_IO_WKT_TRAITS_POINT_H
#if BOOST_VERSION >= 105600 && (! defined(BOOST_GCC) || BOOST_GCC >= 40500) #if BOOST_VERSION >= 105600 && (! defined(BOOST_GCC) || BOOST_GCC >= 40500)
#include <CGAL/number_utils.h> #include <CGAL/number_utils.h>
#include <CGAL/Point_2.h> #include <CGAL/Point_2.h>
#include <boost/geometry/io/wkt/write.hpp> #include <boost/geometry/io/wkt/write.hpp>
#include <boost/geometry/io/wkt/read.hpp> #include <boost/geometry/io/wkt/read.hpp>
namespace boost{ namespace boost {
namespace geometry{ namespace geometry {
namespace traits{ namespace traits {
//WKT traits for Points //WKT traits for Points
template< typename K > struct tag<CGAL::Point_2<K> >
{ typedef point_tag type; };
template< typename K > struct coordinate_type<CGAL::Point_2<K> >
{ typedef double type; };
template< typename K > struct coordinate_system<CGAL::Point_2<K> >
{ typedef cs::cartesian type; };
template< typename K > struct dimension<CGAL::Point_2<K> > : boost::mpl::int_<2> {};
template< typename K > template< typename K >
struct access<CGAL::Point_2<K> , 0> struct tag<CGAL::Point_2<K> >
{ {
static double get(CGAL::Point_2<K> const& p) typedef point_tag type;
{
return CGAL::to_double(p.x());
}
static void set(CGAL::Point_2<K> & p, typename K::FT c)
{
p = CGAL::Point_2<K> (c, p.y());
}
}; };
template< typename K > template< typename K >
struct access<CGAL::Point_2<K> , 1> struct coordinate_type<CGAL::Point_2<K> >
{ {
static double get(CGAL::Point_2<K> const& p) typedef double type;
{
return CGAL::to_double(p.y());
}
static void set(CGAL::Point_2<K> & p, typename K::FT c)
{
p = CGAL::Point_2<K> (p.x(), c);
}
}; };
}}}//end namespaces template< typename K >
#endif struct coordinate_system<CGAL::Point_2<K> >
#endif {
typedef cs::cartesian type;
};
template< typename K >
struct dimension<CGAL::Point_2<K> >
: boost::mpl::int_<2>
{};
template< typename K >
struct access<CGAL::Point_2<K>, 0>
{
static double get(const CGAL::Point_2<K>& p) { return CGAL::to_double(p.x()); }
static void set(CGAL::Point_2<K>& p, typename K::FT c) { p = CGAL::Point_2<K>(c, p.y()); }
};
template< typename K >
struct access<CGAL::Point_2<K>, 1>
{
static double get(const CGAL::Point_2<K>& p) { return CGAL::to_double(p.y()); }
static void set(CGAL::Point_2<K>& p, typename K::FT c) { p = CGAL::Point_2<K>(p.x(), c); }
};
} // namespace traits
} // namespace geometry
} // namespace boost
#endif // BOOST VERSION CHECKS
#endif // CGAL_IO_WKT_TRAITS_POINT_H

View File

@ -14,72 +14,67 @@
#ifndef CGAL_IO_WKT_TRAITS_POINT_3_H #ifndef CGAL_IO_WKT_TRAITS_POINT_3_H
#define CGAL_IO_WKT_TRAITS_POINT_3_H #define CGAL_IO_WKT_TRAITS_POINT_3_H
#if BOOST_VERSION >= 105600 && (! defined(BOOST_GCC) || BOOST_GCC >= 40500) #if BOOST_VERSION >= 105600 && (! defined(BOOST_GCC) || BOOST_GCC >= 40500)
#include <CGAL/number_utils.h> #include <CGAL/number_utils.h>
#include <CGAL/Point_3.h> #include <CGAL/Point_3.h>
#include <boost/geometry/io/wkt/write.hpp> #include <boost/geometry/io/wkt/write.hpp>
#include <boost/geometry/io/wkt/read.hpp> #include <boost/geometry/io/wkt/read.hpp>
namespace boost{ namespace boost {
namespace geometry{ namespace geometry {
namespace traits{ namespace traits {
//WKT traits for Points //WKT traits for Points
template< typename K > struct tag<CGAL::Point_3<K> >
{ typedef point_tag type; };
template< typename K > struct coordinate_type<CGAL::Point_3<K> >
{ typedef typename K::FT type; };
template< typename K > struct coordinate_system<CGAL::Point_3<K> >
{ typedef cs::cartesian type; };
template< typename K > struct dimension<CGAL::Point_3<K> > : boost::mpl::int_<3> {};
template< typename K > template< typename K >
struct access<CGAL::Point_3<K> , 0> struct tag<CGAL::Point_3<K> >
{ {
static double get(CGAL::Point_3<K> const& p) typedef point_tag type;
{
return CGAL::to_double(p.x());
}
static void set(CGAL::Point_3<K> & p, typename K::FT c)
{
p = CGAL::Point_3<K> (c, p.y(), p.z());
}
}; };
template< typename K > template< typename K >
struct access<CGAL::Point_3<K> , 1> struct coordinate_type<CGAL::Point_3<K> >
{ {
static double get(CGAL::Point_3<K> const& p) typedef typename K::FT type;
{
return CGAL::to_double(p.y());
}
static void set(CGAL::Point_3<K> & p, typename K::FT c)
{
p = CGAL::Point_3<K> (p.x(), c, p.z());
}
};
template< typename K >
struct access<CGAL::Point_3<K> , 2>
{
static double get(CGAL::Point_3<K> const& p)
{
return CGAL::to_double(p.z());
}
static void set(CGAL::Point_3<K> & p, typename K::FT c)
{
p = CGAL::Point_3<K> (p.x(), p.y(), c);
}
}; };
}}}//end namespaces template< typename K >
#endif struct coordinate_system<CGAL::Point_3<K> >
#endif {
typedef cs::cartesian type;
};
template< typename K >
struct dimension<CGAL::Point_3<K> >
: boost::mpl::int_<3>
{};
template< typename K >
struct access<CGAL::Point_3<K>, 0>
{
static double get(const CGAL::Point_3<K>& p) { return CGAL::to_double(p.x()); }
static void set(CGAL::Point_3<K>& p, typename K::FT c) { p = CGAL::Point_3<K> (c, p.y(), p.z()); }
};
template< typename K >
struct access<CGAL::Point_3<K>, 1>
{
static double get(const CGAL::Point_3<K>& p) { return CGAL::to_double(p.y()); }
static void set(CGAL::Point_3<K>& p, typename K::FT c) { p = CGAL::Point_3<K> (p.x(), c, p.z()); }
};
template< typename K >
struct access<CGAL::Point_3<K>, 2>
{
static double get(const CGAL::Point_3<K>& p) { return CGAL::to_double(p.z()); }
static void set(CGAL::Point_3<K>& p, typename K::FT c) { p = CGAL::Point_3<K> (p.x(), p.y(), c); }
};
} // namespace traits
} // namespace geometry
} // namespace boost
#endif // BOOST VERSION CHECKS
#endif // CGAL_IO_WKT_TRAITS_POINT_3_H

View File

@ -14,35 +14,50 @@
#ifndef CGAL_IO_WKT_TRAITS_POLYGON_H #ifndef CGAL_IO_WKT_TRAITS_POLYGON_H
#define CGAL_IO_WKT_TRAITS_POLYGON_H #define CGAL_IO_WKT_TRAITS_POLYGON_H
#if BOOST_VERSION >= 105600 && (! defined(BOOST_GCC) || BOOST_GCC >= 40500) #if BOOST_VERSION >= 105600 && (! defined(BOOST_GCC) || BOOST_GCC >= 40500)
#include <CGAL/Polygon_2.h> #include <CGAL/Polygon_2.h>
#include <CGAL/Polygon_with_holes_2.h> #include <CGAL/Polygon_with_holes_2.h>
#include <CGAL/Point_2.h> #include <CGAL/Point_2.h>
#include <boost/geometry/io/wkt/write.hpp> #include <boost/geometry/io/wkt/write.hpp>
#include <boost/geometry/io/wkt/read.hpp> #include <boost/geometry/io/wkt/read.hpp>
namespace boost{ namespace boost {
namespace geometry{ namespace geometry {
namespace traits{ namespace traits {
// WKT traits for Polygon // WKT traits for Polygon
template< typename K > struct tag<CGAL::Polygon_2<K> > template< typename K >
{ typedef ring_tag type; }; struct tag<CGAL::Polygon_2<K> >
{
typedef ring_tag type;
};
template< typename K > template< typename K >
struct tag<CGAL::Polygon_with_holes_2<K> > struct tag<CGAL::Polygon_with_holes_2<K> >
{ typedef polygon_tag type; }; {
typedef polygon_tag type;
};
template< typename K > template< typename K >
struct ring_const_type<CGAL::Polygon_with_holes_2<K> > struct ring_const_type<CGAL::Polygon_with_holes_2<K> >
{ typedef const CGAL::Polygon_2<K>& type; }; {
typedef const CGAL::Polygon_2<K>& type;
};
template< typename K > template< typename K >
struct ring_mutable_type<CGAL::Polygon_with_holes_2<K> > struct ring_mutable_type<CGAL::Polygon_with_holes_2<K> >
{ typedef CGAL::Polygon_2<K>& type; }; {
typedef CGAL::Polygon_2<K>& type;
};
template< typename K > template< typename K >
struct interior_const_type<CGAL::Polygon_with_holes_2<K> > struct interior_const_type<CGAL::Polygon_with_holes_2<K> >
{ typedef const typename CGAL::Polygon_with_holes_2<K>::Holes_container& type; }; {
typedef const typename CGAL::Polygon_with_holes_2<K>::Holes_container& type;
};
template< typename K > template< typename K >
struct interior_mutable_type<CGAL::Polygon_with_holes_2<K> > struct interior_mutable_type<CGAL::Polygon_with_holes_2<K> >
@ -55,7 +70,7 @@ struct exterior_ring<CGAL::Polygon_with_holes_2<K> >
{ {
return (p.outer_boundary()); return (p.outer_boundary());
} }
static CGAL::Polygon_2<K> const& get(CGAL::Polygon_with_holes_2<K> const& p) static const CGAL::Polygon_2<K>& get(const CGAL::Polygon_with_holes_2<K>& p)
{ {
return (p.outer_boundary()); return (p.outer_boundary());
} }
@ -73,8 +88,9 @@ struct interior_rings<CGAL::Polygon_with_holes_2<K> >
return p.holes(); return p.holes();
} }
}; };
}//end traits
}//end geometry } // namespace traits
} // namespace geometry
//extra specialization //extra specialization
template< typename K > template< typename K >
@ -83,7 +99,7 @@ struct range_value<CGAL::Polygon_2<K> >
typedef typename CGAL::Polygon_2<K>::Point_2 type; typedef typename CGAL::Polygon_2<K>::Point_2 type;
}; };
}//end boost } // namespace boost
#endif #endif // BOOST VERSION CHECKS
#endif #endif // CGAL_IO_WKT_TRAITS_POLYGON_H