Skip to content

Commit

Permalink
Pass logging callback to Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
EpicWink committed Sep 30, 2024
1 parent 3a5c7a6 commit 09509aa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
8 changes: 5 additions & 3 deletions pyfqmr/Simplify.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ namespace Simplify
//

void simplify_mesh(int target_count, int update_rate=5, double agressiveness=7,
bool verbose=false, int max_iterations=100, double alpha = 0.000000001,
void (*log)(char*, int)=NULL, int max_iterations=100, double alpha = 0.000000001,
int K = 3, bool lossless=false, double threshold_lossless = 0.0001,
bool preserve_border = false)
{
Expand Down Expand Up @@ -381,8 +381,10 @@ namespace Simplify
if(lossless) threshold = threshold_lossless ;

// target number of triangles reached ? Then break
if ((verbose) && (iteration%5==0)) {
printf("iteration %d - triangles %d threshold %g\n",iteration,triangle_count-deleted_triangles, threshold);
if ((log) && (iteration%5==0)) {
char message[128];
snprintf(message, 127, "iteration %d - triangles %d threshold %g",iteration,triangle_count-deleted_triangles, threshold);
log(message, 128);
}

// remove vertices & mark deleted triangles
Expand Down
21 changes: 16 additions & 5 deletions pyfqmr/Simplify.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ cimport cython
from libcpp.vector cimport vector
from libcpp cimport bool

from logging import getLogger
from time import time as _time

cimport numpy as np
import numpy as np

logger = getLogger("pyfqmr")

class _hidden_ref(object):
"""Hidden Python object to keep a reference to our numpy arrays
"""
Expand All @@ -21,13 +24,17 @@ _REF = _hidden_ref()

cdef extern from "Simplify.h" namespace "Simplify" :
void simplify_mesh( int target_count, int update_rate, double aggressiveness,
bool verbose, int max_iterations,double alpha, int K,
void (*log)(char*, int), int max_iterations,double alpha, int K,
bool lossless, double threshold_lossless, bool preserve_border)
void setMeshFromExt(vector[vector[double]] vertices, vector[vector[int]] faces)
vector[vector[int]] getFaces()
vector[vector[double]] getVertices()
vector[vector[double]] getNormals()

cdef void log_message(char* message, int length) noexcept:
if message is not NULL:
logger.debug(message.decode("utf-8"))

cdef class Simplify :

cdef int[:,:] faces_mv
Expand Down Expand Up @@ -143,17 +150,21 @@ cdef class Simplify :
----
threshold = alpha*pow( iteration + K, agressiveness)
"""

cdef void (*log)(char*, int) noexcept

log = NULL
if verbose:
log = log_message

N_start = self.faces_mv.shape[0]
t_start = _time()
simplify_mesh(target_count, update_rate, aggressiveness, verbose, max_iterations, alpha, K,
simplify_mesh(target_count, update_rate, aggressiveness, log, max_iterations, alpha, K,
lossless, threshold_lossless, preserve_border)
t_end = _time()
N_end = getFaces().size()

if verbose:
from logging import getLogger

logger = getLogger("pyfqmr")
logger.debug('simplified mesh in {} seconds from {} to {} triangles'.format(
round(t_end-t_start,4), N_start, N_end)
)
Expand Down

0 comments on commit 09509aa

Please sign in to comment.