Skip to content

Commit

Permalink
Use standard-library logging for verbose logging (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
EpicWink authored Oct 5, 2024
1 parent d70abbf commit 6de5286
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 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: 18 additions & 3 deletions pyfqmr/Simplify.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ _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:
from logging import getLogger

getLogger("pyfqmr").debug(message.decode("utf-8"))

cdef class Simplify :

cdef int[:,:] faces_mv
Expand Down Expand Up @@ -143,15 +149,24 @@ 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:
print('simplified mesh in {} seconds from {} to {} triangles'.format(
from logging import getLogger

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

Expand Down
3 changes: 3 additions & 0 deletions tests/test_simplify.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from logging import root as root_logger, DEBUG
from pathlib import Path

import pytest
import pyfqmr

import numpy as np

root_logger.setLevel(DEBUG)

# Get the /example folder at the root of this repo
EXAMPLES_DIR = Path(__file__, "..", "..", "example").resolve()

Expand Down

0 comments on commit 6de5286

Please sign in to comment.