New features: camera from directory of images, new controls to move across frames of a video or a directory of images.

git-svn-id: http://find-object.googlecode.com/svn/trunk/find_object@110 620bd6b2-0a58-f614-fd9a-1bd335dccda9
This commit is contained in:
matlabbe 2012-08-28 20:06:43 +00:00
parent e3b15a7106
commit dfb4e5038d
18 changed files with 1753 additions and 152 deletions

View File

@ -45,6 +45,8 @@ SET(SRC_FILES
../src/ObjWidget.cpp
../src/AboutDialog.cpp
../src/utilite/UPlot.cpp
../src/utilite/UDirectory.cpp
../src/utilite/UFile.cpp
../src/rtabmap/PdfPlot.cpp
${moc_srcs}
${moc_uis}

View File

@ -14,6 +14,8 @@ SET(SRC_FILES
../src/QtOpenCV.cpp
../src/Settings.cpp
../src/Camera.cpp
../src/utilite/UDirectory.cpp
../src/utilite/UFile.cpp
${moc_srcs}
)

View File

@ -7,9 +7,11 @@
#include <opencv2/imgproc/imgproc.hpp>
#include "Settings.h"
#include <QtCore/QFile>
#include "utilite/UDirectory.h"
Camera::Camera(QObject * parent) :
QObject(parent)
QObject(parent),
currentImageIndex_(0)
{
qRegisterMetaType<cv::Mat>("cv::Mat");
connect(&cameraTimer_, SIGNAL(timeout()), this, SLOT(takeImage()));
@ -24,6 +26,8 @@ void Camera::stop()
{
stopTimer();
capture_.release();
images_.clear();
currentImageIndex_ = 0;
}
void Camera::pause()
@ -31,50 +35,127 @@ void Camera::pause()
stopTimer();
}
int Camera::getTotalFrames()
{
if(images_.size())
{
return images_.size();
}
else if(capture_.isOpened())
{
return (int)capture_.get(CV_CAP_PROP_FRAME_COUNT);
}
return 0;
}
int Camera::getCurrentFrameIndex()
{
if(images_.size())
{
return currentImageIndex_;
}
else if(capture_.isOpened())
{
return (int)capture_.get(CV_CAP_PROP_POS_FRAMES);
}
return 0;
}
void Camera::moveToFrame(int frame)
{
printf("Moved to frame %d.\n", frame);
if(frame < images_.size())
{
currentImageIndex_ = frame;
}
else if(capture_.isOpened() && frame < (int)capture_.get(CV_CAP_PROP_FRAME_COUNT))
{
capture_.set(CV_CAP_PROP_POS_FRAMES, frame);
}
}
void Camera::takeImage()
{
cv::Mat img;
if(capture_.isOpened())
{
cv::Mat img;
capture_.read(img);// capture a frame
if(img.empty())
}
else if(!images_.empty())
{
if(currentImageIndex_ < images_.size())
{
printf("Camera: Could not grab a frame, the end of the feed may be reached...\n");
img = cv::imread(images_[currentImageIndex_++]);
}
}
if(img.empty())
{
printf("Camera: Could not grab a frame, the end of the feed may be reached...\n");
}
else
{
//resize
if( Settings::getCamera_2imageWidth() &&
Settings::getCamera_3imageHeight() &&
Settings::getCamera_2imageWidth() != img.cols &&
Settings::getCamera_3imageHeight() != img.rows)
{
cv::Mat resampled;
cv::resize(img, resampled, cv::Size(Settings::getCamera_2imageWidth(), Settings::getCamera_3imageHeight()));
emit imageReceived(resampled);
}
else if(capture_.isOpened())
{
emit imageReceived(img.clone()); // clone required with VideoCapture::read()
}
else
{
//resize
if( Settings::getCamera_2imageWidth() &&
Settings::getCamera_3imageHeight() &&
Settings::getCamera_2imageWidth() != img.cols &&
Settings::getCamera_3imageHeight() != img.rows)
{
cv::Mat resampled;
cv::resize(img, resampled, cv::Size(Settings::getCamera_2imageWidth(), Settings::getCamera_3imageHeight()));
emit imageReceived(resampled);
}
else
{
emit imageReceived(img.clone()); // clone required
}
emit imageReceived(img); // clone not required with cv::imread()
}
}
}
bool Camera::start()
{
if(!capture_.isOpened())
if(!capture_.isOpened() && images_.empty())
{
QString videoFile = Settings::getCamera_5videoFilePath();
if(!videoFile.isEmpty())
QString path = Settings::getCamera_5mediaPath();
if(UDirectory::exists(path.toStdString()))
{
capture_.open(videoFile.toStdString().c_str());
if(!capture_.isOpened())
//Images directory
QString ext = Settings::getGeneral_imageFormats();
ext.remove('*');
ext.remove('.');
UDirectory dir(path.toStdString(), ext.toStdString()); // this will load fileNames matching the extensions (in natural order)
const std::list<std::string> & fileNames = dir.getFileNames();
currentImageIndex_ = 0;
images_.clear();
// Modify to have full path
for(std::list<std::string>::const_iterator iter = fileNames.begin(); iter!=fileNames.end(); ++iter)
{
printf("WARNING: Cannot open file \"%s\". If you want to disable loading automatically this video file, clear the Camera/videoFilePath parameter. By default, webcam will be used instead of the file.\n", videoFile.toStdString().c_str());
images_.append(path.toStdString() + UDirectory::separator() + *iter);
}
printf("Loaded %d filenames.\n", (int)images_.size());
if(images_.isEmpty())
{
printf("WARNING: Directory \"%s\" is empty (no images matching the \"%s\" extensions). "
"If you want to disable loading automatically this directory, "
"clear the Camera/mediaPath parameter. By default, webcam will be used instead of the directory.\n",
path.toStdString().c_str(),
ext.toStdString().c_str());
}
}
if(!capture_.isOpened())
else if(!path.isEmpty())
{
//Video file
capture_.open(path.toStdString().c_str());
if(!capture_.isOpened())
{
printf("WARNING: Cannot open file \"%s\". If you want to disable loading automatically this video file, clear the Camera/mediaPath parameter. By default, webcam will be used instead of the file.\n", path.toStdString().c_str());
}
}
if(!capture_.isOpened() && images_.empty())
{
//set camera device
capture_.open(Settings::getCamera_1deviceId());
@ -85,7 +166,7 @@ bool Camera::start()
}
}
}
if(!capture_.isOpened())
if(!capture_.isOpened() && images_.empty())
{
printf("Failed to open a capture object!\n");
return false;
@ -110,7 +191,7 @@ void Camera::updateImageRate()
{
if(Settings::getCamera_4imageRate())
{
cameraTimer_.setInterval(1000/Settings::getCamera_4imageRate());
cameraTimer_.setInterval((int)(1000.0/Settings::getCamera_4imageRate()));
}
else
{

View File

@ -20,14 +20,15 @@ public:
virtual bool isRunning() {return cameraTimer_.isActive();}
void pause();
int getTotalFrames();
int getCurrentFrameIndex();
void moveToFrame(int frame);
signals:
void imageReceived(const cv::Mat & image);
public slots:
virtual void updateImageRate();
private slots:
virtual void takeImage();
protected:
@ -37,6 +38,8 @@ protected:
private:
cv::VideoCapture capture_;
QTimer cameraTimer_;
QList<std::string> images_;
unsigned int currentImageIndex_;
};
#endif /* CAMERA_H_ */

View File

@ -33,6 +33,8 @@
#include <QtGui/QCloseEvent>
#include <QtGui/QCheckBox>
#include "utilite/UDirectory.h"
// Camera ownership transferred
MainWindow::MainWindow(Camera * camera, QWidget * parent) :
QMainWindow(parent),
@ -61,6 +63,7 @@ MainWindow::MainWindow(Camera * camera, QWidget * parent) :
ui_->dockWidget_parameters->setVisible(false);
ui_->dockWidget_plot->setVisible(false);
ui_->widget_controls->setVisible(false);
QByteArray geometry;
QByteArray state;
@ -69,7 +72,7 @@ MainWindow::MainWindow(Camera * camera, QWidget * parent) :
this->restoreState(state);
ui_->toolBox->setupUi();
connect((QSpinBox*)ui_->toolBox->getParameterWidget(Settings::kCamera_4imageRate()),
connect((QDoubleSpinBox*)ui_->toolBox->getParameterWidget(Settings::kCamera_4imageRate()),
SIGNAL(editingFinished()),
camera_,
SLOT(updateImageRate()));
@ -86,6 +89,12 @@ MainWindow::MainWindow(Camera * camera, QWidget * parent) :
this,
SLOT(updateMirrorView()));
ui_->widget_controls->setVisible(Settings::getGeneral_controlsShown());
connect((QCheckBox*)ui_->toolBox->getParameterWidget(Settings::kGeneral_controlsShown()),
SIGNAL(stateChanged(int)),
this,
SLOT(showHideControls()));
//buttons
connect(ui_->pushButton_restoreDefaults, SIGNAL(clicked()), ui_->toolBox, SLOT(resetCurrentPage()));
connect(ui_->pushButton_updateObjects, SIGNAL(clicked()), this, SLOT(updateObjects()));
@ -106,10 +115,21 @@ MainWindow::MainWindow(Camera * camera, QWidget * parent) :
connect(ui_->actionSave_objects, SIGNAL(triggered()), this, SLOT(saveObjects()));
connect(ui_->actionLoad_objects, SIGNAL(triggered()), this, SLOT(loadObjects()));
connect(ui_->actionCamera_from_video_file, SIGNAL(triggered()), this, SLOT(setupCameraFromVideoFile()));
connect(ui_->actionCamera_from_directory_of_images, SIGNAL(triggered()), this, SLOT(setupCameraFromImagesDirectory()));
connect(ui_->actionAbout, SIGNAL(triggered()), aboutDialog_ , SLOT(exec()));
connect(ui_->actionRestore_all_default_settings, SIGNAL(triggered()), ui_->toolBox, SLOT(resetAllPages()));
connect(ui_->actionRemove_all_objects, SIGNAL(triggered()), this, SLOT(removeAllObjects()));
connect(ui_->pushButton_play, SIGNAL(clicked()), this, SLOT(startProcessing()));
connect(ui_->pushButton_stop, SIGNAL(clicked()), this, SLOT(stopProcessing()));
connect(ui_->pushButton_pause, SIGNAL(clicked()), this, SLOT(pauseProcessing()));
connect(ui_->horizontalSlider_frames, SIGNAL(valueChanged(int)), this, SLOT(moveCameraFrame(int)));
connect(ui_->horizontalSlider_frames, SIGNAL(valueChanged(int)), ui_->label_frame, SLOT(setNum(int)));
ui_->pushButton_play->setVisible(true);
ui_->pushButton_pause->setVisible(false);
ui_->pushButton_stop->setEnabled(true);
ui_->horizontalSlider_frames->setEnabled(false);
ui_->objects_area->addAction(ui_->actionAdd_object_from_scene);
ui_->objects_area->addAction(ui_->actionAdd_objects_from_files);
ui_->objects_area->setContextMenuPolicy(Qt::ActionsContextMenu);
@ -117,8 +137,8 @@ MainWindow::MainWindow(Camera * camera, QWidget * parent) :
ui_->actionStart_camera->setShortcut(Qt::Key_Space);
ui_->actionPause_camera->setShortcut(Qt::Key_Space);
ui_->actionCamera_from_video_file->setCheckable(true);
ui_->actionCamera_from_video_file->setChecked(!Settings::getCamera_5videoFilePath().isEmpty());
ui_->actionCamera_from_video_file->setChecked(!Settings::getCamera_5mediaPath().isEmpty() && !UDirectory::exists(Settings::getCamera_5mediaPath().toStdString()));
ui_->actionCamera_from_directory_of_images->setChecked(!Settings::getCamera_5mediaPath().isEmpty() && UDirectory::exists(Settings::getCamera_5mediaPath().toStdString()));
if(Settings::getGeneral_autoStartCamera())
{
@ -294,6 +314,11 @@ void MainWindow::updateMirrorView()
}
}
void MainWindow::showHideControls()
{
ui_->widget_controls->setVisible(Settings::getGeneral_controlsShown());
}
void MainWindow::addObjectFromScene()
{
disconnect(camera_, SIGNAL(imageReceived(const cv::Mat &)), this, SLOT(update(const cv::Mat &)));
@ -407,24 +432,54 @@ void MainWindow::setupCameraFromVideoFile()
{
if(!ui_->actionCamera_from_video_file->isChecked())
{
Settings::setCamera_5videoFilePath("");
ui_->toolBox->updateParameter(Settings::kCamera_5videoFilePath());
Settings::setCamera_5mediaPath("");
ui_->toolBox->updateParameter(Settings::kCamera_5mediaPath());
}
else
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Setup camera from video file..."), Settings::workingDirectory(), tr("Video Files (%1)").arg(Settings::getGeneral_videoFormats()));
if(!fileName.isEmpty())
{
Settings::setCamera_5videoFilePath(fileName);
ui_->toolBox->updateParameter(Settings::kCamera_5videoFilePath());
Settings::setCamera_5mediaPath(fileName);
ui_->toolBox->updateParameter(Settings::kCamera_5mediaPath());
if(camera_->isRunning())
{
this->stopProcessing();
this->startProcessing();
}
Settings::setGeneral_controlsShown(true);
ui_->toolBox->updateParameter(Settings::kGeneral_controlsShown());
}
}
ui_->actionCamera_from_video_file->setChecked(!Settings::getCamera_5videoFilePath().isEmpty());
ui_->actionCamera_from_video_file->setChecked(!Settings::getCamera_5mediaPath().isEmpty());
ui_->actionCamera_from_directory_of_images->setChecked(false);
}
void MainWindow::setupCameraFromImagesDirectory()
{
if(!ui_->actionCamera_from_directory_of_images->isChecked())
{
Settings::setCamera_5mediaPath("");
ui_->toolBox->updateParameter(Settings::kCamera_5mediaPath());
}
else
{
QString directory = QFileDialog::getExistingDirectory(this, tr("Setup camera from directory of images..."), Settings::workingDirectory());
if(!directory.isEmpty())
{
Settings::setCamera_5mediaPath(directory);
ui_->toolBox->updateParameter(Settings::kCamera_5mediaPath());
if(camera_->isRunning())
{
this->stopProcessing();
this->startProcessing();
}
Settings::setGeneral_controlsShown(true);
ui_->toolBox->updateParameter(Settings::kGeneral_controlsShown());
}
}
ui_->actionCamera_from_directory_of_images->setChecked(!Settings::getCamera_5mediaPath().isEmpty());
ui_->actionCamera_from_video_file->setChecked(false);
}
void MainWindow::showObject(ObjWidget * obj)
@ -543,33 +598,36 @@ void MainWindow::updateData()
// Get the total size and verify descriptors
for(int i=0; i<objects_.size(); ++i)
{
if(dim >= 0 && objects_.at(i)->descriptors().cols != dim)
if(!objects_.at(i)->descriptors().empty())
{
if(this->isVisible())
if(dim >= 0 && objects_.at(i)->descriptors().cols != dim)
{
QMessageBox::critical(this, tr("Error"), tr("Descriptors of the objects are not all the same size!\nObjects opened must have all the same size (and from the same descriptor extractor)."));
if(this->isVisible())
{
QMessageBox::critical(this, tr("Error"), tr("Descriptors of the objects are not all the same size!\nObjects opened must have all the same size (and from the same descriptor extractor)."));
}
else
{
printf("ERROR: Descriptors of the objects are not all the same size! Objects opened must have all the same size (and from the same descriptor extractor).\n");
}
return;
}
else
dim = objects_.at(i)->descriptors().cols;
if(type >= 0 && objects_.at(i)->descriptors().type() != type)
{
printf("ERROR: Descriptors of the objects are not all the same size! Objects opened must have all the same size (and from the same descriptor extractor).\n");
if(this->isVisible())
{
QMessageBox::critical(this, tr("Error"), tr("Descriptors of the objects are not all the same type!\nObjects opened must have been processed by the same descriptor extractor."));
}
else
{
printf("ERROR: Descriptors of the objects are not all the same type! Objects opened must have been processed by the same descriptor extractor.\n");
}
return;
}
return;
type = objects_.at(i)->descriptors().type();
count += objects_.at(i)->descriptors().rows;
}
dim = objects_.at(i)->descriptors().cols;
if(type >= 0 && objects_.at(i)->descriptors().type() != type)
{
if(this->isVisible())
{
QMessageBox::critical(this, tr("Error"), tr("Descriptors of the objects are not all the same type!\nObjects opened must have been processed by the same descriptor extractor."));
}
else
{
printf("ERROR: Descriptors of the objects are not all the same type! Objects opened must have been processed by the same descriptor extractor.\n");
}
return;
}
type = objects_.at(i)->descriptors().type();
count += objects_.at(i)->descriptors().rows;
}
// Copy data
@ -619,6 +677,19 @@ void MainWindow::startProcessing()
ui_->actionStart_camera->setEnabled(false);
ui_->actionLoad_scene_from_file->setEnabled(false);
ui_->label_timeRefreshRate->setVisible(true);
//update control bar
ui_->pushButton_play->setVisible(false);
ui_->pushButton_pause->setVisible(true);
ui_->pushButton_stop->setEnabled(true);
int totalFrames = camera_->getTotalFrames();
if(totalFrames)
{
ui_->label_frame->setVisible(true);
ui_->horizontalSlider_frames->setEnabled(true);
ui_->horizontalSlider_frames->setMaximum(totalFrames-1);
}
if(updateStatusMessage)
{
this->statusBar()->showMessage(tr("Camera started."), 2000);
@ -652,6 +723,12 @@ void MainWindow::stopProcessing()
ui_->actionPause_camera->setEnabled(false);
ui_->actionStart_camera->setEnabled(true);
ui_->actionLoad_scene_from_file->setEnabled(true);
ui_->pushButton_play->setVisible(true);
ui_->pushButton_pause->setVisible(false);
ui_->pushButton_stop->setEnabled(false);
ui_->horizontalSlider_frames->setEnabled(false);
ui_->horizontalSlider_frames->setValue(0);
ui_->label_frame->setVisible(false);
}
void MainWindow::pauseProcessing()
@ -661,17 +738,34 @@ void MainWindow::pauseProcessing()
ui_->actionStart_camera->setEnabled(false);
if(camera_->isRunning())
{
ui_->pushButton_play->setVisible(true);
ui_->pushButton_pause->setVisible(false);
camera_->pause();
}
else
{
ui_->pushButton_play->setVisible(false);
ui_->pushButton_pause->setVisible(true);
camera_->start();
}
}
void MainWindow::moveCameraFrame(int frame)
{
if(ui_->horizontalSlider_frames->isEnabled())
{
camera_->moveToFrame(frame);
if(!camera_->isRunning())
{
camera_->takeImage();
}
}
}
void MainWindow::update(const cv::Mat & image)
{
updateRate_.start();
QTime totalTime;
totalTime.start();
// reset objects color
for(int i=0; i<objects_.size(); ++i)
{
@ -990,9 +1084,17 @@ void MainWindow::update(const cv::Mat & image)
}
ui_->label_detectorDescriptorType->setText(QString("%1/%2").arg(Settings::currentDetectorType()).arg(Settings::currentDescriptorType()));
int ms = updateRate_.restart();
ui_->label_timeTotal->setNum(ms);
int refreshRate = qRound(1000.0f/float(ms));
//update slider
if(ui_->horizontalSlider_frames->isEnabled())
{
ui_->horizontalSlider_frames->blockSignals(true);
ui_->horizontalSlider_frames->setValue(camera_->getCurrentFrameIndex()-1);
ui_->label_frame->setNum(camera_->getCurrentFrameIndex()-1);
ui_->horizontalSlider_frames->blockSignals(false);
}
ui_->label_timeTotal->setNum(totalTime.elapsed());
int refreshRate = qRound(1000.0f/float(updateRate_.restart()));
if(refreshRate > 0 && refreshRate < lowestRefreshRate_)
{
lowestRefreshRate_ = refreshRate;
@ -1000,7 +1102,7 @@ void MainWindow::update(const cv::Mat & image)
// Refresh the label only after each 1000 ms
if(refreshStartTime_.elapsed() > 1000)
{
if(Settings::getCamera_4imageRate()>0)
if(Settings::getCamera_4imageRate()>0.0)
{
ui_->label_timeRefreshRate->setText(QString("(%1 Hz - %2 Hz)").arg(QString::number(Settings::getCamera_4imageRate())).arg(QString::number(lowestRefreshRate_)));
}
@ -1030,5 +1132,7 @@ void MainWindow::notifyParametersChanged()
ui_->label_timeRefreshRate->setVisible(false);
}
ui_->actionCamera_from_video_file->setChecked(!Settings::getCamera_5videoFilePath().isEmpty());
ui_->actionCamera_from_video_file->setChecked(!Settings::getCamera_5mediaPath().isEmpty() && !UDirectory::exists(Settings::getCamera_5mediaPath().toStdString()));
ui_->actionCamera_from_directory_of_images->setChecked(!Settings::getCamera_5mediaPath().isEmpty() && UDirectory::exists(Settings::getCamera_5mediaPath().toStdString()));
}

View File

@ -57,13 +57,16 @@ private slots:
void addObjectsFromFiles();
void loadSceneFromFile();
void setupCameraFromVideoFile();
void setupCameraFromImagesDirectory();
void removeObject(ObjWidget * object);
void removeAllObjects();
void updateObjectsSize();
void updateMirrorView();
void showHideControls();
void update(const cv::Mat & image);
void updateObjects();
void notifyParametersChanged();
void moveCameraFrame(int frame);
signals:
void objectsFound(const QMap<int, QPair<QRect, QTransform> > &);

View File

@ -104,6 +104,8 @@ void ParametersToolBox::setupUi()
currentItem->setObjectName(group);
QVBoxLayout * layout = new QVBoxLayout(currentItem);
currentItem->setLayout(layout);
layout->setContentsMargins(0,0,0,0);
layout->setSpacing(0);
layout->addSpacerItem(new QSpacerItem(0,0, QSizePolicy::Minimum, QSizePolicy::Expanding));
addParameter(layout, iter.key(), iter.value());

View File

@ -55,8 +55,8 @@ class Settings
PARAMETER(Camera, 1deviceId, int, 0);
PARAMETER(Camera, 2imageWidth, int, 640);
PARAMETER(Camera, 3imageHeight, int, 480);
PARAMETER(Camera, 4imageRate, int, 2); // Hz
PARAMETER(Camera, 5videoFilePath, QString, "");
PARAMETER(Camera, 4imageRate, double, 2.0); // Hz
PARAMETER(Camera, 5mediaPath, QString, "");
//List format : [Index:item0;item1;item3;...]
PARAMETER(Detector_Descriptor, 1Detector, QString, "7:Dense;Fast;GFTT;MSER;ORB;SIFT;Star;SURF");
@ -155,6 +155,7 @@ class Settings
PARAMETER(General, videoFormats, QString, "*.avi *.m4v *.mp4");
PARAMETER(General, mirrorView, bool, true);
PARAMETER(General, invertedSearch, bool, false);
PARAMETER(General, controlsShown, bool, false);
PARAMETER(Homography, homographyComputed, bool, true);
PARAMETER(Homography, ransacReprojThr, double, 1.0);

View File

@ -1,5 +1,8 @@
<RCC>
<qresource prefix="images">
<file>resources/Find-Object.png</file>
<file>resources/TheWorkingGroup_video_pause.ico</file>
<file>resources/TheWorkingGroup_video_play.ico</file>
<file>resources/TheWorkingGroup_video_stop.ico</file>
</qresource>
</RCC>

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

View File

@ -18,7 +18,7 @@
<normaloff>:/images/resources/Find-Object.png</normaloff>:/images/resources/Find-Object.png</iconset>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout" stretch="2">
<layout class="QVBoxLayout" name="verticalLayout" stretch="0,1,0">
<property name="spacing">
<number>0</number>
</property>
@ -26,101 +26,169 @@
<number>0</number>
</property>
<item>
<layout class="QVBoxLayout" name="verticalLayout" stretch="0,1">
<property name="spacing">
<number>0</number>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2" stretch="0,0,0,0,0,0,0">
<item>
<widget class="QLabel" name="label_5">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Camera</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_timeRefreshRate">
<property name="text">
<string> (0 Hz - 0 Hz)</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="label_detectorDescriptorType">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="label_nfeatures">
<property name="text">
<string>0</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_15">
<property name="text">
<string> features</string>
</property>
</widget>
</item>
</layout>
<widget class="QLabel" name="label_5">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Camera</string>
</property>
</widget>
</item>
<item>
<widget class="QFrame" name="frame">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
<widget class="QLabel" name="label_timeRefreshRate">
<property name="text">
<string> (0 Hz - 0 Hz)</string>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="label_detectorDescriptorType">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="label_nfeatures">
<property name="text">
<string>0</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_15">
<property name="text">
<string> features</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="ObjWidget" name="imageView_source" native="true"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QWidget" name="widget_controls" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<property name="spacing">
<number>0</number>
<number>-1</number>
</property>
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="ObjWidget" name="imageView_source" native="true"/>
<widget class="QPushButton" name="pushButton_play">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../resources.qrc">
<normaloff>:/images/resources/TheWorkingGroup_video_play.ico</normaloff>:/images/resources/TheWorkingGroup_video_play.ico</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_pause">
<property name="maximumSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../resources.qrc">
<normaloff>:/images/resources/TheWorkingGroup_video_pause.ico</normaloff>:/images/resources/TheWorkingGroup_video_pause.ico</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_stop">
<property name="maximumSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../resources.qrc">
<normaloff>:/images/resources/TheWorkingGroup_video_stop.ico</normaloff>:/images/resources/TheWorkingGroup_video_stop.ico</iconset>
</property>
</widget>
</item>
<item>
<widget class="QSlider" name="horizontalSlider_frames">
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="minimum">
<number>0</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_frame">
<property name="text">
<string>0</string>
</property>
</widget>
</item>
</layout>
</widget>
@ -160,6 +228,7 @@
<addaction name="actionPause_camera"/>
<addaction name="actionStop_camera"/>
<addaction name="separator"/>
<addaction name="actionCamera_from_directory_of_images"/>
<addaction name="actionCamera_from_video_file"/>
<addaction name="separator"/>
<addaction name="actionRestore_all_default_settings"/>
@ -444,7 +513,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>265</width>
<width>222</width>
<height>557</height>
</rect>
</property>
@ -492,6 +561,9 @@
<height>0</height>
</size>
</property>
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="maximum">
<number>100</number>
</property>
@ -535,6 +607,9 @@
</property>
</action>
<action name="actionAdd_object_from_scene">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Add object from scene...</string>
</property>
@ -585,6 +660,9 @@
</property>
</action>
<action name="actionCamera_from_video_file">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Camera from video file...</string>
</property>
@ -594,6 +672,14 @@
<string>Remove all objects</string>
</property>
</action>
<action name="actionCamera_from_directory_of_images">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Camera from directory of images...</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>

348
src/utilite/UDirectory.cpp Normal file
View File

@ -0,0 +1,348 @@
// Taken from UtiLite library r185 [www.utilite.googlecode.com]
/*
* utilite is a cross-platform library with
* useful utilities for fast and small developing.
* Copyright (C) 2010 Mathieu Labbe
*
* utilite is free library: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* utilite is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "utilite/UDirectory.h"
#ifdef WIN32
#include <Windows.h>
#include <direct.h>
#include <algorithm>
#include <conio.h>
#else
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/dir.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#endif
#include "utilite/UStl.h"
#include "utilite/UFile.h"
#include "utilite/UDirectory.h"
#ifdef WIN32
bool sortCallback(const std::string & a, const std::string & b)
{
return uStrNumCmp(a,b) < 0;
}
#elif __APPLE__
int sortCallback(const void * aa, const void * bb)
{
const struct dirent ** a = (const struct dirent **)aa;
const struct dirent ** b = (const struct dirent **)bb;
return uStrNumCmp((*a)->d_name, (*b)->d_name);
}
#else
int sortCallback( const dirent ** a, const dirent ** b)
{
return uStrNumCmp((*a)->d_name, (*b)->d_name);
}
#endif
UDirectory::UDirectory(const std::string & path, const std::string & extensions)
{
extensions_ = uListToVector(uSplit(extensions, ' '));
path_ = path;
iFileName_ = fileNames_.begin();
this->update();
}
UDirectory::UDirectory(const UDirectory & dir)
{
*this = dir;
}
UDirectory & UDirectory::operator=(const UDirectory & dir)
{
extensions_ = dir.extensions_;
path_ = dir.path_;
fileNames_ = dir.fileNames_;
for(iFileName_=fileNames_.begin(); iFileName_!=fileNames_.end(); ++iFileName_)
{
if(iFileName_->compare(*dir.iFileName_) == 0)
{
break;
}
}
return *this;
}
UDirectory::~UDirectory()
{
}
void UDirectory::setPath(const std::string & path, const std::string & extensions)
{
extensions_ = uListToVector(uSplit(extensions, ' '));
path_ = path;
fileNames_.clear();
iFileName_ = fileNames_.begin();
this->update();
}
void UDirectory::update()
{
if(exists(path_))
{
std::string lastName;
bool endOfDir = false;
if(iFileName_ != fileNames_.end())
{
//Record the last file name
lastName = *iFileName_;
}
else if(fileNames_.size())
{
lastName = *fileNames_.rbegin();
endOfDir = true;
}
fileNames_.clear();
#ifdef WIN32
WIN32_FIND_DATA fileInformation;
HANDLE hFile = ::FindFirstFile((path_+"\\*").c_str(), &fileInformation);
if(hFile != INVALID_HANDLE_VALUE)
{
do
{
fileNames_.push_back(fileInformation.cFileName);
} while(::FindNextFile(hFile, &fileInformation) == TRUE);
::FindClose(hFile);
std::vector<std::string> vFileNames = uListToVector(fileNames_);
std::sort(vFileNames.begin(), vFileNames.end(), sortCallback);
fileNames_ = uVectorToList(vFileNames);
}
#else
int nameListSize;
struct dirent ** nameList = 0;
nameListSize = scandir(path_.c_str(), &nameList, 0, sortCallback);
if(nameList && nameListSize>0)
{
for (int i=0;i<nameListSize;++i)
{
fileNames_.push_back(nameList[i]->d_name);
free(nameList[i]);
}
free(nameList);
}
#endif
//filter extensions...
std::list<std::string>::iterator iter = fileNames_.begin();
bool valid;
while(iter!=fileNames_.end())
{
valid = false;
if(extensions_.size() == 0 &&
iter->compare(".") != 0 &&
iter->compare("..") != 0)
{
valid = true;
}
for(unsigned int i=0; i<extensions_.size(); ++i)
{
if(UFile::getExtension(*iter).compare(extensions_[i]) == 0)
{
valid = true;
break;
}
}
if(!valid)
{
iter = fileNames_.erase(iter);
}
else
{
++iter;
}
}
iFileName_ = fileNames_.begin();
if(!lastName.empty())
{
bool found = false;
for(std::list<std::string>::iterator iter=fileNames_.begin(); iter!=fileNames_.end(); ++iter)
{
if(lastName.compare(*iter) == 0)
{
found = true;
iFileName_ = iter;
break;
}
}
if(endOfDir && found)
{
++iFileName_;
}
else if(endOfDir && fileNames_.size())
{
iFileName_ = --fileNames_.end();
}
}
}
}
bool UDirectory::isValid()
{
return exists(path_);
}
std::string UDirectory::getNextFileName()
{
std::string fileName;
if(iFileName_ != fileNames_.end())
{
fileName = *iFileName_;
++iFileName_;
}
return fileName;
}
void UDirectory::rewind()
{
iFileName_ = fileNames_.begin();
}
bool UDirectory::exists(const std::string & dirPath)
{
bool r = false;
#if WIN32
DWORD dwAttrib = GetFileAttributes(dirPath.c_str());
r = (dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
#else
DIR *dp;
if((dp = opendir(dirPath.c_str())) != NULL)
{
r = true;
closedir(dp);
}
#endif
return r;
}
// return the directory path of the file
std::string UDirectory::getDir(const std::string & filePath)
{
std::string dir = filePath;
int i=dir.size()-1;
for(; i>=0; --i)
{
if(dir[i] == '/' || dir[i] == '\\')
{
//remove separators...
dir[i] = 0;
--i;
while(i>=0 && (dir[i] == '/' || dir[i] == '\\'))
{
dir[i] = 0;
--i;
}
break;
}
else
{
dir[i] = 0;
}
}
if(i<0)
{
dir = ".";
}
else
{
dir.resize(i+1);
}
return dir;
}
std::string UDirectory::currentDir(bool trailingSeparator)
{
std::string dir;
char * buffer;
#ifdef WIN32
buffer = _getcwd(NULL, 0);
#else
buffer = getcwd(NULL, MAXPATHLEN);
#endif
if( buffer != NULL )
{
dir = buffer;
free(buffer);
if(trailingSeparator)
{
dir += separator();
}
}
return dir;
}
bool UDirectory::makeDir(const std::string & dirPath)
{
int status;
#if WIN32
status = _mkdir(dirPath.c_str());
#else
status = mkdir(dirPath.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
#endif
return status==0;
}
bool UDirectory::removeDir(const std::string & dirPath)
{
int status;
#if WIN32
status = _rmdir(dirPath.c_str());
#else
status = rmdir(dirPath.c_str());
#endif
return status==0;
}
std::string UDirectory::homeDir()
{
std::string path;
#if WIN32
char profilePath[250];
ExpandEnvironmentStrings("%userprofile%",profilePath,250);
path = profilePath;
#else
path = getenv("HOME");
#endif
return path;
}
std::string UDirectory::separator()
{
#ifdef WIN32
return "\\";
#else
return "/";
#endif
}

139
src/utilite/UDirectory.h Normal file
View File

@ -0,0 +1,139 @@
// Taken from UtiLite library r185 [www.utilite.googlecode.com]
/*
* utilite is a cross-platform library with
* useful utilities for fast and small developing.
* Copyright (C) 2010 Mathieu Labbe
*
* utilite is free library: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* utilite is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef UDIRECTORY_H
#define UDIRECTORY_H
//#include "utilite/UtiLiteExp.h" // DLL export/import defines
#include <string>
#include <vector>
#include <list>
/**
* Class UDirectory.
*
* This class can be used to get file names in a directory.
*/
class UDirectory
{
public:
/**
* Check if a directory exists.
* @param dirPath the directory path
* @return true if the directory exists
*/
static bool exists(const std::string & dirPath);
/**
* Get the directory path of a file path.
* @param filePath the file path
* @return the directory path of the file
*/
static std::string getDir(const std::string & filePath);
/**
* Get the current directory.
* @param trailingSeparator If true, a '/' is added to the path.
* @return the current directory
*/
static std::string currentDir(bool trailingSeparator = false);
/**
* Make a directory.
* @param dirPath the directory path
* @return true on success, false otherwise.
*/
static bool makeDir(const std::string & dirPath);
/**
* Remove a directory.
* @param dirPath the directory path
* @return true on success, false otherwise.
*/
static bool removeDir(const std::string & dirPath);
/**
* Return the "home" directory.
* @return the directory path.
*/
static std::string homeDir();
/**
* Return \ (Win32) or / (Unix) depending of the platform.
*/
static std::string separator();
public:
/**
* Create a UDirectory object with path initialized to an existing "path" and with filter "extensions".
* @param path the path to an existing directory
* @param extensions filter to get only file names with the extensions specified, format is a
* list of extensions separated by a space: "jpg bmp" get only file names finishing by jpg or bmp.
*/
UDirectory(const std::string & path = "", const std::string & extensions = "");
UDirectory(const UDirectory & dir);
UDirectory & operator=(const UDirectory & dir);
~UDirectory();
/**
* Set path of the directory.
* @param path the new directory path.
*/
void setPath(const std::string & path, const std::string & extensions = "");
/**
* Update indexed file names (if the directory changed).
*/
void update();
/**
* Check is the directory exists.
* @return if directory exists.
*/
bool isValid();
/**
* Get the next file name.
* @return the next file name
*/
std::string getNextFileName();
/**
* Get all file names.
* @see UDirectory()
* @return all the file names in directory matching the set extensions.
*/
const std::list<std::string> & getFileNames() const {return fileNames_;}
/**
* Return the pointer of file names to beginning.
*/
void rewind();
private:
std::string path_;
std::vector<std::string> extensions_;
std::list<std::string> fileNames_;
std::list<std::string>::iterator iFileName_;
};
#endif /* UDIRECTORY_H */

97
src/utilite/UFile.cpp Normal file
View File

@ -0,0 +1,97 @@
// Taken from UtiLite library r185 [www.utilite.googlecode.com]
/*
* utilite is a cross-platform library with
* useful utilities for fast and small developing.
* Copyright (C) 2010 Mathieu Labbe
*
* utilite is free library: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* utilite is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "utilite/UFile.h"
#include <fstream>
#include "utilite/UStl.h"
bool UFile::exists(const std::string &filePath)
{
bool fileExists = false;
std::ifstream in(filePath.c_str(), std::ios::in);
if (in.is_open())
{
fileExists = true;
in.close();
}
return fileExists;
}
long UFile::length(const std::string &filePath)
{
long fileSize = 0;
FILE* fp = 0;
#ifdef _MSC_VER
fopen_s(&fp, filePath.c_str(), "rb");
#else
fp = fopen(filePath.c_str(), "rb");
#endif
if(fp == NULL)
{
return 0;
}
fseek(fp , 0 , SEEK_END);
fileSize = ftell(fp);
fclose(fp);
return fileSize;
}
int UFile::erase(const std::string &filePath)
{
return remove(filePath.c_str());
}
int UFile::rename(const std::string &oldFilePath,
const std::string &newFilePath)
{
return rename(oldFilePath.c_str(), newFilePath.c_str());
}
std::string UFile::getName(const std::string & filePath)
{
std::string fullPath = filePath;
std::string name;
for(int i=fullPath.size()-1; i>=0; --i)
{
if(fullPath[i] == '/' || fullPath[i] == '\\')
{
break;
}
else
{
name.insert(name.begin(), fullPath[i]);
}
}
return name;
}
std::string UFile::getExtension(const std::string &filePath)
{
std::list<std::string> list = uSplit(filePath, '.');
if(list.size())
{
return list.back();
}
return "";
}

137
src/utilite/UFile.h Normal file
View File

@ -0,0 +1,137 @@
// Taken from UtiLite library r185 [www.utilite.googlecode.com]
/*
* utilite is a cross-platform library with
* useful utilities for fast and small developing.
* Copyright (C) 2010 Mathieu Labbe
*
* utilite is free library: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* utilite is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FILE_H
#define FILE_H
//#include "utilite/UtiLiteExp.h" // DLL export/import defines
#include "utilite/UDirectory.h"
#include <string>
/**
* Class UFile.
*
* This class can be used to modify/erase files on hard drive.
*/
class UFile
{
public:
/**
* Check if a file exists.
* @param filePath the file path
* @return true if the file exists, otherwise false.
*/
static bool exists(const std::string &filePath);
/**
* Get the file length.
* @param filePath the file path
* @return long the length of the file in bytes. Return -1 if the file doesn't exist.
*/
static long length(const std::string &filePath);
/**
* Erase a file.
* @param filePath the file path
* @return 0 if success.
*/
static int erase(const std::string &filePath);
/**
* Rename a file.
* @param oldFilePath the old file path
* @param newFilePath the new file path
* @return 0 if success.
*/
static int rename(const std::string &oldFilePath,
const std::string &newFilePath);
/**
* Get the file name from a file path (with extension).
* @param filePath the file path
* @return the file name.
*/
static std::string getName(const std::string & filePath);
static std::string getExtension(const std::string &filePath);
public:
/**
* Create a UFile object with path initialized to an existing file .
* @param path the path to an existing file
*/
UFile(const std::string & path) : path_(path) {}
~UFile() {}
/**
* Check if the file exists. Same as exists().
* @return true if the path exits
*/
bool isValid() {return exists(path_);}
/**
* Check if the file exists.
* @return true if the path exits
*/
bool exists() {return exists(path_);}
/**
* Get the length of the file.
* @return long the length of the file in bytes. Return -1 if the file doesn't exist.
*/
long length() {return length(path_);}
/**
* Rename the file name. The path stays the same.
* @param the new name
*/
int rename(const std::string &newName)
{
std::string ext = this->getExtension();
std::string newPath = UDirectory::getDir(path_) + std::string("/") + newName;
if(ext.size())
{
newPath += std::string(".") + getExtension(path_);
}
int result = rename(path_, newPath);
if(result == 0)
{
path_ = newPath;
}
return result;
}
/**
* Get the file name without the path.
* @return the file name
*/
std::string getName() {return getName(path_);}
/**
* Get the file extension.
* @return the file extension
*/
std::string getExtension() {return getExtension(path_);}
private:
std::string path_;
};
#endif

593
src/utilite/UStl.h Normal file
View File

@ -0,0 +1,593 @@
// Taken from UtiLite library r185 [www.utilite.googlecode.com]
/*
* utilite is a cross-platform library with
* useful utilities for fast and small developing.
* Copyright (C) 2010 Mathieu Labbe
*
* utilite is free library: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* utilite is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef USTL_H
#define USTL_H
#include <list>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <algorithm>
/**
* \file UStl.h
* \brief Wrappers of STL for convenient functions.
*
* All functions you will find here are here
* for the use of STL in a more convenient way.
*/
/**
* Get unique keys from a std::multimap.
* @param mm the multimap
* @return the list which contains unique keys
*/
template<class K, class V>
inline std::list<K> uUniqueKeys(const std::multimap<K, V> & mm)
{
std::list<K> l;
typename std::list<K>::reverse_iterator lastValue;
for(typename std::multimap<K, V>::const_iterator iter = mm.begin(); iter!=mm.end(); ++iter)
{
if(iter == mm.begin() || (iter != mm.begin() && *lastValue != iter->first))
{
l.push_back(iter->first);
lastValue = l.rbegin();
}
}
return l;
}
/**
* Get all keys from a std::multimap.
* @param mm the multimap
* @return the list which contains all keys (may contains duplicated keys)
*/
template<class K, class V>
inline std::list<K> uKeys(const std::multimap<K, V> & mm)
{
std::list<K> l;
for(typename std::multimap<K, V>::const_iterator iter = mm.begin(); iter!=mm.end(); ++iter)
{
l.push_back(iter->first);
}
return l;
}
/**
* Get all values from a std::multimap.
* @param mm the multimap
* @return the list which contains all values (contains values from duplicated keys)
*/
template<class K, class V>
inline std::list<V> uValues(const std::multimap<K, V> & mm)
{
std::list<V> l;
for(typename std::multimap<K, V>::const_iterator iter = mm.begin(); iter!=mm.end(); ++iter)
{
l.push_back(iter->second);
}
return l;
}
/**
* Get values for a specified key from a std::multimap.
* @param mm the multimap
* @param key the key
* @return the list which contains the values of the key
*/
template<class K, class V>
inline std::list<V> uValues(const std::multimap<K, V> & mm, const K & key)
{
std::list<V> l;
std::pair<typename std::multimap<K, V>::const_iterator, typename std::multimap<K, V>::const_iterator> range;
range = mm.equal_range(key);
for(typename std::multimap<K, V>::const_iterator iter = range.first; iter!=range.second; ++iter)
{
l.push_back(iter->second);
}
return l;
}
/**
* Get all keys from a std::map.
* @param m the map
* @return the vector of keys
*/
template<class K, class V>
inline std::vector<K> uKeys(const std::map<K, V> & m)
{
std::vector<K> v(m.size());
int i=0;
for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
{
v[i] = iter->first;
++i;
}
return v;
}
/**
* Get all keys from a std::map.
* @param m the map
* @return the list of keys
*/
template<class K, class V>
inline std::list<K> uKeysList(const std::map<K, V> & m)
{
std::list<K> l;
for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
{
l.push_back(iter->first);
}
return l;
}
/**
* Get all keys from a std::map.
* @param m the map
* @return the set of keys
*/
template<class K, class V>
inline std::set<K> uKeysSet(const std::map<K, V> & m)
{
std::set<K> s;
int i=0;
for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
{
s.insert(s.end(), iter->first);
++i;
}
return s;
}
/**
* Get all values from a std::map.
* @param m the map
* @return the vector of values
*/
template<class K, class V>
inline std::vector<V> uValues(const std::map<K, V> & m)
{
std::vector<V> v(m.size());
int i=0;
for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
{
v[i] = iter->second;
++i;
}
return v;
}
/**
* Get all values from a std::map.
* @param m the map
* @return the list of values
*/
template<class K, class V>
inline std::list<V> uValuesList(const std::map<K, V> & m)
{
std::list<V> l;
for(typename std::map<K, V>::const_iterator iter = m.begin(); iter!=m.end(); ++iter)
{
l.push_back(iter->second);
}
return l;
}
/**
* Get the value of a specified key from a std::map.
* @param m the map
* @param key the key
* @param defaultValue the default value used if the key is not found
* @return the value
*/
template<class K, class V>
inline V uValue(const std::map<K, V> & m, const K & key, const V & defaultValue = V())
{
V v = defaultValue;
typename std::map<K, V>::const_iterator i = m.find(key);
if(i != m.end())
{
v = i->second;
}
return v;
}
/**
* Get the value of a specified key from a std::map. This will
* remove the value from the map;
* @param m the map
* @param key the key
* @param defaultValue the default value used if the key is not found
* @return the value
*/
template<class K, class V>
inline V uTake(std::map<K, V> & m, const K & key, const V & defaultValue = V())
{
V v;
typename std::map<K, V>::iterator i = m.find(key);
if(i != m.end())
{
v = i->second;
m.erase(i);
}
else
{
v = defaultValue;
}
return v;
}
/**
* Get the iterator at a specified position in a std::list. If the position
* is out of range, the result is the end iterator of the list.
* @param list the list
* @param pos the index position in the list
* @return the iterator at the specified index
*/
template<class V>
inline typename std::list<V>::iterator uIteratorAt(std::list<V> & list, const unsigned int & pos)
{
typename std::list<V>::iterator iter = list.begin();
for(unsigned int i = 0; i<pos && iter != list.end(); ++i )
{
++iter;
}
return iter;
}
/**
* Get the iterator at a specified position in a std::list. If the position
* is out of range, the result is the end iterator of the list.
* @param list the list
* @param pos the index position in the list
* @return the iterator at the specified index
*/
template<class V>
inline typename std::list<V>::const_iterator uIteratorAt(const std::list<V> & list, const unsigned int & pos)
{
typename std::list<V>::const_iterator iter = list.begin();
for(unsigned int i = 0; i<pos && iter != list.end(); ++i )
{
++iter;
}
return iter;
}
/**
* Get the iterator at a specified position in a std::vector. If the position
* is out of range, the result is the end iterator of the vector.
* @param v the vector
* @param pos the index position in the vector
* @return the iterator at the specified index
*/
template<class V>
inline typename std::vector<V>::iterator uIteratorAt(std::vector<V> & v, const unsigned int & pos)
{
return v.begin() + pos;
}
/**
* Get the value at a specified position in a std::list. If the position
* is out of range, the result is undefined.
* @param list the list
* @param pos the index position in the list
* @return the value at the specified index
*/
template<class V>
inline V & uValueAt(std::list<V> & list, const unsigned int & pos)
{
typename std::list<V>::iterator iter = uIteratorAt(list, pos);
return *iter;
}
/**
* Get the value at a specified position in a std::list. If the position
* is out of range, the result is undefined.
* @param list the list
* @param pos the index position in the list
* @return the value at the specified index
*/
template<class V>
inline const V & uValueAt(const std::list<V> & list, const unsigned int & pos)
{
typename std::list<V>::const_iterator iter = uIteratorAt(list, pos);
return *iter;
}
/**
* Check if the list contains the specified value.
* @param list the list
* @param value the value
* @return true if the value is found in the list, otherwise false
*/
template<class V>
inline bool uContains(const std::list<V> & list, const V & value)
{
return std::find(list.begin(), list.end(), value) != list.end();
}
/**
* Check if the map contains the specified key.
* @param map the map
* @param key the key
* @return true if the value is found in the map, otherwise false
*/
template<class K, class V>
inline bool uContains(const std::map<K, V> & map, const K & key)
{
return map.find(key) != map.end();
}
/**
* Check if the multimap contains the specified key.
* @param map the map
* @param key the key
* @return true if the value is found in the map, otherwise false
*/
template<class K, class V>
inline bool uContains(const std::multimap<K, V> & map, const K & key)
{
return map.find(key) != map.end();
}
/**
* Insert an item in the map. Contrary to the insert in the STL,
* if the key already exists, the value will be replaced by the new one.
*/
template<class K, class V>
inline void uInsert(std::map<K, V> & map, const std::pair<K, V> & pair)
{
std::pair<typename std::map<K, V>::iterator, bool> inserted = map.insert(pair);
if(inserted.second == false)
{
inserted.first->second = pair.second;
}
}
/**
* Convert a std::list to a std::vector.
* @param list the list
* @return the vector
*/
template<class V>
inline std::vector<V> uListToVector(const std::list<V> & list)
{
return std::vector<V>(list.begin(), list.end());
}
/**
* Convert a std::vector to a std::list.
* @param v the vector
* @return the list
*/
template<class V>
inline std::list<V> uVectorToList(const std::vector<V> & v)
{
return std::list<V>(v.begin(), v.end());
}
/**
* Append a list to another list.
* @param list the list on which the other list will be appended
* @param newItems the list of items to be appended
*/
template<class V>
inline void uAppend(std::list<V> & list, const std::list<V> & newItems)
{
list.insert(list.end(), newItems.begin(), newItems.end());
}
/**
* Get the index in the list of the specified value. S negative index is returned
* if the value is not found.
* @param list the list
* @param value the value
* @return the index of the value in the list
*/
template<class V>
inline int uIndexOf(const std::vector<V> & list, const V & value)
{
int index=-1;
int i=0;
for(typename std::vector<V>::const_iterator iter = list.begin(); iter!=list.end(); ++iter)
{
if(*iter == value)
{
index = i;
break;
}
++i;
}
return index;
}
/**
* Split a string into multiple string around the specified separator.
* Example:
* @code
* std::list<std::string> v = split("Hello the world!", ' ');
* @endcode
* The list v will contain {"Hello", "the", "world!"}
* @param str the string
* @param separator the separator character
* @return the list of strings
*/
inline std::list<std::string> uSplit(const std::string & str, char separator = ' ')
{
std::list<std::string> v;
std::string buf;
for(unsigned int i=0; i<str.size(); ++i)
{
if(str[i] != separator)
{
buf += str[i];
}
else if(buf.size())
{
v.push_back(buf);
buf = "";
}
}
if(buf.size())
{
v.push_back(buf);
}
return v;
}
/**
* Check if a character is a digit.
* @param c the character
* @return if the character is a digit (if c >= '0' && c <= '9')
*/
inline bool uIsDigit(const char c)
{
return c >= '0' && c <= '9';
}
/**
* Split a string into number and character strings.
* Example:
* @code
* std::list<std::string> v = uSplit("Hello 03 my 65 world!");
* @endcode
* The list v will contain {"Hello ", "03", " my ", "65", " world!"}
* @param str the string
* @return the list of strings
*/
inline std::list<std::string> uSplitNumChar(const std::string & str)
{
std::list<std::string> list;
std::string buf;
bool num = false;
for(unsigned int i=0; i<str.size(); ++i)
{
if(uIsDigit(str[i]))
{
if(!num && buf.size())
{
list.push_back(buf);
buf.clear();
}
buf += str[i];
num = true;
}
else
{
if(num)
{
list.push_back(buf);
buf.clear();
}
buf += str[i];
num = false;
}
}
if(buf.size())
{
list.push_back(buf);
}
return list;
}
/**
* Compare two alphanumeric strings. Useful to sort filenames (human-like sorting).
* Example:
* @code
* std::string a = "Image9.jpg";
* std::string b = "Image10.jpg";
* int r = uStrNumCmp(a, b); // r returns -1 (a is smaller than b). In contrast, std::strcmp(a, b) would return 1.
* @endcode
* @param a the first string
* @param b the second string
* @return -1 if a<b, 0 if a=b and 1 if a>b
*/
inline int uStrNumCmp(const std::string & a, const std::string & b)
{
std::vector<std::string> listA;
std::vector<std::string> listB;
listA = uListToVector(uSplitNumChar(a));
listB = uListToVector(uSplitNumChar(b));
unsigned int i;
int result = 0;
for(i=0; i<listA.size() && i<listB.size(); ++i)
{
if(uIsDigit(listA[i].at(0)) && uIsDigit(listB[i].at(0)))
{
//padding if zeros at the beginning
if(listA[i].at(0) == '0' && listB[i].size() < listA[i].size())
{
while(listB[i].size() < listA[i].size())
{
listB[i] += '0';
}
}
else if(listB[i].at(0) == '0' && listA[i].size() < listB[i].size())
{
while(listA[i].size() < listB[i].size())
{
listA[i] += '0';
}
}
if(listB[i].size() < listA[i].size())
{
result = 1;
}
else if(listB[i].size() > listA[i].size())
{
result = -1;
}
else
{
result = listA[i].compare(listB[i]);
}
}
else if(uIsDigit(listA[i].at(0)))
{
result = -1;
}
else if(uIsDigit(listB[i].at(0)))
{
result = 1;
}
else
{
result = listA[i].compare(listB[i]);
}
if(result != 0)
{
break;
}
}
return result;
}
#endif /* USTL_H */