42 #include "FileHeightFieldImage.hh" 43 #include "ImageDialog.hh" 45 #include <QImageReader> 52 QList<QByteArray> imageFormats = QImageReader::supportedImageFormats();
55 if ( imageFormats.contains(
"jpeg")) {
56 formats +=
"*.jpeg *.jpg ";
59 if ( imageFormats.contains(
"tiff")) {
63 if ( imageFormats.contains(
"tif")) {
67 if ( imageFormats.contains(
"png")) {
71 return QString( tr(
"HeightField PNG files ( ")+ formats +
")" );
83 bool FileHeightFieldPNGPlugin::showImageDialog(
const QImage& _image,
int* _minX,
int* _maxX,
int* _minY,
int* _maxY,
double* _height)
86 imageDialog.heightValue->setValue(*_height);
88 int result = imageDialog.exec();
90 if ( result == QDialog::Accepted) {
91 *_minX = imageDialog.minX->value();
92 *_maxX = imageDialog.maxX->value();
93 *_minY = imageDialog.minY->value();
94 *_maxY = imageDialog.maxY->value();
95 *_height = imageDialog.heightValue->value();
103 int FileHeightFieldPNGPlugin::loadObject(QString _filename)
106 QFile file(_filename);
107 if ( !file.exists() ) {
108 emit log(
LOGERR,tr(
"Unable to load file: ") + _filename);
112 QImage image(_filename);
116 int maxX = image.width();
119 int maxY = image.height();
122 double height = image.height() / 100;
124 if ( OpenFlipper::Options::gui() ) {
127 QMetaObject::invokeMethod(
this,
"showImageDialog",
129 (QThread::currentThread() != QApplication::instance()->thread()) ? Qt::BlockingQueuedConnection: Qt::DirectConnection,
130 Q_RETURN_ARG(
bool, ret),
131 Q_ARG(
const QImage&, image),
136 Q_ARG(
double*,&height));
148 TriMesh* mesh =
object->mesh();
154 loadImageAsTriangleMesh(image,mesh,minX ,maxX , minY ,maxY, height);
160 emit openedFile(
id );
168 void FileHeightFieldPNGPlugin::loadImageAsTriangleMesh(QImage& _image,TriMesh* _mesh,
int _minX ,
int _maxX ,
int _minY ,
int _maxY,
double _height) {
170 const int mWidth = _maxY - _minY;
171 const int mHeigth = _maxX - _minX;
174 _mesh->reserve( mWidth*mHeigth, mWidth*mHeigth * 4, mWidth*mHeigth * 2 );
176 for (
int i = _minX ; i < _maxX ; ++i ) {
177 for (
int j = _minY ; j < _maxY ; ++j ) {
178 const QColor currentColor = _image.pixel(i,j);
179 double value = std::max(currentColor.redF(),currentColor.blueF());
180 value = std::max(currentColor.greenF(),value);
182 _mesh->set_color(vh,
TriMesh::Color(currentColor.redF(),currentColor.greenF(),currentColor.blueF(),1.0f));
187 for (
int i = 0 ; i < mHeigth - 1 ; ++i ) {
188 const int upperStart = mWidth * i;
189 const int lowerStart = mWidth * (i + 1);
191 for (
int j = 0 ; j < mWidth -1 ; ++j ) {
192 std::vector<TriMesh::VertexHandle> handles;
193 handles.push_back( _mesh->vertex_handle(upperStart + j) );
194 handles.push_back( _mesh->vertex_handle(lowerStart + j + 1) );
195 handles.push_back( _mesh->vertex_handle(lowerStart + j) );
198 _mesh->add_face(handles);
202 handles.push_back( _mesh->vertex_handle(upperStart + j) );
203 handles.push_back( _mesh->vertex_handle(upperStart + j + 1) );
204 handles.push_back( _mesh->vertex_handle(lowerStart + j + 1) );
206 _mesh->add_face(handles);
211 _mesh->update_normals();
215 bool FileHeightFieldPNGPlugin::saveObject(
int _id, QString _filename) {
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
Kernel::Point Point
Coordinate type.
Kernel::Color Color
Color type.
DataType supportedType()
Return your supported object type( e.g. DATA_TRIANGLE_MESH )
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
Type for a MeshObject containing a triangle mesh.
#define DATA_TRIANGLE_MESH
void initializePlugin()
Initialize Plugin.