Commit 48633b93 authored by Jan Möbius's avatar Jan Möbius

Plugin Collections for file, type and postprocessor plugins



git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@15803 383ad7c9-94d9-4d36-a494-682f7c89f535
parents
include (plugin)
openflipper_plugin ()
/*===========================================================================*\
* *
* OpenFlipper *
* Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
* www.openflipper.org *
* *
*--------------------------------------------------------------------------- *
* This file is part of OpenFlipper. *
* *
* OpenFlipper is free software: 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 with the *
* following exceptions: *
* *
* If other files instantiate templates or use macros *
* or inline functions from this file, or you compile this file and *
* link it with other files to produce an executable, this file does *
* not by itself cause the resulting executable to be covered by the *
* GNU Lesser General Public License. This exception does not however *
* invalidate any other reasons why the executable file might be *
* covered by the GNU Lesser General Public License. *
* *
* OpenFlipper 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 LesserGeneral Public *
* License along with OpenFlipper. If not, *
* see <http://www.gnu.org/licenses/>. *
* *
\*===========================================================================*/
/*===========================================================================*\
* *
* $Revision$ *
* $LastChangedBy$ *
* $Date$ *
* *
\*===========================================================================*/
#include <QtGui>
#include <QFileInfo>
#include <QSettings>
#include "FileHeightFieldImage.hh"
#include "ImageDialog.hh"
#include "OpenFlipper/BasePlugin/PluginFunctions.hh"
void FileHeightFieldPNGPlugin::initializePlugin() {
}
QString FileHeightFieldPNGPlugin::getLoadFilters() {
QList<QByteArray> imageFormats = QImageReader::supportedImageFormats();
QString formats;
if ( imageFormats.contains("jpeg")) {
formats += "*.jpeg *.jpg ";
}
if ( imageFormats.contains("tiff")) {
formats += "*.tiff ";
}
if ( imageFormats.contains("tif")) {
formats += "*.tif ";
}
if ( imageFormats.contains("png")) {
formats += "*.png";
}
return QString( tr("HeightField PNG files ( ")+ formats + ")" );
};
QString FileHeightFieldPNGPlugin::getSaveFilters() {
return QString( "" );
};
DataType FileHeightFieldPNGPlugin::supportedType() {
DataType type = DATA_TRIANGLE_MESH;
return type;
}
int FileHeightFieldPNGPlugin::loadObject(QString _filename)
{
QFile file(_filename);
if ( !file.exists() ) {
emit log(LOGERR,tr("Unable to load file: ") + _filename);
return -1;
}
QImage image(_filename);
// Prepare for taking specific regions from the file (defaults to full size)
int minX = 0;
int maxX = image.width();
int minY = 0;
int maxY = image.height();
// Try to calculate a reasonable height
double height = image.height() / 100;
if ( OpenFlipper::Options::gui() ) {
ImageDialog* imageDialog = new ImageDialog(image);
imageDialog->heightValue->setValue(height);
int result = imageDialog->exec();
if ( result == QDialog::Accepted) {
minX = imageDialog->minX->value();
maxX = imageDialog->maxX->value();
minY = imageDialog->minY->value();
maxY = imageDialog->maxY->value();
height = imageDialog->heightValue->value();
} else {
return -1;
}
}
int id = -1;
emit addEmptyObject( DATA_TRIANGLE_MESH, id );
TriMeshObject* object = 0;
if(PluginFunctions::getObject( id, object))
{
TriMesh* mesh = object->mesh();
if ( mesh ) {
// Load the data into the mesh.
loadImageAsTriangleMesh(image,mesh,minX ,maxX , minY ,maxY, height);
// Make sure everything is ready
object->update();
// Tell core about update
emit openedFile( id );
// Update viewport
PluginFunctions::viewAll();
}
}
return id;
}
void FileHeightFieldPNGPlugin::loadImageAsTriangleMesh(QImage& _image,TriMesh* _mesh,int _minX , int _maxX , int _minY , int _maxY, double _height) {
const int mWidth = _maxY - _minY;
const int mHeigth = _maxX - _minX;
// Reserve to get reasonable memory usage
_mesh->reserve( mWidth*mHeigth, mWidth*mHeigth * 4, mWidth*mHeigth * 2 );
for ( int i = _minX ; i < _maxX ; ++i ) {
for ( int j = _minY ; j < _maxY ; ++j ) {
const QColor currentColor = _image.pixel(i,j);
double value = std::max(currentColor.redF(),currentColor.blueF());
value = std::max(currentColor.greenF(),value);
_mesh->add_vertex(TriMesh::Point(i,j,-value * _height));
}
}
// Triangulate
for ( int i = 0 ; i < mHeigth - 1 ; ++i ) {
const int upperStart = mWidth * i;
const int lowerStart = mWidth * (i + 1);
for ( int j = 0 ; j < mWidth -1 ; ++j ) {
std::vector<TriMesh::VertexHandle> handles;
handles.push_back( _mesh->vertex_handle(upperStart + j) );
handles.push_back( _mesh->vertex_handle(lowerStart + j + 1) );
handles.push_back( _mesh->vertex_handle(lowerStart + j) );
_mesh->add_face(handles);
handles.clear();
handles.push_back( _mesh->vertex_handle(upperStart + j) );
handles.push_back( _mesh->vertex_handle(upperStart + j + 1) );
handles.push_back( _mesh->vertex_handle(lowerStart + j + 1) );
_mesh->add_face(handles);
}
}
//Calculate some normals
_mesh->update_normals();
}
bool FileHeightFieldPNGPlugin::saveObject(int _id, QString _filename) {
return false;
}
Q_EXPORT_PLUGIN2( fileheightfieldPpngplugin , FileHeightFieldPNGPlugin );
/*===========================================================================*\
* *
* OpenFlipper *
* Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
* www.openflipper.org *
* *
*--------------------------------------------------------------------------- *
* This file is part of OpenFlipper. *
* *
* OpenFlipper is free software: 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 with the *
* following exceptions: *
* *
* If other files instantiate templates or use macros *
* or inline functions from this file, or you compile this file and *
* link it with other files to produce an executable, this file does *
* not by itself cause the resulting executable to be covered by the *
* GNU Lesser General Public License. This exception does not however *
* invalidate any other reasons why the executable file might be *
* covered by the GNU Lesser General Public License. *
* *
* OpenFlipper 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 LesserGeneral Public *
* License along with OpenFlipper. If not, *
* see <http://www.gnu.org/licenses/>. *
* *
\*===========================================================================*/
/*===========================================================================*\
* *
* $Revision$ *
* $LastChangedBy$ *
* $Date$ *
* *
\*===========================================================================*/
#include <QObject>
#include <OpenFlipper/common/Types.hh>
#include <OpenFlipper/BasePlugin/BaseInterface.hh>
#include <OpenFlipper/BasePlugin/FileInterface.hh>
#include <OpenFlipper/BasePlugin/LoadSaveInterface.hh>
#include <OpenFlipper/BasePlugin/LoggingInterface.hh>
#include <ObjectTypes/TriangleMesh/TriangleMesh.hh>
class FileHeightFieldPNGPlugin : public QObject, BaseInterface, FileInterface, LoadSaveInterface, LoggingInterface
{
Q_OBJECT
Q_INTERFACES(FileInterface)
Q_INTERFACES(LoadSaveInterface)
Q_INTERFACES(LoggingInterface)
Q_INTERFACES(BaseInterface)
signals:
void openedFile( int _id );
void addEmptyObject( DataType _type, int& _id);
void load(QString _filename, DataType _type, int& _id);
void save(int _id , QString _filename );
void log(Logtype _type, QString _message);
void log(QString _message);
private slots:
void fileOpened( int /*_id*/ ){};
void noguiSupported( ) {} ;
void initializePlugin();
public :
~FileHeightFieldPNGPlugin() {};
QString name() { return (QString("FileHeightFieldPNG")); };
QString description( ) { return (QString(tr("Load/Save a height field from an png image"))); };
DataType supportedType();
QString getSaveFilters();
QString getLoadFilters();
QWidget* saveOptionsWidget(QString /*_currentFilter*/) { return 0; };
QWidget* loadOptionsWidget(QString /*_currentFilter*/) { return 0; };
public slots:
int loadObject(QString _filename);
bool saveObject(int _id, QString _filename);
QString version() { return QString("1.0"); };
private:
void loadImageAsTriangleMesh(QImage& _image,TriMesh* _mesh,int _minX , int _maxX , int _minY , int _maxY, double _height);
};
/*===========================================================================*\
* *
* OpenFlipper *
* Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
* www.openflipper.org *
* *
*--------------------------------------------------------------------------- *
* This file is part of OpenFlipper. *
* *
* OpenFlipper is free software: 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 with the *
* following exceptions: *
* *
* If other files instantiate templates or use macros *
* or inline functions from this file, or you compile this file and *
* link it with other files to produce an executable, this file does *
* not by itself cause the resulting executable to be covered by the *
* GNU Lesser General Public License. This exception does not however *
* invalidate any other reasons why the executable file might be *
* covered by the GNU Lesser General Public License. *
* *
* OpenFlipper 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 LesserGeneral Public *
* License along with OpenFlipper. If not, *
* see <http://www.gnu.org/licenses/>. *
* *
\*===========================================================================*/
/*===========================================================================*\
* *
* $Revision: 13581 $ *
* $LastChangedBy: moebius $ *
* $Date: 2012-01-30 14:22:52 +0100 (Mo, 30 Jan 2012) $ *
* *
\*===========================================================================*/
#include "ImageDialog.hh"
#include <QtGui>
#include <iostream>
ImageDialog::ImageDialog(QImage _image, QWidget *parent) :
QDialog(parent),
imageWidth_(_image.width() ),
imageHeight_(_image.height() ),
dragging_(false)
{
setupUi(this);
pixmap_ = QPixmap::fromImage(_image).scaled(QSize(400,400),Qt::KeepAspectRatio,Qt::SmoothTransformation);
image->setPixmap(pixmap_);
// Set the correct boundaries for the spinboxes
minX->setMaximum(imageWidth_);
maxX->setMaximum(imageWidth_);
maxX->setValue(imageWidth_);
minY->setMaximum(imageHeight_);
maxY->setMaximum(imageHeight_);
maxY->setValue(imageHeight_);
// Connect the spin boxes
connect(minX,SIGNAL(valueChanged(int )),this,SLOT(slidersChanged()));
connect(maxX,SIGNAL(valueChanged(int )),this,SLOT(slidersChanged()));
connect(minY,SIGNAL(valueChanged(int )),this,SLOT(slidersChanged()));
connect(maxY,SIGNAL(valueChanged(int )),this,SLOT(slidersChanged()));
connect(image,SIGNAL(mouseButtonMoveEvent ( QPoint )) ,this,SLOT(mouseButtonMoveEvent ( QPoint )));
connect(image,SIGNAL(mouseButtonPressEvent ( QPoint )) ,this,SLOT(mouseButtonPressEvent ( QPoint )));
connect(image,SIGNAL(mouseButtonReleaseEvent ( QPoint )),this,SLOT(mouseButtonReleaseEvent ( QPoint )));
setMouseTracking ( true);
grabMouse();
}
void ImageDialog::slidersChanged() {
// Copy to internal buffer
buffer_ = pixmap_;
QPainter painter( &buffer_);
QPen pen(Qt::red);
pen.setWidth(2);
painter.setPen( pen );
painter.setBrush(QBrush(Qt::red,Qt::NoBrush));
const int minXVal = minX->value();
const int maxXVal = maxX->value();
const int minYVal = minY->value();
const int maxYVal = maxY->value();
// Calculate the scaling factor that is used to fit the image to the widget
const double scale = (double)pixmap_.width() / (double)imageWidth_;
// Working on the actual pixmap -> no offsets!
painter.drawRect( minXVal * scale , minYVal * scale , (maxXVal-minXVal) * scale, (maxYVal-minYVal) * scale);
painter.end();
image->setPixmap(buffer_);
}
void ImageDialog::mouseButtonMoveEvent ( QPoint _p) {
if ( dragging_ ) {
// Calculate the right values for the position.
// The image is scaled to 400x400
// But the aspect ratio is kept. So we need to calculate the new sizes.
// Calculate the free space above and before the real image
// As we are working on the pixmap widget, we need to consider these offsets to calculate
// coordinates inside the real pixmap.
int xoffset = (400 - image->pixmap()->width() ) / 2;
int yoffset = (400 - image->pixmap()->height()) / 2;
// Calculate the scaling factor that is used to fit the image to the widget
const double scale = (double)imageWidth_ / (double)image->pixmap()->width();
int newXMin = (std::min(dragStartPoint_.x(),_p.x()) - xoffset ) * scale;
int newXMax = (std::max(dragStartPoint_.x(),_p.x()) - xoffset ) * scale;
int newYMin = (std::min(dragStartPoint_.y(),_p.y()) - yoffset ) * scale;
int newYMax = (std::max(dragStartPoint_.y(),_p.y()) - yoffset ) * scale;
minX->blockSignals(true);
maxX->blockSignals(true);
minY->blockSignals(true);
maxY->blockSignals(true);
minX->setValue(newXMin);
maxX->setValue(newXMax);
minY->setValue(newYMin);
maxY->setValue(newYMax);
minX->blockSignals(false);
maxX->blockSignals(false);
minY->blockSignals(false);
maxY->blockSignals(false);
slidersChanged();
}
}
void ImageDialog::mouseButtonPressEvent ( QPoint _p ) {
dragStartPoint_ = _p;
dragging_ = true;
}
void ImageDialog::mouseButtonReleaseEvent ( QPoint _p ) {
dragging_ = false;
}
/*===========================================================================*\
* *
* OpenFlipper *
* Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
* www.openflipper.org *
* *
*--------------------------------------------------------------------------- *
* This file is part of OpenFlipper. *
* *
* OpenFlipper is free software: 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 with the *
* following exceptions: *
* *
* If other files instantiate templates or use macros *
* or inline functions from this file, or you compile this file and *
* link it with other files to produce an executable, this file does *
* not by itself cause the resulting executable to be covered by the *
* GNU Lesser General Public License. This exception does not however *
* invalidate any other reasons why the executable file might be *
* covered by the GNU Lesser General Public License. *
* *
* OpenFlipper 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 LesserGeneral Public *
* License along with OpenFlipper. If not, *
* see <http://www.gnu.org/licenses/>. *
* *
\*===========================================================================*/
/*===========================================================================*\
* *
* $Revision: 13581 $ *
* $LastChangedBy: moebius $ *
* $Date: 2012-01-30 14:22:52 +0100 (Mo, 30 Jan 2012) $ *
* *
\*===========================================================================*/
#include "ui_ImageDialog.hh"
#include <QtGui>
class ImageDialog : public QDialog, public Ui::ImageDialog
{
Q_OBJECT
public:
ImageDialog(QImage _image, QWidget *parent = 0);
public slots:
void slidersChanged();
private slots:
void mouseButtonMoveEvent ( QPoint _p );
void mouseButtonPressEvent ( QPoint _p );
void mouseButtonReleaseEvent ( QPoint _p );
private:
int imageWidth_;
int imageHeight_;
QPixmap pixmap_;
QPixmap buffer_;
bool dragging_;
QPoint dragStartPoint_;
};
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ImageDialog</class>
<widget class="QDialog" name="ImageDialog">
<property name="windowModality">
<enum>Qt::ApplicationModal</enum>
</property>
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>482</width>
<height>511</height>
</rect>
</property>
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<property name="sizeGripEnabled">
<bool>false</bool>
</property>
<property name="modal">
<bool>false</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QClickableLabel" name="image">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>400</width>
<height>400</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>400</width>
<height>400</height>
</size>
</property>
<property name="mouseTracking">
<bool>true</bool>
</property>
<property name="text">
<string>Image Placeholder</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QSpinBox" name="minY"/>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QSpinBox" name="maxY"/>
</item>
</layout>
</item>