Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows support #16

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 57 additions & 33 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,58 +1,82 @@
# swatbotics/apriltags-cpp/CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
cmake_minimum_required(VERSION 3.5.1)

project(APRILTAGS)

set(CMAKE_CXX_STANDARD 11) # for std::chrono
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})

if(WIN32)
set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/build)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${LIBRARY_OUTPUT_PATH})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${LIBRARY_OUTPUT_PATH})
else()
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
endif()

if(APPLE)
include_directories(/opt/local/include) # MacPorts
link_directories(/opt/local/lib)
find_library(OPENGL_LIBRARY OpenGL)
include_directories(/opt/local/include) # MacPorts
link_directories(/opt/local/lib)
find_library(OPENGL_LIBRARY OpenGL)
else()
find_library(OPENGL_LIBRARY GL)
find_library(GLU_LIBRARY GLU)
set(OPENGL_LIBRARY ${OPENGL_LIBRARY} ${GLU_LIBRARY})
find_library(OPENGL_LIBRARY GL)
find_library(GLU_LIBRARY GLU)
set(OPENGL_LIBRARY ${OPENGL_LIBRARY} ${GLU_LIBRARY})
endif()

find_library(GLUT_LIBRARY glut)

include(FindPkgConfig)

pkg_search_module(OPENCV REQUIRED opencv>=2.3 opencv-2.3.1)
include_directories(${OPENCV_INCLUDE_DIRS})

pkg_check_modules(CAIRO cairo)

if (${CAIRO_FOUND})
add_definitions(-DMZ_HAVE_CAIRO)
endif ()
if(UNIX)
pkg_search_module(OPENCV REQUIRED opencv>=3.4)
include_directories(${OPENCV_INCLUDE_DIRS})

pkg_check_modules(CAIRO cairo)
if(${CAIRO_FOUND})
add_definitions(-DMZ_HAVE_CAIRO)
endif()
else()
find_package(OpenCV REQUIRED)
if(${OpenCV_FOUND})
if( ${OpenCV_VERSION}} VERSION_GREATER "3.4")
include_directories(${OpenCV_INCLUDE_DIRS})
else()
message(FATAL_ERROR "OpenCV found but version too old. current version: " ${OpenCV_FIND_VERSION} ". required: 3.4 or newer.")
endif()
else()
message(FATAL_ERROR "OpenCV not found!")
endif()
endif()

find_package( CGAL QUIET COMPONENTS )

if (CGAL_FOUND)
include( ${CGAL_USE_FILE} )
add_definitions(-DHAVE_CGAL)
find_package( Boost REQUIRED )
if (${CGAL_FOUND})
include( ${CGAL_USE_FILE} )
add_definitions(-DHAVE_CGAL)
find_package( Boost REQUIRED )

if(MSVC)
add_definitions("/EHsc") # To work with boost header only
# otherwise may give linking error
endif(MSVC)
else()
message("CGAL not found, can not use new quad detector")
message("CGAL not found, can not use new quad detector")
endif()

set(CMAKE_C_FLAGS "-Wall -g")
set(CMAKE_CXX_FLAGS "-Wall -g")

if (APPLE OR UNIX)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wsign-compare -frounding-math")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wsign-compare -frounding-math")
endif()
if(UNIX)
set(CMAKE_C_FLAGS "-Wall -g")
set(CMAKE_CXX_FLAGS "-Wall -g")

set(CMAKE_C_FLAGS_DEBUG "-O")
set(CMAKE_CXX_FLAGS_DEBUG "-O")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wsign-compare -frounding-math")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wsign-compare -frounding-math")

set(CMAKE_C_FLAGS_DEBUG "-O")
set(CMAKE_CXX_FLAGS_DEBUG "-O")

set(CMAKE_C_FLAGS_RELEASE "-O2")
set(CMAKE_CXX_FLAGS_RELEASE "-O2")
set(CMAKE_C_FLAGS_RELEASE "-O2")
set(CMAKE_CXX_FLAGS_RELEASE "-O2")
endif()

add_subdirectory(src)

Binary file added images/Tags36h11.pdf
Binary file not shown.
42 changes: 24 additions & 18 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,35 @@ add_library(apriltags
UnionFindSimple.cpp
)

set(AT_LIBS apriltags ${OPENCV_LDFLAGS})

target_link_libraries(apriltags ${AT_LIBS})

add_executable(camtest camtest.cpp)
target_link_libraries(camtest ${AT_LIBS})
target_link_libraries(apriltags ${OPENCV_LDFLAGS})

add_executable(tagtest tagtest.cpp)
target_link_libraries(tagtest ${AT_LIBS})
set_target_properties(apriltags PROPERTIES PREFIX "lib")
set_target_properties(apriltags PROPERTIES DEBUG_POSTFIX "d")

add_executable(quadtest quadtest.cpp)
target_link_libraries(quadtest ${AT_LIBS})
if(WIN32)
target_compile_options(apriltags PRIVATE "$<$<CONFIG:DEBUG>:-O>")
target_compile_options(apriltags PRIVATE "$<$<CONFIG:RELEASE>:-O2>")
endif()

if (GLUT_LIBRARY)
set(AT_LIBS apriltags ${OPENCV_LDFLAGS})

add_executable(gltest gltest.cpp)
target_link_libraries(gltest ${GLUT_LIBRARY} ${OPENGL_LIBRARY} ${AT_LIBS})
if(NOT WIN32)
add_executable(camtest camtest.cpp)
target_link_libraries(camtest ${AT_LIBS})

endif()
add_executable(tagtest tagtest.cpp)
target_link_libraries(tagtest ${AT_LIBS})

if (CAIRO_FOUND)
add_executable(quadtest quadtest.cpp)
target_link_libraries(quadtest ${AT_LIBS})

add_executable(maketags maketags.cpp)
target_link_libraries(maketags ${CAIRO_LIBRARIES} ${AT_LIBS} ${CAIRO_LIBS})
if(GLUT_LIBRARY)
add_executable(gltest gltest.cpp)
target_link_libraries(gltest ${GLUT_LIBRARY} ${OPENGL_LIBRARY} ${AT_LIBS})
endif()

endif()
if(CAIRO_FOUND)
add_executable(maketags maketags.cpp)
target_link_libraries(maketags ${CAIRO_LIBRARIES} ${AT_LIBS} ${CAIRO_LIBS})
endif()
endif()
13 changes: 7 additions & 6 deletions src/MathUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

#include "MathUtil.h"
#include <cmath>
#include <opencv2/core/cvdef.h>

const at::real MathUtil::epsilon = AT_EPSILON;
const at::real MathUtil::twopi_inv = 0.5/M_PI;
const at::real MathUtil::twopi = 2.0*M_PI;
const at::real MathUtil::twopi_inv = 0.5/CV_PI;
const at::real MathUtil::twopi = 2.0*CV_PI;

at::real MathUtil::fabs(at::real f) {
return std::fabs(f);
Expand Down Expand Up @@ -160,12 +161,12 @@ at::real MathUtil::atan2(at::real y, at::real x) {
if (x>=0) {
return atn;
}
return M_PI+atn;
return CV_PI +atn;
}
if (x>=0) {
return atn;
}
return -at::real(M_PI)+atn;
return -at::real(CV_PI)+atn;

}

Expand All @@ -178,9 +179,9 @@ at::real MathUtil::atan(at::real x) {
return atan_mag1(x);
}
if (x < 0) {
return -M_PI/2-atan_mag1(1/x);
return -CV_PI/2-atan_mag1(1/x);
} else {
return M_PI/2-atan_mag1(1/x);
return CV_PI/2-atan_mag1(1/x);
}

}
Expand Down
24 changes: 16 additions & 8 deletions src/Profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#define _PROFILER_H_

#include <map>
#include <sys/time.h>
#include <chrono>

// Lightweight classes/structs used for profiling AprilTags.

Expand Down Expand Up @@ -56,10 +56,17 @@ struct TimingInfo {
typedef std::map<AlgorithmStep, TimingInfo> TimingLookup;

//////////////////////////////////////////////////////////////////////
// Class for profiling. You may need to replace getTimeAsDouble on
// platforms where gettimeofday is not available.
// Class for profiling



class Profiler {
typedef std::chrono::high_resolution_clock Time;
typedef std::chrono::microseconds Ms;

private:
std::chrono::time_point<Time, Ms> init;

public:

TimingLookup timers;
Expand All @@ -69,22 +76,23 @@ class Profiler {

Profiler(): num_iterations(0), num_detections(0) {}

static double getTimeAsDouble() {
struct timeval tp;
gettimeofday(&tp, 0);
return double(tp.tv_sec) + double(tp.tv_usec) * 1e-6;

double getTimeAsDouble() {
auto now = std::chrono::time_point_cast<Ms>(Time::now());
return (now - init).count();
}

void start(int step, int substep, const char* desc) {

init = std::chrono::time_point_cast<Ms>(Time::now());

std::pair<TimingLookup::iterator, bool> info =
timers.insert(std::make_pair(AlgorithmStep(step, substep), TimingInfo()));

TimingLookup::iterator i = info.first;

i->second.start = getTimeAsDouble();
i->second.desc = desc;

}

void end(int step, int substep) {
Expand Down
19 changes: 10 additions & 9 deletions src/TagDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
#include "DebugImage.h"

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <opencv2/core/cvdef.h>
#ifdef HAVE_CGAL
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Polygon_2.h>
Expand All @@ -44,7 +45,7 @@ static const at::real kDefaultSigma = 0;
static const at::real kDefaultSegSigma = 0.8;
static const bool kDefaultSegDecimate = false;
static const at::real kDefaultMinMag = 0.004;
static const at::real kDefaultMaxEdgeCost = 30*M_PI/180;
static const at::real kDefaultMaxEdgeCost = 30*CV_PI/180;
static const at::real kDefaultThetaThresh = 100;
static const at::real kDefaultMagThresh = 1200;
static const at::real kDefaultMinimumLineLength = 4;
Expand All @@ -65,7 +66,7 @@ void TagDetector::reportTimers() {
std::cout << "report averaged over " << prof.num_iterations << " frames with " << prof.num_detections << " detections (" << (double(prof.num_detections)/prof.num_iterations) << " per frame)\n\n";

for (TimingLookup::const_iterator i=prof.timers.begin(); i!=prof.timers.end(); ++i) {
int usec = 1e6 * i->second.run / prof.num_iterations;
int usec = i->second.run / prof.num_iterations;
std::cout << std::setw(12) << usec << " usec";
if (i->first.step) { std::cout << " "; }
if (i->first.substep) { std::cout << " "; }
Expand Down Expand Up @@ -247,7 +248,7 @@ TagDetector::TagDetector(const TagFamily& f,

at::real TagDetector::arctan2(at::real y, at::real x) {

at::real coeff_1 = at::real(M_PI/4);
at::real coeff_1 = at::real(CV_PI/4);
at::real coeff_2 = 3*coeff_1;
at::real abs_y = fabs(y)+MathUtil::epsilon; // kludge to prevent 0/0 condition

Expand Down Expand Up @@ -1091,9 +1092,9 @@ void TagDetector::getQuads_AT(const Images& images,
at::real tminab = std::min(tmina, tminb + bshift);
at::real tmaxab = std::max(tmaxa, tmaxb + bshift);

if (tmaxab - tminab > 2*M_PI) {
if (tmaxab - tminab > 2*CV_PI) {
// corner case that's probably not useful to handle correctly. oh well.
tmaxab = tminab + 2*M_PI;
tmaxab = tminab + 2*CV_PI;
}

at::real mmaxab = std::max(mmax[ida], mmax[idb]);
Expand Down Expand Up @@ -1244,7 +1245,7 @@ void TagDetector::getQuads_AT(const Images& images,
}

if (flip > noflip) {
seg->theta += M_PI;
seg->theta += CV_PI;
}

at::real dot = dx*cos(seg->theta) + dy*sin(seg->theta);
Expand Down Expand Up @@ -2006,8 +2007,8 @@ bool TagDetector::decodeQuad(const Images& images,
d.hxy = quad.opticalCenter;

if (true) {
at::real c = cos(d.rotation*M_PI/2.0);
at::real s = sin(d.rotation*M_PI/2.0);
at::real c = cos(d.rotation*CV_PI/2.0);
at::real s = sin(d.rotation*CV_PI/2.0);
at::real R[9] = {
c, -s, 0,
s, c, 0,
Expand Down