Loading...
Searching...
No Matches
surface.h
Go to the documentation of this file.
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#ifndef BALL_MATHS_SURFACE_H
#define BALL_MATHS_SURFACE_H
#ifndef BALL_MATHS_VECTOR3_H
# include <BALL/MATHS/vector3.h>
#endif
namespace BALL
{
template <typename T>
class TSurface
{
public:
BALL_CREATE(TSurface)
class Triangle
{
public:
Index v1;
Index v2;
Index v3;
bool operator == (const Triangle& triangle) const
{
return (v1 == triangle.v1) && (v2 == triangle.v2) && (v3 == triangle.v3);
}
bool operator != (const Triangle& triangle) const
{
return !(v1 == triangle.v1) && (v2 == triangle.v2) && (v3 == triangle.v3);
}
};
typedef TVector3<T> Vertex;
typedef TVector3<T> Normal;
TSurface();
TSurface(const TSurface& surface);
virtual ~TSurface();
void set(const TSurface& surface);
TSurface& operator = (const TSurface& surface);
void get(TSurface& surface) const;
void clear();
void readMSMSFile(const String& vert_filename, const String& face_filename);
float getArea() const;
Size getNumberOfTriangles() const;
Size getNumberOfVertices() const;
Size getNumberOfNormals() const;
Triangle& getTriangle(Position index);
const Triangle& getTriangle(Position index) const;
void clearTriangles();
void resizeTriangles(Size size);
void pushBackTriangle(const Triangle& triangle);
Vertex& getVertex(Position index);
const Vertex& getVertex(Position index) const;
void clearVertices();
void resizeVertices(Size size);
void pushBackVertex(const Vertex& vertex);
Normal& getNormal(Position index);
const Normal& getNormal(Position index) const;
void clearNormals();
void resizeNormals(Size size);
void pushBackNormal(const Normal& n);
bool operator == (const TSurface& surface) const;
bool operator != (const TSurface& surface) const;
vector<Vertex> vertex;
vector<Normal> normal;
vector<Triangle> triangle;
};
#ifdef BALL_COMPILER_MSVC
template class BALL_EXPORT TSurface<float>;
#endif
template <typename T>
TSurface<T>::TSurface()
{
}
template <typename T>
TSurface<T>::TSurface(const TSurface<T>& surface)
: vertex(surface.vertex),
normal(surface.normal),
triangle(surface.triangle)
{
}
template <typename T>
TSurface<T>::~TSurface()
{
}
template <typename T>
void TSurface<T>::clear()
{
vertex.clear();
normal.clear();
triangle.clear();
}
template <typename T>
void TSurface<T>::set(const TSurface<T>& surface)
{
vertex = surface.vertex;
normal = surface.normal;
triangle = surface.triangle;
}
template <typename T>
TSurface<T>& TSurface<T>::operator = (const TSurface<T>& surface)
{
vertex = surface.vertex;
normal = surface.normal;
triangle = surface.triangle;
return *this;
}
template <typename T>
void TSurface<T>::get(TSurface<T>& surface) const
{
surface.vertex = vertex;
surface.normal = normal;
surface.triangle = triangle;
}
template <typename T>
void TSurface<T>::readMSMSFile(const String& vert_filename, const String& face_filename)
{
// delete old contents
normal.clear();
vertex.clear();
triangle.clear();
std::ifstream file(vert_filename.c_str());
if (!file)
{
throw Exception::FileNotFound(__FILE__, __LINE__, vert_filename);
}
// there are two formats: one with three lines of
// header and one without
String line;
while ((line.countFields() != 9) && file)
{
line.getline(file);
}
String s[6];
while (file && (line.countFields() == 9))
{
// read the vertex coordinates and the normal vector
line.split(s, 6);
vertex.push_back(Vertex(s[0].toFloat(), s[1].toFloat(), s[2].toFloat()));
normal.push_back(Normal(s[3].toFloat(), s[4].toFloat(), s[5].toFloat()));
// read the next line
line.getline(file);
}
file.close();
// workaround for trouble in File
file.clear();
// now read the faces file:
file.open(face_filename.c_str());
if (!file)
{
throw Exception::FileNotFound(__FILE__, __LINE__, face_filename);
}
// there are two formats: one with three lines of
// header and one without
while ((line.countFields() != 5) && file)
{
line.getline(file);
}
Triangle t;
Size number_of_vertices = (Size)vertex.size();
while (file && (line.countFields() == 5))
{
// read the vertex indices
line.split(s, 5);
t.v1 = (Index)s[0].toInt() - 1;
t.v2 = (Index)s[1].toInt() - 1;
t.v3 = (Index)s[2].toInt() - 1;
// if all three vertex indices are valid, insert the triangle
if ((t.v1 < (Index)number_of_vertices) && (t.v1 >= 0)
&& (t.v1 < (Index)number_of_vertices) && (t.v1 >= 0)
&& (t.v1 < (Index)number_of_vertices) && (t.v1 >= 0))
{
triangle.push_back(t);
}
// read the next line
line.getline(file);
}
file.close();
}
template <typename T>
float TSurface<T>::getArea() const
{
// add the areas of all triangles
double area = 0;
for (Size i = 0; i < triangle.size(); i++)
{
// add the length of the vector products of two sides of each triangle
// this is equivalent to the surface area of the parallelogram, and thus to twice the triangle area
area += ((vertex[triangle[i].v2] - vertex[triangle[i].v1]) % (vertex[triangle[i].v3] - vertex[triangle[i].v1])).getLength();
}
// A = 1/2 \sum |r1 x r2|
return (float)( area * 0.5 );
}
template <typename T>
bool TSurface<T>::operator == (const TSurface<T>& surface) const
{
return ((surface.vertex == vertex)
&& (surface.normal == normal)
&& (surface.triangle == triangle));
}
template <typename T>
BALL_INLINE
Size TSurface<T>::getNumberOfTriangles() const
{
return (Size)triangle.size();
}
template <typename T>
BALL_INLINE
Size TSurface<T>::getNumberOfVertices() const
{
return (Size)vertex.size();
}
template <typename T>
BALL_INLINE
Size TSurface<T>::getNumberOfNormals() const
{
return (Size)normal.size();
}
template <typename T>
BALL_INLINE
typename TSurface<T>::Triangle& TSurface<T>::getTriangle(Position index)
{
return triangle[index];
}
template <typename T>
BALL_INLINE
const typename TSurface<T>::Triangle& TSurface<T>::getTriangle(Position index) const
{
return triangle[index];
}
template <typename T>
BALL_INLINE
void TSurface<T>::clearTriangles()
{
triangle.clear();
}
template <typename T>
BALL_INLINE
void TSurface<T>::resizeTriangles(Size size)
{
triangle.resize(size);
}
template <typename T>
BALL_INLINE
void TSurface<T>::pushBackTriangle(const Triangle& t)
{
triangle.push_back(t);
}
template <typename T>
BALL_INLINE
typename TSurface<T>::Vertex& TSurface<T>::getVertex(Position index)
{
return vertex[index];
}
template <typename T>
BALL_INLINE
const typename TSurface<T>::Vertex& TSurface<T>::getVertex(Position index) const
{
return vertex[index];
}
template <typename T>
BALL_INLINE
void TSurface<T>::clearVertices()
{
vertex.clear();
}
template <typename T>
BALL_INLINE
void TSurface<T>::resizeVertices(Size size)
{
vertex.resize(size);
}
template <typename T>
BALL_INLINE
void TSurface<T>::pushBackVertex(const typename TSurface<T>::Vertex& position)
{
vertex.push_back(position);
}
template <typename T>
BALL_INLINE
typename TSurface<T>::Normal& TSurface<T>::getNormal(Position index)
{
return normal[index];
}
template <typename T>
BALL_INLINE
const typename TSurface<T>::Normal& TSurface<T>::getNormal(Position index) const
{
return normal[index];
}
template <typename T>
BALL_INLINE
void TSurface<T>::clearNormals()
{
normal.clear();
}
template <typename T>
BALL_INLINE
void TSurface<T>::resizeNormals(Size size)
{
normal.resize(size);
}
template <typename T>
BALL_INLINE
void TSurface<T>::pushBackNormal(const typename TSurface<T>::Normal& n)
{
normal.push_back(n);
}
template <typename T>
bool TSurface<T>::operator != (const TSurface<T>& surface) const
{
return !(*this == surface);
}
typedef TSurface<float> Surface;
} // namespace BALL
#endif // BALL_MATHS_SURFACE_H
BALL_EXPORT
#define BALL_EXPORT
Definition COMMON/global.h:50
BALL::Exception::FileNotFound
Definition COMMON/exception.h:361
BALL::String
Definition string.h:57
BALL::String::countFields
Size countFields(const char *delimiters=CHARACTER_CLASS__WHITESPACE) const
BALL::String::split
Size split(String string_array[], Size array_size, const char *delimiters=CHARACTER_CLASS__WHITESPACE, Index from=0) const
BALL::String::c_str
const char * c_str() const BALL_NOEXCEPT
BALL::String::getline
std::istream & getline(std::istream &s=std::cin, char delimiter='\n')
BALL::TSurface::Triangle
Definition surface.h:40
BALL::TSurface::Triangle::v1
Index v1
Definition surface.h:42
BALL::TSurface::Triangle::v3
Index v3
Definition surface.h:44
BALL::TSurface::Triangle::v2
Index v2
Definition surface.h:43
BALL::TSurface
Definition surface.h:28
BALL::TSurface< float >::normal
vector< Normal > normal
Definition surface.h:188
BALL::TSurface::Vertex
TVector3< T > Vertex
A vertex.
Definition surface.h:59
BALL::TSurface::triangle
vector< Triangle > triangle
the triangles
Definition surface.h:191
BALL::TSurface::getNormal
const Normal & getNormal(Position index) const
Return the position of a normal.
Definition surface.h:453
BALL::TSurface::resizeVertices
void resizeVertices(Size size)
Resize the vertex array.
Definition surface.h:431
BALL::TSurface::pushBackTriangle
void pushBackTriangle(const Triangle &triangle)
Add a triangle.
Definition surface.h:402
BALL::TSurface::pushBackNormal
void pushBackNormal(const Normal &n)
Add a normal.
Definition surface.h:474
BALL::TSurface::getNumberOfTriangles
Size getNumberOfTriangles() const
Return the number of triangles.
Definition surface.h:353
BALL::TSurface::set
void set(const TSurface &surface)
Definition surface.h:228
BALL::TSurface< float >::vertex
vector< Vertex > vertex
Definition surface.h:185
BALL::TSurface::operator=
TSurface & operator=(const TSurface &surface)
Definition surface.h:236
BALL::TSurface::clearTriangles
void clearTriangles()
Clear all triangles.
Definition surface.h:388
BALL::TSurface::getNumberOfNormals
Size getNumberOfNormals() const
Return the number of normals.
Definition surface.h:367
BALL::TSurface::clearVertices
void clearVertices()
Clear all vertices.
Definition surface.h:424
BALL::TSurface::clear
void clear()
Definition surface.h:220
BALL::TSurface::clearNormals
void clearNormals()
Clear all normals.
Definition surface.h:460
BALL::TSurface::getTriangle
Triangle & getTriangle(Position index)
Return a triangle with a given index.
Definition surface.h:374
BALL::TSurface::readMSMSFile
void readMSMSFile(const String &vert_filename, const String &face_filename)
Definition surface.h:253
BALL::TSurface::getVertex
Vertex & getVertex(Position index)
Return the position of a vertex.
Definition surface.h:410
BALL::TSurface::~TSurface
virtual ~TSurface()
Definition surface.h:215
BALL::TSurface::TSurface
TSurface(const TSurface &surface)
Definition surface.h:207
BALL::TSurface::get
void get(TSurface &surface) const
Definition surface.h:245
BALL::TSurface::getNormal
Normal & getNormal(Position index)
Return the position of a normal.
Definition surface.h:446
BALL::TSurface::Normal
TVector3< T > Normal
A normal.
Definition surface.h:62
BALL::TSurface::getVertex
const Vertex & getVertex(Position index) const
Return the position of a vertex.
Definition surface.h:417
BALL::TSurface::getTriangle
const Triangle & getTriangle(Position index) const
Return a triangle with a given index.
Definition surface.h:381
BALL::TSurface::resizeNormals
void resizeNormals(Size size)
Resize the normal array.
Definition surface.h:467
BALL::TSurface::getNumberOfVertices
Size getNumberOfVertices() const
Return the number of vertices.
Definition surface.h:360
BALL::TSurface::TSurface
TSurface()
Definition surface.h:202
BALL::TSurface::operator==
bool operator==(const TSurface &surface) const
Definition surface.h:344
BALL::TSurface::operator!=
bool operator!=(const TSurface &surface) const
Definition surface.h:480
BALL::TSurface::pushBackVertex
void pushBackVertex(const Vertex &vertex)
Add a vertex.
Definition surface.h:439
BALL::TSurface::getArea
float getArea() const
Definition surface.h:328
BALL::TSurface::resizeTriangles
void resizeTriangles(Size size)
Resize the triangle array.
Definition surface.h:395
BALL::TVector3
Definition vector3.h:71
BALL_CREATE
#define BALL_CREATE(name)
Definition create.h:62
BALL_INLINE
#define BALL_INLINE
Definition debug.h:15
BALL::Surface
TSurface< float > Surface
Definition surface.h:488
BALL
Definition constants.h:13
BALL::Size
BALL_SIZE_TYPE Size
Definition COMMON/global.h:114
BALL::Position
BALL_SIZE_TYPE Position
Definition COMMON/global.h:140
BALL::Index
BALL_INDEX_TYPE Index
Definition COMMON/global.h:105
vector3.h