The first commit

This commit is contained in:
lgfischer committed 2012-11-10 10:27:37 -02:00
commit 907922fdee
45 files changed
+7594

No files matched your search

+272
View File
@@ -0,0 +1,272 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright Leonardo Fischer 2011 - http://coderender.blogspot.com
//
// Distributed under the licence available in the accompanying file
// LICENCE.txt. Please read it before use this code.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef DCELStream_h
#define DCELStream_h
#include <iostream>
#include "Mesh.h"
template <class MeshT>
class DCELStream
{
public:
static void read(MeshT& mesh, std::istream &stream);
/**
Format:
version 1
#this is a comment
vc <number of vertices in the mesh>
fc <number of faces in the mesh>
ec <number of edges in the mesh>
v <custom user data>
... # repeat v for vc times, one for each vertex
f <number of vertices that define this face> <list of id of each vertex> <custom user data>
... # repeat f for fc times, one for each face
e <id of the source vertex of the edge> <id of the source of the twin edge> <custom user data>
... # repeat e for ec times, one for each half edge
end
*/
static void write(const MeshT& mesh, std::ostream &stream);
template<class MeshT, class MeshT2>
static void copyDcelData(const MeshT& source, MeshT2& target )
{
const MeshT::Vertex* vertex = NULL;
const unsigned int numVertices = source.getNumVertices();
const MeshT::Face* face = NULL;
const unsigned int numFaces = source.getNumFaces();
const MeshT::HalfEdge* edge = NULL;
const unsigned int numEdges = source.getNumHalfEdges();
target.clear();
target.getVertices().reserve( numVertices );
target.getFaces().reserve( numFaces );
target.getHalfEdges().reserve( numEdges );
for( unsigned int vertexId=0; vertexId<numVertices; ++vertexId )
{
target.createVertex();
// convert here the data from the source to target vertex
}
for( unsigned int faceId=0; faceId<numFaces; ++faceId )
{
face = source.getFace( faceId );
unsigned int vid[3];//generalizar
unsigned int vertexCount=0;
MeshT::EdgeIterator it(face);
while( it.hasNext() )
{
vid[vertexCount] = source.getVertexId( it.getNext()->getOrigin() );
++vertexCount;
}
target.createTriangularFace( vid[0], vid[1], vid[2] );
// convert here the data from the source to target faces
}
/*
for( unsigned int edgeId=0; edgeId<numEdges; ++edgeId )
{
// convert here the data from the source to target half-edges
}
*/
};
protected:
private:
static void read_v1(MeshT& mesh, std::istream &stream);
static void consumeComment(const std::string& commentBegin, std::istream &stream);
};
//////////////////////////////////////////////////////////////////////////
// IMPLEMENTATION //
//////////////////////////////////////////////////////////////////////////
template <class MeshT>
void DCELStream<MeshT>::read(MeshT& mesh, std::istream &stream)
{
while( stream.good() )
{
std::string str;
stream >> str;
if( str=="version" )
{
unsigned int version;
stream >> version;
switch(version)
{
case 1:
read_v1(mesh, stream);
break;
default:
std::cerr << "Warning: unknown version " << version << std::endl;
}
}
else if( str.at(0)=='#' )
{
consumeComment(str, stream);
}
else
{
std::cerr << "Unknown line: " << str << std::endl;
}
}
}
template <class MeshT>
void DCELStream<MeshT>::write(const MeshT& mesh, std::ostream &stream)
{
const MeshT::Vertex* vertex = NULL;
const unsigned int numVertices = mesh.getNumVertices();
const MeshT::Face* face = NULL;
const unsigned int numFaces = mesh.getNumFaces();
const MeshT::HalfEdge* edge = NULL;
const unsigned int numEdges = mesh.getNumHalfEdges();
stream << "version 1" << std::endl;
stream << "vc " << numVertices << std::endl;
stream << "fc " << numFaces << std::endl;
stream << "ec " << numEdges << std::endl;
for( unsigned int vertexId=0; vertexId<numVertices; ++vertexId )
{
vertex = mesh.getVertex( vertexId );
stream << "v " << vertex->getData() << std::endl;
}
for( unsigned int faceId=0; faceId<numFaces; ++faceId )
{
face = mesh.getFace( faceId );
stream << "f ";
unsigned int vid[3];//generalizar
unsigned int vertexCount=0;
MeshT::EdgeIterator it(face);
while( it.hasNext() )
{
vid[vertexCount] = mesh.getVertexId( it.getNext()->getOrigin() );
++vertexCount;
}
stream << vertexCount;
for( unsigned int v=0; v<vertexCount; ++v )
{
stream << " " << vid[v];
}
stream << " " << face->getData();
stream << std::endl;
}
for( unsigned int edgeId=0; edgeId<numEdges; ++edgeId )
{
const MeshT::HalfEdge* edge = mesh.getHalfEdge( edgeId );
stream << "e " << mesh.getVertexId( edge->getOrigin() )
<< " " << mesh.getVertexId( edge->getTwin()->getOrigin() )
<< " " << edge->getData() << std::endl;
}
stream << "end";
}
template <class MeshT>
void DCELStream<MeshT>::read_v1(MeshT& mesh, std::istream &stream)
{
std::string str;
bool reading = true;
while( reading && stream.good() )
{
stream >> str;
if( str=="v" )
{
MeshT::Vertex* vertex = mesh.getVertex( mesh.createVertex() );
stream >> (vertex->getData());
}
else if( str=="f" )
{
unsigned int vertexCount;
stream >> vertexCount;
unsigned int vid[3];//TODO generalize
for( unsigned int i=0; i<vertexCount; ++i )
stream >> (vid[i]);
unsigned int fid = mesh.createTriangularFace( vid[0], vid[1], vid[2] );
MeshT::Face* face = mesh.getFace( fid );
stream >> face->getData();
}
else if( str=="e" )
{
unsigned int from, to;
stream >> from >> to;
MeshT::HalfEdge* edge = mesh.getHalfEdge( mesh.getVertex(from), mesh.getVertex(to) );
if( edge )
{
stream >> edge->getData();
}
else
{
std::cerr << '?';
}
}
else if( str=="vc" )
{
unsigned int vc = -1;
stream >> vc;
mesh.getVertices().reserve( vc*5 );
}
else if( str=="fc" )
{
unsigned int fc = -1;
stream >> fc;
mesh.getFaces().reserve( fc*5 );
}
else if( str=="ec" )
{
unsigned int ec = -1;
stream >> ec;
mesh.getHalfEdges().reserve( ec*5 );
}
else if( str=="end" )
{
reading = false;
}
else if( str.at(0)=='#' )
{
consumeComment(str, stream);
}
else
{
std::cerr << "Unknown line: " << str;
}
}
mesh.manageUnhandledTriangles();
}
template <class MeshT>
void DCELStream<MeshT>::consumeComment(const std::string& commentBegin, std::istream &stream)
{
char buffer[512];
stream.getline( buffer, sizeof(buffer) );
std::cerr << commentBegin << " " << buffer << std::endl;
}
#endif//DCELStream_h
+125
View File
@@ -0,0 +1,125 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright Leonardo Fischer 2011 - http://coderender.blogspot.com
//
// Distributed under the licence available in the accompanying file
// LICENCE.txt. Please read it before use this code.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef DCEL_EdgeIterator_h
#define DCEL_EdgeIterator_h
#include "Vertex.h"
#include "Face.h"
#include "HalfEdge.h"
//#include <cassert>
/**
A class that helps the iteration of the edges of a vertice or face.
In the case of a vertex, it runs over each edge that has the given
edge as origin.
In the case of a face, it runs over each edge that forms the boundary
of that face.
*/
template<class VertexDataT, class HalfEdgeDataT, class FaceDataT>
class EdgeIteratorT
{
typedef VertexT<VertexDataT, HalfEdgeDataT, FaceDataT> Vertex;
typedef HalfEdgeT<VertexDataT, HalfEdgeDataT, FaceDataT> HalfEdge;
typedef FaceT<VertexDataT, HalfEdgeDataT, FaceDataT> Face;
public:
/**
Creates an EdgeIterator that runs over each edge that starts on the
given vertex.
*/
inline EdgeIteratorT(const Vertex* vertex)
{
reset(vertex);
};
/**
Creates an EdgeIterator that runs over each edge that forms the
boundary of the given face.
*/
inline EdgeIteratorT(const Face* face)
{
reset(face);
};
/**
Returns the next edge in the sequence.
Returns NULL if all the sequence of edges has ended.
*/
inline HalfEdge* getNext()
{
HalfEdge* next = nextEdge;
if( nextEdge )
{
if( vertexIterator ) // edges starting at a vertex iterator
{
HalfEdge* twin = nextEdge->getTwin();
//assert(twin);
nextEdge = twin->getNext();
}
else // edges from face iterator
{
nextEdge = nextEdge->getNext();
}
}
if( nextEdge==startEdge )
{
nextEdge = NULL;
}
return next;
};
/**
Check if there is any edge in the sequence of edges.
*/
inline bool hasNext() const
{
return nextEdge!=NULL;
};
/**
Makes the iterator start again, from the first edge
of the sequence.
*/
inline void reset()
{
nextEdge = startEdge;
};
inline void reset(const Vertex* vertex)
{
//assert(vertex);
vertexIterator = true;
startEdge = vertex->getIncidentEdge();
reset();
};
inline void reset(const Face* face)
{
//assert(face);
vertexIterator = false;
startEdge = face->getBoundary();
reset();
};
protected:
private:
HalfEdge* startEdge;
HalfEdge* nextEdge;
bool vertexIterator;
};
#endif//DCEL_EdgeIterator_h
+58
View File
@@ -0,0 +1,58 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright Leonardo Fischer 2011 - http://coderender.blogspot.com
//
// Distributed under the licence available in the accompanying file
// LICENCE.txt. Please read it before use this code.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef Exception_h
#define Exception_h
#include <exception>
#include <string>
namespace cpp
{
class Exception: public std::exception
{
public:
Exception(const char* m)
:msg(m)
{};
Exception(const std::string& msg)
: msg(msg)
{};
Exception(const std::exception& e)
: msg(e.what())
{
}
~Exception() throw()
{}
Exception& operator=(const std::exception& e) throw()
{
this->msg = e.what();
return *this;
}
virtual const char* what() const throw()
{
return msg.c_str();
}
virtual const std::string& message() const
{
return this->msg;
}
private:
std::string msg;
};
};
#endif//Exception_h
+58
View File
@@ -0,0 +1,58 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright Leonardo Fischer 2011 - http://coderender.blogspot.com
//
// Distributed under the licence available in the accompanying file
// LICENCE.txt. Please read it before use this code.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef DCEL_Face_h
#define DCEL_Face_h
//#include <cassert>
template<class VertexDataT, class HalfEdgeDataT, class FaceDataT>
class HalfEdgeT;
template<class VertexDataT, class HalfEdgeDataT, class FaceDataT>
class FaceT
{
typedef HalfEdgeT<VertexDataT, HalfEdgeDataT, FaceDataT> HalfEdge;
typedef FaceT<VertexDataT, HalfEdgeDataT, FaceDataT> Face;
public:
FaceT():
boundary( NULL )
{
};
inline HalfEdge* getBoundary() const
{
return this->boundary;
};
inline void setBoundary(HalfEdge* bound)
{
//assert( this );
this->boundary = bound;
};
inline FaceDataT& getData()
{
return data;
};
inline const FaceDataT& getData() const
{
return data;
};
protected:
private:
HalfEdge* boundary;
FaceDataT data;
};
#endif//DCEL_Face_h
+123
View File
@@ -0,0 +1,123 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright Leonardo Fischer 2011 - http://coderender.blogspot.com
//
// Distributed under the licence available in the accompanying file
// LICENCE.txt. Please read it before use this code.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef DCEL_HalfEdge_h
#define DCEL_HalfEdge_h
//#include <cassert>
template<class VertexDataT, class HalfEdgeDataT, class FaceDataT>
class VertexT;
template<class VertexDataT, class HalfEdgeDataT, class FaceDataT>
class FaceT;
template<class VertexDataT, class HalfEdgeDataT, class FaceDataT>
class HalfEdgeT
{
typedef VertexT<VertexDataT, HalfEdgeDataT, FaceDataT> Vertex;
typedef HalfEdgeT<VertexDataT, HalfEdgeDataT, FaceDataT> HalfEdge;
typedef FaceT<VertexDataT, HalfEdgeDataT, FaceDataT> Face;
public:
HalfEdgeT( ):
data(),
twin(NULL),
next(NULL),
prev(NULL),
origin(NULL),
face(NULL)
{
};
inline HalfEdge* getTwin() const
{
//assert(this);
return this->twin;
};
inline void setTwin(HalfEdge* newTwin)
{
//assert(this);
this->twin = newTwin;
newTwin->twin = this;
};
inline HalfEdge* getNext() const
{
//assert(this);
return this->next;
};
inline void setNext(HalfEdge* newNext)
{
//assert(this);
this->next = newNext;
newNext->prev = this;
};
inline HalfEdge* getPrev() const
{
//assert(this);
return this->prev;
};
inline void setPrev(HalfEdge* newPrev)
{
//assert(this);
this->prev = newPrev;
newPrev->next = this;
};
inline Vertex* getOrigin() const
{
//assert(this);
return this->origin;
};
inline void setOrigin(Vertex* newOrigin)
{
//assert(this);
this->origin = newOrigin;
};
inline Face* getFace() const
{
//assert(this);
return this->face;
};
inline void setFace(Face* newFace)
{
//assert(this);
this->face = newFace;
};
inline HalfEdgeDataT& getData()
{
return data;
};
inline const HalfEdgeDataT& getData() const
{
return data;
};
protected:
private:
HalfEdge* twin;
HalfEdge* next;
HalfEdge* prev;
Vertex* origin;
Face* face;
HalfEdgeDataT data;
};
#endif//DCEL_HalfEdge_h
+1035
View File
File diff suppressed because it is too large. Load diff
+102
View File
@@ -0,0 +1,102 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright Leonardo Fischer 2011 - http://coderender.blogspot.com
//
// Distributed under the licence available in the accompanying file
// LICENCE.txt. Please read it before use this code.
//
///////////////////////////////////////////////////////////////////////////////
#include "PlyImporter.h"
#include "../rply/rply.h"
/*
This code uses the RPly 1.01 library, from
http://w3.impa.br/~diego/software/rply/.
The code for this lib can be downloaded from
http://w3.impa.br/~diego/software/rply/rply-1.01.tar.gz. There is a copy of
the RPly 1.01 available with this file.
*/
int vertex_cb(p_ply_argument argument)
{
PlyLoader* loader;
long coordinate;
ply_get_argument_user_data(argument, (void**)&loader, &coordinate);
float position = (float)ply_get_argument_value(argument);
switch(coordinate)
{
case 0:
loader->vertices.push_back( Vector3f(position,0,0) );
break;
case 1:
loader->vertices.back().y = position;
break;
case 2:
loader->vertices.back().z = position;
}
return 1;
}
int face_cb(p_ply_argument argument)
{
long length, value_index;
ply_get_argument_property(argument, NULL, &length, &value_index);
if( length!=3 ) //ensures only triangles are read
{
throw cpp::Exception("This file contains other things than triangles (1)!'");
}
unsigned int val = (unsigned int)ply_get_argument_value(argument);
if( value_index<0 )
{
if( val!=3 ) //ensures only triangles are read
{
throw cpp::Exception("This file contains other things than triangles (2)!'");
}
return 1; // just returns, this is not usefull data (its the size of the list that will follow)
}
PlyLoader* loader;
ply_get_argument_user_data(argument, (void**)&loader, NULL);
loader->faces.push_back( val );
return 1;
}
void error_cb(const char *message)
{
std::cerr << message << std::endl;
}
void PlyLoader::load( const std::string& plyFilename )
{
this->vertices.clear();
this->verticeCount = 0;
this->faces.clear();
this->faceCount = 0;
p_ply ply = ply_open(plyFilename.c_str(), error_cb);
if( !ply )
{
throw cpp::Exception(std::string("Cannot open the file '")+plyFilename+"'");
}
if(!ply_read_header(ply) )
{
throw cpp::Exception(std::string("Error loading the header of the file '")+plyFilename+"'");
}
this->verticeCount = ply_set_read_cb(ply, "vertex", "x", vertex_cb, this, 0);
ply_set_read_cb(ply, "vertex", "y", vertex_cb, this, 1);
ply_set_read_cb(ply, "vertex", "z", vertex_cb, this, 2);
this->faceCount = ply_set_read_cb(ply, "face", "vertex_indices", face_cb, this, 0);
if (!ply_read(ply))
{
throw cpp::Exception(std::string("Error loading the the whole file '")+plyFilename+"'");
}
ply_close(ply);
}
+105
View File
@@ -0,0 +1,105 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright Leonardo Fischer 2011 - http://coderender.blogspot.com
//
// Distributed under the licence available in the accompanying file
// LICENCE.txt. Please read it before use this code.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef PlyImporter_h
#define PlyImporter_h
#include "DCELStream.h"
#include "Vector3.h"
/**
This class is used internally by the PlyImporter, and should not be used externally.
It loads a set of vertices into the "vertices" list and the set of faces into the
"faces" list. Each 3 values into the "faces" lists stores one triangular face,
composed by the vertices referenced by the 3 unsigned int values.
*/
class PlyLoader
{
public:
void load(const std::string& plyFilename);
std::list<Vector3f> vertices;
unsigned int verticeCount;
std::list<unsigned int> faces;
unsigned int faceCount;
};
/**
Class that imports a PLY file into a DCEL mesh.
It assumes that the mesh is totally triangulated.
*/
template <class MeshT>
class PlyImporter
{
public:
/**
Loads from the given filename.
It just calls the 'import' method with the opened stream.
*/
void import( const std::string& plyFilename, MeshT& mesh );
private:
};
template <class MeshT>
void PlyImporter<MeshT>::import( const std::string& plyFilename, MeshT& mesh )
{
std::cerr << "Starting importing the file '" << plyFilename << "'" << std::endl;
std::cerr << "- loading the PLY file" << std::endl;
//loads the ply file
PlyLoader loader;
loader.load( plyFilename );
// put it into the mesh
std::cerr << "- loading the DCEL mesh: " << std::endl;
std::cerr << " + " << loader.verticeCount << " vertices" << std::endl;
mesh.clear();
mesh.getVertices().reserve(loader.verticeCount);
mesh.getFaces().reserve(loader.faceCount);
mesh.getHalfEdges().reserve( 2*(loader.verticeCount+loader.faceCount-2) ); // Euler: V – A + F = 2 --> A = V + F - 2
std::list<Vector3f>::iterator vit = loader.vertices.begin();
while( vit!=loader.vertices.end() )
{
unsigned int id = mesh.createVertex();
mesh.getVertex(id)->getData().position = *vit;
vit++;
}
std::cerr << " + " << loader.faceCount << " faces" << std::endl;
std::list<unsigned int>::iterator fit = loader.faces.begin();
unsigned int count=0;
while( fit!=loader.faces.end() )
{
count++;
unsigned int a = *fit; ++fit;
unsigned int b = *fit; ++fit;
unsigned int c = *fit; ++fit;
unsigned int fid = mesh.createTriangularFace( a, b, c );
if (fid!=-1)
{
mesh.checkFace(fid);
mesh.checkAllFaces();
}
}
std::cerr << " + " << mesh.getNumUnhandledTriangles() << " bad triangles" << std::endl;
mesh.manageUnhandledTriangles();
mesh.checkAllFaces();
std::cerr << "Done!" << std::endl;
}
#endif//PlyImporter_h
+313
View File
@@ -0,0 +1,313 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright Leonardo Fischer 2011 - http://coderender.blogspot.com
//
// Distributed under the licence available in the accompanying file
// LICENCE.txt. Please read it before use this code.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef VECTOR_3_H
#define VECTOR_3_H
#include <cmath>
template <class T>
class Vector3
{
public:
Vector3()
{}
Vector3(T x, T y, T z)
:x(x)
,y(y)
,z(z)
{}
Vector3(const Vector3<T>& v)
:x(v.x)
,y(v.y)
,z(v.z)
{}
Vector3(const Vector3<T>* v)
:x(v->x)
,y(v->y)
,z(v->z)
{}
inline void set(T x, T y, T z)
{
this->x = x;
this->y = y;
this->z = z;
}
inline void set(const Vector3<T>& other)
{
this->x = other.x;
this->y = other.y;
this->z = other.z;
}
inline Vector3<T> operator=(const Vector3<T>& other)
{
this->x = other.x;
this->y = other.y;
this->z = other.z;
return *this;
}
/**
Explicitly, but easily, converts this Vector instance to one of the given type.
Vector3<float> u(0,0,0);
Vector3<double> v = u.cast<double>();
*/
template <class U>
Vector3<U> cast() const
{
return Vector3<U>( (U)x, (U)y, (U)z );
}
inline bool operator==(const Vector3<T>& other) const
{
return this->x == other.x && this->y == other.y && this->z==other.z;
}
inline bool operator!=(const Vector3<T>& other) const
{
return this->x!=other.x || this->y!=other.y || this->z!=other.z;
}
inline Vector3<T> operator+(const Vector3<T>& other) const
{
return Vector3<T>(this->x + other.x, this->y + other.y, this->z + other.z);
}
inline void operator+=(const Vector3<T>& other)
{
this->x+=other.x;
this->y+=other.y;
this->z+=other.z;
}
inline Vector3<T> operator-() const
{
return Vector3<T>(-this->x, -this->y, -this->z);
}
inline Vector3<T> operator-(const Vector3<T>& other) const
{
return Vector3<T>(this->x - other.x, this->y - other.y, this->z - other.z);
}
inline void operator-=(const Vector3<T>& other)
{
this->x-=other.x;
this->y-=other.y;
this->z-=other.z;
}
inline Vector3<T> operator*(T s) const
{
return Vector3<T>(this->x*s, this->y*s, this->z*s);
}
inline void operator*=(T s)
{
this->x*=s;
this->y*=s;
this->z*=s;
}
inline Vector3<T> operator/(T s) const
{
return Vector3<T>(this->x/s, this->y/s, this->z/s);
}
inline void operator/=(T s)
{
this->x/=s;
this->y/=s;
this->z/=s;
}
inline Vector3<T> cross( const Vector3<T>& other ) const
{
return Vector3<T>(
this->y*other.z - this->z*other.y,
this->z*other.x - this->x*other.z,
this->x*other.y - this->y*other.x );
}
inline T dot( const Vector3<T>& other ) const
{
return this->x*other.x + this->y*other.y + this->z*other.z;
}
inline T length() const
{
return( sqrt( this->x*this->x + this->y*this->y + this->z*this->z ) );
}
/**
Returns the square of the length of the vector.
Use this if you just want to compare legths of vectors, as this is faster to
compute than lenght()
*/
inline T length2() const
{
return this->x*this->x + this->y*this->y + this->z*this->z;
}
/**
Resizes the vector.
*/
inline void setLength(const T newLenght)
{
const T scale = ( newLenght / this->length() );
this->x *= scale;
this->y *= scale;
this->z *= scale;
}
inline T distance(const Vector3<T>& other) const
{
const T xDelta = this->x-other.x;
const T yDelta = this->y-other.y;
const T zDelta = this->z-other.z;
return sqrt( xDelta*xDelta + yDelta*yDelta + zDelta*zDelta);
}
/**
Returns the square of the distance to the other vector
Use this if you just want to compare distances, as the square of
the distance is faster to compute than the distance itself.
*/
inline T distance2(const Vector3<T>& other) const
{
const T xDelta = this->x-other.x;
const T yDelta = this->y-other.y;
const T zDelta = this->z-other.z;
return xDelta*xDelta + yDelta*yDelta + zDelta*zDelta;
}
inline void normalize()
{
const T len = this->length();
this->x /= len;
this->y /= len;
this->z /= len;
}
/**
Returns a normalized copy of the vector.
Use if you need a normalized version of the vector AND
the vector itself.
*/
inline Vector3<T> normalizedCopy()
{
Vector3<T> v = this;
const T len = v.length(); //inline normalize
v.x /= len;
v.y /= len;
v.z /= len;
return v;
}
inline void rotateAroundY(T radians)
{
const double cs = cos(radians);
const double sn = sin(radians);
set((T)(x*cs - z*sn), y, (T)(x*sn + z*cs));
}
/**
This method will rotate the point represented by this vector
arround a given axis.
This is the most general form for the methods rotateAroundX,
Y and Z, but is more expensive.
\note the axis MUST be normalized
\note that the axis passes through the point (0,0,0).
Math from http://local.wasp.uwa.edu.au/~pbourke/geometry/rotate/
*/
inline void rotateAround(const Vector3<T>& axis, T radians)
{
T costheta = cos(radians);
T sintheta = sin(radians);
Vector3<T> q;
q.x = (costheta + (1 - costheta) * axis.x * axis.x) * this->x;
q.x += ((1 - costheta) * axis.x * axis.y - axis.z * sintheta) * this->y;
q.x += ((1 - costheta) * axis.x * axis.z + axis.y * sintheta) * this->z;
q.y = ((1 - costheta) * axis.x * axis.y + axis.z * sintheta) * this->x;
q.y += (costheta + (1 - costheta) * axis.y * axis.y) * this->y;
q.y += ((1 - costheta) * axis.y * axis.z - axis.x * sintheta) * this->z;
q.z = ((1 - costheta) * axis.x * axis.z - axis.y * sintheta) * this->x;
q.z += ((1 - costheta) * axis.y * axis.z + axis.x * sintheta) * this->y;
q.z += (costheta + (1 - costheta) * axis.z * axis.z) * this->z;
this->set(q);
}
inline void rotateAround(const Vector3<T>& p1, const Vector3<T>& p2, T radians)
{
Vector3<T> r;
r.x = p2.x - p1.x;
r.y = p2.y - p1.y;
r.z = p2.z - p1.z;
this->x -= p1.x;
this->y -= p1.y;
this->z -= p1.z;
r.normalize();
T costheta, sintheta;
costheta = cos(radians);
sintheta = sin(radians);
Vector3<T> q;
q.x = (costheta + (1 - costheta) * r.x * r.x) * this->x;
q.x += ((1 - costheta) * r.x * r.y - r.z * sintheta) * this->y;
q.x += ((1 - costheta) * r.x * r.z + r.y * sintheta) * this->z;
q.y = ((1 - costheta) * r.x * r.y + r.z * sintheta) * this->x;
q.y += (costheta + (1 - costheta) * r.y * r.y) * this->y;
q.y += ((1 - costheta) * r.y * r.z - r.x * sintheta) * this->z;
q.z = ((1 - costheta) * r.x * r.z - r.y * sintheta) * this->x;
q.z += ((1 - costheta) * r.y * r.z + r.x * sintheta) * this->y;
q.z += (costheta + (1 - costheta) * r.z * r.z) * this->z;
q.x += p1.x;
q.y += p1.y;
q.z += p1.z;
this->set(q);
}
bool isNaN() const
{
return x!=x || y!=y || z!=z;
}
T x;
T y;
T z;
};
typedef Vector3<int> Vector3i;
typedef Vector3<float> Vector3f;
typedef Vector3<double> Vector3d;
#endif//VECTOR_3_H
+60
View File
@@ -0,0 +1,60 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright Leonardo Fischer 2011 - http://coderender.blogspot.com
//
// Distributed under the licence available in the accompanying file
// LICENCE.txt. Please read it before use this code.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef DCEL_Vertex_h
#define DCEL_Vertex_h
//#include <cassert>
template<class VertexDataT, class HalfEdgeDataT, class FaceDataT>
class HalfEdgeT;
template<class VertexDataT, class HalfEdgeDataT, class FaceDataT>
class VertexT
{
typedef VertexT<VertexDataT, HalfEdgeDataT, FaceDataT> Vertex;
typedef HalfEdgeT<VertexDataT, HalfEdgeDataT, FaceDataT> HalfEdge;
public:
VertexT():
data(),
incidentEdge(NULL)
{
};
inline HalfEdge* getIncidentEdge() const
{
//assert(this);
return this->incidentEdge;
};
inline void setIncidentEdge(HalfEdge* edge)
{
//assert(this);
this->incidentEdge = edge;
};
inline VertexDataT& getData()
{
return data;
};
inline const VertexDataT& getData() const
{
return data;
};
protected:
private:
HalfEdge* incidentEdge;
VertexDataT data;
};
#endif//DCEL_Vertex_h
+66
View File
@@ -0,0 +1,66 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright Leonardo Fischer 2011 - http://coderender.blogspot.com
//
// Distributed under the licence available in the accompanying file
// LICENCE.txt. Please read it before use this code.
//
///////////////////////////////////////////////////////////////////////////////
#include "WavefrontObjImporter.h"
#include <fstream>
#include <sstream>
void WavefrontObjLoader::load( const std::string& objFile )
{
vertices.clear();
verticeCount=0;
faces.clear();
faceCount=0;
std::ifstream stream;
try
{
stream.open( objFile.c_str(), std::ios::in );
if( stream )
{
// load the obj
while( stream.good() )
{
char buffer[512];
stream.getline(buffer, sizeof(buffer));
std::stringstream str(buffer);
std::string name;
str >> name;
if( name=="v" )
{
Vector3f position;
str >> position.x >> position.y >> position.z;
vertices.push_back( position );
verticeCount++;
}
else if( name=="f" )
{
unsigned int a, b, c;
str >> a >> b >> c;
faces.push_back(a-1);
faces.push_back(b-1);
faces.push_back(c-1);
faceCount++;
}
}
stream.close();
}
else
{
throw cpp::Exception("Can't open the file '"+objFile+"'");
}
}
catch( const std::exception& e )
{
if( stream.is_open() )
stream.close();
std::cerr << e.what();
throw;
}
}
+107
View File
@@ -0,0 +1,107 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright Leonardo Fischer 2011 - http://coderender.blogspot.com
//
// Distributed under the licence available in the accompanying file
// LICENCE.txt. Please read it before use this code.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef WavefrontObjImporter_h
#define WavefrontObjImporter_h
#include "DCELStream.h"
#include "Vector3.h"
class WavefrontObjLoader
{
public:
void load( const std::string& objFile );
std::list<Vector3f> vertices;
unsigned int verticeCount;
std::list<unsigned int> faces;
unsigned int faceCount;
protected:
private:
};
/**
Class that imports a Wavefront OBJ file into a DCEL mesh.
It assumes that the mesh is totally triangulated.
*/
template <class MeshT>
class WavefrontObjImporter
{
public:
/**
Loads from the given filename.
It just calls the 'import' method with the opened stream.
*/
void import( const std::string& objFile, MeshT& mesh);
void import( std::list<Vector3f>& vertices, unsigned int verticeCount, std::list<unsigned int>& faces, unsigned int faceCount, MeshT& mesh);
};
template <class MeshT>
void WavefrontObjImporter<MeshT>::import( const std::string& objFilename, MeshT& mesh )
{
std::cerr << "Starting importing the file '" << objFilename << "'" << std::endl;
std::cerr << "- loading the OBJ file" << std::endl;
WavefrontObjLoader loader;
loader.load(objFilename);
import(loader.vertices, loader.verticeCount, loader.faces, loader.faceCount, mesh);
}
template <class MeshT>
void WavefrontObjImporter<MeshT>::import( std::list<Vector3f>& vertices, unsigned int verticeCount, std::list<unsigned int>& faces, unsigned int faceCount, MeshT& mesh)
{
// put it into the mesh
std::cerr << "- loading the DCEL mesh: " << std::endl;
std::cerr << " + " << verticeCount << " vertices" << std::endl;
mesh.clear();
mesh.getVertices().reserve(verticeCount);
mesh.getFaces().reserve(faceCount);
//mesh.getEdges().reserve( 2*(verticeCount+faceCount) ); // Euler: V – A + F = 2 --> A = V + F - 2
mesh.getHalfEdges().reserve( 2*(3*faceCount) ); // just a good expensive number of edges =)
std::list<Vector3f>::iterator vit = vertices.begin();
while( vit!=vertices.end() )
{
unsigned int id = mesh.createVertex();
mesh.getVertex(id)->getData().position = *vit;
vit++;
}
std::cerr << " + " << faceCount << " faces" << std::endl;
std::list<unsigned int>::iterator fit = faces.begin();
unsigned int count=0;
while( fit!=faces.end() )
{
count++;
unsigned int a = *fit; ++fit;
unsigned int b = *fit; ++fit;
unsigned int c = *fit; ++fit;
unsigned int fid = mesh.createTriangularFace( a, b, c );
if (fid!=-1)
{
//std::cout << a << ' ' << b << ' ' << c << std::endl;
mesh.checkFace(fid);
mesh.checkAllFaces();
}
}
std::cerr << " + " << mesh.getNumUnhandledTriangles() << " bad triangles" << std::endl;
mesh.manageUnhandledTriangles();
mesh.checkAllFaces();
std::cerr << "Done!" << std::endl;
}
#endif//WavefrontObjImporter_h