1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
// K-3D
// Copyright (c) 1995-2004, Timothy M. Shead
//
// Contact: tshead@k-3d.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
/** \file
\brief Implements a RAW file importer (.raw polygonal meshes)
\author Romain Behar (romainbehar@yahoo.com)
*/
#include <k3dsdk/file_helpers.h>
#include <k3dsdk/iapplication.h>
#include <k3dsdk/ideletable.h>
#include <k3dsdk/idocument.h>
#include <k3dsdk/ifile_format.h>
#include <k3dsdk/igeometry_read_format.h>
#include <k3dsdk/imaterial_collection.h>
#include <k3dsdk/imaterial.h>
#include <k3dsdk/iobject_collection.h>
#include <k3dsdk/material.h>
#include <k3dsdk/mesh.h>
#include <k3dsdk/module.h>
#include <k3dsdk/plugins.h>
#include <k3dsdk/result.h>
#include <k3dsdk/selection.h>
#include <k3dsdk/string_modifiers.h>
#include <k3dsdk/utility.h>
#include "helpers.h"
#include <boost/filesystem/fstream.hpp>
#include <boost/filesystem/path.hpp>
namespace libk3dgeometry
{
bool raw_line(std::istream& file, std::string& linebuffer)
{
// Strip comments (lines beginning with '#') and empty lines,
// return true if buffer has been filled, return false on end of file
while(!file.eof())
{
k3d::getline(file, linebuffer);
// Skip comments ...
if(linebuffer[0] == '#')
continue;
// Skip blank lines
if(!(k3d::trim(linebuffer)).size())
continue;
// Must qualify
return true;
}
return false;
}
/////////////////////////////////////////////////////////////////////////////
// raw_reader_implementation
class raw_reader_implementation :
public k3d::ifile_format,
public k3d::igeometry_read_format,
public k3d::ideletable
{
public:
unsigned long priority()
{
return 0;
}
bool query_can_handle(const boost::filesystem::path& FilePath)
{
return "raw" == k3d::file_extension(FilePath);
}
bool pre_read(k3d::idocument& Document, const boost::filesystem::path& FilePath)
{
return true;
}
bool read_options(k3d::idocument& Document, const boost::filesystem::path& FilePath)
{
return true;
}
bool read_file(k3d::idocument& Document, const boost::filesystem::path& FilePath);
k3d::iplugin_factory& factory()
{
return get_factory();
}
static k3d::iplugin_factory& get_factory()
{
static k3d::plugin_factory<k3d::application_plugin<raw_reader_implementation>, k3d::interface_list<k3d::igeometry_read_format> > factory(
k3d::uuid(0x49ca924e, 0x159023b3, 0x492fb90d, 0x9afb0555),
"RAWReader",
"Raw ( .raw )",
"");
return factory;
}
};
bool raw_reader_implementation::read_file(k3d::idocument& Document, const boost::filesystem::path& FilePath)
{
// Try to open the input file ...
boost::filesystem::ifstream file(FilePath);
if(!file.good())
{
std::cerr << __PRETTY_FUNCTION__ << ": error opening [" << FilePath.native_file_string() << "]" << std::endl;
return_val_if_fail(0, false);
}
// Empty file check ...
std::string linebuffer;
if(!raw_line(file, linebuffer))
return false;
// Extract point, polygon numbers
std::istringstream stream(linebuffer);
unsigned int npoints = 0;
unsigned int npols = 0;
stream >> npoints >> npols;
// File format for raw polygonal surfaces :
// - all the lines beginning with '#' are ignored as well as empty lines
// - all parameters are space-separated
// - the first line contains two unsigned integers separated by spaces :
// the number of vertices (npoints) and the number of polygons (npols)
// - then follow :
// * npoints lines each containing the x, y and z coordinates of the vertices
// * npols lines each containing the ordered indices of the points of a polygon
// - points are 0-indexed
// - a file can contain several raw objects
// Give objects unique names as we build them ...
unsigned long object_number = 0;
// Read the stream ...
k3d::iobject* frozen_mesh;
k3d::iobject* mesh_instance;
while(npoints && npols)
{
// Create a new mesh ...
k3d::mesh* const mesh = detail::create_mesh(Document, "RAW Object " + k3d::to_string(++object_number), frozen_mesh, mesh_instance);
return_val_if_fail(mesh, false);
// Create a polyhedron ...
k3d::polyhedron* const polyhedron = new k3d::polyhedron();
return_val_if_fail(polyhedron, false);
mesh->polyhedra.push_back(polyhedron);
// Set default material
polyhedron->material = dynamic_cast<k3d::imaterial*>(k3d::default_material(Document));
// Read points
std::vector<k3d::point*> points;
for(unsigned int n = 0; n < npoints; n++)
{
// Get next data line
if(!raw_line(file, linebuffer))
{
std::cerr << "ERROR: Unexpected end of input file (file \"" << FilePath.native_file_string() << "\", point #" << n << "." << std::endl;
return false;
}
// Extract a record type ...
std::istringstream stream(linebuffer);
k3d::vector3 coords;
stream >> coords;
k3d::point* const point = new k3d::point(coords);
return_val_if_fail(point, false);
points.push_back(point);
mesh->points.push_back(point);
}
// Read polygons
for(unsigned int n = 0; n < npols; n++)
{
// Get next data line
if(!raw_line(file, linebuffer))
{
std::cerr << warning << "ERROR: Unexpected end of input file (file \"" << FilePath.native_file_string() << "\", triangle #" << n << "." << std::endl;
return false;
}
// Create a face ...
k3d::split_edge* previous_edge = 0;
k3d::face* face = 0;
// Extract a polygon ...
std::istringstream stream(linebuffer);
do
{
unsigned long geometric_index = 0;
stream >> geometric_index;
k3d::point* const point = points[geometric_index];
return_val_if_fail(point, false);
k3d::split_edge* edge = new k3d::split_edge(point);
return_val_if_fail(edge, false);
if(!face)
{
face = new k3d::face(edge);
return_val_if_fail(face, false);
polyhedron->faces.push_back(face);
}
else
{
previous_edge->face_clockwise = edge;
}
polyhedron->edges.push_back(edge);
previous_edge = edge;
}
while(!stream.eof());
// Close loop
if(face)
{
previous_edge->face_clockwise = face->first_edge;
}
}
// Go on for another one ?
npoints = npols = 0;
if(raw_line(file, linebuffer))
{
std::istringstream stream(linebuffer);
stream >> npoints >> npols;
}
}
return true;
}
k3d::iplugin_factory& raw_reader_factory()
{
return raw_reader_implementation::get_factory();
}
} // namespace libk3dgeometry
|