Skip to content

Commit

Permalink
Added segmentation algorithms
Browse files Browse the repository at this point in the history
Unusable and hard-coded for now. General idea is to define regions of
planarity among meshes and store these regions separately. This shall be
used for removing planar parts of an arbitrary mesh.

Ideas so far:

* Use RMS curvature
* Identify "almost" planar vertices
* _Try_ to identify a region containing these vertices
* Make this region as large as possible

This requires severe changes in the mesh class. Namely, it should be
possible to _copy_ parts of the mesh -- not a great idea as the vertex
indices might change.
  • Loading branch information
Pseudomanifold committed Feb 18, 2011
1 parent 560b128 commit eea9d53
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 2 deletions.
10 changes: 8 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
ADD_SUBDIRECTORY(SubdivisionAlgorithms)
ADD_SUBDIRECTORY(TriangulationAlgorithms)
ADD_SUBDIRECTORY(FairingAlgorithms)
ADD_SUBDIRECTORY(SegmentationAlgorithms)

ADD_SUBDIRECTORY(Tests)

INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR} SubdivisionAlgorithms TriangulationAlgorithms FairingAlgorithms external)
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}
SubdivisionAlgorithms
TriangulationAlgorithms
FairingAlgorithms
SegmentationAlgorithms
external)

# Build psalm command-line interface; the target `psalm_cli` will be renamed
# later on. Otherwise, name clashes with libpsalm would occur.
Expand All @@ -31,7 +37,7 @@ SET(PSALM_SRC
)

ADD_EXECUTABLE(psalm_cli ${PSALM_SRC})
TARGET_LINK_LIBRARIES(psalm_cli SubdivisionAlgorithms FairingAlgorithms ${Boost_LIBRARIES})
TARGET_LINK_LIBRARIES(psalm_cli SubdivisionAlgorithms FairingAlgorithms SegmentationAlgorithms ${Boost_LIBRARIES})
SET_TARGET_PROPERTIES(psalm_cli PROPERTIES OUTPUT_NAME psalm)

# Build `libpsalm` _without_ relying on any libraries; this makes inclusion of
Expand Down
7 changes: 7 additions & 0 deletions SegmentationAlgorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SET( SEGMENTATION_ALGORITHMS_SRC
SegmentationAlgorithm.cpp
PlanarSegmentation.cpp
)

ADD_LIBRARY(SegmentationAlgorithms SHARED ${SEGMENTATION_ALGORITHMS_SRC})
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR})
75 changes: 75 additions & 0 deletions SegmentationAlgorithms/PlanarSegmentation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*!
* @file PlanarSegmentation.cpp
* @brief Functions for the planar segmentation algorithm
*/

#include "PlanarSegmentation.h"

namespace psalm
{

/*!
* Performs a planar segmentation to the input mesh: Planar vertices are
* identified by calculating the discrete mean curvature of a vertex. If
* all planar vertices have been identified, edges leading to planar
* vertices will be inserted into a new, which is returned as the
* function's result.
*
* @param input_mesh Mesh for which the planar segmentation shall be
* performed
*
* @return Segmented mesh
*/

mesh PlanarSegmentation::apply_to(mesh& input_mesh)
{
mesh res;

/*
* FIXME: This assumes that the vertex IDs are numbered
* sequentially. If this is not the case, the function will
* fail...
*/

// Identify planar vertices

size_t n = input_mesh.num_vertices();
bool* is_planar = new bool[n];

for(size_t i = 0; i < n; i++)
{
vertex* v = input_mesh.get_vertex(i);
double curvature = v->calc_mean_curvature();

if(curvature < 0.005)
is_planar[i] = true;
else
is_planar[i] = false;
}

for(size_t i = 0; i < input_mesh.num_faces(); i++)
{
face* f = input_mesh.get_face(i);

size_t non_planar = 0;
for(size_t j = 0; j < f->num_vertices(); j++)
{
if(!is_planar[f->get_vertex(j)->get_id()])
non_planar++;
}

if(non_planar > 1)
continue;

vertex* v1 = res.add_vertex(f->get_vertex(0)->get_position());
vertex* v2 = res.add_vertex(f->get_vertex(1)->get_position());
vertex* v3 = res.add_vertex(f->get_vertex(2)->get_position());

res.add_face(v1,v2,v3);
}

delete[] is_planar;
return(res);
}

} // end of namespace "psalm"
28 changes: 28 additions & 0 deletions SegmentationAlgorithms/PlanarSegmentation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*!
* @file PlanarSegmentation.h
* @brief Class describing a planar segmentation algorithm
*/

#ifndef __PLANAR_SEGMENTATION_H__
#define __PLANAR_SEGMENTATION_H__

#include "SegmentationAlgorithm.h"

namespace psalm
{

/*!
* @class PlanarSegmentation
* @brief Describes algorithm for performing a planar segmentation of an
* input mesh
*/

class PlanarSegmentation : public SegmentationAlgorithm
{
public:
mesh apply_to(mesh& input_mesh);
};

} // end of namespace "psalm"

#endif
19 changes: 19 additions & 0 deletions SegmentationAlgorithms/SegmentationAlgorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*!
* @file SegmentationAlgorithm.cpp
* @brief Implementation of generic functions for segmentation algorithms
*/

#include "SegmentationAlgorithm.h"

namespace psalm
{

/*!
* Empty constructor
*/

SegmentationAlgorithm::SegmentationAlgorithm()
{
}

} // end of namespace "libpsalm"
29 changes: 29 additions & 0 deletions SegmentationAlgorithms/SegmentationAlgorithm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*!
* @file SegmentationAlgorithm.h
* @brief Generic (abstract) class for segmentation algorithms
*/

#ifndef __SEGMENTATION_ALGORITHM_H__
#define __SEGMENTATION_ALGORIOTM_H__

#include "mesh.h"

namespace psalm
{

/*!
* @class SegmentationAlgorithm
* @brief Generic segmentation algorithm class
*/

class SegmentationAlgorithm
{
public:
SegmentationAlgorithm();

virtual mesh apply_to(mesh& input_mesh) = 0;
};

} // end of namespace "psalm"

#endif
5 changes: 5 additions & 0 deletions psalm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

#include "FairingAlgorithms/CurvatureFlow.h"

#include "SegmentationAlgorithms/PlanarSegmentation.h"

#include "mesh.h"

psalm::mesh scene_mesh;
Expand Down Expand Up @@ -483,6 +485,9 @@ int main(int argc, char* argv[])
if(fairing_algorithm)
fairing_algorithm->apply_to(scene_mesh);

psalm::PlanarSegmentation segmentation_algorithm;
segmentation_algorithm.apply_to(scene_mesh).save("test.ply");

scene_mesh.prune(remove_faces, remove_vertices);

// If an output file has been set (even if it is empty), it
Expand Down

0 comments on commit eea9d53

Please sign in to comment.